File: w3m-namazu.el

package info (click to toggle)
w3m-el 1.4.538+0.20141022-5
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 7,416 kB
  • ctags: 3,724
  • sloc: lisp: 44,834; sh: 486; makefile: 404
file content (278 lines) | stat: -rw-r--r-- 8,913 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
;;; w3m-namazu.el --- The add-on program to search files with Namazu.

;; Copyright (C) 2001, 2002, 2003, 2004, 2005, 2007, 2009
;; TSUCHIYA Masatoshi <tsuchiya@namazu.org>

;; Author: TSUCHIYA Masatoshi <tsuchiya@namazu.org>
;; Keywords: w3m, WWW, hypermedia, namazu

;; This file is a part of emacs-w3m.

;; This program 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, or (at your option)
;; any later version.

;; This program 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 this program; see the file COPYING.  If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.


;;; Commentary:

;; w3m-namazu.el is the add-on program of emacs-w3m to search files
;; with Namazu.  For more detail about emacs-w3m, see:
;;
;;    http://emacs-w3m.namazu.org/


;;; History:

;; Original program was posted by
;; Takayuki Arakawa <takayu@pop02.odn.ne.jp> in [emacs-w3m:01340] at
;; Jul 31, 2001.

;; Many codes are imported from namazu.el written by
;; Yukihiro Matsumoto <matz@netlab.co.jp> et al.

;; All stuffs are rewritten by
;; TSUCHIYA Masatoshi <tsuchiya@namazu.org> at Aug 2, 2001.


;;; Code:

