1 ;;; gnus-namazu.el --- Search mail with Namazu.
3 ;; Copyright (C) 2000,2001 Tsuchiya Masatoshi <tsuchiya@namazu.org>
5 ;; Author: Tsuchiya Masatoshi <tsuchiya@namazu.org>
6 ;; Keywords: mail searching namazu
10 ;; This file is a part of Semi-Gnus.
12 ;; This program is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; This program is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with this program; if not, you can either send email to this
24 ;; program's maintainer or write to: The Free Software Foundation,
25 ;; Inc.; 59 Temple Place, Suite 330; Boston, MA 02111-1307, USA.
30 ;; This file defines the command to search mails with Namazu and
31 ;; browse its results with Gnus. This module requires the external
32 ;; command Namazu. Visit the following page for more information.
34 ;; http://www.namazu.org/
39 ;; Make index of articles with Namzu before using this module.
41 ;; % mkdir ~/News/namazu
42 ;; % mknmz -a -h -O ~/News/namazu ~/Mail
44 ;; Furthermore, put these expressions to your ~/.gnus, to set the path
45 ;; of the index files to `gnus-namazu-index-directories'.
47 ;; (setq gnus-namazu-index-directories
48 ;; (list (expand-file-name "~/News/namazu")))
50 ;; If you would like to use this module in Gnus (not T-gnus), put this
51 ;; file into the lisp/ directory in the Gnus source tree and run `make
52 ;; install'. And then, put the following expressions into your ~/.gnus.
54 ;; (require 'gnus-namazu)
55 ;; (gnus-namazu-insinuate)
60 ;; In group buffer or in summary buffer, type C-c C-n query RET.
65 (eval-when-compile (require 'cl))
71 ;; It is required for Mule 2.3. See the file Mule23@1934.en.
73 (autoload 'regexp-opt "regexp-opt"))
75 ;; To suppress byte-compile warning.
77 (defvar nnml-directory)
78 (defvar nnmh-directory))
80 (defgroup gnus-namazu nil
81 "Search nnmh and nnml groups in Gnus with Namazu."
84 :prefix "gnus-namazu-")
86 (defcustom gnus-namazu-indexed-servers nil
87 "*List of servers indexed with Namazu."
88 :type '(repeat gnus-select-method)
91 (defcustom gnus-namazu-index-directories
94 ((boundp 'nnir-namazu-index-directory)
95 (symbol-value 'nnir-namazu-index-directory))
96 ((boundp 'gnus-namazu-index-directory)
97 (symbol-value 'gnus-namazu-index-directory))
99 (expand-file-name "namazu" gnus-directory))))
100 "*Index directory of Namazu."
101 :type '(repeat directory)
104 (defcustom gnus-namazu-command
106 ((boundp 'namazu-command)
107 (symbol-value 'namazu-command))
108 ((boundp 'nnir-namazu-program)
109 (symbol-value 'nnir-namazu-program))
111 "*Name of the executable file of Namazu."
115 (defcustom gnus-namazu-additional-arguments nil
116 "*Additional arguments of Namazu.
117 The options `-q', `-a', and `-l' are always used, very few other
118 options make any sense in this context."
119 :type '(repeat string)
122 (defcustom gnus-namazu-field-keywords
123 '("date" "from" "newsgroups" "size" "subject" "summary" "to" "uri")
124 "*List of keywords to do field-search."
125 :type '(repeat string)
128 (defcustom gnus-namazu-coding-system
129 (if (memq system-type '(windows-nt OS/2 emx))
130 (if (>= emacs-major-version 20) 'shift_jis '*sjis*)
131 (if (>= emacs-major-version 20) 'euc-japan '*euc-japan*))
132 "*Coding system for Namazu process."
137 (defmacro gnus-namazu/make-article (group number)
138 `(cons ,group ,number))
139 (defmacro gnus-namazu/article-group (x) `(car ,x))
140 (defmacro gnus-namazu/article-number (x) `(cdr ,x))
143 (defun gnus-namazu/setup ()
144 (unless gnus-namazu-indexed-servers
145 (setq gnus-namazu-indexed-servers
147 (mapcar (lambda (method)
148 (when (memq (car method) '(nnml nnmh))
150 (cons gnus-select-method
151 gnus-secondary-select-methods)))))
152 (unless gnus-namazu-indexed-servers
153 (error "%s" "Can't find either nnml backend or nnmh backend"))
154 (when (or (not gnus-namazu-index-directories)
158 (file-directory-p dir)
160 (expand-file-name "NMZ.i" dir))
162 gnus-namazu-index-directories)))
163 (error "%s" "Can't find index. Check `gnus-namazu-index-directories'")))
165 (defun gnus-namazu/server-directory (server)
166 "Return the top directory of articles in SERVER."
167 (and (memq (car server) '(nnml nnmh))
168 (nnoo-change-server (car server) (nth 1 server) (nthcdr 2 server))
169 (file-name-as-directory
170 (expand-file-name (if (eq 'nnml (car server))
174 ;;; Functions to call Namazu.
175 (defsubst gnus-namazu/normalize-results ()
176 (goto-char (point-min))
179 ((eq ?~ (char-after (point)))
180 (insert (expand-file-name (buffer-substring (gnus-point-at-bol)
181 (gnus-point-at-eol))))
182 (delete-region (point) (gnus-point-at-eol)))
183 ((and (eq system-type 'windows-nt)
184 (looking-at "/\\(.\\)|/"))
185 (replace-match "\\1:/")))
188 (defsubst gnus-namazu/call-namazu (query)
189 (let ((coding-system-for-read gnus-namazu-coding-system)
190 (coding-system-for-write gnus-namazu-coding-system)
191 (default-process-coding-system
192 (cons gnus-namazu-coding-system gnus-namazu-coding-system))
193 (file-name-coding-system gnus-namazu-coding-system)
194 (pathname-coding-system gnus-namazu-coding-system))
196 `(,gnus-namazu-command
197 nil ; input from /dev/null
199 nil ; don't redisplay
200 "-q" ; don't be verbose
201 "-a" ; show all matches
202 "-l" ; use list format
203 ,@gnus-namazu-additional-arguments
205 ,@gnus-namazu-index-directories))))
207 (defun gnus-namazu/search (groups query)
209 (when (zerop (gnus-namazu/call-namazu query))
216 (when (setq dir (gnus-namazu/server-directory s))
217 (cons (file-name-as-directory dir) s)))
218 gnus-namazu-indexed-servers))))
220 (regexp-opt (mapcar 'car server-alist) t)))
221 (gnus-namazu/normalize-results)
222 (goto-char (point-min))
224 (let (server group file)
225 (and (looking-at topdir-regexp)
226 (setq server (cdr (assoc (match-string 1) server-alist))
227 file (buffer-substring-no-properties
228 (match-end 0) (point-at-eol)))
229 (string-match "/\\([0-9]+\\)\\'" file)
231 (setq group (substring file 0 (match-beginning 0))
232 file (match-string 1 file))
234 (gnus-group-prefixed-name
235 (nnheader-replace-chars-in-string group ?/ ?.)
237 (when (or (not groups)
238 (member group groups))
239 (push (gnus-namazu/make-article
240 group (string-to-number file))
243 (nreverse articles)))))
246 (defun gnus-namazu/get-target-groups ()
248 ((eq major-mode 'gnus-group-mode)
252 (gnus-group-process-prefix current-prefix-arg))
254 (prog1 gnus-group-marked (gnus-group-unmark-all-groups)))))
255 ((eq major-mode 'gnus-summary-mode)
256 ;; In Summary buffer.
257 (if current-prefix-arg
258 (list (gnus-read-group "Group: "))
259 (if (and (gnus-ephemeral-group-p gnus-newsgroup-name)
260 (string-match "\\`nnvirtual:namazu-search" gnus-newsgroup-name))
261 (cadr (assq 'gnus-namazu-target-groups
262 (gnus-info-method (gnus-get-info gnus-newsgroup-name))))
263 (list gnus-newsgroup-name))))))
265 (defun gnus-namazu/get-current-query ()
266 (and (eq major-mode 'gnus-summary-mode)
267 (gnus-ephemeral-group-p gnus-newsgroup-name)
268 (string-match "\\`nnvirtual:namazu-search" gnus-newsgroup-name)
269 (cadr (assq 'gnus-namazu-current-query
270 (gnus-info-method (gnus-get-info gnus-newsgroup-name))))))
272 (defvar gnus-namazu/read-query-original-buffer nil)
273 (defvar gnus-namazu/read-query-prompt nil)
274 (defvar gnus-namazu/read-query-history nil)
276 (defun gnus-namazu/get-current-subject ()
277 (and gnus-namazu/read-query-original-buffer
278 (bufferp gnus-namazu/read-query-original-buffer)
279 (with-current-buffer gnus-namazu/read-query-original-buffer
280 (when (eq major-mode 'gnus-summary-mode)
281 (let ((s (gnus-summary-article-subject)))
282 ;; Remove typically prefixes of mailing lists.
284 "^\\(\\[[^]]*[0-9]+\\]\\|([^)]*[0-9]+)\\)\\s-*" s)
285 (setq s (substring s (match-end 0))))
287 "^\\(Re\\(\\^?\\([0-9]+\\|\\[[0-9]+\\]\\)\\)?:\\s-*\\)+" s)
288 (setq s (substring s (match-end 0))))
289 (when (string-match "\\s-*(\\(re\\|was\\)\\b" s)
290 (setq s (substring s 0 (match-beginning 0))))
293 (defun gnus-namazu/get-current-from ()
294 (and gnus-namazu/read-query-original-buffer
295 (bufferp gnus-namazu/read-query-original-buffer)
296 (with-current-buffer gnus-namazu/read-query-original-buffer
297 (when (eq major-mode 'gnus-summary-mode)
298 (cadr (mail-extract-address-components
300 (gnus-summary-article-header))))))))
302 (defmacro gnus-namazu/minibuffer-prompt-end ()
303 (if (fboundp 'minibuffer-prompt-end)
304 '(minibuffer-prompt-end)
307 (defun gnus-namazu/message (string &rest arguments)
309 gnus-namazu/read-query-prompt
310 (buffer-substring (gnus-namazu/minibuffer-prompt-end)
312 (s2 (apply (function format) string arguments))
317 (message (if (>= w 0)
318 (concat s1 (make-string w ?\ ) s2)
320 (if (sit-for 0.3) (message s1))
323 (defun gnus-namazu/complete-query ()
327 ((and (re-search-backward "\\+\\([a-z]*\\)" nil t)
328 (= pos (match-end 0)))
329 (let* ((partial (match-string 1))
333 (mapcar 'list gnus-namazu-field-keywords))))
336 (gnus-namazu/message "No completions of %s" partial))
337 ((= 1 (length completions))
338 (goto-char (match-beginning 1))
339 (delete-region (match-beginning 1) (match-end 1))
340 (insert (car completions) ":")
342 (gnus-namazu/message "Completed"))
344 (let ((x (try-completion partial (mapcar 'list completions))))
345 (if (string= x partial)
346 (if (and (eq last-command
347 'gnus-namazu/field-keyword-completion)
348 completion-auto-help)
349 (with-output-to-temp-buffer "*Completions*"
350 (display-completion-list completions))
351 (gnus-namazu/message "Sole completion"))
352 (goto-char (match-beginning 1))
353 (delete-region (match-beginning 1) (match-end 1))
355 (setq pos (point))))))))
356 ((and (looking-at "\\+subject:")
357 (= pos (match-end 0)))
358 (let ((s (gnus-namazu/get-current-subject)))
362 (setq pos (point)))))
363 ((and (looking-at "\\+from:")
364 (= pos (match-end 0)))
365 (let ((f (gnus-namazu/get-current-from)))
369 (setq pos (point))))))
372 (defvar gnus-namazu/read-query-map
373 (let ((keymap (copy-keymap minibuffer-local-map)))
374 (define-key keymap "\t" 'gnus-namazu/complete-query)
377 (defun gnus-namazu/read-query (prompt &optional initial)
378 (let ((gnus-namazu/read-query-original-buffer (current-buffer))
379 (gnus-namazu/read-query-prompt prompt))
381 (when (setq initial (gnus-namazu/get-current-query))
382 (setq initial (cons initial 0))))
383 (read-from-minibuffer prompt initial gnus-namazu/read-query-map nil
384 'gnus-namazu/read-query-history)))
386 (defun gnus-namazu/truncate-article-list (articles)
387 (let ((hit (length articles)))
388 (when (> hit gnus-large-newsgroup)
389 (let* ((cursor-in-echo-area nil)
391 (when (> hit gnus-large-newsgroup)
392 (read-from-minibuffer
394 "Too many articles were retrieved. How many articles (max %d): "
396 (cons (number-to-string gnus-large-newsgroup) 0)))))
397 (unless (string-match "\\`[ \t]*\\'" input)
398 (setcdr (nthcdr (min (1- (string-to-number input)) hit) articles)
402 (defun gnus-namazu-search (groups query)
403 "Search QUERY through GROUPS with Namazu,
404 and make a virtual group contains its results."
407 (gnus-namazu/get-target-groups)
408 (gnus-namazu/read-query "Enter Keyword: ")))
410 (let ((articles (gnus-namazu/search groups query)))
412 (let ((real-groups groups)
415 "nnvirtual:namazu-search?query=%s&groups=%s&id=%d%d%d"
417 (if groups (mapconcat 'identity groups ",") "ALL")
419 (gnus-namazu/truncate-article-list articles)
422 (add-to-list 'real-groups (gnus-namazu/article-group a))))
423 ;; Generate virtual group which includes all results.
425 (gnus-group-read-ephemeral-group
428 (nnvirtual-component-groups ,real-groups)
429 (gnus-namazu-target-groups ,groups)
430 (gnus-namazu-current-query ,query))
431 t (cons (current-buffer) (current-window-configuration)) t))
432 ;; Generate new summary buffer which contains search results.
433 (gnus-group-read-group
435 (sort (delq nil ;; Ad-hoc fix, to avoid wrong-type-argument error.
438 (nnvirtual-reverse-map-article
439 (gnus-namazu/article-group a)
440 (gnus-namazu/article-number a)))
443 (message "No entry."))))
446 (defun gnus-namazu-insinuate ()
448 'gnus-group-mode-hook
450 (define-key gnus-group-mode-map "\C-c\C-n" 'gnus-namazu-search)))
452 'gnus-summary-mode-hook
454 (define-key gnus-summary-mode-map "\C-c\C-n" 'gnus-namazu-search))))
457 (provide 'gnus-namazu)
458 ;; gnus-namazu.el ends here.