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