[Date Prev][Date Next][Thread Prev][][Date Index][Thread Index]

w3m-namazu.el



はじめまして 荒川と申します

emacsでw3mが使えるとの話を聞き試してみたら、
これがとても快適だったので、このMLの方にも参加させてもらう事にしました。

自分も少しは emacs & w3m の開発に協力しようと
emacs-LISPを覚えつつ、w3m-namazu.elというものを作りましたので公開します。

このw3m-namazu.elはどういうものかというと
まぁ、名前の通りなのですが
namazuの検索結果をw3m.elで見るのに便利なマイナーモードです。

詳しくは
http://www22.freeweb.ne.jp/computer/pop-club/emacs/w3m-namazu/
にホームページを作りましたので、参考にしてください

Takayuki Arakawa
e-mail: takayu@pop02.odn.ne.jp
        takayu-arakawa@ezweb.ne.jp
URL:    http://www22.freeweb.ne.jp/computer/pop-club/
;;;; w3m-namazu.el --- Namazu interface with w3m  -*-Emacs-Lisp-*-

;; Author: Takayuki Arakawa <takayu@pop02.odn.ne.jp>
;;         Masayuki Ataka   <ataka@milk.freemail.ne.jp>
;; Time-stamp: <2001-07-30 09:43:48 takayu>

;;; Commentary:

;; w3m-namazu.el is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation; either version 2 of the
;; License, or (at your option) any later version.

;; w3m-namazu.el is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;; General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with w3m-namazu.el; if not, write to the Free Software
;; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
;; USA

;;; Install:

