1 ;;; gnus-namazu.el --- Search mail with Namazu.
3 ;; Copyright (C) 2000,2001,2002 Tsuchiya Masatoshi <tsuchiya@namazu.org>
5 ;; Author: Tsuchiya Masatoshi <tsuchiya@namazu.org>
6 ;; Keywords: mail searching namazu
8 ;; This file is a part of Semi-Gnus.
10 ;; This program is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; This program is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with this program; if not, you can either send email to this
22 ;; program's maintainer or write to: The Free Software Foundation,
23 ;; Inc.; 59 Temple Place, Suite 330; Boston, MA 02111-1307, USA.
28 ;; This file defines the command to search mails and persistent
29 ;; articles with Namazu and browse its results with Gnus. This module
30 ;; requires the external command, Namazu. Visit the following page
31 ;; for more information.
38 ;; Make index of articles with Namzu before using this module.
40 ;; % mkdir ~/News/namazu
41 ;; % mknmz -a -h -O ~/News/namazu ~/Mail ~/News/cache
43 ;; The first command makes the directory for index files, and the
44 ;; second command generates index files of mails and persistent
47 ;; When you put index files of Namazu into the directory other than
48 ;; the default one (~/News/namazu), it is necessary to put this
49 ;; expression to your ~/.gnus, in order to set the path of index files
50 ;; to `gnus-namazu-index-directories'.
52 ;; (setq gnus-namazu-index-directories
53 ;; (list (expand-file-name "~/namazu")))
55 ;; If you would like to use this module in Gnus (not T-gnus), put this
56 ;; file into the lisp/ directory in the Gnus source tree and run `make
57 ;; install'. And then, put the following expressions into your ~/.gnus.
59 ;; (require 'gnus-namazu)
60 ;; (gnus-namazu-insinuate)
65 ;; In group buffer or in summary buffer, type C-c C-n query RET.
70 (eval-when-compile (require 'cl))
76 ;; It is required for Mule 2.3. See the file Mule23@1934.en.
78 (autoload 'regexp-opt "regexp-opt"))
80 ;; To suppress byte-compile warning.
82 (defvar nnml-directory)
83 (defvar nnml-group-alist)
84 (defvar nnmh-directory)
85 (defvar nnmh-group-alist))
88 (defgroup gnus-namazu nil
89 "Search nnmh and nnml groups in Gnus with Namazu."
92 :prefix "gnus-namazu-")
94 (defcustom gnus-namazu-index-directories
96 (or (and (boundp 'gnus-namazu-index-directory)
97 (symbol-value 'gnus-namazu-index-directory))
98 (and (boundp 'nnir-namazu-index-directory)
99 (symbol-value 'nnir-namazu-index-directory))
100 (expand-file-name "namazu" gnus-directory)))
101 "*Index directory of Namazu."
102 :type '(repeat directory)
105 (defcustom gnus-namazu-command
106 (or (and (boundp 'namazu-command)
107 (symbol-value 'namazu-command))
108 (and (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 (boundp 'MULE) '*sjis* 'shift_jis)
131 (if (boundp 'MULE) '*euc-japan* 'euc-japan))
132 "*Coding system for Namazu process."
136 (defcustom gnus-namazu-need-path-normalization
137 (eq system-type 'windows-nt)
138 "*Non-nil means that outputs of namazu may contain a not normalized path."
142 (defcustom gnus-namazu-case-sensitive-filesystem
143 (not (eq system-type 'windows-nt))
144 "*Non-nil means that the using file system distinguishes cases of characters."
148 (defcustom gnus-namazu-query-highlight t
149 "Non-nil means that queried words is highlighted."
153 (defface gnus-namazu-query-highlight-face
154 '((((type tty pc) (class color))
155 (:background "magenta4" :foreground "cyan1"))
156 (((class color) (background light))
157 (:background "magenta4" :foreground "lightskyblue1"))
158 (((class color) (background dark))
159 (:background "palevioletred2" :foreground "brown4"))
160 (t (:inverse-video t)))
161 "Face used for namazu query matching words."
164 ;;; Internal Variable:
165 (defvar gnus-namazu/group-alist nil
166 "Associative list to map groups in lower case to official names.")
167 (defconst gnus-namazu/group-name-regexp "\\`nnvirtual:namazu-search\\?")
169 ;; Multibyte group name:
171 (fboundp 'gnus-group-decoded-name)
172 (let ((gnus-group-name-charset-group-alist
173 (list (cons gnus-namazu/group-name-regexp gnus-namazu-coding-system)))
174 (query (decode-coding-string
175 (string 27 36 66 52 65 59 122 27 40 66)
176 (if (boundp 'MULE) '*iso-2022-jp* 'iso-2022-7bit))))
177 (not (string-match query
178 (gnus-summary-buffer-name
179 (encode-coding-string
180 (concat "nnvirtual:namazu-search?query=" query)
181 gnus-namazu-coding-system)))))
182 (let (current-load-list)
183 (defadvice gnus-summary-buffer-name
184 (before gnus-namazu-summary-buffer-name activate compile)
185 "Advised by `gnus-namazu' to handle encoded group names."
186 (ad-set-arg 0 (gnus-group-decoded-name (ad-get-arg 0))))))
188 (defmacro gnus-namazu/make-article (group number)
189 `(cons ,group ,number))
190 (defmacro gnus-namazu/article-group (x) `(car ,x))
191 (defmacro gnus-namazu/article-number (x) `(cdr ,x))
193 (defsubst gnus-namazu/indexed-servers ()
194 "Choice appropriate servers from opened ones, and return thier list."
196 (gnus-servers-using-backend 'nnml)
197 (gnus-servers-using-backend 'nnmh)))
199 (defun gnus-namazu/setup ()
200 (and (boundp 'gnus-group-name-charset-group-alist)
201 (not (member (cons gnus-namazu/group-name-regexp
202 gnus-namazu-coding-system)
203 gnus-group-name-charset-group-alist))
204 (let ((pair (assoc gnus-namazu/group-name-regexp
205 gnus-group-name-charset-group-alist)))
207 (setcdr pair gnus-namazu-coding-system)
208 (push (cons gnus-namazu/group-name-regexp
209 gnus-namazu-coding-system)
210 gnus-group-name-charset-group-alist))))
211 (unless gnus-namazu-case-sensitive-filesystem
212 ;; FIXME: The alist to map group names in lower case to real names
213 ;; is reconstructed every when gnus-namazu/setup() is called.
214 ;; This reconstruction make gnus-namazu-search() slow.
215 (setq gnus-namazu/group-alist nil)
216 (dolist (server (gnus-namazu/indexed-servers))
217 (dolist (group (gnus-namazu/request-list server))
218 (let ((name (gnus-group-prefixed-name group server)))
219 (unless (assoc name gnus-namazu/group-alist)
220 (push (cons (downcase name) name) gnus-namazu/group-alist)))))))
222 (defun gnus-namazu/shutdown ()
223 (setq gnus-namazu/group-alist nil))
224 (add-hook 'gnus-exit-gnus-hook 'gnus-namazu/shutdown)
226 (defun gnus-namazu/request-list (server)
227 "Return groups of the server SERVER."
228 (and (memq (car server) '(nnml nnmh))
229 (nnoo-change-server (car server) (nth 1 server) (nthcdr 2 server))
230 (gnus-request-list server)
231 (mapcar (function car)
232 (if (eq 'nnml (car server))
236 (defun gnus-namazu/server-directory (server)
237 "Return the top directory of the server SERVER."
238 (and (memq (car server) '(nnml nnmh))
239 (nnoo-change-server (car server) (nth 1 server) (nthcdr 2 server))
240 (file-name-as-directory
241 (expand-file-name (if (eq 'nnml (car server))
245 ;;; Functions to call Namazu.
246 (defsubst gnus-namazu/normalize-results ()
247 "Normalize file names returned by Namazu in this current buffer."
248 (goto-char (point-min))
250 (when (if gnus-namazu-need-path-normalization
251 (or (not (looking-at "/\\(.\\)|/"))
252 (replace-match "\\1:/"))
253 (eq ?~ (char-after (point))))
254 (insert (expand-file-name
255 (buffer-substring (gnus-point-at-bol) (gnus-point-at-eol))))
256 (delete-region (point) (gnus-point-at-eol)))
259 (defsubst gnus-namazu/call-namazu (query)
260 (let ((coding-system-for-read gnus-namazu-coding-system)
261 (coding-system-for-write gnus-namazu-coding-system)
262 (default-process-coding-system
263 (cons gnus-namazu-coding-system gnus-namazu-coding-system))
264 (file-name-coding-system gnus-namazu-coding-system)
265 (pathname-coding-system gnus-namazu-coding-system))
267 `(,gnus-namazu-command
268 nil ; input from /dev/null
270 nil ; don't redisplay
271 "-q" ; don't be verbose
272 "-a" ; show all matches
273 "-l" ; use list format
274 ,@gnus-namazu-additional-arguments
276 ,@gnus-namazu-index-directories))))
278 (defsubst gnus-namazu/group-prefixed-name (group method)
279 "Return the whole name from GROUP and METHOD."
280 (if gnus-namazu-case-sensitive-filesystem
281 (gnus-group-prefixed-name group method)
282 (let ((name (gnus-group-prefixed-name group method)))
283 (or (cdr (assoc (downcase name) gnus-namazu/group-alist))
286 (defun gnus-namazu/check-cache-group (str)
287 "Get the news group from the partial path STR of the cached article."
288 (if (gnus-use-long-file-name 'not-cache)
291 (dolist (group (gnus-namazu/cache-group-candidates
292 (nnheader-replace-chars-in-string str ?/ ?.)))
293 (when (gnus-gethash group gnus-newsrc-hashtb)
294 (throw 'found-group group))))))
296 (defun gnus-namazu/cache-group-candidates (str)
297 "Regard the string STR as the partial path of the cached article and
298 generate possible group names from it."
299 (if (string-match "_\\(_\\(_\\)?\\)?" str)
300 (let ((prefix (substring str 0 (match-beginning 0)))
301 (suffix (substring str (match-end 0))))
303 ((match-beginning 2) ;; The number of discoverd underscores = 3
305 (gnus-namazu/cache-group-candidates (concat prefix "/__" suffix))
306 (gnus-namazu/cache-group-candidates (concat prefix ".._" suffix))))
307 ((match-beginning 1) ;; The number of discoverd underscores = 2
309 (gnus-namazu/cache-group-candidates (concat prefix "//" suffix))
310 (gnus-namazu/cache-group-candidates (concat prefix ".." suffix))))
311 (t ;; The number of discoverd underscores = 1
312 (gnus-namazu/cache-group-candidates (concat prefix "/" suffix)))))
313 (if (string-match "\\." str)
314 ;; Handle the first occurence of period.
315 (list (concat (substring str 0 (match-beginning 0))
317 (substring str (match-end 0)))
321 (defun gnus-namazu/search (groups query)
323 (let ((exit-status (gnus-namazu/call-namazu query)))
324 (unless (zerop exit-status)
325 (error "Namazu finished abnormally: %d" exit-status))
332 (when (setq dir (gnus-namazu/server-directory s))
333 (cons (file-name-as-directory dir) s)))
334 (gnus-namazu/indexed-servers)))))
335 (topdir-regexp (regexp-opt (mapcar 'car server-alist)))
336 (cache-regexp (concat
338 (file-name-as-directory
339 (expand-file-name gnus-cache-directory)))
340 "\\(.*\\)/\\([0-9]+\\)$")))
341 (gnus-namazu/normalize-results)
342 (goto-char (point-min))
344 (let (server group file)
346 ;; Check the discoverd file is the persistent article.
347 (and (looking-at cache-regexp)
348 (setq file (match-string-no-properties 2)
349 group (gnus-namazu/check-cache-group
350 (match-string-no-properties 1))))
351 ;; Check the discovered file is managed by Gnus servers.
352 (and (looking-at topdir-regexp)
353 (setq file (buffer-substring-no-properties
354 (match-end 0) (gnus-point-at-eol))
355 server (cdr (assoc (match-string-no-properties 0)
357 ;; Check validity of the file name.
358 (string-match "/\\([0-9]+\\)\\'" file)
360 (setq group (substring file 0 (match-beginning 0))
361 file (match-string 1 file))
363 (gnus-namazu/group-prefixed-name
364 (nnheader-replace-chars-in-string group ?/ ?.)
367 (member group groups))
368 (push (gnus-namazu/make-article group (string-to-number file))
371 (nreverse articles)))))
375 (defun gnus-namazu/get-target-groups ()
377 ((eq major-mode 'gnus-group-mode)
381 (gnus-group-process-prefix current-prefix-arg))
383 (prog1 gnus-group-marked (gnus-group-unmark-all-groups)))))
384 ((eq major-mode 'gnus-summary-mode)
385 ;; In Summary buffer.
386 (if current-prefix-arg
387 (list (gnus-read-group "Group: "))
389 (gnus-ephemeral-group-p gnus-newsgroup-name)
390 (string-match gnus-namazu/group-name-regexp gnus-newsgroup-name))
391 (cadr (assq 'gnus-namazu-target-groups
392 (gnus-info-method (gnus-get-info gnus-newsgroup-name))))
393 (list gnus-newsgroup-name))))))
395 (defun gnus-namazu/get-current-query ()
396 (and (eq major-mode 'gnus-summary-mode)
397 (gnus-ephemeral-group-p gnus-newsgroup-name)
398 (string-match gnus-namazu/group-name-regexp gnus-newsgroup-name)
399 (cadr (assq 'gnus-namazu-current-query
400 (gnus-info-method (gnus-get-info gnus-newsgroup-name))))))
402 (defvar gnus-namazu/read-query-original-buffer nil)
403 (defvar gnus-namazu/read-query-prompt nil)
404 (defvar gnus-namazu/read-query-history nil)
406 (defun gnus-namazu/get-current-subject ()
407 (and gnus-namazu/read-query-original-buffer
408 (bufferp gnus-namazu/read-query-original-buffer)
409 (with-current-buffer gnus-namazu/read-query-original-buffer
410 (when (eq major-mode 'gnus-summary-mode)
411 (let ((s (gnus-summary-article-subject)))
412 ;; Remove typically prefixes of mailing lists.
414 "^\\(\\[[^]]*[0-9]+\\]\\|([^)]*[0-9]+)\\)\\s-*" s)
415 (setq s (substring s (match-end 0))))
417 "^\\(Re\\(\\^?\\([0-9]+\\|\\[[0-9]+\\]\\)\\)?:\\s-*\\)+" s)
418 (setq s (substring s (match-end 0))))
419 (when (string-match "\\s-*(\\(re\\|was\\)\\b" s)
420 (setq s (substring s 0 (match-beginning 0))))
423 (defun gnus-namazu/get-current-from ()
424 (and gnus-namazu/read-query-original-buffer
425 (bufferp gnus-namazu/read-query-original-buffer)
426 (with-current-buffer gnus-namazu/read-query-original-buffer
427 (when (eq major-mode 'gnus-summary-mode)
428 (cadr (mail-extract-address-components
430 (gnus-summary-article-header))))))))
432 (defmacro gnus-namazu/minibuffer-prompt-end ()
433 (if (fboundp 'minibuffer-prompt-end)
434 '(minibuffer-prompt-end)
437 (defun gnus-namazu/message (string &rest arguments)
439 gnus-namazu/read-query-prompt
440 (buffer-substring (gnus-namazu/minibuffer-prompt-end)
442 (s2 (apply (function format) string arguments))
447 (message (if (>= w 0)
448 (concat s1 (make-string w ?\ ) s2)
450 (if (sit-for 0.3) (message s1))
453 (defun gnus-namazu/complete-query ()
457 ((and (re-search-backward "\\+\\([a-z]*\\)" nil t)
458 (= pos (match-end 0)))
459 (let* ((partial (match-string 1))
463 (mapcar 'list gnus-namazu-field-keywords))))
466 (gnus-namazu/message "No completions of %s" partial))
467 ((= 1 (length completions))
468 (goto-char (match-beginning 1))
469 (delete-region (match-beginning 1) (match-end 1))
470 (insert (car completions) ":")
472 (gnus-namazu/message "Completed"))
474 (let ((x (try-completion partial (mapcar 'list completions))))
475 (if (string= x partial)
476 (if (and (eq last-command
477 'gnus-namazu/field-keyword-completion)
478 completion-auto-help)
479 (with-output-to-temp-buffer "*Completions*"
480 (display-completion-list completions))
481 (gnus-namazu/message "Sole completion"))
482 (goto-char (match-beginning 1))
483 (delete-region (match-beginning 1) (match-end 1))
485 (setq pos (point))))))))
486 ((and (looking-at "\\+subject:")
487 (= pos (match-end 0)))
488 (let ((s (gnus-namazu/get-current-subject)))
492 (setq pos (point)))))
493 ((and (looking-at "\\+from:")
494 (= pos (match-end 0)))
495 (let ((f (gnus-namazu/get-current-from)))
499 (setq pos (point))))))
502 (defvar gnus-namazu/read-query-map
503 (let ((keymap (copy-keymap minibuffer-local-map)))
504 (define-key keymap "\t" 'gnus-namazu/complete-query)
507 (defun gnus-namazu/read-query (prompt &optional initial)
508 (let ((gnus-namazu/read-query-original-buffer (current-buffer))
509 (gnus-namazu/read-query-prompt prompt))
511 (when (setq initial (gnus-namazu/get-current-query))
512 (setq initial (cons initial 0))))
513 (read-from-minibuffer prompt initial gnus-namazu/read-query-map nil
514 'gnus-namazu/read-query-history)))
516 (defun gnus-namazu/highlight-words (query)
520 "[ \t\r\f\n]*\\(\\(and\\|or\\|\\(not\\)\\)[ \t\r\f\n]+\\)?\
521 \\(\\+[^ \t\r\f\n]+:\\)?\\(/\\([^/]+\\)/\\|\\(\"\\([^\"]+\\)\"\\|\
522 {\\([^{}]+\\)}\\)\\|[^ \t\r\f\n]+\\)" query start)
523 (setq start (match-end 0))
524 (or (match-beginning 3) ; NOT search
525 (match-beginning 4) ; Field search
526 (match-beginning 6) ; Regular expression search
527 (if (match-beginning 7) ; Phrase search
528 (dolist (str (split-string
529 (if (match-beginning 8)
530 (match-string 8 query)
531 (match-string 9 query))))
532 (when (> (length str) 0)
534 (push (match-string 5 query) strings))))
540 (if (string-match "\\`\\*?\\([^\\*]*\\)\\*?\\'" str)
541 (match-string 1 str) str))
543 0 0 'gnus-namazu-query-highlight-face)))))
545 (defun gnus-namazu/truncate-article-list (articles)
546 (let ((hit (length articles)))
547 (when (> hit gnus-large-newsgroup)
548 (let* ((cursor-in-echo-area nil)
550 (when (> hit gnus-large-newsgroup)
551 (read-from-minibuffer
553 "Too many articles were retrieved. How many articles (max %d): "
555 (cons (number-to-string gnus-large-newsgroup) 0)))))
556 (unless (string-match "\\`[ \t]*\\'" input)
557 (setcdr (nthcdr (min (1- (string-to-number input)) hit) articles)
562 (defun gnus-namazu-search (groups query)
563 "Search QUERY through GROUPS with Namazu,
564 and make a virtual group contains its results."
567 (gnus-namazu/get-target-groups)
568 (gnus-namazu/read-query "Enter query: ")))
570 (let ((articles (gnus-namazu/search groups query)))
572 (let ((real-groups groups)
574 (apply (function format)
575 "nnvirtual:namazu-search?query=%s&groups=%s&id=%d%d%d"
577 (if groups (mapconcat 'identity groups ",") "ALL")
579 (gnus-namazu/truncate-article-list articles)
582 (add-to-list 'real-groups (gnus-namazu/article-group a))))
583 ;; Generate virtual group which includes all results.
584 (when (fboundp 'gnus-group-decoded-name)
586 (encode-coding-string vgroup gnus-namazu-coding-system)))
588 (gnus-group-read-ephemeral-group
591 (nnvirtual-component-groups ,real-groups)
592 (gnus-namazu-target-groups ,groups)
593 (gnus-namazu-current-query ,query))
594 t (cons (current-buffer) (current-window-configuration)) t))
595 (when gnus-namazu-query-highlight
596 (gnus-group-set-parameter vgroup 'highlight-words
597 (gnus-namazu/highlight-words query)))
598 ;; Generate new summary buffer which contains search results.
599 (gnus-group-read-group
601 (sort (delq nil ;; Ad-hoc fix, to avoid wrong-type-argument error.
604 (nnvirtual-reverse-map-article
605 (gnus-namazu/article-group a)
606 (gnus-namazu/article-number a)))
609 (message "No entry."))))
611 (defun gnus-namazu-insinuate ()
613 'gnus-group-mode-hook
615 (define-key gnus-group-mode-map "\C-c\C-n" 'gnus-namazu-search)))
617 'gnus-summary-mode-hook
619 (define-key gnus-summary-mode-map "\C-c\C-n" 'gnus-namazu-search))))
621 (provide 'gnus-namazu)
623 ;; gnus-namazu.el ends here.