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