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