1 ;;; mh-search --- MH-Search mode
3 ;; Copyright (C) 1993, 1995, 2001-2011 Free Software Foundation, Inc.
5 ;; Author: Indexed search by Satyaki Das <satyaki@theforce.stanford.edu>
6 ;; Maintainer: Bill Wohler <wohler@newt.com>
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;; Mode used to compose search criteria.
29 ;; (1) The following search engines are supported:
37 ;; (2) To use this package, you first have to build an index. Please
38 ;; read the documentation for `mh-search' to get started. That
39 ;; documentation will direct you to the specific instructions for
40 ;; your particular searcher.
52 (defvar mh-searcher nil
53 "Cached value of chosen search program.")
55 (defvar mh-search-function nil
56 "Function which executes the search program.")
58 (defvar mh-search-next-result-function nil
59 "Function to parse the next line of output.
60 Expected to return a list of three strings: name of the folder,
61 message number, and optionally the match.")
63 (defvar mh-search-regexp-builder nil
64 "Function used to construct search regexp.")
66 (defvar mh-index-folder
"+mhe-index"
67 "Folder that contains the folders resulting from the index searches.")
69 (defvar mh-flists-results-folder
"sequence"
70 "Subfolder for `mh-index-folder' where flists output is placed.")
72 (defvar mh-flists-sequence
)
74 (defvar mh-flists-called-flag nil
)
78 ;;; MH-Folder Commands
81 (defun mh-search (folder search-regexp
82 &optional redo-search-flag window-config
)
85 This command helps you find messages in your entire corpus of
86 mail. You can search for messages to or from a particular person
87 or about a particular subject. In fact, you can also search for
88 messages containing selected strings in any arbitrary header
89 field or any string found within the messages.
91 Out of the box, MH-E uses \"pick\" to find messages. With a
92 little extra effort, you can set an indexing program which
93 rewards you with extremely quick results. The drawback is that
94 sometimes the index does not contain the words you're looking
95 for. You can still use \"pick\" in these situations.
97 You are prompted for the FOLDER to search. This can be \"all\" to
98 search all folders. Note that the search works recursively on the
101 Next, an MH-Search buffer appears where you can enter search
102 criteria SEARCH-REGEXP.
111 Edit this template by entering your search criteria in an
112 appropriate header field that is already there, or create a new
113 field yourself. If the string you're looking for could be
114 anywhere in a message, then place the string underneath the row
117 As an example, let's say that we want to find messages from
118 Ginnean about horseback riding in the Kosciusko National
119 Park (Australia) during January, 1994. Normally we would start
120 with a broad search and narrow it down if necessary to produce a
121 manageable amount of data, but we'll cut to the chase and create
122 a fairly restrictive set of criteria as follows:\\<mh-search-mode-map>
133 As with MH-Letter mode, MH-Search provides commands like
134 \\[mh-to-field] to help you fill in the blanks.\\<mh-folder-mode-map>
136 If you find that you do the same thing over and over when editing
137 the search template, you may wish to bind some shortcuts to keys.
138 This can be done with the variable `mh-search-mode-hook', which is
139 called when \\[mh-search] is run on a new pattern.\\<mh-search-mode-map>
141 To perform the search, type \\[mh-index-do-search].
143 Sometimes you're searching for text that is either not indexed,
144 or hasn't been indexed yet. In this case you can override the
145 default method with the pick method by running the command
146 \\[mh-pick-do-search].
148 The messages that are found are put in a temporary sub-folder of
149 \"+mhe-index\" and are displayed in an MH-Folder buffer. This
150 buffer is special because it displays messages from multiple
151 folders; each set of messages from a given folder has a heading
152 with the folder name.\\<mh-folder-mode-map>
154 The appearance of the heading can be modified by customizing the
155 face `mh-search-folder'. You can jump back and forth between the
156 headings using the commands \\[mh-index-next-folder] and
157 \\[mh-index-previous-folder].
159 In addition, the command \\[mh-index-visit-folder] can be used to
160 visit the folder of the message at point. Initially, only the
161 messages that matched the search criteria are displayed in the
162 folder. While the temporary buffer has its own set of message
163 numbers, the actual messages numbers are shown in the visited
164 folder. Thus, the command \\[mh-index-visit-folder] is useful to
165 find the actual message number of an interesting message, or to
166 view surrounding messages with the command \\[mh-rescan-folder].
168 Because this folder is temporary, you'll probably get in the
169 habit of killing it when you're done with \\[mh-kill-folder].
171 You can regenerate the results by running this command with a
172 prefix argument REDO-SEARCH-FLAG.
174 Note: This command uses an \"X-MHE-Checksum:\" header field to
175 cache the MD5 checksum of a message. This means that if an
176 incoming message already contains an \"X-MHE-Checksum:\" field,
177 that message might not be found by this command. The following
178 \"procmail\" recipe avoids this problem by renaming the existing
182 | formail -R \"X-MHE-Checksum\" \"X-Old-MHE-Checksum\"
184 Configuring Indexed Searches
186 The command \\[mh-search] runs the command defined by the option
187 `mh-search-program'. The default value is \"Auto-detect\" which
188 means that MH-E will automatically choose one of \"swish++\",
189 \"swish-e\", \"mairix\", \"namazu\", \"pick\" and \"grep\" in
190 that order. If, for example, you have both \"swish++\" and
191 \"mairix\" installed and you want to use \"mairix\", then you can
192 set this option to \"mairix\".
194 The documentation for the following commands describe how to set
195 up the various indexing programs to use with MH-E.
197 - `mh-swish++-execute-search'
198 - `mh-swish-execute-search'
199 - `mh-mairix-execute-search'
200 - `mh-namazu-execute-search'
201 - `mh-pick-execute-search'
202 - `mh-grep-execute-search'
204 In a program, if FOLDER is \"+\" or nil, then mail in all folders
205 are searched. Optional argument WINDOW-CONFIG stores the window
206 configuration that will be restored after the user quits the
207 folder containing the index search results."
211 ;; Yes, we do want to call mh-search-choose every time in case the
212 ;; user has switched the searcher manually.
213 (unless (mh-search-choose (and current-prefix-arg
214 mh-index-previous-search
215 (cadr mh-index-previous-search
)))
216 (error "No search program found"))
217 (or (and current-prefix-arg mh-index-sequence-search-flag
)
218 (and current-prefix-arg
(car mh-index-previous-search
))
219 (mh-prompt-for-folder "Search" "+" nil
"all" t
)))
220 (or (and current-prefix-arg
(caddr mh-index-previous-search
))
221 mh-search-regexp-builder
222 (read-string (format "%s regexp: "
223 (upcase-initials (symbol-name mh-searcher
)))))
225 (if (and (not (and current-prefix-arg
226 (caddr mh-index-previous-search
)))
227 mh-search-regexp-builder
)
228 (current-window-configuration)
231 ;; Redoing a sequence search?
232 (when (and redo-search-flag mh-index-data mh-index-sequence-search-flag
233 (not mh-flists-called-flag
))
234 (let ((mh-flists-called-flag t
))
235 (apply #'mh-index-sequenced-messages mh-index-previous-search
))
236 (return-from mh-search
))
237 ;; We have fancy query parsing.
238 (when (symbolp search-regexp
)
239 (mh-search-folder folder window-config
)
240 (return-from mh-search
))
241 ;; Begin search proper.
243 (let ((result-count 0)
244 (old-window-config (or window-config mh-previous-window-config
))
245 (previous-search mh-index-previous-search
)
246 (index-folder (format "%s/%s" mh-index-folder
247 (mh-index-generate-pretty-name search-regexp
))))
248 ;; Create a new folder for the search results or recreate the old one...
249 (if (and redo-search-flag mh-index-previous-search
)
250 (let ((buffer-name (buffer-name (current-buffer))))
251 (mh-process-or-undo-commands buffer-name
)
252 (save-excursion (mh-exec-cmd-quiet nil
"rmf" buffer-name
))
253 (mh-exec-cmd-quiet nil
"folder" "-create" "-fast" buffer-name
)
254 (setq index-folder buffer-name
))
255 (setq index-folder
(mh-index-new-folder index-folder search-regexp
)))
257 (let ((folder-path (format "%s%s" mh-user-path
(substring folder
1)))
258 (folder-results-map (make-hash-table :test
#'equal
))
259 (origin-map (make-hash-table :test
#'equal
)))
260 ;; Run search program...
261 (message "Executing %s... " mh-searcher
)
262 (funcall mh-search-function folder-path search-regexp
)
264 ;; Parse searcher output.
265 (message "Processing %s output... " mh-searcher
)
266 (goto-char (point-min))
267 (loop for next-result
= (funcall mh-search-next-result-function
)
269 do
(unless (eq next-result
'error
)
270 (unless (gethash (car next-result
) folder-results-map
)
271 (setf (gethash (car next-result
) folder-results-map
)
272 (make-hash-table :test
#'equal
)))
273 (setf (gethash (cadr next-result
)
274 (gethash (car next-result
) folder-results-map
))
277 ;; Copy the search results over.
278 (maphash #'(lambda (folder msgs
)
279 (let ((cur (car (mh-translate-range folder
"cur")))
280 (msgs (sort (loop for msg being the hash-keys of msgs
283 (mh-exec-cmd "refile" msgs
"-src" folder
284 "-link" index-folder
)
285 ;; Restore cur to old value, that refile changed
287 (mh-exec-cmd-quiet nil
"mark" folder
"-add" "-zero"
289 "cur" (format "%s" cur
)))
290 (loop for msg in msgs
291 do
(incf result-count
)
292 (setf (gethash result-count origin-map
)
293 (cons folder msg
)))))
296 ;; Vist the results folder.
297 (mh-visit-folder index-folder
() (list folder-results-map origin-map
))
299 (goto-char (point-min))
301 (mh-update-sequences)
304 ;; Update the speedbar, if needed.
305 (when (mh-speed-flists-active-p)
306 (mh-speed-flists t mh-current-folder
))
309 (when (or (and redo-search-flag previous-search
) window-config
)
310 (setq mh-previous-window-config old-window-config
))
311 (setq mh-index-previous-search
(list folder mh-searcher search-regexp
))
313 ;; Write out data to disk.
314 (unless mh-flists-called-flag
(mh-index-write-data))
316 (message "%s found %s matches in %s folders"
317 (upcase-initials (symbol-name mh-searcher
))
318 (loop for msg-hash being the hash-values of mh-index-data
319 sum
(hash-table-count msg-hash
))
320 (loop for msg-hash being the hash-values of mh-index-data
321 count
(> (hash-table-count msg-hash
) 0)))))))
324 (defvar pick-folder
) ; XEmacs
326 (defun mh-search-folder (folder window-config
)
327 "Search FOLDER for messages matching a pattern.
329 In a program, argument WINDOW-CONFIG is the current window
330 configuration and is used when the search folder is dismissed."
331 (interactive (list (mh-prompt-for-folder "Search" mh-current-folder nil nil t
)
332 (current-window-configuration)))
333 (let ((pick-folder (if (equal folder
"+") mh-current-folder folder
)))
334 (switch-to-buffer-other-window "search-pattern")
335 (if (or (zerop (buffer-size))
336 (not (y-or-n-p "Reuse pattern? ")))
337 (mh-make-pick-template)
339 (mh-make-local-vars 'mh-current-folder folder
340 'mh-previous-window-config window-config
)
341 (message "%s" (substitute-command-keys
342 (concat "Type \\[mh-index-do-search] to search messages, "
343 "\\[mh-pick-do-search] to use pick, "
344 "\\[mh-help] for help")))))
346 (defun mh-make-pick-template ()
347 "Initialize the current buffer with a template for a pick pattern."
348 (let ((inhibit-read-only t
)) (erase-buffer))
356 (goto-char (point-min))
358 (add-text-properties (point) (1+ (point)) '(front-sticky t
))
359 (add-text-properties (- (mh-line-end-position) 2)
360 (1- (mh-line-end-position))
362 (add-text-properties (point) (1- (mh-line-end-position)) '(read-only t
))
364 (add-text-properties (point) (1+ (point)) '(front-sticky t
))
365 (add-text-properties (point) (1- (mh-line-end-position)) '(read-only t
))
366 (goto-char (point-max)))
371 (defun mh-index-new-messages (folders)
372 "Display unseen messages.
374 If you use a program such as \"procmail\" to use \"rcvstore\" to file
375 your incoming mail automatically, you can display new, unseen,
376 messages using this command. All messages in the \"unseen\"
377 sequence from the folders in `mh-new-messages-folders' are
380 With a prefix argument, enter a space-separated list of FOLDERS,
381 or nothing to search all folders."
383 (list (if current-prefix-arg
384 (split-string (read-string "Search folder(s) (default all): "))
385 mh-new-messages-folders
)))
386 (mh-index-sequenced-messages folders mh-unseen-seq
))
389 (defun mh-index-ticked-messages (folders)
390 "Display ticked messages.
392 All messages in `mh-tick-seq' from the folders in
393 `mh-ticked-messages-folders' are listed.
395 With a prefix argument, enter a space-separated list of FOLDERS,
396 or nothing to search all folders."
398 (list (if current-prefix-arg
399 (split-string (read-string "Search folder(s) (default all): "))
400 mh-ticked-messages-folders
)))
401 (mh-index-sequenced-messages folders mh-tick-seq
))
404 (defvar mh-mairix-folder
) ; XEmacs
405 (defvar mh-flists-search-folders
) ; XEmacs
408 (defun mh-index-sequenced-messages (folders sequence
)
409 "Display messages in any sequence.
411 All messages from the FOLDERS in `mh-new-messages-folders' in the
412 SEQUENCE you provide are listed. With a prefix argument, enter a
413 space-separated list of folders at the prompt, or nothing to
416 (list (if current-prefix-arg
417 (split-string (read-string "Search folder(s) (default all): "))
418 mh-new-messages-folders
)
419 (mh-read-seq-default "Search" nil
)))
420 (unless sequence
(setq sequence mh-unseen-seq
))
421 (let* ((mh-flists-search-folders folders
)
422 (mh-flists-sequence sequence
)
423 (mh-flists-called-flag t
)
424 (mh-searcher 'flists
)
425 (mh-search-function 'mh-flists-execute
)
426 (mh-search-next-result-function 'mh-mairix-next-result
)
427 (mh-mairix-folder mh-user-path
)
428 (mh-search-regexp-builder nil
)
429 (new-folder (format "%s/%s/%s" mh-index-folder
430 mh-flists-results-folder sequence
))
431 (window-config (if (equal new-folder mh-current-folder
)
432 mh-previous-window-config
433 (current-window-configuration)))
436 (cond ((buffer-live-p (get-buffer new-folder
))
437 ;; The destination folder is being visited. Trick `mh-search'
438 ;; into thinking that the folder resulted from a previous search.
439 (set-buffer new-folder
)
440 (setq mh-index-previous-search
(list folders mh-searcher sequence
))
442 ((mh-folder-exists-p new-folder
)
443 ;; Folder exists but we don't have it open. That means they are
444 ;; stale results from a old flists search. Clear it out.
445 (mh-exec-cmd-quiet nil
"rmf" new-folder
)))
446 (setq message
(mh-search "+" mh-flists-results-folder
447 redo-flag window-config
)
448 mh-index-sequence-search-flag t
449 mh-index-previous-search
(list folders mh-searcher sequence
))
450 (mh-index-write-data)
451 (when (stringp message
) (message "%s" message
))))
453 (defvar mh-flists-search-folders
)
455 (defun mh-flists-execute (&rest args
)
457 Search for messages belonging to `mh-flists-sequence' in the
458 folders specified by `mh-flists-search-folders'. If
459 `mh-recursive-folders-flag' is t, then the folders are searched
460 recursively. All parameters ARGS are ignored."
461 (set-buffer (get-buffer-create mh-temp-index-buffer
))
463 (unless (executable-find "sh")
464 (error "Didn't find sh"))
466 (let ((seq (symbol-name mh-flists-sequence
)))
467 (insert "for folder in `" (expand-file-name "flists" mh-progs
) " "
468 (cond ((eq mh-flists-search-folders t
)
469 (mh-quote-for-shell mh-inbox
))
470 ((eq mh-flists-search-folders nil
) "")
471 ((listp mh-flists-search-folders
)
472 (loop for folder in mh-flists-search-folders
474 (concat " " (mh-quote-for-shell folder
)))))
475 (if mh-recursive-folders-flag
" -recurse" "")
476 " -sequence " seq
" -noshowzero -fast` ; do\n"
477 (expand-file-name "mhpath" mh-progs
) " \"+$folder\" " seq
"\n"
480 (point-min) (point-max) "sh" nil
(get-buffer mh-temp-index-buffer
))))
485 (defun mh-index-next-folder (&optional backward-flag
)
486 "Jump to the next folder marker.
488 With non-nil optional argument BACKWARD-FLAG, jump to the previous
491 (if (null mh-index-data
)
492 (message "Only applicable in an MH-E index search buffer")
493 (let ((point (point)))
494 (forward-line (if backward-flag
0 1))
495 (cond ((if backward-flag
496 (re-search-backward "^+" (point-min) t
)
497 (re-search-forward "^+" (point-max) t
))
499 ((and (if backward-flag
500 (goto-char (point-max))
501 (goto-char (point-min)))
504 (re-search-backward "^+" (point-min) t
)
505 (re-search-forward "^+" (point-max) t
))
507 (t (goto-char point
))))))
510 (defun mh-index-previous-folder ()
511 "Jump to the previous folder marker."
513 (mh-index-next-folder t
))
516 (defun mh-index-visit-folder ()
517 "Visit original folder from where the message at point was found."
519 (unless mh-index-data
520 (error "Not in an index folder"))
523 (cond ((and (bolp) (eolp))
524 (ignore-errors (forward-line -
1))
525 (setq msg
(mh-get-msg-num t
)))
526 ((equal (char-after (mh-line-beginning-position)) ?
+)
527 (setq folder
(buffer-substring-no-properties
528 (mh-line-beginning-position)
529 (mh-line-end-position))))
530 (t (setq msg
(mh-get-msg-num t
)))))
532 (setq folder
(car (gethash (gethash msg mh-index-msg-checksum-map
)
533 mh-index-checksum-origin-map
))))
534 (when (or (not (get-buffer folder
))
535 (y-or-n-p (format "Reuse buffer displaying %s? " folder
)))
537 folder
(loop for x being the hash-keys of
(gethash folder mh-index-data
)
538 when
(mh-msg-exists-p x folder
) collect x
)))))
545 mh-pick-menu mh-search-mode-map
"Menu for MH-E Search"
547 ["Perform Search" mh-index-do-search t
]
548 ["Search with pick" mh-pick-do-search t
]))
554 ;; If this changes, modify mh-search-mode-help-messages accordingly, below.
555 (gnus-define-keys mh-search-mode-map
557 "\C-c\C-c" mh-index-do-search
558 "\C-c\C-p" mh-pick-do-search
559 "\C-c\C-f\C-b" mh-to-field
560 "\C-c\C-f\C-c" mh-to-field
561 "\C-c\C-f\C-m" mh-to-field
562 "\C-c\C-f\C-s" mh-to-field
563 "\C-c\C-f\C-t" mh-to-field
564 "\C-c\C-fb" mh-to-field
565 "\C-c\C-fc" mh-to-field
566 "\C-c\C-fm" mh-to-field
567 "\C-c\C-fs" mh-to-field
568 "\C-c\C-ft" mh-to-field
)
572 ;;; MH-Search Help Messages
574 ;; Group messages logically, more or less.
575 (defvar mh-search-mode-help-messages
577 "Perform search: \\[mh-index-do-search]\n"
578 "Search with pick: \\[mh-pick-do-search]\n\n"
579 "Move to a field by typing C-c C-f C-<field>\n"
580 "where <field> is the first letter of the desired field\n"
581 "(except for From: which uses \"m\")."))
582 "Key binding cheat sheet.
584 This is an associative array which is used to show the most common
585 commands. The key is a prefix char. The value is one or more strings
586 which are concatenated together and displayed in the minibuffer if ?
587 is pressed after the prefix character. The special key nil is used to
588 display the non-prefixed commands.
590 The substitutions described in `substitute-command-keys' are performed
597 (put 'mh-search-mode
'mode-class
'special
)
599 (define-derived-mode mh-search-mode fundamental-mode
"MH-Search"
600 "Mode for creating search templates in MH-E.\\<mh-search-mode-map>
602 Edit this template by entering your search criteria in an
603 appropriate header field that is already there, or create a new
604 field yourself. If the string you're looking for could be
605 anywhere in a message, then place the string underneath the row
608 To perform the search, type \\[mh-index-do-search].
610 Sometimes you're searching for text that is either not indexed,
611 or hasn't been indexed yet. In this case you can override the
612 default method with the pick method by running the command
613 \\[mh-pick-do-search].
615 The hook `mh-search-mode-hook' is called upon entry to this mode.
617 \\{mh-search-mode-map}"
619 (easy-menu-add mh-pick-menu
)
620 (mh-set-help mh-search-mode-help-messages
))
624 ;;; MH-Search Commands
626 (defun mh-index-do-search (&optional searcher
)
627 "Find messages using `mh-search-program'.
628 If optional argument SEARCHER is present, use it instead of
629 `mh-search-program'."
631 (unless (mh-search-choose searcher
) (error "No search program found"))
632 (let* ((regexp-list (mh-pick-parse-search-buffer))
633 (pattern (funcall mh-search-regexp-builder regexp-list
)))
635 (mh-search mh-current-folder pattern nil mh-previous-window-config
)
636 (error "No search terms"))))
638 (defun mh-pick-do-search ()
639 "Find messages using \"pick\".
641 Uses the pick method described in `mh-pick-execute-search'."
643 (mh-index-do-search 'pick
))
645 (defun mh-pick-parse-search-buffer ()
646 "Parse the search buffer contents.
647 The function returns a alist. The car of each element is either
648 the header name to search in or nil to search the whole message.
649 The cdr of the element is the pattern to search."
651 (let ((pattern-list ())
654 (goto-char (point-min))
656 (if (search-forward "--------" (mh-line-end-position) t
)
657 (setq in-body-flag t
)
660 (setq start
(if in-body-flag
662 (search-forward ":" (mh-line-end-position) t
)
664 (push (cons (and (not in-body-flag
)
666 (buffer-substring-no-properties
668 (mh-index-parse-search-regexp
669 (buffer-substring-no-properties
670 start
(mh-line-end-position))))
675 (defun mh-index-parse-search-regexp (input-string)
676 "Construct parse tree for INPUT-STRING.
677 All occurrences of &, |, ! and ~ in INPUT-STRING are replaced by
678 AND, OR and NOT as appropriate. Then the resulting string is
682 (insert input-string
)
684 (mh-replace-string "\t" " ")
686 (mh-replace-string " AND " " and ")
687 (mh-replace-string "&" " and ")
688 (mh-replace-string " -and " " and ")
690 (mh-replace-string " OR " " or ")
691 (mh-replace-string "|" " or ")
692 (mh-replace-string " -or " " or ")
694 (mh-replace-string " NOT " " not ")
695 (mh-replace-string "!" " not ")
696 (mh-replace-string "~" " not ")
697 (mh-replace-string " -not " " not ")
698 ;; synonyms of left brace
699 (mh-replace-string "(" " ( ")
700 (mh-replace-string " -lbrace " " ( ")
701 ;; synonyms of right brace
702 (mh-replace-string ")" " ) ")
703 (mh-replace-string " -rbrace " " ) ")
704 ;; get the normalized input
705 (setq input
(format "( %s )" (buffer-substring (point-min) (point-max)))))
707 (let ((tokens (mh-index-add-implicit-ops (split-string input
)))
711 (dolist (token tokens
)
712 (cond ((equal token
"(") (push 'paren op-stack
))
713 ((equal token
"not") (push 'not op-stack
))
714 ((equal token
"or") (push 'or op-stack
))
715 ((equal token
"and") (push 'and op-stack
))
717 (multiple-value-setq (op-stack operand-stack
)
718 (values-list (mh-index-evaluate op-stack operand-stack
)))
719 (when (eq (car op-stack
) 'not
)
720 (setq op-stack
(cdr op-stack
))
721 (push `(not ,(pop operand-stack
)) operand-stack
))
722 (when (eq (car op-stack
) 'and
)
723 (setq op-stack
(cdr op-stack
))
724 (setq oper1
(pop operand-stack
))
725 (push `(and ,(pop operand-stack
) ,oper1
) operand-stack
)))
726 ((eq (car op-stack
) 'not
)
727 (setq op-stack
(cdr op-stack
))
728 (push `(not ,token
) operand-stack
)
729 (when (eq (car op-stack
) 'and
)
730 (setq op-stack
(cdr op-stack
))
731 (setq oper1
(pop operand-stack
))
732 (push `(and ,(pop operand-stack
) ,oper1
) operand-stack
)))
733 ((eq (car op-stack
) 'and
)
734 (setq op-stack
(cdr op-stack
))
735 (push `(and ,(pop operand-stack
) ,token
) operand-stack
))
736 (t (push token operand-stack
))))
737 (prog1 (pop operand-stack
)
738 (when (or op-stack operand-stack
)
739 (error "Invalid regexp: %s" input
))))))
741 (defun mh-index-add-implicit-ops (tokens)
742 "Add implicit operators in the list TOKENS."
747 (setq current
(pop tokens
))
748 (cond ((or (equal current
")") (equal current
"and") (equal current
"or"))
749 (setq literal-seen nil
)
750 (push current result
))
753 (setq literal-seen nil
)
756 (push current result
)
757 (unless (or (equal current
"(") (equal current
"not"))
758 (setq literal-seen t
)))))
761 (defun mh-index-evaluate (op-stack operand-stack
)
762 "Read expression till starting paren based on OP-STACK and OPERAND-STACK."
763 (block mh-index-evaluate
766 (setq op
(pop op-stack
))
767 (cond ((eq op
'paren
)
768 (return-from mh-index-evaluate
(list op-stack operand-stack
)))
770 (push `(not ,(pop operand-stack
)) operand-stack
))
771 ((or (eq op
'and
) (eq op
'or
))
772 (setq oper1
(pop operand-stack
))
773 (push `(,op
,(pop operand-stack
) ,oper1
) operand-stack
))))
774 (error "Ran out of tokens"))))
778 ;;; Indexing Functions
780 ;; Support different search programs
781 (defvar mh-search-choices
783 mh-swish
++-binary mh-swish
++-execute-search mh-swish
++-next-result
784 mh-swish
++-regexp-builder
)
786 mh-swish-binary mh-swish-execute-search mh-swish-next-result nil
)
788 mh-mairix-binary mh-mairix-execute-search mh-mairix-next-result
789 mh-mairix-regexp-builder
)
791 mh-namazu-binary mh-namazu-execute-search mh-namazu-next-result nil
)
793 mh-pick-binary mh-pick-execute-search mh-pick-next-result
794 mh-pick-regexp-builder
)
796 mh-grep-binary mh-grep-execute-search mh-grep-next-result nil
))
797 "List of possible searcher choices.")
799 (defun mh-search-choose (&optional searcher
)
800 "Choose a searching function.
801 The side-effects of this function are that the variables
802 `mh-searcher', `mh-search-function', and
803 `mh-search-next-result-function' are set according to the first
804 searcher in `mh-search-choices' present on the system. If
805 optional argument SEARCHER is present, use it instead of
806 `mh-search-program'."
808 (let ((program-alist (cond (searcher
809 (list (assoc searcher mh-search-choices
)))
812 (assoc mh-search-program mh-search-choices
)))
813 (t mh-search-choices
))))
815 (let* ((current (pop program-alist
))
816 (executable (symbol-value (cadr current
))))
818 (setq mh-searcher
(car current
))
819 (setq mh-search-function
(nth 2 current
))
820 (setq mh-search-next-result-function
(nth 3 current
))
821 (setq mh-search-regexp-builder
(nth 4 current
))
822 (return mh-searcher
))))
827 (defvar mh-swish
++-binary
(or (executable-find "search++")
828 (executable-find "search")))
829 (defvar mh-swish
++-directory
".swish++")
830 (defvar mh-swish-folder nil
)
832 (defun mh-swish++-execute-search
(folder-path search-regexp
)
835 In the examples below, replace \"/home/user/Mail\" with the path to
838 First create the directory \"/home/user/Mail/.swish++\". Then create
839 the file \"/home/user/Mail/.swish++/swish++.conf\" with the following
842 IncludeMeta Bcc Cc Comments Content-Description From Keywords
843 IncludeMeta Newsgroups Resent-To Subject To
844 IncludeMeta Message-Id References In-Reply-To
846 IndexFile /home/user/Mail/.swish++/swish++.index
848 Use the following command line to generate the swish index. Run
849 this daily from cron:
851 find /home/user/Mail -path /home/user/Mail/mhe-index -prune \\
852 -o -path /home/user/Mail/.swish++ -prune \\
853 -o -name \"[0-9]*\" -print \\
854 | index -c /home/user/Mail/.swish++/swish++.conf -
856 This command does not index the folders that hold the results of your
857 searches in \"+mhe-index\" since they tend to be ephemeral and the
858 original messages are indexed anyway.
860 On some systems (Debian GNU/Linux, for example), use \"index++\"
861 instead of \"index\".
863 In a program, FOLDER-PATH is the directory in which SEARCH-REGEXP is
865 (set-buffer (get-buffer-create mh-temp-index-buffer
))
867 (unless mh-swish
++-binary
868 (error "Set `mh-swish++-binary' appropriately"))
869 (call-process mh-swish
++-binary nil
'(t nil
) nil
871 (format "-i%s%s/swish++.index"
872 mh-user-path mh-swish
++-directory
)
874 (goto-char (point-min))
875 (setq mh-swish-folder
876 (let ((last-char (substring folder-path
(1- (length folder-path
)))))
877 (if (equal last-char
"/")
879 (format "%s/" folder-path
)))))
881 (defalias 'mh-swish
++-next-result
'mh-swish-next-result
)
883 (defun mh-swish++-regexp-builder
(regexp-list)
884 "Generate query for swish++.
885 REGEXP-LIST is an alist of fields and values."
887 (dolist (elem regexp-list
)
889 (setq regexp
(concat regexp
" and "
890 (if (car elem
) "(" "")
891 (if (car elem
) (symbol-name (car elem
)) "")
892 (if (car elem
) " = " "")
893 (mh-swish++-print-regexp
(cdr elem
))
894 (if (car elem
) ")" "")))))
895 (substring regexp
4)))
897 (defun mh-swish++-print-regexp
(expr)
898 "Return infix expression corresponding to EXPR."
899 (cond ((atom expr
) (format "%s" expr
))
900 ((eq (car expr
) 'not
)
901 (format "(not %s)" (mh-swish++-print-regexp
(cadr expr
))))
902 (t (format "(%s %s %s)" (mh-swish++-print-regexp
(cadr expr
))
903 (symbol-name (car expr
))
904 (mh-swish++-print-regexp
(caddr expr
))))))
908 (defvar mh-swish-binary
(executable-find "swish-e"))
909 (defvar mh-swish-directory
".swish")
911 (defun mh-swish-execute-search (folder-path search-regexp
)
914 In the examples below, replace \"/home/user/Mail\" with the path
915 to your MH directory.
917 First create the directory \"/home/user/Mail/.swish\". Then
918 create the file \"/home/user/Mail/.swish/config\" with the
922 IndexDir /home/user/Mail
923 IndexFile /home/user/Mail/.swish/index
924 IndexName \"Mail Index\"
925 IndexDescription \"Mail Index\"
926 IndexPointer \"http://nowhere\"
927 IndexAdmin \"nobody\"
932 IgnoreTotalWordCountWhenRanking yes
933 WordCharacters abcdefghijklmnopqrstuvwxyz0123456789-
934 BeginCharacters abcdefghijklmnopqrstuvwxyz
935 EndCharacters abcdefghijklmnopqrstuvwxyz0123456789
938 FileRules filename contains \\D
939 FileRules pathname contains /home/user/Mail/.swish
940 FileRules pathname contains /home/user/Mail/mhe-index
942 This configuration does not index the folders that hold the
943 results of your searches in \"+mhe-index\" since they tend to be
944 ephemeral and the original messages are indexed anyway.
946 If there are any directories you would like to ignore, append
947 lines like the following to \"config\":
949 FileRules pathname contains /home/user/Mail/scripts
951 Use the following command line to generate the swish index. Run
952 this daily from cron:
954 swish-e -c /home/user/Mail/.swish/config
956 In a program, FOLDER-PATH is the directory in which SEARCH-REGEXP
958 (set-buffer (get-buffer-create mh-temp-index-buffer
))
960 (unless mh-swish-binary
961 (error "Set `mh-swish-binary' appropriately"))
962 (call-process mh-swish-binary nil
'(t nil
) nil
964 "-f" (format "%s%s/index" mh-user-path mh-swish-directory
))
965 (goto-char (point-min))
966 (setq mh-swish-folder
967 (let ((last-char (substring folder-path
(1- (length folder-path
)))))
968 (if (equal last-char
"/")
970 (format "%s/" folder-path
)))))
972 (defun mh-swish-next-result ()
973 "Get the next result from swish output."
976 (when (or (eobp) (equal (char-after (point)) ?.
))
978 (when (equal (char-after (point)) ?
#)
980 (let* ((start (search-forward " " (mh-line-end-position) t
))
981 (end (search-forward " " (mh-line-end-position) t
)))
982 (unless (and start end
)
985 (unless (file-exists-p (buffer-substring-no-properties start end
))
987 (unless (search-backward "/" start t
)
989 (list (let* ((s (buffer-substring-no-properties start
(1+ (point)))))
990 (unless (string-match mh-swish-folder s
)
992 (if (and (string-match mh-user-path s
)
993 (< (match-end 0) (1- (length s
))))
995 (substring s
(match-end 0) (1- (length s
))))
997 (let* ((s (buffer-substring-no-properties (1+ (point)) end
))
998 (n (ignore-errors (string-to-number s
))))
999 (if n n
(return 'error
)))
1005 (defvar mh-mairix-binary
(executable-find "mairix"))
1006 (defvar mh-mairix-directory
".mairix")
1007 (defvar mh-mairix-folder nil
)
1009 (defun mh-mairix-execute-search (folder-path search-regexp-list
)
1012 In the examples below, replace \"/home/user/Mail\" with the path
1013 to your MH directory.
1015 First create the directory \"/home/user/Mail/.mairix\". Then
1016 create the file \"/home/user/Mail/.mairix/config\" with the
1019 base=/home/user/Mail
1021 # List of folders that should be indexed. 3 dots at the end means there
1022 # are subfolders within the folder
1023 mh=archive...:inbox:drafts:news:sent:trash
1026 database=/home/user/Mail/.mairix/database
1028 Use the following command line to generate the mairix index. Run
1029 this daily from cron:
1031 mairix -f /home/user/Mail/.mairix/config
1033 In a program, FOLDER-PATH is the directory in which
1034 SEARCH-REGEXP-LIST is used to search."
1035 (set-buffer (get-buffer-create mh-temp-index-buffer
))
1037 (unless mh-mairix-binary
1038 (error "Set `mh-mairix-binary' appropriately"))
1039 (apply #'call-process mh-mairix-binary nil
'(t nil
) nil
1040 "-r" "-f" (format "%s%s/config" mh-user-path mh-mairix-directory
)
1042 (goto-char (point-min))
1043 (setq mh-mairix-folder
1044 (let ((last-char (substring folder-path
(1- (length folder-path
)))))
1045 (if (equal last-char
"/")
1047 (format "%s/" folder-path
)))))
1049 (defun mh-mairix-next-result ()
1050 "Return next result from mairix output."
1053 (when (or (eobp) (and (bolp) (eolp)))
1055 (unless (eq (char-after) ?
/)
1057 (let ((start (point))
1059 (setq end
(mh-line-end-position))
1060 (unless (search-forward mh-mairix-folder end t
)
1062 (goto-char (match-beginning 0))
1063 (unless (equal (point) start
)
1066 (unless (search-backward "/" start t
)
1068 (setq msg-start
(1+ (point)))
1070 (unless (search-forward mh-user-path end t
)
1072 (list (format "+%s" (buffer-substring-no-properties
1073 (point) (1- msg-start
)))
1075 (buffer-substring-no-properties msg-start end
))
1079 (defun mh-mairix-regexp-builder (regexp-list)
1080 "Generate query for mairix.
1081 REGEXP-LIST is an alist of fields and values."
1083 (dolist (pair regexp-list
)
1087 (cond ((eq (car pair
) 'to
) "t:")
1088 ((eq (car pair
) 'from
) "f:")
1089 ((eq (car pair
) 'cc
) "c:")
1090 ((eq (car pair
) 'to-or-cc
) "tc:")
1091 ((eq (car pair
) 'address
) "a:")
1092 ((eq (car pair
) 'subject
) "s:")
1093 ((eq (car pair
) 'subject-or-body
) "bs:")
1094 ((eq (car pair
) 'date
) "d:")
1095 ((eq (car pair
) 'message-id
) "m:")
1096 ((eq (car pair
) 'message-body
) "b:")
1097 ((eq (car pair
) 'message-size
) "z:")
1098 ((eq (car pair
) 'message-attachment-name
) "n:")
1099 ((eq (car pair
) 'message-flags
) "F:")
1101 (let ((sop (cdr (mh-mairix-convert-to-sop* (cdr pair
))))
1103 (dolist (conjunct sop
)
1104 (let ((expr-list (cdr conjunct
))
1106 (dolist (e expr-list
)
1107 (setq expr-string
(concat expr-string
","
1108 (if (atom e
) "" "~")
1109 (if (atom e
) e
(cadr e
)))))
1110 (setq final
(concat final
"/" (substring expr-string
1)))))
1111 (substring final
1)))
1115 (defun mh-mairix-convert-to-sop* (expr)
1116 "Convert EXPR to sum of product form."
1117 (cond ((atom expr
) `(or (and ,expr
)))
1118 ((eq (car expr
) 'or
)
1120 (loop for e in
(mapcar #'mh-mairix-convert-to-sop
* (cdr expr
))
1122 ((eq (car expr
) 'and
)
1123 (let ((conjuncts (mapcar #'mh-mairix-convert-to-sop
* (cdr expr
)))
1125 (setq result
(pop conjuncts
))
1127 (setq next-factor
(pop conjuncts
))
1128 (setq result
(let ((res ()))
1129 (dolist (t1 (cdr result
))
1130 (dolist (t2 (cdr next-factor
))
1131 (push `(and ,@(cdr t1
) ,@(cdr t2
)) res
)))
1134 ((atom (cadr expr
)) `(or (and ,expr
)))
1135 ((eq (caadr expr
) 'not
) (mh-mairix-convert-to-sop* (cadadr expr
)))
1136 ((eq (caadr expr
) 'and
) (mh-mairix-convert-to-sop*
1137 `(or ,@(mapcar #'(lambda (x) `(not ,x
))
1139 ((eq (caadr expr
) 'or
) (mh-mairix-convert-to-sop*
1140 `(and ,@(mapcar #'(lambda (x) `(not ,x
))
1142 (t (error "Unreachable: %s" expr
))))
1146 (defvar mh-namazu-binary
(executable-find "namazu"))
1147 (defvar mh-namazu-directory
".namazu")
1148 (defvar mh-namazu-folder nil
)
1150 (defun mh-namazu-execute-search (folder-path search-regexp
)
1153 In the examples below, replace \"/home/user/Mail\" with the path to
1156 First create the directory \"/home/user/Mail/.namazu\". Then create
1157 the file \"/home/user/Mail/.namazu/mknmzrc\" with the following
1160 package conf; # Don't remove this line!
1161 $ADDRESS = 'user@localhost';
1162 $ALLOW_FILE = \"[0-9]*\";
1163 $EXCLUDE_PATH = \"^/home/user/Mail/(mhe-index|spam)\";
1165 This configuration does not index the folders that hold the results of
1166 your searches in \"+mhe-index\" since they tend to be ephemeral and
1167 the original messages are indexed anyway.
1169 Use the following command line to generate the namazu index. Run this
1172 mknmz -f /home/user/Mail/.namazu/mknmzrc -O /home/user/Mail/.namazu \\
1175 In a program, FOLDER-PATH is the directory in which SEARCH-REGEXP
1177 (let ((namazu-index-directory
1178 (format "%s%s" mh-user-path mh-namazu-directory
)))
1179 (unless (file-exists-p namazu-index-directory
)
1180 (error "Namazu directory %s not present" namazu-index-directory
))
1181 (unless (executable-find mh-namazu-binary
)
1182 (error "Set `mh-namazu-binary' appropriately"))
1183 (set-buffer (get-buffer-create mh-temp-index-buffer
))
1185 (call-process mh-namazu-binary nil
'(t nil
) nil
1186 "-alR" search-regexp namazu-index-directory
)
1187 (goto-char (point-min))
1188 (setq mh-namazu-folder
1189 (let ((last (substring folder-path
(1- (length folder-path
)))))
1190 (if (equal last
"/")
1192 (format "%s/" folder-path
))))))
1194 (defun mh-namazu-next-result ()
1195 "Get the next result from namazu output."
1198 (when (eobp) (return nil
))
1199 (let ((file-name (buffer-substring-no-properties
1200 (point) (mh-line-end-position))))
1201 (unless (equal (string-match mh-namazu-folder file-name
) 0)
1203 (unless (file-exists-p file-name
)
1205 (string-match mh-user-path file-name
)
1206 (let* ((folder/msg
(substring file-name
(match-end 0)))
1207 (mark (mh-search-from-end ?
/ folder
/msg
)))
1208 (unless mark
(return 'error
))
1209 (list (format "+%s" (substring folder
/msg
0 mark
))
1210 (let ((n (ignore-errors (string-to-number
1211 (substring folder
/msg
(1+ mark
))))))
1212 (if n n
(return 'error
)))
1218 (defvar mh-index-pick-folder
)
1219 (defvar mh-pick-binary
"pick")
1220 (defconst mh-pick-single-dash
'(cc date from subject to
)
1221 "Search components that are supported by single-dash option in pick.")
1223 (defun mh-pick-execute-search (folder-path search-regexp
)
1226 Read \"pick(1)\" or the section Finding Messages with pick in the
1227 MH book to find out more about how to enter the criteria (see URL
1228 `http://www.ics.uci.edu/~mh/book/mh/finpic.htm').
1230 In a program, FOLDER-PATH is the directory in which SEARCH-REGEXP
1232 (set-buffer (get-buffer-create mh-temp-index-buffer
))
1235 (mh-folder-list (substring folder-path
(length mh-user-path
)))))
1236 (loop for folder in folders do
1237 (setq folder
(concat "+" folder
))
1238 (insert folder
"\n")
1239 (apply #'call-process
(expand-file-name "pick" mh-progs
)
1240 nil
'(t nil
) nil folder
"-list" search-regexp
)))
1241 (goto-char (point-min)))
1243 (defun mh-pick-next-result ()
1244 "Return the next pick search result."
1247 (when (eobp) (return nil
))
1248 (when (search-forward-regexp "^\+" (mh-line-end-position) t
)
1249 (setq mh-index-pick-folder
1250 (buffer-substring-no-properties (mh-line-beginning-position)
1251 (mh-line-end-position)))
1253 (unless (search-forward-regexp "^[1-9][0-9]*$" (mh-line-end-position) t
)
1255 (list mh-index-pick-folder
1257 (buffer-substring-no-properties (mh-line-beginning-position)
1258 (mh-line-end-position)))
1262 ;; All implementations of pick have special options -cc, -date, -from and
1263 ;; -subject that allow to search for corresponding components. Any other
1264 ;; component is searched using option --COMPNAME, for example: `pick
1265 ;; --x-mailer mh-e'. Mailutils "pick" supports this option using a certain
1266 ;; kludge, but it prefers the following syntax for this purpose:
1267 ;; "--component=COMPNAME --pattern=PATTERN".
1268 ;; -- Sergey Poznyakoff, Aug 2003
1269 (defun mh-pick-regexp-builder (pattern-list)
1270 "Generate pick search expression from PATTERN-LIST."
1272 (dolist (pattern pattern-list
)
1274 (setq result
`(,@result
"-and" "-lbrace"
1275 ,@(mh-pick-construct-regexp
1276 (if (and (mh-variant-p 'gnu-mh
) (car pattern
))
1277 (format "--pattern=%s" (cdr pattern
))
1281 ((mh-variant-p 'gnu-mh
)
1282 (format "--component=%s" (car pattern
)))
1283 ((member (car pattern
) mh-pick-single-dash
)
1284 (format "-%s" (car pattern
)))
1286 (format "--%s" (car pattern
))))
1291 (defun mh-pick-construct-regexp (expr component
)
1292 "Construct pick compatible expression corresponding to EXPR.
1293 COMPONENT is the component to search."
1294 (cond ((atom expr
) (list component expr
))
1295 ((eq (car expr
) 'and
)
1296 `("-lbrace" ,@(mh-pick-construct-regexp (cadr expr
) component
) "-and"
1297 ,@(mh-pick-construct-regexp (caddr expr
) component
) "-rbrace"))
1298 ((eq (car expr
) 'or
)
1299 `("-lbrace" ,@(mh-pick-construct-regexp (cadr expr
) component
) "-or"
1300 ,@(mh-pick-construct-regexp (caddr expr
) component
) "-rbrace"))
1301 ((eq (car expr
) 'not
)
1302 `("-lbrace" "-not" ,@(mh-pick-construct-regexp (cadr expr
) component
)
1304 (t (error "Unknown operator %s seen" (car expr
)))))
1308 (defvar mh-grep-binary
(executable-find "grep"))
1310 (defun mh-grep-execute-search (folder-path search-regexp
)
1313 Unlike the other search methods, this method does not use the
1314 MH-Search buffer. Instead, you simply enter a regular expression
1315 in the minibuffer. For help in constructing regular expressions,
1316 see your man page for \"grep\".
1318 In a program, FOLDER-PATH is the directory in which SEARCH-REGEXP
1320 (set-buffer (get-buffer-create mh-temp-index-buffer
))
1322 (call-process mh-grep-binary nil
'(t nil
) nil
1323 "-i" "-r" search-regexp folder-path
)
1324 (goto-char (point-min)))
1326 (defun mh-grep-next-result ()
1327 "Read the next result.
1328 Parse it and return the message folder, message index and the
1329 match. If no other matches left then return nil. If the current
1330 record is invalid return 'error."
1335 (let ((eol-pos (mh-line-end-position))
1336 (bol-pos (mh-line-beginning-position))
1337 folder-start msg-end
)
1339 (unless (search-forward mh-user-path eol-pos t
)
1341 (setq folder-start
(point))
1342 (unless (search-forward ":" eol-pos t
)
1344 (let ((match (buffer-substring-no-properties (point) eol-pos
)))
1346 (setq msg-end
(point))
1347 (unless (search-backward "/" folder-start t
)
1349 (list (format "+%s" (buffer-substring-no-properties
1350 folder-start
(point)))
1351 (let ((n (ignore-errors (string-to-number
1352 (buffer-substring-no-properties
1353 (1+ (point)) msg-end
)))))
1354 (if n n
(return 'error
)))
1360 ;;; Folder Utilities
1363 (defun mh-index-group-by-folder ()
1364 "Partition the messages based on source folder.
1365 Returns an alist with the folder names in the car and the cdr
1366 being the list of messages originally from that folder."
1368 (goto-char (point-min))
1369 (let ((result-table (make-hash-table :test
#'equal
)))
1370 (loop for msg being the hash-keys of mh-index-msg-checksum-map
1371 do
(push msg
(gethash (car (gethash
1372 (gethash msg mh-index-msg-checksum-map
)
1373 mh-index-checksum-origin-map
))
1375 (loop for x being the hash-keys of result-table
1376 collect
(cons x
(nreverse (gethash x result-table
)))))))
1379 (defun mh-index-insert-folder-headers ()
1380 "Annotate the search results with original folder names."
1381 (let ((cur-msg (mh-get-msg-num nil
))
1382 (old-buffer-modified-flag (buffer-modified-p))
1383 (buffer-read-only nil
)
1384 current-folder last-folder
)
1385 (goto-char (point-min))
1387 (setq current-folder
(car (gethash (gethash (mh-get-msg-num nil
)
1388 mh-index-msg-checksum-map
)
1389 mh-index-checksum-origin-map
)))
1390 (when (and current-folder
(not (equal current-folder last-folder
)))
1391 (insert (if last-folder
"\n" "") current-folder
"\n")
1392 (setq last-folder current-folder
))
1396 (mh-goto-msg cur-msg t
))
1397 (set-buffer-modified-p old-buffer-modified-flag
))
1398 (mh-index-create-imenu-index))
1401 (defun mh-index-delete-folder-headers ()
1402 "Delete the folder headers."
1403 (let ((cur-msg (mh-get-msg-num nil
))
1404 (old-buffer-modified-flag (buffer-modified-p))
1405 (buffer-read-only nil
))
1406 (while (and (not cur-msg
) (not (eobp)))
1408 (setq cur-msg
(mh-get-msg-num nil
)))
1409 (goto-char (point-min))
1411 (if (or (char-equal (char-after) ?
+) (char-equal (char-after) 10))
1412 (delete-region (point) (progn (forward-line) (point)))
1414 (when cur-msg
(mh-goto-msg cur-msg t t
))
1415 (set-buffer-modified-p old-buffer-modified-flag
)))
1417 (mh-require 'which-func nil t
)
1420 (defvar which-func-mode
) ; < Emacs 22, XEmacs
1423 (defun mh-index-create-imenu-index ()
1424 "Create alist of folder names and positions in index folder buffers."
1426 (if (boundp 'which-func-mode
)
1427 (setq which-func-mode t
))
1429 (goto-char (point-min))
1430 (while (re-search-forward "^+" nil t
)
1433 (push (cons (buffer-substring-no-properties
1434 (point) (mh-line-end-position))
1435 (set-marker (make-marker) (point)))
1437 (setq imenu--index-alist
(nreverse alist
)))))
1440 (defun mh-search-p ()
1441 "Non-nil means that this folder was generated by searching."
1445 (defvar mh-speed-flists-inhibit-flag
) ; XEmacs
1448 (defun mh-index-execute-commands ()
1449 "Delete/refile the actual messages.
1450 The copies in the searched folder are then deleted/refiled to get
1451 the desired result. Before deleting the messages we make sure
1452 that the message being deleted is identical to the one that the
1453 user has marked in the index buffer."
1456 (mh-speed-flists-inhibit-flag t
))
1458 (lambda (folder msgs
)
1459 (push folder folders
)
1460 (if (not (get-buffer folder
))
1461 ;; If source folder not open, just delete the messages...
1462 (apply #'mh-exec-cmd
"rmm" folder
(mh-coalesce-msg-list msgs
))
1463 ;; Otherwise delete the messages in the source buffer...
1464 (with-current-buffer folder
1465 (let ((old-refile-list mh-refile-list
)
1466 (old-delete-list mh-delete-list
))
1467 (setq mh-refile-list nil
1468 mh-delete-list msgs
)
1469 (unwind-protect (mh-execute-commands)
1470 (setq mh-refile-list
1473 (loop for y in
(cdr x
)
1474 unless
(memq y msgs
) collect y
)))
1477 (loop for x in old-delete-list
1478 unless
(memq x msgs
) collect x
))
1479 (mh-set-folder-modified-p (mh-outstanding-commands-p))
1480 (when (mh-outstanding-commands-p)
1481 (mh-notate-deleted-and-refiled)))))))
1482 (mh-index-matching-source-msgs (append (loop for x in mh-refile-list
1488 (defun mh-index-generate-pretty-name (string)
1489 "Given STRING generate a name which is suitable for use as a folder name.
1490 White space from the beginning and end are removed. All spaces in
1491 the name are replaced with underscores and all / are replaced
1492 with $. If STRING is longer than 20 it is truncated too. STRING
1493 could be a list of strings in which case they are concatenated to
1494 construct the base name."
1496 (if (stringp string
)
1498 (when (car string
) (insert (car string
)))
1499 (dolist (s (cdr string
))
1501 (setq string
(mh-replace-string "-lbrace" " "))
1502 (setq string
(mh-replace-string "-rbrace" " "))
1503 (setq string
(mh-replace-string "-search" " "))
1504 (subst-char-in-region (point-min) (point-max) ?
( ? t
)
1505 (subst-char-in-region (point-min) (point-max) ?
) ? t
)
1506 (subst-char-in-region (point-min) (point-max) ?- ? t
)
1507 (goto-char (point-min))
1508 (while (and (not (eobp)) (memq (char-after) '(? ?
\t ?
\n ?
\r ?_
)))
1510 (goto-char (point-max))
1511 (while (and (not (bobp)) (memq (char-before) '(? ?
\t ?
\n ?
\r ?_
)))
1513 (subst-char-in-region (point-min) (point-max) ? ?_ t
)
1514 (subst-char-in-region (point-min) (point-max) ?
\t ?_ t
)
1515 (subst-char-in-region (point-min) (point-max) ?
\n ?_ t
)
1516 (subst-char-in-region (point-min) (point-max) ?
\r ?_ t
)
1517 (subst-char-in-region (point-min) (point-max) ?
/ ?$ t
)
1518 (let ((out (truncate-string-to-width (buffer-string) 20)))
1519 (cond ((eq mh-searcher
'flists
)
1520 (format "%s/%s" mh-flists-results-folder mh-flists-sequence
))
1521 ((equal out mh-flists-results-folder
) (concat out
"1"))
1524 (defun mh-folder-exists-p (folder)
1525 "Check if FOLDER exists."
1526 (and (mh-folder-name-p folder
)
1529 (mh-exec-cmd-output "folder" nil
"-fast" "-nocreate" folder
)
1530 (goto-char (point-min))
1531 ;; Strip + from folder; use optional + in regexp.
1532 (looking-at (format "+?%s" (substring folder
1)))))))
1534 (defun mh-msg-exists-p (msg folder
)
1535 "Check if MSG exists in FOLDER."
1536 (file-exists-p (format "%s%s/%s" mh-user-path
(substring folder
1) msg
)))
1538 (defun mh-index-new-folder (name search-regexp
)
1539 "Return a folder name based on NAME for search results of SEARCH-REGEXP.
1541 If folder NAME already exists and was generated for the same
1542 SEARCH-REGEXP then it is reused.
1544 Otherwise if the folder NAME was generated from a different
1545 search then check if NAME-2 can be used. Otherwise try NAME-3.
1546 This is repeated till we find a new folder name.
1548 If the folder returned doesn't exist then it is created."
1549 (unless (mh-folder-name-p name
)
1550 (error "The argument should be a valid MH folder name"))
1553 for candidate
= (if (equal i
1) name
(format "%s-%s" name i
))
1554 when
(or (not (mh-folder-exists-p candidate
))
1555 (equal (mh-index-folder-search-regexp candidate
)
1558 ;; Do pending refiles/deletes...
1559 (when (get-buffer chosen-name
)
1560 (mh-process-or-undo-commands chosen-name
))
1561 ;; Recreate folder...
1562 (save-excursion (mh-exec-cmd-quiet nil
"rmf" chosen-name
))
1563 (mh-exec-cmd-quiet nil
"folder" "-create" "-fast" chosen-name
)
1564 (mh-remove-from-sub-folders-cache chosen-name
)
1565 (when (boundp 'mh-speed-folder-map
)
1566 (mh-speed-add-folder chosen-name
))
1569 (defun mh-index-folder-search-regexp (folder)
1570 "If FOLDER was created by a index search, return the search regexp.
1571 Return nil if FOLDER doesn't exist or the .mhe_index file is
1575 (insert-file-contents
1576 (format "%s%s/%s" mh-user-path
(substring folder
1) mh-index-data-file
))
1577 (goto-char (point-min))
1579 (cadr (read (current-buffer))))))
1583 ;;; Sequence Support
1586 (defun mh-index-create-sequences ()
1587 "Mirror sequences present in source folders in index folder."
1588 (let ((seq-hash (make-hash-table :test
#'equal
))
1590 (loop for folder being the hash-keys of mh-index-data
1591 do
(setf (gethash folder seq-hash
)
1592 (mh-create-sequence-map
1593 (mh-read-folder-sequences folder nil
))))
1594 (dolist (msg (mh-translate-range mh-current-folder
"all"))
1595 (let* ((checksum (gethash msg mh-index-msg-checksum-map
))
1596 (pair (gethash checksum mh-index-checksum-origin-map
))
1597 (ofolder (car pair
))
1599 (loop for seq in
(ignore-errors
1600 (gethash omsg
(gethash ofolder seq-hash
)))
1601 do
(if (assoc seq seq-list
)
1602 (push msg
(cdr (assoc seq seq-list
)))
1603 (push (list seq msg
) seq-list
)))))
1604 (loop for seq in seq-list
1605 do
(apply #'mh-exec-cmd
"mark" mh-current-folder
1606 "-sequence" (symbol-name (car seq
)) "-add"
1607 (mapcar #'(lambda (x) (format "%s" x
)) (cdr seq
))))))
1610 (defun mh-create-sequence-map (seq-list)
1611 "Return a map from msg number to list of sequences in which it is present.
1612 SEQ-LIST is an assoc list whose keys are sequence names and whose
1613 cdr is the list of messages in that sequence."
1614 (loop with map
= (make-hash-table)
1616 when
(and (not (memq (car seq
) (mh-unpropagated-sequences)))
1617 (mh-valid-seq-p (car seq
)))
1618 do
(loop for msg in
(cdr seq
)
1619 do
(push (car seq
) (gethash msg map
)))
1620 finally return map
))
1623 (defun mh-index-add-to-sequence (seq msgs
)
1624 "Add to SEQ the messages in the list MSGS.
1625 This function updates the source folder sequences. Also makes an
1626 attempt to update the source folder buffer if we have it open."
1627 ;; Don't need to do anything for cur
1629 (when (and (not (memq seq
(mh-unpropagated-sequences)))
1630 (mh-valid-seq-p seq
))
1632 (mh-speed-flists-inhibit-flag t
))
1633 (maphash (lambda (folder msgs
)
1634 (push folder folders
)
1635 ;; Add messages to sequence in source folder...
1636 (apply #'mh-exec-cmd-quiet nil
"mark" folder
1637 "-add" "-nozero" "-sequence" (symbol-name seq
)
1638 (mapcar (lambda (x) (format "%s" x
))
1639 (mh-coalesce-msg-list msgs
)))
1640 ;; Update source folder buffer if we have it open...
1641 (when (get-buffer folder
)
1642 (with-current-buffer folder
1643 (mh-put-msg-in-seq msgs seq
))))
1644 (mh-index-matching-source-msgs msgs
))
1648 (defun mh-index-delete-from-sequence (seq msgs
)
1649 "Delete from SEQ the messages in MSGS.
1650 This function updates the source folder sequences. Also makes an
1651 attempt to update the source folder buffer if present."
1653 (when (and (not (memq seq
(mh-unpropagated-sequences)))
1654 (mh-valid-seq-p seq
))
1656 (mh-speed-flists-inhibit-flag t
))
1657 (maphash (lambda (folder msgs
)
1658 (push folder folders
)
1659 ;; Remove messages from sequence in source folder...
1660 (apply #'mh-exec-cmd-quiet nil
"mark" folder
1661 "-del" "-nozero" "-sequence" (symbol-name seq
)
1662 (mapcar (lambda (x) (format "%s" x
))
1663 (mh-coalesce-msg-list msgs
)))
1664 ;; Update source folder buffer if we have it open...
1665 (when (get-buffer folder
)
1666 (with-current-buffer folder
1667 (mh-delete-msg-from-seq msgs seq t
))))
1668 (mh-index-matching-source-msgs msgs
))
1671 (defvar mh-unpropagated-sequences
'(cur range subject search
)
1672 "List of sequences that aren't preserved.")
1674 (defun mh-unpropagated-sequences ()
1675 "Return a list of sequences that aren't propagated to the source folders.
1676 It is just the sequences in the variable
1677 `mh-unpropagated-sequences' in addition to the
1678 Previous-Sequence (see mh-profile 5)."
1680 (cons mh-previous-seq mh-unpropagated-sequences
)
1681 mh-unpropagated-sequences
))
1683 (defun mh-index-matching-source-msgs (msgs &optional delete-from-index-data
)
1684 "Return a table of original messages and folders for messages in MSGS.
1685 If optional argument DELETE-FROM-INDEX-DATA is non-nil, then each
1686 of the messages, whose counter-part is found in some source
1687 folder, is removed from `mh-index-data'."
1688 (let ((table (make-hash-table :test
#'equal
)))
1690 (let* ((checksum (gethash msg mh-index-msg-checksum-map
))
1691 (pair (gethash checksum mh-index-checksum-origin-map
)))
1692 (when (and checksum
(car pair
) (cdr pair
)
1693 (mh-index-match-checksum (cdr pair
) (car pair
) checksum
))
1694 (push (cdr pair
) (gethash (car pair
) table
))
1695 (when delete-from-index-data
1696 (remhash (cdr pair
) (gethash (car pair
) mh-index-data
))))))
1699 (defun mh-index-match-checksum (msg folder checksum
)
1700 "Check if MSG in FOLDER has X-MHE-Checksum header value of CHECKSUM."
1702 (mh-exec-cmd-output mh-scan-prog nil
"-width" "80"
1703 "-format" "%{x-mhe-checksum}\n" folder msg
)
1704 (goto-char (point-min))
1705 (string-equal (buffer-substring-no-properties
1706 (point) (mh-line-end-position))
1711 ;;; Serialization of Index Data
1713 (defun mh-index-write-data ()
1714 "Write index data to file."
1716 (unless (eq major-mode
'mh-folder-mode
)
1717 (error "Can't be called from folder in \"%s\"" major-mode
))
1718 (let ((data mh-index-data
)
1719 (msg-checksum-map mh-index-msg-checksum-map
)
1720 (checksum-origin-map mh-index-checksum-origin-map
)
1721 (previous-search mh-index-previous-search
)
1722 (sequence-search-flag mh-index-sequence-search-flag
)
1723 (outfile (concat buffer-file-name mh-index-data-file
))
1726 (with-temp-file outfile
1727 (mh-index-write-hashtable
1728 data
(lambda (x) (loop for y being the hash-keys of x collect y
)))
1729 (mh-index-write-hashtable msg-checksum-map
#'identity
)
1730 (mh-index-write-hashtable checksum-origin-map
#'identity
)
1731 (pp previous-search
(current-buffer)) (insert "\n")
1732 (pp sequence-search-flag
(current-buffer)) (insert "\n")))))
1734 (defun mh-index-write-hashtable (table proc
)
1735 "Write TABLE to `current-buffer'.
1736 PROC is used to serialize the values corresponding to the hash
1738 (pp (loop for x being the hash-keys of table
1739 collect
(cons x
(funcall proc
(gethash x table
))))
1744 (defun mh-index-read-data ()
1745 "Read index data from file."
1747 (unless (eq major-mode
'mh-folder-mode
)
1748 (error "Can't be called from folder in \"%s\"" major-mode
))
1749 (let ((infile (concat buffer-file-name mh-index-data-file
))
1752 (insert-file-contents-literally infile
)
1753 (goto-char (point-min))
1754 (setq t1
(mh-index-read-hashtable
1756 (loop with table
= (make-hash-table :test
#'equal
)
1757 for x in data do
(setf (gethash x table
) t
)
1758 finally return table
)))
1759 t2
(mh-index-read-hashtable #'identity
)
1760 t3
(mh-index-read-hashtable #'identity
)
1761 t4
(read (current-buffer))
1762 t5
(read (current-buffer))))
1763 (setq mh-index-data t1
1764 mh-index-msg-checksum-map t2
1765 mh-index-checksum-origin-map t3
1766 mh-index-previous-search t4
1767 mh-index-sequence-search-flag t5
))))
1769 (defun mh-index-read-hashtable (proc)
1770 "From BUFFER read a hash table serialized as a list.
1771 PROC is used to convert the value to actual data."
1772 (loop with table
= (make-hash-table :test
#'equal
)
1773 for pair in
(read (current-buffer))
1774 do
(setf (gethash (car pair
) table
) (funcall proc
(cdr pair
)))
1775 finally return table
))
1779 ;;; Checksum Routines
1781 ;; A few different checksum programs are supported. The supported
1788 ;; To add support for your favorite checksum program add a clause to
1789 ;; the cond statement in mh-checksum-choose. This should set the
1790 ;; variable mh-checksum-cmd to the command line needed to run the
1791 ;; checsum program and should set mh-checksum-parser to a function
1792 ;; which returns a cons cell containing the message number and
1795 (defvar mh-checksum-cmd
)
1796 (defvar mh-checksum-parser
)
1798 (defun mh-checksum-choose ()
1799 "Check if a program to create a checksum is present."
1800 (unless (boundp 'mh-checksum-cmd
)
1801 (let ((exec-path (append '("/sbin" "/usr/sbin") exec-path
)))
1802 (cond ((executable-find "md5sum")
1803 (setq mh-checksum-cmd
(list (executable-find "md5sum")))
1804 (setq mh-checksum-parser
#'mh-md5sum-parser
))
1805 ((executable-find "openssl")
1806 (setq mh-checksum-cmd
(list (executable-find "openssl") "md5"))
1807 (setq mh-checksum-parser
#'mh-openssl-parser
))
1808 ((executable-find "md5")
1809 (setq mh-checksum-cmd
(list (executable-find "md5")))
1810 (setq mh-checksum-parser
#'mh-md5-parser
))
1811 (t (error "No suitable checksum program"))))))
1813 (defun mh-md5sum-parser ()
1814 "Parse md5sum output."
1815 (let ((begin (mh-line-beginning-position))
1816 (end (mh-line-end-position))
1817 first-space last-slash
)
1818 (setq first-space
(search-forward " " end t
))
1820 (setq last-slash
(search-backward "/" begin t
))
1821 (cond ((and first-space last-slash
)
1822 (cons (string-to-number (buffer-substring-no-properties
1823 (1+ last-slash
) end
))
1824 (buffer-substring-no-properties begin
(1- first-space
))))
1825 (t (cons nil nil
)))))
1827 (defun mh-openssl-parser ()
1828 "Parse openssl output."
1829 (let ((begin (mh-line-beginning-position))
1830 (end (mh-line-end-position))
1831 last-space last-slash
)
1833 (setq last-space
(search-backward " " begin t
))
1834 (setq last-slash
(search-backward "/" begin t
))
1835 (cond ((and last-slash last-space
)
1836 (cons (string-to-number (buffer-substring-no-properties
1837 (1+ last-slash
) (1- last-space
)))
1838 (buffer-substring-no-properties (1+ last-space
) end
))))))
1840 (defalias 'mh-md5-parser
'mh-openssl-parser
)
1843 (defun mh-index-update-maps (folder &optional origin-map
)
1844 "Annotate all as yet unannotated messages in FOLDER with their MD5 hash.
1845 As a side effect msg -> checksum map is updated. Optional
1846 argument ORIGIN-MAP is a hashtable which maps each message in the
1847 index folder to the original folder and message from whence it
1848 was copied. If present the checksum -> (origin-folder,
1849 origin-index) map is updated too."
1850 (clrhash mh-index-msg-checksum-map
)
1851 ;; Clear temp buffer
1852 (with-current-buffer (get-buffer-create mh-temp-checksum-buffer
)
1854 ;; Run scan to check if any messages needs MD5 annotations at all
1856 (mh-exec-cmd-output mh-scan-prog nil
"-width" "80"
1857 "-format" "%(msg)\n%{x-mhe-checksum}\n"
1859 (goto-char (point-min))
1862 (setq msg
(buffer-substring-no-properties
1863 (point) (mh-line-end-position)))
1866 (cond ((not (string-match "^[0-9]*$" msg
)))
1868 ;; need to compute checksum
1869 (set-buffer mh-temp-checksum-buffer
)
1870 (insert mh-user-path
(substring folder
1) "/" msg
"\n"))
1873 (setq checksum
(buffer-substring-no-properties
1874 (point) (mh-line-end-position)))
1875 (let ((msg (string-to-number msg
)))
1877 (mh-index-update-single-msg msg checksum origin-map
)))))
1879 ;; Run checksum program if needed
1880 (unless (and (eobp) (bobp))
1881 (apply #'mh-xargs mh-checksum-cmd
)
1882 (goto-char (point-min))
1884 (let* ((intermediate (funcall mh-checksum-parser
))
1885 (msg (car intermediate
))
1886 (checksum (cdr intermediate
)))
1889 (mh-exec-cmd "anno" folder msg
"-component" "X-MHE-Checksum"
1890 "-nodate" "-text" checksum
"-inplace")
1892 (with-current-buffer folder
1893 (mh-index-update-single-msg msg checksum origin-map
)))
1895 (mh-index-write-data))
1897 (defun mh-index-update-single-msg (msg checksum origin-map
)
1898 "Update various maps for one message.
1899 MSG is a index folder message, CHECKSUM its MD5 hash and
1900 ORIGIN-MAP, if non-nil, a hashtable containing which maps each
1901 message in the index folder to the folder and message that it was
1902 copied from. The function updates the hash tables
1903 `mh-index-msg-checksum-map' and `mh-index-checksum-origin-map'.
1905 This function should only be called in the appropriate index
1907 (cond ((gethash checksum mh-index-checksum-origin-map
)
1909 (let* ((intermediate (gethash msg origin-map
))
1910 (ofolder (car intermediate
))
1911 (omsg (cdr intermediate
)))
1912 ;; This is most probably a duplicate. So eliminate it.
1913 (call-process "rm" nil nil nil
1914 (format "%s%s/%s" mh-user-path
1915 (substring mh-current-folder
1) msg
))
1916 (when (gethash ofolder mh-index-data
)
1917 (remhash omsg
(gethash ofolder mh-index-data
))))))
1919 (setf (gethash msg mh-index-msg-checksum-map
) checksum
)
1920 (when (and origin-map
(gethash msg origin-map
))
1921 (setf (gethash checksum mh-index-checksum-origin-map
)
1922 (gethash msg origin-map
))))))
1925 (provide 'mh-search
)
1928 ;; indent-tabs-mode: nil
1929 ;; sentence-end-double-space: nil
1932 ;;; mh-search ends here