comparison lisp/mail/mh-utils.el @ 6365:a1b8926f7ece

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