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-directory'.
47 ;; (setq gnus-namazu-index-directory (expand-file-name "~/News/namazu"))
49 ;; If you would like to use this module in Gnus (not T-gnus), put this
50 ;; file into the lisp/ directory in the Gnus source tree and run `make
51 ;; install'. And then, put the following expressions into your ~/.gnus.
53 ;; (require 'gnus-namazu)
54 ;; (gnus-namazu-insinuate)
59 ;; In group buffer or in summary buffer, type C-c C-n query RET.
64 (eval-when-compile (require 'cl))
70 ;; It is required for Mule 2.3. See the file Mule23@1934.en.
72 (autoload 'regexp-opt "regexp-opt"))
74 (defgroup gnus-namazu nil
75 "Search nnmh and nnml groups in Gnus with Namazu."
78 :prefix "gnus-namazu-")
80 (defcustom gnus-namazu-indexed-servers nil
81 "*List of servers indexed with Namazu."
82 :type '(repeat gnus-select-method)
85 (defcustom gnus-namazu-index-directory
86 (if (boundp 'nnir-namazu-index-directory)
87 (symbol-value 'nnir-namazu-index-directory)
88 (expand-file-name "namazu" gnus-directory))
89 "*Index directory of Namazu."
93 (defcustom gnus-namazu-command
95 ((boundp 'namazu-command)
96 (symbol-value 'namazu-command))
97 ((boundp 'nnir-namazu-program)
98 (symbol-value 'nnir-namazu-program))
100 "*Name of the executable file of Namazu."
104 (defcustom gnus-namazu-additional-arguments nil
105 "*Additional arguments of Namazu.
106 The options `-q', `-a', and `-l' are always used, very few other
107 options make any sense in this context."
108 :type '(repeat string)
111 (defcustom gnus-namazu-field-keywords
112 '("date" "from" "newsgroups" "size" "subject" "summary" "to" "uri")
113 "*List of keywords to do field-search."
114 :type '(repeat string)
117 (defcustom gnus-namazu-coding-system
118 (if (>= emacs-major-version 20) 'euc-japan '*euc-japan*)
119 "*Coding system for Namazu process."
124 (defmacro gnus-namazu/make-article (group number)
125 `(cons ,group ,number))
126 (defmacro gnus-namazu/article-group (x) `(car ,x))
127 (defmacro gnus-namazu/article-number (x) `(cdr ,x))
130 (defun gnus-namazu/setup ()
131 (unless gnus-namazu-indexed-servers
132 (setq gnus-namazu-indexed-servers
134 (mapcar (lambda (method)
135 (when (memq (car method) '(nnml nnmh))
137 (cons gnus-select-method
138 gnus-secondary-select-methods)))))
139 (unless gnus-namazu-indexed-servers
140 (error "%s" "Can't find either nnml backend or nnmh backend"))
141 (unless (and (stringp gnus-namazu-index-directory)
142 (file-directory-p gnus-namazu-index-directory)
144 (expand-file-name "NMZ.i" gnus-namazu-index-directory)))
145 (error "%s" "Can't find index. Check `gnus-namazu-index-directory'")))
148 ;; To suppress byte-compile warning.
150 (defvar nnml-directory)
151 (defvar nnmh-directory))
153 (defun gnus-namazu/server-directory (server)
154 "Return the top directory of articles in SERVER."
155 (and (memq (car server) '(nnml nnmh))
156 (nnoo-change-server (car server) (nth 1 server) (nthcdr 2 server))
157 (file-name-as-directory
158 (expand-file-name (if (eq 'nnml (car server))
163 ;;; Functions to call Namazu.
164 (defsubst gnus-namazu/call-namazu (query)
165 (let ((coding-system-for-read gnus-namazu-coding-system)
166 (coding-system-for-write gnus-namazu-coding-system)
167 (default-process-coding-system
168 (cons gnus-namazu-coding-system gnus-namazu-coding-system))
170 (copy-sequence process-environment)))
172 (dolist (env process-environment)
173 (when (string-match "\
174 \\`\\(L\\(ANG\\|C_\\(ALL\\|CTYPE\\|COLLATE\\|TIME\\|NUMERIC\\|MONETARY\\|MESSAGES\\)\\)\\)=" env)
175 (setenv (match-string 1 env) nil)))
178 `(,gnus-namazu-command
179 nil ; input from /dev/null
181 nil ; don't redisplay
182 "-q" ; don't be verbose
183 "-a" ; show all matches
184 "-l" ; use list format
185 ,@gnus-namazu-additional-arguments
187 ,gnus-namazu-index-directory))))
189 (defun gnus-namazu/search (groups query)
191 (when (zerop (gnus-namazu/call-namazu query))
198 (when (setq dir (gnus-namazu/server-directory s))
199 (cons (file-name-as-directory dir) s)))
200 gnus-namazu-indexed-servers))))
202 (regexp-opt (mapcar 'car server-alist) t)))
203 (goto-char (point-min))
205 (let (server group file)
206 (and (looking-at topdir-regexp)
207 (setq server (cdr (assoc (match-string 1) server-alist))
208 file (buffer-substring-no-properties
209 (match-end 0) (point-at-eol)))
210 (string-match "/\\([0-9]+\\)$" file)
212 (setq group (substring file 0 (match-beginning 0))
213 file (match-string 1 file))
215 (gnus-group-prefixed-name
216 (nnheader-replace-chars-in-string group ?/ ?.)
218 (when (or (not groups)
219 (member group groups))
220 (push (gnus-namazu/make-article
221 group (string-to-number file))
224 (nreverse articles)))))
227 (defun gnus-namazu/get-target-groups ()
229 ((eq major-mode 'gnus-group-mode)
233 (gnus-group-process-prefix current-prefix-arg))
235 (prog1 gnus-group-marked (gnus-group-unmark-all-groups)))))
236 ((eq major-mode 'gnus-summary-mode)
237 ;; In Summary buffer.
238 (if current-prefix-arg
239 (list (gnus-read-group "Group: "))
240 (if (and (gnus-ephemeral-group-p gnus-newsgroup-name)
241 (string-match "^nnvirtual:namazu-search" gnus-newsgroup-name))
242 (cadr (assq 'gnus-namazu-target-groups
243 (gnus-info-method (gnus-get-info gnus-newsgroup-name))))
244 (list gnus-newsgroup-name))))))
246 (defun gnus-namazu/get-current-query ()
247 (and (eq major-mode 'gnus-summary-mode)
248 (gnus-ephemeral-group-p gnus-newsgroup-name)
249 (string-match "^nnvirtual:namazu-search" gnus-newsgroup-name)
250 (cadr (assq 'gnus-namazu-current-query
251 (gnus-info-method (gnus-get-info gnus-newsgroup-name))))))
253 (defvar gnus-namazu/read-query-original-buffer nil)
254 (defvar gnus-namazu/read-query-prompt nil)
255 (defvar gnus-namazu/read-query-history nil)
257 (defun gnus-namazu/get-current-subject ()
258 (and gnus-namazu/read-query-original-buffer
259 (bufferp gnus-namazu/read-query-original-buffer)
260 (with-current-buffer gnus-namazu/read-query-original-buffer
261 (when (eq major-mode 'gnus-summary-mode)
262 (let ((s (gnus-summary-article-subject)))
263 ;; Remove typically prefixes of mailing lists.
265 "^\\(\\[[^]]*[0-9]+\\]\\|([^)]*[0-9]+)\\)\\s-*" s)
266 (setq s (substring s (match-end 0))))
268 "^\\(Re\\(\\^?\\([0-9]+\\|\\[[0-9]+\\]\\)\\)?:\\s-*\\)+" s)
269 (setq s (substring s (match-end 0))))
270 (when (string-match "\\s-*(\\(re\\|was\\)\\b" s)
271 (setq s (substring s 0 (match-beginning 0))))
274 (defun gnus-namazu/get-current-from ()
275 (and gnus-namazu/read-query-original-buffer
276 (bufferp gnus-namazu/read-query-original-buffer)
277 (with-current-buffer gnus-namazu/read-query-original-buffer
278 (when (eq major-mode 'gnus-summary-mode)
279 (cadr (mail-extract-address-components
281 (gnus-summary-article-header))))))))
283 (defmacro gnus-namazu/minibuffer-prompt-end ()
284 (if (fboundp 'minibuffer-prompt-end)
285 '(minibuffer-prompt-end)
288 (defun gnus-namazu/message (string &rest arguments)
290 gnus-namazu/read-query-prompt
291 (buffer-substring (gnus-namazu/minibuffer-prompt-end)
293 (s2 (apply (function format) string arguments))
298 (message (if (>= w 0)
299 (concat s1 (make-string w ?\ ) s2)
301 (if (sit-for 0.3) (message s1))
304 (defun gnus-namazu/complete-query ()
308 ((and (re-search-backward "\\+\\([a-z]*\\)" nil t)
309 (= pos (match-end 0)))
310 (let* ((partial (match-string 1))
314 (mapcar 'list gnus-namazu-field-keywords))))
317 (gnus-namazu/message "No completions of %s" partial))
318 ((= 1 (length completions))
319 (goto-char (match-beginning 1))
320 (delete-region (match-beginning 1) (match-end 1))
321 (insert (car completions) ":")
323 (gnus-namazu/message "Completed"))
325 (let ((x (try-completion partial (mapcar 'list completions))))
326 (if (string= x partial)
327 (if (and (eq last-command
328 'gnus-namazu/field-keyword-completion)
329 completion-auto-help)
330 (with-output-to-temp-buffer "*Completions*"
331 (display-completion-list completions))
332 (gnus-namazu/message "Sole completion"))
333 (goto-char (match-beginning 1))
334 (delete-region (match-beginning 1) (match-end 1))
336 (setq pos (point))))))))
337 ((and (looking-at "\\+subject:")
338 (= pos (match-end 0)))
339 (let ((s (gnus-namazu/get-current-subject)))
343 (setq pos (point)))))
344 ((and (looking-at "\\+from:")
345 (= pos (match-end 0)))
346 (let ((f (gnus-namazu/get-current-from)))
350 (setq pos (point))))))
353 (defvar gnus-namazu/read-query-map
354 (let ((keymap (copy-keymap minibuffer-local-map)))
355 (define-key keymap "\t" 'gnus-namazu/complete-query)
358 (defun gnus-namazu/read-query (prompt &optional initial)
359 (let ((gnus-namazu/read-query-original-buffer (current-buffer))
360 (gnus-namazu/read-query-prompt prompt))
362 (when (setq initial (gnus-namazu/get-current-query))
363 (setq initial (cons initial 0))))
364 (read-from-minibuffer prompt initial gnus-namazu/read-query-map nil
365 'gnus-namazu/read-query-history)))
367 (defun gnus-namazu/truncate-article-list (articles)
368 (let ((hit (length articles)))
369 (when (> hit gnus-large-newsgroup)
370 (let* ((cursor-in-echo-area nil)
372 (when (> hit gnus-large-newsgroup)
373 (read-from-minibuffer
375 "Too many articles were retrieved. How many articles (max %d): "
377 (cons (number-to-string gnus-large-newsgroup) 0)))))
378 (unless (string-match "^[ \t]*$" input)
379 (setcdr (nthcdr (min (1- (string-to-number input)) hit) articles)
383 (defun gnus-namazu-search (groups query)
384 "Search QUERY through GROUPS with Namazu,
385 and make a virtual group contains its results."
388 (gnus-namazu/get-target-groups)
389 (gnus-namazu/read-query "Enter Keyword: ")))
391 (let ((articles (gnus-namazu/search groups query)))
393 (let ((real-groups groups)
396 "nnvirtual:namazu-search?query=%s&groups=%s&id=%d%d%d"
398 (if groups (mapconcat 'identity groups ",") "ALL")
400 (gnus-namazu/truncate-article-list articles)
403 (add-to-list 'real-groups (gnus-namazu/article-group a))))
404 ;; Generate virtual group which includes all results.
406 (gnus-group-read-ephemeral-group
409 (nnvirtual-component-groups ,real-groups)
410 (gnus-namazu-target-groups ,groups)
411 (gnus-namazu-current-query ,query))
412 t (cons (current-buffer) (current-window-configuration)) t))
413 ;; Generate new summary buffer which contains search results.
414 (gnus-group-read-group
416 (sort (delq nil ;; Ad-hoc fix, to avoid wrong-type-argument error.
419 (nnvirtual-reverse-map-article
420 (gnus-namazu/article-group a)
421 (gnus-namazu/article-number a)))
424 (message "No entry."))))
427 (defun gnus-namazu-insinuate ()
429 'gnus-group-mode-hook
431 (define-key gnus-group-mode-map "\C-c\C-n" 'gnus-namazu-search)))
433 'gnus-summary-mode-hook
435 (define-key gnus-summary-mode-map "\C-c\C-n" 'gnus-namazu-search))))
438 (provide 'gnus-namazu)
439 ;; gnus-namazu.el ends here.