(gnus-namazu-index-directory): Abolished.
[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-directories'.
46 ;;
47 ;;    (setq gnus-namazu-index-directories
48 ;;          (list (expand-file-name "~/News/namazu")))
49
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.
53 ;;
54 ;;    (require 'gnus-namazu)
55 ;;    (gnus-namazu-insinuate)
56
57
58 ;;; Usage:
59
60 ;; In group buffer or in summary buffer, type C-c C-n query RET.
61
62
63 ;;; Code:
64
65 (eval-when-compile (require 'cl))
66 (require 'nnoo)
67 (require 'nnheader)
68 (require 'nnmail)
69 (require 'gnus-sum)
70
71 ;; It is required for Mule 2.3.  See the file Mule23@1934.en.
72 (eval-and-compile
73   (autoload 'regexp-opt "regexp-opt"))
74
75 ;; To suppress byte-compile warning.
76 (eval-when-compile
77   (defvar nnml-directory)
78   (defvar nnmh-directory))
79
80 (defgroup gnus-namazu nil
81   "Search nnmh and nnml groups in Gnus with Namazu."
82   :group 'namazu
83   :group 'gnus
84   :prefix "gnus-namazu-")
85
86 (defcustom gnus-namazu-indexed-servers nil
87   "*List of servers indexed with Namazu."
88   :type '(repeat gnus-select-method)
89   :group 'gnus-namazu)
90
91 (defcustom gnus-namazu-index-directories
92   (list
93    (cond
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))
98     (t
99      (expand-file-name "namazu" gnus-directory))))
100   "*Index directory of Namazu."
101   :type '(repeat directory)
102   :group 'gnus-namazu)
103
104 (defcustom gnus-namazu-command
105   (cond
106    ((boundp 'namazu-command)
107     (symbol-value 'namazu-command))
108    ((boundp 'nnir-namazu-program)
109     (symbol-value 'nnir-namazu-program))
110    (t "namazu"))
111   "*Name of the executable file of Namazu."
112   :group 'gnus-namazu
113   :type 'string)
114
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)
120   :group 'gnus-namazu)
121
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)
126   :group 'gnus-namazu)
127
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."
133   :type 'coding-system
134   :group 'gnus-namazu)
135
136
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))
141
142
143 (defun gnus-namazu/setup ()
144   (unless gnus-namazu-indexed-servers
145     (setq gnus-namazu-indexed-servers
146           (delq nil
147                 (mapcar (lambda (method)
148                           (when (memq (car method) '(nnml nnmh))
149                             method))
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)
155             (memq nil (mapcar
156                        (lambda (dir)
157                          (and (stringp dir)
158                               (file-directory-p dir)
159                               (file-readable-p
160                                (expand-file-name "NMZ.i" dir))
161                               dir))
162                        gnus-namazu-index-directories)))
163     (error "%s" "Can't find index.  Check `gnus-namazu-index-directories'")))
164
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))
171                               nnml-directory
172                             nnmh-directory)))))
173
174 ;;; Functions to call Namazu.
175 (defsubst gnus-namazu/normalize-results ()
176   (goto-char (point-min))
177   (while (not (eobp))
178     (cond
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:/")))
186     (forward-line 1)))
187
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))
195     (apply 'call-process
196            `(,gnus-namazu-command
197              nil                        ; input from /dev/null
198              t                          ; output
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
204              ,query
205              ,@gnus-namazu-index-directories))))
206
207 (defun gnus-namazu/search (groups query)
208   (with-temp-buffer
209     (when (zerop (gnus-namazu/call-namazu query))
210       (let* ((articles)
211              (server-alist
212               (delq nil
213                     (let (dir)
214                       (mapcar
215                        (lambda (s)
216                          (when (setq dir (gnus-namazu/server-directory s))
217                            (cons (file-name-as-directory dir) s)))
218                        gnus-namazu-indexed-servers))))
219              (topdir-regexp
220               (regexp-opt (mapcar 'car server-alist) t)))
221         (gnus-namazu/normalize-results)
222         (goto-char (point-min))
223         (while (not (eobp))
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)
230                  (progn
231                    (setq group (substring file 0 (match-beginning 0))
232                          file (match-string 1 file))
233                    (setq group
234                          (gnus-group-prefixed-name
235                           (nnheader-replace-chars-in-string group ?/ ?.)
236                           server))
237                    (when (or (not groups)
238                              (member group groups))
239                      (push (gnus-namazu/make-article
240                             group (string-to-number file))
241                            articles)))))
242           (forward-line 1))
243         (nreverse articles)))))
244
245
246 (defun gnus-namazu/get-target-groups ()
247   (cond
248    ((eq major-mode 'gnus-group-mode)
249     ;; In Group buffer.
250     (cond
251      (current-prefix-arg
252       (gnus-group-process-prefix current-prefix-arg))
253      (gnus-group-marked
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))))))
264
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))))))
271
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)
275
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.
283              (when (string-match
284                     "^\\(\\[[^]]*[0-9]+\\]\\|([^)]*[0-9]+)\\)\\s-*" s)
285                (setq s (substring s (match-end 0))))
286              (when (string-match
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))))
291              s)))))
292
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
299                   (mail-header-from
300                    (gnus-summary-article-header))))))))
301
302 (defmacro gnus-namazu/minibuffer-prompt-end ()
303   (if (fboundp 'minibuffer-prompt-end)
304       '(minibuffer-prompt-end)
305     '(point-min)))
306
307 (defun gnus-namazu/message (string &rest arguments)
308   (let* ((s1 (concat
309               gnus-namazu/read-query-prompt
310               (buffer-substring (gnus-namazu/minibuffer-prompt-end)
311                                 (point-max))))
312          (s2 (apply (function format) string arguments))
313          (w (- (window-width)
314                (string-width s1)
315                (string-width s2)
316                1)))
317     (message (if (>= w 0)
318                  (concat s1 (make-string w ?\ ) s2)
319                s2))
320     (if (sit-for 0.3) (message s1))
321     s2))
322
323 (defun gnus-namazu/complete-query ()
324   (interactive)
325   (let ((pos (point)))
326     (cond
327      ((and (re-search-backward "\\+\\([a-z]*\\)" nil t)
328            (= pos (match-end 0)))
329       (let* ((partial (match-string 1))
330              (completions
331               (all-completions
332                partial
333                (mapcar 'list gnus-namazu-field-keywords))))
334         (cond
335          ((null completions)
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) ":")
341           (setq pos (point))
342           (gnus-namazu/message "Completed"))
343          (t
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))
354               (insert x)
355               (setq pos (point))))))))
356      ((and (looking-at "\\+subject:")
357            (= pos (match-end 0)))
358       (let ((s (gnus-namazu/get-current-subject)))
359         (when s
360           (goto-char pos)
361           (insert "\"" s "\"")
362           (setq pos (point)))))
363      ((and (looking-at "\\+from:")
364            (= pos (match-end 0)))
365       (let ((f (gnus-namazu/get-current-from)))
366         (when f
367           (goto-char pos)
368           (insert "\"" f "\"")
369           (setq pos (point))))))
370     (goto-char pos)))
371
372 (defvar gnus-namazu/read-query-map
373   (let ((keymap (copy-keymap minibuffer-local-map)))
374     (define-key keymap "\t" 'gnus-namazu/complete-query)
375     keymap))
376
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))
380     (unless initial
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)))
385
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)
390              (input
391               (when (> hit gnus-large-newsgroup)
392                 (read-from-minibuffer
393                  (format
394                   "Too many articles were retrieved.  How many articles (max %d): "
395                   hit)
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)
399                   nil))))
400     articles))
401
402 (defun gnus-namazu-search (groups query)
403   "Search QUERY through GROUPS with Namazu,
404 and make a virtual group contains its results."
405   (interactive
406    (list
407     (gnus-namazu/get-target-groups)
408     (gnus-namazu/read-query "Enter Keyword: ")))
409   (gnus-namazu/setup)
410   (let ((articles (gnus-namazu/search groups query)))
411     (if articles
412         (let ((real-groups groups)
413               (vgroup
414                (apply 'format
415                       "nnvirtual:namazu-search?query=%s&groups=%s&id=%d%d%d"
416                       query
417                       (if groups (mapconcat 'identity groups ",") "ALL")
418                       (current-time))))
419           (gnus-namazu/truncate-article-list articles)
420           (unless real-groups
421             (dolist (a articles)
422               (add-to-list 'real-groups (gnus-namazu/article-group a))))
423           ;; Generate virtual group which includes all results.
424           (setq vgroup
425                 (gnus-group-read-ephemeral-group
426                  vgroup
427                  `(nnvirtual ,vgroup
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
434            t t vgroup
435            (sort (delq nil ;; Ad-hoc fix, to avoid wrong-type-argument error.
436                        (mapcar
437                         (lambda (a)
438                           (nnvirtual-reverse-map-article
439                            (gnus-namazu/article-group a)
440                            (gnus-namazu/article-number a)))
441                         articles))
442                  '<)))
443       (message "No entry."))))
444
445
446 (defun gnus-namazu-insinuate ()
447   (add-hook
448    'gnus-group-mode-hook
449    (lambda ()
450      (define-key gnus-group-mode-map "\C-c\C-n" 'gnus-namazu-search)))
451   (add-hook
452    'gnus-summary-mode-hook
453    (lambda ()
454      (define-key gnus-summary-mode-map "\C-c\C-n" 'gnus-namazu-search))))
455
456
457 (provide 'gnus-namazu)
458 ;; gnus-namazu.el ends here.