Update FSF's address.
[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
57;; Change Log:
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
63(defconst mh-e-RCS-id "$Id: mh-e.el,v 1.10 1996/01/04 23:45:17 kwzh Exp erik $")
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
RS
521 (erase-buffer)
522 (insert " mh-e info:\n\nversion: " mh-e-version "\n" mh-e-time-stamp
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)
868 (message (if maildrop-name
869 (format "inc %s -file %s..." folder maildrop-name)
870 (format "inc %s..." folder)))
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)))
c26cf6c8
RS
883 (message
884 (if maildrop-name
885 (format "inc %s -file %s...done" folder maildrop-name)
886 (format "inc %s...done" folder)))
887 (goto-char start-of-inc)
847b8219
KH
888 (cond ((save-excursion
889 (re-search-forward "^inc: no mail" nil t))
c26cf6c8
RS
890 (message "No new mail%s%s" (if maildrop-name " in " "")
891 (if maildrop-name maildrop-name "")))
892 ((re-search-forward "^inc:" nil t) ; Error messages
893 (error "inc error"))
894 (t
847b8219 895 (mh-remove-cur-notation)
c26cf6c8
RS
896 (setq new-mail-p t)))
897 (keep-lines mh-valid-scan-line) ; Flush random scan lines
c26cf6c8
RS
898 (setq mh-seq-list (mh-read-folder-sequences folder t))
899 (mh-notate-user-sequences)
900 (if new-mail-p
901 (progn
942fc772
KH
902 (mh-make-folder-mode-line)
903 (mh-goto-cur-msg))
c26cf6c8
RS
904 (goto-char point-before-inc))))))
905
906
847b8219 907(defun mh-make-folder-mode-line (&optional ignored)
c26cf6c8 908 ;; Set the fields of the mode line for a folder buffer.
847b8219
KH
909 ;; The optional argument is now obsolete. It used to be used to pass
910 ;; in what is now stored in the buffer-local variable
911 ;; mh-mode-line-annotation.
c26cf6c8
RS
912 (save-excursion
913 (mh-first-msg)
914 (setq mh-first-msg-num (mh-get-msg-num nil))
915 (mh-last-msg)
916 (setq mh-last-msg-num (mh-get-msg-num nil))
942fc772
KH
917 (setq mh-msg-count (count-lines (point-min) (point-max)))
918 (setq mode-line-buffer-identification
919 (list (format "{%%b%s} %d msg%s"
920 (if mh-mode-line-annotation
921 (format "/%s" mh-mode-line-annotation)
922 "")
923 mh-msg-count
924 (if (zerop mh-msg-count)
925 "s"
926 (if (> mh-msg-count 1)
927 (format "s (%d-%d)" mh-first-msg-num
928 mh-last-msg-num)
929 (format " (%d)" mh-first-msg-num))))))))
c26cf6c8
RS
930
931
932(defun mh-unmark-all-headers (remove-all-flags)
933 ;; Remove all '+' flags from the headers, and if called with a non-nil
934 ;; argument, remove all 'D', '^' and '%' flags too.
935 ;; Optimized for speed (i.e., no regular expressions).
936 (save-excursion
937 (let ((case-fold-search nil)
847b8219 938 (last-line (1- (point-max)))
c26cf6c8
RS
939 char)
940 (mh-first-msg)
941 (while (<= (point) last-line)
942 (forward-char mh-cmd-note)
943 (setq char (following-char))
944 (if (or (and remove-all-flags
847b8219
KH
945 (or (eql char (aref mh-note-deleted 0))
946 (eql char (aref mh-note-refiled 0))))
947 (eql char (aref mh-note-cur 0)))
c26cf6c8
RS
948 (progn
949 (delete-char 1)
950 (insert " ")))
847b8219
KH
951 (if remove-all-flags
952 (progn
953 (forward-char 1)
954 (if (eql (following-char) (aref mh-note-seq 0))
955 (progn
956 (delete-char 1)
957 (insert " ")))))
c26cf6c8
RS
958 (forward-line)))))
959
960
847b8219
KH
961(defun mh-remove-cur-notation ()
962 ;; Remove old cur notation (cf mh-goto-cur-msg code).
963 (let ((cur-msg (car (mh-seq-to-msgs 'cur))))
964 (save-excursion
965 (and cur-msg
966 (mh-goto-msg cur-msg t t)
967 (looking-at mh-cur-scan-msg-regexp)
968 (mh-notate nil ? mh-cmd-note)))))
969
c26cf6c8
RS
970(defun mh-goto-cur-msg ()
971 ;; Position the cursor at the current message.
972 (let ((cur-msg (car (mh-seq-to-msgs 'cur))))
973 (cond ((and cur-msg
847b8219
KH
974 (mh-goto-msg cur-msg t t))
975 (mh-notate nil mh-note-cur mh-cmd-note)
c26cf6c8
RS
976 (mh-recenter 0)
977 (mh-maybe-show cur-msg))
978 (t
979 (mh-last-msg)
980 (message "No current message")))))
981
982
983(defun mh-process-or-undo-commands (folder)
984 ;; If FOLDER has outstanding commands, then either process or discard them.
985 ;; Called by functions like mh-sort-folder, so also invalidate show buffer.
986 (set-buffer folder)
987 (if (mh-outstanding-commands-p)
988 (if (or mh-do-not-confirm
989 (y-or-n-p
990 "Process outstanding deletes and refiles (or lose them)? "))
991 (mh-process-commands folder)
992 (mh-undo-folder)))
993 (mh-update-unseen)
994 (mh-invalidate-show-buffer))
995
996
997(defun mh-process-commands (folder)
998 ;; Process outstanding commands for the folder FOLDER.
999 (message "Processing deletes and refiles for %s..." folder)
1000 (set-buffer folder)
1001 (with-mh-folder-updating (nil)
1002 ;; Update the unseen sequence if it exists
1003 (mh-update-unseen)
1004
1005 ;; Then refile messages
1006 (mh-mapc
1007 (function
1008 (lambda (dest)
1009 (let ((msgs (mh-seq-to-msgs dest)))
1010 (cond (msgs
1011 (apply 'mh-exec-cmd "refile"
847b8219
KH
1012 "-src" folder (symbol-name dest)
1013 (mh-coalesce-msg-list msgs))
c26cf6c8
RS
1014 (mh-delete-scan-msgs msgs))))))
1015 mh-refile-list)
1016 (setq mh-refile-list nil)
1017
1018 ;; Now delete messages
1019 (cond (mh-delete-list
847b8219
KH
1020 (apply 'mh-exec-cmd "rmm" folder
1021 (mh-coalesce-msg-list mh-delete-list))
c26cf6c8
RS
1022 (mh-delete-scan-msgs mh-delete-list)
1023 (setq mh-delete-list nil)))
1024
1025 ;; Don't need to remove sequences since delete and refile do so.
1026
1027 ;; Mark cur message
1028 (if (> (buffer-size) 0)
847b8219 1029 (mh-define-sequence 'cur (list (or (mh-get-msg-num nil) "last"))))
c26cf6c8
RS
1030
1031 (and (buffer-file-name (get-buffer mh-show-buffer))
1032 (not (file-exists-p (buffer-file-name (get-buffer mh-show-buffer))))
1033 ;; If "inc" were to put a new msg in this file,
1034 ;; we would not notice, so mark it invalid now.
1035 (mh-invalidate-show-buffer))
1036
1037 (setq mh-seq-list (mh-read-folder-sequences mh-current-folder nil))
1038 (mh-unmark-all-headers t)
1039 (mh-notate-user-sequences)
1040 (message "Processing deletes and refiles for %s...done" folder)))
1041
1042
1043(defun mh-update-unseen ()
847b8219
KH
1044 ;; Flush updates to the Unseen sequence out to MH.
1045 ;; Return non-NIL iff set the MH folder.
c26cf6c8 1046 (if mh-seen-list
847b8219
KH
1047 (let* ((unseen-seq (mh-find-seq mh-unseen-seq))
1048 (unseen-msgs (mh-seq-msgs unseen-seq)))
1049 (if unseen-msgs
1050 (progn
1051 (mh-undefine-sequence mh-unseen-seq mh-seen-list)
1052 (while mh-seen-list
1053 (setq unseen-msgs (delq (car mh-seen-list) unseen-msgs))
1054 (setq mh-seen-list (cdr mh-seen-list)))
1055 (setcdr unseen-seq unseen-msgs)
1056 t) ;since we set the folder
1057 (setq mh-seen-list nil)))))
c26cf6c8
RS
1058
1059
1060(defun mh-delete-scan-msgs (msgs)
1061 ;; Delete the scan listing lines for each of the msgs in the LIST.
c26cf6c8 1062 (save-excursion
942fc772
KH
1063 (while msgs
1064 (if (mh-goto-msg (car msgs) t t)
1065 (mh-delete-line 1))
1066 (setq msgs (cdr msgs)))))
c26cf6c8
RS
1067
1068
1069(defun mh-outstanding-commands-p ()
1070 ;; Returns non-nil if there are outstanding deletes or refiles.
1071 (or mh-delete-list mh-refile-list))
1072
847b8219
KH
1073
1074(defun mh-coalesce-msg-list (messages)
1075 ;; Give a list of MESSAGES, return a list of message number ranges.
1076 ;; Sort of the opposite of mh-read-msg-list, which expands ranges.
1077 ;; Message lists passed to MH programs go through this so
1078 ;; command line arguments won't exceed system limits.
1079 (let ((msgs (sort (copy-sequence messages) 'mh-greaterp))
1080 (range-high nil)
1081 (prev -1)
1082 (ranges nil))
1083 (while prev
1084 (if range-high
1085 (if (or (not (numberp prev))
1086 (not (eql (car msgs) (1- prev))))
1087 (progn ;non-sequential, flush old range
1088 (if (eql prev range-high)
1089 (setq ranges (cons range-high ranges))
1090 (setq ranges (cons (format "%s-%s" prev range-high) ranges)))
1091 (setq range-high nil))))
1092 (or range-high
1093 (setq range-high (car msgs))) ;start new or first range
1094 (setq prev (car msgs))
1095 (setq msgs (cdr msgs)))
1096 ranges))
1097
1098(defun mh-greaterp (msg1 msg2)
1099 ;; Sort two message indicators. Strings are "smaller" than numbers.
1100 ;; Legal values are things like "cur", "last", 1, and 1820.
1101 (if (numberp msg1)
1102 (if (numberp msg2)
1103 (> msg1 msg2)
1104 t)
1105 (if (numberp msg2)
1106 nil
1107 (string-lessp msg2 msg1))))
1108
c26cf6c8
RS
1109\f
1110
1111;;; Basic sequence handling
1112
1113(defun mh-delete-seq-locally (seq)
1114 ;; Remove mh-e's record of SEQUENCE.
1115 (let ((entry (mh-find-seq seq)))
1116 (setq mh-seq-list (delq entry mh-seq-list))))
1117
1118(defun mh-read-folder-sequences (folder save-refiles)
1119 ;; Read and return the predefined sequences for a FOLDER.
1120 ;; If SAVE-REFILES is non-nil, then keep the sequences
1121 ;; that note messages to be refiled.
1122 (let ((seqs ()))
1123 (cond (save-refiles
1124 (mh-mapc (function (lambda (seq) ; Save the refiling sequences
1125 (if (mh-folder-name-p (mh-seq-name seq))
1126 (setq seqs (cons seq seqs)))))
1127 mh-seq-list)))
1128 (save-excursion
1129 (if (eq 0 (mh-exec-cmd-quiet nil "mark" folder "-list"))
1130 (progn
1131 ;; look for name in line of form "cur: 4" or "myseq (private): 23"
1132 (while (re-search-forward "^[^: ]+" nil t)
1133 (setq seqs (cons (mh-make-seq (intern (buffer-substring
1134 (match-beginning 0)
1135 (match-end 0)))
1136 (mh-read-msg-list))
1137 seqs)))
1138 (delete-region (point-min) (point))))) ; avoid race with mh-process-daemon
1139 seqs))
1140
1141(defun mh-read-msg-list ()
1142 ;; Return a list of message numbers from the current point to the end of
847b8219 1143 ;; the line. Expands ranges into set of individual numbers.
c26cf6c8
RS
1144 (let ((msgs ())
1145 (end-of-line (save-excursion (end-of-line) (point)))
1146 num)
1147 (while (re-search-forward "[0-9]+" end-of-line t)
1148 (setq num (string-to-int (buffer-substring (match-beginning 0)
1149 (match-end 0))))
1150 (cond ((looking-at "-") ; Message range
1151 (forward-char 1)
1152 (re-search-forward "[0-9]+" end-of-line t)
1153 (let ((num2 (string-to-int (buffer-substring (match-beginning 0)
1154 (match-end 0)))))
1155 (if (< num2 num)
1156 (error "Bad message range: %d-%d" num num2))
1157 (while (<= num num2)
1158 (setq msgs (cons num msgs))
1159 (setq num (1+ num)))))
847b8219
KH
1160 ((not (zerop num)) ;"pick" outputs "0" to mean no match
1161 (setq msgs (cons num msgs)))))
c26cf6c8
RS
1162 msgs))
1163
1164(defun mh-notate-user-sequences ()
1165 ;; Mark the scan listing of all messages in user-defined sequences.
1166 (let ((seqs mh-seq-list)
1167 name)
1168 (while seqs
1169 (setq name (mh-seq-name (car seqs)))
1170 (if (not (mh-internal-seq name))
847b8219 1171 (mh-notate-seq name mh-note-seq (1+ mh-cmd-note)))
c26cf6c8
RS
1172 (setq seqs (cdr seqs)))))
1173
1174
1175(defun mh-internal-seq (name)
1176 ;; Return non-NIL if NAME is the name of an internal mh-e sequence.
1177 (or (memq name '(answered cur deleted forwarded printed))
1178 (eq name mh-unseen-seq)
1179 (eq name mh-previous-seq)
1180 (mh-folder-name-p name)))
1181
1182
847b8219 1183(defun mh-delete-msg-from-seq (message sequence &optional internal-flag)
c26cf6c8 1184 "Delete MESSAGE from SEQUENCE. MESSAGE defaults to displayed message.
847b8219
KH
1185From Lisp, optional third arg INTERNAL-FLAG non-nil means do not
1186inform MH of the change."
c26cf6c8
RS
1187 (interactive (list (mh-get-msg-num t)
1188 (mh-read-seq-default "Delete from" t)
1189 nil))
847b8219 1190 (let ((entry (mh-find-seq sequence)))
c26cf6c8 1191 (cond (entry
847b8219 1192 (mh-notate-if-in-one-seq message ? (1+ mh-cmd-note) sequence)
c26cf6c8 1193 (if (not internal-flag)
847b8219
KH
1194 (mh-undefine-sequence sequence (list message)))
1195 (setcdr entry (delq message (mh-seq-msgs entry)))))))
c26cf6c8
RS
1196
1197
1198(defun mh-undefine-sequence (seq msgs)
847b8219 1199 ;; Remove from the SEQUENCE the list of MSGS.
c26cf6c8
RS
1200 (mh-exec-cmd "mark" mh-current-folder "-delete"
1201 "-sequence" (symbol-name seq)
847b8219 1202 (mh-coalesce-msg-list msgs)))
c26cf6c8
RS
1203
1204
1205(defun mh-define-sequence (seq msgs)
1206 ;; Define the SEQUENCE to contain the list of MSGS.
1207 ;; Do not mark pseudo-sequences or empty sequences.
1208 ;; Signals an error if SEQUENCE is an illegal name.
1209 (if (and msgs
1210 (not (mh-folder-name-p seq)))
1211 (save-excursion
1212 (mh-exec-cmd-error nil "mark" mh-current-folder "-add" "-zero"
1213 "-sequence" (symbol-name seq)
847b8219 1214 (mh-coalesce-msg-list msgs)))))
c26cf6c8
RS
1215
1216
1217(defun mh-map-over-seqs (func seq-list)
1218 ;; Apply the FUNCTION to each element in the list of SEQUENCES,
1219 ;; passing the sequence name and the list of messages as arguments.
1220 (while seq-list
1221 (funcall func (mh-seq-name (car seq-list)) (mh-seq-msgs (car seq-list)))
1222 (setq seq-list (cdr seq-list))))
1223
1224
1225(defun mh-notate-if-in-one-seq (msg notation offset seq)
1226 ;; If the MESSAGE is in only the SEQUENCE, then mark the scan listing of the
1227 ;; message with the CHARACTER at the given OFFSET from the beginning of the
1228 ;; listing line.
847b8219 1229 (let ((in-seqs (mh-seq-containing-msg msg nil)))
c26cf6c8
RS
1230 (if (and (eq seq (car in-seqs)) (null (cdr in-seqs)))
1231 (mh-notate msg notation offset))))
1232
1233
847b8219 1234(defun mh-seq-containing-msg (msg &optional include-internal-p)
c26cf6c8 1235 ;; Return a list of the sequences containing MESSAGE.
847b8219 1236 ;; If INCLUDE-INTERNAL-P non-nil, include mh-e internal sequences in list.
c26cf6c8
RS
1237 (let ((l mh-seq-list)
1238 (seqs ()))
1239 (while l
847b8219
KH
1240 (and (memq msg (mh-seq-msgs (car l)))
1241 (or include-internal-p
1242 (not (mh-internal-seq (mh-seq-name (car l)))))
1243 (setq seqs (cons (mh-seq-name (car l)) seqs)))
c26cf6c8
RS
1244 (setq l (cdr l)))
1245 seqs))
1246
1247
1248\f
1249
1250;;; User prompting commands.
1251
1252
1253(defun mh-read-msg-range (prompt)
1254 ;; Read a list of blank-separated items.
1255 (let* ((buf (read-string prompt))
1256 (buf-size (length buf))
1257 (start 0)
1258 (input ()))
1259 (while (< start buf-size)
1260 (let ((next (read-from-string buf start buf-size)))
1261 (setq input (cons (car next) input))
1262 (setq start (cdr next))))
1263 (nreverse input)))
1264
1265\f
1266
1267;;; Build the folder-mode keymap:
1268
1269(suppress-keymap mh-folder-mode-map)
1270(define-key mh-folder-mode-map "q" 'mh-quit)
1271(define-key mh-folder-mode-map "b" 'mh-compat-quit)
1272(define-key mh-folder-mode-map "?" 'mh-msg-is-in-seq)
1273(define-key mh-folder-mode-map "%" 'mh-put-msg-in-seq)
1274(define-key mh-folder-mode-map "|" 'mh-pipe-msg)
1275(define-key mh-folder-mode-map "\ea" 'mh-edit-again)
1276(define-key mh-folder-mode-map "\e%" 'mh-delete-msg-from-seq)
1277(define-key mh-folder-mode-map "\e#" 'mh-delete-seq)
1278(define-key mh-folder-mode-map "\C-xn" 'mh-narrow-to-seq)
1279(define-key mh-folder-mode-map "\C-xw" 'mh-widen)
1280(define-key mh-folder-mode-map "\eb" 'mh-burst-digest)
1281(define-key mh-folder-mode-map "\eu" 'mh-undo-folder)
1282(define-key mh-folder-mode-map "\e " 'mh-page-digest)
1283(define-key mh-folder-mode-map "\e\177" 'mh-page-digest-backwards)
1284(define-key mh-folder-mode-map "\ed" 'mh-redistribute)
1285(define-key mh-folder-mode-map "\ee" 'mh-extract-rejected-mail)
1286(define-key mh-folder-mode-map "\ef" 'mh-visit-folder)
1287(define-key mh-folder-mode-map "\ek" 'mh-kill-folder)
1288(define-key mh-folder-mode-map "\el" 'mh-list-folders)
1289(define-key mh-folder-mode-map "\en" 'mh-store-msg)
1290(define-key mh-folder-mode-map "\ep" 'mh-pack-folder)
1291(define-key mh-folder-mode-map "\eq" 'mh-list-sequences)
1292(define-key mh-folder-mode-map "\es" 'mh-search-folder)
1293(define-key mh-folder-mode-map "\er" 'mh-rescan-folder)
1294(define-key mh-folder-mode-map "l" 'mh-print-msg)
1295(define-key mh-folder-mode-map "t" 'mh-toggle-showing)
1296(define-key mh-folder-mode-map "c" 'mh-copy-msg)
1297(define-key mh-folder-mode-map "i" 'mh-inc-folder)
1298(define-key mh-folder-mode-map "x" 'mh-execute-commands)
1299(define-key mh-folder-mode-map "e" 'mh-execute-commands)
1300(define-key mh-folder-mode-map "f" 'mh-forward)
1301(define-key mh-folder-mode-map "m" 'mh-send)
1302(define-key mh-folder-mode-map "s" 'mh-send)
1303(define-key mh-folder-mode-map "r" 'mh-reply)
1304(define-key mh-folder-mode-map "a" 'mh-reply)
1305(define-key mh-folder-mode-map "j" 'mh-goto-msg)
1306(define-key mh-folder-mode-map "g" 'mh-goto-msg)
847b8219 1307(define-key mh-folder-mode-map "\e<" 'mh-first-msg)
c26cf6c8
RS
1308(define-key mh-folder-mode-map "\e>" 'mh-last-msg)
1309(define-key mh-folder-mode-map "\177" 'mh-previous-page)
1310(define-key mh-folder-mode-map " " 'mh-page-msg)
847b8219 1311(define-key mh-folder-mode-map "\r" 'mh-show)
c26cf6c8
RS
1312(define-key mh-folder-mode-map "." 'mh-show)
1313(define-key mh-folder-mode-map "," 'mh-header-display)
1314(define-key mh-folder-mode-map "u" 'mh-undo)
1315(define-key mh-folder-mode-map "d" 'mh-delete-msg)
1316(define-key mh-folder-mode-map "\C-d" 'mh-delete-msg-no-motion)
1317(define-key mh-folder-mode-map "p" 'mh-previous-undeleted-msg)
1318(define-key mh-folder-mode-map "n" 'mh-next-undeleted-msg)
1319(define-key mh-folder-mode-map "o" 'mh-refile-msg)
1320(define-key mh-folder-mode-map "^" 'mh-refile-msg)
1321(define-key mh-folder-mode-map "\C-o" 'mh-write-msg-to-file)
1322(define-key mh-folder-mode-map ">" 'mh-write-msg-to-file)
1323(define-key mh-folder-mode-map "!" 'mh-refile-or-write-again)
1324
942fc772
KH
1325;; "C-c /" prefix is used in mh-folder-mode by pgp.el and mailcrypt
1326
c26cf6c8
RS
1327\f
1328
1329;;;autoload the other mh-e parts
1330
1331;;; mh-comp
1332
1333(autoload 'mh-smail "mh-comp"
847b8219
KH
1334 "Compose and send mail with the MH mail system.
1335This function is an entry point to mh-e, the Emacs front end
1336to the MH mail system.
1337See documentation of `\\[mh-send]' for more details on composing mail." t)
1338
c26cf6c8 1339(autoload 'mh-smail-other-window "mh-comp"
847b8219
KH
1340 "Compose and send mail in other window with the MH mail system.
1341This function is an entry point to mh-e, the Emacs front end
1342to the MH mail system.
1343See documentation of `\\[mh-send]' for more details on composing mail." t)
1344
c26cf6c8 1345(autoload 'mh-edit-again "mh-comp"
847b8219
KH
1346 "Clean-up a draft or a message previously sent and make it resendable.
1347Default is the current message.
1348The variable mh-new-draft-cleaned-headers specifies the headers to remove.
1349See also documentation for `\\[mh-send]' function." t)
1350
c26cf6c8 1351(autoload 'mh-extract-rejected-mail "mh-comp"
847b8219
KH
1352 "Extract a letter returned by the mail system and make it resendable.
1353Default is the current message. The variable mh-new-draft-cleaned-headers
1354gives the headers to clean out of the original message.
1355See also documentation for `\\[mh-send]' function." t)
1356
c26cf6c8 1357(autoload 'mh-forward "mh-comp"
847b8219
KH
1358 "Forward a message or message sequence. Defaults to displayed message.
1359If optional prefix argument provided, then prompt for the message sequence.
1360See also documentation for `\\[mh-send]' function." t)
1361
c26cf6c8 1362(autoload 'mh-redistribute "mh-comp"
847b8219
KH
1363 "Redistribute a letter.
1364Depending on how your copy of MH was compiled, you may need to change the
1365setting of the variable mh-redist-full-contents. See its documentation." t)
1366
c26cf6c8 1367(autoload 'mh-reply "mh-comp"
847b8219
KH
1368 "Reply to a MESSAGE (default: displayed message).
1369If optional prefix argument INCLUDEP provided, then include the message
1370in the reply using filter mhl.reply in your MH directory.
1371Prompts for type of addresses to reply to:
1372 from sender only,
1373 to sender and primary recipients,
1374 cc/all sender and all recipients.
1375If the file named by `mh-repl-formfile' exists, it is used as a skeleton
1376for the reply. See also documentation for `\\[mh-send]' function." t)
1377
c26cf6c8 1378(autoload 'mh-send "mh-comp"
847b8219
KH
1379 "Compose and send a letter.
1380The file named by `mh-comp-formfile' will be used as the form.
1381Do not call this function from outside mh-e; use \\[mh-smail] instead.
1382The letter is composed in mh-letter-mode; see its documentation for more
1383details. If `mh-compose-letter-function' is defined, it is called on the
1384draft and passed three arguments: to, subject, and cc." t)
1385
c26cf6c8 1386(autoload 'mh-send-other-window "mh-comp"
847b8219
KH
1387 "Compose and send a letter in another window.
1388Do not call this function from outside mh-e;
1389use \\[mh-smail-other-window] instead.
1390See also documentation for `\\[mh-send]' function." t)
1391
c26cf6c8 1392(autoload 'mh-letter-mode "mh-comp"
847b8219
KH
1393 "Mode for composing letters in mh-e.
1394For more details, type \\[describe-mode] while in MH-Letter mode." t)
c26cf6c8
RS
1395
1396
1397;;; mh-funcs
1398
1399(autoload 'mh-burst-digest "mh-funcs"
847b8219
KH
1400 "Burst apart the current message, which should be a digest.
1401The message is replaced by its table of contents and the messages from the
1402digest are inserted into the folder after that message." t)
1403
c26cf6c8 1404(autoload 'mh-copy-msg "mh-funcs"
847b8219
KH
1405 "Copy to another FOLDER the specified MESSAGE(s) without deleting them.
1406Default is the displayed message. If optional prefix argument is
1407provided, then prompt for the message sequence." t)
1408
c26cf6c8
RS
1409(autoload 'mh-kill-folder "mh-funcs"
1410 "Remove the current folder." t)
847b8219 1411
c26cf6c8
RS
1412(autoload 'mh-list-folders "mh-funcs"
1413 "List mail folders." t)
847b8219 1414
c26cf6c8 1415(autoload 'mh-pack-folder "mh-funcs"
847b8219
KH
1416 "Renumber the messages of a folder to be 1..n.
1417First, offer to execute any outstanding commands for the current folder.
1418If optional prefix argument provided, prompt for the range of messages
1419to display after packing. Otherwise, show the entire folder." t)
1420
c26cf6c8 1421(autoload 'mh-pipe-msg "mh-funcs"
847b8219
KH
1422 "Pipe the current message through the given shell COMMAND.
1423If INCLUDE-HEADERS (prefix argument) is provided, send the entire message.
1424Otherwise just send the message's body without the headers." t)
1425
c26cf6c8
RS
1426(autoload 'mh-page-digest "mh-funcs"
1427 "Advance displayed message to next digested message." t)
847b8219 1428
c26cf6c8
RS
1429(autoload 'mh-page-digest-backwards "mh-funcs"
1430 "Back up displayed message to previous digested message." t)
847b8219 1431
c26cf6c8 1432(autoload 'mh-print-msg "mh-funcs"
847b8219
KH
1433 "Print MESSAGE(s) (default: displayed message) on printer.
1434If optional prefix argument provided, then prompt for the message sequence.
1435The variable mh-lpr-command-format is used to generate the print command.
1436The messages are formatted by mhl. See the variable mhl-formfile." t)
1437
c26cf6c8 1438(autoload 'mh-sort-folder "mh-funcs"
847b8219
KH
1439 "Sort the messages in the current folder by date.
1440Calls the MH program sortm to do the work.
1441The arguments in the list mh-sortm-args are passed to sortm
1442if this function is passed an argument." t)
1443
c26cf6c8
RS
1444(autoload 'mh-undo-folder "mh-funcs"
1445 "Undo all commands in current folder." t)
847b8219 1446
c26cf6c8 1447(autoload 'mh-store-msg "mh-funcs"
86a32538 1448 "Store the file(s) contained in the current message into DIRECTORY.
847b8219
KH
1449The message can contain a shar file or uuencoded file.
1450Default directory is the last directory used, or initially the value of
1451mh-store-default-directory or the current directory." t)
1452
86a32538
RS
1453(autoload 'mh-store-buffer "mh-funcs"
1454 "Store the file(s) contained in the current buffer into DIRECTORY.
847b8219
KH
1455The buffer can contain a shar file or uuencoded file.
1456Default directory is the last directory used, or initially the value of
1457`mh-store-default-directory' or the current directory." t)
c26cf6c8
RS
1458
1459
1460;;; mh-pick
1461
1462(autoload 'mh-search-folder "mh-pick"
847b8219
KH
1463 "Search FOLDER for messages matching a pattern.
1464Add the messages found to the sequence named `search'." t)
c26cf6c8
RS
1465
1466;;; mh-seq
1467
c26cf6c8
RS
1468(autoload 'mh-delete-seq "mh-seq"
1469 "Delete the SEQUENCE." t)
1470(autoload 'mh-list-sequences "mh-seq"
1471 "List the sequences defined in FOLDER." t)
1472(autoload 'mh-msg-is-in-seq "mh-seq"
1473 "Display the sequences that contain MESSAGE (default: displayed message)." t)
1474(autoload 'mh-narrow-to-seq "mh-seq"
847b8219
KH
1475 "Restrict display of this folder to just messages in SEQUENCE
1476Use \\[mh-widen] to undo this command." t)
1477(autoload 'mh-put-msg-in-seq "mh-seq"
1478 "Add MESSAGE(s) (default: displayed message) to SEQUENCE.
1479If optional prefix argument provided, then prompt for the message sequence." t)
c26cf6c8
RS
1480(autoload 'mh-widen "mh-seq"
1481 "Remove restrictions from current folder, thereby showing all messages." t)
1482(autoload 'mh-rename-seq "mh-seq"
847b8219 1483 "Rename SEQUENCE to have NEW-NAME." t)
c26cf6c8
RS
1484
1485;;; mh-e.el ends here