Fix documents.
[elisp/gnus.git-] / lisp / gnus-namazu.el
1 ;;; gnus-namazu.el --- Search mail with Namazu.
2
3 ;; Copyright (C) 2000,2001 Tsuchiya Masatoshi <tsuchiya@namazu.org>
4
5 ;; Author: Tsuchiya Masatoshi <tsuchiya@namazu.org>
6 ;; Keywords: mail searching namazu
7
8 ;;; Copyright:
9
10 ;; This file is a part of Semi-Gnus.
11
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)
15 ;; any later version.
16
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.
21
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.
26
27
28 ;;; Commentary:
29
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.
33 ;;
34 ;;     http://www.namazu.org/
35
36
37 ;;; Install:
38
39 ;; Make index of articles with Namzu before using this module.
40 ;;
41 ;;       % mkdir ~/News/namazu
42 ;;       % mknmz -a -h -O ~/News/namazu ~/Mail
43 ;;
44 ;; Furthermore, put these expressions to your ~/.gnus, to set the path
45 ;; of the index files to `gnus-namazu-index-directory'.
46 ;;
47 ;;       (setq gnus-namazu-index-directory (expand-file-name "~/News/namazu"))
48
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.
52 ;;
53 ;;       (require 'gnus-namazu)
54 ;;       (gnus-namazu-insinuate)
55
56
57 ;;; Usage:
58
59 ;; In group buffer or in summary buffer, type C-c C-n query RET.
60
61
62 ;;; Code:
63
64 (eval-when-compile (require 'cl))
65 (require 'nnoo)
66 (require 'nnheader)
67 (require 'nnmail)
68 (require 'gnus-sum)
69
70 ;; It is required for Mule 2.3.  See the file Mule23@1934.en.
71 (eval-and-compile
72   (autoload 'regexp-opt "regexp-opt"))
73
74 (defgroup gnus-namazu nil
75   "Search nnmh and nnml groups in Gnus with Namazu."
76   :group 'namazu
77   :group 'gnus
78   :prefix "gnus-namazu-")
79
80 (defcustom gnus-namazu-indexed-servers nil
81   "*List of servers indexed with Namazu."
82   :type '(repeat gnus-select-method)
83   :group 'gnus-namazu)
84
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."
90   :type 'directory
91   :group 'gnus-namazu)
92
93 (defcustom gnus-namazu-command
94   (cond
95    ((boundp 'namazu-command)
96     (symbol-value 'namazu-command))
97    ((boundp 'nnir-namazu-program)
98     (symbol-value 'nnir-namazu-program))
99    (t "namazu"))
100   "*Name of the executable file of Namazu."
101   :group 'gnus-namazu
102   :type 'string)
103
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)
109   :group 'gnus-namazu)
110
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)
115   :group 'gnus-namazu)
116
117 (defcustom gnus-namazu-coding-system
118   (if (>= emacs-major-version 20) 'euc-japan '*euc-japan*)
119   "*Coding system for Namazu process."
120   :type 'coding-system
121   :group 'gnus-namazu)
122
123
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))
128
129
130 (defun gnus-namazu/setup ()
131   (unless gnus-namazu-indexed-servers
132     (setq gnus-namazu-indexed-servers
133           (delq nil
134                 (mapcar (lambda (method)
135                           (when (memq (car method) '(nnml nnmh))
136                             method))
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)
143                (file-readable-p
144                 (expand-file-name "NMZ.i" gnus-namazu-index-directory)))
145     (error "%s" "Can't find index.  Check `gnus-namazu-index-directory'")))
146
147
148 ;; To suppress byte-compile warning.
149 (eval-when-compile
150   (defvar nnml-directory)
151   (defvar nnmh-directory))
152
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))
159                               nnml-directory
160                             nnmh-directory)))))
161
162
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))
169         (process-environment
170          (copy-sequence process-environment)))
171     ;; Disable locale.
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)))
176     (setenv "LANG" "C")
177     (apply 'call-process
178            `(,gnus-namazu-command
179              nil                        ; input from /dev/null
180              t                          ; output
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
186              ,query
187              ,gnus-namazu-index-directory))))
188
189 (defun gnus-namazu/search (groups query)
190   (with-temp-buffer
191     (when (zerop (gnus-namazu/call-namazu query))
192       (let* ((articles)
193              (server-alist
194               (delq nil
195                     (let (dir)
196                       (mapcar
197                        (lambda (s)
198                          (when (setq dir (gnus-namazu/server-directory s))
199                            (cons (file-name-as-directory dir) s)))
200                        gnus-namazu-indexed-servers))))
201              (topdir-regexp
202               (regexp-opt (mapcar 'car server-alist) t)))
203         (goto-char (point-min))
204         (while (not (eobp))
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)
211                  (progn
212                    (setq group (substring file 0 (match-beginning 0))
213                          file (match-string 1 file))
214                    (setq group
215                          (gnus-group-prefixed-name
216                           (nnheader-replace-chars-in-string group ?/ ?.)
217                           server))
218                    (when (or (not groups)
219                              (member group groups))
220                      (push (gnus-namazu/make-article
221                             group (string-to-number file))
222                            articles)))))
223           (forward-line 1))
224         (nreverse articles)))))
225
226
227 (defun gnus-namazu/get-target-groups ()
228   (cond
229    ((eq major-mode 'gnus-group-mode)
230     ;; In Group buffer.
231     (cond
232      (current-prefix-arg
233       (gnus-group-process-prefix current-prefix-arg))
234      (gnus-group-marked
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))))))
245
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))))))
252
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)
256
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.
264              (when (string-match
265                     "^\\(\\[[^]]*[0-9]+\\]\\|([^)]*[0-9]+)\\)\\s-*" s)
266                (setq s (substring s (match-end 0))))
267              (when (string-match
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))))
272              s)))))
273
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
280                   (mail-header-from
281                    (gnus-summary-article-header))))))))
282
283 (defmacro gnus-namazu/minibuffer-prompt-end ()
284   (if (fboundp 'minibuffer-prompt-end)
285       '(minibuffer-prompt-end)
286     '(point-min)))
287
288 (defun gnus-namazu/message (string &rest arguments)
289   (let* ((s1 (concat
290               gnus-namazu/read-query-prompt
291               (buffer-substring (gnus-namazu/minibuffer-prompt-end)
292                                 (point-max))))
293          (s2 (apply (function format) string arguments))
294          (w (- (window-width)
295                (string-width s1)
296                (string-width s2)
297                1)))
298     (message (if (>= w 0)
299                  (concat s1 (make-string w ?\ ) s2)
300                s2))
301     (if (sit-for 0.3) (message s1))
302     s2))
303
304 (defun gnus-namazu/complete-query ()
305   (interactive)
306   (let ((pos (point)))
307     (cond
308      ((and (re-search-backward "\\+\\([a-z]*\\)" nil t)
309            (= pos (match-end 0)))
310       (let* ((partial (match-string 1))
311              (completions
312               (all-completions
313                partial
314                (mapcar 'list gnus-namazu-field-keywords))))
315         (cond
316          ((null completions)
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) ":")
322           (setq pos (point))
323           (gnus-namazu/message "Completed"))
324          (t
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))
335               (insert x)
336               (setq pos (point))))))))
337      ((and (looking-at "\\+subject:")
338            (= pos (match-end 0)))
339       (let ((s (gnus-namazu/get-current-subject)))
340         (when s
341           (goto-char pos)
342           (insert "\"" s "\"")
343           (setq pos (point)))))
344      ((and (looking-at "\\+from:")
345            (= pos (match-end 0)))
346       (let ((f (gnus-namazu/get-current-from)))
347         (when f
348           (goto-char pos)
349           (insert "\"" f "\"")
350           (setq pos (point))))))
351     (goto-char pos)))
352
353 (defvar gnus-namazu/read-query-map
354   (let ((keymap (copy-keymap minibuffer-local-map)))
355     (define-key keymap "\t" 'gnus-namazu/complete-query)
356     keymap))
357
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))
361     (unless initial
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)))
366
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)
371              (input
372               (when (> hit gnus-large-newsgroup)
373                 (read-from-minibuffer
374                  (format
375                   "Too many articles were retrieved.  How many articles (max %d): "
376                   hit)
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)
380                   nil))))
381     articles))
382
383 (defun gnus-namazu-search (groups query)
384   "Search QUERY through GROUPS with Namazu,
385 and make a virtual group contains its results."
386   (interactive
387    (list
388     (gnus-namazu/get-target-groups)
389     (gnus-namazu/read-query "Enter Keyword: ")))
390   (gnus-namazu/setup)
391   (let ((articles (gnus-namazu/search groups query)))
392     (if articles
393         (let ((real-groups groups)
394               (vgroup
395                (apply 'format
396                       "nnvirtual:namazu-search?query=%s&groups=%s&id=%d%d%d"
397                       query
398                       (if groups (mapconcat 'identity groups ",") "ALL")
399                       (current-time))))
400           (gnus-namazu/truncate-article-list articles)
401           (unless real-groups
402             (dolist (a articles)
403               (add-to-list 'real-groups (gnus-namazu/article-group a))))
404           ;; Generate virtual group which includes all results.
405           (setq vgroup
406                 (gnus-group-read-ephemeral-group
407                  vgroup
408                  `(nnvirtual ,vgroup
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
415            t t vgroup
416            (sort (delq nil ;; Ad-hoc fix, to avoid wrong-type-argument error.
417                        (mapcar
418                         (lambda (a)
419                           (nnvirtual-reverse-map-article
420                            (gnus-namazu/article-group a)
421                            (gnus-namazu/article-number a)))
422                         articles))
423                  '<)))
424       (message "No entry."))))
425
426
427 (defun gnus-namazu-insinuate ()
428   (add-hook
429    'gnus-group-mode-hook
430    (lambda ()
431      (define-key gnus-group-mode-map "\C-c\C-n" 'gnus-namazu-search)))
432   (add-hook
433    'gnus-summary-mode-hook
434    (lambda ()
435      (define-key gnus-summary-mode-map "\C-c\C-n" 'gnus-namazu-search))))
436
437
438 (provide 'gnus-namazu)
439 ;; gnus-namazu.el ends here.