(auto-mode-alist): Add uppercase version of archive
[bpt/emacs.git] / lisp / mail / mh-e.el
CommitLineData
c26cf6c8
RS
1;;; mh-e.el --- GNU Emacs interface to the MH mail system
2
b578f267 3;; Copyright (C) 1985,86,87,88,90,92,93,94,95 Free Software Foundation, Inc.
c26cf6c8
RS
4
5;; Maintainer: Stephen Gildea <gildea@lcs.mit.edu>
942fc772 6;; Version: 5.0.2
c26cf6c8 7;; Keywords: mail
847b8219 8;; Bug-reports: include `M-x mh-version' output in any correspondence
c26cf6c8 9
942fc772 10;; This file is part of mh-e, part of GNU Emacs.
9b7bc076
KH
11
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
b578f267
EN
29;; HOW TO USE:
30;; M-x mh-rmail to read mail. Type C-h m there for a list of commands.
31;; C-u M-x mh-rmail to visit any folder.
32;; M-x mh-smail to send mail. From within the mail reader, "m" works, too.
33
34;; MH (Message Handler) is a powerful mail reader. The MH newsgroup
35;; is comp.mail.mh; the mailing list is mh-users@ics.uci.edu (send to
36;; mh-users-request to be added). See the monthly Frequently Asked
37;; Questions posting there for information on getting MH and mh-e.
38
39;; mh-e is an Emacs interface to the MH mail system.
40;; The mailing list mh-e@x.org is for discussion of mh-e and
41;; announcements of new versions. Send a "subscribe" message to
42;; mh-e-request@x.org to be added. Do not report bugs here; mail
43;; them directly to the author (see top of mh-e.el source).
44;; Include the output of M-x mh-version in any bug report.
45
46;; mh-e works with GNU Emacs 18 or 19, and MH 6.
47
48;; NB. MH must have been compiled with the MHE compiler flag or several
49;; features necessary for mh-e will be missing from MH commands, specifically
50;; the -build switch to repl and forw.
51
52;; Your .emacs might benefit from these bindings:
53;; (global-set-key "\C-cr" 'mh-rmail)
54;; (global-set-key "\C-xm" 'mh-smail)
55;; (global-set-key "\C-x4m" 'mh-smail-other-window)
56
717e06e5 57;;; Change Log:
b578f267
EN
58
59;; Original version for Gosling emacs by Brian Reid, Stanford, 1982.
60;; Modified by James Larus, BBN, July 1984 and UCB, 1984 & 1985.
61;; Rewritten for GNU Emacs, James Larus 1985. larus@ginger.berkeley.edu
62;; Modified by Stephen Gildea 1988. gildea@lcs.mit.edu
2b70d21e 63(defconst mh-e-RCS-id "$Id: mh-e.el,v 1.13 1996/01/25 01:02:59 kwzh Exp kwzh $")
c26cf6c8
RS
64
65;;; Code:
66
67(provide 'mh-e)
68(require 'mh-utils)
69
70\f
c26cf6c8
RS
71;;; Hooks:
72
73(defvar mh-folder-mode-hook nil
86a32538 74 "Invoked in MH-Folder mode on a new folder.")
c26cf6c8
RS
75
76(defvar mh-inc-folder-hook nil
77 "Invoked by \\<mh-folder-mode-map>`\\[mh-inc-folder]' after incorporating mail into a folder.")
78
79(defvar mh-show-hook nil
80 "Invoked after \\<mh-folder-mode-map>`\\[mh-show]' shows a message.")
81
82(defvar mh-show-mode-hook nil
86a32538 83 "Invoked in MH-Show mode on each message.")
c26cf6c8
RS
84
85(defvar mh-delete-msg-hook nil
86 "Invoked after marking each message for deletion.")
87
88(defvar mh-refile-msg-hook nil
89 "Invoked after marking each message for refiling.")
90
91(defvar mh-before-quit-hook nil
92 "Invoked by \\<mh-folder-mode-map>`\\[mh-quit]' before quitting mh-e. See also mh-quit-hook.")
93
94(defvar mh-quit-hook nil
95 "Invoked after \\<mh-folder-mode-map>`\\[mh-quit]' quits mh-e. See also mh-before-quit-hook.")
96
97
98
99;;; Personal preferences:
100
847b8219 101(defvar mh-lpr-command-format "lpr -J '%s'"
c26cf6c8
RS
102 "*Format for Unix command that prints a message.
103The string should be a Unix command line, with the string '%s' where
104the job's name (folder and message number) should appear. The formatted
105message text is piped to this command when you type \\<mh-folder-mode-map>`\\[mh-print-msg]'.")
106
107(defvar mh-scan-prog "scan"
108 "*Program to run to generate one-line-per-message listing of a folder.
109Normally \"scan\" or a file name linked to scan. This file is searched
110for relative to the mh-progs directory unless it is an absolute pathname.
111Automatically becomes buffer-local when set in any fashion.")
112(make-variable-buffer-local 'mh-scan-prog)
113
114(defvar mh-inc-prog "inc"
115 "*Program to run to incorporate new mail into a folder.
116Normally \"inc\". This file is searched for relative to
117the mh-progs directory unless it is an absolute pathname.")
118
119(defvar mh-print-background nil
120 "*Print messages in the background if non-nil.
121WARNING: do not delete the messages until printing is finished;
122otherwise, your output may be truncated.")
123
124(defvar mh-recenter-summary-p nil
125 "*Recenter summary window when the show window is toggled off if non-nil.")
126
c26cf6c8
RS
127(defvar mh-do-not-confirm nil
128 "*Non-nil means do not prompt for confirmation before some mh-e commands.
129Affects non-recoverable commands such as mh-kill-folder and mh-undo-folder.")
130
131(defvar mh-store-default-directory nil
132 "*Last directory used by \\[mh-store-msg]; default for next store.
133A directory name string, or nil to use current directory.")
134
135;;; Parameterize mh-e to work with different scan formats. The defaults work
136;;; with the standard MH scan listings, in which the first 4 characters on
137;;; the line are the message number, followed by two places for notations.
138
139(defvar mh-good-msg-regexp "^....[^D^]"
bd0343fe 140 "Regexp specifying the scan lines that are 'good' messages.")
c26cf6c8
RS
141
142(defvar mh-deleted-msg-regexp "^....D"
143 "Regexp matching scan lines of deleted messages.")
144
145(defvar mh-refiled-msg-regexp "^....\\^"
146 "Regexp matching scan lines of refiled messages.")
147
148(defvar mh-valid-scan-line "^ *[0-9]"
149 "Regexp matching scan lines for messages (not error messages).")
150
c26cf6c8
RS
151(defvar mh-cur-scan-msg-regexp "^....\\+"
152 "Regexp matching scan line for the cur message.")
153
847b8219
KH
154(defvar mh-note-deleted "D"
155 "String whose first character is used to notate deleted messages.")
156
157(defvar mh-note-refiled "^"
158 "String whose first character is used to notate refiled messages.")
159
160(defvar mh-note-cur "+"
161 "String whose first character is used to notate the current message.")
c26cf6c8
RS
162
163(defvar mh-partial-folder-mode-line-annotation "select"
164 "Annotation when displaying part of a folder.
165The string is displayed after the folder's name. NIL for no annotation.")
166
167
168;;; Internal variables:
169
847b8219 170(defvar mh-last-destination nil) ;Destination of last refile or write command.
c26cf6c8
RS
171
172(defvar mh-folder-mode-map (make-keymap)
173 "Keymap for MH folders.")
174
847b8219 175(defvar mh-delete-list nil) ;List of msg numbers to delete.
c26cf6c8 176
847b8219 177(defvar mh-refile-list nil) ;List of folder names in mh-seq-list.
c26cf6c8 178
847b8219 179(defvar mh-next-direction 'forward) ;Direction to move to next message.
c26cf6c8 180
847b8219 181(defvar mh-narrowed-to-seq nil) ;Sequence display is narrowed to or nil if not narrowed.
c26cf6c8 182
847b8219 183(defvar mh-first-msg-num nil) ;Number of first msg in buffer.
c26cf6c8 184
847b8219 185(defvar mh-last-msg-num nil) ;Number of last msg in buffer.
c26cf6c8 186
847b8219 187(defvar mh-mode-line-annotation nil) ;Indiction this is not the full folder.
c26cf6c8
RS
188
189;;; Macros and generic functions:
190
191(defun mh-mapc (func list)
192 (while list
193 (funcall func (car list))
194 (setq list (cdr list))))
195
196\f
197
198;;; Entry points:
199
200;;;###autoload
201(defun mh-rmail (&optional arg)
202 "Inc(orporate) new mail with MH, or, with arg, scan an MH mail folder.
203This function is an entry point to mh-e, the Emacs front end
204to the MH mail system."
205 (interactive "P")
206 (mh-find-path)
207 (if arg
208 (call-interactively 'mh-visit-folder)
209 (mh-inc-folder)))
210
211
212;;; mh-smail and mh-smail-other-window have been moved to the new file
213;;; mh-comp.el, but Emacs 18 still looks for them here, so provide a
214;;; definition here, too, for a while.
215
216(defun mh-smail ()
217 "Compose and send mail with the MH mail system.
218This function is an entry point to mh-e, the Emacs front end
219to the MH mail system."
220 (interactive)
221 (mh-find-path)
222 (require 'mh-comp)
223 (call-interactively 'mh-send))
224
225
226(defun mh-smail-other-window ()
227 "Compose and send mail in other window with the MH mail system.
228This function is an entry point to mh-e, the Emacs front end
229to the MH mail system."
230 (interactive)
231 (mh-find-path)
232 (require 'mh-comp)
233 (call-interactively 'mh-send-other-window))
234
235\f
236
237;;; User executable mh-e commands:
238
239
240(defun mh-delete-msg (msg-or-seq)
241 "Mark the specified MESSAGE(s) for subsequent deletion and move to the next.
242Default is the displayed message. If optional prefix argument is
243given then prompt for the message sequence."
244 (interactive (list (if current-prefix-arg
245 (mh-read-seq-default "Delete" t)
246 (mh-get-msg-num t))))
247 (mh-delete-msg-no-motion msg-or-seq)
248 (mh-next-msg))
249
250
251(defun mh-delete-msg-no-motion (msg-or-seq)
252 "Mark the specified MESSAGE(s) for subsequent deletion.
253Default is the displayed message. If optional prefix argument is
254provided, then prompt for the message sequence."
255 (interactive (list (if current-prefix-arg
256 (mh-read-seq-default "Delete" t)
257 (mh-get-msg-num t))))
258 (if (numberp msg-or-seq)
259 (mh-delete-a-msg msg-or-seq)
260 (mh-map-to-seq-msgs 'mh-delete-a-msg msg-or-seq)))
261
262
263(defun mh-execute-commands ()
264 "Process outstanding delete and refile requests."
265 (interactive)
266 (if mh-narrowed-to-seq (mh-widen))
267 (mh-process-commands mh-current-folder)
268 (mh-set-scan-mode)
269 (mh-goto-cur-msg) ; after mh-set-scan-mode for efficiency
270 (mh-make-folder-mode-line)
847b8219 271 t) ; return t for [local-]write-file-hooks
c26cf6c8
RS
272
273
274(defun mh-first-msg ()
275 "Move to the first message."
276 (interactive)
847b8219
KH
277 (goto-char (point-min))
278 (while (and (not (eobp)) (not (looking-at mh-valid-scan-line)))
279 (forward-line 1)))
c26cf6c8
RS
280
281
282(defun mh-header-display ()
283 "Show the current message with all its headers.
86a32538
RS
284Displays headers that might have been suppressed by setting the
285variables `mh-clean-message-header' or `mhl-formfile', or by the fallback
286behavior of scrolling uninteresting headers off the top of the window.
287Type \"\\[mh-show]\" to show the message normally again."
c26cf6c8
RS
288 (interactive)
289 (and (not mh-showing-with-headers)
290 (or mhl-formfile mh-clean-message-header)
291 (mh-invalidate-show-buffer))
292 (let ((mhl-formfile nil)
293 (mh-clean-message-header nil))
294 (mh-show-msg nil)
295 (mh-in-show-buffer (mh-show-buffer)
296 (goto-char (point-min))
297 (mh-recenter 0))
298 (setq mh-showing-with-headers t)))
299
300
301(defun mh-inc-folder (&optional maildrop-name)
847b8219 302 "Inc(orporate)s new mail into the Inbox folder.
c26cf6c8 303Optional prefix argument specifies an alternate maildrop from the default.
847b8219
KH
304If the prefix argument is given, incorporates mail into the current
305folder, otherwise uses the folder named by `mh-inbox'.
306Runs `mh-inc-folder-hook' after incorporating new mail.
c26cf6c8
RS
307Do not call this function from outside mh-e; use \\[mh-rmail] instead."
308 (interactive (list (if current-prefix-arg
309 (expand-file-name
310 (read-file-name "inc mail from file: "
311 mh-user-path)))))
312 (let ((config (current-window-configuration)))
313 (if (not maildrop-name)
847b8219
KH
314 (cond ((not (get-buffer mh-inbox))
315 (mh-make-folder mh-inbox)
c26cf6c8 316 (setq mh-previous-window-config config))
847b8219
KH
317 ((not (eq (current-buffer) (get-buffer mh-inbox)))
318 (switch-to-buffer mh-inbox)
c26cf6c8
RS
319 (setq mh-previous-window-config config)))))
320 (mh-get-new-mail maildrop-name)
321 (run-hooks 'mh-inc-folder-hook))
322
323
324(defun mh-last-msg ()
325 "Move to the last message."
326 (interactive)
327 (goto-char (point-max))
328 (while (and (not (bobp)) (looking-at "^$"))
329 (forward-line -1)))
330
331
332(defun mh-next-undeleted-msg (&optional arg)
847b8219
KH
333 "Move to the NTH next undeleted message in window."
334 (interactive "p")
c26cf6c8 335 (setq mh-next-direction 'forward)
847b8219 336 (forward-line 1)
c26cf6c8
RS
337 (cond ((re-search-forward mh-good-msg-regexp nil 0 arg)
338 (beginning-of-line)
339 (mh-maybe-show))
340 (t
341 (forward-line -1)
342 (if (get-buffer mh-show-buffer)
343 (delete-windows-on mh-show-buffer)))))
344
345
847b8219
KH
346(defun mh-refile-msg (msg-or-seq folder)
347 "Refile MESSAGE(s) (default: displayed message) into FOLDER.
c26cf6c8
RS
348If optional prefix argument provided, then prompt for message sequence."
349 (interactive
350 (list (if current-prefix-arg
351 (mh-read-seq-default "Refile" t)
352 (mh-get-msg-num t))
353 (intern
354 (mh-prompt-for-folder
355 "Destination"
847b8219
KH
356 (or (and mh-default-folder-for-message-function
357 (let ((refile-file (mh-msg-filename (mh-get-msg-num t))))
c26cf6c8 358 (save-excursion
847b8219 359 (set-buffer (get-buffer-create mh-temp-buffer))
c26cf6c8 360 (erase-buffer)
847b8219
KH
361 (insert-file-contents refile-file)
362 (let ((buffer-file-name refile-file))
363 (funcall mh-default-folder-for-message-function)))))
c26cf6c8
RS
364 (and (eq 'refile (car mh-last-destination))
365 (symbol-name (cdr mh-last-destination)))
366 "")
367 t))))
847b8219 368 (setq mh-last-destination (cons 'refile folder))
c26cf6c8 369 (if (numberp msg-or-seq)
847b8219
KH
370 (mh-refile-a-msg msg-or-seq folder)
371 (mh-map-to-seq-msgs 'mh-refile-a-msg msg-or-seq folder))
c26cf6c8
RS
372 (mh-next-msg))
373
374
847b8219 375(defun mh-refile-or-write-again (message)
c26cf6c8
RS
376 "Re-execute the last refile or write command on the given MESSAGE.
377Default is the displayed message. Use the same folder or file as the
378previous refile or write command."
379 (interactive (list (mh-get-msg-num t)))
380 (if (null mh-last-destination)
381 (error "No previous refile or write"))
382 (cond ((eq (car mh-last-destination) 'refile)
847b8219 383 (mh-refile-a-msg message (cdr mh-last-destination))
c26cf6c8
RS
384 (message "Destination folder: %s" (cdr mh-last-destination)))
385 (t
847b8219 386 (apply 'mh-write-msg-to-file message (cdr mh-last-destination))
c26cf6c8
RS
387 (message "Destination: %s" (cdr mh-last-destination))))
388 (mh-next-msg))
389
390
391(defun mh-quit ()
847b8219 392 "Quit the current mh-e folder.
c26cf6c8
RS
393Start by running mh-before-quit-hook. Restore the previous window
394configuration, if one exists. Finish by running mh-quit-hook."
395 (interactive)
396 (run-hooks 'mh-before-quit-hook)
847b8219 397 (mh-update-sequences)
c26cf6c8
RS
398 (mh-invalidate-show-buffer)
399 (bury-buffer (current-buffer))
400 (if (get-buffer mh-show-buffer)
401 (bury-buffer mh-show-buffer))
402 (if mh-previous-window-config
403 (set-window-configuration mh-previous-window-config))
404 (run-hooks 'mh-quit-hook))
405
406(defun mh-page-msg (&optional arg)
407 "Page the displayed message forwards.
408Scrolls ARG lines or a full screen if no argument is supplied."
409 (interactive "P")
410 (scroll-other-window arg))
411
412
413(defun mh-previous-page (&optional arg)
414 "Page the displayed message backwards.
415Scrolls ARG lines or a full screen if no argument is supplied."
416 (interactive "P")
417 (mh-in-show-buffer (mh-show-buffer)
418 (scroll-down arg)))
419
420
421(defun mh-previous-undeleted-msg (&optional arg)
847b8219 422 "Move to the NTH previous undeleted message in window."
c26cf6c8
RS
423 (interactive "p")
424 (setq mh-next-direction 'backward)
425 (beginning-of-line)
426 (cond ((re-search-backward mh-good-msg-regexp nil 0 arg)
427 (mh-maybe-show))
428 (t
429 (if (get-buffer mh-show-buffer)
430 (delete-windows-on mh-show-buffer)))))
431
432
433(defun mh-rescan-folder (&optional range)
434 "Rescan a folder after optionally processing the outstanding commands.
435If optional prefix argument is provided, prompt for the range of
436messages to display. Otherwise show the entire folder."
437 (interactive (list (if current-prefix-arg
438 (mh-read-msg-range "Range to scan [all]? ")
439 nil)))
440 (setq mh-next-direction 'forward)
441 (mh-scan-folder mh-current-folder (or range "all")))
442
443
444(defun mh-write-msg-to-file (msg file no-headers)
445 "Append MESSAGE to the end of a FILE.
446If NO-HEADERS (prefix argument) is provided, write only the message body.
447Otherwise send the entire message including the headers."
448 (interactive
449 (list (mh-get-msg-num t)
450 (let ((default-dir (if (eq 'write (car mh-last-destination))
451 (file-name-directory (car (cdr mh-last-destination)))
452 default-directory)))
847b8219
KH
453 (read-file-name (format "Save message%s in file: "
454 (if current-prefix-arg " body" ""))
455 default-dir
456 (if (eq 'write (car mh-last-destination))
457 (car (cdr mh-last-destination))
458 (expand-file-name "mail.out" default-dir))))
c26cf6c8 459 current-prefix-arg))
847b8219 460 (let ((msg-file-to-output (mh-msg-filename msg))
c26cf6c8 461 (output-file (mh-expand-file-name file)))
847b8219 462 (setq mh-last-destination (list 'write file (if no-headers 'no-headers)))
c26cf6c8 463 (save-excursion
847b8219 464 (set-buffer (get-buffer-create mh-temp-buffer))
c26cf6c8 465 (erase-buffer)
847b8219 466 (insert-file-contents msg-file-to-output)
c26cf6c8
RS
467 (goto-char (point-min))
468 (if no-headers (search-forward "\n\n"))
469 (append-to-file (point) (point-max) output-file))))
470
471
472(defun mh-toggle-showing ()
473 "Toggle the scanning mode/showing mode of displaying messages."
474 (interactive)
475 (if mh-showing
476 (mh-set-scan-mode)
477 (mh-show)))
478
479
480(defun mh-undo (msg-or-seq)
847b8219 481 "Undo the pending deletion or refile of the specified MESSAGE(s).
c26cf6c8
RS
482Default is the displayed message. If optional prefix argument is
483provided, then prompt for the message sequence."
484 (interactive (list (if current-prefix-arg
485 (mh-read-seq-default "Undo" t)
486 (mh-get-msg-num t))))
487 (cond ((numberp msg-or-seq)
488 (let ((original-position (point)))
489 (beginning-of-line)
490 (while (not (or (looking-at mh-deleted-msg-regexp)
491 (looking-at mh-refiled-msg-regexp)
492 (and (eq mh-next-direction 'forward) (bobp))
493 (and (eq mh-next-direction 'backward)
494 (save-excursion (forward-line) (eobp)))))
495 (forward-line (if (eq mh-next-direction 'forward) -1 1)))
496 (if (or (looking-at mh-deleted-msg-regexp)
497 (looking-at mh-refiled-msg-regexp))
498 (progn
499 (mh-undo-msg (mh-get-msg-num t))
500 (mh-maybe-show))
501 (goto-char original-position)
502 (error "Nothing to undo"))))
503 (t
847b8219 504 (mh-map-to-seq-msgs 'mh-undo-msg msg-or-seq)))
c26cf6c8
RS
505 ;; update the mh-refile-list so mh-outstanding-commands-p will work
506 (mh-mapc (function
507 (lambda (elt)
508 (if (not (mh-seq-to-msgs elt))
509 (setq mh-refile-list (delq elt mh-refile-list)))))
510 mh-refile-list)
511 (if (not (mh-outstanding-commands-p))
512 (mh-set-folder-modified-p nil)))
513
514
847b8219 515;;;###autoload
c26cf6c8 516(defun mh-version ()
847b8219 517 "Display version information about mh-e and the MH mail handling system."
c26cf6c8
RS
518 (interactive)
519 (mh-find-progs)
847b8219 520 (set-buffer (get-buffer-create mh-temp-buffer))
c26cf6c8 521 (erase-buffer)
2b70d21e 522 (insert " mh-e info:\n\nversion: " mh-e-RCS-id
c26cf6c8
RS
523 "\nEmacs: " emacs-version " on " (symbol-name system-type) " ")
524 (condition-case ()
525 (call-process "uname" nil t nil "-a")
526 (file-error))
527 (insert "\n\n MH info:\n\n" (expand-file-name "inc" mh-progs) ":\n")
528 (let ((help-start (point)))
529 (condition-case err-data
530 (mh-exec-cmd-output "inc" nil "-help")
531 (file-error (insert (mapconcat 'concat (cdr err-data) ": "))))
532 (goto-char help-start)
533 (search-forward "version: " nil t)
534 (beginning-of-line)
535 (delete-region help-start (point))
536 (goto-char (point-min)))
847b8219 537 (display-buffer mh-temp-buffer))
c26cf6c8
RS
538
539
540(defun mh-visit-folder (folder &optional range)
847b8219 541 "Visit FOLDER and display RANGE of messages.
c26cf6c8 542Do not call this function from outside mh-e; see \\[mh-rmail] instead."
847b8219 543 (interactive (list (mh-prompt-for-folder "Visit" mh-inbox t)
c26cf6c8
RS
544 (mh-read-msg-range "Range [all]? ")))
545 (let ((config (current-window-configuration)))
546 (mh-scan-folder folder (or range "all"))
547 (setq mh-previous-window-config config))
548 nil)
549
550
551(defun mh-compat-quit ()
847b8219
KH
552 "The \"b\" key is obsolescent; will assume you want \"\\[mh-quit]\" ..."
553 ;; Was going to make it run mh-burst-digest, but got complaint that
554 ;; 'b' should mean 'back', as it does in info, less, and rn.
555 ;; This is a temporary compatibility function.
c26cf6c8
RS
556 (interactive)
557 (message "%s" (documentation this-command))
558 (sit-for 1)
559 (call-interactively 'mh-quit))
560
847b8219
KH
561
562(defun mh-update-sequences ()
563 "Update MH's Unseen sequence and current folder and message.
564Flush mh-e's state out to MH. The message at the cursor becomes current."
565 (interactive)
566 ;; mh-update-sequences is the opposite of mh-read-folder-sequences,
567 ;; which updates mh-e's state from MH.
568 (let ((folder-set (mh-update-unseen))
569 (new-cur (mh-get-msg-num nil)))
570 (if new-cur
571 (let ((seq-entry (mh-find-seq 'cur)))
572 (mh-remove-cur-notation)
573 (setcdr seq-entry (list new-cur)) ;delete-seq-locally, add-msgs-to-seq
574 (mh-define-sequence 'cur (list new-cur))
575 (beginning-of-line)
576 (if (looking-at mh-good-msg-regexp)
577 (mh-notate nil mh-note-cur mh-cmd-note)))
578 (or folder-set
579 (save-excursion
580 (mh-exec-cmd-quiet t "folder" mh-current-folder "-fast"))))))
581
582
c26cf6c8
RS
583\f
584
585;;; Support routines.
586
587(defun mh-delete-a-msg (msg)
588 ;; Delete the MESSAGE.
589 (save-excursion
590 (mh-goto-msg msg nil t)
591 (if (looking-at mh-refiled-msg-regexp)
592 (error "Message %d is refiled. Undo refile before deleting." msg))
593 (if (looking-at mh-deleted-msg-regexp)
594 nil
595 (mh-set-folder-modified-p t)
596 (setq mh-delete-list (cons msg mh-delete-list))
597 (mh-add-msgs-to-seq msg 'deleted t)
847b8219 598 (mh-notate msg mh-note-deleted mh-cmd-note)
c26cf6c8
RS
599 (run-hooks 'mh-delete-msg-hook))))
600
601(defun mh-refile-a-msg (msg destination)
602 ;; Refile MESSAGE in FOLDER. FOLDER is a symbol, not a string.
603 (save-excursion
604 (mh-goto-msg msg nil t)
605 (cond ((looking-at mh-deleted-msg-regexp)
606 (error "Message %d is deleted. Undo delete before moving." msg))
607 ((looking-at mh-refiled-msg-regexp)
608 (if (y-or-n-p
609 (format "Message %d already refiled. Copy to %s as well? "
610 msg destination))
611 (mh-exec-cmd "refile" (mh-get-msg-num t) "-link"
612 "-src" mh-current-folder
613 (symbol-name destination))
614 (message "Message not copied.")))
615 (t
616 (mh-set-folder-modified-p t)
617 (if (not (memq destination mh-refile-list))
618 (setq mh-refile-list (cons destination mh-refile-list)))
619 (if (not (memq msg (mh-seq-to-msgs destination)))
620 (mh-add-msgs-to-seq msg destination t))
847b8219 621 (mh-notate msg mh-note-refiled mh-cmd-note)
c26cf6c8
RS
622 (run-hooks 'mh-refile-msg-hook)))))
623
624
625(defun mh-next-msg ()
626 ;; Move backward or forward to the next undeleted message in the buffer.
627 (if (eq mh-next-direction 'forward)
628 (mh-next-undeleted-msg 1)
629 (mh-previous-undeleted-msg 1)))
630
631
632(defun mh-set-scan-mode ()
633 ;; Display the scan listing buffer, but do not show a message.
634 (if (get-buffer mh-show-buffer)
635 (delete-windows-on mh-show-buffer))
636 (setq mh-showing nil)
dc9bdc98 637 (force-mode-line-update)
c26cf6c8
RS
638 (if mh-recenter-summary-p
639 (mh-recenter nil)))
640
641
642(defun mh-undo-msg (msg)
643 ;; Undo the deletion or refile of one MESSAGE.
644 (cond ((memq msg mh-delete-list)
645 (setq mh-delete-list (delq msg mh-delete-list))
646 (mh-delete-msg-from-seq msg 'deleted t))
647 (t
648 (mh-mapc (function (lambda (dest)
649 (mh-delete-msg-from-seq msg dest t)))
650 mh-refile-list)))
651 (mh-notate msg ? mh-cmd-note))
652
653
654\f
655
656;;; The folder data abstraction.
657
658(defun mh-make-folder (name)
659 ;; Create and initialize a new mail folder called NAME and make it the
660 ;; current folder.
661 (switch-to-buffer name)
662 (setq buffer-read-only nil)
663 (erase-buffer)
664 (setq buffer-read-only t)
665 (mh-folder-mode)
666 (mh-set-folder-modified-p nil)
847b8219
KH
667 (setq buffer-file-name mh-folder-filename)
668 (mh-make-folder-mode-line))
c26cf6c8
RS
669
670
671;;; Ensure new buffers won't get this mode if default-major-mode is nil.
672(put 'mh-folder-mode 'mode-class 'special)
673
674(defun mh-folder-mode ()
675 "Major mh-e mode for \"editing\" an MH folder scan listing.\\<mh-folder-mode-map>
676You can show the message the cursor is pointing to, and step through the
677messages. Messages can be marked for deletion or refiling into another
678folder; these commands are executed all at once with a separate command.
679
680A prefix argument (\\[universal-argument]) to delete, refile, list, or undo
681applies the action to a message sequence.
682
683Here is a list of the standard keys for mh-e commands, grouped by function.
684This list is purposefully not customized; mh-e has a long history, and many
685alternate key bindings as a result. This list is to encourage users to use
686standard keys so the other keys can perhaps someday be put to new uses.
687
688t toggle show or scan-only mode
847b8219 689RET show message, or back to top if already showing
c26cf6c8 690
847b8219
KH
691SPC page message forward
692DEL page message back
c26cf6c8
RS
693
694n next message
695p previous message
847b8219 696g go to message by number
c26cf6c8
RS
697
698d mark for deletion
699o, ^ mark for output (refile) to another folder
700? show folder of pending refile
701u undo delete or refile marking
702
703x execute marked deletes and refiles
704i incorporate new mail
705
706m mail a new message
707r reply to a message
708f forward a message
709
710q quit mh-e
711
712M-f visit new folder
713M-r rescan this folder
714
715Here are all the commands with their current binding, listed in key order:
716\\{mh-folder-mode-map}
717
718Variables controlling mh-e operation are (defaults in parentheses):
719
720 mh-recursive-folders (nil)
721 Non-nil means commands which operate on folders do so recursively.
722
723 mh-bury-show-buffer (t)
724 Non-nil means that the buffer used to display message is buried.
725 It will never be offered as the default other buffer.
726
727 mh-clean-message-header (nil)
728 Non-nil means remove header lines matching the regular expression
729 specified in mh-invisible-headers from messages.
730
731 mh-visible-headers (nil)
732 If non-nil, it contains a regexp specifying the headers that are shown in
733 a message if mh-clean-message-header is non-nil. Setting this variable
734 overrides mh-invisible-headers.
735
736 mh-do-not-confirm (nil)
737 Non-nil means do not prompt for confirmation before executing some
738 non-recoverable commands such as mh-kill-folder and mh-undo-folder.
739
740 mhl-formfile (nil)
741 Name of format file to be used by mhl to show messages.
742 A value of T means use the default format file.
743 Nil means don't use mhl to format messages.
744
745 mh-lpr-command-format (\"lpr -p -J '%s'\")
746 Format for command used to print a message on a system printer.
747
748 mh-scan-prog (\"scan\")
749 Program to run to generate one-line-per-message listing of a folder.
750 Normally \"scan\" or a file name linked to scan. This file is searched
751 for relative to the mh-progs directory unless it is an absolute pathname.
752 Automatically becomes buffer-local when set in any fashion.
753
754 mh-print-background (nil)
755 Print messages in the background if non-nil.
756 WARNING: do not delete the messages until printing is finished;
757 otherwise, your output may be truncated.
758
759 mh-recenter-summary-p (nil)
760 If non-nil, then the scan listing is recentered when the window displaying
761 a messages is toggled off.
762
763 mh-summary-height (4)
764 Number of lines in the summary window including the mode line.
765
c26cf6c8
RS
766The value of mh-folder-mode-hook is called when a new folder is set up."
767
768 (kill-all-local-variables)
769 (use-local-map mh-folder-mode-map)
770 (setq major-mode 'mh-folder-mode)
771 (mh-set-mode-name "MH-Folder")
847b8219 772 (mh-make-local-vars
c26cf6c8
RS
773 'mh-current-folder (buffer-name) ; Name of folder, a string
774 'mh-show-buffer (format "show-%s" (buffer-name)) ; Buffer that displays msgs
775 'mh-folder-filename ; e.g. "/usr/foobar/Mail/inbox/"
776 (file-name-as-directory (mh-expand-file-name (buffer-name)))
777 'mh-showing nil ; Show message also?
c26cf6c8
RS
778 'mh-delete-list nil ; List of msgs nums to delete
779 'mh-refile-list nil ; List of folder names in mh-seq-list
780 'mh-seq-list nil ; Alist of (seq . msgs) nums
781 'mh-seen-list nil ; List of displayed messages
782 'mh-next-direction 'forward ; Direction to move to next message
783 'mh-narrowed-to-seq nil ; Sequence display is narrowed to
784 'mh-first-msg-num nil ; Number of first msg in buffer
785 'mh-last-msg-num nil ; Number of last msg in buffer
942fc772 786 'mh-msg-count nil ; Number of msgs in buffer
847b8219 787 'mh-mode-line-annotation nil ; Indiction this is not the full folder
c26cf6c8
RS
788 'mh-previous-window-config nil) ; Previous window configuration
789 (setq truncate-lines t)
790 (auto-save-mode -1)
791 (setq buffer-offer-save t)
847b8219
KH
792 (if (boundp 'local-write-file-hooks)
793 (setq local-write-file-hooks '(mh-execute-commands)) ;Emacs 19
794 (make-local-variable 'write-file-hooks)
795 (setq write-file-hooks '(mh-execute-commands))) ;Emacs 18
c26cf6c8
RS
796 (make-local-variable 'revert-buffer-function)
797 (setq revert-buffer-function 'mh-undo-folder)
798 (or (assq 'mh-showing minor-mode-alist)
799 (setq minor-mode-alist
800 (cons '(mh-showing " Show") minor-mode-alist)))
801 (run-hooks 'mh-folder-mode-hook))
802
803
847b8219
KH
804(defun mh-make-local-vars (&rest pairs)
805 ;; Take VARIABLE-VALUE pairs and make local variables initialized to the
c26cf6c8
RS
806 ;; value.
807 (while pairs
808 (make-variable-buffer-local (car pairs))
809 (set (car pairs) (car (cdr pairs)))
810 (setq pairs (cdr (cdr pairs)))))
811
812
813(defun mh-scan-folder (folder range)
814 ;; Scan the FOLDER over the RANGE. Return in the folder's buffer.
815 (cond ((null (get-buffer folder))
816 (mh-make-folder folder))
817 (t
818 (mh-process-or-undo-commands folder)
819 (switch-to-buffer folder)))
820 (mh-regenerate-headers range)
821 (cond ((zerop (buffer-size))
822 (if (equal range "all")
823 (message "Folder %s is empty" folder)
824 (message "No messages in %s, range %s" folder range))
825 (sit-for 5)))
826 (mh-goto-cur-msg))
827
828
847b8219
KH
829(defun mh-regenerate-headers (range &optional update)
830 ;; scan folder over range RANGE.
831 ;; If UPDATE, append the scan lines, otherwise replace.
832 (let ((folder mh-current-folder)
833 scan-start)
c26cf6c8
RS
834 (message "Scanning %s..." folder)
835 (with-mh-folder-updating (nil)
847b8219
KH
836 (if update
837 (goto-char (point-max))
838 (erase-buffer))
839 (setq scan-start (point))
c26cf6c8
RS
840 (mh-exec-cmd-output mh-scan-prog nil
841 "-noclear" "-noheader"
842 "-width" (window-width)
843 folder range)
847b8219 844 (goto-char scan-start)
c26cf6c8
RS
845 (cond ((looking-at "scan: no messages in")
846 (keep-lines mh-valid-scan-line)) ; Flush random scan lines
847 ((looking-at "scan: ")) ; Keep error messages
848 (t
849 (keep-lines mh-valid-scan-line))) ; Flush random scan lines
c26cf6c8
RS
850 (setq mh-seq-list (mh-read-folder-sequences folder nil))
851 (mh-notate-user-sequences)
847b8219
KH
852 (or update
853 (setq mh-mode-line-annotation
854 (if (equal range "all")
855 nil
856 mh-partial-folder-mode-line-annotation)))
857 (mh-make-folder-mode-line))
c26cf6c8
RS
858 (message "Scanning %s...done" folder)))
859
860
861(defun mh-get-new-mail (maildrop-name)
862 ;; Read new mail from a maildrop into the current buffer.
863 ;; Return in the current buffer.
864 (let ((point-before-inc (point))
865 (folder mh-current-folder)
866 (new-mail-p nil))
867 (with-mh-folder-updating (t)
f965c3d7
KH
868 (if maildrop-name
869 (message "inc %s -file %s..." folder maildrop-name)
870 (message "inc %s..." folder))
c26cf6c8
RS
871 (setq mh-next-direction 'forward)
872 (goto-char (point-max))
873 (let ((start-of-inc (point)))
874 (if maildrop-name
847b8219
KH
875 ;; I think MH 5 used "-ms-file" instead of "-file",
876 ;; which would make inc'ing from maildrops fail.
c26cf6c8
RS
877 (mh-exec-cmd-output mh-inc-prog nil folder
878 "-file" (expand-file-name maildrop-name)
879 "-width" (window-width)
880 "-truncate")
847b8219
KH
881 (mh-exec-cmd-output mh-inc-prog nil
882 "-width" (window-width)))
f965c3d7
KH
883 (if maildrop-name
884 (message "inc %s -file %s...done" folder maildrop-name)
885 (message "inc %s...done" folder))
c26cf6c8 886 (goto-char start-of-inc)
847b8219
KH
887 (cond ((save-excursion
888 (re-search-forward "^inc: no mail" nil t))
c26cf6c8
RS
889 (message "No new mail%s%s" (if maildrop-name " in " "")
890 (if maildrop-name maildrop-name "")))
891 ((re-search-forward "^inc:" nil t) ; Error messages
892 (error "inc error"))
893 (t
847b8219 894 (mh-remove-cur-notation)
c26cf6c8
RS
895 (setq new-mail-p t)))
896 (keep-lines mh-valid-scan-line) ; Flush random scan lines
c26cf6c8
RS
897 (setq mh-seq-list (mh-read-folder-sequences folder t))
898 (mh-notate-user-sequences)
899 (if new-mail-p
900 (progn
942fc772
KH
901 (mh-make-folder-mode-line)
902 (mh-goto-cur-msg))
c26cf6c8
RS
903 (goto-char point-before-inc))))))
904
905
847b8219 906(defun mh-make-folder-mode-line (&optional ignored)
c26cf6c8 907 ;; Set the fields of the mode line for a folder buffer.
847b8219
KH
908 ;; The optional argument is now obsolete. It used to be used to pass
909 ;; in what is now stored in the buffer-local variable
910 ;; mh-mode-line-annotation.
c26cf6c8
RS
911 (save-excursion
912 (mh-first-msg)
913 (setq mh-first-msg-num (mh-get-msg-num nil))
914 (mh-last-msg)
915 (setq mh-last-msg-num (mh-get-msg-num nil))
942fc772
KH
916 (setq mh-msg-count (count-lines (point-min) (point-max)))
917 (setq mode-line-buffer-identification
918 (list (format "{%%b%s} %d msg%s"
919 (if mh-mode-line-annotation
920 (format "/%s" mh-mode-line-annotation)
921 "")
922 mh-msg-count
923 (if (zerop mh-msg-count)
924 "s"
925 (if (> mh-msg-count 1)
926 (format "s (%d-%d)" mh-first-msg-num
927 mh-last-msg-num)
928 (format " (%d)" mh-first-msg-num))))))))
c26cf6c8
RS
929
930
931(defun mh-unmark-all-headers (remove-all-flags)
932 ;; Remove all '+' flags from the headers, and if called with a non-nil
933 ;; argument, remove all 'D', '^' and '%' flags too.
934 ;; Optimized for speed (i.e., no regular expressions).
935 (save-excursion
936 (let ((case-fold-search nil)
847b8219 937 (last-line (1- (point-max)))
c26cf6c8
RS
938 char)
939 (mh-first-msg)
940 (while (<= (point) last-line)
941 (forward-char mh-cmd-note)
942 (setq char (following-char))
943 (if (or (and remove-all-flags
847b8219
KH
944 (or (eql char (aref mh-note-deleted 0))
945 (eql char (aref mh-note-refiled 0))))
946 (eql char (aref mh-note-cur 0)))
c26cf6c8
RS
947 (progn
948 (delete-char 1)
949 (insert " ")))
847b8219
KH
950 (if remove-all-flags
951 (progn
952 (forward-char 1)
953 (if (eql (following-char) (aref mh-note-seq 0))
954 (progn
955 (delete-char 1)
956 (insert " ")))))
c26cf6c8
RS
957 (forward-line)))))
958
959
847b8219
KH
960(defun mh-remove-cur-notation ()
961 ;; Remove old cur notation (cf mh-goto-cur-msg code).
962 (let ((cur-msg (car (mh-seq-to-msgs 'cur))))
963 (save-excursion
964 (and cur-msg
965 (mh-goto-msg cur-msg t t)
966 (looking-at mh-cur-scan-msg-regexp)
967 (mh-notate nil ? mh-cmd-note)))))
968
c26cf6c8
RS
969(defun mh-goto-cur-msg ()
970 ;; Position the cursor at the current message.
971 (let ((cur-msg (car (mh-seq-to-msgs 'cur))))
972 (cond ((and cur-msg
847b8219
KH
973 (mh-goto-msg cur-msg t t))
974 (mh-notate nil mh-note-cur mh-cmd-note)
c26cf6c8
RS
975 (mh-recenter 0)
976 (mh-maybe-show cur-msg))
977 (t
978 (mh-last-msg)
979 (message "No current message")))))
980
981
982(defun mh-process-or-undo-commands (folder)
983 ;; If FOLDER has outstanding commands, then either process or discard them.
984 ;; Called by functions like mh-sort-folder, so also invalidate show buffer.
985 (set-buffer folder)
986 (if (mh-outstanding-commands-p)
987 (if (or mh-do-not-confirm
988 (y-or-n-p
989 "Process outstanding deletes and refiles (or lose them)? "))
990 (mh-process-commands folder)
991 (mh-undo-folder)))
992 (mh-update-unseen)
993 (mh-invalidate-show-buffer))
994
995
996(defun mh-process-commands (folder)
997 ;; Process outstanding commands for the folder FOLDER.
998 (message "Processing deletes and refiles for %s..." folder)
999 (set-buffer folder)
1000 (with-mh-folder-updating (nil)
1001 ;; Update the unseen sequence if it exists
1002 (mh-update-unseen)
1003
1004 ;; Then refile messages
1005 (mh-mapc
1006 (function
1007 (lambda (dest)
1008 (let ((msgs (mh-seq-to-msgs dest)))
1009 (cond (msgs
1010 (apply 'mh-exec-cmd "refile"
847b8219
KH
1011 "-src" folder (symbol-name dest)
1012 (mh-coalesce-msg-list msgs))
c26cf6c8
RS
1013 (mh-delete-scan-msgs msgs))))))
1014 mh-refile-list)
1015 (setq mh-refile-list nil)
1016
1017 ;; Now delete messages
1018 (cond (mh-delete-list
847b8219
KH
1019 (apply 'mh-exec-cmd "rmm" folder
1020 (mh-coalesce-msg-list mh-delete-list))
c26cf6c8
RS
1021 (mh-delete-scan-msgs mh-delete-list)
1022 (setq mh-delete-list nil)))
1023
1024 ;; Don't need to remove sequences since delete and refile do so.
1025
1026 ;; Mark cur message
1027 (if (> (buffer-size) 0)
847b8219 1028 (mh-define-sequence 'cur (list (or (mh-get-msg-num nil) "last"))))
c26cf6c8
RS
1029
1030 (and (buffer-file-name (get-buffer mh-show-buffer))
1031 (not (file-exists-p (buffer-file-name (get-buffer mh-show-buffer))))
1032 ;; If "inc" were to put a new msg in this file,
1033 ;; we would not notice, so mark it invalid now.
1034 (mh-invalidate-show-buffer))
1035
1036 (setq mh-seq-list (mh-read-folder-sequences mh-current-folder nil))
1037 (mh-unmark-all-headers t)
1038 (mh-notate-user-sequences)
1039 (message "Processing deletes and refiles for %s...done" folder)))
1040
1041
1042(defun mh-update-unseen ()
847b8219
KH
1043 ;; Flush updates to the Unseen sequence out to MH.
1044 ;; Return non-NIL iff set the MH folder.
c26cf6c8 1045 (if mh-seen-list
847b8219
KH
1046 (let* ((unseen-seq (mh-find-seq mh-unseen-seq))
1047 (unseen-msgs (mh-seq-msgs unseen-seq)))
1048 (if unseen-msgs
1049 (progn
1050 (mh-undefine-sequence mh-unseen-seq mh-seen-list)
1051 (while mh-seen-list
1052 (setq unseen-msgs (delq (car mh-seen-list) unseen-msgs))
1053 (setq mh-seen-list (cdr mh-seen-list)))
1054 (setcdr unseen-seq unseen-msgs)
1055 t) ;since we set the folder
1056 (setq mh-seen-list nil)))))
c26cf6c8
RS
1057
1058
1059(defun mh-delete-scan-msgs (msgs)
1060 ;; Delete the scan listing lines for each of the msgs in the LIST.
c26cf6c8 1061 (save-excursion
942fc772
KH
1062 (while msgs
1063 (if (mh-goto-msg (car msgs) t t)
1064 (mh-delete-line 1))
1065 (setq msgs (cdr msgs)))))
c26cf6c8
RS
1066
1067
1068(defun mh-outstanding-commands-p ()
1069 ;; Returns non-nil if there are outstanding deletes or refiles.
1070 (or mh-delete-list mh-refile-list))
1071
847b8219
KH
1072
1073(defun mh-coalesce-msg-list (messages)
1074 ;; Give a list of MESSAGES, return a list of message number ranges.
1075 ;; Sort of the opposite of mh-read-msg-list, which expands ranges.
1076 ;; Message lists passed to MH programs go through this so
1077 ;; command line arguments won't exceed system limits.
1078 (let ((msgs (sort (copy-sequence messages) 'mh-greaterp))
1079 (range-high nil)
1080 (prev -1)
1081 (ranges nil))
1082 (while prev
1083 (if range-high
1084 (if (or (not (numberp prev))
1085 (not (eql (car msgs) (1- prev))))
1086 (progn ;non-sequential, flush old range
1087 (if (eql prev range-high)
1088 (setq ranges (cons range-high ranges))
1089 (setq ranges (cons (format "%s-%s" prev range-high) ranges)))
1090 (setq range-high nil))))
1091 (or range-high
1092 (setq range-high (car msgs))) ;start new or first range
1093 (setq prev (car msgs))
1094 (setq msgs (cdr msgs)))
1095 ranges))
1096
1097(defun mh-greaterp (msg1 msg2)
1098 ;; Sort two message indicators. Strings are "smaller" than numbers.
1099 ;; Legal values are things like "cur", "last", 1, and 1820.
1100 (if (numberp msg1)
1101 (if (numberp msg2)
1102 (> msg1 msg2)
1103 t)
1104 (if (numberp msg2)
1105 nil
1106 (string-lessp msg2 msg1))))
1107
c26cf6c8
RS
1108\f
1109
1110;;; Basic sequence handling
1111
1112(defun mh-delete-seq-locally (seq)
1113 ;; Remove mh-e's record of SEQUENCE.
1114 (let ((entry (mh-find-seq seq)))
1115 (setq mh-seq-list (delq entry mh-seq-list))))
1116
1117(defun mh-read-folder-sequences (folder save-refiles)
1118 ;; Read and return the predefined sequences for a FOLDER.
1119 ;; If SAVE-REFILES is non-nil, then keep the sequences
1120 ;; that note messages to be refiled.
1121 (let ((seqs ()))
1122 (cond (save-refiles
1123 (mh-mapc (function (lambda (seq) ; Save the refiling sequences
1124 (if (mh-folder-name-p (mh-seq-name seq))
1125 (setq seqs (cons seq seqs)))))
1126 mh-seq-list)))
1127 (save-excursion
1128 (if (eq 0 (mh-exec-cmd-quiet nil "mark" folder "-list"))
1129 (progn
1130 ;; look for name in line of form "cur: 4" or "myseq (private): 23"
1131 (while (re-search-forward "^[^: ]+" nil t)
1132 (setq seqs (cons (mh-make-seq (intern (buffer-substring
1133 (match-beginning 0)
1134 (match-end 0)))
1135 (mh-read-msg-list))
1136 seqs)))
1137 (delete-region (point-min) (point))))) ; avoid race with mh-process-daemon
1138 seqs))
1139
1140(defun mh-read-msg-list ()
1141 ;; Return a list of message numbers from the current point to the end of
847b8219 1142 ;; the line. Expands ranges into set of individual numbers.
c26cf6c8
RS
1143 (let ((msgs ())
1144 (end-of-line (save-excursion (end-of-line) (point)))
1145 num)
1146 (while (re-search-forward "[0-9]+" end-of-line t)
1147 (setq num (string-to-int (buffer-substring (match-beginning 0)
1148 (match-end 0))))
1149 (cond ((looking-at "-") ; Message range
1150 (forward-char 1)
1151 (re-search-forward "[0-9]+" end-of-line t)
1152 (let ((num2 (string-to-int (buffer-substring (match-beginning 0)
1153 (match-end 0)))))
1154 (if (< num2 num)
1155 (error "Bad message range: %d-%d" num num2))
1156 (while (<= num num2)
1157 (setq msgs (cons num msgs))
1158 (setq num (1+ num)))))
847b8219
KH
1159 ((not (zerop num)) ;"pick" outputs "0" to mean no match
1160 (setq msgs (cons num msgs)))))
c26cf6c8
RS
1161 msgs))
1162
1163(defun mh-notate-user-sequences ()
1164 ;; Mark the scan listing of all messages in user-defined sequences.
1165 (let ((seqs mh-seq-list)
1166 name)
1167 (while seqs
1168 (setq name (mh-seq-name (car seqs)))
1169 (if (not (mh-internal-seq name))
847b8219 1170 (mh-notate-seq name mh-note-seq (1+ mh-cmd-note)))
c26cf6c8
RS
1171 (setq seqs (cdr seqs)))))
1172
1173
1174(defun mh-internal-seq (name)
1175 ;; Return non-NIL if NAME is the name of an internal mh-e sequence.
1176 (or (memq name '(answered cur deleted forwarded printed))
1177 (eq name mh-unseen-seq)
1178 (eq name mh-previous-seq)
1179 (mh-folder-name-p name)))
1180
1181
847b8219 1182(defun mh-delete-msg-from-seq (message sequence &optional internal-flag)
c26cf6c8 1183 "Delete MESSAGE from SEQUENCE. MESSAGE defaults to displayed message.
847b8219
KH
1184From Lisp, optional third arg INTERNAL-FLAG non-nil means do not
1185inform MH of the change."
c26cf6c8
RS
1186 (interactive (list (mh-get-msg-num t)
1187 (mh-read-seq-default "Delete from" t)
1188 nil))
847b8219 1189 (let ((entry (mh-find-seq sequence)))
c26cf6c8 1190 (cond (entry
847b8219 1191 (mh-notate-if-in-one-seq message ? (1+ mh-cmd-note) sequence)
c26cf6c8 1192 (if (not internal-flag)
847b8219
KH
1193 (mh-undefine-sequence sequence (list message)))
1194 (setcdr entry (delq message (mh-seq-msgs entry)))))))
c26cf6c8
RS
1195
1196
1197(defun mh-undefine-sequence (seq msgs)
847b8219 1198 ;; Remove from the SEQUENCE the list of MSGS.
c26cf6c8
RS
1199 (mh-exec-cmd "mark" mh-current-folder "-delete"
1200 "-sequence" (symbol-name seq)
847b8219 1201 (mh-coalesce-msg-list msgs)))
c26cf6c8
RS
1202
1203
1204(defun mh-define-sequence (seq msgs)
1205 ;; Define the SEQUENCE to contain the list of MSGS.
1206 ;; Do not mark pseudo-sequences or empty sequences.
1207 ;; Signals an error if SEQUENCE is an illegal name.
1208 (if (and msgs
1209 (not (mh-folder-name-p seq)))
1210 (save-excursion
1211 (mh-exec-cmd-error nil "mark" mh-current-folder "-add" "-zero"
1212 "-sequence" (symbol-name seq)
847b8219 1213 (mh-coalesce-msg-list msgs)))))
c26cf6c8
RS
1214
1215
1216(defun mh-map-over-seqs (func seq-list)
1217 ;; Apply the FUNCTION to each element in the list of SEQUENCES,
1218 ;; passing the sequence name and the list of messages as arguments.
1219 (while seq-list
1220 (funcall func (mh-seq-name (car seq-list)) (mh-seq-msgs (car seq-list)))
1221 (setq seq-list (cdr seq-list))))
1222
1223
1224(defun mh-notate-if-in-one-seq (msg notation offset seq)
1225 ;; If the MESSAGE is in only the SEQUENCE, then mark the scan listing of the
1226 ;; message with the CHARACTER at the given OFFSET from the beginning of the
1227 ;; listing line.
847b8219 1228 (let ((in-seqs (mh-seq-containing-msg msg nil)))
c26cf6c8
RS
1229 (if (and (eq seq (car in-seqs)) (null (cdr in-seqs)))
1230 (mh-notate msg notation offset))))
1231
1232
847b8219 1233(defun mh-seq-containing-msg (msg &optional include-internal-p)
c26cf6c8 1234 ;; Return a list of the sequences containing MESSAGE.
847b8219 1235 ;; If INCLUDE-INTERNAL-P non-nil, include mh-e internal sequences in list.
c26cf6c8
RS
1236 (let ((l mh-seq-list)
1237 (seqs ()))
1238 (while l
847b8219
KH
1239 (and (memq msg (mh-seq-msgs (car l)))
1240 (or include-internal-p
1241 (not (mh-internal-seq (mh-seq-name (car l)))))
1242 (setq seqs (cons (mh-seq-name (car l)) seqs)))
c26cf6c8
RS
1243 (setq l (cdr l)))
1244 seqs))
1245
1246
1247\f
1248
1249;;; User prompting commands.
1250
1251
1252(defun mh-read-msg-range (prompt)
1253 ;; Read a list of blank-separated items.
1254 (let* ((buf (read-string prompt))
1255 (buf-size (length buf))
1256 (start 0)
1257 (input ()))
1258 (while (< start buf-size)
1259 (let ((next (read-from-string buf start buf-size)))
1260 (setq input (cons (car next) input))
1261 (setq start (cdr next))))
1262 (nreverse input)))
1263
1264\f
1265
1266;;; Build the folder-mode keymap:
1267
1268(suppress-keymap mh-folder-mode-map)
1269(define-key mh-folder-mode-map "q" 'mh-quit)
1270(define-key mh-folder-mode-map "b" 'mh-compat-quit)
1271(define-key mh-folder-mode-map "?" 'mh-msg-is-in-seq)
1272(define-key mh-folder-mode-map "%" 'mh-put-msg-in-seq)
1273(define-key mh-folder-mode-map "|" 'mh-pipe-msg)
1274(define-key mh-folder-mode-map "\ea" 'mh-edit-again)
1275(define-key mh-folder-mode-map "\e%" 'mh-delete-msg-from-seq)
1276(define-key mh-folder-mode-map "\e#" 'mh-delete-seq)
1277(define-key mh-folder-mode-map "\C-xn" 'mh-narrow-to-seq)
1278(define-key mh-folder-mode-map "\C-xw" 'mh-widen)
1279(define-key mh-folder-mode-map "\eb" 'mh-burst-digest)
1280(define-key mh-folder-mode-map "\eu" 'mh-undo-folder)
1281(define-key mh-folder-mode-map "\e " 'mh-page-digest)
1282(define-key mh-folder-mode-map "\e\177" 'mh-page-digest-backwards)
1283(define-key mh-folder-mode-map "\ed" 'mh-redistribute)
1284(define-key mh-folder-mode-map "\ee" 'mh-extract-rejected-mail)
1285(define-key mh-folder-mode-map "\ef" 'mh-visit-folder)
1286(define-key mh-folder-mode-map "\ek" 'mh-kill-folder)
1287(define-key mh-folder-mode-map "\el" 'mh-list-folders)
1288(define-key mh-folder-mode-map "\en" 'mh-store-msg)
1289(define-key mh-folder-mode-map "\ep" 'mh-pack-folder)
1290(define-key mh-folder-mode-map "\eq" 'mh-list-sequences)
1291(define-key mh-folder-mode-map "\es" 'mh-search-folder)
1292(define-key mh-folder-mode-map "\er" 'mh-rescan-folder)
1293(define-key mh-folder-mode-map "l" 'mh-print-msg)
1294(define-key mh-folder-mode-map "t" 'mh-toggle-showing)
1295(define-key mh-folder-mode-map "c" 'mh-copy-msg)
1296(define-key mh-folder-mode-map "i" 'mh-inc-folder)
1297(define-key mh-folder-mode-map "x" 'mh-execute-commands)
1298(define-key mh-folder-mode-map "e" 'mh-execute-commands)
1299(define-key mh-folder-mode-map "f" 'mh-forward)
1300(define-key mh-folder-mode-map "m" 'mh-send)
1301(define-key mh-folder-mode-map "s" 'mh-send)
1302(define-key mh-folder-mode-map "r" 'mh-reply)
1303(define-key mh-folder-mode-map "a" 'mh-reply)
1304(define-key mh-folder-mode-map "j" 'mh-goto-msg)
1305(define-key mh-folder-mode-map "g" 'mh-goto-msg)
847b8219 1306(define-key mh-folder-mode-map "\e<" 'mh-first-msg)
c26cf6c8
RS
1307(define-key mh-folder-mode-map "\e>" 'mh-last-msg)
1308(define-key mh-folder-mode-map "\177" 'mh-previous-page)
1309(define-key mh-folder-mode-map " " 'mh-page-msg)
847b8219 1310(define-key mh-folder-mode-map "\r" 'mh-show)
c26cf6c8
RS
1311(define-key mh-folder-mode-map "." 'mh-show)
1312(define-key mh-folder-mode-map "," 'mh-header-display)
1313(define-key mh-folder-mode-map "u" 'mh-undo)
1314(define-key mh-folder-mode-map "d" 'mh-delete-msg)
1315(define-key mh-folder-mode-map "\C-d" 'mh-delete-msg-no-motion)
1316(define-key mh-folder-mode-map "p" 'mh-previous-undeleted-msg)
1317(define-key mh-folder-mode-map "n" 'mh-next-undeleted-msg)
1318(define-key mh-folder-mode-map "o" 'mh-refile-msg)
1319(define-key mh-folder-mode-map "^" 'mh-refile-msg)
1320(define-key mh-folder-mode-map "\C-o" 'mh-write-msg-to-file)
1321(define-key mh-folder-mode-map ">" 'mh-write-msg-to-file)
1322(define-key mh-folder-mode-map "!" 'mh-refile-or-write-again)
1323
942fc772
KH
1324;; "C-c /" prefix is used in mh-folder-mode by pgp.el and mailcrypt
1325
c26cf6c8
RS
1326\f
1327
1328;;;autoload the other mh-e parts
1329
1330;;; mh-comp
1331
1332(autoload 'mh-smail "mh-comp"
847b8219
KH
1333 "Compose and send mail with the MH mail system.
1334This function is an entry point to mh-e, the Emacs front end
1335to the MH mail system.
1336See documentation of `\\[mh-send]' for more details on composing mail." t)
1337
c26cf6c8 1338(autoload 'mh-smail-other-window "mh-comp"
847b8219
KH
1339 "Compose and send mail in other window with the MH mail system.
1340This function is an entry point to mh-e, the Emacs front end
1341to the MH mail system.
1342See documentation of `\\[mh-send]' for more details on composing mail." t)
1343
c26cf6c8 1344(autoload 'mh-edit-again "mh-comp"
847b8219
KH
1345 "Clean-up a draft or a message previously sent and make it resendable.
1346Default is the current message.
1347The variable mh-new-draft-cleaned-headers specifies the headers to remove.
1348See also documentation for `\\[mh-send]' function." t)
1349
c26cf6c8 1350(autoload 'mh-extract-rejected-mail "mh-comp"
847b8219
KH
1351 "Extract a letter returned by the mail system and make it resendable.
1352Default is the current message. The variable mh-new-draft-cleaned-headers
1353gives the headers to clean out of the original message.
1354See also documentation for `\\[mh-send]' function." t)
1355
c26cf6c8 1356(autoload 'mh-forward "mh-comp"
847b8219
KH
1357 "Forward a message or message sequence. Defaults to displayed message.
1358If optional prefix argument provided, then prompt for the message sequence.
1359See also documentation for `\\[mh-send]' function." t)
1360
c26cf6c8 1361(autoload 'mh-redistribute "mh-comp"
847b8219
KH
1362 "Redistribute a letter.
1363Depending on how your copy of MH was compiled, you may need to change the
1364setting of the variable mh-redist-full-contents. See its documentation." t)
1365
c26cf6c8 1366(autoload 'mh-reply "mh-comp"
847b8219
KH
1367 "Reply to a MESSAGE (default: displayed message).
1368If optional prefix argument INCLUDEP provided, then include the message
1369in the reply using filter mhl.reply in your MH directory.
1370Prompts for type of addresses to reply to:
1371 from sender only,
1372 to sender and primary recipients,
1373 cc/all sender and all recipients.
1374If the file named by `mh-repl-formfile' exists, it is used as a skeleton
1375for the reply. See also documentation for `\\[mh-send]' function." t)
1376
c26cf6c8 1377(autoload 'mh-send "mh-comp"
847b8219
KH
1378 "Compose and send a letter.
1379The file named by `mh-comp-formfile' will be used as the form.
1380Do not call this function from outside mh-e; use \\[mh-smail] instead.
1381The letter is composed in mh-letter-mode; see its documentation for more
1382details. If `mh-compose-letter-function' is defined, it is called on the
1383draft and passed three arguments: to, subject, and cc." t)
1384
c26cf6c8 1385(autoload 'mh-send-other-window "mh-comp"
847b8219
KH
1386 "Compose and send a letter in another window.
1387Do not call this function from outside mh-e;
1388use \\[mh-smail-other-window] instead.
1389See also documentation for `\\[mh-send]' function." t)
1390
c26cf6c8 1391(autoload 'mh-letter-mode "mh-comp"
847b8219
KH
1392 "Mode for composing letters in mh-e.
1393For more details, type \\[describe-mode] while in MH-Letter mode." t)
c26cf6c8
RS
1394
1395
1396;;; mh-funcs
1397
1398(autoload 'mh-burst-digest "mh-funcs"
847b8219
KH
1399 "Burst apart the current message, which should be a digest.
1400The message is replaced by its table of contents and the messages from the
1401digest are inserted into the folder after that message." t)
1402
c26cf6c8 1403(autoload 'mh-copy-msg "mh-funcs"
847b8219
KH
1404 "Copy to another FOLDER the specified MESSAGE(s) without deleting them.
1405Default is the displayed message. If optional prefix argument is
1406provided, then prompt for the message sequence." t)
1407
c26cf6c8
RS
1408(autoload 'mh-kill-folder "mh-funcs"
1409 "Remove the current folder." t)
847b8219 1410
c26cf6c8
RS
1411(autoload 'mh-list-folders "mh-funcs"
1412 "List mail folders." t)
847b8219 1413
c26cf6c8 1414(autoload 'mh-pack-folder "mh-funcs"
847b8219
KH
1415 "Renumber the messages of a folder to be 1..n.
1416First, offer to execute any outstanding commands for the current folder.
1417If optional prefix argument provided, prompt for the range of messages
1418to display after packing. Otherwise, show the entire folder." t)
1419
c26cf6c8 1420(autoload 'mh-pipe-msg "mh-funcs"
847b8219
KH
1421 "Pipe the current message through the given shell COMMAND.
1422If INCLUDE-HEADERS (prefix argument) is provided, send the entire message.
1423Otherwise just send the message's body without the headers." t)
1424
c26cf6c8
RS
1425(autoload 'mh-page-digest "mh-funcs"
1426 "Advance displayed message to next digested message." t)
847b8219 1427
c26cf6c8
RS
1428(autoload 'mh-page-digest-backwards "mh-funcs"
1429 "Back up displayed message to previous digested message." t)
847b8219 1430
c26cf6c8 1431(autoload 'mh-print-msg "mh-funcs"
847b8219
KH
1432 "Print MESSAGE(s) (default: displayed message) on printer.
1433If optional prefix argument provided, then prompt for the message sequence.
1434The variable mh-lpr-command-format is used to generate the print command.
1435The messages are formatted by mhl. See the variable mhl-formfile." t)
1436
c26cf6c8 1437(autoload 'mh-sort-folder "mh-funcs"
847b8219
KH
1438 "Sort the messages in the current folder by date.
1439Calls the MH program sortm to do the work.
1440The arguments in the list mh-sortm-args are passed to sortm
1441if this function is passed an argument." t)
1442
c26cf6c8
RS
1443(autoload 'mh-undo-folder "mh-funcs"
1444 "Undo all commands in current folder." t)
847b8219 1445
c26cf6c8 1446(autoload 'mh-store-msg "mh-funcs"
86a32538 1447 "Store the file(s) contained in the current message into DIRECTORY.
847b8219
KH
1448The message can contain a shar file or uuencoded file.
1449Default directory is the last directory used, or initially the value of
1450mh-store-default-directory or the current directory." t)
1451
86a32538
RS
1452(autoload 'mh-store-buffer "mh-funcs"
1453 "Store the file(s) contained in the current buffer into DIRECTORY.
847b8219
KH
1454The buffer can contain a shar file or uuencoded file.
1455Default directory is the last directory used, or initially the value of
1456`mh-store-default-directory' or the current directory." t)
c26cf6c8
RS
1457
1458
1459;;; mh-pick
1460
1461(autoload 'mh-search-folder "mh-pick"
847b8219
KH
1462 "Search FOLDER for messages matching a pattern.
1463Add the messages found to the sequence named `search'." t)
c26cf6c8
RS
1464
1465;;; mh-seq
1466
c26cf6c8
RS
1467(autoload 'mh-delete-seq "mh-seq"
1468 "Delete the SEQUENCE." t)
1469(autoload 'mh-list-sequences "mh-seq"
1470 "List the sequences defined in FOLDER." t)
1471(autoload 'mh-msg-is-in-seq "mh-seq"
1472 "Display the sequences that contain MESSAGE (default: displayed message)." t)
1473(autoload 'mh-narrow-to-seq "mh-seq"
847b8219
KH
1474 "Restrict display of this folder to just messages in SEQUENCE
1475Use \\[mh-widen] to undo this command." t)
1476(autoload 'mh-put-msg-in-seq "mh-seq"
1477 "Add MESSAGE(s) (default: displayed message) to SEQUENCE.
1478If optional prefix argument provided, then prompt for the message sequence." t)
c26cf6c8
RS
1479(autoload 'mh-widen "mh-seq"
1480 "Remove restrictions from current folder, thereby showing all messages." t)
1481(autoload 'mh-rename-seq "mh-seq"
847b8219 1482 "Rename SEQUENCE to have NEW-NAME." t)
c26cf6c8
RS
1483
1484;;; mh-e.el ends here