(c-offsets-alist): Fix typo.
[bpt/emacs.git] / lisp / mh-e / mh-pick.el
CommitLineData
bdcfe844 1;;; mh-pick.el --- make a search pattern and search for a message in MH-E
c26cf6c8 2
a1b4049d
BW
3;; Copyright (C) 1993, 1995, 2001 Free Software Foundation, Inc.
4
5;; Author: Bill Wohler <wohler@newt.com>
6;; Maintainer: Bill Wohler <wohler@newt.com>
7;; Keywords: mail
8;; See: mh-e.el
c26cf6c8 9
60370d40 10;; This file is part of GNU Emacs.
c26cf6c8 11
9b7bc076 12;; GNU Emacs is free software; you can redistribute it and/or modify
c26cf6c8
RS
13;; it under the terms of the GNU General Public License as published by
14;; the Free Software Foundation; either version 2, or (at your option)
15;; any later version.
16
9b7bc076 17;; GNU Emacs is distributed in the hope that it will be useful,
c26cf6c8
RS
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.
21
22;; You should have received a copy of the GNU General Public License
b578f267
EN
23;; along with GNU Emacs; see the file COPYING. If not, write to the
24;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25;; Boston, MA 02111-1307, USA.
c26cf6c8
RS
26
27;;; Commentary:
28
bdcfe844 29;; Internal support for MH-E package.
c26cf6c8 30
847b8219
KH
31;;; Change Log:
32
942da20c 33;; $Id: mh-pick.el,v 1.10 2003/01/08 23:21:16 wohler Exp $
847b8219 34
c26cf6c8
RS
35;;; Code:
36
c26cf6c8 37(require 'mh-e)
a1b4049d
BW
38(require 'easymenu)
39(require 'gnus-util)
c26cf6c8 40
847b8219
KH
41;;; Internal variables:
42
43(defvar mh-pick-mode-map (make-sparse-keymap)
44 "Keymap for searching folder.")
45
c3d9274a 46(defvar mh-searching-folder nil) ;Folder this pick is searching.
c26cf6c8 47
c3d9274a 48;;;###mh-autoload
c26cf6c8 49(defun mh-search-folder (folder)
847b8219 50 "Search FOLDER for messages matching a pattern.
bdcfe844 51This function uses the MH command `pick' to do the work.
847b8219 52Add the messages found to the sequence named `search'."
c26cf6c8 53 (interactive (list (mh-prompt-for-folder "Search"
c3d9274a
BW
54 mh-current-folder
55 t)))
c26cf6c8
RS
56 (switch-to-buffer-other-window "pick-pattern")
57 (if (or (zerop (buffer-size))
c3d9274a 58 (not (y-or-n-p "Reuse pattern? ")))
c26cf6c8
RS
59 (mh-make-pick-template)
60 (message ""))
bdcfe844
BW
61 (setq mh-searching-folder folder)
62 (message "%s" (substitute-command-keys
c3d9274a
BW
63 (concat "Type \\[mh-do-pick-search] to search messages, "
64 "\\[mh-help] for help."))))
c26cf6c8
RS
65
66(defun mh-make-pick-template ()
bdcfe844 67 "Initialize the current buffer with a template for a pick pattern."
c26cf6c8
RS
68 (erase-buffer)
69 (insert "From: \n"
c3d9274a
BW
70 "To: \n"
71 "Cc: \n"
72 "Date: \n"
73 "Subject: \n"
74 "---------\n")
c26cf6c8
RS
75 (mh-pick-mode)
76 (goto-char (point-min))
77 (end-of-line))
78
bdcfe844
BW
79;;; Menu extracted from mh-menubar.el V1.1 (31 July 2001)
80(easy-menu-define
81 mh-pick-menu mh-pick-mode-map "Menu for MH-E pick-mode"
82 '("Pick"
83 ["Execute the Search" mh-do-pick-search t]))
84
85
86;;; Help Messages
87;;; Group messages logically, more or less.
88(defvar mh-pick-mode-help-messages
89 '((nil
90 "Search messages: \\[mh-do-pick-search]\n"
91 "Move to a field by typing C-c C-f C-<field>\n"
92 "where <field> is the first letter of the desired field."))
93 "Key binding cheat sheet.
94
95This is an associative array which is used to show the most common commands.
96The key is a prefix char. The value is one or more strings which are
97concatenated together and displayed in the minibuffer if ? is pressed after
98the prefix character. The special key nil is used to display the
99non-prefixed commands.
100
101The substitutions described in `substitute-command-keys' are performed as
102well.")
103
c26cf6c8
RS
104(put 'mh-pick-mode 'mode-class 'special)
105
a1b4049d 106(define-derived-mode mh-pick-mode fundamental-mode "MH-Pick"
bdcfe844 107 "Mode for creating search templates in MH-E.\\<mh-pick-mode-map>
a1b4049d 108
847b8219
KH
109After each field name, enter the pattern to search for. If a field's
110value does not matter for the search, leave it empty. To search the
111entire message, supply the pattern in the \"body\" of the template.
112Each non-empty field must be matched for a message to be selected.
113To effect a logical \"or\", use \\[mh-search-folder] multiple times.
c26cf6c8 114When you have finished, type \\[mh-do-pick-search] to do the search.
a1b4049d 115
bdcfe844
BW
116The value of `mh-pick-mode-hook' is a list of functions to be called,
117with no arguments, upon entry to this mode.
a1b4049d
BW
118
119\\{mh-pick-mode-map}"
120
c26cf6c8 121 (make-local-variable 'mh-searching-folder)
bdcfe844
BW
122 (easy-menu-add mh-pick-menu)
123 (make-local-variable 'mh-help-messages)
124 (setq mh-help-messages mh-pick-mode-help-messages)
125 (run-hooks 'mh-pick-mode-hook))
c26cf6c8 126
c3d9274a 127;;;###mh-autoload
c26cf6c8
RS
128(defun mh-do-pick-search ()
129 "Find messages that match the qualifications in the current pattern buffer.
a1b4049d 130Messages are searched for in the folder named in `mh-searching-folder'.
847b8219 131Add the messages found to the sequence named `search'."
c26cf6c8
RS
132 (interactive)
133 (let ((pattern-buffer (buffer-name))
c3d9274a
BW
134 (searching-buffer mh-searching-folder)
135 range
136 msgs
137 (pattern nil)
138 (new-buffer nil))
c26cf6c8
RS
139 (save-excursion
140 (cond ((get-buffer searching-buffer)
c3d9274a
BW
141 (set-buffer searching-buffer)
142 (setq range (list (format "%d-%d"
143 mh-first-msg-num mh-last-msg-num))))
144 (t
145 (mh-make-folder searching-buffer)
146 (setq range '("all"))
147 (setq new-buffer t))))
c26cf6c8
RS
148 (message "Searching...")
149 (goto-char (point-min))
847b8219 150 (while (and range
c3d9274a 151 (setq pattern (mh-next-pick-field pattern-buffer)))
c26cf6c8 152 (setq msgs (mh-seq-from-command searching-buffer
c3d9274a
BW
153 'search
154 (mh-list-to-string
155 (list "pick" pattern searching-buffer
156 "-list"
157 (mh-coalesce-msg-list range)))))
158 (setq range msgs)) ;restrict the pick range for next pass
c26cf6c8
RS
159 (message "Searching...done")
160 (if new-buffer
c3d9274a
BW
161 (mh-scan-folder searching-buffer msgs)
162 (switch-to-buffer searching-buffer))
847b8219
KH
163 (mh-add-msgs-to-seq msgs 'search)
164 (delete-other-windows)))
c26cf6c8 165
bdcfe844
BW
166(defun mh-seq-from-command (folder seq command)
167 "In FOLDER, make a sequence named SEQ by executing COMMAND.
168COMMAND is a list. The first element is a program name
169and the subsequent elements are its arguments, all strings."
c26cf6c8 170 (let ((msg)
c3d9274a
BW
171 (msgs ())
172 (case-fold-search t))
c26cf6c8
RS
173 (save-excursion
174 (save-window-excursion
c3d9274a
BW
175 (if (eq 0 (apply 'mh-exec-cmd-quiet nil command))
176 ;; "pick" outputs one number per line
177 (while (setq msg (car (mh-read-msg-list)))
178 (setq msgs (cons msg msgs))
179 (forward-line 1))))
c26cf6c8 180 (set-buffer folder)
c3d9274a 181 (setq msgs (nreverse msgs)) ;put in ascending order
c26cf6c8
RS
182 msgs)))
183
c26cf6c8 184(defun mh-next-pick-field (buffer)
bdcfe844
BW
185 "Return the next piece of a pick argument extracted from BUFFER.
186Return a list like (\"--fieldname\" \"pattern\") or (\"-search\" \"bodypat\")
187or nil if no pieces remain."
c26cf6c8
RS
188 (set-buffer buffer)
189 (let ((case-fold-search t))
190 (cond ((eobp)
c3d9274a
BW
191 nil)
192 ((re-search-forward "^\\([a-z][^: \t\n]*\\):[ \t]*\\([a-z0-9].*\\)$"
193 nil t)
194 (let* ((component
195 (format "--%s"
196 (downcase (buffer-substring (match-beginning 1)
197 (match-end 1)))))
198 (pat (buffer-substring (match-beginning 2) (match-end 2))))
199 (forward-line 1)
200 (list component pat)))
201 ((re-search-forward "^-*$" nil t)
202 (forward-char 1)
203 (let ((body (buffer-substring (point) (point-max))))
204 (if (and (> (length body) 0) (not (equal body "\n")))
205 (list "-search" body)
206 nil)))
207 (t
208 nil))))
c26cf6c8 209
bdcfe844
BW
210\f
211
c26cf6c8 212;;; Build the pick-mode keymap:
bdcfe844 213;;; If this changes, modify mh-pick-mode-help-messages accordingly, above.
a1b4049d 214(gnus-define-keys mh-pick-mode-map
c3d9274a
BW
215 "\C-c?" mh-help
216 "\C-c\C-c" mh-do-pick-search
217 "\C-c\C-f\C-b" mh-to-field
218 "\C-c\C-f\C-c" mh-to-field
219 "\C-c\C-f\C-d" mh-to-field
220 "\C-c\C-f\C-f" mh-to-field
221 "\C-c\C-f\C-r" mh-to-field
222 "\C-c\C-f\C-s" mh-to-field
223 "\C-c\C-f\C-t" mh-to-field
224 "\C-c\C-fb" mh-to-field
225 "\C-c\C-fc" mh-to-field
226 "\C-c\C-fd" mh-to-field
227 "\C-c\C-ff" mh-to-field
228 "\C-c\C-fr" mh-to-field
229 "\C-c\C-fs" mh-to-field
230 "\C-c\C-ft" mh-to-field)
a1b4049d 231
bdcfe844
BW
232(provide 'mh-pick)
233
234;;; Local Variables:
c3d9274a 235;;; indent-tabs-mode: nil
bdcfe844
BW
236;;; sentence-end-double-space: nil
237;;; End:
60370d40
PJ
238
239;;; mh-pick.el ends here