1 ;;; gnus-namazu.el --- Search mail with Namazu -*- coding: iso-2022-7bit; -*-
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 to browse its results with Gnus.
31 ;; Namazu is a full-text search engine intended for easy use. For
32 ;; more detail about Namazu, visit the following page:
39 ;; If this module has already been installed, only 3 steps are
40 ;; required to search articles with this module.
42 ;; (1) Install Namazu.
43 ;; (2) Start Gnus and type M-x gnus-namazu-create-index RET to make
45 ;; (3) In group buffer or in summary buffer, type C-c C-n query RET.
50 ;; Before installing this module, you must install Namazu.
52 ;; This file is a part of T-gnus but is not *YET* a part of Gnus.
53 ;; When you would like to use this module in Gnus (not T-gnus), put
54 ;; this file into the lisp/ directory in the Gnus source tree and run
55 ;; `make install'. And then, put the following expression into your
58 ;; (require 'gnus-namazu)
59 ;; (gnus-namazu-insinuate)
61 ;; In order to make index of articles with Namazu before using this
62 ;; module, type M-x gnus-namazu-create-index RET. Otherwise, you can
63 ;; create index by yourself with the following commands:
65 ;; % mkdir ~/News/namazu
66 ;; % mknmz -a -h -O ~/News/namazu ~/Mail ~/News/cache
68 ;; The first command makes the directory for index files, and the
69 ;; second command generates index files of mails and persistent
72 ;; In order to update indices for incoming articles, this module
73 ;; automatically runs mknmz, the indexer of Namazu, at an interval of
74 ;; 3 days; this period is set to `gnus-namazu-index-update-interval'.
76 ;; Indices will be updated when `gnus-namazu-search' is called. If
77 ;; you want to update indices everywhen Gnus is started, you can put
78 ;; the following expression to your ~/.gnus.
80 ;; (add-hook 'gnus-startup-hook 'gnus-namazu-update-all-indices)
82 ;; In order to control mknmz closely, disable the automatic updating
83 ;; feature and run mknmz by yourself. In this case, set nil to the
86 ;; (setq gnus-namazu-index-update-interval nil)
88 ;; When your index is put into the directory other than the default
89 ;; one (~/News/namazu), it is necessary to set its place to
90 ;; `gnus-namazu-index-directories' as follows:
92 ;; (setq gnus-namazu-index-directories
93 ;; (list (expand-file-name "~/namazu")))
98 (eval-when-compile (require 'cl))
104 ;; It is required for Mule 2.3. See the file Mule23@1934.en.
106 (autoload 'regexp-opt "regexp-opt"))
108 ;; To suppress byte-compile warning.
110 (defvar nnml-directory)
111 (defvar nnmh-directory))
114 (defgroup gnus-namazu nil
115 "Search nnmh and nnml groups in Gnus with Namazu."
118 :prefix "gnus-namazu-")
120 (defconst gnus-namazu-default-index-directory
121 (expand-file-name "namazu" gnus-directory)
122 "Default place of Namazu index files.")
124 (defcustom gnus-namazu-index-directories
126 (or (and (boundp 'gnus-namazu-index-directory)
127 (symbol-value 'gnus-namazu-index-directory))
128 (and (boundp 'nnir-namazu-index-directory)
129 (symbol-value 'nnir-namazu-index-directory))
130 gnus-namazu-default-index-directory))
131 "*Places of Namazu index files."
132 :type '(repeat directory)
135 (defcustom gnus-namazu-command
136 (or (and (boundp 'namazu-command)
137 (symbol-value 'namazu-command))
138 (and (boundp 'nnir-namazu-program)
139 (symbol-value 'nnir-namazu-program))
141 "*Name of the executable file of Namazu."
145 (defcustom gnus-namazu-additional-arguments nil
146 "*Additional arguments of Namazu.
147 The options `-q', `-a', and `-l' are always used, very few other
148 options make any sense in this context."
149 :type '(repeat string)
152 (defcustom gnus-namazu-index-update-interval
153 259200 ; 3 days == 259200 seconds.
154 "*Number of seconds between running the indexer of Namazu."
155 :type '(choice (const :tag "Never run the indexer" nil)
156 (integer :tag "Number of seconds"))
159 (defcustom gnus-namazu-make-index-command "mknmz"
160 "*Name of the executable file of the indexer of Namazu."
164 (defcustom gnus-namazu-make-index-arguments
166 (list "--all" "--mailnews" "--deny=^.*[^0-9].*$")
167 (when (or (and (boundp 'current-language-environment)
169 (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))
184 (if (boundp 'MULE) '*sjis* 'shift_jis)
185 (if (boundp 'MULE) '*euc-japan* 'euc-japan))
186 "*Coding system for Namazu process."
190 (defcustom gnus-namazu-need-path-normalization
191 (eq system-type 'windows-nt)
192 "*Non-nil means that outputs of namazu may contain a not normalized path."
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
227 (string 27 36 66 52 65 59 122 27 40 66)
228 (if (boundp 'MULE) '*iso-2022-jp* 'iso-2022-7bit))))
229 (not (string-match query
230 (gnus-summary-buffer-name
231 (encode-coding-string
232 (concat "nnvirtual:namazu-search?query=" query)
233 gnus-namazu-coding-system)))))
234 (let (current-load-list)
235 (defadvice gnus-summary-buffer-name
236 (before gnus-namazu-summary-buffer-name activate compile)
237 "Advised by `gnus-namazu' to handle encoded group names."
238 (ad-set-arg 0 (gnus-group-decoded-name (ad-get-arg 0))))))
240 (defmacro gnus-namazu/make-article (group number)
241 `(cons ,group ,number))
242 (defmacro gnus-namazu/article-group (x) `(car ,x))
243 (defmacro gnus-namazu/article-number (x) `(cdr ,x))
245 (defsubst gnus-namazu/indexed-servers ()
246 "Choice appropriate servers from opened ones, and return thier list."
248 (gnus-servers-using-backend 'nnml)
249 (gnus-servers-using-backend 'nnmh)))
251 (defsubst gnus-namazu/default-index-directory ()
252 (if (member gnus-namazu-default-index-directory
253 gnus-namazu-index-directories)
254 gnus-namazu-default-index-directory
255 (car gnus-namazu-index-directories)))
257 (defun gnus-namazu/setup ()
258 (and (boundp 'gnus-group-name-charset-group-alist)
259 (not (member (cons gnus-namazu/group-name-regexp
260 gnus-namazu-coding-system)
261 gnus-group-name-charset-group-alist))
262 (let ((pair (assoc gnus-namazu/group-name-regexp
263 gnus-group-name-charset-group-alist)))
265 (setcdr pair gnus-namazu-coding-system)
266 (push (cons gnus-namazu/group-name-regexp
267 gnus-namazu-coding-system)
268 gnus-group-name-charset-group-alist))))
269 (gnus-namazu-update-all-indices))
271 (defun gnus-namazu/server-directory (server)
272 "Return the top directory of the server SERVER."
273 (and (memq (car server) '(nnml nnmh))
274 (nnoo-change-server (car server) (nth 1 server) (nthcdr 2 server))
275 (file-name-as-directory
276 (expand-file-name (if (eq 'nnml (car server))
280 ;;; Functions to call Namazu.
281 (defsubst gnus-namazu/normalize-results ()
282 "Normalize file names returned by Namazu in this current buffer."
283 (goto-char (point-min))
285 (when (if gnus-namazu-need-path-normalization
286 (or (not (looking-at "/\\(.\\)|/"))
287 (replace-match "\\1:/"))
288 (eq ?~ (char-after (point))))
289 (insert (expand-file-name
290 (buffer-substring (gnus-point-at-bol) (gnus-point-at-eol))))
291 (delete-region (point) (gnus-point-at-eol)))
294 (defsubst gnus-namazu/call-namazu (query)
295 (let ((coding-system-for-read gnus-namazu-coding-system)
296 (coding-system-for-write gnus-namazu-coding-system)
297 (input-coding-system gnus-namazu-coding-system)
298 (output-coding-system gnus-namazu-coding-system)
299 (default-process-coding-system
300 (cons gnus-namazu-coding-system gnus-namazu-coding-system))
301 program-coding-system-alist
302 (file-name-coding-system gnus-namazu-coding-system)
303 (pathname-coding-system gnus-namazu-coding-system))
305 `(,gnus-namazu-command
306 nil ; input from /dev/null
308 nil ; don't redisplay
309 "-q" ; don't be verbose
310 "-a" ; show all matches
311 "-l" ; use list format
312 ,@gnus-namazu-additional-arguments
314 ,@gnus-namazu-index-directories))))
316 (defsubst gnus-namazu/group-prefixed-name (group method)
317 "Return the whole name from GROUP and METHOD."
318 (if gnus-namazu-case-sensitive-filesystem
319 (gnus-group-prefixed-name group method)
320 (let* ((orig (gnus-group-prefixed-name group method))
321 (name (downcase orig)))
323 (mapatoms (lambda (sym)
324 (when (string= name (downcase (symbol-name sym)))
325 (throw 'found-group (symbol-name sym))))
329 (defun gnus-namazu/real-group-name (cond str)
330 "Generate the real group name from the partial path, STR."
334 (dolist (group (gnus-namazu/possible-real-groups
335 (nnheader-replace-chars-in-string str ?/ ?.)))
336 (when (gnus-gethash group gnus-newsrc-hashtb)
337 (throw 'found-group group))))))
339 (defun gnus-namazu/possible-real-groups (str)
340 "Regard the string STR as the partial path of the cached article and
341 generate possible group names from it."
342 (if (string-match "_\\(_\\(_\\)?\\)?" str)
343 (let ((prefix (substring str 0 (match-beginning 0)))
344 (suffix (substring str (match-end 0))))
346 ((match-beginning 2) ;; The number of discoverd underscores = 3
348 (gnus-namazu/possible-real-groups (concat prefix "/__" suffix))
349 (gnus-namazu/possible-real-groups (concat prefix ".._" suffix))))
350 ((match-beginning 1) ;; The number of discoverd underscores = 2
352 (gnus-namazu/possible-real-groups (concat prefix "//" suffix))
353 (gnus-namazu/possible-real-groups (concat prefix ".." suffix))))
354 (t ;; The number of discoverd underscores = 1
355 (gnus-namazu/possible-real-groups (concat prefix "/" suffix)))))
356 (if (string-match "\\." str)
357 ;; Handle the first occurence of period.
358 (list (concat (substring str 0 (match-beginning 0))
360 (substring str (match-end 0)))
364 (defun gnus-namazu/search (groups query)
366 (let ((exit-status (gnus-namazu/call-namazu query)))
367 (unless (zerop exit-status)
368 (error "Namazu finished abnormally: %d" exit-status))
375 (when (setq dir (gnus-namazu/server-directory s))
376 (cons (file-name-as-directory dir) s)))
377 (gnus-namazu/indexed-servers)))))
378 (topdir-regexp (regexp-opt (mapcar 'car server-alist)))
379 (cache-regexp (concat
381 (file-name-as-directory
382 (expand-file-name gnus-cache-directory)))
383 "\\(.*\\)/\\([0-9]+\\)$"))
384 (agent-regexp (concat
386 (file-name-as-directory
387 (expand-file-name gnus-agent-directory)))
388 "\\(.*\\)/\\([0-9]+\\)$")))
389 (gnus-namazu/normalize-results)
390 (goto-char (point-min))
392 (let (server group file)
394 ;; Check the discoverd file is the persistent article.
395 (and (looking-at cache-regexp)
396 (setq file (match-string-no-properties 2)
397 group (gnus-namazu/real-group-name
398 (gnus-use-long-file-name 'not-cache)
399 (match-string-no-properties 1))))
400 ;; Check the discoverd file is covered by the agent.
401 (and (looking-at agent-regexp)
402 (setq file (match-string-no-properties 2)
403 group (gnus-namazu/real-group-name
404 nnmail-use-long-file-names
405 (match-string-no-properties 1))))
406 ;; Check the discovered file is managed by Gnus servers.
407 (and (looking-at topdir-regexp)
408 (setq file (buffer-substring-no-properties
409 (match-end 0) (gnus-point-at-eol))
410 server (cdr (assoc (match-string-no-properties 0)
412 ;; Check validity of the file name.
413 (string-match "/\\([0-9]+\\)\\'" file)
415 (setq group (substring file 0 (match-beginning 0))
416 file (match-string 1 file))
418 (gnus-namazu/group-prefixed-name
419 (if nnmail-use-long-file-names
421 (nnheader-replace-chars-in-string group
425 (member group groups))
426 (push (gnus-namazu/make-article group (string-to-number file))
429 (nreverse articles)))))
433 (defun gnus-namazu/get-target-groups ()
435 ((eq major-mode 'gnus-group-mode)
439 (gnus-group-process-prefix current-prefix-arg))
441 (prog1 gnus-group-marked (gnus-group-unmark-all-groups)))))
442 ((eq major-mode 'gnus-summary-mode)
443 ;; In Summary buffer.
444 (if current-prefix-arg
445 (list (gnus-read-group "Group: "))
447 (gnus-ephemeral-group-p gnus-newsgroup-name)
448 (string-match gnus-namazu/group-name-regexp gnus-newsgroup-name))
449 (cadr (assq 'gnus-namazu-target-groups
450 (gnus-info-method (gnus-get-info gnus-newsgroup-name))))
451 (list gnus-newsgroup-name))))))
453 (defun gnus-namazu/get-current-query ()
454 (and (eq major-mode 'gnus-summary-mode)
455 (gnus-ephemeral-group-p gnus-newsgroup-name)
456 (string-match gnus-namazu/group-name-regexp gnus-newsgroup-name)
457 (cadr (assq 'gnus-namazu-current-query
458 (gnus-info-method (gnus-get-info gnus-newsgroup-name))))))
460 (defvar gnus-namazu/read-query-original-buffer nil)
461 (defvar gnus-namazu/read-query-prompt nil)
462 (defvar gnus-namazu/read-query-history nil)
464 (defun gnus-namazu/get-current-subject ()
465 (and gnus-namazu/read-query-original-buffer
466 (bufferp gnus-namazu/read-query-original-buffer)
467 (with-current-buffer gnus-namazu/read-query-original-buffer
468 (when (eq major-mode 'gnus-summary-mode)
469 (let ((s (gnus-summary-article-subject)))
470 ;; Remove typically prefixes of mailing lists.
472 "^\\(\\[[^]]*[0-9]+\\]\\|([^)]*[0-9]+)\\)\\s-*" s)
473 (setq s (substring s (match-end 0))))
475 "^\\(Re\\(\\^?\\([0-9]+\\|\\[[0-9]+\\]\\)\\)?:\\s-*\\)+" s)
476 (setq s (substring s (match-end 0))))
477 (when (string-match "\\s-*(\\(re\\|was\\)\\b" s)
478 (setq s (substring s 0 (match-beginning 0))))
481 (defun gnus-namazu/get-current-from ()
482 (and gnus-namazu/read-query-original-buffer
483 (bufferp gnus-namazu/read-query-original-buffer)
484 (with-current-buffer gnus-namazu/read-query-original-buffer
485 (when (eq major-mode 'gnus-summary-mode)
486 (cadr (mail-extract-address-components
488 (gnus-summary-article-header))))))))
490 (defun gnus-namazu/get-current-to ()
491 (and gnus-namazu/read-query-original-buffer
492 (bufferp gnus-namazu/read-query-original-buffer)
493 (with-current-buffer gnus-namazu/read-query-original-buffer
494 (when (eq major-mode 'gnus-summary-mode)
495 (cadr (mail-extract-address-components
496 (cdr (assq 'To (mail-header-extra
497 (gnus-summary-article-header))))))))))
499 (defmacro gnus-namazu/minibuffer-prompt-end ()
500 (if (fboundp 'minibuffer-prompt-end)
501 '(minibuffer-prompt-end)
504 (defun gnus-namazu/message (string &rest arguments)
506 gnus-namazu/read-query-prompt
507 (buffer-substring (gnus-namazu/minibuffer-prompt-end)
509 (s2 (apply (function format) string arguments))
514 (message (if (>= w 0)
515 (concat s1 (make-string w ?\ ) s2)
517 (if (sit-for 0.3) (message s1))
520 (defun gnus-namazu/complete-query ()
524 ((and (re-search-backward "\\+\\([-a-z]*\\)" nil t)
525 (= pos (match-end 0)))
526 (let* ((partial (match-string 1))
530 (mapcar 'list gnus-namazu-field-keywords))))
533 (gnus-namazu/message "No completions of %s" partial))
534 ((= 1 (length completions))
535 (goto-char (match-beginning 1))
536 (delete-region (match-beginning 1) (match-end 1))
537 (insert (car completions) ":")
539 (gnus-namazu/message "Completed"))
541 (let ((x (try-completion partial (mapcar 'list completions))))
542 (if (string= x partial)
543 (if (and (eq last-command
544 'gnus-namazu/field-keyword-completion)
545 completion-auto-help)
546 (with-output-to-temp-buffer "*Completions*"
547 (display-completion-list completions))
548 (gnus-namazu/message "Sole completion"))
549 (goto-char (match-beginning 1))
550 (delete-region (match-beginning 1) (match-end 1))
552 (setq pos (point))))))))
553 ((and (looking-at "\\+subject:")
554 (= pos (match-end 0)))
555 (let ((s (gnus-namazu/get-current-subject)))
559 (setq pos (point)))))
560 ((and (looking-at "\\+from:")
561 (= pos (match-end 0)))
562 (let ((f (gnus-namazu/get-current-from)))
566 (setq pos (point)))))
567 ((and (looking-at "\\+to:")
568 (= pos (match-end 0)))
569 (let ((to (gnus-namazu/get-current-to)))
572 (insert "\"" to "\"")
573 (setq pos (point))))))
576 (defvar gnus-namazu/read-query-map
577 (let ((keymap (copy-keymap minibuffer-local-map)))
578 (define-key keymap "\t" 'gnus-namazu/complete-query)
581 (defun gnus-namazu/read-query (prompt &optional initial)
582 (let ((gnus-namazu/read-query-original-buffer (current-buffer))
583 (gnus-namazu/read-query-prompt prompt))
585 (when (setq initial (gnus-namazu/get-current-query))
586 (setq initial (cons initial 0))))
587 (read-from-minibuffer prompt initial gnus-namazu/read-query-map nil
588 'gnus-namazu/read-query-history)))
590 (defun gnus-namazu/highlight-words (query)
593 ;; Remove tokens for NOT search
594 (goto-char (point-min))
595 (while (re-search-forward "[
\e$B!!
\e(B \t\r\f\n]+not[
\e$B!!
\e(B \t\r\f\n]+\
596 \\([^
\e$B!!
\e(B \t\r\f\n\"{(/]+\\|\"[^\"]+\"\\|{[^}]+}\\|([^)]+)\\|/[^/]+/\\)+" nil t)
597 (delete-region (match-beginning 0) (match-end 0)))
598 ;; Remove tokens for Field search
599 (goto-char (point-min))
600 (while (re-search-forward "[
\e$B!!
\e(B \t\r\f\n]+\\+[^
\e$B!!
\e(B \t\r\f\n:]+:\
601 \\([^
\e$B!!
\e(B \t\r\f\n\"{(/]+\\|\"[^\"]+\"\\|{[^}]+}\\|([^)]+)\\|/[^/]+/\\)+" nil t)
602 (delete-region (match-beginning 0) (match-end 0)))
603 ;; Remove tokens for Regexp search
604 (goto-char (point-min))
605 (while (re-search-forward "/[^/]+/" nil t)
606 (delete-region (match-beginning 0) (match-end 0)))
607 ;; Remove brackets, double quote, asterisk and operators
608 (goto-char (point-min))
609 (while (re-search-forward "\\([(){}\"*]\\|\\b\\(and\\|or\\)\\b\\)" nil t)
610 (delete-region (match-beginning 0) (match-end 0)))
611 ;; Collect all keywords
613 (goto-char (point-min))
614 (while (re-search-forward "[^
\e$B!!
\e(B \t\r\f\n]+" nil t)
615 (push (match-string 0) query))
617 (list (list (regexp-opt query)
618 0 0 'gnus-namazu-query-highlight-face)))))
620 (defun gnus-namazu/truncate-article-list (articles)
621 (let ((hit (length articles)))
622 (when (and gnus-large-newsgroup
623 (> hit gnus-large-newsgroup))
624 (let* ((cursor-in-echo-area nil)
625 (input (read-from-minibuffer
627 Too many articles were retrieved. How many articles (max %d): "
629 (cons (number-to-string gnus-large-newsgroup) 0))))
630 (unless (string-match "\\`[ \t]*\\'" input)
631 (setcdr (nthcdr (min (1- (string-to-number input)) hit) articles)
636 (defun gnus-namazu-search (groups query)
637 "Search QUERY through GROUPS with Namazu,
638 and make a virtual group contains its results."
641 (gnus-namazu/get-target-groups)
642 (gnus-namazu/read-query "Enter query: ")))
644 (let ((articles (gnus-namazu/search groups query)))
646 (let ((real-groups groups)
648 (apply (function format)
649 "nnvirtual:namazu-search?query=%s&groups=%s&id=%d%d%d"
651 (if groups (mapconcat 'identity groups ",") "ALL")
653 (gnus-namazu/truncate-article-list articles)
656 (add-to-list 'real-groups (gnus-namazu/article-group a))))
657 ;; Generate virtual group which includes all results.
658 (when (fboundp 'gnus-group-decoded-name)
660 (encode-coding-string vgroup gnus-namazu-coding-system)))
662 (gnus-group-read-ephemeral-group
665 (nnvirtual-component-groups ,real-groups)
666 (gnus-namazu-target-groups ,groups)
667 (gnus-namazu-current-query ,query))
668 t (cons (current-buffer) (current-window-configuration)) t))
669 (when gnus-namazu-query-highlight
670 (gnus-group-set-parameter vgroup 'highlight-words
671 (gnus-namazu/highlight-words query)))
672 ;; Generate new summary buffer which contains search results.
673 (gnus-group-read-group
675 (sort (delq nil ;; Ad-hoc fix, to avoid wrong-type-argument error.
678 (nnvirtual-reverse-map-article
679 (gnus-namazu/article-group a)
680 (gnus-namazu/article-number a)))
683 (message "No entry."))))
685 (defmacro gnus-namazu/lock-file-name (&optional directory)
686 `(expand-file-name "NMZ.lock2" ,directory))
688 (defmacro gnus-namazu/status-file-name (&optional directory)
689 `(expand-file-name "NMZ.status" ,directory))
691 (defmacro gnus-namazu/index-file-name (&optional directory)
692 `(expand-file-name "NMZ.i" ,directory))
694 (defun gnus-namazu/mknmz-cleanup (directory)
695 (let ((lockfile (gnus-namazu/lock-file-name directory)))
696 (when (file-exists-p lockfile)
697 (delete-file lockfile)
698 (dolist (tmpfile (directory-files directory t "\\`NMZ\\..*\\.tmp\\'" t))
699 (delete-file tmpfile)))))
702 (defun gnus-namazu-create-index (directory &optional target-directories force)
703 "Create index under DIRECTORY."
706 (if (and current-prefix-arg (> (length gnus-namazu-index-directories) 1))
707 (completing-read "Directory: "
708 (mapcar 'list gnus-namazu-index-directories) nil t)
709 (gnus-namazu/default-index-directory))
711 (setq directory (file-name-as-directory (expand-file-name directory)))
712 (unless target-directories
713 (setq target-directories
715 (mapcar (lambda (dir)
716 (when (file-directory-p dir) dir))
718 (mapcar 'gnus-namazu/server-directory
719 (gnus-namazu/indexed-servers))
721 (expand-file-name gnus-cache-directory)
722 (expand-file-name gnus-agent-directory)))))))
723 (if (file-exists-p (gnus-namazu/lock-file-name directory))
725 (error "Found lock file: %s" (gnus-namazu/lock-file-name directory)))
727 (get-buffer-create (concat " *mknmz*" directory))
729 (unless (file-directory-p directory)
730 (make-directory directory t))
731 (setq default-directory directory)
732 (let ((args (append gnus-namazu-make-index-arguments
733 target-directories)))
734 (insert "% " gnus-namazu-make-index-command " "
735 (mapconcat 'identity args " ") "\n")
736 (goto-char (point-max))
738 (pop-to-buffer (current-buffer)))
739 (message "Make index at %s..." directory)
741 (apply 'call-process gnus-namazu-make-index-command nil t t args)
742 (gnus-namazu/mknmz-cleanup directory))
743 (message "Make index at %s...done" directory)
745 (kill-buffer (current-buffer)))))))
747 (defun gnus-namazu/lapse-seconds (start end)
748 "Return lapse seconds from START to END.
749 START and END are lists which represent time in Emacs-style."
750 (+ (* (- (car end) (car start)) 65536)
754 (defun gnus-namazu/index-old-p (directory)
755 "Return non-nil value when the index under the DIRECTORY is older
756 than the period that is set to `gnus-namazu-index-update-interval'"
757 (let ((file (gnus-namazu/index-file-name directory)))
758 (or (not (file-exists-p file))
759 (and (integerp gnus-namazu-index-update-interval)
760 (>= (gnus-namazu/lapse-seconds
761 (nth 5 (file-attributes file))
763 gnus-namazu-index-update-interval)))))
765 (defvar gnus-namazu/update-directories nil)
766 (defvar gnus-namazu/update-process nil)
768 (defun gnus-namazu/update-p (directory &optional force)
769 "Return the DIRECTORY when the index undef the DIRECTORY should be updated."
770 (setq directory (file-name-as-directory (expand-file-name directory)))
771 (labels ((error-message (format &rest args)
772 (apply (if force 'error 'message) format args)
774 (if gnus-namazu/update-process
775 (error-message "%s" "Can not run two update processes simultaneously")
777 (gnus-namazu/index-old-p directory))
778 (let ((status-file (gnus-namazu/status-file-name directory)))
779 (or (file-exists-p status-file)
780 (error-message "Can not find status file: %s" status-file)))
781 (let ((lock-file (gnus-namazu/lock-file-name directory)))
782 (or (not (file-exists-p lock-file))
783 (error-message "Found lock file: %s" lock-file)))
787 (defun gnus-namazu-update-index (directory &optional force)
788 "Update the index under the DIRECTORY."
791 (if (and current-prefix-arg (> (length gnus-namazu-index-directories) 1))
792 (completing-read "Directory: "
793 (mapcar 'list gnus-namazu-index-directories) nil t)
794 (gnus-namazu/default-index-directory))
796 (when (setq directory (gnus-namazu/update-p directory force))
797 (with-current-buffer (get-buffer-create (concat " *mknmz*" directory))
799 (unless (file-directory-p directory)
800 (make-directory directory t))
801 (setq default-directory directory)
802 (let ((proc (start-process gnus-namazu-make-index-command
804 gnus-namazu-make-index-command
805 (format "--update=%s" directory))))
807 (prog1 (setq gnus-namazu/update-process proc)
808 (process-kill-without-query proc)
809 (set-process-sentinel proc 'gnus-namazu/update-sentinel)
810 (add-hook 'kill-emacs-hook 'gnus-namazu-stop-update)
811 (message "Update index at %s..." directory))
812 (kill-buffer (current-buffer))
814 (error "Can not start %s"
815 gnus-namazu-make-index-command)))))))
818 (defun gnus-namazu-update-all-indices (&optional directories force)
819 "Update all indices which is set to `gnus-namazu-index-directories'."
820 (interactive (list nil t))
821 (when (setq directories
823 (lambda (d) (gnus-namazu/update-p d force))
824 (or directories gnus-namazu-index-directories))))
825 (setq gnus-namazu/update-directories (cdr directories))
826 (gnus-namazu-update-index (car directories))))
828 (defun gnus-namazu/update-sentinel (process event)
829 (let ((buffer (process-buffer process)))
830 (when (buffer-name buffer)
831 (with-current-buffer buffer
832 (gnus-namazu/mknmz-cleanup default-directory)
833 (when (and (eq 'exit (process-status process))
834 (zerop (process-exit-status process)))
835 (message "Update index at %s...done" default-directory)))
836 (unless (or debug-on-error debug-on-quit)
837 (kill-buffer buffer))))
838 (setq gnus-namazu/update-process nil)
839 (when gnus-namazu/update-directories
840 (gnus-namazu-update-all-indices gnus-namazu/update-directories)))
843 (defun gnus-namazu-stop-update ()
844 "Stop the running indexer of Namazu."
846 (setq gnus-namazu/update-directories nil)
847 (and gnus-namazu/update-process
848 (processp gnus-namazu/update-process)
849 (kill-process gnus-namazu/update-process)))
851 (let (current-load-list)
852 (defadvice gnus-offer-save-summaries
853 (before gnus-namazu-kill-summary-buffers activate compile)
854 "Advised by `gnus-namazu'.
855 In order to avoid annoying questions, kill summary buffers which
856 generated by `gnus-namazu' itself before `gnus-offer-save-summaries'
858 (let ((buffers (buffer-list)))
860 (when (with-current-buffer (car buffers)
861 (and (eq major-mode 'gnus-summary-mode)
862 (gnus-ephemeral-group-p gnus-newsgroup-name)
863 (string-match gnus-namazu/group-name-regexp
864 gnus-newsgroup-name)))
865 (kill-buffer (car buffers)))
866 (setq buffers (cdr buffers))))))
869 (defun gnus-namazu-insinuate ()
871 'gnus-group-mode-hook
873 (define-key gnus-group-mode-map "\C-c\C-n" 'gnus-namazu-search)))
875 'gnus-summary-mode-hook
877 (define-key gnus-summary-mode-map "\C-c\C-n" 'gnus-namazu-search))))
879 (provide 'gnus-namazu)
881 ;; gnus-namazu.el ends here.