(mh-send-uses-spost): New variable.
[bpt/emacs.git] / lisp / mh-e / mh-search.el
CommitLineData
dda00b2c 1;;; mh-search --- MH-Search mode
bdcfe844 2
44140699
BW
3;; Copyright (C) 1993, 1995,
4;; 2001, 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
bdcfe844 5
44140699 6;; Author: Indexed search by Satyaki Das <satyaki@theforce.stanford.edu>
bdcfe844
BW
7;; Maintainer: Bill Wohler <wohler@newt.com>
8;; Keywords: mail
9;; See: mh-e.el
10
11;; This file is part of GNU Emacs.
12
13;; GNU Emacs is free software; you can redistribute it and/or modify
14;; it under the terms of the GNU General Public License as published by
15;; the Free Software Foundation; either version 2, or (at your option)
16;; any later version.
17
18;; GNU Emacs is distributed in the hope that it will be useful,
19;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21;; GNU General Public License for more details.
22
23;; You should have received a copy of the GNU General Public License
24;; along with GNU Emacs; see the file COPYING. If not, write to the
3a35cf56
LK
25;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26;; Boston, MA 02110-1301, USA.
bdcfe844
BW
27
28;;; Commentary:
29
dda00b2c
BW
30;; Mode used to compose search criteria.
31
cee9f5c6
BW
32;; (1) The following search engines are supported:
33;; swish++
34;; swish-e
35;; mairix
36;; namazu
37;; pick
38;; grep
dda00b2c 39
44140699
BW
40;; (2) To use this package, you first have to build an index. Please
41;; read the documentation for `mh-search' to get started. That
42;; documentation will direct you to the specific instructions for
a4d7cec3 43;; your particular searcher.
bdcfe844
BW
44
45;;; Change Log:
46
bdcfe844
BW
47;;; Code:
48
dda00b2c 49(require 'mh-e)
a66894d8 50(mh-require-cl)
7094eefe 51
44140699 52(require 'gnus-util)
dda00b2c 53(require 'imenu)
bdcfe844 54
a4d7cec3
BW
55(defvar mh-searcher nil
56 "Cached value of chosen search program.")
44140699 57
a4d7cec3 58(defvar mh-search-function nil
bdcfe844 59 "Function which executes the search program.")
44140699 60
a4d7cec3 61(defvar mh-search-next-result-function nil
11db987f
BW
62 "Function to parse the next line of output.
63Expected to return a list of three strings: name of the folder,
64message number, and optionally the match.")
44140699 65
a4d7cec3 66(defvar mh-search-regexp-builder nil
3d7ca223 67 "Function used to construct search regexp.")
bdcfe844 68
c3d9274a
BW
69(defvar mh-index-folder "+mhe-index"
70 "Folder that contains the folders resulting from the index searches.")
71
44140699
BW
72(defvar mh-flists-results-folder "sequence"
73 "Subfolder for `mh-index-folder' where flists output is placed.")
c3d9274a 74
44140699 75(defvar mh-flists-sequence)
c3d9274a 76
44140699 77(defvar mh-flists-called-flag nil)
c3d9274a 78
44140699 79\f
c3d9274a 80
dda00b2c 81;;; MH-Folder Commands
bdcfe844 82
44140699 83;;;###mh-autoload
be397698
BW
84(defun mh-search (folder search-regexp
85 &optional redo-search-flag window-config)
44140699
BW
86 "Search your MH mail.
87
a4d7cec3
BW
88This command helps you find messages in your entire corpus of
89mail. You can search for messages to or from a particular person
90or about a particular subject. In fact, you can also search for
91messages containing selected strings in any arbitrary header
92field or any string found within the messages.
44140699 93
a4d7cec3
BW
94Out of the box, MH-E uses \"pick\" to find messages. With a
95little extra effort, you can set an indexing program which
96rewards you with extremely quick results. The drawback is that
97sometimes the index does not contain the words you're looking
98for. You can still use \"pick\" in these situations.
44140699
BW
99
100You are prompted for the FOLDER to search. This can be \"all\" to
a4d7cec3
BW
101search all folders. Note that the search works recursively on the
102listed folder.
44140699
BW
103
104Next, an MH-Search buffer appears where you can enter search
a4d7cec3 105criteria SEARCH-REGEXP.
44140699
BW
106
107 From:
108 To:
109 Cc:
110 Date:
111 Subject:
112 --------
113
114Edit this template by entering your search criteria in an
115appropriate header field that is already there, or create a new
116field yourself. If the string you're looking for could be
117anywhere in a message, then place the string underneath the row
118of dashes.
119
44140699
BW
120As an example, let's say that we want to find messages from
121Ginnean about horseback riding in the Kosciusko National
122Park (Australia) during January, 1994. Normally we would start
123with a broad search and narrow it down if necessary to produce a
124manageable amount of data, but we'll cut to the chase and create
a4d7cec3 125a fairly restrictive set of criteria as follows:\\<mh-search-mode-map>
44140699
BW
126
127 From: ginnean
128 To:
129 Cc:
130 Date: Jan 1994
131 Subject:
132 --------
133 horse
134 kosciusko
135
136As with MH-Letter mode, MH-Search provides commands like
a4d7cec3 137\\[mh-to-field] to help you fill in the blanks.\\<mh-folder-mode-map>
44140699
BW
138
139If you find that you do the same thing over and over when editing
140the search template, you may wish to bind some shortcuts to keys.
141This can be done with the variable `mh-search-mode-hook', which is
a4d7cec3 142called when \\[mh-search] is run on a new pattern.\\<mh-search-mode-map>
44140699
BW
143
144To perform the search, type \\[mh-index-do-search].
145
146Sometimes you're searching for text that is either not indexed,
147or hasn't been indexed yet. In this case you can override the
148default method with the pick method by running the command
149\\[mh-pick-do-search].
150
151The messages that are found are put in a temporary sub-folder of
152\"+mhe-index\" and are displayed in an MH-Folder buffer. This
153buffer is special because it displays messages from multiple
154folders; each set of messages from a given folder has a heading
155with the folder name.\\<mh-folder-mode-map>
bdcfe844 156
44140699 157The appearance of the heading can be modified by customizing the
a4d7cec3 158face `mh-search-folder'. You can jump back and forth between the
44140699
BW
159headings using the commands \\[mh-index-next-folder] and
160\\[mh-index-previous-folder].
c3d9274a 161
44140699
BW
162In addition, the command \\[mh-index-visit-folder] can be used to
163visit the folder of the message at point. Initially, only the
164messages that matched the search criteria are displayed in the
165folder. While the temporary buffer has its own set of message
166numbers, the actual messages numbers are shown in the visited
167folder. Thus, the command \\[mh-index-visit-folder] is useful to
168find the actual message number of an interesting message, or to
169view surrounding messages with the command \\[mh-rescan-folder].
bdcfe844 170
44140699 171Because this folder is temporary, you'll probably get in the
a4d7cec3
BW
172habit of killing it when you're done with \\[mh-kill-folder].
173
174You can regenerate the results by running this command with a
175prefix argument REDO-SEARCH-FLAG.
176
177Note: This command uses an \"X-MHE-Checksum:\" header field to
178cache the MD5 checksum of a message. This means that if an
179incoming message already contains an \"X-MHE-Checksum:\" field,
180that message might not be found by this command. The following
181\"procmail\" recipe avoids this problem by renaming the existing
182header field:
183
184 :0 wf
185 | formail -R \"X-MHE-Checksum\" \"X-Old-MHE-Checksum\"
bdcfe844 186
a4d7cec3 187Configuring Indexed Searches
2dcf34f9 188
44140699 189The command \\[mh-search] runs the command defined by the option
a4d7cec3 190`mh-search-program'. The default value is \"Auto-detect\" which
44140699
BW
191means that MH-E will automatically choose one of \"swish++\",
192\"swish-e\", \"mairix\", \"namazu\", \"pick\" and \"grep\" in
193that order. If, for example, you have both \"swish++\" and
194\"mairix\" installed and you want to use \"mairix\", then you can
195set this option to \"mairix\".
c3d9274a 196
44140699 197The documentation for the following commands describe how to set
a4d7cec3 198up the various indexing programs to use with MH-E.
a66894d8 199
44140699
BW
200 - `mh-swish++-execute-search'
201 - `mh-swish-execute-search'
202 - `mh-mairix-execute-search'
203 - `mh-namazu-execute-search'
204 - `mh-pick-execute-search'
205 - `mh-grep-execute-search'
924df208 206
a4d7cec3
BW
207In a program, if FOLDER is \"+\" or nil, then mail in all folders
208are searched. Optional argument WINDOW-CONFIG stores the window
209configuration that will be restored after the user quits the
210folder containing the index search results."
bdcfe844 211 (interactive
a4d7cec3 212 (list (progn
191c8741 213 (mh-find-path)
a4d7cec3
BW
214 ;; Yes, we do want to call mh-search-choose every time in case the
215 ;; user has switched the searcher manually.
216 (unless (mh-search-choose (and current-prefix-arg
217 mh-index-previous-search
218 (cadr mh-index-previous-search)))
219 (error "No search program found"))
a66894d8
BW
220 (or (and current-prefix-arg mh-index-sequence-search-flag)
221 (and current-prefix-arg (car mh-index-previous-search))
3d7ca223 222 (mh-prompt-for-folder "Search" "+" nil "all" t)))
44140699 223 (or (and current-prefix-arg (caddr mh-index-previous-search))
a4d7cec3 224 mh-search-regexp-builder
44140699 225 (read-string (format "%s regexp: "
a4d7cec3
BW
226 (upcase-initials (symbol-name mh-searcher)))))
227 current-prefix-arg
44140699
BW
228 (if (and (not (and current-prefix-arg
229 (caddr mh-index-previous-search)))
a4d7cec3 230 mh-search-regexp-builder)
3d7ca223
BW
231 (current-window-configuration)
232 nil)))
be397698
BW
233 (block mh-search
234 ;; Redoing a sequence search?
235 (when (and redo-search-flag mh-index-data mh-index-sequence-search-flag
236 (not mh-flists-called-flag))
237 (let ((mh-flists-called-flag t))
238 (apply #'mh-index-sequenced-messages mh-index-previous-search))
239 (return-from mh-search))
240 ;; We have fancy query parsing.
241 (when (symbolp search-regexp)
242 (mh-search-folder folder window-config)
243 (return-from mh-search))
244 ;; Begin search proper.
245 (mh-checksum-choose)
246 (let ((result-count 0)
247 (old-window-config (or window-config mh-previous-window-config))
248 (previous-search mh-index-previous-search)
249 (index-folder (format "%s/%s" mh-index-folder
250 (mh-index-generate-pretty-name search-regexp))))
251 ;; Create a new folder for the search results or recreate the old one...
252 (if (and redo-search-flag mh-index-previous-search)
253 (let ((buffer-name (buffer-name (current-buffer))))
254 (mh-process-or-undo-commands buffer-name)
255 (save-excursion (mh-exec-cmd-quiet nil "rmf" buffer-name))
256 (mh-exec-cmd-quiet nil "folder" "-create" "-fast" buffer-name)
257 (setq index-folder buffer-name))
258 (setq index-folder (mh-index-new-folder index-folder search-regexp)))
259
260 (let ((folder-path (format "%s%s" mh-user-path (substring folder 1)))
261 (folder-results-map (make-hash-table :test #'equal))
262 (origin-map (make-hash-table :test #'equal)))
263 ;; Run search program...
264 (message "Executing %s... " mh-searcher)
265 (funcall mh-search-function folder-path search-regexp)
266
267 ;; Parse searcher output.
268 (message "Processing %s output... " mh-searcher)
269 (goto-char (point-min))
270 (loop for next-result = (funcall mh-search-next-result-function)
271 while next-result
272 do (unless (eq next-result 'error)
273 (unless (gethash (car next-result) folder-results-map)
274 (setf (gethash (car next-result) folder-results-map)
275 (make-hash-table :test #'equal)))
276 (setf (gethash (cadr next-result)
277 (gethash (car next-result) folder-results-map))
278 t)))
279
280 ;; Copy the search results over.
281 (maphash #'(lambda (folder msgs)
282 (let ((cur (car (mh-translate-range folder "cur")))
283 (msgs (sort (loop for msg being the hash-keys of msgs
284 collect msg)
285 #'<)))
286 (mh-exec-cmd "refile" msgs "-src" folder
287 "-link" index-folder)
288 ;; Restore cur to old value, that refile changed
289 (when cur
290 (mh-exec-cmd-quiet nil "mark" folder "-add" "-zero"
291 "-sequence"
292 "cur" (format "%s" cur)))
293 (loop for msg in msgs
294 do (incf result-count)
295 (setf (gethash result-count origin-map)
296 (cons folder msg)))))
297 folder-results-map)
298
299 ;; Vist the results folder.
300 (mh-visit-folder index-folder () (list folder-results-map origin-map))
301
302 (goto-char (point-min))
303 (forward-line)
304 (mh-update-sequences)
305 (mh-recenter nil)
306
307 ;; Update the speedbar, if needed.
308 (when (mh-speed-flists-active-p)
309 (mh-speed-flists t mh-current-folder))
310
311 ;; Maintain history.
312 (when (or (and redo-search-flag previous-search) window-config)
313 (setq mh-previous-window-config old-window-config))
314 (setq mh-index-previous-search (list folder mh-searcher search-regexp))
315
316 ;; Write out data to disk.
317 (unless mh-flists-called-flag (mh-index-write-data))
318
319 (message "%s found %s matches in %s folders"
320 (upcase-initials (symbol-name mh-searcher))
321 (loop for msg-hash being hash-values of mh-index-data
322 sum (hash-table-count msg-hash))
323 (loop for msg-hash being hash-values of mh-index-data
324 count (> (hash-table-count msg-hash) 0)))))))
c3d9274a 325
dda00b2c 326;; Shush compiler.
42f8c37f 327(defvar pick-folder) ; XEmacs
dda00b2c 328
44140699
BW
329(defun mh-search-folder (folder window-config)
330 "Search FOLDER for messages matching a pattern.
331
332In a program, argument WINDOW-CONFIG is the current window
333configuration and is used when the search folder is dismissed."
334 (interactive (list (mh-prompt-for-folder "Search" mh-current-folder nil nil t)
335 (current-window-configuration)))
336 (let ((pick-folder (if (equal folder "+") mh-current-folder folder)))
337 (switch-to-buffer-other-window "search-pattern")
338 (if (or (zerop (buffer-size))
339 (not (y-or-n-p "Reuse pattern? ")))
340 (mh-make-pick-template)
341 (message ""))
342 (mh-make-local-vars 'mh-current-folder folder
343 'mh-previous-window-config window-config)
344 (message "%s" (substitute-command-keys
345 (concat "Type \\[mh-index-do-search] to search messages, "
346 "\\[mh-pick-do-search] to use pick, "
347 "\\[mh-help] for help")))))
348
349(defun mh-make-pick-template ()
350 "Initialize the current buffer with a template for a pick pattern."
351 (let ((inhibit-read-only t)) (erase-buffer))
352 (insert "From: \n"
353 "To: \n"
354 "Cc: \n"
355 "Date: \n"
356 "Subject: \n"
357 "---------\n")
358 (mh-search-mode)
359 (goto-char (point-min))
360 (dotimes (i 5)
361 (add-text-properties (point) (1+ (point)) '(front-sticky t))
d5dc8c56
BW
362 (add-text-properties (- (mh-line-end-position) 2)
363 (1- (mh-line-end-position))
44140699 364 '(rear-nonsticky t))
d5dc8c56 365 (add-text-properties (point) (1- (mh-line-end-position)) '(read-only t))
44140699
BW
366 (forward-line))
367 (add-text-properties (point) (1+ (point)) '(front-sticky t))
d5dc8c56 368 (add-text-properties (point) (1- (mh-line-end-position)) '(read-only t))
44140699
BW
369 (goto-char (point-max)))
370
dda00b2c
BW
371;; Sequence Searches
372
a4d7cec3 373;;;###mh-autoload
dda00b2c
BW
374(defun mh-index-new-messages (folders)
375 "Display unseen messages.
376
377If you use a program such as \"procmail\" to use \"rcvstore\" to file
378your incoming mail automatically, you can display new, unseen,
379messages using this command. All messages in the \"unseen\"
380sequence from the folders in `mh-new-messages-folders' are
381listed.
382
383With a prefix argument, enter a space-separated list of FOLDERS,
384or nothing to search all folders."
385 (interactive
386 (list (if current-prefix-arg
387 (split-string (read-string "Search folder(s) (default all): "))
388 mh-new-messages-folders)))
389 (mh-index-sequenced-messages folders mh-unseen-seq))
44140699 390
a4d7cec3 391;;;###mh-autoload
dda00b2c
BW
392(defun mh-index-ticked-messages (folders)
393 "Display ticked messages.
394
395All messages in `mh-tick-seq' from the folders in
396`mh-ticked-messages-folders' are listed.
397
398With a prefix argument, enter a space-separated list of FOLDERS,
399or nothing to search all folders."
400 (interactive
401 (list (if current-prefix-arg
402 (split-string (read-string "Search folder(s) (default all): "))
403 mh-ticked-messages-folders)))
404 (mh-index-sequenced-messages folders mh-tick-seq))
405
406;; Shush compiler.
42f8c37f
BW
407(defvar mh-mairix-folder) ; XEmacs
408(defvar mh-flists-search-folders) ; XEmacs
dda00b2c
BW
409
410;;;###mh-autoload
411(defun mh-index-sequenced-messages (folders sequence)
412 "Display messages in any sequence.
413
414All messages from the FOLDERS in `mh-new-messages-folders' in the
415SEQUENCE you provide are listed. With a prefix argument, enter a
416space-separated list of folders at the prompt, or nothing to
417search all folders."
418 (interactive
419 (list (if current-prefix-arg
420 (split-string (read-string "Search folder(s) (default all): "))
421 mh-new-messages-folders)
422 (mh-read-seq-default "Search" nil)))
423 (unless sequence (setq sequence mh-unseen-seq))
424 (let* ((mh-flists-search-folders folders)
425 (mh-flists-sequence sequence)
426 (mh-flists-called-flag t)
427 (mh-searcher 'flists)
428 (mh-search-function 'mh-flists-execute)
429 (mh-search-next-result-function 'mh-mairix-next-result)
430 (mh-mairix-folder mh-user-path)
431 (mh-search-regexp-builder nil)
432 (new-folder (format "%s/%s/%s" mh-index-folder
433 mh-flists-results-folder sequence))
434 (window-config (if (equal new-folder mh-current-folder)
435 mh-previous-window-config
436 (current-window-configuration)))
437 (redo-flag nil)
438 message)
439 (cond ((buffer-live-p (get-buffer new-folder))
440 ;; The destination folder is being visited. Trick `mh-search'
441 ;; into thinking that the folder resulted from a previous search.
442 (set-buffer new-folder)
443 (setq mh-index-previous-search (list folders mh-searcher sequence))
444 (setq redo-flag t))
445 ((mh-folder-exists-p new-folder)
446 ;; Folder exists but we don't have it open. That means they are
447 ;; stale results from a old flists search. Clear it out.
448 (mh-exec-cmd-quiet nil "rmf" new-folder)))
449 (setq message (mh-search "+" mh-flists-results-folder
450 redo-flag window-config)
451 mh-index-sequence-search-flag t
452 mh-index-previous-search (list folders mh-searcher sequence))
453 (mh-index-write-data)
454 (when (stringp message) (message "%s" message))))
455
456(defvar mh-flists-search-folders)
457
458(defun mh-flists-execute (&rest args)
459 "Execute flists.
460Search for messages belonging to `mh-flists-sequence' in the
461folders specified by `mh-flists-search-folders'. If
462`mh-recursive-folders-flag' is t, then the folders are searched
463recursively. All parameters ARGS are ignored."
464 (set-buffer (get-buffer-create mh-temp-index-buffer))
465 (erase-buffer)
466 (unless (executable-find "sh")
467 (error "Didn't find sh"))
468 (with-temp-buffer
469 (let ((seq (symbol-name mh-flists-sequence)))
470 (insert "for folder in `" (expand-file-name "flists" mh-progs) " "
471 (cond ((eq mh-flists-search-folders t)
472 (mh-quote-for-shell mh-inbox))
473 ((eq mh-flists-search-folders nil) "")
474 ((listp mh-flists-search-folders)
475 (loop for folder in mh-flists-search-folders
476 concat
477 (concat " " (mh-quote-for-shell folder)))))
478 (if mh-recursive-folders-flag " -recurse" "")
479 " -sequence " seq " -noshowzero -fast` ; do\n"
480 (expand-file-name "mhpath" mh-progs) " \"+$folder\" " seq "\n"
481 "done\n"))
482 (call-process-region
483 (point-min) (point-max) "sh" nil (get-buffer mh-temp-index-buffer))))
484
485;; Navigation
486
487;;;###mh-autoload
488(defun mh-index-next-folder (&optional backward-flag)
489 "Jump to the next folder marker.
490
491With non-nil optional argument BACKWARD-FLAG, jump to the previous
492group of results."
493 (interactive "P")
494 (if (null mh-index-data)
495 (message "Only applicable in an MH-E index search buffer")
496 (let ((point (point)))
497 (forward-line (if backward-flag 0 1))
498 (cond ((if backward-flag
499 (re-search-backward "^+" (point-min) t)
500 (re-search-forward "^+" (point-max) t))
501 (beginning-of-line))
502 ((and (if backward-flag
503 (goto-char (point-max))
504 (goto-char (point-min)))
505 nil))
506 ((if backward-flag
507 (re-search-backward "^+" (point-min) t)
508 (re-search-forward "^+" (point-max) t))
509 (beginning-of-line))
510 (t (goto-char point))))))
511
512;;;###mh-autoload
513(defun mh-index-previous-folder ()
514 "Jump to the previous folder marker."
515 (interactive)
516 (mh-index-next-folder t))
517
518;;;###mh-autoload
519(defun mh-index-visit-folder ()
520 "Visit original folder from where the message at point was found."
521 (interactive)
522 (unless mh-index-data
523 (error "Not in an index folder"))
524 (let (folder msg)
525 (save-excursion
526 (cond ((and (bolp) (eolp))
527 (ignore-errors (forward-line -1))
528 (setq msg (mh-get-msg-num t)))
d5dc8c56 529 ((equal (char-after (mh-line-beginning-position)) ?+)
dda00b2c 530 (setq folder (buffer-substring-no-properties
d5dc8c56
BW
531 (mh-line-beginning-position)
532 (mh-line-end-position))))
dda00b2c
BW
533 (t (setq msg (mh-get-msg-num t)))))
534 (when (not folder)
535 (setq folder (car (gethash (gethash msg mh-index-msg-checksum-map)
536 mh-index-checksum-origin-map))))
537 (when (or (not (get-buffer folder))
538 (y-or-n-p (format "Reuse buffer displaying %s? " folder)))
539 (mh-visit-folder
540 folder (loop for x being the hash-keys of (gethash folder mh-index-data)
541 when (mh-msg-exists-p x folder) collect x)))))
542
543\f
544
545;;; Search Menu
546
547(easy-menu-define
548 mh-pick-menu mh-search-mode-map "Menu for MH-E Search"
549 '("Search"
550 ["Perform Search" mh-index-do-search t]
551 ["Search with pick" mh-pick-do-search t]))
552
553\f
554
555;;; MH-Search Keys
556
44140699
BW
557;; If this changes, modify mh-search-mode-help-messages accordingly, below.
558(gnus-define-keys mh-search-mode-map
559 "\C-c?" mh-help
560 "\C-c\C-c" mh-index-do-search
561 "\C-c\C-p" mh-pick-do-search
562 "\C-c\C-f\C-b" mh-to-field
563 "\C-c\C-f\C-c" mh-to-field
dda00b2c 564 "\C-c\C-f\C-m" mh-to-field
44140699
BW
565 "\C-c\C-f\C-s" mh-to-field
566 "\C-c\C-f\C-t" mh-to-field
567 "\C-c\C-fb" mh-to-field
568 "\C-c\C-fc" mh-to-field
dda00b2c 569 "\C-c\C-fm" mh-to-field
44140699
BW
570 "\C-c\C-fs" mh-to-field
571 "\C-c\C-ft" mh-to-field)
572
dda00b2c
BW
573\f
574
575;;; MH-Search Help Messages
44140699
BW
576
577;; Group messages logically, more or less.
578(defvar mh-search-mode-help-messages
579 '((nil
dda00b2c
BW
580 "Perform search: \\[mh-index-do-search]\n"
581 "Search with pick: \\[mh-pick-do-search]\n\n"
44140699
BW
582 "Move to a field by typing C-c C-f C-<field>\n"
583 "where <field> is the first letter of the desired field\n"
584 "(except for From: which uses \"m\")."))
585 "Key binding cheat sheet.
586
587This is an associative array which is used to show the most common
588commands. The key is a prefix char. The value is one or more strings
589which are concatenated together and displayed in the minibuffer if ?
590is pressed after the prefix character. The special key nil is used to
591display the non-prefixed commands.
592
593The substitutions described in `substitute-command-keys' are performed
594as well.")
595
dda00b2c
BW
596\f
597
598;;; MH-Search Mode
599
44140699
BW
600(put 'mh-search-mode 'mode-class 'special)
601
602(define-derived-mode mh-search-mode fundamental-mode "MH-Search"
603 "Mode for creating search templates in MH-E.\\<mh-search-mode-map>
604
a4d7cec3
BW
605Edit this template by entering your search criteria in an
606appropriate header field that is already there, or create a new
607field yourself. If the string you're looking for could be
608anywhere in a message, then place the string underneath the row
609of dashes.
610
611To perform the search, type \\[mh-index-do-search].
612
613Sometimes you're searching for text that is either not indexed,
614or hasn't been indexed yet. In this case you can override the
615default method with the pick method by running the command
616\\[mh-pick-do-search].
44140699
BW
617
618The hook `mh-search-mode-hook' is called upon entry to this mode.
619
620\\{mh-search-mode-map}"
621
44140699 622 (easy-menu-add mh-pick-menu)
dda00b2c
BW
623 (mh-set-help mh-search-mode-help-messages))
624
625\f
626
627;;; MH-Search Commands
a66894d8 628
a4d7cec3
BW
629(defun mh-index-do-search (&optional searcher)
630 "Find messages using `mh-search-program'.
631If optional argument SEARCHER is present, use it instead of
632`mh-search-program'."
3d7ca223 633 (interactive)
a4d7cec3 634 (unless (mh-search-choose searcher) (error "No search program found"))
3d7ca223 635 (let* ((regexp-list (mh-pick-parse-search-buffer))
a4d7cec3 636 (pattern (funcall mh-search-regexp-builder regexp-list)))
3d7ca223 637 (if pattern
a4d7cec3 638 (mh-search mh-current-folder pattern nil mh-previous-window-config)
3d7ca223
BW
639 (error "No search terms"))))
640
44140699 641(defun mh-pick-do-search ()
a4d7cec3 642 "Find messages using \"pick\".
44140699
BW
643
644Uses the pick method described in `mh-pick-execute-search'."
645 (interactive)
646 (mh-index-do-search 'pick))
647
648(defun mh-pick-parse-search-buffer ()
649 "Parse the search buffer contents.
650The function returns a alist. The car of each element is either
651the header name to search in or nil to search the whole message.
652The cdr of the element is the pattern to search."
653 (save-excursion
654 (let ((pattern-list ())
655 (in-body-flag nil)
656 start begin)
657 (goto-char (point-min))
658 (while (not (eobp))
d5dc8c56 659 (if (search-forward "--------" (mh-line-end-position) t)
44140699
BW
660 (setq in-body-flag t)
661 (beginning-of-line)
662 (setq begin (point))
663 (setq start (if in-body-flag
664 (point)
d5dc8c56 665 (search-forward ":" (mh-line-end-position) t)
44140699
BW
666 (point)))
667 (push (cons (and (not in-body-flag)
668 (intern (downcase
669 (buffer-substring-no-properties
670 begin (1- start)))))
671 (mh-index-parse-search-regexp
672 (buffer-substring-no-properties
d5dc8c56 673 start (mh-line-end-position))))
44140699
BW
674 pattern-list))
675 (forward-line))
676 pattern-list)))
677
3d7ca223
BW
678(defun mh-index-parse-search-regexp (input-string)
679 "Construct parse tree for INPUT-STRING.
2dcf34f9
BW
680All occurrences of &, |, ! and ~ in INPUT-STRING are replaced by
681AND, OR and NOT as appropriate. Then the resulting string is
682parsed."
3d7ca223
BW
683 (let (input)
684 (with-temp-buffer
685 (insert input-string)
3d7ca223
BW
686 ;; replace tabs
687 (mh-replace-string "\t" " ")
688 ;; synonyms of AND
a66894d8 689 (mh-replace-string " AND " " and ")
3d7ca223
BW
690 (mh-replace-string "&" " and ")
691 (mh-replace-string " -and " " and ")
692 ;; synonyms of OR
a66894d8 693 (mh-replace-string " OR " " or ")
3d7ca223
BW
694 (mh-replace-string "|" " or ")
695 (mh-replace-string " -or " " or ")
696 ;; synonyms of NOT
a66894d8 697 (mh-replace-string " NOT " " not ")
3d7ca223
BW
698 (mh-replace-string "!" " not ")
699 (mh-replace-string "~" " not ")
700 (mh-replace-string " -not " " not ")
701 ;; synonyms of left brace
702 (mh-replace-string "(" " ( ")
703 (mh-replace-string " -lbrace " " ( ")
704 ;; synonyms of right brace
705 (mh-replace-string ")" " ) ")
706 (mh-replace-string " -rbrace " " ) ")
707 ;; get the normalized input
708 (setq input (format "( %s )" (buffer-substring (point-min) (point-max)))))
709
710 (let ((tokens (mh-index-add-implicit-ops (split-string input)))
711 (op-stack ())
712 (operand-stack ())
713 oper1)
714 (dolist (token tokens)
715 (cond ((equal token "(") (push 'paren op-stack))
716 ((equal token "not") (push 'not op-stack))
717 ((equal token "or") (push 'or op-stack))
718 ((equal token "and") (push 'and op-stack))
719 ((equal token ")")
720 (multiple-value-setq (op-stack operand-stack)
721 (mh-index-evaluate op-stack operand-stack))
722 (when (eq (car op-stack) 'not)
a66894d8 723 (setq op-stack (cdr op-stack))
3d7ca223
BW
724 (push `(not ,(pop operand-stack)) operand-stack))
725 (when (eq (car op-stack) 'and)
a66894d8 726 (setq op-stack (cdr op-stack))
3d7ca223
BW
727 (setq oper1 (pop operand-stack))
728 (push `(and ,(pop operand-stack) ,oper1) operand-stack)))
729 ((eq (car op-stack) 'not)
a66894d8 730 (setq op-stack (cdr op-stack))
3d7ca223
BW
731 (push `(not ,token) operand-stack)
732 (when (eq (car op-stack) 'and)
a66894d8 733 (setq op-stack (cdr op-stack))
3d7ca223
BW
734 (setq oper1 (pop operand-stack))
735 (push `(and ,(pop operand-stack) ,oper1) operand-stack)))
736 ((eq (car op-stack) 'and)
a66894d8 737 (setq op-stack (cdr op-stack))
3d7ca223
BW
738 (push `(and ,(pop operand-stack) ,token) operand-stack))
739 (t (push token operand-stack))))
740 (prog1 (pop operand-stack)
dda00b2c
BW
741 (when (or op-stack operand-stack)
742 (error "Invalid regexp: %s" input))))))
44140699 743
dda00b2c
BW
744(defun mh-index-add-implicit-ops (tokens)
745 "Add implicit operators in the list TOKENS."
746 (let ((result ())
747 (literal-seen nil)
748 current)
749 (while tokens
750 (setq current (pop tokens))
751 (cond ((or (equal current ")") (equal current "and") (equal current "or"))
752 (setq literal-seen nil)
753 (push current result))
754 ((and literal-seen
755 (push "and" result)
756 (setq literal-seen nil)
44140699 757 nil))
dda00b2c
BW
758 (t
759 (push current result)
760 (unless (or (equal current "(") (equal current "not"))
761 (setq literal-seen t)))))
762 (nreverse result)))
a66894d8 763
dda00b2c
BW
764(defun mh-index-evaluate (op-stack operand-stack)
765 "Read expression till starting paren based on OP-STACK and OPERAND-STACK."
766 (block mh-index-evaluate
767 (let (op oper1)
768 (while op-stack
769 (setq op (pop op-stack))
770 (cond ((eq op 'paren)
771 (return-from mh-index-evaluate (values op-stack operand-stack)))
772 ((eq op 'not)
773 (push `(not ,(pop operand-stack)) operand-stack))
774 ((or (eq op 'and) (eq op 'or))
775 (setq oper1 (pop operand-stack))
776 (push `(,op ,(pop operand-stack) ,oper1) operand-stack))))
777 (error "Ran out of tokens"))))
a66894d8 778
bdcfe844
BW
779\f
780
dda00b2c 781;;; Indexing Functions
3d7ca223 782
a4d7cec3
BW
783;; Support different search programs
784(defvar mh-search-choices
44140699
BW
785 '((swish++
786 mh-swish++-binary mh-swish++-execute-search mh-swish++-next-result
787 mh-swish++-regexp-builder)
788 (swish
789 mh-swish-binary mh-swish-execute-search mh-swish-next-result nil)
790 (mairix
791 mh-mairix-binary mh-mairix-execute-search mh-mairix-next-result
792 mh-mairix-regexp-builder)
793 (namazu
794 mh-namazu-binary mh-namazu-execute-search mh-namazu-next-result nil)
795 (pick
796 mh-pick-binary mh-pick-execute-search mh-pick-next-result
797 mh-pick-regexp-builder)
798 (grep
799 mh-grep-binary mh-grep-execute-search mh-grep-next-result nil))
a4d7cec3 800 "List of possible searcher choices.")
44140699 801
a4d7cec3
BW
802(defun mh-search-choose (&optional searcher)
803 "Choose a searching function.
44140699 804The side-effects of this function are that the variables
a4d7cec3
BW
805`mh-searcher', `mh-search-function', and
806`mh-search-next-result-function' are set according to the first
807searcher in `mh-search-choices' present on the system. If
808optional argument SEARCHER is present, use it instead of
809`mh-search-program'."
44140699 810 (block nil
a4d7cec3
BW
811 (let ((program-alist (cond (searcher
812 (list (assoc searcher mh-search-choices)))
813 (mh-search-program
44140699 814 (list
a4d7cec3
BW
815 (assoc mh-search-program mh-search-choices)))
816 (t mh-search-choices))))
44140699
BW
817 (while program-alist
818 (let* ((current (pop program-alist))
819 (executable (symbol-value (cadr current))))
820 (when executable
a4d7cec3
BW
821 (setq mh-searcher (car current))
822 (setq mh-search-function (nth 2 current))
823 (setq mh-search-next-result-function (nth 3 current))
824 (setq mh-search-regexp-builder (nth 4 current))
825 (return mh-searcher))))
44140699 826 nil)))
3d7ca223 827
dda00b2c 828;;; Swish++
3d7ca223 829
44140699
BW
830(defvar mh-swish++-binary (or (executable-find "search++")
831 (executable-find "search")))
832(defvar mh-swish++-directory ".swish++")
833(defvar mh-swish-folder nil)
834
44140699 835(defun mh-swish++-execute-search (folder-path search-regexp)
a4d7cec3 836 "Execute swish++.
44140699
BW
837
838In the examples below, replace \"/home/user/Mail\" with the path to
839your MH directory.
840
841First create the directory \"/home/user/Mail/.swish++\". Then create
842the file \"/home/user/Mail/.swish++/swish++.conf\" with the following
843contents:
844
845 IncludeMeta Bcc Cc Comments Content-Description From Keywords
846 IncludeMeta Newsgroups Resent-To Subject To
847 IncludeMeta Message-Id References In-Reply-To
848 IncludeFile Mail *
849 IndexFile /home/user/Mail/.swish++/swish++.index
850
851Use the following command line to generate the swish index. Run
852this daily from cron:
853
854 find /home/user/Mail -path /home/user/Mail/mhe-index -prune \\
855 -o -path /home/user/Mail/.swish++ -prune \\
856 -o -name \"[0-9]*\" -print \\
857 | index -c /home/user/Mail/.swish++/swish++.conf -
858
859This command does not index the folders that hold the results of your
860searches in \"+mhe-index\" since they tend to be ephemeral and the
861original messages are indexed anyway.
862
863On some systems (Debian GNU/Linux, for example), use \"index++\"
864instead of \"index\".
865
866In a program, FOLDER-PATH is the directory in which SEARCH-REGEXP is
867used to search."
9c2cf222 868 (set-buffer (get-buffer-create mh-temp-index-buffer))
3d7ca223 869 (erase-buffer)
44140699
BW
870 (unless mh-swish++-binary
871 (error "Set `mh-swish++-binary' appropriately"))
872 (call-process mh-swish++-binary nil '(t nil) nil
873 "-m" "10000"
874 (format "-i%s%s/swish++.index"
875 mh-user-path mh-swish++-directory)
876 search-regexp)
877 (goto-char (point-min))
878 (setq mh-swish-folder
879 (let ((last-char (substring folder-path (1- (length folder-path)))))
880 (if (equal last-char "/")
881 folder-path
882 (format "%s/" folder-path)))))
3d7ca223 883
44140699 884(defalias 'mh-swish++-next-result 'mh-swish-next-result)
3d7ca223 885
44140699
BW
886(defun mh-swish++-regexp-builder (regexp-list)
887 "Generate query for swish++.
888REGEXP-LIST is an alist of fields and values."
889 (let ((regexp ""))
890 (dolist (elem regexp-list)
891 (when (cdr elem)
892 (setq regexp (concat regexp " and "
893 (if (car elem) "(" "")
894 (if (car elem) (symbol-name (car elem)) "")
895 (if (car elem) " = " "")
896 (mh-swish++-print-regexp (cdr elem))
897 (if (car elem) ")" "")))))
898 (substring regexp 4)))
899
900(defun mh-swish++-print-regexp (expr)
901 "Return infix expression corresponding to EXPR."
902 (cond ((atom expr) (format "%s" expr))
903 ((eq (car expr) 'not)
904 (format "(not %s)" (mh-swish++-print-regexp (cadr expr))))
905 (t (format "(%s %s %s)" (mh-swish++-print-regexp (cadr expr))
906 (symbol-name (car expr))
907 (mh-swish++-print-regexp (caddr expr))))))
3d7ca223 908
dda00b2c 909;;; Swish
bdcfe844 910
44140699
BW
911(defvar mh-swish-binary (executable-find "swish-e"))
912(defvar mh-swish-directory ".swish")
bdcfe844 913
44140699 914(defun mh-swish-execute-search (folder-path search-regexp)
a4d7cec3 915 "Execute swish-e.
44140699
BW
916
917In the examples below, replace \"/home/user/Mail\" with the path
918to your MH directory.
919
920First create the directory \"/home/user/Mail/.swish\". Then
921create the file \"/home/user/Mail/.swish/config\" with the
922following contents:
923
924 DefaultContents TXT*
925 IndexDir /home/user/Mail
926 IndexFile /home/user/Mail/.swish/index
927 IndexName \"Mail Index\"
928 IndexDescription \"Mail Index\"
929 IndexPointer \"http://nowhere\"
930 IndexAdmin \"nobody\"
931 #MetaNames automatic
932 IndexReport 3
933 FollowSymLinks no
934 UseStemming no
935 IgnoreTotalWordCountWhenRanking yes
936 WordCharacters abcdefghijklmnopqrstuvwxyz0123456789-
937 BeginCharacters abcdefghijklmnopqrstuvwxyz
938 EndCharacters abcdefghijklmnopqrstuvwxyz0123456789
939 IgnoreLimit 50 1000
940 IndexComments 0
941 FileRules filename contains \\D
942 FileRules pathname contains /home/user/Mail/.swish
943 FileRules pathname contains /home/user/Mail/mhe-index
944
945This configuration does not index the folders that hold the
946results of your searches in \"+mhe-index\" since they tend to be
947ephemeral and the original messages are indexed anyway.
948
949If there are any directories you would like to ignore, append
950lines like the following to \"config\":
e495eaec 951
44140699
BW
952 FileRules pathname contains /home/user/Mail/scripts
953
954Use the following command line to generate the swish index. Run
955this daily from cron:
956
957 swish-e -c /home/user/Mail/.swish/config
e495eaec 958
2dcf34f9
BW
959In a program, FOLDER-PATH is the directory in which SEARCH-REGEXP
960is used to search."
9c2cf222 961 (set-buffer (get-buffer-create mh-temp-index-buffer))
bdcfe844 962 (erase-buffer)
44140699
BW
963 (unless mh-swish-binary
964 (error "Set `mh-swish-binary' appropriately"))
965 (call-process mh-swish-binary nil '(t nil) nil
966 "-w" search-regexp
967 "-f" (format "%s%s/index" mh-user-path mh-swish-directory))
968 (goto-char (point-min))
969 (setq mh-swish-folder
970 (let ((last-char (substring folder-path (1- (length folder-path)))))
971 (if (equal last-char "/")
972 folder-path
973 (format "%s/" folder-path)))))
bdcfe844 974
44140699
BW
975(defun mh-swish-next-result ()
976 "Get the next result from swish output."
bdcfe844 977 (prog1
c3d9274a 978 (block nil
44140699 979 (when (or (eobp) (equal (char-after (point)) ?.))
c3d9274a 980 (return nil))
44140699
BW
981 (when (equal (char-after (point)) ?#)
982 (return 'error))
d5dc8c56
BW
983 (let* ((start (search-forward " " (mh-line-end-position) t))
984 (end (search-forward " " (mh-line-end-position) t)))
44140699 985 (unless (and start end)
bdcfe844 986 (return 'error))
44140699
BW
987 (setq end (1- end))
988 (unless (file-exists-p (buffer-substring-no-properties start end))
c3d9274a 989 (return 'error))
44140699
BW
990 (unless (search-backward "/" start t)
991 (return 'error))
992 (list (let* ((s (buffer-substring-no-properties start (1+ (point)))))
993 (unless (string-match mh-swish-folder s)
994 (return 'error))
995 (if (and (string-match mh-user-path s)
996 (< (match-end 0) (1- (length s))))
997 (format "+%s"
998 (substring s (match-end 0) (1- (length s))))
999 (return 'error)))
1000 (let* ((s (buffer-substring-no-properties (1+ (point)) end))
8d2aa237
BW
1001 (n (ignore-errors (string-to-number s))))
1002 (if n n (return 'error)))
44140699 1003 nil)))
bdcfe844
BW
1004 (forward-line)))
1005
dda00b2c 1006;;; Mairix
3d7ca223
BW
1007
1008(defvar mh-mairix-binary (executable-find "mairix"))
1009(defvar mh-mairix-directory ".mairix")
1010(defvar mh-mairix-folder nil)
1011
1012(defun mh-mairix-execute-search (folder-path search-regexp-list)
a4d7cec3 1013 "Execute mairix.
3d7ca223 1014
2dcf34f9
BW
1015In the examples below, replace \"/home/user/Mail\" with the path
1016to your MH directory.
3d7ca223 1017
2dcf34f9
BW
1018First create the directory \"/home/user/Mail/.mairix\". Then
1019create the file \"/home/user/Mail/.mairix/config\" with the
1020following contents:
3d7ca223 1021
e495eaec 1022 base=/home/user/Mail
a1506d29 1023
e495eaec
BW
1024 # List of folders that should be indexed. 3 dots at the end means there
1025 # are subfolders within the folder
1026 mh=archive...:inbox:drafts:news:sent:trash
a1506d29 1027
e495eaec
BW
1028 vfolder_format=raw
1029 database=/home/user/Mail/mairix/database
3d7ca223 1030
2dcf34f9
BW
1031Use the following command line to generate the mairix index. Run
1032this daily from cron:
3d7ca223 1033
e495eaec 1034 mairix -f /home/user/Mail/.mairix/config
3d7ca223 1035
2dcf34f9
BW
1036In a program, FOLDER-PATH is the directory in which
1037SEARCH-REGEXP-LIST is used to search."
9c2cf222 1038 (set-buffer (get-buffer-create mh-temp-index-buffer))
3d7ca223
BW
1039 (erase-buffer)
1040 (unless mh-mairix-binary
836f2863 1041 (error "Set `mh-mairix-binary' appropriately"))
3d7ca223 1042 (apply #'call-process mh-mairix-binary nil '(t nil) nil
e495eaec 1043 "-r" "-f" (format "%s%s/config" mh-user-path mh-mairix-directory)
3d7ca223
BW
1044 search-regexp-list)
1045 (goto-char (point-min))
1046 (setq mh-mairix-folder
1047 (let ((last-char (substring folder-path (1- (length folder-path)))))
1048 (if (equal last-char "/")
1049 folder-path
1050 (format "%s/" folder-path)))))
1051
1052(defun mh-mairix-next-result ()
1053 "Return next result from mairix output."
1054 (prog1
1055 (block nil
1056 (when (or (eobp) (and (bolp) (eolp)))
1057 (return nil))
1058 (unless (eq (char-after) ?/)
924df208 1059 (return 'error))
3d7ca223
BW
1060 (let ((start (point))
1061 end msg-start)
d5dc8c56 1062 (setq end (mh-line-end-position))
3d7ca223
BW
1063 (unless (search-forward mh-mairix-folder end t)
1064 (return 'error))
1065 (goto-char (match-beginning 0))
1066 (unless (equal (point) start)
1067 (return 'error))
1068 (goto-char end)
1069 (unless (search-backward "/" start t)
1070 (return 'error))
1071 (setq msg-start (1+ (point)))
1072 (goto-char start)
1073 (unless (search-forward mh-user-path end t)
1074 (return 'error))
1075 (list (format "+%s" (buffer-substring-no-properties
1076 (point) (1- msg-start)))
8d2aa237
BW
1077 (string-to-number
1078 (buffer-substring-no-properties msg-start end))
11db987f 1079 nil)))
3d7ca223
BW
1080 (forward-line)))
1081
1082(defun mh-mairix-regexp-builder (regexp-list)
1083 "Generate query for mairix.
1084REGEXP-LIST is an alist of fields and values."
1085 (let ((result ()))
1086 (dolist (pair regexp-list)
1087 (when (cdr pair)
1088 (push
1089 (concat
1090 (cond ((eq (car pair) 'to) "t:")
1091 ((eq (car pair) 'from) "f:")
1092 ((eq (car pair) 'cc) "c:")
1093 ((eq (car pair) 'subject) "s:")
1094 ((eq (car pair) 'date) "d:")
1095 (t ""))
1096 (let ((sop (cdr (mh-mairix-convert-to-sop* (cdr pair))))
1097 (final ""))
1098 (dolist (conjunct sop)
1099 (let ((expr-list (cdr conjunct))
1100 (expr-string ""))
1101 (dolist (e expr-list)
e495eaec 1102 (setq expr-string (concat expr-string ","
3d7ca223
BW
1103 (if (atom e) "" "~")
1104 (if (atom e) e (cadr e)))))
e495eaec 1105 (setq final (concat final "/" (substring expr-string 1)))))
3d7ca223
BW
1106 (substring final 1)))
1107 result)))
1108 result))
1109
1110(defun mh-mairix-convert-to-sop* (expr)
1111 "Convert EXPR to sum of product form."
1112 (cond ((atom expr) `(or (and ,expr)))
1113 ((eq (car expr) 'or)
1114 (cons 'or
1115 (loop for e in (mapcar #'mh-mairix-convert-to-sop* (cdr expr))
1116 append (cdr e))))
1117 ((eq (car expr) 'and)
1118 (let ((conjuncts (mapcar #'mh-mairix-convert-to-sop* (cdr expr)))
1119 result next-factor)
1120 (setq result (pop conjuncts))
1121 (while conjuncts
1122 (setq next-factor (pop conjuncts))
1123 (setq result (let ((res ()))
1124 (dolist (t1 (cdr result))
1125 (dolist (t2 (cdr next-factor))
1126 (push `(and ,@(cdr t1) ,@(cdr t2)) res)))
1127 (cons 'or res))))
1128 result))
1129 ((atom (cadr expr)) `(or (and ,expr)))
1130 ((eq (caadr expr) 'not) (mh-mairix-convert-to-sop* (cadadr expr)))
1131 ((eq (caadr expr) 'and) (mh-mairix-convert-to-sop*
1132 `(or ,@(mapcar #'(lambda (x) `(not ,x))
1133 (cdadr expr)))))
1134 ((eq (caadr expr) 'or) (mh-mairix-convert-to-sop*
1135 `(and ,@(mapcar #'(lambda (x) `(not ,x))
1136 (cdadr expr)))))
1137 (t (error "Unreachable: %s" expr))))
1138
dda00b2c 1139;;; Namazu
3d7ca223 1140
44140699
BW
1141(defvar mh-namazu-binary (executable-find "namazu"))
1142(defvar mh-namazu-directory ".namazu")
1143(defvar mh-namazu-folder nil)
924df208 1144
44140699 1145(defun mh-namazu-execute-search (folder-path search-regexp)
a4d7cec3 1146 "Execute namazu.
af435184 1147
44140699
BW
1148In the examples below, replace \"/home/user/Mail\" with the path to
1149your MH directory.
a66894d8 1150
44140699
BW
1151First create the directory \"/home/user/Mail/.namazu\". Then create
1152the file \"/home/user/Mail/.namazu/mknmzrc\" with the following
1153contents:
a8a47814 1154
44140699
BW
1155 package conf; # Don't remove this line!
1156 $ADDRESS = 'user@localhost';
1157 $ALLOW_FILE = \"[0-9]*\";
1158 $EXCLUDE_PATH = \"^/home/user/Mail/(mhe-index|spam)\";
a8a47814 1159
44140699
BW
1160This configuration does not index the folders that hold the results of
1161your searches in \"+mhe-index\" since they tend to be ephemeral and
1162the original messages are indexed anyway.
a66894d8 1163
44140699
BW
1164Use the following command line to generate the namazu index. Run this
1165daily from cron:
a8a47814 1166
44140699
BW
1167 mknmz -f /home/user/Mail/.namazu/mknmzrc -O /home/user/Mail/.namazu \\
1168 /home/user/Mail
a8a47814 1169
44140699
BW
1170In a program, FOLDER-PATH is the directory in which SEARCH-REGEXP
1171is used to search."
1172 (let ((namazu-index-directory
1173 (format "%s%s" mh-user-path mh-namazu-directory)))
1174 (unless (file-exists-p namazu-index-directory)
1175 (error "Namazu directory %s not present" namazu-index-directory))
1176 (unless (executable-find mh-namazu-binary)
1177 (error "Set `mh-namazu-binary' appropriately"))
1178 (set-buffer (get-buffer-create mh-temp-index-buffer))
1179 (erase-buffer)
1180 (call-process mh-namazu-binary nil '(t nil) nil
1181 "-alR" search-regexp namazu-index-directory)
1182 (goto-char (point-min))
1183 (setq mh-namazu-folder
1184 (let ((last (substring folder-path (1- (length folder-path)))))
1185 (if (equal last "/")
1186 folder-path
1187 (format "%s/" folder-path))))))
924df208 1188
44140699
BW
1189(defun mh-namazu-next-result ()
1190 "Get the next result from namazu output."
1191 (prog1
1192 (block nil
1193 (when (eobp) (return nil))
1194 (let ((file-name (buffer-substring-no-properties
d5dc8c56 1195 (point) (mh-line-end-position))))
44140699
BW
1196 (unless (equal (string-match mh-namazu-folder file-name) 0)
1197 (return 'error))
1198 (unless (file-exists-p file-name)
1199 (return 'error))
1200 (string-match mh-user-path file-name)
1201 (let* ((folder/msg (substring file-name (match-end 0)))
1202 (mark (mh-search-from-end ?/ folder/msg)))
1203 (unless mark (return 'error))
1204 (list (format "+%s" (substring folder/msg 0 mark))
8d2aa237 1205 (let ((n (ignore-errors (string-to-number
44140699 1206 (substring folder/msg (1+ mark))))))
8d2aa237 1207 (if n n (return 'error)))
44140699
BW
1208 nil))))
1209 (forward-line)))
924df208 1210
dda00b2c 1211;;; Pick
bdcfe844 1212
44140699
BW
1213(defvar mh-index-pick-folder)
1214(defvar mh-pick-binary "pick")
1215(defconst mh-pick-single-dash '(cc date from subject to)
1216 "Search components that are supported by single-dash option in pick.")
bdcfe844 1217
44140699
BW
1218(defun mh-pick-execute-search (folder-path search-regexp)
1219 "Execute pick.
bdcfe844 1220
a4d7cec3
BW
1221Read \"pick(1)\" or the section Finding Messages with pick in the
1222MH book to find out more about how to enter the criteria (see URL
1223`http://www.ics.uci.edu/~mh/book/mh/finpic.htm').
e495eaec 1224
44140699
BW
1225In a program, FOLDER-PATH is the directory in which SEARCH-REGEXP
1226is used to search."
1227 (set-buffer (get-buffer-create mh-temp-index-buffer))
1228 (erase-buffer)
11db987f
BW
1229 (let ((folders
1230 (mh-folder-list (substring folder-path (length mh-user-path)))))
1231 (loop for folder in folders do
1232 (setq folder (concat "+" folder))
1233 (insert folder "\n")
1234 (apply #'call-process (expand-file-name "pick" mh-progs)
1235 nil '(t nil) nil folder "-list" search-regexp)))
44140699 1236 (goto-char (point-min)))
e495eaec 1237
44140699
BW
1238(defun mh-pick-next-result ()
1239 "Return the next pick search result."
11db987f
BW
1240 (prog1
1241 (block nil
1242 (when (eobp) (return nil))
d5dc8c56 1243 (when (search-forward-regexp "^\+" (mh-line-end-position) t)
11db987f 1244 (setq mh-index-pick-folder
d5dc8c56
BW
1245 (buffer-substring-no-properties (mh-line-beginning-position)
1246 (mh-line-end-position)))
11db987f 1247 (return 'error))
d5dc8c56 1248 (unless (search-forward-regexp "^[1-9][0-9]*$" (mh-line-end-position) t)
11db987f
BW
1249 (return 'error))
1250 (list mh-index-pick-folder
1251 (string-to-number
d5dc8c56
BW
1252 (buffer-substring-no-properties (mh-line-beginning-position)
1253 (mh-line-end-position)))
11db987f 1254 nil))
44140699 1255 (forward-line)))
bdcfe844 1256
44140699
BW
1257;; All implementations of pick have special options -cc, -date, -from and
1258;; -subject that allow to search for corresponding components. Any other
1259;; component is searched using option --COMPNAME, for example: `pick
1260;; --x-mailer mh-e'. Mailutils "pick" supports this option using a certain
1261;; kludge, but it prefers the following syntax for this purpose:
1262;; "--component=COMPNAME --pattern=PATTERN".
1263;; -- Sergey Poznyakoff, Aug 2003
1264(defun mh-pick-regexp-builder (pattern-list)
1265 "Generate pick search expression from PATTERN-LIST."
1266 (let ((result ()))
1267 (dolist (pattern pattern-list)
1268 (when (cdr pattern)
1269 (setq result `(,@result "-and" "-lbrace"
1270 ,@(mh-pick-construct-regexp
1271 (if (and (mh-variant-p 'mu-mh) (car pattern))
1272 (format "--pattern=%s" (cdr pattern))
1273 (cdr pattern))
1274 (if (car pattern)
1275 (cond
1276 ((mh-variant-p 'mu-mh)
1277 (format "--component=%s" (car pattern)))
1278 ((member (car pattern) mh-pick-single-dash)
1279 (format "-%s" (car pattern)))
1280 (t
1281 (format "--%s" (car pattern))))
1282 "-search"))
1283 "-rbrace"))))
1284 (cdr result)))
1285
1286(defun mh-pick-construct-regexp (expr component)
1287 "Construct pick compatible expression corresponding to EXPR.
1288COMPONENT is the component to search."
1289 (cond ((atom expr) (list component expr))
1290 ((eq (car expr) 'and)
1291 `("-lbrace" ,@(mh-pick-construct-regexp (cadr expr) component) "-and"
1292 ,@(mh-pick-construct-regexp (caddr expr) component) "-rbrace"))
1293 ((eq (car expr) 'or)
1294 `("-lbrace" ,@(mh-pick-construct-regexp (cadr expr) component) "-or"
1295 ,@(mh-pick-construct-regexp (caddr expr) component) "-rbrace"))
1296 ((eq (car expr) 'not)
1297 `("-lbrace" "-not" ,@(mh-pick-construct-regexp (cadr expr) component)
1298 "-rbrace"))
1299 (t (error "Unknown operator %s seen" (car expr)))))
bdcfe844 1300
dda00b2c 1301;;; Grep
c3d9274a 1302
44140699 1303(defvar mh-grep-binary (executable-find "grep"))
bdcfe844 1304
44140699 1305(defun mh-grep-execute-search (folder-path search-regexp)
a4d7cec3
BW
1306 "Execute grep.
1307
1308Unlike the other search methods, this method does not use the
1309MH-Search buffer. Instead, you simply enter a regular expression
1310in the minibuffer. For help in constructing regular expressions,
1311see your man page for \"grep\".
bdcfe844 1312
2dcf34f9
BW
1313In a program, FOLDER-PATH is the directory in which SEARCH-REGEXP
1314is used to search."
9c2cf222 1315 (set-buffer (get-buffer-create mh-temp-index-buffer))
bdcfe844 1316 (erase-buffer)
44140699
BW
1317 (call-process mh-grep-binary nil '(t nil) nil
1318 "-i" "-r" search-regexp folder-path)
1319 (goto-char (point-min)))
bdcfe844 1320
44140699
BW
1321(defun mh-grep-next-result ()
1322 "Read the next result.
1323Parse it and return the message folder, message index and the
1324match. If no other matches left then return nil. If the current
1325record is invalid return 'error."
bdcfe844
BW
1326 (prog1
1327 (block nil
44140699 1328 (when (eobp)
bdcfe844 1329 (return nil))
d5dc8c56
BW
1330 (let ((eol-pos (mh-line-end-position))
1331 (bol-pos (mh-line-beginning-position))
44140699
BW
1332 folder-start msg-end)
1333 (goto-char bol-pos)
1334 (unless (search-forward mh-user-path eol-pos t)
bdcfe844 1335 (return 'error))
44140699
BW
1336 (setq folder-start (point))
1337 (unless (search-forward ":" eol-pos t)
bdcfe844 1338 (return 'error))
44140699
BW
1339 (let ((match (buffer-substring-no-properties (point) eol-pos)))
1340 (forward-char -1)
1341 (setq msg-end (point))
1342 (unless (search-backward "/" folder-start t)
1343 (return 'error))
1344 (list (format "+%s" (buffer-substring-no-properties
1345 folder-start (point)))
8d2aa237
BW
1346 (let ((n (ignore-errors (string-to-number
1347 (buffer-substring-no-properties
1348 (1+ (point)) msg-end)))))
1349 (if n n (return 'error)))
44140699 1350 match))))
bdcfe844
BW
1351 (forward-line)))
1352
1353\f
1354
dda00b2c
BW
1355;;; Folder Utilities
1356
1357;;;###mh-autoload
1358(defun mh-index-group-by-folder ()
1359 "Partition the messages based on source folder.
1360Returns an alist with the the folder names in the car and the cdr
1361being the list of messages originally from that folder."
1362 (save-excursion
1363 (goto-char (point-min))
1364 (let ((result-table (make-hash-table :test #'equal)))
1365 (loop for msg being hash-keys of mh-index-msg-checksum-map
1366 do (push msg (gethash (car (gethash
1367 (gethash msg mh-index-msg-checksum-map)
1368 mh-index-checksum-origin-map))
1369 result-table)))
1370 (loop for x being the hash-keys of result-table
1371 collect (cons x (nreverse (gethash x result-table)))))))
1372
1373;;;###mh-autoload
1374(defun mh-index-insert-folder-headers ()
1375 "Annotate the search results with original folder names."
1376 (let ((cur-msg (mh-get-msg-num nil))
1377 (old-buffer-modified-flag (buffer-modified-p))
1378 (buffer-read-only nil)
1379 current-folder last-folder)
1380 (goto-char (point-min))
1381 (while (not (eobp))
1382 (setq current-folder (car (gethash (gethash (mh-get-msg-num nil)
1383 mh-index-msg-checksum-map)
1384 mh-index-checksum-origin-map)))
1385 (when (and current-folder (not (equal current-folder last-folder)))
1386 (insert (if last-folder "\n" "") current-folder "\n")
1387 (setq last-folder current-folder))
1388 (forward-line))
1389 (when cur-msg
1390 (mh-notate-cur)
1391 (mh-goto-msg cur-msg t))
1392 (set-buffer-modified-p old-buffer-modified-flag))
1393 (mh-index-create-imenu-index))
1394
1395;;;###mh-autoload
1396(defun mh-index-delete-folder-headers ()
1397 "Delete the folder headers."
1398 (let ((cur-msg (mh-get-msg-num nil))
1399 (old-buffer-modified-flag (buffer-modified-p))
1400 (buffer-read-only nil))
1401 (while (and (not cur-msg) (not (eobp)))
1402 (forward-line)
1403 (setq cur-msg (mh-get-msg-num nil)))
1404 (goto-char (point-min))
1405 (while (not (eobp))
1406 (if (or (char-equal (char-after) ?+) (char-equal (char-after) 10))
1407 (delete-region (point) (progn (forward-line) (point)))
1408 (forward-line)))
1409 (when cur-msg (mh-goto-msg cur-msg t t))
1410 (set-buffer-modified-p old-buffer-modified-flag)))
1411
92486f28
BW
1412(mh-require 'which-func nil t)
1413
dda00b2c 1414;; Shush compiler.
42f8c37f 1415(defvar which-func-mode) ; < Emacs 22, XEmacs
dda00b2c
BW
1416
1417;;;###mh-autoload
1418(defun mh-index-create-imenu-index ()
1419 "Create alist of folder names and positions in index folder buffers."
1420 (save-excursion
1421 (if (boundp 'which-func-mode)
1422 (setq which-func-mode t))
1423 (let ((alist ()))
1424 (goto-char (point-min))
1425 (while (re-search-forward "^+" nil t)
1426 (save-excursion
1427 (beginning-of-line)
1428 (push (cons (buffer-substring-no-properties
d5dc8c56 1429 (point) (mh-line-end-position))
dda00b2c
BW
1430 (set-marker (make-marker) (point)))
1431 alist)))
1432 (setq imenu--index-alist (nreverse alist)))))
1433
1434;;;###mh-autoload
1435(defun mh-search-p ()
1436 "Non-nil means that this folder was generated by searching."
1437 mh-index-data)
1438
1439;; Shush compiler
42f8c37f 1440(defvar mh-speed-flists-inhibit-flag) ; XEmacs
dda00b2c
BW
1441
1442;;;###mh-autoload
1443(defun mh-index-execute-commands ()
1444 "Delete/refile the actual messages.
1445The copies in the searched folder are then deleted/refiled to get
1446the desired result. Before deleting the messages we make sure
1447that the message being deleted is identical to the one that the
1448user has marked in the index buffer."
1449 (save-excursion
1450 (let ((folders ())
1451 (mh-speed-flists-inhibit-flag t))
1452 (maphash
1453 (lambda (folder msgs)
1454 (push folder folders)
1455 (if (not (get-buffer folder))
1456 ;; If source folder not open, just delete the messages...
1457 (apply #'mh-exec-cmd "rmm" folder (mh-coalesce-msg-list msgs))
1458 ;; Otherwise delete the messages in the source buffer...
1459 (save-excursion
1460 (set-buffer folder)
1461 (let ((old-refile-list mh-refile-list)
1462 (old-delete-list mh-delete-list))
1463 (setq mh-refile-list nil
1464 mh-delete-list msgs)
1465 (unwind-protect (mh-execute-commands)
1466 (setq mh-refile-list
1467 (mapcar (lambda (x)
1468 (cons (car x)
1469 (loop for y in (cdr x)
1470 unless (memq y msgs) collect y)))
1471 old-refile-list)
1472 mh-delete-list
1473 (loop for x in old-delete-list
1474 unless (memq x msgs) collect x))
1475 (mh-set-folder-modified-p (mh-outstanding-commands-p))
1476 (when (mh-outstanding-commands-p)
1477 (mh-notate-deleted-and-refiled)))))))
1478 (mh-index-matching-source-msgs (append (loop for x in mh-refile-list
1479 append (cdr x))
1480 mh-delete-list)
1481 t))
1482 folders)))
bdcfe844 1483
44140699
BW
1484(defun mh-index-generate-pretty-name (string)
1485 "Given STRING generate a name which is suitable for use as a folder name.
1486White space from the beginning and end are removed. All spaces in
1487the name are replaced with underscores and all / are replaced
1488with $. If STRING is longer than 20 it is truncated too. STRING
1489could be a list of strings in which case they are concatenated to
1490construct the base name."
1491 (with-temp-buffer
1492 (if (stringp string)
1493 (insert string)
1494 (when (car string) (insert (car string)))
1495 (dolist (s (cdr string))
1496 (insert "_" s)))
1497 (setq string (mh-replace-string "-lbrace" " "))
1498 (setq string (mh-replace-string "-rbrace" " "))
a4d7cec3 1499 (setq string (mh-replace-string "-search" " "))
44140699
BW
1500 (subst-char-in-region (point-min) (point-max) ?( ? t)
1501 (subst-char-in-region (point-min) (point-max) ?) ? t)
1502 (subst-char-in-region (point-min) (point-max) ?- ? t)
1503 (goto-char (point-min))
1504 (while (and (not (eobp)) (memq (char-after) '(? ?\t ?\n ?\r ?_)))
1505 (delete-char 1))
1506 (goto-char (point-max))
1507 (while (and (not (bobp)) (memq (char-before) '(? ?\t ?\n ?\r ?_)))
1508 (delete-backward-char 1))
1509 (subst-char-in-region (point-min) (point-max) ? ?_ t)
1510 (subst-char-in-region (point-min) (point-max) ?\t ?_ t)
1511 (subst-char-in-region (point-min) (point-max) ?\n ?_ t)
1512 (subst-char-in-region (point-min) (point-max) ?\r ?_ t)
1513 (subst-char-in-region (point-min) (point-max) ?/ ?$ t)
1514 (let ((out (truncate-string-to-width (buffer-string) 20)))
a4d7cec3 1515 (cond ((eq mh-searcher 'flists)
44140699
BW
1516 (format "%s/%s" mh-flists-results-folder mh-flists-sequence))
1517 ((equal out mh-flists-results-folder) (concat out "1"))
1518 (t out)))))
bdcfe844 1519
44140699
BW
1520(defun mh-folder-exists-p (folder)
1521 "Check if FOLDER exists."
1522 (and (mh-folder-name-p folder)
1523 (save-excursion
1524 (with-temp-buffer
1525 (mh-exec-cmd-output "folder" nil "-fast" "-nocreate" folder)
1526 (goto-char (point-min))
c1def6ea 1527 (looking-at (format "+?%s" folder))))))
c3d9274a 1528
44140699
BW
1529(defun mh-msg-exists-p (msg folder)
1530 "Check if MSG exists in FOLDER."
1531 (file-exists-p (format "%s%s/%s" mh-user-path (substring folder 1) msg)))
bdcfe844 1532
44140699
BW
1533(defun mh-index-new-folder (name search-regexp)
1534 "Return a folder name based on NAME for search results of SEARCH-REGEXP.
bdcfe844 1535
44140699
BW
1536If folder NAME already exists and was generated for the same
1537SEARCH-REGEXP then it is reused.
bdcfe844 1538
44140699
BW
1539Otherwise if the folder NAME was generated from a different
1540search then check if NAME<2> can be used. Otherwise try NAME<3>.
1541This is repeated till we find a new folder name.
bdcfe844 1542
44140699
BW
1543If the folder returned doesn't exist then it is created."
1544 (unless (mh-folder-name-p name)
1545 (error "The argument should be a valid MH folder name"))
1546 (let ((chosen-name
1547 (loop for i from 1
1548 for candidate = (if (equal i 1) name (format "%s<%s>" name i))
1549 when (or (not (mh-folder-exists-p candidate))
1550 (equal (mh-index-folder-search-regexp candidate)
1551 search-regexp))
1552 return candidate)))
1553 ;; Do pending refiles/deletes...
1554 (when (get-buffer chosen-name)
1555 (mh-process-or-undo-commands chosen-name))
1556 ;; Recreate folder...
1557 (save-excursion (mh-exec-cmd-quiet nil "rmf" chosen-name))
1558 (mh-exec-cmd-quiet nil "folder" "-create" "-fast" chosen-name)
1559 (mh-remove-from-sub-folders-cache chosen-name)
1560 (when (boundp 'mh-speed-folder-map)
1561 (mh-speed-add-folder chosen-name))
1562 chosen-name))
3d7ca223 1563
44140699
BW
1564(defun mh-index-folder-search-regexp (folder)
1565 "If FOLDER was created by a index search, return the search regexp.
1566Return nil if FOLDER doesn't exist or the .mhe_index file is
1567garbled."
1568 (ignore-errors
1569 (with-temp-buffer
1570 (insert-file-contents
1571 (format "%s%s/%s" mh-user-path (substring folder 1) mh-index-data-file))
1572 (goto-char (point-min))
1573 (forward-list 3)
1574 (cadr (read (current-buffer))))))
3d7ca223 1575
bdcfe844
BW
1576\f
1577
dda00b2c 1578;;; Sequence Support
bdcfe844 1579
44140699
BW
1580;;;###mh-autoload
1581(defun mh-index-create-sequences ()
1582 "Mirror sequences present in source folders in index folder."
1583 (let ((seq-hash (make-hash-table :test #'equal))
1584 (seq-list ()))
1585 (loop for folder being the hash-keys of mh-index-data
1586 do (setf (gethash folder seq-hash)
1587 (mh-create-sequence-map
1588 (mh-read-folder-sequences folder nil))))
1589 (dolist (msg (mh-translate-range mh-current-folder "all"))
1590 (let* ((checksum (gethash msg mh-index-msg-checksum-map))
1591 (pair (gethash checksum mh-index-checksum-origin-map))
1592 (ofolder (car pair))
1593 (omsg (cdr pair)))
1594 (loop for seq in (ignore-errors
1595 (gethash omsg (gethash ofolder seq-hash)))
1596 do (if (assoc seq seq-list)
1597 (push msg (cdr (assoc seq seq-list)))
1598 (push (list seq msg) seq-list)))))
1599 (loop for seq in seq-list
1600 do (apply #'mh-exec-cmd "mark" mh-current-folder
1601 "-sequence" (symbol-name (car seq)) "-add"
1602 (mapcar #'(lambda (x) (format "%s" x)) (cdr seq))))))
bdcfe844 1603
c3d9274a 1604;;;###mh-autoload
44140699
BW
1605(defun mh-create-sequence-map (seq-list)
1606 "Return a map from msg number to list of sequences in which it is present.
1607SEQ-LIST is an assoc list whose keys are sequence names and whose
1608cdr is the list of messages in that sequence."
1609 (loop with map = (make-hash-table)
1610 for seq in seq-list
1611 when (and (not (memq (car seq) (mh-unpropagated-sequences)))
1612 (mh-valid-seq-p (car seq)))
1613 do (loop for msg in (cdr seq)
1614 do (push (car seq) (gethash msg map)))
1615 finally return map))
bdcfe844 1616
44140699
BW
1617;;;###mh-autoload
1618(defun mh-index-add-to-sequence (seq msgs)
1619 "Add to SEQ the messages in the list MSGS.
1620This function updates the source folder sequences. Also makes an
1621attempt to update the source folder buffer if we have it open."
1622 ;; Don't need to do anything for cur
1623 (save-excursion
1624 (when (and (not (memq seq (mh-unpropagated-sequences)))
1625 (mh-valid-seq-p seq))
1626 (let ((folders ())
1627 (mh-speed-flists-inhibit-flag t))
1628 (maphash (lambda (folder msgs)
1629 (push folder folders)
1630 ;; Add messages to sequence in source folder...
1631 (apply #'mh-exec-cmd-quiet nil "mark" folder
1632 "-add" "-nozero" "-sequence" (symbol-name seq)
1633 (mapcar (lambda (x) (format "%s" x))
1634 (mh-coalesce-msg-list msgs)))
1635 ;; Update source folder buffer if we have it open...
1636 (when (get-buffer folder)
1637 (save-excursion
1638 (set-buffer folder)
1639 (mh-put-msg-in-seq msgs seq))))
1640 (mh-index-matching-source-msgs msgs))
1641 folders))))
bdcfe844 1642
44140699
BW
1643;;;###mh-autoload
1644(defun mh-index-delete-from-sequence (seq msgs)
1645 "Delete from SEQ the messages in MSGS.
1646This function updates the source folder sequences. Also makes an
1647attempt to update the source folder buffer if present."
1648 (save-excursion
1649 (when (and (not (memq seq (mh-unpropagated-sequences)))
1650 (mh-valid-seq-p seq))
1651 (let ((folders ())
1652 (mh-speed-flists-inhibit-flag t))
1653 (maphash (lambda (folder msgs)
1654 (push folder folders)
1655 ;; Remove messages from sequence in source folder...
1656 (apply #'mh-exec-cmd-quiet nil "mark" folder
1657 "-del" "-nozero" "-sequence" (symbol-name seq)
1658 (mapcar (lambda (x) (format "%s" x))
1659 (mh-coalesce-msg-list msgs)))
1660 ;; Update source folder buffer if we have it open...
1661 (when (get-buffer folder)
1662 (save-excursion
1663 (set-buffer folder)
1664 (mh-delete-msg-from-seq msgs seq t))))
1665 (mh-index-matching-source-msgs msgs))
1666 folders))))
c3d9274a 1667
44140699
BW
1668(defvar mh-unpropagated-sequences '(cur range subject search)
1669 "List of sequences that aren't preserved.")
c3d9274a 1670
44140699
BW
1671(defun mh-unpropagated-sequences ()
1672 "Return a list of sequences that aren't propagated to the source folders.
1673It is just the sequences in the variable
1674`mh-unpropagated-sequences' in addition to the
1675Previous-Sequence (see mh-profile 5)."
1676 (if mh-previous-seq
1677 (cons mh-previous-seq mh-unpropagated-sequences)
1678 mh-unpropagated-sequences))
bdcfe844 1679
44140699
BW
1680(defun mh-index-matching-source-msgs (msgs &optional delete-from-index-data)
1681 "Return a table of original messages and folders for messages in MSGS.
1682If optional argument DELETE-FROM-INDEX-DATA is non-nil, then each
1683of the messages, whose counter-part is found in some source
1684folder, is removed from `mh-index-data'."
1685 (let ((table (make-hash-table :test #'equal)))
1686 (dolist (msg msgs)
1687 (let* ((checksum (gethash msg mh-index-msg-checksum-map))
1688 (pair (gethash checksum mh-index-checksum-origin-map)))
1689 (when (and checksum (car pair) (cdr pair)
1690 (mh-index-match-checksum (cdr pair) (car pair) checksum))
1691 (push (cdr pair) (gethash (car pair) table))
1692 (when delete-from-index-data
1693 (remhash (cdr pair) (gethash (car pair) mh-index-data))))))
1694 table))
bdcfe844 1695
44140699
BW
1696(defun mh-index-match-checksum (msg folder checksum)
1697 "Check if MSG in FOLDER has X-MHE-Checksum header value of CHECKSUM."
1698 (with-temp-buffer
1699 (mh-exec-cmd-output mh-scan-prog nil "-width" "80"
1700 "-format" "%{x-mhe-checksum}\n" folder msg)
bdcfe844 1701 (goto-char (point-min))
d5dc8c56
BW
1702 (string-equal (buffer-substring-no-properties
1703 (point) (mh-line-end-position))
44140699 1704 checksum)))
bdcfe844
BW
1705
1706\f
1707
dda00b2c 1708;;; Serialization of Index Data
44140699
BW
1709
1710(defun mh-index-write-data ()
1711 "Write index data to file."
1712 (ignore-errors
1713 (unless (eq major-mode 'mh-folder-mode)
1714 (error "Can't be called from folder in \"%s\"" major-mode))
1715 (let ((data mh-index-data)
1716 (msg-checksum-map mh-index-msg-checksum-map)
1717 (checksum-origin-map mh-index-checksum-origin-map)
1718 (previous-search mh-index-previous-search)
1719 (sequence-search-flag mh-index-sequence-search-flag)
1720 (outfile (concat buffer-file-name mh-index-data-file))
1721 (print-length nil)
1722 (print-level nil))
1723 (with-temp-file outfile
1724 (mh-index-write-hashtable
1725 data (lambda (x) (loop for y being the hash-keys of x collect y)))
1726 (mh-index-write-hashtable msg-checksum-map #'identity)
1727 (mh-index-write-hashtable checksum-origin-map #'identity)
1728 (pp previous-search (current-buffer)) (insert "\n")
1729 (pp sequence-search-flag (current-buffer)) (insert "\n")))))
1730
1731(defun mh-index-write-hashtable (table proc)
1732 "Write TABLE to `current-buffer'.
1733PROC is used to serialize the values corresponding to the hash
1734table keys."
1735 (pp (loop for x being the hash-keys of table
1736 collect (cons x (funcall proc (gethash x table))))
1737 (current-buffer))
1738 (insert "\n"))
1739
924df208 1740;;;###mh-autoload
44140699
BW
1741(defun mh-index-read-data ()
1742 "Read index data from file."
1743 (ignore-errors
1744 (unless (eq major-mode 'mh-folder-mode)
1745 (error "Can't be called from folder in \"%s\"" major-mode))
1746 (let ((infile (concat buffer-file-name mh-index-data-file))
1747 t1 t2 t3 t4 t5)
1748 (with-temp-buffer
1749 (insert-file-contents-literally infile)
1750 (goto-char (point-min))
1751 (setq t1 (mh-index-read-hashtable
1752 (lambda (data)
1753 (loop with table = (make-hash-table :test #'equal)
1754 for x in data do (setf (gethash x table) t)
1755 finally return table)))
1756 t2 (mh-index-read-hashtable #'identity)
1757 t3 (mh-index-read-hashtable #'identity)
1758 t4 (read (current-buffer))
1759 t5 (read (current-buffer))))
1760 (setq mh-index-data t1
1761 mh-index-msg-checksum-map t2
1762 mh-index-checksum-origin-map t3
1763 mh-index-previous-search t4
1764 mh-index-sequence-search-flag t5))))
1765
1766(defun mh-index-read-hashtable (proc)
1767 "From BUFFER read a hash table serialized as a list.
1768PROC is used to convert the value to actual data."
1769 (loop with table = (make-hash-table :test #'equal)
1770 for pair in (read (current-buffer))
1771 do (setf (gethash (car pair) table) (funcall proc (cdr pair)))
1772 finally return table))
bdcfe844
BW
1773
1774\f
1775
dda00b2c
BW
1776;;; Checksum Routines
1777
1778;; A few different checksum programs are supported. The supported
1779;; programs are:
44140699 1780
44140699
BW
1781;; 1. md5sum
1782;; 2. md5
1783;; 3. openssl
dda00b2c
BW
1784
1785;; To add support for your favorite checksum program add a clause to
1786;; the cond statement in mh-checksum-choose. This should set the
1787;; variable mh-checksum-cmd to the command line needed to run the
1788;; checsum program and should set mh-checksum-parser to a function
1789;; which returns a cons cell containing the message number and
1790;; checksum string.
44140699
BW
1791
1792(defvar mh-checksum-cmd)
1793(defvar mh-checksum-parser)
1794
1795(defun mh-checksum-choose ()
1796 "Check if a program to create a checksum is present."
1797 (unless (boundp 'mh-checksum-cmd)
1798 (let ((exec-path (append '("/sbin" "/usr/sbin") exec-path)))
1799 (cond ((executable-find "md5sum")
1800 (setq mh-checksum-cmd (list (executable-find "md5sum")))
1801 (setq mh-checksum-parser #'mh-md5sum-parser))
1802 ((executable-find "openssl")
1803 (setq mh-checksum-cmd (list (executable-find "openssl") "md5"))
1804 (setq mh-checksum-parser #'mh-openssl-parser))
1805 ((executable-find "md5")
1806 (setq mh-checksum-cmd (list (executable-find "md5")))
1807 (setq mh-checksum-parser #'mh-md5-parser))
1808 (t (error "No suitable checksum program"))))))
1809
1810(defun mh-md5sum-parser ()
1811 "Parse md5sum output."
d5dc8c56
BW
1812 (let ((begin (mh-line-beginning-position))
1813 (end (mh-line-end-position))
44140699
BW
1814 first-space last-slash)
1815 (setq first-space (search-forward " " end t))
1816 (goto-char end)
1817 (setq last-slash (search-backward "/" begin t))
1818 (cond ((and first-space last-slash)
8d2aa237
BW
1819 (cons (string-to-number (buffer-substring-no-properties
1820 (1+ last-slash) end))
44140699
BW
1821 (buffer-substring-no-properties begin (1- first-space))))
1822 (t (cons nil nil)))))
1823
1824(defun mh-openssl-parser ()
1825 "Parse openssl output."
d5dc8c56
BW
1826 (let ((begin (mh-line-beginning-position))
1827 (end (mh-line-end-position))
44140699
BW
1828 last-space last-slash)
1829 (goto-char end)
1830 (setq last-space (search-backward " " begin t))
1831 (setq last-slash (search-backward "/" begin t))
1832 (cond ((and last-slash last-space)
8d2aa237
BW
1833 (cons (string-to-number (buffer-substring-no-properties
1834 (1+ last-slash) (1- last-space)))
44140699
BW
1835 (buffer-substring-no-properties (1+ last-space) end))))))
1836
1837(defalias 'mh-md5-parser 'mh-openssl-parser)
1838
1839;;;###mh-autoload
1840(defun mh-index-update-maps (folder &optional origin-map)
1841 "Annotate all as yet unannotated messages in FOLDER with their MD5 hash.
1842As a side effect msg -> checksum map is updated. Optional
1843argument ORIGIN-MAP is a hashtable which maps each message in the
1844index folder to the original folder and message from whence it
1845was copied. If present the checksum -> (origin-folder,
1846origin-index) map is updated too."
1847 (clrhash mh-index-msg-checksum-map)
1848 (save-excursion
1849 ;; Clear temp buffer
1850 (set-buffer (get-buffer-create mh-temp-checksum-buffer))
1851 (erase-buffer)
1852 ;; Run scan to check if any messages needs MD5 annotations at all
1853 (with-temp-buffer
1854 (mh-exec-cmd-output mh-scan-prog nil "-width" "80"
1855 "-format" "%(msg)\n%{x-mhe-checksum}\n"
1856 folder "all")
1857 (goto-char (point-min))
1858 (let (msg checksum)
1859 (while (not (eobp))
1860 (setq msg (buffer-substring-no-properties
d5dc8c56 1861 (point) (mh-line-end-position)))
44140699
BW
1862 (forward-line)
1863 (save-excursion
1864 (cond ((not (string-match "^[0-9]*$" msg)))
1865 ((eolp)
1866 ;; need to compute checksum
1867 (set-buffer mh-temp-checksum-buffer)
1868 (insert mh-user-path (substring folder 1) "/" msg "\n"))
1869 (t
1870 ;; update maps
1871 (setq checksum (buffer-substring-no-properties
d5dc8c56 1872 (point) (mh-line-end-position)))
8d2aa237 1873 (let ((msg (string-to-number msg)))
44140699
BW
1874 (set-buffer folder)
1875 (mh-index-update-single-msg msg checksum origin-map)))))
1876 (forward-line))))
1877 ;; Run checksum program if needed
1878 (unless (and (eobp) (bobp))
1879 (apply #'mh-xargs mh-checksum-cmd)
1880 (goto-char (point-min))
1881 (while (not (eobp))
1882 (let* ((intermediate (funcall mh-checksum-parser))
1883 (msg (car intermediate))
1884 (checksum (cdr intermediate)))
1885 (when msg
1886 ;; annotate
1887 (mh-exec-cmd "anno" folder msg "-component" "X-MHE-Checksum"
1888 "-nodate" "-text" checksum "-inplace")
1889 ;; update maps
1890 (save-excursion
1891 (set-buffer folder)
1892 (mh-index-update-single-msg msg checksum origin-map)))
1893 (forward-line)))))
1894 (mh-index-write-data))
1895
1896(defun mh-index-update-single-msg (msg checksum origin-map)
1897 "Update various maps for one message.
1898MSG is a index folder message, CHECKSUM its MD5 hash and
1899ORIGIN-MAP, if non-nil, a hashtable containing which maps each
1900message in the index folder to the folder and message that it was
1901copied from. The function updates the hash tables
1902`mh-index-msg-checksum-map' and `mh-index-checksum-origin-map'.
1903
1904This function should only be called in the appropriate index
1905folder buffer."
1af028e6
SD
1906 (cond ((gethash checksum mh-index-checksum-origin-map)
1907 (when origin-map
1908 (let* ((intermediate (gethash msg origin-map))
1909 (ofolder (car intermediate))
1910 (omsg (cdr intermediate)))
1911 ;; This is most probably a duplicate. So eliminate it.
1912 (call-process "rm" nil nil nil
1913 (format "%s%s/%s" mh-user-path
1914 (substring mh-current-folder 1) msg))
1915 (when (gethash ofolder mh-index-data)
1916 (remhash omsg (gethash ofolder mh-index-data))))))
44140699
BW
1917 (t
1918 (setf (gethash msg mh-index-msg-checksum-map) checksum)
1af028e6 1919 (when (and origin-map (gethash msg origin-map))
44140699
BW
1920 (setf (gethash checksum mh-index-checksum-origin-map)
1921 (gethash msg origin-map))))))
1922
1af028e6 1923
44140699 1924(provide 'mh-search)
bdcfe844 1925
cee9f5c6
BW
1926;; Local Variables:
1927;; indent-tabs-mode: nil
1928;; sentence-end-double-space: nil
1929;; End:
bdcfe844 1930
cee9f5c6 1931;; arch-tag: 607762ad-0dff-4fe1-a27e-6c0dde0dcc47
44140699 1932;;; mh-search ends here