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