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