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