(eval-when-compile
  (require 'cl))

(require 'w3m)

(eval-and-compile
  (autoload 'w3m-search-read-query "w3m-search"))

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

(defcustom w3m-namazu-command "namazu"
  "*Name of the executable file of Namazu."
  :group 'w3m-namazu
  :type '(string :size 0))

(defcustom w3m-namazu-arguments
  '("-h"			; print in HTML format.
    "-H"			; print further result links.
    "-n" w3m-namazu-page-max	; set number of documents shown to NUM.
    "-w" whence)		; set first number of documents shown to NUM.
  "*Arguments of Namazu."
  :group 'w3m-namazu
  :type '(repeat
	  (restricted-sexp :format "Argument: %v\n"
			   :match-alternatives
			   (stringp 'w3m-namazu-page-max 'whence)
			   :size 0)))

(defcustom w3m-namazu-page-max
  (if (boundp 'namazu-search-num)
      (symbol-value 'namazu-search-num)
    30)
  "*A maximum number of documents which are retrieved by one-time search."
  :group 'w3m-namazu
  :type '(integer :size 0))

(defconst w3m-namazu-default-index-customize-spec
  '`(choice
     (const :tag "No default index" nil)
     ,@(mapcar (lambda (x) (list 'const (car x)))
	       w3m-namazu-index-alist)
     (directory :format "Index directory: %v\n" :size 0)))

(defcustom w3m-namazu-index-alist
  (when (boundp 'namazu-dir-alist)
    (mapcar (lambda (pair)
	      (cons (car pair)
		    (split-string (cdr pair))))
	    (symbol-value 'namazu-dir-alist)))
  "*Alist of alias and index directories."
  :group 'w3m-namazu
  :type '(repeat
	  (group
	   :indent 0 :inline t
	   (cons :format "%v"
		 (string :format "Alias: %v\n" :size 0)
		 (repeat
		  :format "%v%i\n" :indent 8
		  (directory :format "Index directory: %v\n" :size 0)))))
  :set (lambda (symbol value)
	 (custom-set-default symbol value)
	 (put 'w3m-namazu-default-index 'custom-type
	      (eval w3m-namazu-default-index-customize-spec))))

(defcustom w3m-namazu-default-index
  (unless (and (boundp 'namazu-always-query-index-directory)
	       (symbol-value 'namazu-always-query-index-directory))
    (when (boundp 'namazu-default-dir)
      (symbol-value 'namazu-default-dir)))
  "*Alias or directory of the default index.
If this variable equals nil, it is required to input an index path
whenever `w3m-namazu' is called interactively without prefix
argument."
  :group 'w3m-namazu
  :type (eval w3m-namazu-default-index-customize-spec))

(defcustom w3m-namazu-output-coding-system
  (if (boundp 'namazu-cs-write)
      (symbol-value 'namazu-cs-write)
    (if (memq system-type '(OS/2 emx windows-nt))
	'shift_jis-dos
      'euc-japan-unix))
  "*Coding system for namazu process."
  :group 'w3m-namazu
  :type '(coding-system :size 0))

(defcustom w3m-namazu-input-coding-system
  (if (boundp 'namazu-cs-read)
      (symbol-value 'namazu-cs-read)
    'undecided)
  "*Coding system for namazu process."
  :group 'w3m-namazu
  :type '(coding-system :size 0))


(defun w3m-namazu-call-process (index query whence)
  (setq index (if (assoc index w3m-namazu-index-alist)
		  (mapcar 'expand-file-name
			  (cdr (assoc index w3m-namazu-index-alist)))
		(list (expand-file-name index))))
  (let ((file-name-coding-system w3m-file-name-coding-system)
	(coding-system-for-read w3m-namazu-input-coding-system)
	(coding-system-for-write w3m-namazu-output-coding-system)
	(default-process-coding-system
	  (cons w3m-namazu-input-coding-system
		w3m-namazu-output-coding-system)))
    (apply 'call-process w3m-namazu-command nil t nil
	   (let ((w3m-namazu-page-max
		  (number-to-string w3m-namazu-page-max)))
	     (nconc (mapcar 'eval w3m-namazu-arguments)
		    (list query)
		    index)))))

;;;###autoload
(defun w3m-about-namazu (url &optional no-decode no-cache &rest args)
  (let (index query (whence "0"))
    (when (string-match "\\`about://namazu/\\?" url)
      (dolist (s (split-string (substring url (match-end 0)) "&"))
	(when (string-match "\\`\\(?:index\\|\\(query\\)\\|\\(whence\\)\\)=" s)
	  (set (cond
		((match-beginning 1) 'query)
		((match-beginning 2) 'whence)
		(t 'index))
	       (substring s (match-end 0)))))
      (when (zerop (w3m-namazu-call-process (w3m-url-decode-string index)
					    (w3m-url-decode-string query)
					    whence))
	(let ((case-fold-search t))
	  (goto-char (point-min))
	  (let ((max (if (re-search-forward
			  "<!-- HIT -->\\([0-9]+\\)<!-- HIT -->" nil t)
			 (string-to-number (match-string 1))
		       0))
		(cur (string-to-number whence)))
	    (goto-char (point-min))
	    (when (search-forward "<head>" nil t)
	      (when (> cur 0)
		(insert
		 (format "
<link rel=\"prev\" href=\"about://namazu/?index=%s&query=%s&whence=%d\">"
			 index
			 query
			 (max (- cur w3m-namazu-page-max) 0))))
	      (when (> max (+ cur w3m-namazu-page-max))
		(insert
		 (format "
<link rel=\"next\" href=\"about://namazu/?index=%s&query=%s&whence=%d\">"
			 index
			 query
			 (+ cur w3m-namazu-page-max))))))
	  (goto-char (point-min))
	  (while (search-forward "<a href=\"/" nil t)
	    (forward-char -1)
	    (insert "file://"))
	  (goto-char (point-min))
	  (while (re-search-forward "<a href=\"\\(\\?\\)&\\(?:amp;\\)?whence="
				    nil t)
	    (goto-char (match-beginning 1))
	    (delete-char 1)
	    (insert (format "about://namazu/?index=%s&query=%s" index query))))
	"text/html"))))

(defun w3m-namazu-complete-index (index predicate flag)
  "Function to complete index name"
  (if (eq flag 'lambda)
      (and (or (and (assoc index w3m-namazu-index-alist) t)
	       (file-directory-p index))
	   (or (not predicate)
	       (funcall predicate index)))
    (let ((alist
	   (mapcar
	    'list
	    (nconc
	     (all-completions index w3m-namazu-index-alist)
	     (let ((partial (file-name-nondirectory index))
		   (dir (file-name-as-directory
			 (or (file-name-directory index)
			     default-directory))))
	       (delq nil
		     (mapcar
		      (lambda (file)
			(when (file-directory-p (expand-file-name file dir))
			  (concat dir file)))
		      (file-name-all-completions partial dir))))))))
      (cond
       ((not flag) (try-completion index alist predicate))
       ((eq flag t) (all-completions index alist predicate))))))

(defvar w3m-namazu-index-history nil)
(defvar w3m-namazu-query-history nil)

;;;###autoload
(defun w3m-namazu (index query &optional reload)
  "Search indexed files with Namazu."
  (interactive
   (list
    (if (if w3m-namazu-default-index
	    current-prefix-arg
	  (not (and current-prefix-arg
		    w3m-namazu-index-history)))
	(let* ((default (or (car w3m-namazu-index-history)
			    w3m-namazu-default-index))
	       (s (completing-read
		   (if default
		       (format "Namazu index (default %s): " default)
		     "Namazu index: ")
		   'w3m-namazu-complete-index nil t nil
		   'w3m-namazu-index-history)))
	  (if (string= s "") default s))
      (or w3m-namazu-default-index
	  (car w3m-namazu-index-history)))
    (w3m-search-read-query "Namazu query: " "Namazu query (default %s): "
			   'w3m-namazu-query-history)
    current-prefix-arg))
  (unless (stringp index)
    (error "%s" "Index is required"))
  (unless (stringp query)
    (error "%s" "Query is required"))
  (w3m-goto-url (format "about://namazu/?index=%s&query=%s&whence=0"
			(w3m-url-encode-string index)
			(w3m-url-encode-string query))
		reload))

(provide 'w3m-namazu)

;;; w3m-namazu.el ends here