;; Please put this file to appropriate directory where the load-path
;; exists, and if you want byte-compile it.  And add following lisp
;; expressions to your ~/.emacs.
;;
;; (autoload 'w3m-namazu-minor-mode "w3m-namazu" "*Namazu interface with w3m." t)

;;; Code:

(require 'w3m)

(defgroup w3m-namazu nil
  "w3m-Namazu front-end for Emacs."
  :group 'w3m)

(defcustom w3m-namazu-command "namazu"
  "*Program name of Namazu."
  :type 'string
  :group 'w3m-namazu)

(defcustom w3m-namazu-page-max 10
  "*Namazu topics' max number in buffer."
  :type 'integer
  :group 'w3m-namazu)

(defcustom w3m-namazu-tmp-file-name "~/.nmz.html"
  "*Namazu tmp file name."
  :type 'file
  :group 'w3m-namazu)

(defcustom w3m-namazu-index-file "~/w3m-namazu.index"
  "*Namazu index file name.
The context of this file is the list of target and Namazu index path
like this...

((\"TARGET1\" . \"PATH/TO/INDEX1\")
 (\"TARGET2\" . \"PATH/TO/INDEX2\")
 ...)

TARGET is the tag name for long path name.
PATH is the path to the directory where the Namazu index exist."
  :type 'file
  :group 'w3m-namazu)

(defcustom w3m-namazu-default-target nil
  "*Default target that namazu searches.
If nil the first item in the w3m-namazu-index-file is chosen."
  :type '(choice string (const nil))
  :group 'w3m-namazu)

(defvar w3m-namazu-hook nil)
(defvar w3m-namazu-minor-mode-hook nil)
(defvar w3m-namazu-keymap-hook nil)

(defvar w3m-namazu-minor-mode nil
  "minor mode for displaing the result of namazu with w3m")
(defvar w3m-namazu-query nil)
(defvar w3m-namazu-index nil)
(defvar w3m-namazu-list-begin 0)


(defun w3m-namazu-minor-mode (&optional arg)
  "\\<w3m-namazu-minor-mode-map>
   minor mode for displaing the result of namazu with w3m

   \\[w3m-namazu] start namazu search system with w3m
   \\[w3m-namazu-all-result] display all result 
   \\[w3m-namazu-next-result] display next result
   \\[w3m-namazu-previous-result] display previous result

   \\[w3m-namazu-minor-mode-exit]       exit w3m-namazu-minor-mode

   "
  (interactive "P")
;;  (w3m-namazu-index-load)
  (setq w3m-namazu-minor-mode
	(if (null arg) (not w3m-namazu-minor-mode)
	  (> (prefix-numeric-value arg) 0)))
  (if w3m-namazu-minor-mode
      (run-hooks 'w3m-namazu-minor-mode-hook nil)
    (force-mode-line-update)))

(defun w3m-namazu-minor-mode-exit ()
  "exit w3m-namazu-minor-mode.
Return to w3m mode."
  (interactive)
  (w3m-namazu-minor-mode -1))

(or (assq 'w3m-namazu-minor-mode minor-mode-alist)
    (setq minor-mode-alist
	  (cons
	   '(w3m-namazu-minor-mode "/namazu") minor-mode-alist)))

(defvar w3m-namazu-minor-mode-map
  (let ((map (make-sparse-keymap)))
    (define-key map "q"        'w3m-namazu-minor-mode-exit)
    (define-key map "\C-c\C-s" 'w3m-namazu)
    (define-key map "\C-c\C-a" 'w3m-namazu-all-result)
    (define-key map "\C-c\C-n" 'w3m-namazu-next-result)
    (define-key map "\C-c\C-p" 'w3m-namazu-previous-result)
    (define-key map "\C-c\C-w" 'w3m-wget)
    (run-hooks 'w3m-namazu-keymap-hook)
    map))

(or (assq 'w3m-namazu-minor-mode minor-mode-map-alist)
    (setq minor-mode-map-alist
	  (cons (cons 'w3m-namazu-minor-mode w3m-namazu-minor-mode-map)
		minor-mode-map-alist)))

(defun w3m-namazu (&optional arg)
  (interactive "P")
   (let* ((ialist (w3m-namazu-load-index))
	  (target  (if arg
		       (completing-read "Which Engine? : " ialist nil t)
		     (if w3m-namazu-default-target
			 (if (assoc w3m-namazu-default-target ialist)
			     w3m-namazu-default-target
			   (error "Variable w3m-namazu-default-target value is incorrect."))
		       (caar ialist))))
	  (default (thing-at-point 'word))
	  (prompt  (if default
		       (format "%s search (default %s): " target default)
		     (format "%s search: " target)))
	  (query   (read-string prompt nil nil default)))
       (setq w3m-namazu-index-alist ialist
	     w3m-namazu-index       (cdr (assoc target ialist))
	     w3m-namazu-query       query
	     w3m-namazu-list-begin  0)
       (w3m-namazu-call-namazu 'init)))


(defun w3m-namazu-call-namazu (option)
  (let
      ((opt (cond 
	     ((equal option 'init)
	      (concat " -h"		; " --html"
		      " -w 0"		; " --whence=0"
		      " -n " (number-to-string w3m-namazu-page-max))) ; " --max="
	     ((or (equal option 'next) (equal option 'prev))
	      (concat " -h"		; " --html"
		      " -w " (number-to-string w3m-namazu-list-begin) ; " --whence="
		      " -n " (number-to-string w3m-namazu-page-max))) ; " --max="
	     ((equal option 'all)
	      (concat " -h "		; " --html"
		      " -a "))))	; " --all"
       (query (concat " \"" w3m-namazu-query "\" "))
       (index w3m-namazu-index)
       (tmp   w3m-namazu-tmp-file-name))
    (shell-command (concat w3m-namazu-command opt query index " > " tmp))
    ;;; case for wget -U option out of work.
	(w3m-namazu-replace-wget-garbage tmp)
    (w3m-find-file tmp)
    (w3m-reload-this-page)
    (w3m-namazu-minor-mode 1)
    (run-hooks 'w3m-namazu-hook)))

;;; case for wget -U option out of work.
(defun w3m-namazu-replace-wget-garbage (tmp-file)
  (save-excursion
	(set-buffer (find-file-noselect tmp-file))
	(while (search-forward "%257" nil t)
	  (replace-match "%7"))
	(basic-save-buffer)
	(kill-buffer nil)))


(defun w3m-namazu-next-result ()
  (interactive)
  (setq w3m-namazu-list-begin (+ w3m-namazu-list-begin w3m-namazu-page-max))
  (w3m-namazu-call-namazu 'next))

(defun w3m-namazu-previous-result ()
  (interactive)
  (setq w3m-namazu-list-begin (- w3m-namazu-list-begin w3m-namazu-page-max))
  (if (>= w3m-namazu-list-begin 0)
      (w3m-namazu-call-namazu 'prev)
    (setq w3m-namazu-list-begin 0)
    (error "No previous index")))

(defun w3m-namazu-all-result ()
  (interactive)
  (setq w3m-namazu-list-begin (- w3m-namazu-list-begin w3m-namazu-page-max))
  (w3m-namazu-call-namazu 'all))

(defun w3m-wget (directory)
  (interactive "sdirectory: ")
  (shell-command (concat "wget.sh " directory " " w3m-current-url))
  (w3m-reload-this-page))

(defun w3m-namazu-save-index-alist ()
  "Save w3m-namazu-index-alist into w3m-namazu-index-file."
  (save-excursion
    (set-buffer (find-file-noselect w3m-namazu-index-file))
    (erase-buffer)
    (prin1 w3m-namazu-index-alist (current-buffer))
    (basic-save-buffer)
    (kill-buffer nil)))

(defun w3m-namazu-load-index ()
  "Laod w3m-namazu-index-alist from w3m-namazu-index-file."
  (if (not (file-exists-p w3m-namazu-index-file))
      (error "%s does not exist." w3m-namazu-index-file)
    (save-excursion
      (save-restriction
	(set-buffer (find-file-noselect w3m-namazu-index-file))
	(widen)
	(goto-char (point-min))
;;       (condition-case nil
;; 	  (setq w3m-namazu-index-alist
;; 		(read (current-buffer)))
;; 	(error "Confirm the index file (%s), please" w3m-namazu-index-file))
	(prog1
	    (read (current-buffer))
	  (kill-buffer nil))))))


;;;
(provide 'w3m-namazu)

;;; w3m-namazu.el ends here.