*** empty log message ***
[emacs.git] / lisp / mh-e.el
bloba10df5aa4efaa1ed266a250dfc6da0dd483c488e
1 ;;; mh-e.el --- GNU Emacs interface to the MH mailer
3 ;;; Copyright (C) 1985, 86, 87, 88, 89, 92 Free Software Foundation
5 ;; Author: James Larus <larus@ginger.berkeley.edu>
6 ;; Version: 3.7
7 ;; Keywords: mail
9 (defvar mh-e-RCS-id)
10 (setq mh-e-RCS-id "$Header: /var/home/larus/lib/emacs/RCS/mh-e.el,v 3.1 90/09/28 15:47:58 larus Exp Locker: larus $")
12 ;; GNU Emacs is distributed in the hope that it will be useful,
13 ;; but without any warranty. No author or distributor
14 ;; accepts responsibility to anyone for the consequences of using it
15 ;; or for whether it serves any particular purpose or works at all,
16 ;; unless he says so in writing.
18 ;; Everyone is granted permission to copy, modify and redistribute
19 ;; GNU Emacs, but only under the conditions described in the
20 ;; document "GNU Emacs copying permission notice". An exact copy
21 ;; of the document is supposed to have been given to you along with
22 ;; GNU Emacs so that you can know how you may redistribute it all.
23 ;; It should be in a file named COPYING. Among other things, the
24 ;; copyright notice and this notice must be preserved on all copies.
26 ;;; Commentary:
28 ;;; This file contains mh-e, a GNU Emacs front end to the MH mail system
29 ;;; (specifically, for use with MH.5 and MH.6).
31 ;;; Original version for Gosling emacs by Brian Reid, Stanford, 1982.
32 ;;; Modified by James Larus, BBN, July 1984 and UCB, 1984 & 1985.
33 ;;; Rewritten for GNU Emacs, James Larus 1985. larus@ginger.berkeley.edu
34 ;;; Modified by Stephen Gildea 1988. gildea@bbn.com
36 ;;; NB. MH must have been compiled with the MHE compiler flag or several
37 ;;; features necessary mh-e will be missing from MH commands, specifically
38 ;;; the -build switch to repl and forw.
40 ;;; Code:
44 ;;; Constants:
46 ;;; Set for local environment:
47 ;;;* These are now in paths.el.
48 ;;;(defvar mh-progs "/usr/new/mh/" "Directory containing MH commands.")
49 ;;;(defvar mh-lib "/usr/new/lib/mh/" "Directory of MH library.")
51 (defvar mh-redist-full-contents t
52 "Non-nil if the `dist' command needs whole letter for redistribution.
53 This is the case when `send' is compiled with the BERK option.")
56 ;;; Hooks:
58 (defvar mh-folder-mode-hook nil
59 "Invoked in `mh-folder mode' on a new folder.")
61 (defvar mh-letter-mode-hook nil
62 "Invoked in `mh-letter-mode' on a new letter.")
64 (defvar mh-compose-letter-function nil
65 "Invoked in `mh-compose-and-send-mail' on a draft letter.
66 It is passed three arguments: TO recipients, SUBJECT, and CC recipients.")
68 (defvar mh-before-send-letter-hook nil
69 "Invoked at the beginning of the \\[mh-send-letter] command.")
71 (defvar mh-inc-folder-hook nil
72 "Invoked after incorporating mail into a folder with \\[mh-inc-folder].")
74 (defvar mh-quit-hook nil
75 "Invoked after quitting mh-e with \\[mh-quit].")
78 (defvar mh-ins-string nil
79 "Temporarily set by `mh-insert-prefix' prior to running `mh-yank-hooks'.")
81 (defvar mh-yank-hooks
82 '(lambda ()
83 (save-excursion
84 (goto-char (point))
85 (or (bolp) (forward-line 1))
86 (while (< (point) (mark))
87 (insert mh-ins-string)
88 (forward-line 1))))
89 "Hook to run citation function.
90 Expects POINT and MARK to be set to the region to cite.")
93 ;;; Personal preferences:
95 (defvar mh-clean-message-header nil
96 "*Non-nil means clean headers of messages that are displayed or inserted.
97 The variables `mh-visible-headers' and `mh-invisible-headers' control what
98 is removed.")
100 (defvar mh-visible-headers nil
101 "*If non-nil, contains a regexp specifying the headers to keep when cleaning.
102 Only used if `mh-clean-message-header' is non-nil. Setting this variable
103 overrides `mh-invisible-headers'.")
105 (defvar mhl-formfile nil
106 "*Name of format file to be used by mhl to show messages.
107 A value of T means use the default format file.
108 Nil means don't use mhl to format messages.")
110 (defvar mh-lpr-command-format "lpr -p -J '%s'"
111 "*Format for Unix command that prints a message.
112 The string should be a Unix command line, with the string '%s' where
113 the job's name (folder and message number) should appear. The message text
114 is piped to this command.")
116 (defvar mh-print-background nil
117 "*Print messages in the background if non-nil.
118 WARNING: do not delete the messages until printing is finished;
119 otherwise, your output may be truncated.")
121 (defvar mh-summary-height 4
122 "*Number of lines in summary window.")
124 (defvar mh-recenter-summary-p nil
125 "*Recenter summary window when the show window is toggled off if non-nil.")
127 (defvar mh-ins-buf-prefix ">> "
128 "*String to put before each non-blank line of a yanked or inserted message.
129 Used when the message is inserted in an outgoing letter.")
131 (defvar mh-do-not-confirm nil
132 "*Non-nil means do not prompt for confirmation before some commands.
133 Only affects certain innocuous commands.")
135 (defvar mh-bury-show-buffer t
136 "*Non-nil means that the displayed show buffer for a folder is buried.")
138 (defvar mh-delete-yanked-msg-window nil
139 "*Controls window display when a message is yanked by \\[mh-yank-cur-msg].
140 If non-nil, yanking the current message into a draft letter deletes any
141 windows displaying the message.")
143 (defvar mh-yank-from-start-of-msg t
144 "*Controls which part of a message is yanked by \\[mh-yank-cur-msg].
145 If non-nil, include the entire message. If the symbol `body, then yank the
146 message minus the header. If nil, yank only the portion of the message
147 following the point. If the show buffer has a region, this variable is
148 ignored.")
150 (defvar mh-reply-default-reply-to nil
151 "*Sets the person or persons to whom a reply will be sent.
152 If nil, prompt for recipient. If non-nil, then \\[mh-reply] will use this
153 value and it should be one of \"from\", \"to\", or \"cc\".")
155 (defvar mh-recursive-folders nil
156 "*If non-nil, then commands which operate on folders do so recursively.")
159 ;;; Parameterize mh-e to work with different scan formats. The defaults work
160 ;;; the standard MH scan listings.
162 (defvar mh-cmd-note 4
163 "Offset to insert notation.")
165 (defvar mh-note-repl "-"
166 "String whose first character is used to notate replied to messages.")
168 (defvar mh-note-forw "F"
169 "String whose first character is used to notate forwarded messages.")
171 (defvar mh-note-dist "R"
172 "String whose first character is used to notate redistributed messages.")
174 (defvar mh-good-msg-regexp "^....[^D^]"
175 "Regexp specifiying the scan lines that are 'good' messages.")
177 (defvar mh-deleted-msg-regexp "^....D"
178 "Regexp matching scan lines of deleted messages.")
180 (defvar mh-refiled-msg-regexp "^....\\^"
181 "Regexp matching scan lines of refiled messages.")
183 (defvar mh-valid-scan-line "^ *[0-9]"
184 "Regexp matching scan lines for messages (not error messages).")
186 (defvar mh-msg-number-regexp "^ *\\([0-9]+\\)"
187 "Regexp to find the number of a message in a scan line.
188 The message's number must be surrounded with \\( \\)")
190 (defvar mh-msg-search-regexp "^[^0-9]*%d[^0-9]"
191 "Format string containing a regexp matching the scan listing for a message.
192 The desired message's number will be an argument to format.")
194 (defvar mh-flagged-scan-msg-regexp "^....\\D\\|^....\\^\\|^....\\+\\|^.....%"
195 "Regexp matching flagged scan lines.
196 Matches lines marked as deleted, refiled, in a sequence, or the cur message.")
198 (defvar mh-cur-scan-msg-regexp "^....\\+"
199 "Regexp matching scan line for the cur message.")
201 (defvar mh-show-buffer-mode-line-buffer-id "{%%b} %s/%d"
202 "Format string to produce `mode-line-buffer-id' for show buffers.
203 First argument is folder name. Second is message number.")
205 (defvar mh-partial-folder-mode-line-annotation "select"
206 "Annotation when displaying part of a folder.
207 The string is displayed after the folder's name. NIL for no annotation.")
210 ;;; Real constants:
212 (defvar mh-invisible-headers
213 "^Received: \\|^Message-Id: \\|^Remailed-\\|^Via: \\|^Mail-from: \\|^Return-Path: \\|^In-Reply-To: \\|^Resent-"
214 "Regexp matching lines in a message header that are not to be shown.
215 If `mh-visible-headers' is non-nil, it is used instead to specify what
216 to keep.")
218 (defvar mh-rejected-letter-start "^ ----- Unsent message follows -----$"
219 "Regexp specifying the beginning of the wrapper around a returned letter.
220 This wrapper is generated by the mail system when rejecting a letter.")
222 (defvar mh-to-field-choices '((?t . "To:") (?s . "Subject:") (?c . "Cc:")
223 (?b . "Bcc:") (?f . "Fcc:"))
224 "A-list of (character . field name) strings for mh-to-field.")
227 ;;; Global variables:
229 (defvar mh-user-path ""
230 "User's mail folder.")
232 (defvar mh-last-destination nil
233 "Destination of last refile or write command.")
235 (defvar mh-folder-mode-map (make-keymap)
236 "Keymap for MH folders.")
238 (defvar mh-letter-mode-map (copy-keymap text-mode-map)
239 "Keymap for composing mail.")
241 (defvar mh-pick-mode-map (make-sparse-keymap)
242 "Keymap for searching folder.")
244 (defvar mh-letter-mode-syntax-table nil
245 "Syntax table used while in mh-e letter mode.")
247 (if mh-letter-mode-syntax-table
249 (setq mh-letter-mode-syntax-table
250 (make-syntax-table text-mode-syntax-table))
251 (set-syntax-table mh-letter-mode-syntax-table)
252 (modify-syntax-entry ?% "." mh-letter-mode-syntax-table))
254 (defvar mh-folder-list nil
255 "List of folder names for completion.")
257 (defvar mh-draft-folder nil
258 "Name of folder containing draft messages.
259 NIL means do not use draft folder.")
261 (defvar mh-unseen-seq nil
262 "Name of the unseen sequence.")
264 (defvar mh-previous-window-config nil
265 "Window configuration before mh-e command.")
267 (defvar mh-previous-seq nil
268 "Name of the sequence to which a message was last added.")
270 (defvar mh-signature-file-name "~/.signature"
271 "Name of file containing the user's signature.")
274 ;;; Macros and generic functions:
276 (defmacro mh-push (v l)
277 (list 'setq l (list 'cons v l)))
280 (defmacro mh-when (pred &rest body)
281 (list 'cond (cons pred body)))
284 (defmacro with-mh-folder-updating (save-modification-flag-p &rest body)
285 ;; Format is (with-mh-folder-updating (SAVE-MODIFICATION-FLAG-P) &body BODY).
286 ;; Execute BODY, which can modify the folder buffer without having to
287 ;; worry about file locking or the read-only flag, and return its result.
288 ;; If SAVE-MODIFICATION-FLAG-P is non-nil, the buffer's modification
289 ;; flag is unchanged, otherwise it is cleared.
290 (setq save-modification-flag-p (car save-modification-flag-p)) ; CL style
291 (` (let ((folder-updating-mod-flag (buffer-modified-p)))
292 (prog1
293 (let ((buffer-read-only nil)
294 (buffer-file-name nil)) ; don't let the buffer get locked
295 (,@ body))
296 (, (if save-modification-flag-p
297 '(mh-set-folder-modified-p folder-updating-mod-flag)
298 '(mh-set-folder-modified-p nil)))))))
301 (defun mh-mapc (func list)
302 (while list
303 (funcall func (car list))
304 (setq list (cdr list))))
308 ;;; Entry points:
310 ;;;###autoload
311 (defun mh-rmail (&optional arg)
312 "Inc(orporate) new mail (no arg) or scan a MH mail box (arg given).
313 This front end uses the MH mail system, which uses different conventions
314 from the usual mail system."
315 (interactive "P")
316 (mh-find-path)
317 (if arg
318 (call-interactively 'mh-visit-folder)
319 (mh-inc-folder)))
322 ;;;###autoload
323 (defun mh-smail ()
324 "Compose and send mail with the MH mail system."
325 (interactive)
326 (mh-find-path)
327 (call-interactively 'mh-send))
330 (defun mh-smail-other-window ()
331 "Compose and send mail in other window with the MH mail system."
332 (interactive)
333 (mh-find-path)
334 (call-interactively 'mh-send-other-window))
338 ;;; User executable mh-e commands:
340 (defun mh-burst-digest ()
341 "Burst apart the current message, which should be a digest.
342 The message is replaced by its table of contents and the letters from the
343 digest are inserted into the folder after that message."
344 (interactive)
345 (let ((digest (mh-get-msg-num t)))
346 (mh-process-or-undo-commands mh-current-folder)
347 (mh-set-folder-modified-p t) ; lock folder while bursting
348 (message "Bursting digest...")
349 (mh-exec-cmd "burst" mh-current-folder digest "-inplace")
350 (mh-scan-folder mh-current-folder (format "%d-last" mh-first-msg-num))
351 (message "Bursting digest...done")))
354 (defun mh-copy-msg (prefix-provided msg-or-seq dest)
355 "Copy specified MESSAGE(s) to another FOLDER without deleting them.
356 Default is the displayed message. If optional prefix argument is
357 provided, then prompt for the message sequence."
358 (interactive (list current-prefix-arg
359 (if current-prefix-arg
360 (mh-read-seq-default "Copy" t)
361 (mh-get-msg-num t))
362 (mh-prompt-for-folder "Copy to" "" t)))
363 (mh-exec-cmd "refile" msg-or-seq "-link" "-src" mh-current-folder dest)
364 (if prefix-provided
365 (mh-notate-seq msg-or-seq ?C mh-cmd-note)
366 (mh-notate msg-or-seq ?C mh-cmd-note)))
369 (defun mh-delete-msg (msg-or-seq)
370 "Mark the specified MESSAGE(s) for subsequent deletion and move to the next.
371 Default is the displayed message. If optional prefix argument is
372 given then prompt for the message sequence."
373 (interactive (list (if current-prefix-arg
374 (mh-read-seq-default "Delete" t)
375 (mh-get-msg-num t))))
376 (if (numberp msg-or-seq)
377 (mh-delete-a-msg msg-or-seq)
378 (mh-map-to-seq-msgs 'mh-delete-a-msg msg-or-seq))
379 (mh-next-msg))
382 (defun mh-delete-msg-no-motion (msg-or-seq)
383 "Mark the specified MESSAGE(s) for subsequent deletion.
384 Default is the displayed message. If optional prefix argument is
385 provided, then prompt for the message sequence."
386 (interactive (list (if current-prefix-arg
387 (mh-read-seq-default "Delete" t)
388 (mh-get-msg-num t))))
389 (if (numberp msg-or-seq)
390 (mh-delete-a-msg msg-or-seq)
391 (mh-map-to-seq-msgs 'mh-delete-a-msg msg-or-seq)))
394 (defun mh-delete-msg-from-seq (prefix-provided msg-or-seq &optional from-seq)
395 "Delete MESSAGE (default: displayed message) from SEQUENCE.
396 If optional prefix argument provided, then delete all messages
397 from a sequence."
398 (interactive (let ((argp current-prefix-arg))
399 (list argp
400 (if argp
401 (mh-read-seq-default "Delete" t)
402 (mh-get-msg-num t))
403 (if (not argp)
404 (mh-read-seq-default "Delete from" t)))))
405 (if prefix-provided
406 (mh-remove-seq msg-or-seq)
407 (mh-remove-msg-from-seq msg-or-seq from-seq)))
410 (defun mh-edit-again (msg)
411 "Clean-up a draft or a message previously sent and make it resendable."
412 (interactive (list (mh-get-msg-num t)))
413 (let* ((from-folder mh-current-folder)
414 (config (current-window-configuration))
415 (draft
416 (cond ((and mh-draft-folder (equal from-folder mh-draft-folder))
417 (find-file (mh-msg-filename msg))
418 (rename-buffer (format "draft-%d" msg))
419 (buffer-name))
421 (mh-read-draft "clean-up" (mh-msg-filename msg) nil)))))
422 (mh-clean-msg-header (point-min)
423 "^Date:\\|^Received:\\|^Message-Id:\\|^From:"
424 nil)
425 (goto-char (point-min))
426 (set-buffer-modified-p nil)
427 (mh-compose-and-send-mail draft "" from-folder nil nil nil nil nil nil
428 config)))
431 (defun mh-execute-commands ()
432 "Process outstanding delete and refile requests."
433 (interactive)
434 (if mh-narrowed-to-seq (mh-widen))
435 (mh-process-commands mh-current-folder)
436 (mh-set-scan-mode)
437 (mh-goto-cur-msg) ; after mh-set-scan-mode for efficiency
438 (mh-make-folder-mode-line)
439 t) ; return t for write-file-hooks
442 (defun mh-extract-rejected-mail (msg)
443 "Extract a letter returned by the mail system and make it resendable.
444 Default is the displayed message."
445 (interactive (list (mh-get-msg-num t)))
446 (let ((from-folder mh-current-folder)
447 (config (current-window-configuration))
448 (draft (mh-read-draft "extraction" (mh-msg-filename msg) nil)))
449 (goto-char (point-min))
450 (cond ((re-search-forward mh-rejected-letter-start nil t)
451 (forward-char 1)
452 (delete-region (point-min) (point))
453 (mh-clean-msg-header (point-min)
454 "^Date:\\|^Received:\\|^Message-Id:\\|^From:\\|^Sender:"
455 nil))
457 (message "Does not appear to be a rejected letter.")))
458 (goto-char (point-min))
459 (set-buffer-modified-p nil)
460 (mh-compose-and-send-mail draft "" from-folder msg (mh-get-field "To")
461 (mh-get-field "From") (mh-get-field "cc")
462 nil nil config)))
465 (defun mh-first-msg ()
466 "Move to the first message."
467 (interactive)
468 (goto-char (point-min)))
471 (defun mh-forward (prefix-provided msg-or-seq to cc)
472 "Forward MESSAGE(s) (default: displayed message).
473 If optional prefix argument provided, then prompt for the message sequence."
474 (interactive (list current-prefix-arg
475 (if current-prefix-arg
476 (mh-read-seq-default "Forward" t)
477 (mh-get-msg-num t))
478 (read-string "To: ")
479 (read-string "Cc: ")))
480 (let* ((folder mh-current-folder)
481 (config (current-window-configuration))
482 ;; forw always leaves file in "draft" since it doesn't have -draft
483 (draft-name (expand-file-name "draft" mh-user-path))
484 (draft (cond ((or (not (file-exists-p draft-name))
485 (y-or-n-p "The file 'draft' exists. Discard it? "))
486 (mh-exec-cmd "forw"
487 "-build" mh-current-folder msg-or-seq)
488 (prog1
489 (mh-read-draft "" draft-name t)
490 (mh-insert-fields "To:" to "Cc:" cc)
491 (set-buffer-modified-p nil)))
493 (mh-read-draft "" draft-name nil)))))
494 (goto-char (point-min))
495 (re-search-forward "^------- Forwarded Message")
496 (forward-line -1)
497 (narrow-to-region (point) (point-max))
498 (let* ((subject (save-excursion (mh-get-field "From:")))
499 (trim (string-match "<" subject))
500 (forw-subject (save-excursion (mh-get-field "Subject:"))))
501 (if trim
502 (setq subject (substring subject 0 (1- trim))))
503 (widen)
504 (save-excursion
505 (mh-insert-fields "Subject:" (format "[%s: %s]" subject forw-subject)))
506 (delete-other-windows)
507 (if prefix-provided
508 (mh-add-msgs-to-seq (mh-seq-to-msgs msg-or-seq) 'forwarded t)
509 (mh-add-msgs-to-seq msg-or-seq 'forwarded t))
510 (mh-compose-and-send-mail draft "" folder msg-or-seq
511 to subject cc
512 mh-note-forw "Forwarded:"
513 config))))
516 (defun mh-goto-msg (number &optional no-error-if-no-message dont-show)
517 "Position the cursor at message NUMBER.
518 Non-nil second argument means do not signal an error if message does not exist.
519 Non-nil third argument means not to show the message.
520 Return non-nil if cursor is at message."
521 (interactive "NMessage number? ")
522 (let ((cur-msg (mh-get-msg-num nil))
523 (starting-place (point))
524 (msg-pattern (mh-msg-search-pat number)))
525 (cond ((cond ((and cur-msg (= cur-msg number)) t)
526 ((and cur-msg
527 (< cur-msg number)
528 (re-search-forward msg-pattern nil t)) t)
529 ((and cur-msg
530 (> cur-msg number)
531 (re-search-backward msg-pattern nil t)) t)
532 (t ; Do thorough search of buffer
533 (goto-char (point-max))
534 (re-search-backward msg-pattern nil t)))
535 (beginning-of-line)
536 (if (not dont-show) (mh-maybe-show number))
539 (goto-char starting-place)
540 (if (not no-error-if-no-message)
541 (error "No message %d" number))
542 nil))))
545 (defun mh-inc-folder (&optional maildrop-name)
546 "Inc(orporate) new mail into +inbox.
547 Optional prefix argument specifies an alternate maildrop from the default.
548 If this is given, mail is incorporated into the current folder, rather
549 than +inbox. Run `mh-inc-folder-hook' after incorporating new mail."
550 (interactive (list (if current-prefix-arg
551 (expand-file-name
552 (read-file-name "inc mail from file: "
553 mh-user-path)))))
554 (let ((config (current-window-configuration)))
555 (if (not maildrop-name)
556 (cond ((not (get-buffer "+inbox"))
557 (mh-make-folder "+inbox")
558 (setq mh-previous-window-config config))
559 ((not (eq (current-buffer) (get-buffer "+inbox")))
560 (switch-to-buffer "+inbox")
561 (setq mh-previous-window-config config)))))
562 (mh-get-new-mail maildrop-name)
563 (run-hooks 'mh-inc-folder-hook))
566 (defun mh-kill-folder ()
567 "Remove the current folder."
568 (interactive)
569 (if (or mh-do-not-confirm
570 (yes-or-no-p (format "Remove folder %s? " mh-current-folder)))
571 (let ((folder mh-current-folder))
572 (mh-set-folder-modified-p t) ; lock folder to kill it
573 (mh-exec-cmd-daemon "rmf" folder)
574 (mh-remove-folder-from-folder-list folder)
575 (message "Folder %s removed" folder)
576 (mh-set-folder-modified-p nil) ; so kill-buffer doesn't complain
577 (kill-buffer mh-show-buffer)
578 (kill-buffer folder))
579 (message "Folder not removed")))
582 (defun mh-last-msg ()
583 "Move to the last message."
584 (interactive)
585 (goto-char (point-max))
586 (while (and (not (bobp)) (looking-at "^$"))
587 (forward-line -1)))
590 (defun mh-list-folders ()
591 "List mail folders."
592 (interactive)
593 (with-output-to-temp-buffer " *mh-temp*"
594 (save-excursion
595 (switch-to-buffer " *mh-temp*")
596 (erase-buffer)
597 (message "Listing folders...")
598 (mh-exec-cmd-output "folders" t)
599 (goto-char (point-min))
600 (message "Listing folders...done"))))
603 (defun mh-msg-is-in-seq (msg)
604 "Display the sequences that contain MESSAGE (default: displayed message)."
605 (interactive (list (mh-get-msg-num t)))
606 (message "Message %d is in sequences: %s"
608 (mapconcat 'concat
609 (mh-list-to-string (mh-seq-containing-msg msg))
610 " ")))
613 (defun mh-narrow-to-seq (seq)
614 "Restrict display of this folder to just messages in a sequence.
615 Reads which sequence. Use \\[mh-widen] to undo this command."
616 (interactive (list (mh-read-seq "Narrow to" t)))
617 (let ((eob (point-max)))
618 (with-mh-folder-updating (t)
619 (cond ((mh-seq-to-msgs seq)
620 (mh-copy-seq-to-point seq eob)
621 (narrow-to-region eob (point-max))
622 (mh-make-folder-mode-line (symbol-name seq))
623 (mh-recenter nil)
624 (setq mh-narrowed-to-seq seq))
626 (error "No messages in sequence `%s'" (symbol-name seq)))))))
629 (defun mh-next-undeleted-msg (&optional arg)
630 "Move to next undeleted message in window."
631 (interactive "P")
632 (forward-line (prefix-numeric-value arg))
633 (setq mh-next-direction 'forward)
634 (cond ((re-search-forward mh-good-msg-regexp nil 0 arg)
635 (beginning-of-line)
636 (mh-maybe-show))
638 (forward-line -1)
639 (if (get-buffer mh-show-buffer)
640 (delete-windows-on mh-show-buffer)))))
643 (defun mh-pack-folder (range)
644 "Renumber the messages of a folder to be 1..n.
645 First, offer to execute any outstanding commands for the current folder.
646 If optional prefix argument provided, prompt for the range of messages
647 to display after packing. Otherwise, show the entire folder."
648 (interactive (list (if current-prefix-arg
649 (mh-read-msg-range
650 "Range to scan after packing [all]? ")
651 "all")))
652 (mh-pack-folder-1 range)
653 (mh-goto-cur-msg)
654 (message "Packing folder...done"))
657 (defun mh-pipe-msg (prefix-provided command)
658 "Pipe the current message through the given shell COMMAND.
659 If optional prefix argument is provided, send the entire message.
660 Otherwise just send the message's body."
661 (interactive
662 (list current-prefix-arg (read-string "Shell command on message: ")))
663 (save-excursion
664 (set-buffer mh-show-buffer)
665 (goto-char (point-min))
666 (if (not prefix-provided) (search-forward "\n\n"))
667 (shell-command-on-region (point) (point-max) command nil)))
670 (defun mh-refile-msg (prefix-provided msg-or-seq dest)
671 "Refile MESSAGE(s) (default: displayed message) in FOLDER.
672 If optional prefix argument provided, then prompt for message sequence."
673 (interactive
674 (list current-prefix-arg
675 (if current-prefix-arg
676 (mh-read-seq-default "Refile" t)
677 (mh-get-msg-num t))
678 (intern
679 (mh-prompt-for-folder "Destination"
680 (if (eq 'refile (car mh-last-destination))
681 (symbol-name (cdr mh-last-destination))
683 t))))
684 (setq mh-last-destination (cons 'refile dest))
685 (if prefix-provided
686 (mh-map-to-seq-msgs 'mh-refile-a-msg msg-or-seq dest)
687 (mh-refile-a-msg msg-or-seq dest))
688 (mh-next-msg))
691 (defun mh-refile-or-write-again (msg)
692 "Re-execute the last refile or write command on the given MESSAGE.
693 Default is the displayed message. Use the same folder or file as the
694 previous refile or write command."
695 (interactive (list (mh-get-msg-num t)))
696 (if (null mh-last-destination)
697 (error "No previous refile or write"))
698 (cond ((eq (car mh-last-destination) 'refile)
699 (mh-refile-a-msg msg (cdr mh-last-destination))
700 (message "Destination folder: %s" (cdr mh-last-destination)))
702 (mh-write-msg-to-file msg (cdr mh-last-destination))
703 (message "Destination: %s" (cdr mh-last-destination))))
704 (mh-next-msg))
707 (defun mh-reply (prefix-provided msg)
708 "Reply to a MESSAGE (default: displayed message).
709 If optional prefix argument provided, then include the message in the reply
710 using filter mhl.reply in your MH directory."
711 (interactive (list current-prefix-arg (mh-get-msg-num t)))
712 (let ((minibuffer-help-form
713 "from => Sender only\nto => Sender and primary recipients\ncc or all => Sender and all recipients"))
714 (let ((reply-to (or mh-reply-default-reply-to
715 (completing-read "Reply to whom: "
716 '(("from") ("to") ("cc") ("all"))
718 t)))
719 (folder mh-current-folder)
720 (show-buffer mh-show-buffer)
721 (config (current-window-configuration)))
722 (message "Composing a reply...")
723 (cond ((or (equal reply-to "from") (equal reply-to ""))
724 (apply 'mh-exec-cmd
725 "repl" "-build"
726 "-nodraftfolder" mh-current-folder
728 "-nocc" "all"
729 (if prefix-provided
730 (list "-filter" "mhl.reply"))))
731 ((equal reply-to "to")
732 (apply 'mh-exec-cmd
733 "repl" "-build"
734 "-nodraftfolder" mh-current-folder
736 "-cc" "to"
737 (if prefix-provided
738 (list "-filter" "mhl.reply"))))
739 ((or (equal reply-to "cc") (equal reply-to "all"))
740 (apply 'mh-exec-cmd
741 "repl" "-build"
742 "-nodraftfolder" mh-current-folder
744 "-cc" "all" "-nocc" "me"
745 (if prefix-provided
746 (list "-filter" "mhl.reply")))))
748 (let ((draft (mh-read-draft "reply"
749 (expand-file-name "reply" mh-user-path)
750 t)))
751 (delete-other-windows)
752 (set-buffer-modified-p nil)
754 (let ((to (mh-get-field "To:"))
755 (subject (mh-get-field "Subject:"))
756 (cc (mh-get-field "Cc:")))
757 (goto-char (point-min))
758 (mh-goto-header-end 1)
759 (if (not prefix-provided)
760 (mh-display-msg msg folder))
761 (mh-add-msgs-to-seq msg 'answered t)
762 (message "Composing a reply...done")
763 (mh-compose-and-send-mail draft "" folder msg to subject cc
764 mh-note-repl "Replied:" config))))))
767 (defun mh-quit ()
768 "Restore the previous window configuration, if one exists.
769 Finish by running mh-quit-hook."
770 (interactive)
771 (if mh-previous-window-config
772 (set-window-configuration mh-previous-window-config))
773 (run-hooks 'mh-quit-hook))
776 (defun mh-page-digest ()
777 "Advance displayed message to next digested message."
778 (interactive)
779 (save-excursion
780 (mh-show-message-in-other-window)
781 ;; Go to top of screen (in case user moved point).
782 (move-to-window-line 0)
783 (let ((case-fold-search nil))
784 ;; Search for blank line and then for From:
785 (mh-when (not (and (search-forward "\n\n" nil t)
786 (search-forward "From:" nil t)))
787 (other-window -1)
788 (error "No more messages")))
789 ;; Go back to previous blank line, then forward to the first non-blank.
790 (search-backward "\n\n" nil t)
791 (forward-line 2)
792 (mh-recenter 0)
793 (other-window -1)))
796 (defun mh-page-digest-backwards ()
797 "Back up displayed message to previous digested message."
798 (interactive)
799 (save-excursion
800 (mh-show-message-in-other-window)
801 ;; Go to top of screen (in case user moved point).
802 (move-to-window-line 0)
803 (let ((case-fold-search nil))
804 (beginning-of-line)
805 (mh-when (not (and (search-backward "\n\n" nil t)
806 (search-backward "From:" nil t)))
807 (other-window -1)
808 (error "No more messages")))
809 ;; Go back to previous blank line, then forward to the first non-blank.
810 (search-backward "\n\n" nil t)
811 (forward-line 2)
812 (mh-recenter 0)
813 (other-window -1)))
816 (defun mh-page-msg (&optional arg)
817 "Page the displayed message forwards.
818 Scrolls ARG lines or a full screen if no argument is supplied."
819 (interactive "P")
820 (scroll-other-window arg))
823 (defun mh-previous-page (&optional arg)
824 "Page the displayed message backwards.
825 Scrolls ARG lines or a full screen if no argument is supplied."
826 (interactive "P")
827 (save-excursion
828 (mh-show-message-in-other-window)
829 (unwind-protect
830 (scroll-down arg)
831 (other-window -1))))
834 (defun mh-previous-undeleted-msg (&optional arg)
835 "Move to previous undeleted message in window."
836 (interactive "p")
837 (setq mh-next-direction 'backward)
838 (beginning-of-line)
839 (cond ((re-search-backward mh-good-msg-regexp nil 0 arg)
840 (mh-maybe-show))
842 (if (get-buffer mh-show-buffer)
843 (delete-windows-on mh-show-buffer)))))
846 (defun mh-print-msg (prefix-provided msg-or-seq)
847 "Print MESSAGE(s) (default: displayed message) on a line printer.
848 If optional prefix argument provided, then prompt for the message sequence."
849 (interactive (list current-prefix-arg
850 (if current-prefix-arg
851 (reverse (mh-seq-to-msgs
852 (mh-read-seq-default "Print" t)))
853 (mh-get-msg-num t))))
854 (if prefix-provided
855 (message "Printing sequence...")
856 (message "Printing message..."))
857 (let ((print-command
858 (if prefix-provided
859 (format "(scan -clear %s ; %s -nobell -clear %s %s) | %s"
860 (mapconcat (function (lambda (msg) msg)) msg-or-seq " ")
861 (expand-file-name "mhl" mh-lib)
862 (if (stringp mhl-formfile)
863 (format "-form %s" mhl-formfile)
865 (mh-msg-filenames msg-or-seq)
866 (format mh-lpr-command-format
867 (if prefix-provided
868 (format "Sequence from %s" mh-current-folder)
869 (format "%s/%d" mh-current-folder
870 msg-or-seq))))
871 (format "%s -nobell -clear %s %s | %s"
872 (expand-file-name "mhl" mh-lib)
873 (mh-msg-filename msg-or-seq)
874 (if (stringp mhl-formfile)
875 (format "-form %s" mhl-formfile)
877 (format mh-lpr-command-format
878 (if prefix-provided
879 (format "Sequence from %s" mh-current-folder)
880 (format "%s/%d" mh-current-folder
881 msg-or-seq)))))))
882 (if mh-print-background
883 (mh-exec-cmd-daemon shell-file-name "-c" print-command)
884 (call-process shell-file-name nil nil nil "-c" print-command))
885 (if prefix-provided
886 (mh-notate-seq msg-or-seq ?P mh-cmd-note)
887 (mh-notate msg-or-seq ?P mh-cmd-note))
888 (mh-add-msgs-to-seq msg-or-seq 'printed t)
889 (if prefix-provided
890 (message "Printing sequence...done")
891 (message "Printing message...done"))))
894 (defun mh-put-msg-in-seq (prefix-provided from to)
895 "Add MESSAGE(s) (default: displayed message) to SEQUENCE.
896 If optional prefix argument provided, then prompt for the message sequence."
897 (interactive (list current-prefix-arg
898 (if current-prefix-arg
899 (mh-seq-to-msgs
900 (mh-read-seq-default "Add messages from" t))
901 (mh-get-msg-num t))
902 (mh-read-seq-default "Add to" nil)))
903 (setq mh-previous-seq to)
904 (mh-add-msgs-to-seq from to))
907 (defun mh-rescan-folder (range)
908 "Rescan a folder after optionally processing the outstanding commands.
909 If optional prefix argument is provided, prompt for the range of
910 messages to display. Otherwise show the entire folder."
911 (interactive (list (if current-prefix-arg
912 (mh-read-msg-range "Range to scan [all]? ")
913 nil)))
914 (setq mh-next-direction 'forward)
915 (mh-scan-folder mh-current-folder (or range "all")))
918 (defun mh-redistribute (to cc msg)
919 "Redistribute a letter.
920 Depending on how your copy of MH was compiled, you may need to change the
921 setting of the variable mh-redist-full-contents. See its documentation."
922 (interactive (list (read-string "Redist-To: ")
923 (read-string "Redist-Cc: ")
924 (mh-get-msg-num t)))
925 (save-window-excursion
926 (let ((folder mh-current-folder)
927 (draft (mh-read-draft "redistribution"
928 (if mh-redist-full-contents
929 (mh-msg-filename msg)
930 nil)
931 nil)))
932 (mh-goto-header-end 0)
933 (insert "Resent-To: " to "\n")
934 (if (not (equal cc "")) (insert "Resent-cc: " cc "\n"))
935 (mh-clean-msg-header (point-min)
936 "^Message-Id:\\|^Received:\\|^Return-Path:\\|^Sender:\\|^Date:\\|^From:"
937 nil)
938 (save-buffer)
939 (message "Redistributing...")
940 (if mh-redist-full-contents
941 (call-process "/bin/sh" nil 0 nil "-c"
942 (format "mhdist=1 mhaltmsg=%s %s -push %s"
943 (buffer-file-name)
944 (expand-file-name "send" mh-progs)
945 (buffer-file-name)))
946 (call-process "/bin/sh" nil 0 nil "-c"
947 (format "mhdist=1 mhaltmsg=%s mhannotate=1 %s -push %s"
948 (mh-msg-filename msg folder)
949 (expand-file-name "send" mh-progs)
950 (buffer-file-name))))
951 (mh-annotate-msg msg folder mh-note-dist
952 "-component" "Resent:"
953 "-text" (format "\"%s %s\"" to cc))
954 (kill-buffer draft)
955 (message "Redistributing...done"))))
958 (defun mh-write-msg-to-file (msg file)
959 "Append MESSAGE to the end of a FILE."
960 (interactive
961 (list (mh-get-msg-num t)
962 (let ((default-dir (if (eq 'write (car mh-last-destination))
963 (file-name-directory (cdr mh-last-destination))
964 default-directory)))
965 (read-file-name "Save message in file: " default-dir
966 (expand-file-name "mail.out" default-dir)))))
967 (let ((file-name (mh-msg-filename msg))
968 (output-file (mh-expand-file-name file)))
969 (setq mh-last-destination (cons 'write file))
970 (save-excursion
971 (set-buffer (get-buffer-create " *mh-temp*"))
972 (erase-buffer)
973 (insert-file-contents file-name)
974 (append-to-file (point-min) (point-max) output-file))))
977 (defun mh-search-folder (folder)
978 "Search FOLDER for messages matching a pattern."
979 (interactive (list (mh-prompt-for-folder "Search"
980 mh-current-folder
981 t)))
982 (switch-to-buffer-other-window "pick-pattern")
983 (if (or (zerop (buffer-size))
984 (not (y-or-n-p "Reuse pattern? ")))
985 (mh-make-pick-template)
986 (message ""))
987 (setq mh-searching-folder folder))
990 (defun mh-send (to cc subject)
991 "Compose and send a letter."
992 (interactive "sTo: \nsCc: \nsSubject: ")
993 (let ((config (current-window-configuration)))
994 (delete-other-windows)
995 (mh-send-sub to cc subject config)))
998 (defun mh-send-other-window (to cc subject)
999 "Compose and send a letter in another window.."
1000 (interactive "sTo: \nsCc: \nsSubject: ")
1001 (let ((pop-up-windows t))
1002 (mh-send-sub to cc subject (current-window-configuration))))
1005 (defun mh-send-sub (to cc subject config)
1006 "Do the real work of composing and sending a letter.
1007 Expects the TO, CC, and SUBJECT fields as arguments.
1008 CONFIG is the window configuration before sending mail."
1009 (let ((folder mh-current-folder)
1010 (msg-num (mh-get-msg-num nil)))
1011 (message "Composing a message...")
1012 (let ((draft (mh-read-draft
1013 "message"
1014 (if (file-exists-p
1015 (expand-file-name "components" mh-user-path))
1016 (expand-file-name "components" mh-user-path)
1017 (if (file-exists-p
1018 (expand-file-name "components" mh-lib))
1019 (expand-file-name "components" mh-lib)
1020 (error "Can't find components file")))
1021 nil)))
1022 (mh-insert-fields "To:" to "Subject:" subject "Cc:" cc)
1023 (set-buffer-modified-p nil)
1024 (goto-char (point-max))
1025 (message "Composing a message...done")
1026 (mh-compose-and-send-mail draft "" folder msg-num
1027 to subject cc
1028 nil nil config))))
1031 (defun mh-show (&optional msg)
1032 "Show MESSAGE (default: displayed message).
1033 Forces a two-window display with the folder window on top (size
1034 mh-summary-height) and the show buffer below it."
1035 (interactive)
1036 (if (not msg)
1037 (setq msg (mh-get-msg-num t)))
1038 (setq mh-showing t)
1039 (mh-set-mode-name "mh-e show")
1040 (if (not (eql (next-window (minibuffer-window)) (selected-window)))
1041 (delete-other-windows)) ; force ourself to the top window
1042 (let ((folder mh-current-folder))
1043 (mh-show-message-in-other-window)
1044 (mh-display-msg msg folder))
1045 (other-window -1)
1046 (shrink-window (- (window-height) mh-summary-height))
1047 (mh-recenter nil)
1048 (if (not (memq msg mh-seen-list)) (mh-push msg mh-seen-list)))
1051 (defun mh-sort-folder ()
1052 "Sort the messages in the current folder by date."
1053 (interactive "")
1054 (mh-process-or-undo-commands mh-current-folder)
1055 (setq mh-next-direction 'forward)
1056 (mh-set-folder-modified-p t) ; lock folder while sorting
1057 (message "Sorting folder...")
1058 (mh-exec-cmd "sortm" mh-current-folder)
1059 (message "Sorting folder...done")
1060 (mh-scan-folder mh-current-folder "all"))
1063 (defun mh-toggle-showing ()
1064 "Toggle the scanning mode/showing mode of displaying messages."
1065 (interactive)
1066 (if mh-showing
1067 (mh-set-scan-mode)
1068 (mh-show)))
1071 (defun mh-undo (prefix-provided msg-or-seq)
1072 "Undo the deletion or refile of the specified MESSAGE(s).
1073 Default is the displayed message. If optional prefix argument is
1074 provided, then prompt for the message sequence."
1075 (interactive (list current-prefix-arg
1076 (if current-prefix-arg
1077 (mh-read-seq-default "Undo" t)
1078 (mh-get-msg-num t))))
1080 (cond (prefix-provided
1081 (mh-mapc (function mh-undo-msg) (mh-seq-to-msgs msg-or-seq)))
1082 ((or (looking-at mh-deleted-msg-regexp)
1083 (looking-at mh-refiled-msg-regexp))
1084 (mh-undo-msg (mh-get-msg-num t)))
1086 (error "Nothing to undo")))
1087 ;; update the mh-refile-list so mh-outstanding-commands-p will work
1088 (mh-mapc (function
1089 (lambda (elt)
1090 (if (not (mh-seq-to-msgs elt))
1091 (setq mh-refile-list (delq elt mh-refile-list)))))
1092 mh-refile-list)
1094 (if (not (mh-outstanding-commands-p))
1095 (mh-set-folder-modified-p nil)))
1098 (defun mh-undo-msg (msg)
1099 ;; Undo the deletion or refile of one MESSAGE.
1100 (cond ((memq msg mh-delete-list)
1101 (setq mh-delete-list (delq msg mh-delete-list))
1102 (mh-remove-msg-from-seq msg 'deleted t)
1103 (mh-notate msg ? mh-cmd-note))
1105 (mh-mapc (function (lambda (dest)
1106 (mh-remove-msg-from-seq msg dest t)))
1107 mh-refile-list)
1108 (mh-notate msg ? mh-cmd-note))))
1111 (defun mh-undo-folder (&rest ignore)
1112 "Undo all commands in current folder."
1113 (interactive "")
1114 (cond ((or mh-do-not-confirm
1115 (yes-or-no-p "Undo all commands in folder? "))
1116 (setq mh-delete-list nil
1117 mh-refile-list nil
1118 mh-seq-list nil
1119 mh-next-direction 'forward)
1120 (with-mh-folder-updating (nil)
1121 (mh-unmark-all-headers t)))
1123 (message "Commands not undone.")
1124 (sit-for 2))))
1127 (defun mh-visit-folder (folder &optional range)
1128 "Visit FOLDER and display RANGE of messages."
1129 (interactive (list (mh-prompt-for-folder "Visit" "+inbox" t)
1130 (mh-read-msg-range "Range [all]? ")))
1131 (let ((config (current-window-configuration)))
1132 (mh-scan-folder folder (or range "all"))
1133 (setq mh-previous-window-config config)))
1136 (defun mh-widen ()
1137 "Remove restrictions from the current folder, thereby showing all messages."
1138 (interactive "")
1139 (with-mh-folder-updating (t)
1140 (delete-region (point-min) (point-max))
1141 (widen)
1142 (mh-make-folder-mode-line))
1143 (setq mh-narrowed-to-seq nil))
1147 ;;; Support routines.
1149 (defun mh-delete-a-msg (msg)
1150 ;; Delete the MESSAGE.
1151 (save-excursion
1152 (mh-goto-msg msg nil t)
1153 (if (looking-at mh-refiled-msg-regexp)
1154 (error "Message %d is refiled. Undo refile before deleting." msg))
1155 (if (looking-at mh-deleted-msg-regexp)
1157 (mh-set-folder-modified-p t)
1158 (mh-push msg mh-delete-list)
1159 (mh-add-msgs-to-seq msg 'deleted t)
1160 (mh-notate msg ?D mh-cmd-note))))
1163 (defun mh-refile-a-msg (msg destination)
1164 ;; Refile MESSAGE in FOLDER.
1165 (save-excursion
1166 (mh-goto-msg msg nil t)
1167 (cond ((looking-at mh-deleted-msg-regexp)
1168 (error "Message %d is deleted. Undo delete before moving." msg))
1169 ((looking-at mh-refiled-msg-regexp)
1170 (if (y-or-n-p
1171 (format "Message %d already refiled. Copy to %s as well? "
1172 msg destination))
1173 (mh-exec-cmd "refile" (mh-get-msg-num t) "-link"
1174 "-src" mh-current-folder
1175 (symbol-name destination))
1176 (message "Message not copied.")))
1178 (mh-set-folder-modified-p t)
1179 (if (not (memq destination mh-refile-list))
1180 (mh-push destination mh-refile-list))
1181 (if (not (memq msg (mh-seq-to-msgs destination)))
1182 (mh-add-msgs-to-seq msg destination t))
1183 (mh-notate msg ?^ mh-cmd-note)))))
1186 (defun mh-display-msg (msg-num folder)
1187 ;; Display message NUMBER of FOLDER.
1188 (set-buffer folder)
1189 ;; Bind variables in folder buffer in case they are local
1190 (let ((formfile mhl-formfile)
1191 (clean-message-header mh-clean-message-header)
1192 (invisible-headers mh-invisible-headers)
1193 (visible-headers mh-visible-headers)
1194 (msg-filename (mh-msg-filename msg-num))
1195 (show-buffer mh-show-buffer)
1196 (folder mh-current-folder))
1197 (if (not (file-exists-p msg-filename))
1198 (error "Message %d does not exist" msg-num))
1199 (switch-to-buffer show-buffer)
1200 (if mh-bury-show-buffer (bury-buffer (current-buffer)))
1201 (mh-when (not (equal msg-filename buffer-file-name))
1202 ;; Buffer does not yet contain message.
1203 (clear-visited-file-modtime)
1204 (unlock-buffer)
1205 (setq buffer-file-name nil) ; no locking during setup
1206 (erase-buffer)
1207 (if formfile
1208 (if (stringp formfile)
1209 (mh-exec-lib-cmd-output "mhl" "-nobell" "-noclear"
1210 "-form" formfile msg-filename)
1211 (mh-exec-lib-cmd-output "mhl" "-nobell" "-noclear"
1212 msg-filename))
1213 (insert-file-contents msg-filename))
1214 (goto-char (point-min))
1215 (cond (clean-message-header
1216 (mh-clean-msg-header (point-min)
1217 invisible-headers
1218 visible-headers)
1219 (goto-char (point-min)))
1221 (let ((case-fold-search t))
1222 (re-search-forward
1223 "^To:\\|^From:\\|^Subject:\\|^Date:" nil t)
1224 (beginning-of-line)
1225 (mh-recenter 0))))
1226 (set-buffer-modified-p nil)
1227 (setq buffer-file-name msg-filename)
1228 (set-mark nil)
1229 (setq mode-line-buffer-identification
1230 (list (format mh-show-buffer-mode-line-buffer-id
1231 folder msg-num))))))
1234 (defun mh-invalidate-show-buffer ()
1235 ;; Invalidate the show buffer so we must update it to use it.
1236 (if (get-buffer mh-show-buffer)
1237 (save-excursion
1238 (set-buffer mh-show-buffer)
1239 (setq buffer-file-name nil))))
1242 (defun mh-show-message-in-other-window ()
1243 (switch-to-buffer-other-window mh-show-buffer)
1244 (if mh-bury-show-buffer (bury-buffer (current-buffer))))
1247 (defun mh-clean-msg-header (start invisible-headers visible-headers)
1248 ;; Flush extraneous lines in a message header, from the given POINT to the
1249 ;; end of the message header. If VISIBLE-HEADERS is non-nil, it contains a
1250 ;; regular expression specifying the lines to display, otherwise
1251 ;; INVISIBLE-HEADERS contains a regular expression specifying lines to
1252 ;; delete from the header.
1253 (let ((case-fold-search t))
1254 (save-restriction
1255 (goto-char start)
1256 (if (search-forward "\n\n" nil t)
1257 (backward-char 2))
1258 (narrow-to-region start (point))
1259 (goto-char (point-min))
1260 (if visible-headers
1261 (while (< (point) (point-max))
1262 (beginning-of-line)
1263 (cond ((looking-at visible-headers)
1264 (forward-line 1)
1265 (while (looking-at "^[ \t]+") (forward-line 1)))
1267 (mh-delete-line 1)
1268 (while (looking-at "^[ \t]+")
1269 (beginning-of-line)
1270 (mh-delete-line 1)))))
1271 (while (re-search-forward invisible-headers nil t)
1272 (beginning-of-line)
1273 (mh-delete-line 1)
1274 (while (looking-at "^[ \t]+")
1275 (beginning-of-line)
1276 (mh-delete-line 1))))
1277 (unlock-buffer))))
1280 (defun mh-delete-line (lines)
1281 ;; Delete version of kill-line.
1282 (delete-region (point) (save-excursion (forward-line lines) (point))))
1285 (defun mh-read-draft (use initial-contents delete-contents-file)
1286 ;; Read draft file into a draft buffer and make that buffer the current one.
1287 ;; USE is a message used for prompting about the intended use of the message.
1288 ;; INITIAL-CONTENTS is filename that is read into an empty buffer, or NIL
1289 ;; if buffer should not be modified. Delete the initial-contents file if
1290 ;; DELETE-CONTENTS-FILE flag is set.
1291 ;; Returns the draft folder's name.
1292 ;; If the draft folder facility is enabled in ~/.mh_profile, a new buffer is
1293 ;; used each time and saved in the draft folder. The draft file can then be
1294 ;; reused.
1295 (cond (mh-draft-folder
1296 (let ((orig-default-dir default-directory))
1297 (pop-to-buffer (find-file-noselect (mh-new-draft-name) t))
1298 (rename-buffer (format "draft-%s" (buffer-name)))
1299 (setq default-directory orig-default-dir)))
1301 (let ((draft-name (expand-file-name "draft" mh-user-path)))
1302 (pop-to-buffer "draft") ; Create if necessary
1303 (if (buffer-modified-p)
1304 (if (y-or-n-p "Draft has been modified; kill anyway? ")
1305 (set-buffer-modified-p nil)
1306 (error "Draft preserved")))
1307 (setq buffer-file-name draft-name)
1308 (clear-visited-file-modtime)
1309 (unlock-buffer)
1310 (mh-when (and (file-exists-p draft-name)
1311 (not (equal draft-name initial-contents)))
1312 (insert-file-contents draft-name)
1313 (delete-file draft-name)))))
1314 (mh-when (and initial-contents
1315 (or (zerop (buffer-size))
1316 (not (y-or-n-p
1317 (format "A draft exists. Use for %s? " use)))))
1318 (erase-buffer)
1319 (insert-file-contents initial-contents)
1320 (if delete-contents-file (delete-file initial-contents)))
1321 (auto-save-mode 1)
1322 (if mh-draft-folder
1323 (save-buffer)) ; Do not reuse draft name
1324 (buffer-name))
1327 (defun mh-new-draft-name ()
1328 ;; Returns the pathname of folder for draft messages.
1329 (save-excursion
1330 (set-buffer (get-buffer-create " *mh-temp*"))
1331 (erase-buffer)
1332 (mh-exec-cmd-output "mhpath" nil mh-draft-folder "new")
1333 (buffer-substring (point) (1- (mark)))))
1336 (defun mh-next-msg ()
1337 ;; Move backward or forward to the next undeleted message in the buffer.
1338 (if (eq mh-next-direction 'forward)
1339 (mh-next-undeleted-msg 1)
1340 (mh-previous-undeleted-msg 1)))
1343 (defun mh-set-scan-mode ()
1344 ;; Display the scan listing buffer, but do not show a message.
1345 (if (get-buffer mh-show-buffer)
1346 (delete-windows-on mh-show-buffer))
1347 (mh-set-mode-name "mh-e scan")
1348 (setq mh-showing nil)
1349 (if mh-recenter-summary-p
1350 (mh-recenter nil)))
1353 (defun mh-maybe-show (&optional msg)
1354 ;; If in showing mode, then display the message pointed to by the cursor.
1355 (if mh-showing (mh-show msg)))
1358 (defun mh-set-mode-name (mode-name-string)
1359 ;; Set the mode-name and ensure that the mode line is updated.
1360 (setq mode-name mode-name-string)
1361 ;; Force redisplay of all buffers' mode lines to be considered.
1362 (save-excursion (set-buffer (other-buffer)))
1363 (set-buffer-modified-p (buffer-modified-p)))
1367 ;;; The folder data abstraction.
1369 (defvar mh-current-folder nil "Name of current folder, a string.")
1370 (defvar mh-show-buffer nil "Buffer that displays mesage for this folder.")
1371 (defvar mh-folder-filename nil "Full path of directory for this folder.")
1372 (defvar mh-showing nil "If non-nil, show the message in a separate window.")
1373 (defvar mh-next-seq-num nil "Index of free sequence id.")
1374 (defvar mh-delete-list nil "List of msg numbers to delete.")
1375 (defvar mh-refile-list nil "List of folder names in mh-seq-list.")
1376 (defvar mh-seq-list nil "Alist of (seq . msgs) numbers.")
1377 (defvar mh-seen-list nil "List of displayed messages.")
1378 (defvar mh-next-direction 'forward "Direction to move to next message.")
1379 (defvar mh-narrowed-to-seq nil "Sequence display is narrowed to.")
1380 (defvar mh-first-msg-num nil "Number of first msg in buffer.")
1381 (defvar mh-last-msg-num nil "Number of last msg in buffer.")
1384 (defun mh-make-folder (name)
1385 ;; Create and initialize a new mail folder called NAME and make it the
1386 ;; current folder.
1387 (switch-to-buffer name)
1388 (setq buffer-read-only nil)
1389 (erase-buffer)
1390 (setq buffer-read-only t)
1391 (mh-folder-mode)
1392 (mh-set-folder-modified-p nil)
1393 (setq buffer-file-name mh-folder-filename)
1394 (mh-set-mode-name "mh-e scan"))
1397 ;;; Don't use this mode when creating buffers if default-major-mode is nil.
1398 (put 'mh-folder-mode 'mode-class 'special)
1400 (defun mh-folder-mode ()
1401 "Major mode for \"editing\" an MH folder scan listing.
1402 Messages can be marked for refiling and deletion. However, both actions
1403 are deferred until you request execution with \\[mh-execute-commands].
1404 \\{mh-folder-mode-map}
1405 A prefix argument (\\[universal-argument]) to delete, refile, list, or undo
1406 applies the action to a message sequence.
1408 Variables controlling mh-e operation are (defaults in parentheses):
1410 mh-bury-show-buffer (t)
1411 Non-nil means that the buffer used to display message is buried.
1412 It will never be offered as the default other buffer.
1414 mh-clean-message-header (nil)
1415 Non-nil means remove header lines matching the regular expression
1416 specified in mh-invisible-headers from messages.
1418 mh-visible-headers (nil)
1419 If non-nil, it contains a regexp specifying the headers that are shown in
1420 a message if mh-clean-message-header is non-nil. Setting this variable
1421 overrides mh-invisible-headers.
1423 mh-do-not-confirm (nil)
1424 Non-nil means do not prompt for confirmation before executing some
1425 non-recoverable commands such as mh-kill-folder and mh-undo-folder.
1427 mhl-formfile (nil)
1428 Name of format file to be used by mhl to show messages.
1429 A value of T means use the default format file.
1430 Nil means don't use mhl to format messages.
1432 mh-lpr-command-format (\"lpr -p -J '%s'\")
1433 Format for command used to print a message on a system printer.
1435 mh-recenter-summary-p (nil)
1436 If non-nil, then the scan listing is recentered when the window displaying
1437 a messages is toggled off.
1439 mh-summary-height (4)
1440 Number of lines in the summary window.
1442 mh-ins-buf-prefix (\">> \")
1443 String to insert before each non-blank line of a message as it is
1444 inserted in a draft letter.
1446 The value of mh-folder-mode-hook is called when a new folder is set up."
1448 (kill-all-local-variables)
1449 (use-local-map mh-folder-mode-map)
1450 (setq major-mode 'mh-folder-mode)
1451 (mh-set-mode-name "mh-e folder")
1452 (make-local-vars
1453 'mh-current-folder (buffer-name) ; Name of folder, a string
1454 'mh-show-buffer (format "show-%s" (buffer-name)) ; Buffer that displays msgs
1455 'mh-folder-filename ; e.g. "/usr/foobar/Mail/inbox/"
1456 (file-name-as-directory (mh-expand-file-name (buffer-name)))
1457 'mh-showing nil ; Show message also?
1458 'mh-next-seq-num 0 ; Index of free sequence id
1459 'mh-delete-list nil ; List of msgs nums to delete
1460 'mh-refile-list nil ; List of folder names in mh-seq-list
1461 'mh-seq-list nil ; Alist of (seq . msgs) nums
1462 'mh-seen-list nil ; List of displayed messages
1463 'mh-next-direction 'forward ; Direction to move to next message
1464 'mh-narrowed-to-seq nil ; Sequence display is narrowed to
1465 'mh-first-msg-num nil ; Number of first msg in buffer
1466 'mh-last-msg-num nil ; Number of last msg in buffer
1467 'mh-previous-window-config nil) ; Previous window configuration
1468 (auto-save-mode -1)
1469 (setq buffer-offer-save t)
1470 (make-local-variable 'write-file-hooks)
1471 (setq write-file-hooks '(mh-execute-commands))
1472 (make-local-variable 'revert-buffer-function)
1473 (setq revert-buffer-function 'mh-undo-folder)
1474 (run-hooks 'mh-folder-mode-hook))
1477 (defun make-local-vars (&rest pairs)
1478 ;; Take VARIABLE-VALUE pairs and makes local variables initialized to the
1479 ;; value.
1480 (while pairs
1481 (make-variable-buffer-local (car pairs))
1482 (set (car pairs) (car (cdr pairs)))
1483 (setq pairs (cdr (cdr pairs)))))
1486 (defun mh-scan-folder (folder range)
1487 ;; Scan the FOLDER over the RANGE. Return in the folder's buffer.
1488 (cond ((null (get-buffer folder))
1489 (mh-make-folder folder))
1491 (mh-process-or-undo-commands folder)
1492 (switch-to-buffer folder)))
1493 (mh-regenerate-headers range)
1494 (mh-when (zerop (buffer-size))
1495 (if (equal range "all")
1496 (message "Folder %s is empty" folder)
1497 (message "No messages in %s, range %s" folder range))
1498 (sit-for 5))
1499 (mh-goto-cur-msg))
1502 (defun mh-regenerate-headers (range)
1503 ;; Replace buffer with scan of its contents over range RANGE.
1504 (let ((folder mh-current-folder))
1505 (message "Scanning %s..." folder)
1506 (with-mh-folder-updating (nil)
1507 (erase-buffer)
1508 (mh-exec-cmd-output "scan" nil
1509 "-noclear" "-noheader"
1510 "-width" (window-width)
1511 folder range)
1512 (goto-char (point-min))
1513 (cond ((looking-at "scan: no messages in")
1514 (keep-lines mh-valid-scan-line)) ; Flush random scan lines
1515 ((looking-at "scan: ")) ; Keep error messages
1517 (keep-lines mh-valid-scan-line))) ; Flush random scan lines
1518 (mh-delete-seq-locally 'cur) ; To pick up new one
1519 (setq mh-seq-list (mh-read-folder-sequences folder nil))
1520 (mh-notate-user-sequences)
1521 (mh-make-folder-mode-line (if (equal range "all")
1523 mh-partial-folder-mode-line-annotation)))
1524 (message "Scanning %s...done" folder)))
1527 (defun mh-get-new-mail (maildrop-name)
1528 ;; Read new mail from a maildrop into the current buffer.
1529 ;; Return T if there was new mail, NIL otherwise. Return in the current
1530 ;; buffer.
1531 (let ((point-before-inc (point))
1532 (folder mh-current-folder)
1533 (return-value t))
1534 (with-mh-folder-updating (t)
1535 (message (if maildrop-name
1536 (format "inc %s -file %s..." folder maildrop-name)
1537 (format "inc %s..." folder)))
1538 (mh-unmark-all-headers nil)
1539 (setq mh-next-direction 'forward)
1540 (goto-char (point-max))
1541 (let ((start-of-inc (point)))
1542 (if maildrop-name
1543 (mh-exec-cmd-output "inc" nil folder
1544 "-file" (expand-file-name maildrop-name)
1545 "-width" (window-width)
1546 "-truncate")
1547 (mh-exec-cmd-output "inc" nil
1548 "-width" (window-width)))
1549 (message
1550 (if maildrop-name
1551 (format "inc %s -file %s...done" folder maildrop-name)
1552 (format "inc %s...done" folder)))
1553 (goto-char start-of-inc)
1554 (cond ((looking-at "inc: no mail")
1555 (keep-lines mh-valid-scan-line) ; Flush random scan lines
1556 (goto-char point-before-inc)
1557 (message "No new mail%s%s" (if maildrop-name " in " "")
1558 (if maildrop-name maildrop-name "")))
1559 ((re-search-forward "^inc:" nil t) ; Error messages
1560 (error "inc error"))
1562 (mh-delete-seq-locally 'cur) ; To pick up new one
1563 (setq mh-seq-list (mh-read-folder-sequences folder t))
1564 (mh-notate-user-sequences)
1565 (keep-lines mh-valid-scan-line)
1566 (mh-make-folder-mode-line)
1567 (mh-goto-cur-msg)
1568 (setq return-value t))))
1569 return-value)))
1572 (defun mh-make-folder-mode-line (&optional annotation)
1573 ;; Set the fields of the mode line for a folder buffer.
1574 ;; The optional ANNOTATION string is displayed after the folder's name.
1575 (save-excursion
1576 (mh-first-msg)
1577 (setq mh-first-msg-num (mh-get-msg-num nil))
1578 (mh-last-msg)
1579 (setq mh-last-msg-num (mh-get-msg-num nil))
1580 (let ((lines (count-lines (point-min) (point-max))))
1581 (setq mode-line-buffer-identification
1582 (list (format "{%%b%s} %d msg%s"
1583 (if annotation (format "/%s" annotation) "")
1584 lines
1585 (if (zerop lines)
1587 (if (> lines 1)
1588 (format "s (%d-%d)" mh-first-msg-num
1589 mh-last-msg-num)
1590 (format " (%d)" mh-first-msg-num)))))))))
1593 (defun mh-unmark-all-headers (remove-all-flags)
1594 ;; Remove all '+' flags from the headers, and if called with a non-nil
1595 ;; argument, remove all 'D', '^' and '%' flags too.
1596 ;; Optimized for speed (i.e., no regular expressions).
1597 (save-excursion
1598 (let ((case-fold-search nil)
1599 (last-line (- (point-max) mh-cmd-note))
1600 char)
1601 (mh-first-msg)
1602 (while (<= (point) last-line)
1603 (forward-char mh-cmd-note)
1604 (setq char (following-char))
1605 (if (or (and remove-all-flags
1606 (or (eql char ?D)
1607 (eql char ?^)
1608 (eql char ?%)))
1609 (eql char ?+))
1610 (progn
1611 (delete-char 1)
1612 (insert " ")))
1613 (forward-line)))))
1616 (defun mh-goto-cur-msg ()
1617 ;; Position the cursor at the current message.
1618 (let ((cur-msg (car (mh-seq-to-msgs 'cur))))
1619 (cond ((and cur-msg
1620 (mh-goto-msg cur-msg t nil))
1621 (mh-notate nil ?+ mh-cmd-note)
1622 (mh-recenter 0)
1623 (mh-maybe-show cur-msg))
1625 (mh-last-msg)
1626 (message "No current message")))))
1629 (defun mh-pack-folder-1 (range)
1630 ;; Close and pack the current folder.
1631 (mh-process-or-undo-commands mh-current-folder)
1632 (message "Packing folder...")
1633 (mh-set-folder-modified-p t) ; lock folder while packing
1634 (save-excursion
1635 (mh-exec-cmd-quiet " *mh-temp*" "folder" mh-current-folder "-pack"))
1636 (mh-regenerate-headers range))
1639 (defun mh-process-or-undo-commands (folder)
1640 ;; If FOLDER has outstanding commands, then either process or discard them.
1641 (set-buffer folder)
1642 (if (mh-outstanding-commands-p)
1643 (if (or mh-do-not-confirm
1644 (y-or-n-p
1645 "Process outstanding deletes and refiles (or lose them)? "))
1646 (mh-process-commands folder)
1647 (mh-undo-folder))
1648 (mh-invalidate-show-buffer)))
1651 (defun mh-process-commands (folder)
1652 ;; Process outstanding commands for the folder FOLDER.
1653 (message "Processing deletes and refiles for %s..." folder)
1654 (set-buffer folder)
1655 (with-mh-folder-updating (nil)
1656 ;; Update the unseen sequence if it exists
1657 (if (and mh-seen-list (mh-seq-to-msgs mh-unseen-seq))
1658 (mh-undefine-sequence mh-unseen-seq mh-seen-list))
1660 ;; Then refile messages
1661 (mh-mapc
1662 (function
1663 (lambda (dest)
1664 (let ((msgs (mh-seq-to-msgs dest)))
1665 (mh-when msgs
1666 (apply 'mh-exec-cmd "refile"
1667 "-src" folder (symbol-name dest) msgs)
1668 (mh-delete-scan-msgs msgs)))))
1669 mh-refile-list)
1671 ;; Now delete messages
1672 (mh-when mh-delete-list
1673 (apply 'mh-exec-cmd "rmm" folder mh-delete-list)
1674 (mh-delete-scan-msgs mh-delete-list))
1676 ;; Don't need to remove sequences since delete and refile do so.
1678 ;; Mark cur message
1679 (if (> (buffer-size) 0)
1680 (mh-define-sequence 'cur (list (or (mh-get-msg-num nil) "last"))))
1682 (mh-invalidate-show-buffer)
1684 (setq mh-delete-list nil
1685 mh-refile-list nil
1686 mh-seq-list (mh-read-folder-sequences mh-current-folder nil)
1687 mh-seen-list nil)
1688 (mh-unmark-all-headers t)
1689 (mh-notate-user-sequences)
1690 (message "Processing deletes and refiles for %s...done" folder)))
1693 (defun mh-delete-scan-msgs (msgs)
1694 ;; Delete the scan listing lines for each of the msgs in the LIST.
1695 ;; Optimized for speed (i.e., no regular expressions).
1696 (setq msgs (sort msgs (function <))) ;okay to clobber msgs
1697 (save-excursion
1698 (mh-first-msg)
1699 (while (and msgs (< (point) (point-max)))
1700 (cond ((= (mh-get-msg-num nil) (car msgs))
1701 (delete-region (point) (save-excursion (forward-line) (point)))
1702 (setq msgs (cdr msgs)))
1704 (forward-line))))))
1707 (defun mh-set-folder-modified-p (flag)
1708 "Mark current folder as modified or unmodified according to FLAG."
1709 (set-buffer-modified-p flag))
1712 (defun mh-outstanding-commands-p ()
1713 ;; Returns non-nil if there are outstanding deletes or refiles.
1714 (or mh-delete-list mh-refile-list))
1718 ;;; Mode for composing and sending a draft message.
1720 (defvar mh-sent-from-folder nil
1721 "Folder of msg associated with this letter.")
1723 (defvar mh-sent-from-msg nil
1724 "Number of msg associated with this letter.")
1726 (defvar mh-send-args nil
1727 "Extra arguments to pass to \"send\" command.")
1729 (defvar mh-annotate-char nil
1730 "Character to use to annotate mh-sent-from-msg.")
1732 (defvar mh-annotate-field nil
1733 "Field name for message annotation.")
1735 (defun mh-letter-mode ()
1736 "Mode for composing letters in mh-e.
1737 When you have finished composing, type \\[mh-send-letter] to send the letter.
1739 Variables controlling this mode (defaults in parentheses):
1741 mh-delete-yanked-msg-window (nil)
1742 If non-nil, \\[mh-yank-cur-msg] will delete any windows displaying
1743 the yanked message.
1745 mh-yank-from-start-of-msg (t)
1746 If non-nil, \\[mh-yank-cur-msg] will include the entire message.
1747 If `body', just yank the body (no header).
1748 If nil, only the portion of the message following the point will be yanked.
1749 If there is a region, this variable is ignored.
1751 Upon invoking mh-letter-mode, text-mode-hook and mh-letter-mode-hook are
1752 invoked with no args, if those values are non-nil.
1754 \\{mh-letter-mode-map}"
1755 (interactive)
1756 (kill-all-local-variables)
1757 (make-local-variable 'paragraph-start)
1758 (setq paragraph-start (concat "^[ \t]*[-_][-_][-_]+$\\|" paragraph-start))
1759 (make-local-variable 'paragraph-separate)
1760 (setq paragraph-separate
1761 (concat "^[ \t]*[-_][-_][-_]+$\\|" paragraph-separate))
1762 (make-local-variable 'mh-send-args)
1763 (make-local-variable 'mh-annotate-char)
1764 (make-local-variable 'mh-annotate-field)
1765 (make-local-variable 'mh-previous-window-config)
1766 (make-local-variable 'mh-sent-from-folder)
1767 (make-local-variable 'mh-sent-from-msg)
1768 (use-local-map mh-letter-mode-map)
1769 (setq major-mode 'mh-letter-mode)
1770 (mh-set-mode-name "mh-e letter")
1771 (set-syntax-table mh-letter-mode-syntax-table)
1772 (run-hooks 'text-mode-hook 'mh-letter-mode-hook)
1773 (mh-when auto-fill-function
1774 (make-local-variable 'auto-fill-function)
1775 (setq auto-fill-function 'mh-auto-fill-for-letter)))
1778 (defun mh-auto-fill-for-letter ()
1779 ;; Auto-fill in letters treats the header specially by inserting a tab
1780 ;; before continuation line.
1781 (do-auto-fill)
1782 (if (mh-in-header-p)
1783 (save-excursion
1784 (beginning-of-line nil)
1785 (insert-char ?\t 1))))
1788 (defun mh-in-header-p ()
1789 ;; Return non-nil if the point is in the header of a draft message.
1790 (save-excursion
1791 (let ((cur-point (point)))
1792 (goto-char (point-min))
1793 (re-search-forward "^--------" nil t)
1794 (< cur-point (point)))))
1797 (defun mh-to-field ()
1798 "Move point to the end of a specified header field.
1799 The field is indicated by the previous keystroke. Create the field if
1800 it does not exist. Set the mark to point before moving."
1801 (interactive "")
1802 (expand-abbrev)
1803 (let ((target (cdr (assoc (logior last-input-char ?`) mh-to-field-choices)))
1804 (case-fold-search t))
1805 (cond ((mh-position-on-field target t)
1806 (let ((eol (point)))
1807 (skip-chars-backward " \t")
1808 (delete-region (point) eol))
1809 (if (save-excursion
1810 (backward-char 1)
1811 (not (looking-at "[:,]")))
1812 (insert ", ")
1813 (insert " ")))
1815 (goto-char (point-min))
1816 (re-search-forward "^To:")
1817 (forward-line 1)
1818 (while (looking-at "^[ \t]") (forward-line 1))
1819 (insert (format "%s \n" target))
1820 (backward-char 1)))))
1823 (defun mh-to-fcc ()
1824 "Insert an Fcc: field in the current message.
1825 Prompt for the field name with a completion list of the current folders."
1826 (interactive)
1827 (let ((last-input-char ?\C-f)
1828 (folder (mh-prompt-for-folder "Fcc" "" t)))
1829 (expand-abbrev)
1830 (save-excursion
1831 (mh-to-field)
1832 (insert (substring folder 1 nil)))))
1835 (defun mh-insert-signature ()
1836 "Insert the file ~/.signature at the current point."
1837 (interactive "")
1838 (insert-file-contents mh-signature-file-name)
1839 (set-buffer-modified-p (buffer-modified-p))) ; force mode line update
1842 (defun mh-check-whom ()
1843 "Verify recipients of the current letter."
1844 (interactive)
1845 (let ((file-name (buffer-file-name)))
1846 (set-buffer-modified-p t) ; Force writing of contents
1847 (save-buffer)
1848 (message "Checking recipients...")
1849 (switch-to-buffer-other-window "*Mail Recipients*")
1850 (bury-buffer (current-buffer))
1851 (erase-buffer)
1852 (mh-exec-cmd-output "whom" t file-name)
1853 (other-window -1)
1854 (message "Checking recipients...done")))
1858 ;;; Routines to make a search pattern and search for a message.
1860 (defvar mh-searching-folder nil "Folder this pick is searching.")
1863 (defun mh-make-pick-template ()
1864 ;; Initialize the current buffer with a template for a pick pattern.
1865 (erase-buffer)
1866 (kill-all-local-variables)
1867 (make-local-variable 'mh-searching-folder)
1868 (insert "From: \n"
1869 "To: \n"
1870 "Cc: \n"
1871 "Date: \n"
1872 "Subject: \n"
1873 "---------\n")
1874 (mh-letter-mode)
1875 (use-local-map mh-pick-mode-map)
1876 (goto-char (point-min))
1877 (end-of-line))
1880 (defun mh-do-pick-search ()
1881 "Find messages that match the qualifications in the current pattern buffer.
1882 Messages are searched for in the folder named in mh-searching-folder.
1883 Put messages found in a sequence named `search'."
1884 (interactive)
1885 (let ((pattern-buffer (buffer-name))
1886 (searching-buffer mh-searching-folder)
1887 (range)
1888 (pattern nil)
1889 (new-buffer nil))
1890 (save-excursion
1891 (cond ((get-buffer searching-buffer)
1892 (set-buffer searching-buffer)
1893 (setq range (format "%d-%d" mh-first-msg-num mh-last-msg-num)))
1895 (mh-make-folder searching-buffer)
1896 (setq range "all")
1897 (setq new-buffer t))))
1898 (message "Searching...")
1899 (goto-char (point-min))
1900 (while (setq pattern (mh-next-pick-field pattern-buffer))
1901 (setq msgs (mh-seq-from-command searching-buffer
1902 'search
1903 (nconc (cons "pick" pattern)
1904 (list searching-buffer
1905 range
1906 "-sequence" "search"
1907 "-list"))))
1908 (setq range "search"))
1909 (message "Searching...done")
1910 (if new-buffer
1911 (mh-scan-folder searching-buffer msgs)
1912 (switch-to-buffer searching-buffer))
1913 (delete-other-windows)
1914 (mh-notate-seq 'search ?% (1+ mh-cmd-note))))
1917 (defun mh-next-pick-field (buffer)
1918 ;; Return the next piece of a pick argument that can be extracted from the
1919 ;; BUFFER. Returns nil if no pieces remain.
1920 (set-buffer buffer)
1921 (let ((case-fold-search t))
1922 (cond ((eobp)
1923 nil)
1924 ((re-search-forward "^\\([a-z].*\\):[ \t]*\\([a-z0-9].*\\)$" nil t)
1925 (let* ((component
1926 (format "--%s"
1927 (downcase (buffer-substring (match-beginning 1)
1928 (match-end 1)))))
1929 (pat (buffer-substring (match-beginning 2) (match-end 2))))
1930 (forward-line 1)
1931 (list component pat)))
1932 ((re-search-forward "^-*$" nil t)
1933 (forward-char 1)
1934 (let ((body (buffer-substring (point) (point-max))))
1935 (if (and (> (length body) 0) (not (equal body "\n")))
1936 (list "-search" body)
1937 nil)))
1939 nil))))
1943 ;;; Routines to compose and send a letter.
1945 (defun mh-compose-and-send-mail (draft send-args
1946 sent-from-folder sent-from-msg
1947 to subject cc
1948 annotate-char annotate-field
1949 config)
1950 ;; Edit and compose a draft message in buffer DRAFT and send or save it.
1951 ;; SENT-FROM-FOLDER is buffer containing scan listing of current folder, or
1952 ;; nil if none exists.
1953 ;; SENT-FROM-MSG is the message number or sequence name or nil.
1954 ;; SEND-ARGS is an optional argument passed to the send command.
1955 ;; The TO, SUBJECT, and CC fields are passed to the
1956 ;; mh-compose-letter-function.
1957 ;; If ANNOTATE-CHAR is non-null, it is used to notate the scan listing of the
1958 ;; message. In that case, the ANNOTATE-FIELD is used to build a string
1959 ;; for mh-annotate-msg.
1960 ;; CONFIG is the window configuration to restore after sending the letter.
1961 (pop-to-buffer draft)
1962 (mh-letter-mode)
1963 (setq mh-sent-from-folder sent-from-folder)
1964 (setq mh-sent-from-msg sent-from-msg)
1965 (setq mh-send-args send-args)
1966 (setq mh-annotate-char annotate-char)
1967 (setq mh-annotate-field annotate-field)
1968 (setq mh-previous-window-config config)
1969 (setq mode-line-buffer-identification (list "{%b}"))
1970 (if (and (boundp 'mh-compose-letter-function)
1971 (symbol-value 'mh-compose-letter-function))
1972 ;; run-hooks will not pass arguments.
1973 (let ((value (symbol-value 'mh-compose-letter-function)))
1974 (if (and (listp value) (not (eq (car value) 'lambda)))
1975 (while value
1976 (funcall (car value) to subject cc)
1977 (setq value (cdr value)))
1978 (funcall mh-compose-letter-function to subject cc)))))
1981 (defun mh-send-letter (&optional arg)
1982 "Send the draft letter in the current buffer.
1983 If optional prefix argument is provided, monitor delivery.
1984 Run mh-before-send-letter-hook before doing anything."
1985 (interactive "P")
1986 (run-hooks 'mh-before-send-letter-hook)
1987 (set-buffer-modified-p t) ; Make sure buffer is written
1988 (save-buffer)
1989 (message "Sending...")
1990 (let ((draft-buffer (current-buffer))
1991 (file-name (buffer-file-name))
1992 (config mh-previous-window-config))
1993 (cond (arg
1994 (pop-to-buffer "MH mail delivery")
1995 (erase-buffer)
1996 (if mh-send-args
1997 (mh-exec-cmd-output "send" t "-watch" "-nopush"
1998 "-nodraftfolder" mh-send-args file-name)
1999 (mh-exec-cmd-output "send" t "-watch" "-nopush"
2000 "-nodraftfolder" file-name))
2001 (goto-char (point-max))
2002 (recenter -1)
2003 (set-buffer draft-buffer)) ; for annotation below
2004 (mh-send-args
2005 (mh-exec-cmd-daemon "send" "-nodraftfolder" "-noverbose"
2006 mh-send-args file-name))
2008 (mh-exec-cmd-daemon "send" "-nodraftfolder" "-noverbose"
2009 file-name)))
2011 (if mh-annotate-char
2012 (mh-annotate-msg mh-sent-from-msg
2013 mh-sent-from-folder
2014 mh-annotate-char
2015 "-component" mh-annotate-field
2016 "-text" (format "\"%s %s\""
2017 (mh-get-field "To:")
2018 (mh-get-field "Cc:"))))
2020 (mh-when (or (not arg)
2021 (y-or-n-p "Kill draft buffer? "))
2022 (kill-buffer draft-buffer)
2023 (if config
2024 (set-window-configuration config)))
2025 (message "Sending...done")))
2028 (defun mh-insert-letter (prefix-provided folder msg)
2029 "Insert a message from any folder into the current letter.
2030 Removes the message's headers using mh-invisible-headers.
2031 Prefixes each non-blank line with mh-ins-buf-prefix (default \">> \").
2032 If optional prefix argument provided, do not indent and do not delete
2033 headers. Leaves the mark before the letter and point after it."
2034 (interactive
2035 (list current-prefix-arg
2036 (mh-prompt-for-folder "Message from" mh-sent-from-folder nil)
2037 (read-input (format "Message number%s: "
2038 (if mh-sent-from-msg
2039 (format " [%d]" mh-sent-from-msg)
2040 "")))))
2041 (save-restriction
2042 (narrow-to-region (point) (point))
2043 (let ((start (point-min)))
2044 (if (equal msg "") (setq msg (int-to-string mh-sent-from-msg)))
2045 (mh-exec-lib-cmd-output "mhl" "-nobell" "-noclear"
2046 (expand-file-name msg
2047 (mh-expand-file-name folder)))
2048 (mh-when (not prefix-provided)
2049 (mh-clean-msg-header start mh-invisible-headers mh-visible-headers)
2050 (set-mark start) ; since mh-clean-msg-header moves it
2051 (mh-insert-prefix-string mh-ins-buf-prefix)))))
2054 (defun mh-yank-cur-msg ()
2055 "Insert the current message into the draft buffer.
2056 Prefix each non-blank line in the message with the string in
2057 `mh-ins-buf-prefix'. If a region is set in the message's buffer, then
2058 only the region will be inserted. Otherwise, the entire message will
2059 be inserted if `mh-yank-from-start-of-msg' is non-nil. If this variable
2060 is nil, the portion of the message following the point will be yanked.
2061 If `mh-delete-yanked-msg-window' is non-nil, any window displaying the
2062 yanked message will be deleted."
2063 (interactive)
2064 (if (and mh-sent-from-folder mh-sent-from-msg)
2065 (let ((to-point (point))
2066 (to-buffer (current-buffer)))
2067 (set-buffer mh-sent-from-folder)
2068 (if mh-delete-yanked-msg-window
2069 (delete-windows-on mh-show-buffer))
2070 (set-buffer mh-show-buffer) ; Find displayed message
2071 (let ((mh-ins-str (cond ((mark)
2072 (buffer-substring (point) (mark)))
2073 ((eq 'body mh-yank-from-start-of-msg)
2074 (buffer-substring
2075 (save-excursion
2076 (goto-char (point-min))
2077 (mh-goto-header-end 1)
2078 (point))
2079 (point-max)))
2080 (mh-yank-from-start-of-msg
2081 (buffer-substring (point-min) (point-max)))
2083 (buffer-substring (point) (point-max))))))
2084 (set-buffer to-buffer)
2085 (narrow-to-region to-point to-point)
2086 (push-mark)
2087 (insert mh-ins-str)
2088 (mh-insert-prefix-string mh-ins-buf-prefix)
2089 (insert "\n")
2090 (widen)))
2091 (error "There is no current message")))
2094 (defun mh-insert-prefix-string (mh-ins-string)
2095 ;; Run MH-YANK-HOOK to insert a prefix string before each line in the buffer.
2096 ;; Generality for supercite users.
2097 (save-excursion
2098 (set-mark (point-max))
2099 (goto-char (point-min))
2100 (run-hooks 'mh-yank-hooks)))
2103 (defun mh-fully-kill-draft ()
2104 "Kill the draft message file and the draft message buffer.
2105 Use \\[kill-buffer] if you don't want to delete the draft message file."
2106 (interactive "")
2107 (if (y-or-n-p "Kill draft message? ")
2108 (let ((config mh-previous-window-config))
2109 (if (file-exists-p (buffer-file-name))
2110 (delete-file (buffer-file-name)))
2111 (set-buffer-modified-p nil)
2112 (kill-buffer (buffer-name))
2113 (message "")
2114 (if config
2115 (set-window-configuration config)))
2116 (error "Message not killed")))
2119 (defun mh-recenter (arg)
2120 ;; Like recenter but with two improvements: nil arg means recenter,
2121 ;; and only does anything if the current buffer is in the selected
2122 ;; window. (Commands like save-some-buffers can make this false.)
2123 (if (eql (get-buffer-window (current-buffer))
2124 (selected-window))
2125 (recenter (if arg arg '(t)))))
2129 ;;; Commands to manipulate sequences. Sequences are stored in an alist
2130 ;;; of the form:
2131 ;;; ((seq-name msgs ...) (seq-name msgs ...) ...)
2133 (defun mh-make-seq (name msgs) (cons name msgs))
2135 (defmacro mh-seq-name (pair) (list 'car pair))
2137 (defmacro mh-seq-msgs (pair) (list 'cdr pair))
2139 (defun mh-find-seq (name) (assoc name mh-seq-list))
2142 (defun mh-seq-to-msgs (seq)
2143 "Return a list of the messages in SEQUENCE."
2144 (mh-seq-msgs (mh-find-seq seq)))
2147 (defun mh-seq-containing-msg (msg)
2148 ;; Return a list of the sequences containing MESSAGE.
2149 (let ((l mh-seq-list)
2150 (seqs ()))
2151 (while l
2152 (if (memq msg (mh-seq-msgs (car l)))
2153 (mh-push (mh-seq-name (car l)) seqs))
2154 (setq l (cdr l)))
2155 seqs))
2158 (defun mh-msg-to-seq (msg)
2159 ;; Given a MESSAGE number, return the first sequence in which it occurs.
2160 (car (mh-seq-containing-msg msg)))
2163 (defun mh-read-seq-default (prompt not-empty)
2164 ;; Read and return sequence name with default narrowed or previous sequence.
2165 (mh-read-seq prompt not-empty (or mh-narrowed-to-seq mh-previous-seq)))
2168 (defun mh-read-seq (prompt not-empty &optional default)
2169 ;; Read and return a sequence name. Prompt with PROMPT, raise an error
2170 ;; if the sequence is empty and the NOT-EMPTY flag is non-nil, and supply
2171 ;; an optional DEFAULT sequence.
2172 ;; A reply of '%' defaults to the first sequence containing the current
2173 ;; message.
2174 (let* ((input (completing-read (format "%s %s %s" prompt "sequence:"
2175 (if default
2176 (format "[%s] " default)
2177 ""))
2178 (mh-seq-names mh-seq-list)))
2179 (seq (cond ((equal input "%") (mh-msg-to-seq (mh-get-msg-num t)))
2180 ((equal input "") default)
2181 (t (intern input))))
2182 (msgs (mh-seq-to-msgs seq)))
2183 (if (and (null msgs) not-empty)
2184 (error (format "No messages in sequence `%s'" seq)))
2185 seq))
2188 (defun mh-read-folder-sequences (folder define-sequences)
2189 ;; Read and return the predefined sequences for a FOLDER. If
2190 ;; DEFINE-SEQUENCES is non-nil, then define mh-e's sequences before
2191 ;; reading MH's sequences.
2192 (let ((seqs ()))
2193 (mh-when define-sequences
2194 (mh-define-sequences mh-seq-list)
2195 (mh-mapc (function (lambda (seq) ; Save the internal sequences
2196 (if (mh-folder-name-p (mh-seq-name seq))
2197 (mh-push seq seqs))))
2198 mh-seq-list))
2199 (save-excursion
2200 (mh-exec-cmd-quiet " *mh-temp*" "mark" folder "-list")
2201 (goto-char (point-min))
2202 (while (re-search-forward "^[^:]+" nil t)
2203 (mh-push (mh-make-seq (intern (buffer-substring (match-beginning 0)
2204 (match-end 0)))
2205 (mh-read-msg-list))
2206 seqs))
2207 (delete-region (point-min) (point))) ; avoid race with mh-process-daemon
2208 seqs))
2211 (defun mh-seq-names (seq-list)
2212 ;; Return an alist containing the names of the SEQUENCES.
2213 (mapcar (function (lambda (entry) (list (symbol-name (mh-seq-name entry)))))
2214 seq-list))
2217 (defun mh-seq-from-command (folder seq seq-command)
2218 ;; In FOLDER, make a sequence named SEQ by executing COMMAND.
2219 ;; COMMAND is a list. The first element is a program name
2220 ;; and the subsequent elements are its arguments, all strings.
2221 (let ((msg)
2222 (msgs ())
2223 (case-fold-search t))
2224 (save-excursion
2225 (save-window-excursion
2226 (apply 'mh-exec-cmd-quiet " *mh-temp*" seq-command)
2227 (goto-char (point-min))
2228 (while (setq msg (car (mh-read-msg-list)))
2229 (mh-push msg msgs)
2230 (forward-line 1)))
2231 (set-buffer folder)
2232 (setq msgs (nreverse msgs)) ; Put in ascending order
2233 (mh-push (mh-make-seq seq msgs) mh-seq-list)
2234 msgs)))
2237 (defun mh-read-msg-list ()
2238 ;; Return a list of message numbers from the current point to the end of
2239 ;; the line.
2240 (let ((msgs ())
2241 (end-of-line (save-excursion (end-of-line) (point)))
2242 num)
2243 (while (re-search-forward "[0-9]+" end-of-line t)
2244 (setq num (string-to-int (buffer-substring (match-beginning 0)
2245 (match-end 0))))
2246 (cond ((looking-at "-") ; Message range
2247 (forward-char 1)
2248 (re-search-forward "[0-9]+" end-of-line t)
2249 (let ((num2 (string-to-int (buffer-substring (match-beginning 0)
2250 (match-end 0)))))
2251 (if (< num2 num)
2252 (error "Bad message range: %d-%d" num num2))
2253 (while (<= num num2)
2254 (mh-push num msgs)
2255 (setq num (1+ num)))))
2256 ((not (zerop num)) (mh-push num msgs))))
2257 msgs))
2260 (defun mh-remove-seq (seq)
2261 ;; Delete the SEQUENCE.
2262 (mh-map-to-seq-msgs 'mh-notate-if-in-one-seq seq ? (1+ mh-cmd-note) seq)
2263 (mh-undefine-sequence seq (list "all"))
2264 (mh-delete-seq-locally seq))
2267 (defun mh-delete-seq-locally (seq)
2268 ;; Remove mh-e's record of SEQUENCE.
2269 (let ((entry (mh-find-seq seq)))
2270 (setq mh-seq-list (delq entry mh-seq-list))))
2273 (defun mh-remove-msg-from-seq (msg seq &optional internal-flag)
2274 ;; Remove MESSAGE from the SEQUENCE. If optional FLAG is non-nil, do not
2275 ;; inform MH of the change.
2276 (let ((entry (mh-find-seq seq)))
2277 (mh-when entry
2278 (mh-notate-if-in-one-seq msg ? (1+ mh-cmd-note) (mh-seq-name entry))
2279 (if (not internal-flag)
2280 (mh-undefine-sequence seq (list msg)))
2281 (setcdr entry (delq msg (mh-seq-msgs entry))))))
2284 (defun mh-add-msgs-to-seq (msgs seq &optional internal-flag)
2285 ;; Add MESSAGE(s) to the SEQUENCE. If optional FLAG is non-nil, do not mark
2286 ;; the message in the scan listing or inform MH of the addition.
2287 (let ((entry (mh-find-seq seq)))
2288 (if (and msgs (atom msgs)) (setq msgs (list msgs)))
2289 (if (null entry)
2290 (mh-push (mh-make-seq seq msgs) mh-seq-list)
2291 (if msgs (setcdr entry (append msgs (cdr entry)))))
2292 (mh-when (not internal-flag)
2293 (mh-add-to-sequence seq msgs)
2294 (mh-notate-seq seq ?% (1+ mh-cmd-note)))))
2297 (defun mh-rename-seq (seq new-name)
2298 "Rename a SEQUENCE to have a new NAME."
2299 (interactive "SOld sequence name: \nSNew name: ")
2300 (let ((old-seq (mh-find-seq seq)))
2301 (if old-seq
2302 (rplaca old-seq new-name)
2303 (error "Sequence %s does not exists" seq))
2304 (mh-undefine-sequence seq (mh-seq-msgs old-seq))
2305 (mh-define-sequence new-name (mh-seq-msgs old-seq))))
2308 (defun mh-notate-user-sequences ()
2309 ;; Mark the scan listing of all messages in user-defined sequences.
2310 (let ((seqs mh-seq-list)
2311 name)
2312 (while seqs
2313 (setq name (mh-seq-name (car seqs)))
2314 (if (not (mh-internal-seq name))
2315 (mh-notate-seq name ?% (1+ mh-cmd-note)))
2316 (setq seqs (cdr seqs)))))
2319 (defun mh-internal-seq (name)
2320 ;; Return non-NIL if NAME is the name of an internal mh-e sequence.
2321 (or (memq name '(answered cur deleted forwarded printed))
2322 (eq name mh-unseen-seq)
2323 (mh-folder-name-p name)))
2326 (defun mh-folder-name-p (name)
2327 ;; Return non-NIL if NAME is possibly the name of a folder.
2328 ;; A name can be a folder name if it begins with "+".
2329 (if (symbolp name)
2330 (eql (aref (symbol-name name) 0) ?+)
2331 (eql (aref name 0) ?+)))
2334 (defun mh-notate-seq (seq notation offset)
2335 ;; Mark the scan listing of all messages in the SEQUENCE with the CHARACTER
2336 ;; at the given OFFSET from the beginning of the listing line.
2337 (mh-map-to-seq-msgs 'mh-notate seq notation offset))
2340 (defun mh-notate-if-in-one-seq (msg notation offset seq)
2341 ;; If the MESSAGE is in only the SEQUENCE, then mark the scan listing of the
2342 ;; message with the CHARACTER at the given OFFSET from the beginning of the
2343 ;; listing line.
2344 (let ((in-seqs (mh-seq-containing-msg msg)))
2345 (if (and (eq seq (car in-seqs)) (null (cdr in-seqs)))
2346 (mh-notate msg notation offset))))
2349 (defun mh-map-to-seq-msgs (func seq &rest args)
2350 ;; Invoke the FUNCTION at each message in the SEQUENCE, passing the
2351 ;; remaining ARGS as arguments.
2352 (save-excursion
2353 (let ((msgs (mh-seq-to-msgs seq)))
2354 (while msgs
2355 (if (mh-goto-msg (car msgs) t t)
2356 (apply func (car msgs) args))
2357 (setq msgs (cdr msgs))))))
2360 (defun mh-map-over-seqs (func seq-list)
2361 ;; Apply the FUNCTION to each element in the list of SEQUENCES,
2362 ;; passing the sequence name and the list of messages as arguments.
2363 (while seq-list
2364 (funcall func (mh-seq-name (car seq-list)) (mh-seq-msgs (car seq-list)))
2365 (setq seq-list (cdr seq-list))))
2368 (defun mh-define-sequences (seq-list)
2369 ;; Define the sequences in SEQ-LIST.
2370 (mh-map-over-seqs 'mh-define-sequence seq-list))
2373 (defun mh-add-to-sequence (seq msgs)
2374 ;; Add to a SEQUENCE each message the list of MSGS.
2375 (if (not (mh-folder-name-p seq))
2376 (if msgs
2377 (apply 'mh-exec-cmd "mark" mh-current-folder
2378 "-sequence" (symbol-name seq)
2379 "-add" msgs))))
2382 (defun mh-define-sequence (seq msgs)
2383 ;; Define the SEQUENCE to contain the list of MSGS. Do not mark
2384 ;; pseudo-sequences or empty sequences.
2385 (if (and msgs
2386 (not (mh-folder-name-p seq)))
2387 (save-excursion
2388 (apply 'mh-exec-cmd "mark" mh-current-folder
2389 "-sequence" (symbol-name seq)
2390 "-add" "-zero" (mh-list-to-string msgs)))))
2393 (defun mh-undefine-sequence (seq msgs)
2394 ;; Remove from the SEQUENCE the list of MSGS.
2395 (apply 'mh-exec-cmd "mark" mh-current-folder
2396 "-sequence" (symbol-name seq)
2397 "-delete" msgs))
2400 (defun mh-copy-seq-to-point (seq location)
2401 ;; Copy the scan listing of the messages in SEQUENCE to after the point
2402 ;; LOCATION in the current buffer.
2403 (mh-map-to-seq-msgs 'mh-copy-line-to-point seq location))
2406 (defun mh-copy-line-to-point (msg location)
2407 ;; Copy the current line to the LOCATION in the current buffer.
2408 (beginning-of-line)
2409 (let ((beginning-of-line (point)))
2410 (forward-line 1)
2411 (copy-region-as-kill beginning-of-line (point))
2412 (goto-char location)
2413 (yank)
2414 (goto-char beginning-of-line)))
2418 ;;; Issue commands to MH.
2420 (defun mh-exec-cmd (command &rest args)
2421 ;; Execute MH command COMMAND with ARGS.
2422 ;; Any output is assumed to be an error and is shown to the user.
2423 (save-excursion
2424 (set-buffer " *mh-temp*")
2425 (erase-buffer)
2426 (apply 'call-process
2427 (expand-file-name command mh-progs) nil t nil
2428 (mh-list-to-string args))
2429 (if (> (buffer-size) 0)
2430 (save-window-excursion
2431 (switch-to-buffer-other-window " *mh-temp*")
2432 (sit-for 5)))))
2435 (defun mh-exec-cmd-quiet (buffer command &rest args)
2436 ;; In BUFFER, execute MH command COMMAND with ARGS.
2437 ;; ARGS is a list of strings. Return in BUFFER, if one exists.
2438 (mh-when (stringp buffer)
2439 (set-buffer buffer)
2440 (erase-buffer))
2441 (apply 'call-process
2442 (expand-file-name command mh-progs) nil buffer nil
2443 args))
2446 (defun mh-exec-cmd-output (command display &rest args)
2447 ;; Execute MH command COMMAND with DISPLAY flag and ARGS putting the output
2448 ;; into buffer after point. Set mark after inserted text.
2449 (push-mark (point) t)
2450 (apply 'call-process
2451 (expand-file-name command mh-progs) nil t display
2452 (mh-list-to-string args))
2453 (exchange-point-and-mark))
2456 (defun mh-exec-cmd-daemon (command &rest args)
2457 ;; Execute MH command COMMAND with ARGS. Any output from command is
2458 ;; displayed in an asynchronous pop-up window.
2459 (save-excursion
2460 (set-buffer (get-buffer-create " *mh-temp*"))
2461 (erase-buffer))
2462 (let ((process (apply 'start-process
2463 command nil
2464 (expand-file-name command mh-progs)
2465 (mh-list-to-string args))))
2466 (set-process-filter process 'mh-process-daemon)))
2469 (defun mh-process-daemon (process output)
2470 ;; Process daemon that puts output into a temporary buffer.
2471 (set-buffer (get-buffer-create " *mh-temp*"))
2472 (insert-before-markers output)
2473 (display-buffer " *mh-temp*"))
2476 (defun mh-exec-lib-cmd-output (command &rest args)
2477 ;; Execute MH library command COMMAND with ARGS.
2478 ;; Put the output into buffer after point. Set mark after inserted text.
2479 (push-mark (point) t)
2480 (apply 'call-process
2481 (expand-file-name command mh-lib) nil t nil
2482 (mh-list-to-string args))
2483 (exchange-point-and-mark))
2486 (defun mh-list-to-string (l)
2487 ;; Flattens the list L and makes every element of the new list into a string.
2488 (let ((new-list nil))
2489 (while l
2490 (cond ((null (car l)))
2491 ((symbolp (car l)) (mh-push (symbol-name (car l)) new-list))
2492 ((numberp (car l)) (mh-push (int-to-string (car l)) new-list))
2493 ((equal (car l) ""))
2494 ((stringp (car l)) (mh-push (car l) new-list))
2495 ((listp (car l))
2496 (setq new-list (nconc (nreverse (mh-list-to-string (car l)))
2497 new-list)))
2498 (t (error "Bad element in mh-list-to-string: %s" (car l))))
2499 (setq l (cdr l)))
2500 (nreverse new-list)))
2504 ;;; Commands to annotate a message.
2506 (defun mh-annotate-msg (msg buffer note &rest args)
2507 ;; Mark the MESSAGE in BUFFER listing with the character NOTE and annotate
2508 ;; the saved message with ARGS.
2509 (apply 'mh-exec-cmd "anno" buffer msg args)
2510 (save-excursion
2511 (cond ((get-buffer buffer) ; Buffer may be deleted
2512 (set-buffer buffer)
2513 (if (symbolp msg)
2514 (mh-notate-seq msg note (1+ mh-cmd-note))
2515 (mh-notate msg note (1+ mh-cmd-note)))))))
2518 (defun mh-notate (msg notation offset)
2519 ;; Marks MESSAGE with the character NOTATION at position OFFSET.
2520 ;; Null MESSAGE means the message that the cursor points to.
2521 (save-excursion
2522 (if (or (null msg)
2523 (mh-goto-msg msg t t))
2524 (with-mh-folder-updating (t)
2525 (beginning-of-line)
2526 (forward-char offset)
2527 (delete-char 1)
2528 (insert notation)))))
2532 ;;; User prompting commands.
2534 (defun mh-prompt-for-folder (prompt default can-create)
2535 ;; Prompt for a folder name with PROMPT. Returns the folder's name.
2536 ;; DEFAULT is used if the folder exists and the user types return.
2537 ;; If the CAN-CREATE flag is t, then a non-existant folder is made.
2538 (let* ((prompt (format "%s folder%s" prompt
2539 (if (equal "" default)
2540 "? "
2541 (format " [%s]? " default))))
2542 name)
2543 (if (null mh-folder-list)
2544 (setq mh-folder-list (mh-make-folder-list)))
2545 (while (and (setq name (completing-read prompt mh-folder-list
2546 nil nil "+"))
2547 (equal name "")
2548 (equal default "")))
2549 (cond ((or (equal name "") (equal name "+"))
2550 (setq name default))
2551 ((not (mh-folder-name-p name))
2552 (setq name (format "+%s" name))))
2553 (let ((new-file-p (not (file-exists-p (mh-expand-file-name name)))))
2554 (cond ((and new-file-p
2555 (y-or-n-p
2556 (format "Folder %s does not exist. Create it? " name)))
2557 (message "Creating %s" name)
2558 (call-process "mkdir" nil nil nil (mh-expand-file-name name))
2559 (message "Creating %s...done" name)
2560 (mh-push (list name) mh-folder-list)
2561 (mh-push (list (substring name 1 nil)) mh-folder-list))
2562 (new-file-p
2563 (error "Folder %s is not created" name))
2565 (mh-when (null (assoc name mh-folder-list))
2566 (mh-push (list name) mh-folder-list)
2567 (mh-push (list (substring name 1 nil)) mh-folder-list)))))
2568 name))
2571 (defun mh-make-folder-list ()
2572 "Return a list of the user's folders.
2573 Result is in a form suitable for completing read."
2574 (interactive)
2575 (message "Collecting folder names...")
2576 (save-window-excursion
2577 (mh-exec-cmd-quiet " *mh-temp*" "folders" "-fast"
2578 (if mh-recursive-folders
2579 "-recurse"
2580 "-norecurse"))
2581 (goto-char (point-min))
2582 (let ((list nil)
2583 start)
2584 (while (not (eobp))
2585 (setq start (point))
2586 (forward-line 1)
2587 (mh-push (list (format "+%s" (buffer-substring start (1- (point)))))
2588 list))
2589 (message "Collecting folder names...done")
2590 list)))
2593 (defun mh-remove-folder-from-folder-list (folder)
2594 ;; Remove FOLDER from the list of folders.
2595 (setq mh-folder-list
2596 (delq (assoc folder mh-folder-list) mh-folder-list)))
2599 (defun mh-read-msg-range (prompt)
2600 ;; Read a list of blank-separated items.
2601 (let* ((buf (read-string prompt))
2602 (buf-size (length buf))
2603 (start 0)
2604 (input ()))
2605 (while (< start buf-size)
2606 (let ((next (read-from-string buf start buf-size)))
2607 (mh-push (car next) input)
2608 (setq start (cdr next))))
2609 (nreverse input)))
2613 ;;; Misc. functions.
2615 (defun mh-get-msg-num (error-if-no-message)
2616 ;; Return the message number of the displayed message. If the argument
2617 ;; ERROR-IF-NO-MESSAGE is non-nil, then complain if the cursor is not
2618 ;; pointing to a message.
2619 (save-excursion
2620 (beginning-of-line)
2621 (cond ((looking-at mh-msg-number-regexp)
2622 (string-to-int (buffer-substring (match-beginning 1)
2623 (match-end 1))))
2624 (error-if-no-message
2625 (error "Cursor not pointing to message"))
2626 (t nil))))
2629 (defun mh-msg-search-pat (n)
2630 ;; Return a search pattern for message N in the scan listing.
2631 (format mh-msg-search-regexp n))
2634 (defun mh-msg-filename (msg &optional folder)
2635 ;; Return the file name of MESSAGE in FOLDER (default current folder).
2636 (expand-file-name (int-to-string msg)
2637 (if folder
2638 (mh-expand-file-name folder)
2639 mh-folder-filename)))
2642 (defun mh-msg-filenames (msgs &optional folder)
2643 ;; Return a list of file names for MSGS in FOLDER (default current folder).
2644 (mapconcat (function (lambda (msg) (mh-msg-filename msg folder))) msgs " "))
2647 (defun mh-expand-file-name (filename &optional default)
2648 "Just like `expand-file-name', but also handles MH folder names.
2649 Assumes that any filename that starts with '+' is a folder name."
2650 (if (mh-folder-name-p filename)
2651 (expand-file-name (substring filename 1) mh-user-path)
2652 (expand-file-name filename default)))
2655 (defun mh-find-path ()
2656 ;; Set mh-user-path, mh-draft-folder, and mh-unseen-seq from profile file.
2657 (save-excursion
2658 ;; Be sure profile is fully expanded before switching buffers
2659 (let ((profile (expand-file-name (or (getenv "MH") "~/.mh_profile"))))
2660 (if (not (file-exists-p profile))
2661 (error "Cannot find MH profile %s" profile))
2662 (set-buffer (get-buffer-create " *mh-temp*"))
2663 (erase-buffer)
2664 (insert-file-contents profile)
2665 (setq mh-draft-folder (mh-get-field "Draft-Folder:"))
2666 (cond ((equal mh-draft-folder "")
2667 (setq mh-draft-folder nil))
2668 ((not (mh-folder-name-p mh-draft-folder))
2669 (setq mh-draft-folder (format "+%s" mh-draft-folder))))
2670 (setq mh-user-path (mh-get-field "Path:"))
2671 (if (equal mh-user-path "")
2672 (setq mh-user-path "Mail"))
2673 (setq mh-user-path
2674 (file-name-as-directory
2675 (expand-file-name mh-user-path (expand-file-name "~"))))
2676 (if (and mh-draft-folder
2677 (not (file-exists-p (mh-expand-file-name mh-draft-folder))))
2678 (error "Draft folder %s does not exist. Create it and try again."
2679 mh-draft-folder))
2680 (setq mh-unseen-seq (mh-get-field "Unseen-Sequence:"))
2681 (if (equal mh-unseen-seq "")
2682 (setq mh-unseen-seq 'unseen)
2683 (setq mh-unseen-seq (intern mh-unseen-seq))))))
2686 (defun mh-get-field (field)
2687 ;; Find and return the value of field FIELD in the current buffer.
2688 ;; Returns the empty string if the field is not in the message.
2689 (let ((case-fold-search t))
2690 (goto-char (point-min))
2691 (cond ((not (search-forward field nil t)) "")
2692 ((looking-at "[\t ]*$") "")
2694 (re-search-forward "[\t ]*\\([^\t \n].*\\)$" nil t)
2695 (let ((field (buffer-substring (match-beginning 1)
2696 (match-end 1)))
2697 (end-of-match (point)))
2698 (forward-line)
2699 (while (looking-at "[ \t]") (forward-line 1))
2700 (backward-char 1)
2701 (if (<= (point) end-of-match)
2702 field
2703 (format "%s%s"
2704 field
2705 (buffer-substring end-of-match (point)))))))))
2708 (defun mh-insert-fields (&rest name-values)
2709 ;; Insert the NAME-VALUE pairs in the current buffer.
2710 ;; Do not insert any pairs whose value is the empty string.
2711 (let ((case-fold-search t))
2712 (while name-values
2713 (let ((field-name (car name-values))
2714 (value (car (cdr name-values))))
2715 (mh-when (not (equal value ""))
2716 (goto-char (point-min))
2717 (cond ((not (re-search-forward (format "^%s" field-name) nil t))
2718 (mh-goto-header-end 0)
2719 (insert field-name " " value "\n"))
2721 (end-of-line)
2722 (insert " " value))))
2723 (setq name-values (cdr (cdr name-values)))))))
2726 (defun mh-position-on-field (field set-mark)
2727 ;; Set point to the end of the line beginning with FIELD.
2728 ;; Set the mark to the old value of point, if SET-MARK is non-nil.
2729 (let ((case-fold-search t))
2730 (if set-mark (push-mark))
2731 (goto-char (point-min))
2732 (mh-goto-header-end 0)
2733 (if (re-search-backward (format "^%s" field) nil t)
2734 (progn (end-of-line) t)
2735 nil)))
2738 (defun mh-goto-header-end (arg)
2739 ;; Find the end of the message header in the current buffer and position
2740 ;; the cursor at the ARG'th newline after the header.
2741 (if (re-search-forward "^$\\|^-+$" nil nil)
2742 (forward-line arg)))
2746 ;;; Build the folder-mode keymap:
2748 (suppress-keymap mh-folder-mode-map)
2749 (define-key mh-folder-mode-map "q" 'mh-quit)
2750 (define-key mh-folder-mode-map "b" 'mh-quit)
2751 (define-key mh-folder-mode-map "?" 'mh-msg-is-in-seq)
2752 (define-key mh-folder-mode-map "%" 'mh-put-msg-in-seq)
2753 (define-key mh-folder-mode-map "|" 'mh-pipe-msg)
2754 (define-key mh-folder-mode-map "\ea" 'mh-edit-again)
2755 (define-key mh-folder-mode-map "\e%" 'mh-delete-msg-from-seq)
2756 (define-key mh-folder-mode-map "\C-xn" 'mh-narrow-to-seq)
2757 (define-key mh-folder-mode-map "\C-xw" 'mh-widen)
2758 (define-key mh-folder-mode-map "\eb" 'mh-burst-digest)
2759 (define-key mh-folder-mode-map "\eu" 'mh-undo-folder)
2760 (define-key mh-folder-mode-map "\e " 'mh-page-digest)
2761 (define-key mh-folder-mode-map "\e\177" 'mh-page-digest-backwards)
2762 (define-key mh-folder-mode-map "\ee" 'mh-extract-rejected-mail)
2763 (define-key mh-folder-mode-map "\ef" 'mh-visit-folder)
2764 (define-key mh-folder-mode-map "\ek" 'mh-kill-folder)
2765 (define-key mh-folder-mode-map "\el" 'mh-list-folders)
2766 (define-key mh-folder-mode-map "\eo" 'mh-write-msg-to-file)
2767 (define-key mh-folder-mode-map "\ep" 'mh-pack-folder)
2768 (define-key mh-folder-mode-map "\es" 'mh-search-folder)
2769 (define-key mh-folder-mode-map "\er" 'mh-rescan-folder)
2770 (define-key mh-folder-mode-map "l" 'mh-print-msg)
2771 (define-key mh-folder-mode-map "t" 'mh-toggle-showing)
2772 (define-key mh-folder-mode-map "c" 'mh-copy-msg)
2773 (define-key mh-folder-mode-map ">" 'mh-write-msg-to-file)
2774 (define-key mh-folder-mode-map "i" 'mh-inc-folder)
2775 (define-key mh-folder-mode-map "x" 'mh-execute-commands)
2776 (define-key mh-folder-mode-map "e" 'mh-execute-commands)
2777 (define-key mh-folder-mode-map "r" 'mh-redistribute)
2778 (define-key mh-folder-mode-map "f" 'mh-forward)
2779 (define-key mh-folder-mode-map "s" 'mh-send)
2780 (define-key mh-folder-mode-map "m" 'mh-send)
2781 (define-key mh-folder-mode-map "a" 'mh-reply)
2782 (define-key mh-folder-mode-map "j" 'mh-goto-msg)
2783 (define-key mh-folder-mode-map "g" 'mh-goto-msg)
2784 (define-key mh-folder-mode-map "\177" 'mh-previous-page)
2785 (define-key mh-folder-mode-map " " 'mh-page-msg)
2786 (define-key mh-folder-mode-map "." 'mh-show)
2787 (define-key mh-folder-mode-map "u" 'mh-undo)
2788 (define-key mh-folder-mode-map "!" 'mh-refile-or-write-again)
2789 (define-key mh-folder-mode-map "^" 'mh-refile-msg)
2790 (define-key mh-folder-mode-map "d" 'mh-delete-msg)
2791 (define-key mh-folder-mode-map "\C-d" 'mh-delete-msg-no-motion)
2792 (define-key mh-folder-mode-map "p" 'mh-previous-undeleted-msg)
2793 (define-key mh-folder-mode-map "n" 'mh-next-undeleted-msg)
2794 (define-key mh-folder-mode-map "o" 'mh-refile-msg)
2797 ;;; Build the letter-mode keymap:
2799 (define-key mh-letter-mode-map "\C-c\C-f\C-b" 'mh-to-field)
2800 (define-key mh-letter-mode-map "\C-c\C-f\C-c" 'mh-to-field)
2801 (define-key mh-letter-mode-map "\C-c\C-f\C-f" 'mh-to-fcc)
2802 (define-key mh-letter-mode-map "\C-c\C-f\C-s" 'mh-to-field)
2803 (define-key mh-letter-mode-map "\C-c\C-f\C-t" 'mh-to-field)
2804 (define-key mh-letter-mode-map "\C-c\C-fb" 'mh-to-field)
2805 (define-key mh-letter-mode-map "\C-c\C-fc" 'mh-to-field)
2806 (define-key mh-letter-mode-map "\C-c\C-ff" 'mh-to-fcc)
2807 (define-key mh-letter-mode-map "\C-c\C-fs" 'mh-to-field)
2808 (define-key mh-letter-mode-map "\C-c\C-ft" 'mh-to-field)
2809 (define-key mh-letter-mode-map "\C-c\C-q" 'mh-fully-kill-draft)
2810 (define-key mh-letter-mode-map "\C-c\C-w" 'mh-check-whom)
2811 (define-key mh-letter-mode-map "\C-c\C-i" 'mh-insert-letter)
2812 (define-key mh-letter-mode-map "\C-c\C-y" 'mh-yank-cur-msg)
2813 (define-key mh-letter-mode-map "\C-c\C-s" 'mh-insert-signature)
2814 (define-key mh-letter-mode-map "\C-c\C-c" 'mh-send-letter)
2817 ;;; Build the pick-mode keymap:
2819 (define-key mh-pick-mode-map "\C-c\C-c" 'mh-do-pick-search)
2820 (define-key mh-pick-mode-map "\C-c\C-f\C-b" 'mh-to-field)
2821 (define-key mh-pick-mode-map "\C-c\C-f\C-c" 'mh-to-field)
2822 (define-key mh-pick-mode-map "\C-c\C-f\C-f" 'mh-to-field)
2823 (define-key mh-pick-mode-map "\C-c\C-f\C-s" 'mh-to-field)
2824 (define-key mh-pick-mode-map "\C-c\C-f\C-t" 'mh-to-field)
2825 (define-key mh-pick-mode-map "\C-c\C-fb" 'mh-to-field)
2826 (define-key mh-pick-mode-map "\C-c\C-fc" 'mh-to-field)
2827 (define-key mh-pick-mode-map "\C-c\C-ff" 'mh-to-field)
2828 (define-key mh-pick-mode-map "\C-c\C-fs" 'mh-to-field)
2829 (define-key mh-pick-mode-map "\C-c\C-ft" 'mh-to-field)
2830 (define-key mh-pick-mode-map "\C-c\C-w" 'mh-check-whom)
2834 ;;; For Gnu Emacs.
2835 ;;; Local Variables: ***
2836 ;;; eval: (put 'mh-when 'lisp-indent-function 1) ***
2837 ;;; eval: (put 'with-mh-folder-updating 'lisp-indent-function 1) ***
2838 ;;; End: ***
2840 (provide 'mh-e)
2842 ;;; mh-e.el ends here