1 ;;; gnus-namazu.el --- Search mail with Namazu -*- coding: iso-2022-7bit; -*-
3 ;; Copyright (C) 2000, 2001, 2002, 2003, 2004
4 ;; TSUCHIYA Masatoshi <tsuchiya@namazu.org>
6 ;; Author: TSUCHIYA Masatoshi <tsuchiya@namazu.org>
7 ;; Keywords: mail searching namazu
9 ;; This file is a part of Semi-Gnus.
11 ;; This program is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; This program is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with this program; if not, you can either send email to this
23 ;; program's maintainer or write to: The Free Software Foundation,
24 ;; Inc.; 59 Temple Place, Suite 330; Boston, MA 02111-1307, USA.
29 ;; This file defines the command to search mails and persistent
30 ;; articles with Namazu and to browse its results with Gnus.
32 ;; Namazu is a full-text search engine intended for easy use. For
33 ;; more detail about Namazu, visit the following page:
40 ;; If this module has already been installed, only 3 steps are
41 ;; required to search articles with this module.
43 ;; (1) Install Namazu.
44 ;; (2) Start Gnus and type M-x gnus-namazu-create-index RET to make
46 ;; (3) In group buffer or in summary buffer, type C-c C-n query RET.
51 ;; Before installing this module, you must install Namazu.
53 ;; This file is a part of T-gnus but is not *YET* a part of Gnus.
54 ;; When you would like to use this module in Gnus (not T-gnus), put
55 ;; this file into the lisp/ directory in the Gnus source tree and run
56 ;; `make install'. And then, put the following expression into your
59 ;; (require 'gnus-namazu)
60 ;; (gnus-namazu-insinuate)
62 ;; In order to make index of articles with Namazu before using this
63 ;; module, type M-x gnus-namazu-create-index RET. Otherwise, you can
64 ;; create index by yourself with the following commands:
66 ;; % mkdir ~/News/namazu
67 ;; % mknmz -a -h -O ~/News/namazu ~/Mail ~/News/cache
69 ;; The first command makes the directory for index files, and the
70 ;; second command generates index files of mails and persistent
73 ;; In order to update indices for incoming articles, this module
74 ;; automatically runs mknmz, the indexer of Namazu, at an interval of
75 ;; 3 days; this period is set to `gnus-namazu-index-update-interval'.
77 ;; Indices will be updated when `gnus-namazu-search' is called. If
78 ;; you want to update indices everywhen Gnus is started, you can put
79 ;; the following expression to your ~/.gnus.
81 ;; (add-hook 'gnus-startup-hook 'gnus-namazu-update-all-indices)
83 ;; In order to control mknmz closely, disable the automatic updating
84 ;; feature and run mknmz by yourself. In this case, set nil to the
87 ;; (setq gnus-namazu-index-update-interval nil)
89 ;; When your index is put into the directory other than the default
90 ;; one (~/News/namazu), it is necessary to set its place to
91 ;; `gnus-namazu-index-directories' as follows:
93 ;; (setq gnus-namazu-index-directories
94 ;; (list (expand-file-name "~/namazu")))
99 (eval-when-compile (require 'cl))
105 ;; It is required for Mule 2.3. See the file Mule23@1934.en.
107 (autoload 'regexp-opt "regexp-opt"))
109 ;; To suppress byte-compile warning.
111 (defvar nnml-directory)
112 (defvar nnmh-directory))
115 (defgroup gnus-namazu nil
116 "Search nnmh and nnml groups in Gnus with Namazu."
119 :prefix "gnus-namazu-")
121 (defconst gnus-namazu-default-index-directory
122 (expand-file-name "namazu" gnus-directory)
123 "Default place of Namazu index files.")
125 (defcustom gnus-namazu-index-directories
127 (or (and (boundp 'gnus-namazu-index-directory)
128 (symbol-value 'gnus-namazu-index-directory))
129 (and (boundp 'nnir-namazu-index-directory)
130 (symbol-value 'nnir-namazu-index-directory))
131 gnus-namazu-default-index-directory))
132 "*Places of Namazu index files."
133 :type '(repeat directory)
136 (defcustom gnus-namazu-command
137 (or (and (boundp 'namazu-command)
138 (symbol-value 'namazu-command))
139 (and (boundp 'nnir-namazu-program)
140 (symbol-value 'nnir-namazu-program))
142 "*Name of the executable file of Namazu."
146 (defcustom gnus-namazu-additional-arguments nil
147 "*Additional arguments of Namazu.
148 The options `-q', `-a', and `-l' are always used, very few other
149 options make any sense in this context."
150 :type '(repeat string)
153 (defcustom gnus-namazu-index-update-interval
154 259200 ; 3 days == 259200 seconds.
155 "*Number of seconds between running the indexer of Namazu."
156 :type '(choice (const :tag "Never run the indexer" nil)
157 (integer :tag "Number of seconds"))
160 (defcustom gnus-namazu-make-index-command "mknmz"
161 "*Name of the executable file of the indexer of Namazu."
165 (defcustom gnus-namazu-make-index-arguments
167 (list "--all" "--mailnews" "--deny=^.*[^0-9].*$")
168 (when (and (boundp 'current-language-environment)
170 (symbol-value 'current-language-environment)))
171 (list "--indexing-lang=ja")))
172 "*Arguments of the indexer of Namazu."
173 :type '(repeat string)
176 (defcustom gnus-namazu-field-keywords
177 '("date" "from" "newsgroups" "size" "subject" "summary" "to" "uri")
178 "*List of keywords to do field-search."
179 :type '(repeat string)
182 (defcustom gnus-namazu-coding-system
183 (if (memq system-type '(windows-nt OS/2 emx))
186 "*Coding system for Namazu process."
190 (defcustom gnus-namazu-need-path-normalization
191 (and (memq system-type '(windows-nt OS/2 emx)) t)
192 "*Non-nil means that outputs of namazu may contain drive letters."
196 (defcustom gnus-namazu-case-sensitive-filesystem
197 (not (eq system-type 'windows-nt))
198 "*Non-nil means that the using file system distinguishes cases of characters."
202 (defcustom gnus-namazu-query-highlight t
203 "Non-nil means that queried words is highlighted."
207 (defface gnus-namazu-query-highlight-face
208 '((((type tty pc) (class color))
209 (:background "magenta4" :foreground "cyan1"))
210 (((class color) (background light))
211 (:background "magenta4" :foreground "lightskyblue1"))
212 (((class color) (background dark))
213 (:background "palevioletred2" :foreground "brown4"))
214 (t (:inverse-video t)))
215 "Face used for namazu query matching words."
218 ;;; Internal Variable:
219 (defconst gnus-namazu/group-name-regexp "\\`nnvirtual:namazu-search\\?")
221 ;; Multibyte group name:
223 (fboundp 'gnus-group-decoded-name)
224 (let ((gnus-group-name-charset-group-alist
225 (list (cons gnus-namazu/group-name-regexp gnus-namazu-coding-system)))
226 (query (decode-coding-string (string 27 36 66 52 65 59 122 27 40 66)
228 (not (string-match query
229 (gnus-summary-buffer-name
230 (encode-coding-string
231 (concat "nnvirtual:namazu-search?query=" query)
232 gnus-namazu-coding-system)))))
233 (let (current-load-list)
234 (defadvice gnus-summary-buffer-name
235 (before gnus-namazu-summary-buffer-name activate compile)
236 "Advised by `gnus-namazu' to handle encoded group names."
237 (ad-set-arg 0 (gnus-group-decoded-name (ad-get-arg 0))))))
239 (defmacro gnus-namazu/make-article (group number)
240 `(cons ,group ,number))
241 (defmacro gnus-namazu/article-group (x) `(car ,x))
242 (defmacro gnus-namazu/article-number (x) `(cdr ,x))
244 (defsubst gnus-namazu/indexed-servers ()
245 "Choice appropriate servers from opened ones, and return thier list."
247 (gnus-servers-using-backend 'nnml)
248 (gnus-servers-using-backend 'nnmh)))
250 (defsubst gnus-namazu/default-index-directory ()
251 (if (member gnus-namazu-default-index-directory
252 gnus-namazu-index-directories)
253 gnus-namazu-default-index-directory
254 (car gnus-namazu-index-directories)))
256 (defun gnus-namazu/setup ()
257 (and (boundp 'gnus-group-name-charset-group-alist)
258 (not (member (cons gnus-namazu/group-name-regexp
259 gnus-namazu-coding-system)
260 gnus-group-name-charset-group-alist))
261 (let ((pair (assoc gnus-namazu/group-name-regexp
262 gnus-group-name-charset-group-alist)))
264 (setcdr pair gnus-namazu-coding-system)
265 (push (cons gnus-namazu/group-name-regexp
266 gnus-namazu-coding-system)
267 gnus-group-name-charset-group-alist))))
268 (gnus-namazu-update-all-indices))
270 (defun gnus-namazu/server-directory (server)
271 "Return the top directory of the server SERVER."
272 (and (memq (car server) '(nnml nnmh))
273 (nnoo-change-server (car server) (nth 1 server) (nthcdr 2 server))
274 (file-name-as-directory
275 (expand-file-name (if (eq 'nnml (car server))
279 ;;; Functions to call Namazu.
280 (defsubst gnus-namazu/normalize-results ()
281 "Normalize file names returned by Namazu in this current buffer."
282 (goto-char (point-min))
284 (when (looking-at "file://")
285 (delete-region (point) (match-end 0)))
286 (when (if gnus-namazu-need-path-normalization
287 (or (not (looking-at "/\\(.\\)|/"))
288 (replace-match "\\1:/"))
289 (eq ?~ (char-after (point))))
290 (insert (expand-file-name
291 (buffer-substring (point-at-bol) (point-at-eol))))
292 (delete-region (point) (point-at-eol)))
295 (defsubst gnus-namazu/call-namazu (query)
296 (let ((coding-system-for-read gnus-namazu-coding-system)
297 (coding-system-for-write gnus-namazu-coding-system)
298 (default-process-coding-system
299 (cons gnus-namazu-coding-system gnus-namazu-coding-system))
300 program-coding-system-alist
301 (file-name-coding-system gnus-namazu-coding-system))
303 `(,gnus-namazu-command
304 nil ; input from /dev/null
306 nil ; don't redisplay
307 "-q" ; don't be verbose
308 "-a" ; show all matches
309 "-l" ; use list format
310 ,@gnus-namazu-additional-arguments
312 ,@gnus-namazu-index-directories))))
314 (defvar gnus-namazu/directory-table nil)
315 (defun gnus-namazu/make-directory-table (&optional force)
316 (interactive (list t))
317 (unless (and (not force)
318 gnus-namazu/directory-table
319 (eq gnus-namazu-case-sensitive-filesystem
320 (car gnus-namazu/directory-table)))
321 (let ((table (make-vector (length gnus-newsrc-hashtb) 0))
322 cache agent alist dir method)
325 (unless (gnus-ephemeral-group-p (setq group (symbol-name group)))
326 (when (file-directory-p
327 (setq dir (file-name-as-directory
328 (gnus-cache-file-name group ""))))
329 (push (cons dir group) cache))
330 (when (file-directory-p
331 (setq dir (gnus-agent-group-pathname group)))
332 (push (cons dir group) agent))
333 (when (memq (car (setq method (gnus-find-method-for-group group)))
335 (when (file-directory-p
336 (setq dir (nnmail-group-pathname
337 (gnus-group-short-name group)
338 (gnus-namazu/server-directory method))))
339 (push (cons dir group) alist)))))
341 (dolist (pair (nconc agent cache alist))
342 (set (intern (if gnus-namazu-case-sensitive-filesystem
344 (downcase (car pair)))
347 (setq gnus-namazu/directory-table
348 (cons gnus-namazu-case-sensitive-filesystem table)))))
350 (defun gnus-namazu/search (groups query)
351 (gnus-namazu/make-directory-table)
353 (let ((exit-status (gnus-namazu/call-namazu query)))
354 (unless (zerop exit-status)
355 (error "Namazu finished abnormally: %d" exit-status)))
356 (gnus-namazu/normalize-results)
357 (goto-char (point-min))
358 (let (articles group)
360 (setq group (buffer-substring-no-properties
364 ;; NOTE: Only numeric characters are permitted
365 ;; as file names of articles.
366 (skip-chars-backward "0-9")
370 (intern-soft (if gnus-namazu-case-sensitive-filesystem
373 (cdr gnus-namazu/directory-table))))
375 (member group groups))
376 (push (gnus-namazu/make-article
379 (buffer-substring-no-properties (point)
383 (nreverse articles))))
386 (defun gnus-namazu/get-target-groups ()
388 ((eq major-mode 'gnus-group-mode)
392 (gnus-group-process-prefix current-prefix-arg))
394 (prog1 gnus-group-marked (gnus-group-unmark-all-groups)))))
395 ((eq major-mode 'gnus-summary-mode)
396 ;; In Summary buffer.
397 (if current-prefix-arg
398 (list (gnus-read-group "Group: "))
400 (gnus-ephemeral-group-p gnus-newsgroup-name)
401 (string-match gnus-namazu/group-name-regexp gnus-newsgroup-name))
402 (cadr (assq 'gnus-namazu-target-groups
403 (gnus-info-method (gnus-get-info gnus-newsgroup-name))))
404 (list gnus-newsgroup-name))))))
406 (defun gnus-namazu/get-current-query ()
407 (and (eq major-mode 'gnus-summary-mode)
408 (gnus-ephemeral-group-p gnus-newsgroup-name)
409 (string-match gnus-namazu/group-name-regexp gnus-newsgroup-name)
410 (cadr (assq 'gnus-namazu-current-query
411 (gnus-info-method (gnus-get-info gnus-newsgroup-name))))))
413 (defvar gnus-namazu/read-query-original-buffer nil)
414 (defvar gnus-namazu/read-query-prompt nil)
415 (defvar gnus-namazu/read-query-history nil)
417 (defun gnus-namazu/get-current-subject ()
418 (and gnus-namazu/read-query-original-buffer
419 (bufferp gnus-namazu/read-query-original-buffer)
420 (with-current-buffer gnus-namazu/read-query-original-buffer
421 (when (eq major-mode 'gnus-summary-mode)
422 (let ((s (gnus-summary-article-subject)))
423 ;; Remove typically prefixes of mailing lists.
425 "^\\(\\[[^]]*[0-9]+\\]\\|([^)]*[0-9]+)\\)\\s-*" s)
426 (setq s (substring s (match-end 0))))
428 "^\\(Re\\(\\^?\\([0-9]+\\|\\[[0-9]+\\]\\)\\)?:\\s-*\\)+" s)
429 (setq s (substring s (match-end 0))))
430 (when (string-match "\\s-*(\\(re\\|was\\)\\b" s)
431 (setq s (substring s 0 (match-beginning 0))))
434 (defun gnus-namazu/get-current-from ()
435 (and gnus-namazu/read-query-original-buffer
436 (bufferp gnus-namazu/read-query-original-buffer)
437 (with-current-buffer gnus-namazu/read-query-original-buffer
438 (when (eq major-mode 'gnus-summary-mode)
439 (cadr (mail-extract-address-components
441 (gnus-summary-article-header))))))))
443 (defun gnus-namazu/get-current-to ()
444 (and gnus-namazu/read-query-original-buffer
445 (bufferp gnus-namazu/read-query-original-buffer)
446 (with-current-buffer gnus-namazu/read-query-original-buffer
447 (when (eq major-mode 'gnus-summary-mode)
448 (cadr (mail-extract-address-components
449 (cdr (assq 'To (mail-header-extra
450 (gnus-summary-article-header))))))))))
452 (defmacro gnus-namazu/minibuffer-prompt-end ()
453 (if (fboundp 'minibuffer-prompt-end)
454 '(minibuffer-prompt-end)
457 (defun gnus-namazu/message (string &rest arguments)
459 gnus-namazu/read-query-prompt
460 (buffer-substring (gnus-namazu/minibuffer-prompt-end)
462 (s2 (apply (function format) string arguments))
467 (message (if (>= w 0)
468 (concat s1 (make-string w ?\ ) s2)
470 (if (sit-for 0.3) (message s1))
473 (defun gnus-namazu/complete-query ()
477 ((and (re-search-backward "\\+\\([-a-z]*\\)" nil t)
478 (= pos (match-end 0)))
479 (let* ((partial (match-string 1))
483 (mapcar 'list gnus-namazu-field-keywords))))
486 (gnus-namazu/message "No completions of %s" partial))
487 ((= 1 (length completions))
488 (goto-char (match-beginning 1))
489 (delete-region (match-beginning 1) (match-end 1))
490 (insert (car completions) ":")
492 (gnus-namazu/message "Completed"))
494 (let ((x (try-completion partial (mapcar 'list completions))))
495 (if (string= x partial)
496 (if (and (eq last-command
497 'gnus-namazu/field-keyword-completion)
498 completion-auto-help)
499 (with-output-to-temp-buffer "*Completions*"
500 (display-completion-list completions))
501 (gnus-namazu/message "Sole completion"))
502 (goto-char (match-beginning 1))
503 (delete-region (match-beginning 1) (match-end 1))
505 (setq pos (point))))))))
506 ((and (looking-at "\\+subject:")
507 (= pos (match-end 0)))
508 (let ((s (gnus-namazu/get-current-subject)))
512 (setq pos (point)))))
513 ((and (looking-at "\\+from:")
514 (= pos (match-end 0)))
515 (let ((f (gnus-namazu/get-current-from)))
519 (setq pos (point)))))
520 ((and (looking-at "\\+to:")
521 (= pos (match-end 0)))
522 (let ((to (gnus-namazu/get-current-to)))
525 (insert "\"" to "\"")
526 (setq pos (point))))))
529 (defvar gnus-namazu/read-query-map
530 (let ((keymap (copy-keymap minibuffer-local-map)))
531 (define-key keymap "\t" 'gnus-namazu/complete-query)
534 (defun gnus-namazu/read-query (prompt &optional initial)
535 (let ((gnus-namazu/read-query-original-buffer (current-buffer))
536 (gnus-namazu/read-query-prompt prompt))
538 (when (setq initial (gnus-namazu/get-current-query))
539 (setq initial (cons initial 0))))
540 (read-from-minibuffer prompt initial gnus-namazu/read-query-map nil
541 'gnus-namazu/read-query-history)))
543 (defun gnus-namazu/highlight-words (query)
546 ;; Remove tokens for NOT search
547 (goto-char (point-min))
548 (while (re-search-forward "[
\e$B!!
\e(B \t\r\f\n]+not[
\e$B!!
\e(B \t\r\f\n]+\
549 \\([^
\e$B!!
\e(B \t\r\f\n\"{(/]+\\|\"[^\"]+\"\\|{[^}]+}\\|([^)]+)\\|/[^/]+/\\)+" nil t)
550 (delete-region (match-beginning 0) (match-end 0)))
551 ;; Remove tokens for Field search
552 (goto-char (point-min))
553 (while (re-search-forward "[
\e$B!!
\e(B \t\r\f\n]+\\+[^
\e$B!!
\e(B \t\r\f\n:]+:\
554 \\([^
\e$B!!
\e(B \t\r\f\n\"{(/]+\\|\"[^\"]+\"\\|{[^}]+}\\|([^)]+)\\|/[^/]+/\\)+" nil t)
555 (delete-region (match-beginning 0) (match-end 0)))
556 ;; Remove tokens for Regexp search
557 (goto-char (point-min))
558 (while (re-search-forward "/[^/]+/" nil t)
559 (delete-region (match-beginning 0) (match-end 0)))
560 ;; Remove brackets, double quote, asterisk and operators
561 (goto-char (point-min))
562 (while (re-search-forward "\\([(){}\"*]\\|\\b\\(and\\|or\\)\\b\\)" nil t)
563 (delete-region (match-beginning 0) (match-end 0)))
564 ;; Collect all keywords
566 (goto-char (point-min))
567 (while (re-search-forward "[^
\e$B!!
\e(B \t\r\f\n]+" nil t)
568 (push (match-string 0) query))
572 (if (string-match "\\cj" q)
577 (list (list (concat "\\b\\(" (regexp-opt en) "\\)\\b")
578 0 0 'gnus-namazu-query-highlight-face)))
580 (list (list (regexp-opt ja)
581 0 0 'gnus-namazu-query-highlight-face))))))))
583 (defun gnus-namazu/truncate-article-list (articles)
584 (let ((hit (length articles)))
585 (when (and gnus-large-newsgroup
586 (> hit gnus-large-newsgroup))
587 (let* ((cursor-in-echo-area nil)
588 (input (read-from-minibuffer
590 Too many articles were retrieved. How many articles (max %d): "
592 (cons (number-to-string gnus-large-newsgroup) 0))))
593 (unless (string-match "\\`[ \t]*\\'" input)
594 (setcdr (nthcdr (min (1- (string-to-number input)) hit) articles)
599 (defun gnus-namazu-search (groups query)
600 "Search QUERY through GROUPS with Namazu,
601 and make a virtual group contains its results."
604 (gnus-namazu/get-target-groups)
605 (gnus-namazu/read-query "Enter query: ")))
607 (let ((articles (gnus-namazu/search groups query)))
609 (let ((real-groups groups)
611 (apply (function format)
612 "nnvirtual:namazu-search?query=%s&groups=%s&id=%d%d%d"
614 (if groups (mapconcat 'identity groups ",") "ALL")
616 (gnus-namazu/truncate-article-list articles)
619 (add-to-list 'real-groups (gnus-namazu/article-group a))))
620 ;; Generate virtual group which includes all results.
621 (when (fboundp 'gnus-group-decoded-name)
623 (encode-coding-string vgroup gnus-namazu-coding-system)))
625 (gnus-group-read-ephemeral-group
628 (nnvirtual-component-groups ,real-groups)
629 (gnus-namazu-target-groups ,groups)
630 (gnus-namazu-current-query ,query))
631 t (cons (current-buffer) (current-window-configuration)) t))
632 (when gnus-namazu-query-highlight
633 (gnus-group-set-parameter vgroup 'highlight-words
634 (gnus-namazu/highlight-words query)))
635 ;; Generate new summary buffer which contains search results.
636 (gnus-group-read-group
638 (sort (delq nil ;; Ad-hoc fix, to avoid wrong-type-argument error.
641 (nnvirtual-reverse-map-article
642 (gnus-namazu/article-group a)
643 (gnus-namazu/article-number a)))
646 (message "No entry."))))
648 (defmacro gnus-namazu/lock-file-name (&optional directory)
649 `(expand-file-name "NMZ.lock2" ,directory))
651 (defmacro gnus-namazu/status-file-name (&optional directory)
652 `(expand-file-name "NMZ.status" ,directory))
654 (defmacro gnus-namazu/index-file-name (&optional directory)
655 `(expand-file-name "NMZ.i" ,directory))
657 (defun gnus-namazu/mknmz-cleanup (directory)
658 (let ((lockfile (gnus-namazu/lock-file-name directory)))
659 (when (file-exists-p lockfile)
660 (delete-file lockfile)
661 (dolist (tmpfile (directory-files directory t "\\`NMZ\\..*\\.tmp\\'" t))
662 (delete-file tmpfile)))))
665 (defun gnus-namazu-create-index (directory &optional target-directories force)
666 "Create index under DIRECTORY."
669 (if (and current-prefix-arg (> (length gnus-namazu-index-directories) 1))
670 (completing-read "Directory: "
671 (mapcar 'list gnus-namazu-index-directories) nil t)
672 (gnus-namazu/default-index-directory))
674 (setq directory (file-name-as-directory (expand-file-name directory)))
675 (unless target-directories
676 (setq target-directories
678 (mapcar (lambda (dir)
679 (when (file-directory-p dir) dir))
681 (mapcar 'gnus-namazu/server-directory
682 (gnus-namazu/indexed-servers))
684 (expand-file-name gnus-cache-directory)
685 (expand-file-name gnus-agent-directory)))))))
686 (if (file-exists-p (gnus-namazu/lock-file-name directory))
688 (error "Found lock file: %s" (gnus-namazu/lock-file-name directory)))
690 (get-buffer-create (concat " *mknmz*" directory))
692 (unless (file-directory-p directory)
693 (make-directory directory t))
694 (setq default-directory directory)
695 (let ((args (append gnus-namazu-make-index-arguments
696 target-directories)))
697 (insert "% " gnus-namazu-make-index-command " "
698 (mapconcat 'identity args " ") "\n")
699 (goto-char (point-max))
701 (pop-to-buffer (current-buffer)))
702 (message "Make index at %s..." directory)
704 (apply 'call-process gnus-namazu-make-index-command nil t t args)
705 (gnus-namazu/mknmz-cleanup directory))
706 (message "Make index at %s...done" directory)
708 (kill-buffer (current-buffer)))))
709 (gnus-namazu/make-directory-table t)))
711 (defun gnus-namazu/lapse-seconds (start end)
712 "Return lapse seconds from START to END.
713 START and END are lists which represent time in Emacs-style."
714 (+ (* (- (car end) (car start)) 65536)
718 (defun gnus-namazu/index-old-p (directory)
719 "Return non-nil value when the index under the DIRECTORY is older
720 than the period that is set to `gnus-namazu-index-update-interval'"
721 (let ((file (gnus-namazu/index-file-name directory)))
722 (or (not (file-exists-p file))
723 (and (integerp gnus-namazu-index-update-interval)
724 (>= (gnus-namazu/lapse-seconds
725 (nth 5 (file-attributes file))
727 gnus-namazu-index-update-interval)))))
729 (defvar gnus-namazu/update-directories nil)
730 (defvar gnus-namazu/update-process nil)
732 (defun gnus-namazu/update-p (directory &optional force)
733 "Return the DIRECTORY when the index undef the DIRECTORY should be updated."
734 (setq directory (file-name-as-directory (expand-file-name directory)))
735 (labels ((error-message (format &rest args)
736 (apply (if force 'error 'message) format args)
738 (if gnus-namazu/update-process
739 (error-message "%s" "Can not run two update processes simultaneously")
741 (gnus-namazu/index-old-p directory))
742 (let ((status-file (gnus-namazu/status-file-name directory)))
743 (or (file-exists-p status-file)
744 (error-message "Can not find status file: %s" status-file)))
745 (let ((lock-file (gnus-namazu/lock-file-name directory)))
746 (or (not (file-exists-p lock-file))
747 (error-message "Found lock file: %s" lock-file)))
751 (defun gnus-namazu-update-index (directory &optional force)
752 "Update the index under the DIRECTORY."
755 (if (and current-prefix-arg (> (length gnus-namazu-index-directories) 1))
756 (completing-read "Directory: "
757 (mapcar 'list gnus-namazu-index-directories) nil t)
758 (gnus-namazu/default-index-directory))
760 (when (setq directory (gnus-namazu/update-p directory force))
761 (with-current-buffer (get-buffer-create (concat " *mknmz*" directory))
762 (buffer-disable-undo)
764 (unless (file-directory-p directory)
765 (make-directory directory t))
766 (setq default-directory directory)
767 (let ((proc (start-process gnus-namazu-make-index-command
769 gnus-namazu-make-index-command
770 (format "--update=%s" directory))))
772 (prog1 (setq gnus-namazu/update-process proc)
773 (process-kill-without-query proc)
774 (set-process-sentinel proc 'gnus-namazu/update-sentinel)
775 (add-hook 'kill-emacs-hook 'gnus-namazu-stop-update)
776 (message "Update index at %s..." directory))
777 (goto-char (point-min))
778 (if (re-search-forward "^ERROR:.*$" nil t)
780 (pop-to-buffer (current-buffer))
781 (funcall (if force 'error 'message)
782 "Update index at %s...%s" directory (match-string 0)))
783 (kill-buffer (current-buffer))
784 (funcall (if force 'error 'message)
785 "Can not start %s" gnus-namazu-make-index-command))
789 (defun gnus-namazu-update-all-indices (&optional force)
790 "Update all indices which is set to `gnus-namazu-index-directories'."
791 (interactive (list t))
792 (gnus-namazu-update-indices gnus-namazu-index-directories force))
794 (defun gnus-namazu-update-indices (&optional directories force)
795 (when (setq directories
796 (delq nil (mapcar (lambda (d) (gnus-namazu/update-p d force))
798 (setq gnus-namazu/update-directories (cons force (cdr directories)))
799 (gnus-namazu-update-index (car directories) force)))
801 (defun gnus-namazu/update-sentinel (process event)
802 (let ((buffer (process-buffer process)))
803 (when (buffer-name buffer)
804 (with-current-buffer buffer
805 (gnus-namazu/mknmz-cleanup default-directory)
806 (goto-char (point-min))
808 ((re-search-forward "^ERROR:.*$" nil t)
809 (pop-to-buffer (current-buffer))
810 (message "Update index at %s...%s"
811 default-directory (match-string 0))
812 (setq gnus-namazu/update-directories nil))
813 ((and (eq 'exit (process-status process))
814 (zerop (process-exit-status process)))
815 (message "Update index at %s...done" default-directory)
816 (unless (or debug-on-error debug-on-quit)
817 (kill-buffer buffer)))))))
818 (setq gnus-namazu/update-process nil)
819 (unless (gnus-namazu-update-indices (cdr gnus-namazu/update-directories)
820 (car gnus-namazu/update-directories))
821 (gnus-namazu/make-directory-table t)))
824 (defun gnus-namazu-stop-update ()
825 "Stop the running indexer of Namazu."
827 (setq gnus-namazu/update-directories nil)
828 (and gnus-namazu/update-process
829 (processp gnus-namazu/update-process)
830 (kill-process gnus-namazu/update-process)))
832 (let (current-load-list)
833 (defadvice gnus-offer-save-summaries
834 (before gnus-namazu-kill-summary-buffers activate compile)
835 "Advised by `gnus-namazu'.
836 In order to avoid annoying questions, kill summary buffers which
837 generated by `gnus-namazu' itself before `gnus-offer-save-summaries'
839 (let ((buffers (buffer-list)))
841 (when (with-current-buffer (car buffers)
842 (and (eq major-mode 'gnus-summary-mode)
843 (gnus-ephemeral-group-p gnus-newsgroup-name)
844 (string-match gnus-namazu/group-name-regexp
845 gnus-newsgroup-name)))
846 (kill-buffer (car buffers)))
847 (setq buffers (cdr buffers))))))
850 (defun gnus-namazu-insinuate ()
852 'gnus-group-mode-hook
854 (define-key gnus-group-mode-map "\C-c\C-n" 'gnus-namazu-search)))
856 'gnus-summary-mode-hook
858 (define-key gnus-summary-mode-map "\C-c\C-n" 'gnus-namazu-search))))
860 (provide 'gnus-namazu)
862 ;; gnus-namazu.el ends here.