comparison lisp/=mh-e.el @ 731:5c6db33a9ef6

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