Some fixes to follow coding conventions.
[bpt/emacs.git] / lisp / mail / mh-utils.el
CommitLineData
c26cf6c8 1;;; mh-utils.el --- mh-e code needed for both sending and reading
60370d40 2;; Time-stamp: <2001-07-15 09:52:59 pavel>
c26cf6c8 3
0c28d842 4;; Copyright (C) 1993, 1995, 1997, 2000 Free Software Foundation, Inc.
c26cf6c8 5
60370d40 6;; This file is part of GNU Emacs.
c26cf6c8 7
9b7bc076 8;; GNU Emacs is free software; you can redistribute it and/or modify
c26cf6c8
RS
9;; it under the terms of the GNU General Public License as published by
10;; the Free Software Foundation; either version 2, or (at your option)
11;; any later version.
12
9b7bc076 13;; GNU Emacs is distributed in the hope that it will be useful,
c26cf6c8
RS
14;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16;; GNU General Public License for more details.
17
18;; You should have received a copy of the GNU General Public License
b578f267
EN
19;; along with GNU Emacs; see the file COPYING. If not, write to the
20;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21;; Boston, MA 02111-1307, USA.
c26cf6c8
RS
22
23;;; Commentary:
24
25;; Internal support for mh-e package.
26
27;;; Code:
28
c919c21a
RS
29;;; Set for local environment:
30;;; mh-progs and mh-lib used to be set in paths.el, which tried to
31;;; figure out at build time which of several possible directories MH
32;;; was installed into. But if you installed MH after building Emacs,
33;;; this would almost certainly be wrong, so now we do it at run time.
c26cf6c8 34
c919c21a
RS
35(defvar mh-progs nil
36 "Directory containing MH commands, such as inc, repl, and rmm.")
c26cf6c8 37
c919c21a
RS
38(defvar mh-lib nil
39 "Directory containing the MH library.
40This directory contains, among other things,
ae3864d7
KH
41the components file.")
42
43(defvar mh-lib-progs nil
44 "Directory containing MH helper programs.
45This directory contains, among other things,
46the mhl program.")
47
48(defvar mh-nmh-p nil
49 "Non-nil if nmh is installed on this system instead of MH")
c26cf6c8 50
b6d4ab05
KH
51;;;###autoload
52(put 'mh-progs 'risky-local-variable t)
53;;;###autoload
54(put 'mh-lib 'risky-local-variable t)
ae3864d7
KH
55;;;###autoload
56(put 'mh-lib-progs 'risky-local-variable t)
57;;;###autoload
58(put 'mh-nmh-p 'risky-local-variable t)
b6d4ab05 59
c919c21a 60;;; User preferences:
c26cf6c8 61
20f0de75
RS
62(defgroup mh-buffer nil
63 "Layout of MH-E buffers"
64 :prefix "mh-"
65 :group 'mh)
66
67
68(defcustom mh-auto-folder-collect t
c26cf6c8
RS
69 "*Whether to start collecting MH folder names immediately in the background.
70Non-nil means start a background process collecting the names of all
20f0de75
RS
71folders as soon as mh-e is loaded."
72 :type 'boolean
73 :group 'mh)
c26cf6c8 74
20f0de75
RS
75(defcustom mh-recursive-folders nil
76 "*If non-nil, then commands which operate on folders do so recursively."
77 :type 'boolean
78 :group 'mh)
c26cf6c8 79
20f0de75 80(defcustom mh-clean-message-header nil
c26cf6c8
RS
81 "*Non-nil means clean headers of messages that are displayed or inserted.
82The variables `mh-visible-headers' and `mh-invisible-headers' control what
20f0de75
RS
83is removed."
84 :type 'boolean
85 :group 'mh-buffer)
c26cf6c8 86
20f0de75 87(defcustom mh-visible-headers nil
c26cf6c8
RS
88 "*If non-nil, contains a regexp specifying the headers to keep when cleaning.
89Only used if `mh-clean-message-header' is non-nil. Setting this variable
20f0de75
RS
90overrides `mh-invisible-headers'."
91 :type '(choice (const nil) regexp)
92 :group 'mh-buffer)
c26cf6c8
RS
93
94(defvar mh-invisible-headers
b6d4ab05 95 "^Received: \\|^Message-Id: \\|^Remailed-\\|^Via: \\|^Mail-from: \\|^Return-Path: \\|^Delivery-Date: \\|^In-Reply-To: \\|^Resent-"
c26cf6c8
RS
96 "Regexp matching lines in a message header that are not to be shown.
97If `mh-visible-headers' is non-nil, it is used instead to specify what
98to keep.")
99
20f0de75
RS
100(defcustom mh-bury-show-buffer t
101 "*Non-nil means that the displayed show buffer for a folder is buried."
102 :type 'boolean
103 :group 'mh-buffer)
c26cf6c8 104
20f0de75
RS
105(defcustom mh-summary-height 4
106 "*Number of lines in MH-Folder window (including the mode line)."
107 :type 'integer
108 :group 'mh-buffer)
c26cf6c8
RS
109
110(defvar mh-msg-number-regexp "^ *\\([0-9]+\\)"
111 "Regexp to find the number of a message in a scan line.
112The message's number must be surrounded with \\( \\)")
113
114(defvar mh-msg-search-regexp "^[^0-9]*%d[^0-9]"
115 "Format string containing a regexp matching the scan listing for a message.
116The desired message's number will be an argument to format.")
117
20f0de75 118(defcustom mhl-formfile nil
c26cf6c8
RS
119 "*Name of format file to be used by mhl to show and print messages.
120A value of T means use the default format file.
121Nil means don't use mhl to format messages when showing; mhl is still used,
122with the default format file, to format messages when printing them.
123The format used should specify a non-zero value for overflowoffset so
20f0de75
RS
124the message continues to conform to RFC 822 and mh-e can parse the headers."
125 :type '(choice (const nil) (const t) string)
126 :group 'mh)
b3470e4c 127(put 'mhl-formfile 'info-file "mh-e")
c26cf6c8 128
b6d4ab05
KH
129(defvar mh-default-folder-for-message-function nil
130 "Function to select a default folder for refiling or Fcc.
131If set to a function, that function is called with no arguments by
132`\\[mh-refile-msg]' and `\\[mh-to-fcc]' to get a default when
133prompting the user for a folder. The function is called from within a
134save-excursion, with point at the start of the message. It should
135return the folder to offer as the refile or Fcc folder, as a string
136with a leading `+' sign. It can also return an empty string to use no
137default, or NIL to calculate the default the usual way.
138NOTE: This variable is not an ordinary hook;
139It may not be a list of functions.")
140
141(defvar mh-find-path-hook nil
142 "Invoked by mh-find-path while reading the user's MH profile.")
c26cf6c8 143
b6d4ab05
KH
144(defvar mh-folder-list-change-hook nil
145 "Invoked whenever the cached folder list `mh-folder-list' is changed.")
146
147(defvar mh-show-buffer-mode-line-buffer-id "{show-%s} %d"
148 "Format string to produce `mode-line-buffer-identification' for show buffers.
149First argument is folder name. Second is message number.")
c26cf6c8
RS
150
151(defvar mh-cmd-note 4
152 "Offset to insert notation.")
153
b6d4ab05
KH
154(defvar mh-note-seq "%"
155 "String whose first character is used to notate messages in a sequence.")
156
157;;; Internal bookkeeping variables:
158
159;; The value of `mh-folder-list-change-hook' is called whenever
160;; mh-folder-list variable is set.
161(defvar mh-folder-list nil) ;List of folder names for completion.
162
163;; Cached value of the `Path:' component in the user's MH profile.
164(defvar mh-user-path nil) ;User's mail folder directory.
c26cf6c8 165
b6d4ab05
KH
166;; An mh-draft-folder of NIL means do not use a draft folder.
167;; Cached value of the `Draft-Folder:' component in the user's MH profile.
168(defvar mh-draft-folder nil) ;Name of folder containing draft messages.
c26cf6c8 169
b6d4ab05
KH
170;; Cached value of the `Unseen-Sequence:' component in the user's MH profile.
171(defvar mh-unseen-seq nil) ;Name of the Unseen sequence.
c26cf6c8 172
b6d4ab05
KH
173;; Cached value of the `Previous-Sequence:' component in the user's MH profile.
174(defvar mh-previous-seq nil) ;Name of the Previous sequence.
c26cf6c8 175
b6d4ab05
KH
176;; Cached value of the `Inbox:' component in the user's MH profile,
177;; or "+inbox" if no such component.
178(defvar mh-inbox nil) ;Name of the Inbox folder.
c26cf6c8 179
b6d4ab05 180(defconst mh-temp-buffer " *mh-temp*") ;Name of mh-e scratch buffer.
c26cf6c8 181
b6d4ab05 182(defvar mh-previous-window-config nil) ;Window configuration before mh-e command.
c26cf6c8 183
b6d4ab05 184;;; Internal variables local to a folder.
c26cf6c8 185
b6d4ab05 186(defvar mh-current-folder nil) ;Name of current folder, a string.
c26cf6c8 187
b6d4ab05 188(defvar mh-show-buffer nil) ;Buffer that displays message for this folder.
c26cf6c8 189
b6d4ab05 190(defvar mh-folder-filename nil) ;Full path of directory for this folder.
b3470e4c
KH
191
192(defvar mh-msg-count nil) ;Number of msgs in buffer.
c26cf6c8 193
b6d4ab05 194(defvar mh-showing nil) ;If non-nil, show the message in a separate window.
c26cf6c8 195
b6d4ab05
KH
196;;; This holds a documentation string used by describe-mode.
197(defun mh-showing ()
198 "When moving to a new message in the Folder window,
199also show it in a separate Show window."
200 nil)
201
202(defvar mh-seq-list nil) ;The sequences of this folder. An alist of (seq . msgs).
203
204(defvar mh-seen-list nil) ;List of displayed messages to be removed from the Unseen sequence.
205
206;; If non-nil, show buffer contains message with all headers.
207;; If nil, show buffer contains message processed normally.
208(defvar mh-showing-with-headers nil) ;Showing message with headers or normally.
c26cf6c8
RS
209
210
c919c21a
RS
211;;; mh-e macros
212
213(defmacro with-mh-folder-updating (save-modification-flag-p &rest body)
214 ;; Format is (with-mh-folder-updating (SAVE-MODIFICATION-FLAG-P) &body BODY).
215 ;; Execute BODY, which can modify the folder buffer without having to
216 ;; worry about file locking or the read-only flag, and return its result.
217 ;; If SAVE-MODIFICATION-FLAG-P is non-nil, the buffer's modification
218 ;; flag is unchanged, otherwise it is cleared.
219 (setq save-modification-flag-p (car save-modification-flag-p)) ; CL style
b787fc05
GM
220 `(prog1
221 (let ((mh-folder-updating-mod-flag (buffer-modified-p))
222 (buffer-read-only nil)
223 (buffer-file-name nil)) ;don't let the buffer get locked
224 (prog1
225 (progn
226 ,@body)
227 (mh-set-folder-modified-p mh-folder-updating-mod-flag)))
228 ,@(if (not save-modification-flag-p)
229 '((mh-set-folder-modified-p nil)))))
c919c21a
RS
230
231(put 'with-mh-folder-updating 'lisp-indent-hook 1)
232
233(defmacro mh-in-show-buffer (show-buffer &rest body)
b6d4ab05 234 ;; Format is (mh-in-show-buffer (SHOW-BUFFER) &body BODY).
c919c21a
RS
235 ;; Display buffer SHOW-BUFFER in other window and execute BODY in it.
236 ;; Stronger than save-excursion, weaker than save-window-excursion.
237 (setq show-buffer (car show-buffer)) ; CL style
b787fc05
GM
238 `(let ((mh-in-show-buffer-saved-window (selected-window)))
239 (switch-to-buffer-other-window ,show-buffer)
240 (if mh-bury-show-buffer (bury-buffer (current-buffer)))
241 (unwind-protect
242 (progn
243 ,@body)
244 (select-window mh-in-show-buffer-saved-window))))
c919c21a
RS
245
246(put 'mh-in-show-buffer 'lisp-indent-hook 1)
247
b6d4ab05
KH
248(defmacro mh-make-seq (name msgs) (list 'cons name msgs))
249
c919c21a
RS
250(defmacro mh-seq-name (pair) (list 'car pair))
251
252(defmacro mh-seq-msgs (pair) (list 'cdr pair))
253
254
c26cf6c8
RS
255;;; Ensure new buffers won't get this mode if default-major-mode is nil.
256(put 'mh-show-mode 'mode-class 'special)
257
258(defun mh-show-mode ()
259 "Major mode for showing messages in mh-e.
260The value of mh-show-mode-hook is called when a new message is displayed."
261 (kill-all-local-variables)
262 (setq major-mode 'mh-show-mode)
263 (mh-set-mode-name "MH-Show")
264 (run-hooks 'mh-show-mode-hook))
265
266
267(defun mh-maybe-show (&optional msg)
268 ;; If in showing mode, then display the message pointed to by the cursor.
269 (if mh-showing (mh-show msg)))
270
b6d4ab05 271(defun mh-show (&optional message)
c919c21a
RS
272 "Show MESSAGE (default: message at cursor).
273Force a two-window display with the folder window on top (size
c26cf6c8 274mh-summary-height) and the show buffer below it.
c919c21a
RS
275If the message is already visible, display the start of the message.
276
277Display of the message is controlled by setting the variables
278`mh-clean-message-header' and `mhl-formfile'. The default behavior is
279to scroll uninteresting headers off the top of the window.
280Type \"\\[mh-header-display]\" to see the message with all its headers."
c26cf6c8
RS
281 (interactive)
282 (and mh-showing-with-headers
283 (or mhl-formfile mh-clean-message-header)
284 (mh-invalidate-show-buffer))
b6d4ab05 285 (mh-show-msg message))
c26cf6c8
RS
286
287
288(defun mh-show-msg (msg)
289 (if (not msg)
290 (setq msg (mh-get-msg-num t)))
291 (setq mh-showing t)
292 (let ((folder mh-current-folder)
293 (clean-message-header mh-clean-message-header)
294 (show-window (get-buffer-window mh-show-buffer)))
056e1e3f 295 (if (not (eq (next-window (minibuffer-window)) (selected-window)))
c26cf6c8
RS
296 (delete-other-windows)) ; force ourself to the top window
297 (mh-in-show-buffer (mh-show-buffer)
298 (if (and show-window
299 (equal (mh-msg-filename msg folder) buffer-file-name))
300 (progn ;just back up to start
301 (goto-char (point-min))
302 (if (not clean-message-header)
303 (mh-start-of-uncleaned-message)))
304 (mh-display-msg msg folder))))
305 (if (not (= (1+ (window-height)) (screen-height))) ;not horizontally split
306 (shrink-window (- (window-height) mh-summary-height)))
307 (mh-recenter nil)
308 (if (not (memq msg mh-seen-list)) (setq mh-seen-list (cons msg mh-seen-list)))
309 (run-hooks 'mh-show-hook))
310
311
312(defun mh-display-msg (msg-num folder)
313 ;; Display message NUMBER of FOLDER.
314 ;; Sets the current buffer to the show buffer.
315 (set-buffer folder)
316 ;; Bind variables in folder buffer in case they are local
317 (let ((formfile mhl-formfile)
318 (clean-message-header mh-clean-message-header)
319 (invisible-headers mh-invisible-headers)
320 (visible-headers mh-visible-headers)
321 (msg-filename (mh-msg-filename msg-num))
322 (show-buffer mh-show-buffer))
323 (if (not (file-exists-p msg-filename))
324 (error "Message %d does not exist" msg-num))
325 (set-buffer show-buffer)
326 (cond ((not (equal msg-filename buffer-file-name))
b6d4ab05 327 (mh-unvisit-file)
c26cf6c8 328 (erase-buffer)
b6d4ab05
KH
329 ;; Changing contents, so this hook needs to be reinitialized.
330 ;; pgp.el uses this.
331 (if (boundp 'write-contents-hooks) ;Emacs 19
c895a248 332 (kill-local-variable 'write-contents-hooks))
c26cf6c8
RS
333 (if formfile
334 (mh-exec-lib-cmd-output "mhl" "-nobell" "-noclear"
335 (if (stringp formfile)
336 (list "-form" formfile))
337 msg-filename)
338 (insert-file-contents msg-filename))
339 (goto-char (point-min))
340 (cond (clean-message-header
341 (mh-clean-msg-header (point-min)
342 invisible-headers
343 visible-headers)
344 (goto-char (point-min)))
345 (t
346 (mh-start-of-uncleaned-message)))
b6d4ab05 347 ;; the parts of visiting we want to do (no locking)
c26cf6c8
RS
348 (or (eq buffer-undo-list t) ;don't save undo info for prev msgs
349 (setq buffer-undo-list nil))
b6d4ab05
KH
350 (set-buffer-modified-p nil)
351 (set-buffer-auto-saved)
352 ;; the parts of set-visited-file-name we want to do (no locking)
c26cf6c8 353 (setq buffer-file-name msg-filename)
b6d4ab05
KH
354 (setq buffer-backed-up nil)
355 (auto-save-mode 1)
c26cf6c8
RS
356 (set-mark nil)
357 (mh-show-mode)
358 (setq mode-line-buffer-identification
359 (list (format mh-show-buffer-mode-line-buffer-id
360 folder msg-num)))
361 (set-buffer folder)
362 (setq mh-showing-with-headers nil)))))
363
364(defun mh-start-of-uncleaned-message ()
365 ;; position uninteresting headers off the top of the window
366 (let ((case-fold-search t))
367 (re-search-forward
b6d4ab05 368 "^To:\\|^Cc:\\|^From:\\|^Subject:\\|^Date:" nil t)
c26cf6c8
RS
369 (beginning-of-line)
370 (mh-recenter 0)))
371
372
373(defun mh-invalidate-show-buffer ()
374 ;; Invalidate the show buffer so we must update it to use it.
375 (if (get-buffer mh-show-buffer)
376 (save-excursion
377 (set-buffer mh-show-buffer)
b6d4ab05
KH
378 (mh-unvisit-file))))
379
c26cf6c8 380
b6d4ab05
KH
381(defun mh-unvisit-file ()
382 ;; Separate current buffer from the message file it was visiting.
383 (or (not (buffer-modified-p))
384 (null buffer-file-name) ;we've been here before
385 (yes-or-no-p (format "Message %s modified; flush changes? "
386 (file-name-nondirectory buffer-file-name)))
387 (error "Flushing changes not confirmed"))
388 (clear-visited-file-modtime)
389 (unlock-buffer)
390 (setq buffer-file-name nil))
c26cf6c8 391
b6d4ab05 392
c26cf6c8
RS
393(defun mh-get-msg-num (error-if-no-message)
394 ;; Return the message number of the displayed message. If the argument
395 ;; ERROR-IF-NO-MESSAGE is non-nil, then complain if the cursor is not
396 ;; pointing to a message.
397 (save-excursion
398 (beginning-of-line)
399 (cond ((looking-at mh-msg-number-regexp)
400 (string-to-int (buffer-substring (match-beginning 1)
401 (match-end 1))))
402 (error-if-no-message
403 (error "Cursor not pointing to message"))
404 (t nil))))
405
406
407(defun mh-msg-filename (msg &optional folder)
408 ;; Return the file name of MESSAGE in FOLDER (default current folder).
409 (expand-file-name (int-to-string msg)
410 (if folder
411 (mh-expand-file-name folder)
412 mh-folder-filename)))
413
414
415(defun mh-clean-msg-header (start invisible-headers visible-headers)
416 ;; Flush extraneous lines in a message header, from the given POINT to the
417 ;; end of the message header. If VISIBLE-HEADERS is non-nil, it contains a
418 ;; regular expression specifying the lines to display, otherwise
419 ;; INVISIBLE-HEADERS contains a regular expression specifying lines to
420 ;; delete from the header.
421 (let ((case-fold-search t))
422 (save-restriction
423 (goto-char start)
424 (if (search-forward "\n\n" nil 'move)
425 (backward-char 1))
426 (narrow-to-region start (point))
427 (goto-char (point-min))
428 (if visible-headers
429 (while (< (point) (point-max))
430 (cond ((looking-at visible-headers)
431 (forward-line 1)
432 (while (looking-at "[ \t]") (forward-line 1)))
433 (t
434 (mh-delete-line 1)
435 (while (looking-at "[ \t]")
436 (mh-delete-line 1)))))
437 (while (re-search-forward invisible-headers nil t)
438 (beginning-of-line)
439 (mh-delete-line 1)
440 (while (looking-at "[ \t]")
441 (mh-delete-line 1))))
442 (unlock-buffer))))
443
444
445(defun mh-recenter (arg)
446 ;; Like recenter but with two improvements: nil arg means recenter,
447 ;; and only does anything if the current buffer is in the selected
448 ;; window. (Commands like save-some-buffers can make this false.)
056e1e3f
RS
449 (if (eq (get-buffer-window (current-buffer))
450 (selected-window))
c26cf6c8
RS
451 (recenter (if arg arg '(t)))))
452
453
454(defun mh-delete-line (lines)
455 ;; Delete version of kill-line.
b3470e4c 456 (delete-region (point) (progn (forward-line lines) (point))))
c26cf6c8
RS
457
458
c26cf6c8
RS
459(defun mh-notate (msg notation offset)
460 ;; Marks MESSAGE with the character NOTATION at position OFFSET.
461 ;; Null MESSAGE means the message that the cursor points to.
462 (save-excursion
463 (if (or (null msg)
464 (mh-goto-msg msg t t))
465 (with-mh-folder-updating (t)
466 (beginning-of-line)
467 (forward-char offset)
468 (delete-char 1)
469 (insert notation)))))
470
471
b3470e4c
KH
472(defun mh-find-msg-get-num (step)
473 ;; Return the message number of the message on the current scan line
474 ;; or one nearby. Jumps over non-message lines, such as inc errors.
475 ;; STEP tells whether to search forward or backward if we have to search.
476 (or (mh-get-msg-num nil)
477 (let ((msg-num nil)
478 (nreverses 0))
479 (while (and (not msg-num)
480 (< nreverses 2))
481 (cond ((eobp)
482 (setq step -1)
483 (setq nreverses (1+ nreverses)))
484 ((bobp)
485 (setq step 1)
486 (setq nreverses (1+ nreverses))))
487 (forward-line step)
488 (setq msg-num (mh-get-msg-num nil)))
489 msg-num)))
490
c26cf6c8
RS
491(defun mh-goto-msg (number &optional no-error-if-no-message dont-show)
492 "Position the cursor at message NUMBER.
b6d4ab05 493Optional non-nil second argument means return nil instead of
b3470e4c
KH
494signaling an error if message does not exist; in this case,
495the cursor is positioned near where the message would have been.
b6d4ab05
KH
496Non-nil third argument means not to show the message."
497 (interactive "NGo to message: ")
498 (setq number (prefix-numeric-value number)) ;Emacs 19
b3470e4c
KH
499 ;; This basic routine tries to be as fast as possible,
500 ;; using a binary search and minimal regexps.
501 (let ((cur-msg (mh-find-msg-get-num -1))
502 (jump-size mh-msg-count))
503 (while (and (> jump-size 1)
504 cur-msg
505 (not (eq cur-msg number)))
506 (cond ((< cur-msg number)
507 (setq jump-size (min (- number cur-msg)
508 (ash (1+ jump-size) -1)))
509 (forward-line jump-size)
510 (setq cur-msg (mh-find-msg-get-num 1)))
511 (t
512 (setq jump-size (min (- cur-msg number)
513 (ash (1+ jump-size) -1)))
514 (forward-line (- jump-size))
515 (setq cur-msg (mh-find-msg-get-num -1)))))
516 (if (eq cur-msg number)
517 (progn
518 (beginning-of-line)
519 (or dont-show
520 (mh-maybe-show number)
521 t))
522 (if (not no-error-if-no-message)
523 (error "No message %d" number)))))
524
c26cf6c8
RS
525
526(defun mh-msg-search-pat (n)
527 ;; Return a search pattern for message N in the scan listing.
528 (format mh-msg-search-regexp n))
529
530
b6d4ab05
KH
531(defun mh-get-profile-field (field)
532 ;; Find and return the value of FIELD in the current buffer.
533 ;; Returns NIL if the field is not in the buffer.
534 (let ((case-fold-search t))
535 (goto-char (point-min))
536 (cond ((not (re-search-forward (format "^%s" field) nil t)) nil)
537 ((looking-at "[\t ]*$") nil)
538 (t
539 (re-search-forward "[\t ]*\\([^\t \n].*\\)$" nil t)
540 (let ((start (match-beginning 1)))
541 (end-of-line)
542 (buffer-substring start (point)))))))
543
7c3b9c62 544(defvar mail-user-agent)
0c28d842 545(defvar read-mail-command)
7c3b9c62
RS
546
547(defvar mh-find-path-run nil
548 "Non-nil if `mh-find-path' has been run already.")
b6d4ab05 549
c26cf6c8 550(defun mh-find-path ()
ae3864d7 551 ;; Set mh-progs, mh-lib, and mh-libs-progs
c26cf6c8 552 ;; (This step is necessary if MH was installed after this Emacs was dumped.)
b6d4ab05
KH
553 ;; From profile file, set mh-user-path, mh-draft-folder,
554 ;; mh-unseen-seq, mh-previous-seq, mh-inbox.
c26cf6c8 555 (mh-find-progs)
7c3b9c62
RS
556 (unless mh-find-path-run
557 (setq mh-find-path-run t)
0c28d842 558 (setq read-mail-command 'mh-rmail)
7c3b9c62 559 (setq mail-user-agent 'mh-e-user-agent))
c26cf6c8
RS
560 (save-excursion
561 ;; Be sure profile is fully expanded before switching buffers
562 (let ((profile (expand-file-name (or (getenv "MH") "~/.mh_profile"))))
b6d4ab05 563 (set-buffer (get-buffer-create mh-temp-buffer))
c26cf6c8
RS
564 (setq buffer-offer-save nil) ;for people who set default to t
565 (erase-buffer)
566 (condition-case err
567 (insert-file-contents profile)
568 (file-error
569 (mh-install profile err)))
b6d4ab05
KH
570 (setq mh-user-path (mh-get-profile-field "Path:"))
571 (if (not mh-user-path)
c26cf6c8
RS
572 (setq mh-user-path "Mail"))
573 (setq mh-user-path
574 (file-name-as-directory
575 (expand-file-name mh-user-path (expand-file-name "~"))))
b6d4ab05
KH
576 (setq mh-draft-folder (mh-get-profile-field "Draft-Folder:"))
577 (if mh-draft-folder
578 (progn
579 (if (not (mh-folder-name-p mh-draft-folder))
580 (setq mh-draft-folder (format "+%s" mh-draft-folder)))
581 (if (not (file-exists-p (mh-expand-file-name mh-draft-folder)))
60370d40 582 (error "Draft folder \"%s\" not found. Create it and try again"
b6d4ab05
KH
583 (mh-expand-file-name mh-draft-folder)))))
584 (setq mh-inbox (mh-get-profile-field "Inbox:"))
585 (cond ((not mh-inbox)
586 (setq mh-inbox "+inbox"))
587 ((not (mh-folder-name-p mh-inbox))
588 (setq mh-inbox (format "+%s" mh-inbox))))
589 (setq mh-unseen-seq (mh-get-profile-field "Unseen-Sequence:"))
590 (if mh-unseen-seq
591 (setq mh-unseen-seq (intern mh-unseen-seq))
592 (setq mh-unseen-seq 'unseen)) ;old MH default?
593 (setq mh-previous-seq (mh-get-profile-field "Previous-Sequence:"))
594 (if mh-previous-seq
595 (setq mh-previous-seq (intern mh-previous-seq)))
eaff57bb
RS
596 (run-hooks 'mh-find-path-hook)))
597 (and mh-auto-folder-collect
598 (let ((mh-no-install t)) ;only get folders if MH installed
599 (condition-case err
600 (mh-make-folder-list-background)
601 (file-error))))) ;so don't complain if not installed
c26cf6c8 602
f209429d
RS
603(defun mh-file-command-p (file)
604 "Return t if file FILE is the name of a executable regular file."
605 (and (file-regular-p file) (file-executable-p file)))
606
c26cf6c8 607(defun mh-find-progs ()
f209429d 608 "Find the `inc' and `mhl' programs of MH.
ae3864d7
KH
609Set the `mh-progs' and `mh-lib', and `mh-lib-progs' variables to the
610directory names."
f209429d 611 (or (and mh-progs (mh-file-command-p (expand-file-name "inc" mh-progs)))
c26cf6c8
RS
612 (setq mh-progs
613 (or (mh-path-search exec-path "inc")
c919c21a
RS
614 (mh-path-search '("/usr/local/bin/mh/"
615 "/usr/local/mh/"
616 "/usr/bin/mh/" ;Ultrix 4.2
c26cf6c8 617 "/usr/new/mh/" ;Ultrix <4.2
b6d4ab05 618 "/usr/contrib/mh/bin/" ;BSDI
ae3864d7 619 "/usr/pkg/bin/" ; NetBSD
b6d4ab05 620 "/usr/local/bin/"
c919c21a 621 )
f209429d 622 "inc"))))
ae3864d7
KH
623 (or (null mh-progs)
624 (let ((mh-base mh-progs))
625 (while (let ((dir-name (file-name-nondirectory
626 (directory-file-name mh-base))))
627 (or (string= "mh" dir-name)
628 (string= "bin" dir-name)))
629 (setq mh-base
630 (file-name-directory (directory-file-name mh-base))))
631 (or (and mh-lib
632 (file-exists-p (expand-file-name "components" mh-lib)))
633 (setq mh-lib
634 ;; Look for a lib directory roughly parallel to the bin
635 ;; directory: Strip any trailing `mh' or `bin' path
636 ;; components, then look for lib/mh or mh/lib.
637 (or (mh-path-search
539eedec 638 (mapcar (lambda (p) (expand-file-name p mh-base))
38246e2a 639 '("lib/mh" "etc/nmh" "/etc/nmh" "mh/lib" "etc"))
ae3864d7
KH
640 "components"
641 'file-exists-p))))
642 (or (and mh-lib-progs
643 (mh-file-command-p (expand-file-name "mhl" mh-lib-progs)))
644 (setq mh-lib-progs
645 (or (mh-path-search
539eedec
RS
646 (mapcar (lambda (p) (expand-file-name p mh-base))
647 '("lib/mh" "libexec/nmh" "lib/nmh" "mh/lib"))
ae3864d7
KH
648 "mhl")
649 (mh-path-search '("/usr/local/bin/mh/") "mhl")
650 (mh-path-search exec-path "mhl") ;unlikely
651 )))))
652 (unless (and mh-progs mh-lib mh-lib-progs)
653 (error "Cannot find the commands `inc' and `mhl' and the file `components'"))
654 (setq mh-nmh-p (not (null
655 (or (string-match "nmh" mh-lib-progs)
656 (string-match "nmh" mh-lib))))))
657
658(defun mh-path-search (path file &optional func-p)
c26cf6c8
RS
659 ;; Search PATH, a list of directory names, for FILE.
660 ;; Returns the element of PATH that contains FILE, or nil if not found.
661 (while (and path
ae3864d7
KH
662 (not (funcall (or func-p 'mh-file-command-p)
663 (expand-file-name file (car path)))))
c26cf6c8
RS
664 (setq path (cdr path)))
665 (car path))
666
b3470e4c
KH
667(defvar mh-no-install nil) ;do not run install-mh
668
c26cf6c8
RS
669(defun mh-install (profile error-val)
670 ;; Called to do error recovery if we fail to read the profile file.
671 ;; If possible, initialize the MH environment.
672 (if (or (getenv "MH")
b3470e4c
KH
673 (file-exists-p profile)
674 mh-no-install)
675 (signal (car error-val)
676 (list (format "Cannot read MH profile \"%s\"" profile)
677 (car (cdr (cdr error-val))))))
c26cf6c8
RS
678 ;; The "install-mh" command will output a short note which
679 ;; mh-exec-cmd will display to the user.
b6d4ab05
KH
680 ;; The MH 5 version of install-mh might try prompt the user
681 ;; for information, which would fail here.
ae3864d7 682 (mh-exec-cmd (expand-file-name "install-mh" mh-lib-progs) "-auto")
c26cf6c8
RS
683 ;; now try again to read the profile file
684 (erase-buffer)
685 (condition-case err
686 (insert-file-contents profile)
687 (file-error
b3470e4c
KH
688 (signal (car err) ;re-signal with more specific msg
689 (list (format "Cannot read MH profile \"%s\"" profile)
690 (car (cdr (cdr err))))))))
c26cf6c8
RS
691
692
693(defun mh-set-folder-modified-p (flag)
b6d4ab05 694 ;; Mark current folder as modified or unmodified according to FLAG.
c26cf6c8
RS
695 (set-buffer-modified-p flag))
696
697
698(defun mh-find-seq (name) (assoc name mh-seq-list))
699
c26cf6c8 700(defun mh-seq-to-msgs (seq)
b6d4ab05 701 ;; Return a list of the messages in SEQUENCE.
c26cf6c8
RS
702 (mh-seq-msgs (mh-find-seq seq)))
703
704
705(defun mh-add-msgs-to-seq (msgs seq &optional internal-flag)
706 ;; Add MESSAGE(s) to the SEQUENCE. If optional FLAG is non-nil, do not mark
707 ;; the message in the scan listing or inform MH of the addition.
708 (let ((entry (mh-find-seq seq)))
709 (if (and msgs (atom msgs)) (setq msgs (list msgs)))
710 (if (null entry)
711 (setq mh-seq-list (cons (mh-make-seq seq msgs) mh-seq-list))
b6d4ab05 712 (if msgs (setcdr entry (append msgs (mh-seq-msgs entry)))))
c26cf6c8
RS
713 (cond ((not internal-flag)
714 (mh-add-to-sequence seq msgs)
b6d4ab05 715 (mh-notate-seq seq mh-note-seq (1+ mh-cmd-note))))))
c26cf6c8
RS
716
717(autoload 'mh-add-to-sequence "mh-seq")
718(autoload 'mh-notate-seq "mh-seq")
719(autoload 'mh-read-seq-default "mh-seq")
720(autoload 'mh-map-to-seq-msgs "mh-seq")
721
722
723(defun mh-set-mode-name (mode-name-string)
724 ;; Set the mode-name and ensure that the mode line is updated.
725 (setq mode-name mode-name-string)
c80ababd 726 (force-mode-line-update t))
c26cf6c8 727
9832760a 728(defvar mh-folder-hist nil)
c26cf6c8
RS
729
730(defun mh-prompt-for-folder (prompt default can-create)
731 ;; Prompt for a folder name with PROMPT. Returns the folder's name as a
732 ;; string. DEFAULT is used if the folder exists and the user types return.
733 ;; If the CAN-CREATE flag is t, then a non-existent folder is made.
734 (if (null default)
735 (setq default ""))
736 (let* ((prompt (format "%s folder%s" prompt
737 (if (equal "" default)
738 "? "
739 (format " [%s]? " default))))
740 read-name folder-name)
741 (if (null mh-folder-list)
742 (mh-set-folder-list))
9832760a
SM
743 (while (and (setq read-name (completing-read prompt mh-folder-list nil nil
744 "+" 'mh-folder-hist default))
c26cf6c8
RS
745 (equal read-name "")
746 (equal default "")))
747 (cond ((or (equal read-name "") (equal read-name "+"))
748 (setq read-name default))
749 ((not (mh-folder-name-p read-name))
750 (setq read-name (format "+%s" read-name))))
751 (setq folder-name read-name)
752 (cond ((and (> (length folder-name) 0)
056e1e3f 753 (eq (aref folder-name (1- (length folder-name))) ?/))
c26cf6c8
RS
754 (setq folder-name (substring folder-name 0 -1))))
755 (let ((new-file-p (not (file-exists-p (mh-expand-file-name folder-name)))))
756 (cond ((and new-file-p
757 (y-or-n-p
758 (format "Folder %s does not exist. Create it? " folder-name)))
759 (message "Creating %s" folder-name)
760 (call-process "mkdir" nil nil nil (mh-expand-file-name folder-name))
761 (message "Creating %s...done" folder-name)
b6d4ab05
KH
762 (setq mh-folder-list (cons (list read-name) mh-folder-list))
763 (run-hooks 'mh-folder-list-change-hook))
c26cf6c8
RS
764 (new-file-p
765 (error "Folder %s is not created" folder-name))
b3470e4c
KH
766 ((not (file-directory-p (mh-expand-file-name folder-name)))
767 (error "\"%s\" is not a directory"
768 (mh-expand-file-name folder-name)))
c26cf6c8
RS
769 ((and (null (assoc read-name mh-folder-list))
770 (null (assoc (concat read-name "/") mh-folder-list)))
b6d4ab05
KH
771 (setq mh-folder-list (cons (list read-name) mh-folder-list))
772 (run-hooks 'mh-folder-list-change-hook))))
c26cf6c8
RS
773 folder-name))
774
775
b6d4ab05 776(defvar mh-make-folder-list-process nil) ;The background process collecting the folder list.
c26cf6c8 777
b6d4ab05 778(defvar mh-folder-list-temp nil) ;mh-folder-list as it is being built.
c26cf6c8 779
b6d4ab05 780(defvar mh-folder-list-partial-line "") ;Start of last incomplete line from folder process.
c26cf6c8
RS
781
782(defun mh-set-folder-list ()
b6d4ab05
KH
783 ;; Sets mh-folder-list correctly.
784 ;; A useful function for the command line or for when you need to
785 ;; sync by hand. Format is in a form suitable for completing read.
c26cf6c8
RS
786 (message "Collecting folder names...")
787 (if (not mh-make-folder-list-process)
788 (mh-make-folder-list-background))
789 (while (eq (process-status mh-make-folder-list-process) 'run)
790 (accept-process-output mh-make-folder-list-process))
791 (setq mh-folder-list mh-folder-list-temp)
b6d4ab05 792 (run-hooks 'mh-folder-list-change-hook)
c26cf6c8
RS
793 (setq mh-folder-list-temp nil)
794 (delete-process mh-make-folder-list-process)
795 (setq mh-make-folder-list-process nil)
796 (message "Collecting folder names...done"))
797
798(defun mh-make-folder-list-background ()
b6d4ab05
KH
799 ;; Start a background process to compute a list of the user's folders.
800 ;; Call mh-set-folder-list to wait for the result.
c26cf6c8
RS
801 (cond
802 ((not mh-make-folder-list-process)
eaff57bb
RS
803 (unless mh-inbox
804 (mh-find-path))
c26cf6c8
RS
805 (let ((process-connection-type nil))
806 (setq mh-make-folder-list-process
807 (start-process "folders" nil (expand-file-name "folders" mh-progs)
808 "-fast"
809 (if mh-recursive-folders
810 "-recurse"
811 "-norecurse")))
812 (set-process-filter mh-make-folder-list-process
813 'mh-make-folder-list-filter)
814 (process-kill-without-query mh-make-folder-list-process)))))
815
816(defun mh-make-folder-list-filter (process output)
817 ;; parse output from "folders -fast"
818 (let ((position 0)
b3470e4c
KH
819 line-end
820 new-folder
821 (prevailing-match-data (match-data)))
822 (unwind-protect
823 ;; make sure got complete line
824 (while (setq line-end (string-match "\n" output position))
825 (setq new-folder (format "+%s%s"
826 mh-folder-list-partial-line
827 (substring output position line-end)))
828 (setq mh-folder-list-partial-line "")
829 ;; is new folder a subfolder of previous?
830 (if (and mh-folder-list-temp
831 (string-match
832 (regexp-quote
833 (concat (car (car mh-folder-list-temp)) "/"))
834 new-folder))
835 ;; append slash to parent folder for better completion
836 ;; (undone by mh-prompt-for-folder)
837 (setq mh-folder-list-temp
838 (cons
839 (list new-folder)
840 (cons
841 (list (concat (car (car mh-folder-list-temp)) "/"))
842 (cdr mh-folder-list-temp))))
c26cf6c8
RS
843 (setq mh-folder-list-temp
844 (cons (list new-folder)
b3470e4c
KH
845 mh-folder-list-temp)))
846 (setq position (1+ line-end)))
f5399335 847 (set-match-data prevailing-match-data))
c26cf6c8
RS
848 (setq mh-folder-list-partial-line (substring output position))))
849
850
851(defun mh-folder-name-p (name)
852 ;; Return non-NIL if NAME is possibly the name of a folder.
853 ;; A name (a string or symbol) can be a folder name if it begins with "+".
854 (if (symbolp name)
056e1e3f 855 (eq (aref (symbol-name name) 0) ?+)
c26cf6c8 856 (and (> (length name) 0)
056e1e3f 857 (eq (aref name 0) ?+))))
c26cf6c8
RS
858
859
860;;; Issue commands to MH.
861
862
863(defun mh-exec-cmd (command &rest args)
864 ;; Execute mh-command COMMAND with ARGS.
b6d4ab05 865 ;; The side effects are what is desired.
c26cf6c8 866 ;; Any output is assumed to be an error and is shown to the user.
b6d4ab05 867 ;; The output is not read or parsed by mh-e.
c26cf6c8 868 (save-excursion
b6d4ab05 869 (set-buffer (get-buffer-create mh-temp-buffer))
c26cf6c8
RS
870 (erase-buffer)
871 (apply 'call-process
872 (expand-file-name command mh-progs) nil t nil
873 (mh-list-to-string args))
874 (if (> (buffer-size) 0)
875 (save-window-excursion
b6d4ab05 876 (switch-to-buffer-other-window mh-temp-buffer)
c26cf6c8
RS
877 (sit-for 5)))))
878
879
880(defun mh-exec-cmd-error (env command &rest args)
881 ;; In environment ENV, execute mh-command COMMAND with args ARGS.
882 ;; ENV is nil or a string of space-separated "var=value" elements.
883 ;; Signals an error if process does not complete successfully.
884 (save-excursion
b6d4ab05 885 (set-buffer (get-buffer-create mh-temp-buffer))
c26cf6c8
RS
886 (erase-buffer)
887 (let ((status
888 (if env
889 ;; the shell hacks necessary here shows just how broken Unix is
890 (apply 'call-process "/bin/sh" nil t nil "-c"
891 (format "%s %s ${1+\"$@\"}"
892 env
893 (expand-file-name command mh-progs))
894 command
895 (mh-list-to-string args))
896 (apply 'call-process
897 (expand-file-name command mh-progs) nil t nil
898 (mh-list-to-string args)))))
899 (mh-handle-process-error command status))))
900
901
902(defun mh-exec-cmd-daemon (command &rest args)
b6d4ab05
KH
903 ;; Execute MH command COMMAND with ARGS in the background.
904 ;; Any output from command is displayed in an asynchronous pop-up window.
c26cf6c8 905 (save-excursion
b6d4ab05 906 (set-buffer (get-buffer-create mh-temp-buffer))
c26cf6c8
RS
907 (erase-buffer))
908 (let* ((process-connection-type nil)
909 (process (apply 'start-process
910 command nil
911 (expand-file-name command mh-progs)
912 (mh-list-to-string args))))
913 (set-process-filter process 'mh-process-daemon)))
914
915(defun mh-process-daemon (process output)
916 ;; Process daemon that puts output into a temporary buffer.
b6d4ab05 917 (set-buffer (get-buffer-create mh-temp-buffer))
c26cf6c8 918 (insert-before-markers output)
b6d4ab05 919 (display-buffer mh-temp-buffer))
c26cf6c8
RS
920
921
922(defun mh-exec-cmd-quiet (raise-error command &rest args)
923 ;; Args are RAISE-ERROR, COMMANDS, ARGS....
924 ;; Execute MH command COMMAND with ARGS. ARGS is a list of strings.
925 ;; Return at start of mh-temp buffer, where output can be parsed and used.
926 ;; Returns value of call-process, which is 0 for success,
927 ;; unless RAISE-ERROR is non-nil, in which case an error is signaled
928 ;; if call-process returns non-0.
b6d4ab05 929 (set-buffer (get-buffer-create mh-temp-buffer))
c26cf6c8
RS
930 (erase-buffer)
931 (let ((value
932 (apply 'call-process
933 (expand-file-name command mh-progs) nil t nil
934 args)))
935 (goto-char (point-min))
936 (if raise-error
937 (mh-handle-process-error command value)
938 value)))
939
940
941(defun mh-exec-cmd-output (command display &rest args)
942 ;; Execute MH command COMMAND with DISPLAY flag and ARGS.
943 ;; Put the output into buffer after point. Set mark after inserted text.
b6d4ab05 944 ;; Output is expected to be shown to user, not parsed by mh-e.
c26cf6c8
RS
945 (push-mark (point) t)
946 (apply 'call-process
947 (expand-file-name command mh-progs) nil t display
948 (mh-list-to-string args))
949 (exchange-point-and-mark))
950
951
952(defun mh-exec-lib-cmd-output (command &rest args)
953 ;; Execute MH library command COMMAND with ARGS.
954 ;; Put the output into buffer after point. Set mark after inserted text.
ae3864d7 955 (apply 'mh-exec-cmd-output (expand-file-name command mh-lib-progs) nil args))
c26cf6c8
RS
956
957
958(defun mh-handle-process-error (command status)
959 ;; Raise error if COMMAND returned non-0 STATUS, otherwise return STATUS.
960 ;; STATUS is return value from call-process.
961 ;; Program output is in current buffer.
b6d4ab05 962 ;; If output is too long to include in error message, display the buffer.
056e1e3f 963 (cond ((eq status 0) ;success
c26cf6c8
RS
964 status)
965 ((stringp status) ;kill string
52304255 966 (error "%s: %s" command status))
c26cf6c8
RS
967 (t ;exit code
968 (cond
969 ((= (buffer-size) 0) ;program produced no error message
52304255 970 (error "%s: exit code %d" command status))
c26cf6c8
RS
971 (t
972 ;; will error message fit on one line?
973 (goto-line 2)
974 (if (and (< (buffer-size) (screen-width))
975 (eobp))
52304255
KH
976 (error "%s"
977 (buffer-substring 1 (progn (goto-char 1)
c26cf6c8
RS
978 (end-of-line)
979 (point))))
980 (display-buffer (current-buffer))
60370d40 981 (error "%s failed with status %d. See error message in other window"
52304255 982 command status)))))))
c26cf6c8
RS
983
984
985(defun mh-expand-file-name (filename &optional default)
b6d4ab05
KH
986 ;; Just like `expand-file-name', but also handles MH folder names.
987 ;; Assumes that any filename that starts with '+' is a folder name.
c26cf6c8
RS
988 (if (mh-folder-name-p filename)
989 (expand-file-name (substring filename 1) mh-user-path)
990 (expand-file-name filename default)))
991
992
993(defun mh-list-to-string (l)
994 ;; Flattens the list L and makes every element of the new list into a string.
995 (nreverse (mh-list-to-string-1 l)))
996
997(defun mh-list-to-string-1 (l)
998 (let ((new-list nil))
999 (while l
1000 (cond ((null (car l)))
1001 ((symbolp (car l))
1002 (setq new-list (cons (symbol-name (car l)) new-list)))
1003 ((numberp (car l))
1004 (setq new-list (cons (int-to-string (car l)) new-list)))
1005 ((equal (car l) ""))
1006 ((stringp (car l)) (setq new-list (cons (car l) new-list)))
1007 ((listp (car l))
1008 (setq new-list (nconc (mh-list-to-string-1 (car l))
1009 new-list)))
1010 (t (error "Bad element in mh-list-to-string: %s" (car l))))
1011 (setq l (cdr l)))
1012 new-list))
1013
1014(provide 'mh-utils)
1015
c26cf6c8 1016;;; mh-utils.el ends here