1 ;;; mh-utils.el --- mh-e code needed for both sending and reading
2 ;; Time-stamp: <94/04/11 20:56:35 gildea>
4 ;; Copyright 1993 Free Software Foundation, Inc.
6 ;; This file is part of mh-e.
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)
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.
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.
24 ;; Internal support for mh-e package.
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.
35 "Directory containing MH commands, such as inc, repl, and rmm.")
38 "Directory containing the MH library.
39 This directory contains, among other things,
40 the mhl program and the components file.")
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.")
49 (defvar mh-recursive-folders nil
50 "*If non-nil, then commands which operate on folders do so recursively.")
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
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'.")
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
68 (defvar mh-bury-show-buffer t
69 "*Non-nil means that the displayed show buffer for a folder is buried.")
71 (defvar mh-summary-height
4
72 "*Number of lines in MH-Folder window (including the mode line).")
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 \\( \\)")
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.")
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.")
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.")
99 "Offset to insert notation.")
101 (defvar mh-folder-list nil
102 "List of folder names for completion.")
104 (defvar mh-user-path nil
105 "User's mail folder directory.")
107 (defvar mh-draft-folder nil
108 "Name of folder containing draft messages.
109 NIL means do not use draft folder.")
111 (defvar mh-previous-window-config nil
112 "Window configuration before mh-e command.")
114 (defvar mh-current-folder nil
115 "Name of current folder, a string.")
117 (defvar mh-folder-filename nil
118 "Full path of directory for this folder.")
120 (defvar mh-show-buffer nil
121 "Buffer that displays mesage for this folder.")
123 (defvar mh-unseen-seq nil
124 "Name of the Unseen sequence.")
126 (defvar mh-previous-seq nil
127 "Name of the Previous sequence.")
129 (defvar mh-seen-list nil
130 "List of displayed messages.")
132 (defvar mh-seq-list nil
133 "Alist of (seq . msgs) numbers.")
135 (defvar mh-showing nil
136 "If non-nil, show the message in a separate window.")
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.")
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
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
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
)))))))
163 (put 'with-mh-folder-updating
'lisp-indent-hook
1)
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)))
176 (select-window mh-in-show-buffer-saved-window
)))))
178 (put 'mh-in-show-buffer
'lisp-indent-hook
1)
180 (defmacro mh-seq-name
(pair) (list 'car pair
))
182 (defmacro mh-seq-msgs
(pair) (list 'cdr pair
))
185 ;;; Ensure new buffers won't get this mode if default-major-mode is nil.
186 (put 'mh-show-mode
'mode-class
'special
)
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
))
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
)))
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.
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."
212 (and mh-showing-with-headers
213 (or mhl-formfile mh-clean-message-header
)
214 (mh-invalidate-show-buffer))
218 (defun mh-show-msg (msg)
220 (setq msg
(mh-get-msg-num 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)
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
)))
238 (if (not (memq msg mh-seen-list
)) (setq mh-seen-list
(cons msg mh-seen-list
)))
239 (run-hooks 'mh-show-hook
))
242 (defun mh-display-msg (msg-num folder
)
243 ;; Display message NUMBER of FOLDER.
244 ;; Sets the current buffer to the show buffer.
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)
260 (setq buffer-file-name nil
) ; no locking during setup
263 (mh-exec-lib-cmd-output "mhl" "-nobell" "-noclear"
264 (if (stringp formfile
)
265 (list "-form" formfile
))
267 (insert-file-contents msg-filename
))
268 (goto-char (point-min))
269 (cond (clean-message-header
270 (mh-clean-msg-header (point-min)
273 (goto-char (point-min)))
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
)
282 (setq mode-line-buffer-identification
283 (list (format mh-show-buffer-mode-line-buffer-id
286 (setq mh-showing-with-headers nil
)))))
288 (defun mh-start-of-uncleaned-message ()
289 ;; position uninteresting headers off the top of the window
290 (let ((case-fold-search t
))
292 "^To:\\|^From:\\|^Subject:\\|^Date:" nil t
)
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
)
301 (set-buffer mh-show-buffer
)
302 (setq buffer-file-name nil
))))
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.
311 (cond ((looking-at mh-msg-number-regexp
)
312 (string-to-int (buffer-substring (match-beginning 1)
315 (error "Cursor not pointing to message"))
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
)
323 (mh-expand-file-name folder
)
324 mh-folder-filename
)))
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
))
336 (if (search-forward "\n\n" nil
'move
)
338 (narrow-to-region start
(point))
339 (goto-char (point-min))
341 (while (< (point) (point-max))
342 (cond ((looking-at visible-headers
)
344 (while (looking-at "[ \t]") (forward-line 1)))
347 (while (looking-at "[ \t]")
348 (mh-delete-line 1)))))
349 (while (re-search-forward invisible-headers nil t
)
352 (while (looking-at "[ \t]")
353 (mh-delete-line 1))))
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))
363 (recenter (if arg arg
'(t)))))
366 (defun mh-delete-line (lines)
367 ;; Delete version of kill-line.
368 (delete-region (point) (save-excursion (forward-line lines
) (point))))
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 ]*$") "")
379 (re-search-forward "[\t ]*\\([^\t \n].*\\)$" nil t
)
380 (let ((start (match-beginning 1)))
382 (while (looking-at "[ \t]")
384 (buffer-substring start
(1- (point))))))))
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.
392 (mh-goto-msg msg t t
))
393 (with-mh-folder-updating (t)
395 (forward-char offset
)
397 (insert notation
)))))
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
)
412 (re-search-forward msg-pattern nil t
)) t
)
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
)))
420 (if (not dont-show
) (mh-maybe-show number
))
423 (goto-char starting-place
)
424 (if (not no-error-if-no-message
)
425 (error "No message %d" number
))
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
))
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.
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
446 (insert-file-contents profile
)
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"))
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
))))))
473 (defun mh-find-progs ()
474 (or (file-exists-p (expand-file-name "inc" mh-progs
))
476 (or (mh-path-search exec-path
"inc")
477 (mh-path-search '("/usr/local/bin/mh/"
479 "/usr/bin/mh/" ;Ultrix 4.2
480 "/usr/new/mh/" ;Ultrix <4.2
481 "/usr/contrib/mh/bin" ;BSDI
485 (or (file-exists-p (expand-file-name "mhl" 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
493 (mh-path-search exec-path
"mhl") ;unlikely
494 "/usr/local/bin/mh/"))))
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.
500 (not (file-exists-p (expand-file-name file
(car path
)))))
501 (setq path
(cdr path
)))
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
517 (insert-file-contents profile
)
519 (error "Cannot read MH profile \"%s\": %s"
520 profile
(car (cdr (cdr err
)))))))
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
))
528 (defun mh-find-seq (name) (assoc name mh-seq-list
))
530 (defun mh-make-seq (name msgs
) (cons name msgs
))
532 (defun mh-seq-to-msgs (seq)
533 "Return a list of the messages in SEQUENCE."
534 (mh-seq-msgs (mh-find-seq seq
)))
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
)))
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
))))))
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")
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)))
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.
569 (let* ((prompt (format "%s folder%s" prompt
570 (if (equal "" default
)
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
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
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
)))
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
)))))
604 (defvar mh-make-folder-list-process nil
605 "The background process collecting the folder list.")
607 (defvar mh-folder-list-temp nil
608 "mh-folder-list as it is being built.")
610 (defvar mh-folder-list-partial-line
""
611 "Start of last incomplete line from folder process.")
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"))
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."
632 ((not mh-make-folder-list-process
)
634 (let ((process-connection-type nil
))
635 (setq mh-make-folder-list-process
636 (start-process "folders" nil
(expand-file-name "folders" mh-progs
)
638 (if mh-recursive-folders
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
)))))
645 (defun mh-make-folder-list-filter (process output
)
646 ;; parse output from "folders -fast"
651 (setq line-end
(string-match "\n" output position
))
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
)) "/"))
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
)
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
))))
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 "+".
681 (eql (aref (symbol-name name
) 0) ?
+)
682 (and (> (length name
) 0)
683 (eql (aref name
0) ?
+))))
686 ;;; Issue commands to MH.
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.
693 (set-buffer (get-buffer-create " *mh-temp*"))
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*")
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.
709 (set-buffer (get-buffer-create " *mh-temp*"))
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+\"$@\"}"
717 (expand-file-name command mh-progs
))
719 (mh-list-to-string args
))
721 (expand-file-name command mh-progs
) nil t nil
722 (mh-list-to-string args
)))))
723 (mh-handle-process-error command status
))))
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.
730 (set-buffer (get-buffer-create " *mh-temp*"))
732 (let* ((process-connection-type nil
)
733 (process (apply 'start-process
735 (expand-file-name command mh-progs
)
736 (mh-list-to-string args
))))
737 (set-process-filter process
'mh-process-daemon
)))
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*"))
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*"))
757 (expand-file-name command mh-progs
) nil t nil
759 (goto-char (point-min))
761 (mh-handle-process-error command value
)
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
)
770 (expand-file-name command mh-progs
) nil t display
771 (mh-list-to-string args
))
772 (exchange-point-and-mark))
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
))
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
788 ((stringp status
) ;kill string
789 (error (format "%s: %s" command status
)))
792 ((= (buffer-size) 0) ;program produced no error message
793 (error (format "%s: exit code %d" command status
)))
795 ;; will error message fit on one line?
797 (if (and (< (buffer-size) (screen-width))
799 (error (buffer-substring 1 (progn (goto-char 1)
802 (display-buffer (current-buffer))
804 "%s failed with status %d. See error message in other window."
805 command status
))))))))
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
)))
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
)))
820 (defun mh-list-to-string-1 (l)
821 (let ((new-list nil
))
823 (cond ((null (car l
)))
825 (setq new-list
(cons (symbol-name (car l
)) new-list
)))
827 (setq new-list
(cons (int-to-string (car l
)) new-list
)))
829 ((stringp (car l
)) (setq new-list
(cons (car l
) new-list
)))
831 (setq new-list
(nconc (mh-list-to-string-1 (car l
))
833 (t (error "Bad element in mh-list-to-string: %s" (car l
))))
839 (and (not noninteractive
)
840 mh-auto-folder-collect
841 (mh-make-folder-list-background))
843 ;;; mh-utils.el ends here