(idlwave-shell-set-bp): Fix typo.
[emacs.git] / lisp / mail / mh-utils.el
bloba6501fede672323519f2ed7e9fd9ee258d3c22a3
1 ;;; mh-utils.el --- mh-e code needed for both sending and reading
3 ;; Copyright (C) 1993, 1995, 1997, 2000, 2001, 2002 Free Software Foundation, Inc.
5 ;; Author: Bill Wohler <wohler@newt.com>
6 ;; Maintainer: Bill Wohler <wohler@newt.com>
7 ;; Keywords: mail
8 ;; See: mh-e.el
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
27 ;;; Commentary:
29 ;; Internal support for mh-e package.
31 ;;; Change Log:
33 ;; $Id: mh-utils.el,v 1.79 2002/04/07 19:20:56 wohler Exp $
35 ;;; Code:
37 (load "executable" t t) ; Non-fatal dependency on
38 ; executable-find
40 ;;; Autoload mh-seq
42 (autoload 'mh-add-to-sequence "mh-seq")
43 (autoload 'mh-notate-seq "mh-seq")
44 (autoload 'mh-read-seq-default "mh-seq")
45 (autoload 'mh-map-to-seq-msgs "mh-seq")
47 ;;; Other Autoloads
49 (autoload 'mail-header-end "sendmail")
51 ;;; Set for local environment:
52 ;;; mh-progs and mh-lib used to be set in paths.el, which tried to
53 ;;; figure out at build time which of several possible directories MH
54 ;;; was installed into. But if you installed MH after building Emacs,
55 ;;; this would almost certainly be wrong, so now we do it at run time.
57 (defvar mh-progs nil
58 "Directory containing MH commands, such as inc, repl, and rmm.")
60 (defvar mh-lib nil
61 "Directory containing the MH library.
62 This directory contains, among other things,
63 the components file.")
65 (defvar mh-lib-progs nil
66 "Directory containing MH helper programs.
67 This directory contains, among other things,
68 the mhl program.")
70 (defvar mh-nmh-p nil
71 "Non-nil if nmh is installed on this system instead of MH.")
73 ;;;###autoload
74 (put 'mh-progs 'risky-local-variable t)
75 ;;;###autoload
76 (put 'mh-lib 'risky-local-variable t)
77 ;;;###autoload
78 (put 'mh-lib-progs 'risky-local-variable t)
79 ;;;###autoload
80 (put 'mh-nmh-p 'risky-local-variable t)
82 ;;; User preferences:
84 (defgroup mh-buffer nil
85 "Layout of MH-E buffers"
86 :prefix "mh-"
87 :group 'mh)
90 (defcustom mh-auto-folder-collect t
91 "*Whether to start collecting MH folder names immediately in the background.
92 Non-nil means start a background process collecting the names of all
93 folders as soon as mh-e is loaded."
94 :type 'boolean
95 :group 'mh)
97 (defcustom mh-recursive-folders nil
98 "*If non-nil, then commands which operate on folders do so recursively."
99 :type 'boolean
100 :group 'mh)
102 (defcustom mh-clean-message-header t
103 "*Non-nil means clean headers of messages that are displayed or inserted.
104 The variables `mh-visible-headers' and `mh-invisible-headers' control what
105 is removed."
106 :type 'boolean
107 :group 'mh-buffer)
109 (defcustom mh-visible-headers nil
110 "*If non-nil, contains a regexp specifying the headers to keep when cleaning.
111 Only used if `mh-clean-message-header' is non-nil. Setting this variable
112 overrides `mh-invisible-headers'."
113 :type '(choice (const nil) regexp)
114 :group 'mh-buffer)
116 (defvar mh-invisible-headers
117 (concat
119 (let ((max-specpdl-size 1000)) ;workaround for insufficient default
120 (regexp-opt
121 '( ;; RFC 822
122 "Received: " "Message-Id: " "Return-Path: "
123 ;; RFC 2045
124 "Mime-Version" "Content-"
125 ;; sendmail
126 "X-Authentication-Warning: " "X-MIME-Autoconverted: " "From "
127 "Status: "
128 ;; X400
129 "X400-" "P1-Message-Id: " "Original-Encoded-Information-Types: "
130 "P1-Recipient: " "P1-Content-Type: " "Ua-Content-Id: "
131 ;; MH
132 "Resent" "Prev-Resent" "Forwarded: " "Replied: " "Delivery-Date: "
133 "In-Reply-To: " "Remailed-" "Via: " "Mail-from: "
134 ;; gnus
135 "X-Gnus-Mail-Source: "
136 ;; MS Outlook
137 "X-Priority: " "X-Msmail-" "X-MimeOLE: " "X-Apparently-From: "
138 "Importance: " "Sensitivity: " "X-MS-TNEF-Correlator: "
139 ;; Juno
140 "X-Juno-"
141 ;; Hotmail
142 "X-OriginalArrivalTime: " "X-Originating-IP: "
143 ;; Netscape/Mozilla
144 "X-Accept-Language: " "X-Mozilla-Status: "
145 ;; NTMail
146 "X-Info: " "X-VSMLoop: "
147 ;; News
148 "NNTP-" "X-News: "
149 ;; Mailman mailing list manager
150 "List-" "X-Beenthere: " "X-Mailman-Version: "
151 ;; Egroups/yahoogroups mailing list manager
152 "X-eGroups-" "X-Apparently-To: " "Mailing-List: " "Delivered-To: "
153 ;; SourceForge mailing list manager
154 "X-Original-Date: "
155 ;; Unknown mailing list managers
156 "X-Mailing-List: " "X-Loop: "
157 "List-Subscribe: " "List-Unsubscribe: "
158 "X-List-Subscribe: " "X-List-Unsubscribe: "
159 "X-Listserver: " "List-" "X-List-Host: "
160 ;; Sieve filtering
161 "X-Sieve: "
162 ;; Worldtalk gateways
163 "X-Wss-Id: "
164 ;; User added
165 "X-Face: " "X-Qotd-"
166 ;; Miscellaneous
167 "X-Sender: " "X-Ack: " "Errors-To: " "Precedence: " "X-Message-Id"
168 "X-From-Line" "X-Cron-Env: " "Delivery: " "X-Delivered"
169 "X-Received: " "X-Vms-To: " "Xref: " "X-Request-" "X-UIDL: "
170 "X-Orcl-Content-Type: " "X-Server-Uuid: " "X-Envelope-Sender: "
171 "X-Envelope-To: " "Encoding: " "Old-Return-Path: " "Path: "
172 "References: " "Lines: " "Autoforwarded: " "Bestservhost: "
173 "X-pgp: " "X-Accept-Language: " "Priority: " "User-Agent: "
174 "X-MIMETrack: " "X-Abuse-Info: " "X-Complaints-To: "
175 "X-No-Archive: " "X-Original-Complaints-To: "
176 "X-Original-Trace: " "X-Received-Date: " "X-Server-Date: "
177 "X-Trace: " "X-UserInfo1: " "X-submission-address: ")
178 t)))
179 "*Regexp matching lines in a message header that are not to be shown.
180 If `mh-visible-headers' is non-nil, it is used instead to specify what
181 to keep.")
183 ;;; Additional header fields that might someday be added:
184 ;;; "Sender: " "Reply-to: "
186 (defcustom mh-bury-show-buffer t
187 "*Non-nil means that the displayed show buffer for a folder is buried."
188 :type 'boolean
189 :group 'mh-buffer)
191 (defcustom mh-summary-height (or (and (fboundp 'frame-height)
192 (> (frame-height) 24)
193 (min 10 (/ (frame-height) 6)))
195 "*Number of lines in MH-Folder window (including the mode line)."
196 :type 'integer
197 :group 'mh-buffer)
199 ;; Use goto-addr if it was already loaded (which probably sets this
200 ;; variable to t), or if this variable is otherwise set to t.
201 (defcustom mh-show-use-goto-addr (and (boundp 'goto-address-highlight-p)
202 goto-address-highlight-p)
203 "*Non-nil means URLs and e-mail addresses are highlighted using goto-addr while in mh-show-mode."
204 :type 'boolean
205 :group 'mh-buffer)
207 (defvar mh-scan-msg-number-regexp "^ *\\([0-9]+\\)"
208 "Regexp to find the number of a message in a scan line.
209 The message's number must be surrounded with \\( \\)")
211 (defvar mh-scan-msg-search-regexp "^[^0-9]*%d[^0-9]"
212 "Format string containing a regexp matching the scan listing for a message.
213 The desired message's number will be an argument to format.")
215 (defcustom mhl-formfile nil
216 "*Name of format file to be used by mhl to show and print messages.
217 A value of T means use the default format file.
218 Nil means don't use mhl to format messages when showing; mhl is still used,
219 with the default format file, to format messages when printing them.
220 The format used should specify a non-zero value for overflowoffset so
221 the message continues to conform to RFC 822 and mh-e can parse the headers."
222 :type '(choice (const nil) (const t) string)
223 :group 'mh)
224 (put 'mhl-formfile 'info-file "mh-e")
226 (defvar mh-decode-quoted-printable-have-mimedecode
227 (not (null (and (fboundp 'executable-find)(executable-find "mimedecode"))))
228 "Whether the mimedecode command is installed on the system.
229 This sets the default value of variable `mh-decode-quoted-printable' to
230 determine whether quoted-printable MIME parts are decode when viewed in
231 `mh-show'. The source code for mimedecode can be obtained from
232 http://www.freesoft.org/CIE/FAQ/mimedeco.c")
234 (defcustom mh-decode-quoted-printable
235 mh-decode-quoted-printable-have-mimedecode
236 "Whether to decode quoted-printable MIME parts in `mh-show'.
237 This can only be done if the 'mimedecode' command is available in the
238 executable path on the system (the mh-decode-quoted-printable-have-mimedecode
239 variable is set if the command was found). That program is used as a helper
240 program to achieve this. The source code for mimedecode can usually be
241 obtained from http://www.freesoft.org/CIE/FAQ/mimedeco.c"
242 :type 'boolean
243 :group 'mh-buffer)
245 (defcustom mh-update-sequences-after-mh-show t
246 "Whether to call `mh-update-sequence' in `mh-show-mode'.
247 If set, `mh-update-sequence' is run every time a message is shown, telling
248 MH or nmh that this is your current message. It's useful, for example, to
249 display MIME content using \"M-! mhshow RET\""
250 :type 'boolean
251 :group 'mh-buffer)
253 (defcustom mh-highlight-citation-p 'gnus
254 "How to highlight citations in show buffers.
255 The gnus method uses a different color for each indentation."
256 :type '(choice (const :tag "Use gnus" gnus)
257 (const :tag "Use font-lock" font-lock)
258 (const :tag "Don't fontify" nil))
259 :group 'mh-buffer)
261 (defvar mh-default-folder-for-message-function nil
262 "Function to select a default folder for refiling or Fcc.
263 If set to a function, that function is called with no arguments by
264 `\\[mh-refile-msg]' and `\\[mh-to-fcc]' to get a default when
265 prompting the user for a folder. The function is called from within a
266 `save-excursion', with point at the start of the message. It should
267 return the folder to offer as the refile or Fcc folder, as a string
268 with a leading `+' sign. It can also return an empty string to use no
269 default, or NIL to calculate the default the usual way.
270 NOTE: This variable is not an ordinary hook;
271 It may not be a list of functions.")
273 (defvar mh-find-path-hook nil
274 "Invoked by `mh-find-path' while reading the user's MH profile.")
276 (defvar mh-folder-list-change-hook nil
277 "Invoked whenever the cached folder list `mh-folder-list' is changed.")
279 (defvar mh-show-buffer-mode-line-buffer-id "{show-%s} %d"
280 "Format string to produce `mode-line-buffer-identification' for show buffers.
281 First argument is folder name. Second is message number.")
283 (defvar mh-cmd-note 4
284 "Offset to insert notation.")
286 (defvar mh-note-seq "%"
287 "String whose first character is used to notate messages in a sequence.")
289 (defvar mh-mail-header-separator "--------"
290 "*Line used by MH to separate headers from text in messages being composed.
291 This variable should not be used directly in programs. Programs should use
292 `mail-header-separator' instead. `mail-header-separator' is initialized to
293 `mh-mail-header-separator' in `mh-letter-mode'; in other contexts, you may
294 have to perform this initialization yourself.
296 Do not make this a regexp as it may be the argument to `insert' and it is
297 passed through `regexp-quote' before being used by functions like
298 `re-search-forward'.")
300 (defun mh-in-header-p ()
301 ;; Return non-nil if the point is in the header of a draft message.
302 (< (point) (mail-header-end)))
304 (defun mh-header-field-end ()
305 ;; Move to the end of the current header field.
306 ;; Handles RFC 822 continuation lines.
307 (forward-line 1)
308 (while (looking-at "^[ \t]")
309 (forward-line 1))
310 (backward-char 1)) ;to end of previous line
312 (defun mh-letter-header-font-lock (limit)
313 "Return the entire mail header to font-lock.
314 Argument LIMIT limits search."
315 (if (= (point) limit)
317 (let* ((mail-header-end (save-match-data (mail-header-end)))
318 (lesser-limit (if (< mail-header-end limit) mail-header-end limit)))
319 (when (mh-in-header-p)
320 (set-match-data (list 1 lesser-limit))
321 (goto-char lesser-limit)
322 t))))
324 (defun mh-header-field-font-lock (field limit)
325 "Return the value of a header field FIELD to font-lock.
326 Argument LIMIT limits search."
327 (if (= (point) limit)
329 (let* ((mail-header-end (mail-header-end))
330 (lesser-limit (if (< mail-header-end limit) mail-header-end limit))
331 (case-fold-search t))
332 (when (and (< (point) mail-header-end) ;Only within header
333 (re-search-forward (format "^%s" field) lesser-limit t))
334 (let ((match-one-b (match-beginning 0))
335 (match-one-e (match-end 0)))
336 (mh-header-field-end)
337 (if (> (point) limit) ;Don't search for end beyond limit
338 (goto-char limit))
339 (set-match-data (list match-one-b match-one-e
340 (1+ match-one-e) (point)))
341 t)))))
343 (defun mh-header-to-font-lock (limit)
344 (mh-header-field-font-lock "To:" limit))
346 (defun mh-header-cc-font-lock (limit)
347 (mh-header-field-font-lock "cc:" limit))
349 (defun mh-header-subject-font-lock (limit)
350 (mh-header-field-font-lock "Subject:" limit))
352 (defvar mh-show-to-face 'mh-show-to-face
353 "Face for highlighting the To: header field.")
354 (if (boundp 'facemenu-unlisted-faces)
355 (add-to-list 'facemenu-unlisted-faces "^mh-show"))
356 (defface mh-show-to-face
357 '((((class grayscale) (background light))
358 (:foreground "DimGray" :underline t))
359 (((class grayscale) (background dark))
360 (:foreground "LightGray" :underline t))
361 (((class color) (background light)) (:foreground "SaddleBrown"))
362 (((class color) (background dark)) (:foreground "burlywood"))
363 (t (:underline t)))
364 "Face for highlighting the To: header field."
365 :group 'mh-buffer)
367 (defvar mh-show-from-face 'mh-show-from-face
368 "Face for highlighting the From: header field.")
369 (defface mh-show-from-face
370 '((((class color) (background light))
371 (:foreground "red3"))
372 (((class color) (background dark))
373 (:foreground "cyan"))
375 (:bold t)))
376 "Face for highlighting the From: header field."
377 :group 'mh-buffer)
379 (defvar mh-folder-subject-face 'mh-folder-subject-face
380 "Face for highlighting subject text in MH-Folder buffers.")
381 (if (boundp 'facemenu-unlisted-faces)
382 (add-to-list 'facemenu-unlisted-faces "^mh-folder"))
383 (defface mh-folder-subject-face
384 '((((class color) (background light))
385 (:foreground "blue4"))
386 (((class color) (background dark))
387 (:foreground "yellow"))
389 (:bold t)))
390 "Face for highlighting subject text in MH-Folder buffers."
391 :group 'mh)
392 (defvar mh-show-subject-face 'mh-show-subject-face
393 "Face for highlighting the Subject header field.")
394 (copy-face 'mh-folder-subject-face 'mh-show-subject-face)
396 (eval-after-load "font-lock"
397 '(progn
398 (defvar mh-show-cc-face 'mh-show-cc-face
399 "Face for highlighting cc header fields.")
400 (copy-face 'font-lock-variable-name-face 'mh-show-cc-face)
401 (defvar mh-show-date-face 'mh-show-date-face
402 "Face for highlighting the Date header field.")
403 (copy-face 'font-lock-type-face 'mh-show-date-face)
404 (defvar mh-show-header-face 'mh-show-header-face
405 "Face used to deemphasize unspecified header fields.")
406 (copy-face 'font-lock-string-face 'mh-show-header-face)
408 (defvar mh-show-font-lock-keywords
409 '(("^\\(From:\\|Sender:\\)\\(.*\\)"
410 (1 'default) (2 mh-show-from-face))
411 (mh-header-to-font-lock
412 (0 'default) (1 mh-show-to-face))
413 (mh-header-cc-font-lock
414 (0 'default) (1 mh-show-cc-face))
415 ("^\\(Reply-To:\\|Return-Path:\\)\\(.*\\)$"
416 (1 'default) (2 mh-show-from-face))
417 (mh-header-subject-font-lock
418 (0 'default) (1 mh-show-subject-face))
419 ("^\\(Apparently-To:\\|Newsgroups:\\)\\(.*\\)"
420 (1 'default) (2 mh-show-cc-face))
421 ("^\\(In-reply-to\\|Date\\):\\(.*\\)$"
422 (1 'default) (2 mh-show-date-face))
423 (mh-letter-header-font-lock (0 mh-show-header-face append t)))
424 "Additional expressions to highlight in MH-show mode.")
426 (defvar mh-show-font-lock-keywords-with-cite
427 (eval-when-compile
428 (let* ((cite-chars "[>|}]")
429 (cite-prefix "A-Za-z")
430 (cite-suffix (concat cite-prefix "0-9_.@-`'\"")))
431 (append
432 mh-show-font-lock-keywords
433 (list
434 ;; Use MATCH-ANCHORED to effectively anchor the regexp left side.
435 `(,cite-chars
436 (,(concat "\\=[ \t]*"
437 "\\(\\([" cite-prefix "]+[" cite-suffix "]*\\)?"
438 "\\(" cite-chars "[ \t]*\\)\\)+"
439 "\\(.*\\)")
440 (beginning-of-line) (end-of-line)
441 (2 font-lock-constant-face nil t)
442 (4 font-lock-comment-face nil t)))))))
443 "Additional expressions to highlight in MH-show mode.")
446 (defun mh-gnus-article-highlight-citation ()
447 "Highlight cited text in current buffer using gnus."
448 (interactive)
449 (require 'gnus-cite)
450 (let ((modified (buffer-modified-p))
451 (gnus-article-buffer (buffer-name))
452 (gnus-cite-face-list
453 '(gnus-cite-face-2 gnus-cite-face-3 gnus-cite-face-4 gnus-cite-face-5
454 gnus-cite-face-6 gnus-cite-face-7 gnus-cite-face-8 gnus-cite-face-9
455 gnus-cite-face-10 gnus-cite-face-11 gnus-cite-face-1)))
456 (gnus-article-highlight-citation t)
457 (set-buffer-modified-p modified)))
459 ;;; Internal bookkeeping variables:
461 ;; The value of `mh-folder-list-change-hook' is called whenever
462 ;; mh-folder-list variable is set.
463 ;; List of folder names for completion.
464 (defvar mh-folder-list nil)
466 ;; Cached value of the `Path:' component in the user's MH profile.
467 ;; User's mail folder directory.
468 (defvar mh-user-path nil)
470 ;; An mh-draft-folder of NIL means do not use a draft folder.
471 ;; Cached value of the `Draft-Folder:' component in the user's MH profile.
472 ;; Name of folder containing draft messages.
473 (defvar mh-draft-folder nil)
475 ;; Cached value of the `Unseen-Sequence:' component in the user's MH profile.
476 ;; Name of the Unseen sequence.
477 (defvar mh-unseen-seq nil)
479 ;; Cached value of the `Previous-Sequence:' component in the user's MH
480 ;; profile.
481 ;; Name of the Previous sequence.
482 (defvar mh-previous-seq nil)
484 ;; Cached value of the `Inbox:' component in the user's MH profile,
485 ;; or "+inbox" if no such component.
486 ;; Name of the Inbox folder.
487 (defvar mh-inbox nil)
489 ;; Name of mh-e scratch buffer.
490 (defconst mh-temp-buffer " *mh-temp*")
492 ;; Name of the mh-e folder list buffer.
493 (defconst mh-temp-folders-buffer "*Folders*")
495 ;; Name of the mh-e sequences list buffer.
496 (defconst mh-temp-sequences-buffer "*Sequences*")
498 ;; Window configuration before mh-e command.
499 (defvar mh-previous-window-config nil)
501 ;;Non-nil means next SPC or whatever goes to next undeleted message.
502 (defvar mh-page-to-next-msg-p nil)
504 ;;; Internal variables local to a folder.
506 ;; Name of current folder, a string.
507 (defvar mh-current-folder nil)
509 ;; Buffer that displays message for this folder.
510 (defvar mh-show-buffer nil)
512 ;; Full path of directory for this folder.
513 (defvar mh-folder-filename nil)
515 ;;Number of msgs in buffer.
516 (defvar mh-msg-count nil)
518 ;; If non-nil, show the message in a separate window.
519 (defvar mh-showing-mode nil)
521 ;;; This holds a documentation string used by describe-mode.
522 (defun mh-showing-mode (&optional arg)
523 "Change whether messages should be displayed.
524 With arg, display messages iff ARG is positive."
525 (setq mh-showing-mode
526 (if (null arg)
527 (not mh-showing-mode)
528 (> (prefix-numeric-value arg) 0))))
530 ;; The sequences of this folder. An alist of (seq . msgs).
531 (defvar mh-seq-list nil)
533 ;; List of displayed messages to be removed from the Unseen sequence.
534 (defvar mh-seen-list nil)
536 ;; If non-nil, show buffer contains message with all headers.
537 ;; If nil, show buffer contains message processed normally.
538 ;; Showing message with headers or normally.
539 (defvar mh-showing-with-headers nil)
542 ;;; mh-e macros
544 (defmacro with-mh-folder-updating (save-modification-flag-p &rest body)
545 ;; Format is (with-mh-folder-updating (SAVE-MODIFICATION-FLAG-P) &body BODY).
546 ;; Execute BODY, which can modify the folder buffer without having to
547 ;; worry about file locking or the read-only flag, and return its result.
548 ;; If SAVE-MODIFICATION-FLAG-P is non-nil, the buffer's modification
549 ;; flag is unchanged, otherwise it is cleared.
550 (setq save-modification-flag-p (car save-modification-flag-p)) ; CL style
551 `(prog1
552 (let ((mh-folder-updating-mod-flag (buffer-modified-p))
553 (buffer-read-only nil)
554 (buffer-file-name nil)) ;don't let the buffer get locked
555 (prog1
556 (progn
557 ,@body)
558 (mh-set-folder-modified-p mh-folder-updating-mod-flag)))
559 ,@(if (not save-modification-flag-p)
560 '((mh-set-folder-modified-p nil)))))
562 (put 'with-mh-folder-updating 'lisp-indent-hook 1)
564 (defmacro mh-in-show-buffer (show-buffer &rest body)
565 ;; Format is (mh-in-show-buffer (SHOW-BUFFER) &body BODY).
566 ;; Display buffer SHOW-BUFFER in other window and execute BODY in it.
567 ;; Stronger than save-excursion, weaker than save-window-excursion.
568 (setq show-buffer (car show-buffer)) ; CL style
569 `(let ((mh-in-show-buffer-saved-window (selected-window)))
570 (switch-to-buffer-other-window ,show-buffer)
571 (if mh-bury-show-buffer (bury-buffer (current-buffer)))
572 (unwind-protect
573 (progn
574 ,@body)
575 (select-window mh-in-show-buffer-saved-window))))
577 (put 'mh-in-show-buffer 'lisp-indent-hook 1)
579 (defmacro mh-make-seq (name msgs) (list 'cons name msgs))
581 (defmacro mh-seq-name (pair) (list 'car pair))
583 (defmacro mh-seq-msgs (pair) (list 'cdr pair))
586 ;;; Ensure new buffers won't get this mode if default-major-mode is nil.
587 (put 'mh-show-mode 'mode-class 'special)
589 (define-derived-mode mh-show-mode text-mode "MH-Show"
590 "Major mode for showing messages in mh-e.
591 The value of `mh-show-mode-hook' is called when a new message is displayed."
592 (set (make-local-variable 'mail-header-separator) mh-mail-header-separator)
593 (mh-show-unquote-From)
594 (when mh-show-use-goto-addr
595 (if (not (featurep 'goto-addr))
596 (load "goto-addr" t t))
597 (if (fboundp 'goto-address)
598 (goto-address)))
599 (make-local-variable 'font-lock-defaults)
600 (set (make-local-variable 'font-lock-support-mode) nil)
601 (cond
602 ((equal mh-highlight-citation-p 'font-lock)
603 (setq font-lock-defaults '(mh-show-font-lock-keywords-with-cite t)))
604 ((equal mh-highlight-citation-p 'gnus)
605 (setq font-lock-defaults '(mh-show-font-lock-keywords t))
606 (mh-gnus-article-highlight-citation))
608 (setq font-lock-defaults '(mh-show-font-lock-keywords t)))))
610 (defun mh-maybe-show (&optional msg)
611 ;; If in showing mode, then display the message pointed to by the cursor.
612 (if mh-showing-mode (mh-show msg)))
614 (defun mh-show (&optional message)
615 "Show MESSAGE (default: message at cursor).
616 Force a two-window display with the folder window on top (size
617 `mh-summary-height') and the show buffer below it.
618 If the message is already visible, display the start of the message.
620 Display of the message is controlled by setting the variables
621 `mh-clean-message-header' and `mhl-formfile'. The default behavior is
622 to scroll uninteresting headers off the top of the window.
623 Type \"\\[mh-header-display]\" to see the message with all its headers."
624 (interactive)
625 (and mh-showing-with-headers
626 (or mhl-formfile mh-clean-message-header)
627 (mh-invalidate-show-buffer))
628 (mh-show-msg message))
630 (defun mh-show-mouse (EVENT)
631 "Move point to mouse EVENT and show message."
632 (interactive "e")
633 (mouse-set-point EVENT)
634 (mh-show))
636 (defun mh-show-msg (msg)
637 (if (not msg)
638 (setq msg (mh-get-msg-num t)))
639 (mh-showing-mode t)
640 (setq mh-page-to-next-msg-p nil)
641 (let ((folder mh-current-folder)
642 (clean-message-header mh-clean-message-header)
643 (show-window (get-buffer-window mh-show-buffer)))
644 (if (not (eq (next-window (minibuffer-window)) (selected-window)))
645 (delete-other-windows)) ; force ourself to the top window
646 (mh-in-show-buffer (mh-show-buffer)
647 (if (and show-window
648 (equal (mh-msg-filename msg folder) buffer-file-name))
649 (progn ;just back up to start
650 (goto-char (point-min))
651 (if (not clean-message-header)
652 (mh-start-of-uncleaned-message)))
653 (mh-display-msg msg folder))))
654 (if (not (= (1+ (window-height)) (frame-height))) ;not horizontally split
655 (shrink-window (- (window-height) mh-summary-height)))
656 (mh-recenter nil)
657 (if (not (memq msg mh-seen-list)) (setq mh-seen-list (cons msg mh-seen-list)))
658 (when mh-update-sequences-after-mh-show
659 (mh-update-sequences))
660 (run-hooks 'mh-show-hook))
663 (defun mh-decode-quoted-printable ()
664 ;; Run mimedecode commmand on current buffer, replacing it contents.
665 (let ((case-fold-search t))
666 (goto-char (point-min))
667 (when (and (re-search-forward
668 "^content-transfer-encoding:[ \t]*quoted-printable"
669 nil t)
670 (search-forward "\n\n" nil t))
671 (message "Converting quoted-printable characters...")
672 (let ((modified (buffer-modified-p))
673 (command "mimedecode"))
674 (shell-command-on-region (point-min) (point-max) command t t)
675 (if (fboundp 'deactivate-mark)
676 (deactivate-mark))
677 (set-buffer-modified-p modified))
678 (message "Converting quoted-printable characters... done."))))
681 (defun mh-show-unquote-From ()
682 ;; Decode >From at beginning of lines for mh-show-mode
683 (save-excursion
684 (let ((modified (buffer-modified-p))
685 (case-fold-search nil))
686 (goto-char (mail-header-end))
687 (while (re-search-forward "^>From" nil t)
688 (replace-match "From"))
689 (set-buffer-modified-p modified))))
691 (defun mh-display-msg (msg-num folder)
692 ;; Display message NUMBER of FOLDER.
693 ;; Sets the current buffer to the show buffer.
694 (set-buffer folder)
695 ;; Bind variables in folder buffer in case they are local
696 (let ((formfile mhl-formfile)
697 (clean-message-header mh-clean-message-header)
698 (invisible-headers mh-invisible-headers)
699 (visible-headers mh-visible-headers)
700 (msg-filename (mh-msg-filename msg-num))
701 (show-buffer mh-show-buffer))
702 (if (not (file-exists-p msg-filename))
703 (error "Message %d does not exist" msg-num))
704 (set-buffer show-buffer)
705 (cond ((not (equal msg-filename buffer-file-name))
706 (mh-unvisit-file)
707 (erase-buffer)
708 ;; Changing contents, so this hook needs to be reinitialized.
709 ;; pgp.el uses this.
710 (if (boundp 'write-contents-hooks) ;Emacs 19
711 (kill-local-variable 'write-contents-hooks))
712 (if formfile
713 (mh-exec-lib-cmd-output "mhl" "-nobell" "-noclear"
714 (if (stringp formfile)
715 (list "-form" formfile))
716 msg-filename)
717 (insert-file-contents msg-filename))
718 (if mh-decode-quoted-printable
719 (mh-decode-quoted-printable))
720 (goto-char (point-min))
721 (cond (clean-message-header
722 (mh-clean-msg-header (point-min)
723 invisible-headers
724 visible-headers)
725 (goto-char (point-min)))
727 (mh-start-of-uncleaned-message)))
728 ;; the parts of visiting we want to do (no locking)
729 (or (eq buffer-undo-list t) ;don't save undo info for prev msgs
730 (setq buffer-undo-list nil))
731 (set-buffer-modified-p nil)
732 (set-buffer-auto-saved)
733 ;; the parts of set-visited-file-name we want to do (no locking)
734 (setq buffer-file-name msg-filename)
735 (setq buffer-backed-up nil)
736 (auto-save-mode 1)
737 (set-mark nil)
738 (mh-show-mode)
739 (setq mode-line-buffer-identification
740 (list (format mh-show-buffer-mode-line-buffer-id
741 folder msg-num)))
742 (set-buffer folder)
743 (setq mh-showing-with-headers nil)))))
745 (defun mh-start-of-uncleaned-message ()
746 ;; position uninteresting headers off the top of the window
747 (let ((case-fold-search t))
748 (re-search-forward
749 "^To:\\|^Cc:\\|^From:\\|^Subject:\\|^Date:" nil t)
750 (beginning-of-line)
751 (mh-recenter 0)))
754 (defun mh-invalidate-show-buffer ()
755 ;; Invalidate the show buffer so we must update it to use it.
756 (if (get-buffer mh-show-buffer)
757 (save-excursion
758 (set-buffer mh-show-buffer)
759 (mh-unvisit-file))))
762 (defun mh-unvisit-file ()
763 ;; Separate current buffer from the message file it was visiting.
764 (or (not (buffer-modified-p))
765 (null buffer-file-name) ;we've been here before
766 (yes-or-no-p (format "Message %s modified; flush changes? "
767 (file-name-nondirectory buffer-file-name)))
768 (error "Flushing changes not confirmed"))
769 (clear-visited-file-modtime)
770 (unlock-buffer)
771 (setq buffer-file-name nil))
774 (defun mh-get-msg-num (error-if-no-message)
775 ;; Return the message number of the displayed message. If the argument
776 ;; ERROR-IF-NO-MESSAGE is non-nil, then complain if the cursor is not
777 ;; pointing to a message.
778 (save-excursion
779 (beginning-of-line)
780 (cond ((looking-at mh-scan-msg-number-regexp)
781 (string-to-int (buffer-substring (match-beginning 1)
782 (match-end 1))))
783 (error-if-no-message
784 (error "Cursor not pointing to message"))
785 (t nil))))
788 (defun mh-msg-filename (msg &optional folder)
789 ;; Return the file name of MESSAGE in FOLDER (default current folder).
790 (expand-file-name (int-to-string msg)
791 (if folder
792 (mh-expand-file-name folder)
793 mh-folder-filename)))
796 (defun mh-clean-msg-header (start invisible-headers visible-headers)
797 ;; Flush extraneous lines in a message header, from the given POINT to the
798 ;; end of the message header. If VISIBLE-HEADERS is non-nil, it contains a
799 ;; regular expression specifying the lines to display, otherwise
800 ;; INVISIBLE-HEADERS contains a regular expression specifying lines to
801 ;; delete from the header.
802 (let ((case-fold-search t)
803 (after-change-functions nil)) ;Work around emacs-20 font-lock bug
804 ;causing an endless loop.
805 (save-restriction
806 (goto-char start)
807 (if (search-forward "\n\n" nil 'move)
808 (backward-char 1))
809 (narrow-to-region start (point))
810 (goto-char (point-min))
811 (if visible-headers
812 (while (< (point) (point-max))
813 (cond ((looking-at visible-headers)
814 (forward-line 1)
815 (while (looking-at "[ \t]") (forward-line 1)))
817 (mh-delete-line 1)
818 (while (looking-at "[ \t]")
819 (mh-delete-line 1)))))
820 (while (re-search-forward invisible-headers nil t)
821 (beginning-of-line)
822 (mh-delete-line 1)
823 (while (looking-at "[ \t]")
824 (mh-delete-line 1))))
825 (unlock-buffer))))
828 (defun mh-recenter (arg)
829 ;; Like recenter but with two improvements:
830 ;; - only does anything if the current buffer is in the selected
831 ;; window. (Commands like save-some-buffers can make this false.)
832 ;; - nil arg means recenter as with C-u prefix
833 (if (eq (get-buffer-window (current-buffer))
834 (selected-window))
835 ;; '(4) is the same as C-u prefix argument.
836 (recenter (if arg arg '(4)))))
839 (defun mh-delete-line (lines)
840 ;; Delete version of kill-line.
841 (delete-region (point) (progn (forward-line lines) (point))))
843 (defun mh-notate (msg notation offset)
844 ;; Marks MESSAGE with the character NOTATION at position OFFSET.
845 ;; Null MESSAGE means the message that the cursor points to.
846 (save-excursion
847 (if (or (null msg)
848 (mh-goto-msg msg t t))
849 (with-mh-folder-updating (t)
850 (beginning-of-line)
851 (forward-char offset)
852 (delete-char 1)
853 (insert notation)))))
856 (defun mh-find-msg-get-num (step)
857 ;; Return the message number of the message on the current scan line
858 ;; or one nearby. Jumps over non-message lines, such as inc errors.
859 ;; STEP tells whether to search forward or backward if we have to search.
860 (or (mh-get-msg-num nil)
861 (let ((msg-num nil)
862 (nreverses 0))
863 (while (and (not msg-num)
864 (< nreverses 2))
865 (cond ((eobp)
866 (setq step -1)
867 (setq nreverses (1+ nreverses)))
868 ((bobp)
869 (setq step 1)
870 (setq nreverses (1+ nreverses))))
871 (forward-line step)
872 (setq msg-num (mh-get-msg-num nil)))
873 msg-num)))
875 (defun mh-goto-msg (number &optional no-error-if-no-message dont-show)
876 "Position the cursor at message NUMBER.
877 Optional non-nil second argument NO-ERROR-IF-NO-MESSAGE means return nil
878 instead of signaling an error if message does not exist; in this case, the
879 cursor is positioned near where the message would have been.
880 Non-nil third argument DONT-SHOW means not to show the message."
881 (interactive "NGo to message: ")
882 (setq number (prefix-numeric-value number)) ;Emacs 19
883 ;; This basic routine tries to be as fast as possible,
884 ;; using a binary search and minimal regexps.
885 (let ((cur-msg (mh-find-msg-get-num -1))
886 (jump-size mh-msg-count))
887 (while (and (> jump-size 1)
888 cur-msg
889 (not (eq cur-msg number)))
890 (cond ((< cur-msg number)
891 (setq jump-size (min (- number cur-msg)
892 (ash (1+ jump-size) -1)))
893 (forward-line jump-size)
894 (setq cur-msg (mh-find-msg-get-num 1)))
896 (setq jump-size (min (- cur-msg number)
897 (ash (1+ jump-size) -1)))
898 (forward-line (- jump-size))
899 (setq cur-msg (mh-find-msg-get-num -1)))))
900 (if (eq cur-msg number)
901 (progn
902 (beginning-of-line)
903 (or dont-show
904 (mh-maybe-show number)
906 (if (not no-error-if-no-message)
907 (error "No message %d" number)))))
910 (defun mh-msg-search-pat (n)
911 ;; Return a search pattern for message N in the scan listing.
912 (format mh-scan-msg-search-regexp n))
915 (defun mh-get-profile-field (field)
916 ;; Find and return the value of FIELD in the current buffer.
917 ;; Returns NIL if the field is not in the buffer.
918 (let ((case-fold-search t))
919 (goto-char (point-min))
920 (cond ((not (re-search-forward (format "^%s" field) nil t)) nil)
921 ((looking-at "[\t ]*$") nil)
923 (re-search-forward "[\t ]*\\([^\t \n].*\\)$" nil t)
924 (let ((start (match-beginning 1)))
925 (end-of-line)
926 (buffer-substring start (point)))))))
928 (defvar mail-user-agent)
929 (defvar read-mail-command)
931 (defvar mh-find-path-run nil
932 "Non-nil if `mh-find-path' has been run already.")
934 (defun mh-find-path ()
935 ;; Set mh-progs, mh-lib, and mh-libs-progs
936 ;; (This step is necessary if MH was installed after this Emacs was dumped.)
937 ;; From profile file, set mh-user-path, mh-draft-folder,
938 ;; mh-unseen-seq, mh-previous-seq, mh-inbox.
939 (mh-find-progs)
940 (unless mh-find-path-run
941 (setq mh-find-path-run t)
942 (setq read-mail-command 'mh-rmail)
943 (setq mail-user-agent 'mh-e-user-agent))
944 (save-excursion
945 ;; Be sure profile is fully expanded before switching buffers
946 (let ((profile (expand-file-name (or (getenv "MH") "~/.mh_profile"))))
947 (set-buffer (get-buffer-create mh-temp-buffer))
948 (setq buffer-offer-save nil) ;for people who set default to t
949 (erase-buffer)
950 (condition-case err
951 (insert-file-contents profile)
952 (file-error
953 (mh-install profile err)))
954 (setq mh-user-path (mh-get-profile-field "Path:"))
955 (if (not mh-user-path)
956 (setq mh-user-path "Mail"))
957 (setq mh-user-path
958 (file-name-as-directory
959 (expand-file-name mh-user-path (expand-file-name "~"))))
960 (setq mh-draft-folder (mh-get-profile-field "Draft-Folder:"))
961 (if mh-draft-folder
962 (progn
963 (if (not (mh-folder-name-p mh-draft-folder))
964 (setq mh-draft-folder (format "+%s" mh-draft-folder)))
965 (if (not (file-exists-p (mh-expand-file-name mh-draft-folder)))
966 (error "Draft folder \"%s\" not found. Create it and try again"
967 (mh-expand-file-name mh-draft-folder)))))
968 (setq mh-inbox (mh-get-profile-field "Inbox:"))
969 (cond ((not mh-inbox)
970 (setq mh-inbox "+inbox"))
971 ((not (mh-folder-name-p mh-inbox))
972 (setq mh-inbox (format "+%s" mh-inbox))))
973 (setq mh-unseen-seq (mh-get-profile-field "Unseen-Sequence:"))
974 (if mh-unseen-seq
975 (setq mh-unseen-seq (intern mh-unseen-seq))
976 (setq mh-unseen-seq 'unseen)) ;old MH default?
977 (setq mh-previous-seq (mh-get-profile-field "Previous-Sequence:"))
978 (if mh-previous-seq
979 (setq mh-previous-seq (intern mh-previous-seq)))
980 (run-hooks 'mh-find-path-hook)))
981 (and mh-auto-folder-collect
982 (let ((mh-no-install t)) ;only get folders if MH installed
983 (condition-case err
984 (mh-make-folder-list-background)
985 (file-error))))) ;so don't complain if not installed
987 (defun mh-file-command-p (file)
988 "Return t if file FILE is the name of a executable regular file."
989 (and (file-regular-p file) (file-executable-p file)))
991 (defun mh-find-progs ()
992 "Find the directories for the installed MH/nmh binaries and config files.
993 Set the `mh-progs' and `mh-lib', and `mh-lib-progs' variables to the
994 directory names and set `mh-nmh-p' if we detect nmh instead of MH."
995 (let ((path (or (mh-path-search exec-path "mhparam")
996 (mh-path-search '("/usr/local/nmh/bin" ; nmh default
997 "/usr/local/bin/mh/"
998 "/usr/local/mh/"
999 "/usr/bin/mh/" ;Ultrix 4.2
1000 "/usr/new/mh/" ;Ultrix <4.2
1001 "/usr/contrib/mh/bin/" ;BSDI
1002 "/usr/pkg/bin/" ; NetBSD
1003 "/usr/local/bin/"
1005 "mhparam"))))
1006 (if (not path)
1007 (error "Unable to find the `mhparam' command"))
1008 (save-excursion
1009 (let ((tmp-buffer (get-buffer-create mh-temp-buffer)))
1010 (set-buffer tmp-buffer)
1011 (unwind-protect
1012 (progn
1013 (call-process (expand-file-name "mhparam" path)
1014 nil '(t nil) nil "libdir" "etcdir")
1015 (goto-char (point-min))
1016 (if (search-forward-regexp "^libdir:\\s-\\(\\S-+\\)\\s-*$" nil t)
1017 (setq mh-lib-progs (match-string 1)
1018 mh-lib mh-lib-progs
1019 mh-progs path))
1020 (goto-char (point-min))
1021 (if (search-forward-regexp "^etcdir:\\s-\\(\\S-+\\)\\s-*$" nil t)
1022 (setq mh-lib (match-string 1)
1023 mh-nmh-p t)))
1024 (kill-buffer tmp-buffer))))
1025 (unless (and mh-progs mh-lib mh-lib-progs)
1026 (error "Unable to determine paths from `mhparam' command"))))
1028 (defun mh-path-search (path file &optional func-p)
1029 ;; Search PATH, a list of directory names, for FILE.
1030 ;; Returns the element of PATH that contains FILE, or nil if not found.
1031 (while (and path
1032 (not (funcall (or func-p 'mh-file-command-p)
1033 (expand-file-name file (car path)))))
1034 (setq path (cdr path)))
1035 (car path))
1037 (defvar mh-no-install nil) ;do not run install-mh
1039 (defun mh-install (profile error-val)
1040 ;; Called to do error recovery if we fail to read the profile file.
1041 ;; If possible, initialize the MH environment.
1042 (if (or (getenv "MH")
1043 (file-exists-p profile)
1044 mh-no-install)
1045 (signal (car error-val)
1046 (list (format "Cannot read MH profile \"%s\"" profile)
1047 (car (cdr (cdr error-val))))))
1048 ;; The "install-mh" command will output a short note which
1049 ;; mh-exec-cmd will display to the user.
1050 ;; The MH 5 version of install-mh might try prompt the user
1051 ;; for information, which would fail here.
1052 (mh-exec-cmd (expand-file-name "install-mh" mh-lib-progs) "-auto")
1053 ;; now try again to read the profile file
1054 (erase-buffer)
1055 (condition-case err
1056 (insert-file-contents profile)
1057 (file-error
1058 (signal (car err) ;re-signal with more specific msg
1059 (list (format "Cannot read MH profile \"%s\"" profile)
1060 (car (cdr (cdr err))))))))
1063 (defun mh-set-folder-modified-p (flag)
1064 ;; Mark current folder as modified or unmodified according to FLAG.
1065 (set-buffer-modified-p flag))
1068 (defun mh-find-seq (name) (assoc name mh-seq-list))
1070 (defun mh-seq-to-msgs (seq)
1071 ;; Return a list of the messages in SEQUENCE.
1072 (mh-seq-msgs (mh-find-seq seq)))
1075 (defun mh-add-msgs-to-seq (msgs seq &optional internal-flag)
1076 ;; Add MESSAGE(s) to the SEQUENCE. If optional FLAG is non-nil, do not mark
1077 ;; the message in the scan listing or inform MH of the addition.
1078 (let ((entry (mh-find-seq seq)))
1079 (if (and msgs (atom msgs)) (setq msgs (list msgs)))
1080 (if (null entry)
1081 (setq mh-seq-list (cons (mh-make-seq seq msgs) mh-seq-list))
1082 (if msgs (setcdr entry (append msgs (mh-seq-msgs entry)))))
1083 (cond ((not internal-flag)
1084 (mh-add-to-sequence seq msgs)
1085 (mh-notate-seq seq mh-note-seq (1+ mh-cmd-note))))))
1087 (defvar mh-folder-hist nil)
1089 (defun mh-prompt-for-folder (prompt default can-create)
1090 ;; Prompt for a folder name with PROMPT. Returns the folder's name as a
1091 ;; string. DEFAULT is used if the folder exists and the user types return.
1092 ;; If the CAN-CREATE flag is t, then a non-existent folder is made.
1093 (if (null default)
1094 (setq default ""))
1095 (let* ((prompt (format "%s folder%s" prompt
1096 (if (equal "" default)
1097 "? "
1098 (format " [%s]? " default))))
1099 read-name folder-name)
1100 (if (null mh-folder-list)
1101 (mh-set-folder-list))
1102 (while (and (setq read-name (completing-read prompt mh-folder-list nil nil
1103 "+" 'mh-folder-hist default))
1104 (equal read-name "")
1105 (equal default "")))
1106 (cond ((or (equal read-name "") (equal read-name "+"))
1107 (setq read-name default))
1108 ((not (mh-folder-name-p read-name))
1109 (setq read-name (format "+%s" read-name))))
1110 (if (or (not read-name) (equal "" read-name))
1111 (error "No folder specified"))
1112 (setq folder-name read-name)
1113 (cond ((and (> (length folder-name) 0)
1114 (eq (aref folder-name (1- (length folder-name))) ?/))
1115 (setq folder-name (substring folder-name 0 -1))))
1116 (let ((new-file-p (not (file-exists-p (mh-expand-file-name folder-name)))))
1117 (cond ((and new-file-p
1118 (y-or-n-p
1119 (format "Folder %s does not exist. Create it? " folder-name)))
1120 (message "Creating %s" folder-name)
1121 (call-process "mkdir" nil nil nil (mh-expand-file-name folder-name))
1122 (message "Creating %s...done" folder-name)
1123 (setq mh-folder-list (cons (list read-name) mh-folder-list))
1124 (run-hooks 'mh-folder-list-change-hook))
1125 (new-file-p
1126 (error "Folder %s is not created" folder-name))
1127 ((not (file-directory-p (mh-expand-file-name folder-name)))
1128 (error "\"%s\" is not a directory"
1129 (mh-expand-file-name folder-name)))
1130 ((and (null (assoc read-name mh-folder-list))
1131 (null (assoc (concat read-name "/") mh-folder-list)))
1132 (setq mh-folder-list (cons (list read-name) mh-folder-list))
1133 (run-hooks 'mh-folder-list-change-hook))))
1134 folder-name))
1137 (defvar mh-make-folder-list-process nil) ;The background process collecting the folder list.
1139 (defvar mh-folder-list-temp nil) ;mh-folder-list as it is being built.
1141 (defvar mh-folder-list-partial-line "") ;Start of last incomplete line from folder process.
1143 (defun mh-set-folder-list ()
1144 ;; Sets mh-folder-list correctly.
1145 ;; A useful function for the command line or for when you need to
1146 ;; sync by hand. Format is in a form suitable for completing read.
1147 (message "Collecting folder names...")
1148 (if (not mh-make-folder-list-process)
1149 (mh-make-folder-list-background))
1150 (while (eq (process-status mh-make-folder-list-process) 'run)
1151 (accept-process-output mh-make-folder-list-process))
1152 (setq mh-folder-list mh-folder-list-temp)
1153 (run-hooks 'mh-folder-list-change-hook)
1154 (setq mh-folder-list-temp nil)
1155 (delete-process mh-make-folder-list-process)
1156 (setq mh-make-folder-list-process nil)
1157 (message "Collecting folder names...done"))
1159 (defun mh-make-folder-list-background ()
1160 ;; Start a background process to compute a list of the user's folders.
1161 ;; Call mh-set-folder-list to wait for the result.
1162 (cond
1163 ((not mh-make-folder-list-process)
1164 (unless mh-inbox
1165 (mh-find-path))
1166 (let ((process-connection-type nil))
1167 (setq mh-make-folder-list-process
1168 (start-process "folders" nil (expand-file-name "folders" mh-progs)
1169 "-fast"
1170 (if mh-recursive-folders
1171 "-recurse"
1172 "-norecurse")))
1173 (set-process-filter mh-make-folder-list-process
1174 'mh-make-folder-list-filter)
1175 (process-kill-without-query mh-make-folder-list-process)))))
1177 (defun mh-make-folder-list-filter (process output)
1178 ;; parse output from "folders -fast"
1179 (let ((position 0)
1180 line-end
1181 new-folder
1182 (prevailing-match-data (match-data)))
1183 (unwind-protect
1184 ;; make sure got complete line
1185 (while (setq line-end (string-match "\n" output position))
1186 (setq new-folder (format "+%s%s"
1187 mh-folder-list-partial-line
1188 (substring output position line-end)))
1189 (setq mh-folder-list-partial-line "")
1190 ;; is new folder a subfolder of previous?
1191 (if (and mh-folder-list-temp
1192 (string-match
1193 (regexp-quote
1194 (concat (car (car mh-folder-list-temp)) "/"))
1195 new-folder))
1196 ;; append slash to parent folder for better completion
1197 ;; (undone by mh-prompt-for-folder)
1198 (setq mh-folder-list-temp
1199 (cons
1200 (list new-folder)
1201 (cons
1202 (list (concat (car (car mh-folder-list-temp)) "/"))
1203 (cdr mh-folder-list-temp))))
1204 (setq mh-folder-list-temp
1205 (cons (list new-folder)
1206 mh-folder-list-temp)))
1207 (setq position (1+ line-end)))
1208 (set-match-data prevailing-match-data))
1209 (setq mh-folder-list-partial-line (substring output position))))
1212 (defun mh-folder-name-p (name)
1213 ;; Return non-NIL if NAME is possibly the name of a folder.
1214 ;; A name (a string or symbol) can be a folder name if it begins with "+".
1215 (if (symbolp name)
1216 (eq (aref (symbol-name name) 0) ?+)
1217 (and (> (length name) 0)
1218 (eq (aref name 0) ?+))))
1221 ;;; Issue commands to MH.
1224 (defun mh-exec-cmd (command &rest args)
1225 ;; Execute mh-command COMMAND with ARGS.
1226 ;; The side effects are what is desired.
1227 ;; Any output is assumed to be an error and is shown to the user.
1228 ;; The output is not read or parsed by mh-e.
1229 (save-excursion
1230 (set-buffer (get-buffer-create mh-temp-buffer))
1231 (erase-buffer)
1232 (apply 'call-process
1233 (expand-file-name command mh-progs) nil t nil
1234 (mh-list-to-string args))
1235 (if (> (buffer-size) 0)
1236 (save-window-excursion
1237 (switch-to-buffer-other-window mh-temp-buffer)
1238 (sit-for 5)))))
1241 (defun mh-exec-cmd-error (env command &rest args)
1242 ;; In environment ENV, execute mh-command COMMAND with args ARGS.
1243 ;; ENV is nil or a string of space-separated "var=value" elements.
1244 ;; Signals an error if process does not complete successfully.
1245 (save-excursion
1246 (set-buffer (get-buffer-create mh-temp-buffer))
1247 (erase-buffer)
1248 (let ((status
1249 (if env
1250 ;; the shell hacks necessary here shows just how broken Unix is
1251 (apply 'call-process "/bin/sh" nil t nil "-c"
1252 (format "%s %s ${1+\"$@\"}"
1254 (expand-file-name command mh-progs))
1255 command
1256 (mh-list-to-string args))
1257 (apply 'call-process
1258 (expand-file-name command mh-progs) nil t nil
1259 (mh-list-to-string args)))))
1260 (mh-handle-process-error command status))))
1263 (defun mh-exec-cmd-daemon (command &rest args)
1264 ;; Execute MH command COMMAND with ARGS in the background.
1265 ;; Any output from command is displayed in an asynchronous pop-up window.
1266 (save-excursion
1267 (set-buffer (get-buffer-create mh-temp-buffer))
1268 (erase-buffer))
1269 (let* ((process-connection-type nil)
1270 (process (apply 'start-process
1271 command nil
1272 (expand-file-name command mh-progs)
1273 (mh-list-to-string args))))
1274 (set-process-filter process 'mh-process-daemon)))
1276 (defun mh-process-daemon (process output)
1277 ;; Process daemon that puts output into a temporary buffer.
1278 (set-buffer (get-buffer-create mh-temp-buffer))
1279 (insert-before-markers output)
1280 (display-buffer mh-temp-buffer))
1283 (defun mh-exec-cmd-quiet (raise-error command &rest args)
1284 ;; Args are RAISE-ERROR, COMMANDS, ARGS....
1285 ;; Execute MH command COMMAND with ARGS. ARGS is a list of strings.
1286 ;; Return at start of mh-temp buffer, where output can be parsed and used.
1287 ;; Returns value of call-process, which is 0 for success,
1288 ;; unless RAISE-ERROR is non-nil, in which case an error is signaled
1289 ;; if call-process returns non-0.
1290 (set-buffer (get-buffer-create mh-temp-buffer))
1291 (erase-buffer)
1292 (let ((value
1293 (apply 'call-process
1294 (expand-file-name command mh-progs) nil t nil
1295 args)))
1296 (goto-char (point-min))
1297 (if raise-error
1298 (mh-handle-process-error command value)
1299 value)))
1302 (defun mh-exec-cmd-output (command display &rest args)
1303 ;; Execute MH command COMMAND with DISPLAY flag and ARGS.
1304 ;; Put the output into buffer after point. Set mark after inserted text.
1305 ;; Output is expected to be shown to user, not parsed by mh-e.
1306 (push-mark (point) t)
1307 (apply 'call-process
1308 (expand-file-name command mh-progs) nil t display
1309 (mh-list-to-string args))
1310 (exchange-point-and-mark))
1313 (defun mh-exec-lib-cmd-output (command &rest args)
1314 ;; Execute MH library command COMMAND with ARGS.
1315 ;; Put the output into buffer after point. Set mark after inserted text.
1316 (apply 'mh-exec-cmd-output (expand-file-name command mh-lib-progs) nil args))
1319 (defun mh-handle-process-error (command status)
1320 ;; Raise error if COMMAND returned non-0 STATUS, otherwise return STATUS.
1321 ;; STATUS is return value from call-process.
1322 ;; Program output is in current buffer.
1323 ;; If output is too long to include in error message, display the buffer.
1324 (cond ((eq status 0) ;success
1325 status)
1326 ((stringp status) ;kill string
1327 (error "%s: %s" command status))
1328 (t ;exit code
1329 (cond
1330 ((= (buffer-size) 0) ;program produced no error message
1331 (error "%s: exit code %d" command status))
1333 ;; will error message fit on one line?
1334 (goto-line 2)
1335 (if (and (< (buffer-size) (frame-width))
1336 (eobp))
1337 (error "%s"
1338 (buffer-substring 1 (progn (goto-char 1)
1339 (end-of-line)
1340 (point))))
1341 (display-buffer (current-buffer))
1342 (error "%s failed with status %d. See error message in other window"
1343 command status)))))))
1346 (defun mh-expand-file-name (filename &optional default)
1347 ;; Just like `expand-file-name', but also handles MH folder names.
1348 ;; Assumes that any filename that starts with '+' is a folder name.
1349 (if (mh-folder-name-p filename)
1350 (expand-file-name (substring filename 1) mh-user-path)
1351 (expand-file-name filename default)))
1354 (defun mh-list-to-string (l)
1355 ;; Flattens the list L and makes every element of the new list into a string.
1356 (nreverse (mh-list-to-string-1 l)))
1358 (defun mh-list-to-string-1 (l)
1359 (let ((new-list nil))
1360 (while l
1361 (cond ((null (car l)))
1362 ((symbolp (car l))
1363 (setq new-list (cons (symbol-name (car l)) new-list)))
1364 ((numberp (car l))
1365 (setq new-list (cons (int-to-string (car l)) new-list)))
1366 ((equal (car l) ""))
1367 ((stringp (car l)) (setq new-list (cons (car l) new-list)))
1368 ((listp (car l))
1369 (setq new-list (nconc (mh-list-to-string-1 (car l))
1370 new-list)))
1371 (t (error "Bad element in mh-list-to-string: %s" (car l))))
1372 (setq l (cdr l)))
1373 new-list))
1375 (provide 'mh-utils)
1377 ;;; mh-utils.el ends here