Fix cross references.
[emacs.git] / lisp / mh-e / mh-utils.el
blob9b1482640034937a5f47573eb0d652350d9c6f19
1 ;;; mh-utils.el --- MH-E code needed for both sending and reading
3 ;; Copyright (C) 1993, 1995, 1997, 2000, 2001, 2002 Free Software Foundation, Inc.
5 ;; Author: Bill Wohler <wohler@newt.com>
6 ;; Maintainer: Bill Wohler <wohler@newt.com>
7 ;; Keywords: mail
8 ;; See: mh-e.el
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
27 ;;; Commentary:
29 ;; Internal support for MH-E package.
31 ;;; Change Log:
33 ;; $Id: mh-utils.el,v 1.2 2003/02/03 20:55:30 wohler Exp $
35 ;;; Code:
37 ;; Is this XEmacs-land? Located here since needed by mh-customize.el.
38 (defvar mh-xemacs-flag (featurep 'xemacs)
39 "Non-nil means the current Emacs is XEmacs.")
41 (require 'cl)
42 (require 'gnus-util)
43 (require 'font-lock)
44 (require 'mh-loaddefs)
45 (require 'mh-customize)
47 (load "mm-decode" t t) ; Non-fatal dependency
48 (load "mm-view" t t) ; Non-fatal dependency
49 (load "executable" t t) ; Non-fatal dependency on
50 ; executable-find
52 ;; Shush the byte-compiler
53 (defvar font-lock-auto-fontify)
54 (defvar font-lock-defaults)
55 (defvar mark-active)
56 (defvar tool-bar-mode)
58 ;;; Autoloads
59 (autoload 'gnus-article-highlight-citation "gnus-cite")
60 (autoload 'mail-header-end "sendmail")
61 (autoload 'Info-goto-node "info")
62 (unless (fboundp 'make-hash-table)
63 (autoload 'make-hash-table "cl"))
65 ;;; Set for local environment:
66 ;;; mh-progs and mh-lib used to be set in paths.el, which tried to
67 ;;; figure out at build time which of several possible directories MH
68 ;;; was installed into. But if you installed MH after building Emacs,
69 ;;; this would almost certainly be wrong, so now we do it at run time.
71 (defvar mh-progs nil
72 "Directory containing MH commands, such as inc, repl, and rmm.")
74 (defvar mh-lib nil
75 "Directory containing the MH library.
76 This directory contains, among other things, the components file.")
78 (defvar mh-lib-progs nil
79 "Directory containing MH helper programs.
80 This directory contains, among other things, the mhl program.")
82 (defvar mh-nmh-flag nil
83 "Non-nil means nmh is installed on this system instead of MH.")
85 ;;;###autoload
86 (put 'mh-progs 'risky-local-variable t)
87 ;;;###autoload
88 (put 'mh-lib 'risky-local-variable t)
89 ;;;###autoload
90 (put 'mh-lib-progs 'risky-local-variable t)
91 ;;;###autoload
92 (put 'mh-nmh-flag 'risky-local-variable t)
94 ;;; CL Replacements
95 (defun mh-search-from-end (char string)
96 "Return the position of last occurrence of CHAR in STRING.
97 If CHAR is not present in STRING then return nil. The function is used in lieu
98 of `search' in the CL package."
99 (loop for index from (1- (length string)) downto 0
100 when (equal (aref string index) char) return index
101 finally return nil))
103 ;;; Macro to generate correct code for different emacs variants
105 (defmacro mh-mark-active-p (check-transient-mark-mode-flag)
106 "A macro that expands into appropriate code in XEmacs and nil in GNU Emacs.
107 In GNU Emacs if CHECK-TRANSIENT-MARK-MODE-FLAG is non-nil then check if
108 variable `transient-mark-mode' is active."
109 (cond (mh-xemacs-flag ;XEmacs
110 `(and (boundp 'zmacs-regions) zmacs-regions (region-active-p)))
111 ((not check-transient-mark-mode-flag) ;GNU Emacs
112 `(and (boundp 'mark-active) mark-active))
113 (t ;GNU Emacs
114 `(and (boundp 'transient-mark-mode) transient-mark-mode
115 (boundp 'mark-active) mark-active))))
117 ;;; Additional header fields that might someday be added:
118 ;;; "Sender: " "Reply-to: "
120 (defvar mh-scan-msg-number-regexp "^ *\\([0-9]+\\)"
121 "Regexp to find the number of a message in a scan line.
122 The message's number must be surrounded with \\( \\)")
124 (defvar mh-scan-msg-overflow-regexp "^[?0-9][0-9]"
125 "Regexp to find a scan line in which the message number overflowed.
126 The message's number is left truncated in this case.")
128 (defvar mh-scan-msg-format-regexp "%\\([0-9]*\\)(msg)"
129 "Regexp to find message number width in an scan format.
130 The message number width must be surrounded with \\( \\).")
132 (defvar mh-scan-msg-format-string "%d"
133 "Format string for width of the message number in a scan format.
134 Use `0%d' for zero-filled message numbers.")
136 (defvar mh-scan-msg-search-regexp "^[^0-9]*%d[^0-9]"
137 "Format string containing a regexp matching the scan listing for a message.
138 The desired message's number will be an argument to format.")
140 (defvar mh-default-folder-for-message-function nil
141 "Function to select a default folder for refiling or Fcc.
142 If set to a function, that function is called with no arguments by
143 `\\[mh-refile-msg]' and `\\[mh-to-fcc]' to get a default when
144 prompting the user for a folder. The function is called from within a
145 `save-excursion', with point at the start of the message. It should
146 return the folder to offer as the refile or Fcc folder, as a string
147 with a leading `+' sign. It can also return an empty string to use no
148 default, or nil to calculate the default the usual way.
149 NOTE: This variable is not an ordinary hook;
150 It may not be a list of functions.")
152 (defvar mh-show-buffer-mode-line-buffer-id " {show-%s} %d"
153 "Format string to produce `mode-line-buffer-identification' for show buffers.
154 First argument is folder name. Second is message number.")
156 (defvar mh-cmd-note 4
157 "Column to insert notation.
158 Use `mh-set-cmd-note' to modify it.
159 This value may be dynamically updated if `mh-adaptive-cmd-note-flag' is
160 non-nil and `mh-scan-format-file' is t.
161 Note that the first column is column number 0.")
162 (make-variable-buffer-local 'mh-cmd-note)
164 (defvar mh-note-seq "%"
165 "String whose first character is used to notate messages in a sequence.")
167 (defvar mh-mail-header-separator "--------"
168 "*Line used by MH to separate headers from text in messages being composed.
169 This variable should not be used directly in programs. Programs should use
170 `mail-header-separator' instead. `mail-header-separator' is initialized to
171 `mh-mail-header-separator' in `mh-letter-mode'; in other contexts, you may
172 have to perform this initialization yourself.
174 Do not make this a regexp as it may be the argument to `insert' and it is
175 passed through `regexp-quote' before being used by functions like
176 `re-search-forward'.")
178 ;; Variables for MIME display
180 ;; Structure to keep track of MIME handles on a per buffer basis.
181 (defstruct (mh-buffer-data (:conc-name mh-mime-)
182 (:constructor mh-make-buffer-data))
183 (handles ()) ; List of MIME handles
184 (handles-cache (make-hash-table)) ; Cache to avoid multiple decodes of
185 ; nested messages
186 (parts-count 0) ; The button number is generated from
187 ; this number
188 (part-index-hash (make-hash-table))) ; Avoid incrementing the part number
189 ; for nested messages
190 ;;; This has to be a macro, since we do: (setf (mh-buffer-data) ...)
191 (defmacro mh-buffer-data ()
192 "Convenience macro to get the MIME data structures of the current buffer."
193 `(gethash (current-buffer) mh-globals-hash))
195 (defvar mh-globals-hash (make-hash-table)
196 "Keeps track of MIME data on a per buffer basis.")
198 (defvar mh-gnus-pgp-support-flag (not (not (locate-library "mml2015")))
199 "Non-nil means installed Gnus has PGP support.")
201 (defvar mh-mm-inline-media-tests
202 `(("image/jpeg"
203 mm-inline-image
204 (lambda (handle)
205 (mm-valid-and-fit-image-p 'jpeg handle)))
206 ("image/png"
207 mm-inline-image
208 (lambda (handle)
209 (mm-valid-and-fit-image-p 'png handle)))
210 ("image/gif"
211 mm-inline-image
212 (lambda (handle)
213 (mm-valid-and-fit-image-p 'gif handle)))
214 ("image/tiff"
215 mm-inline-image
216 (lambda (handle)
217 (mm-valid-and-fit-image-p 'tiff handle)) )
218 ("image/xbm"
219 mm-inline-image
220 (lambda (handle)
221 (mm-valid-and-fit-image-p 'xbm handle)))
222 ("image/x-xbitmap"
223 mm-inline-image
224 (lambda (handle)
225 (mm-valid-and-fit-image-p 'xbm handle)))
226 ("image/xpm"
227 mm-inline-image
228 (lambda (handle)
229 (mm-valid-and-fit-image-p 'xpm handle)))
230 ("image/x-pixmap"
231 mm-inline-image
232 (lambda (handle)
233 (mm-valid-and-fit-image-p 'xpm handle)))
234 ("image/bmp"
235 mm-inline-image
236 (lambda (handle)
237 (mm-valid-and-fit-image-p 'bmp handle)))
238 ("image/x-portable-bitmap"
239 mm-inline-image
240 (lambda (handle)
241 (mm-valid-and-fit-image-p 'pbm handle)))
242 ("text/plain" mm-inline-text identity)
243 ("text/enriched" mm-inline-text identity)
244 ("text/richtext" mm-inline-text identity)
245 ("text/x-patch" mm-display-patch-inline
246 (lambda (handle)
247 (locate-library "diff-mode")))
248 ("application/emacs-lisp" mm-display-elisp-inline identity)
249 ("application/x-emacs-lisp" mm-display-elisp-inline identity)
250 ("text/html"
251 ,(if (fboundp 'mm-inline-text-html) 'mm-inline-text-html 'mm-inline-text)
252 (lambda (handle)
253 (or (and (boundp 'mm-inline-text-html-renderer)
254 mm-inline-text-html-renderer)
255 (and (boundp 'mm-text-html-renderer) mm-text-html-renderer))))
256 ("text/x-vcard"
257 mm-inline-text-vcard
258 (lambda (handle)
259 (or (featurep 'vcard)
260 (locate-library "vcard"))))
261 ("message/delivery-status" mm-inline-text identity)
262 ("message/rfc822" mh-mm-inline-message identity)
263 ;;("message/partial" mm-inline-partial identity)
264 ;;("message/external-body" mm-inline-external-body identity)
265 ("text/.*" mm-inline-text identity)
266 ("audio/wav" mm-inline-audio
267 (lambda (handle)
268 (and (or (featurep 'nas-sound) (featurep 'native-sound))
269 (device-sound-enabled-p))))
270 ("audio/au"
271 mm-inline-audio
272 (lambda (handle)
273 (and (or (featurep 'nas-sound) (featurep 'native-sound))
274 (device-sound-enabled-p))))
275 ("application/pgp-signature" ignore identity)
276 ("application/x-pkcs7-signature" ignore identity)
277 ("application/pkcs7-signature" ignore identity)
278 ("application/x-pkcs7-mime" ignore identity)
279 ("application/pkcs7-mime" ignore identity)
280 ("multipart/alternative" ignore identity)
281 ("multipart/mixed" ignore identity)
282 ("multipart/related" ignore identity)
283 ;; Disable audio and image
284 ("audio/.*" ignore ignore)
285 ("image/.*" ignore ignore)
286 ;; Default to displaying as text
287 (".*" mm-inline-text mm-readable-p))
288 "Alist of media types/tests saying whether types can be displayed inline.")
290 ;; Needed by mh-comp.el and mh-mime.el
291 (defvar mh-mhn-compose-insert-flag nil
292 "Non-nil means MIME insertion was done.
293 Triggers an automatic call to `mh-edit-mhn' in `mh-send-letter'.
294 This variable is buffer-local.")
295 (make-variable-buffer-local 'mh-mhn-compose-insert-flag)
297 (defvar mh-mml-compose-insert-flag nil
298 "Non-nil means that a MIME insertion was done.
299 This buffer-local variable is used to remember if a MIME insertion was done.
300 Triggers an automatic call to `mh-mml-to-mime' in `mh-send-letter'.")
301 (make-variable-buffer-local 'mh-mml-compose-insert-flag)
303 ;; Copy of `goto-address-mail-regexp'
304 (defvar mh-address-mail-regexp
305 "[-a-zA-Z0-9._]+@[-a-zA-z0-9_]+\\.+[a-zA-Z0-9]+"
306 "A regular expression probably matching an e-mail address.")
308 ;; From goto-addr.el, which we don't want to force-load on users.
309 ;;;###mh-autoload
310 (defun mh-goto-address-find-address-at-point ()
311 "Find e-mail address around or before point.
312 Then search backwards to beginning of line for the start of an e-mail
313 address. If no e-mail address found, return nil."
314 (re-search-backward "[^-_A-z0-9.@]" (line-beginning-position) 'lim)
315 (if (or (looking-at mh-address-mail-regexp) ; already at start
316 (and (re-search-forward mh-address-mail-regexp
317 (line-end-position) 'lim)
318 (goto-char (match-beginning 0))))
319 (match-string-no-properties 0)))
321 (defun mh-in-header-p ()
322 "Return non-nil if the point is in the header of a draft message."
323 (< (point) (mail-header-end)))
325 (defun mh-header-field-beginning ()
326 "Move to the beginning of the current header field.
327 Handles RFC 822 continuation lines."
328 (beginning-of-line)
329 (while (looking-at "^[ \t]")
330 (forward-line -1)))
332 (defun mh-header-field-end ()
333 "Move to the end of the current header field.
334 Handles RFC 822 continuation lines."
335 (forward-line 1)
336 (while (looking-at "^[ \t]")
337 (forward-line 1))
338 (backward-char 1)) ;to end of previous line
340 (defun mh-letter-header-font-lock (limit)
341 "Return the entire mail header to font-lock.
342 Argument LIMIT limits search."
343 (if (= (point) limit)
345 (let* ((mail-header-end (save-match-data (mail-header-end)))
346 (lesser-limit (if (< mail-header-end limit) mail-header-end limit)))
347 (when (mh-in-header-p)
348 (set-match-data (list 1 lesser-limit))
349 (goto-char lesser-limit)
350 t))))
352 (defun mh-header-field-font-lock (field limit)
353 "Return the value of a header field FIELD to font-lock.
354 Argument LIMIT limits search."
355 (if (= (point) limit)
357 (let* ((mail-header-end (mail-header-end))
358 (lesser-limit (if (< mail-header-end limit) mail-header-end limit))
359 (case-fold-search t))
360 (when (and (< (point) mail-header-end) ;Only within header
361 (re-search-forward (format "^%s" field) lesser-limit t))
362 (let ((match-one-b (match-beginning 0))
363 (match-one-e (match-end 0)))
364 (mh-header-field-end)
365 (if (> (point) limit) ;Don't search for end beyond limit
366 (goto-char limit))
367 (set-match-data (list match-one-b match-one-e
368 (1+ match-one-e) (point)))
369 t)))))
371 (defun mh-header-to-font-lock (limit)
372 "Return the value of a header field To to font-lock.
373 Argument LIMIT limits search."
374 (mh-header-field-font-lock "To:" limit))
376 (defun mh-header-cc-font-lock (limit)
377 "Return the value of a header field cc to font-lock.
378 Argument LIMIT limits search."
379 (mh-header-field-font-lock "cc:" limit))
381 (defun mh-header-subject-font-lock (limit)
382 "Return the value of a header field Subject to font-lock.
383 Argument LIMIT limits search."
384 (mh-header-field-font-lock "Subject:" limit))
386 (eval-and-compile
387 ;; Otherwise byte-compilation fails on `mh-show-font-lock-keywords-with-cite'
388 (defvar mh-show-font-lock-keywords
389 '(("^\\(From:\\|Sender:\\)\\(.*\\)" (1 'default) (2 mh-show-from-face))
390 (mh-header-to-font-lock (0 'default) (1 mh-show-to-face))
391 (mh-header-cc-font-lock (0 'default) (1 mh-show-cc-face))
392 ("^\\(Reply-To:\\|Return-Path:\\)\\(.*\\)$"
393 (1 'default) (2 mh-show-from-face))
394 (mh-header-subject-font-lock (0 'default) (1 mh-show-subject-face))
395 ("^\\(Apparently-To:\\|Newsgroups:\\)\\(.*\\)"
396 (1 'default) (2 mh-show-cc-face))
397 ("^\\(In-reply-to\\|Date\\):\\(.*\\)$"
398 (1 'default) (2 mh-show-date-face))
399 (mh-letter-header-font-lock (0 mh-show-header-face append t)))
400 "Additional expressions to highlight in MH-show mode."))
402 (defvar mh-show-font-lock-keywords-with-cite
403 (eval-when-compile
404 (let* ((cite-chars "[>|}]")
405 (cite-prefix "A-Za-z")
406 (cite-suffix (concat cite-prefix "0-9_.@-`'\"")))
407 (append
408 mh-show-font-lock-keywords
409 (list
410 ;; Use MATCH-ANCHORED to effectively anchor the regexp left side.
411 `(,cite-chars
412 (,(concat "\\=[ \t]*"
413 "\\(\\([" cite-prefix "]+[" cite-suffix "]*\\)?"
414 "\\(" cite-chars "[ \t]*\\)\\)+"
415 "\\(.*\\)")
416 (beginning-of-line) (end-of-line)
417 (2 font-lock-constant-face nil t)
418 (4 font-lock-comment-face nil t)))))))
419 "Additional expressions to highlight in MH-show mode.")
421 (defun mh-show-font-lock-fontify-region (beg end loudly)
422 "Limit font-lock in `mh-show-mode' to the header.
423 Used when `mh-highlight-citation-p' is set to gnus, leaving the body to be
424 dealt with by gnus highlighting. The region between BEG and END is
425 given over to be fontified and LOUDLY controls if a user sees a
426 message about the fontification operation."
427 (let ((header-end (mail-header-end)))
428 (cond
429 ((and (< beg header-end)(< end header-end))
430 (font-lock-default-fontify-region beg end loudly))
431 ((and (< beg header-end)(>= end header-end))
432 (font-lock-default-fontify-region beg header-end loudly))
434 nil))))
436 ;; Needed to help shush the byte-compiler.
437 (if mh-xemacs-flag
438 (progn
439 (eval-and-compile
440 (require 'gnus)
441 (require 'gnus-art)
442 (require 'gnus-cite))))
444 (defun mh-gnus-article-highlight-citation ()
445 "Highlight cited text in current buffer using gnus."
446 (interactive)
447 ;; Requiring gnus-cite should have been sufficient. However for Emacs21.1,
448 ;; recursive-load-depth-limit is only 10, so an error occurs. Also it may be
449 ;; better to have an autoload at top-level (though that won't work because
450 ;; of recursive-load-depth-limit). That gets rid of a compiler warning as
451 ;; well.
452 (unless mh-xemacs-flag
453 (require 'gnus-art)
454 (require 'gnus-cite))
455 ;; Don't allow Gnus to create buttons while highlighting, maybe this is bad
456 ;; style?
457 (flet ((gnus-article-add-button (&rest args) nil))
458 (let* ((modified (buffer-modified-p))
459 (gnus-article-buffer (buffer-name))
460 (gnus-cite-face-list `(,@(cdr gnus-cite-face-list)
461 ,(car gnus-cite-face-list))))
462 (gnus-article-highlight-citation t)
463 (set-buffer-modified-p modified))))
465 ;;; Internal bookkeeping variables:
467 ;; Cached value of the `Path:' component in the user's MH profile.
468 ;; User's mail folder directory.
469 (defvar mh-user-path nil)
471 ;; An mh-draft-folder of nil means do not use a draft folder.
472 ;; Cached value of the `Draft-Folder:' component in the user's MH profile.
473 ;; Name of folder containing draft messages.
474 (defvar mh-draft-folder nil)
476 ;; Cached value of the `Unseen-Sequence:' component in the user's MH profile.
477 ;; Name of the Unseen sequence.
478 (defvar mh-unseen-seq nil)
480 ;; Cached value of the `Previous-Sequence:' component in the user's MH
481 ;; profile.
482 ;; Name of the Previous sequence.
483 (defvar mh-previous-seq nil)
485 ;; Cached value of the `Inbox:' component in the user's MH profile,
486 ;; or "+inbox" if no such component.
487 ;; Name of the Inbox folder.
488 (defvar mh-inbox nil)
490 ;; The names of ephemeral buffers have a " *mh-" prefix (so that they are
491 ;; hidden and can be programmatically removed in mh-quit), and the variable
492 ;; names have the form mh-temp-.*-buffer.
493 (defconst mh-temp-buffer " *mh-temp*") ;scratch
495 ;; The names of MH-E buffers that are not ephemeral and can be used by the
496 ;; user (and deleted by the user when no longer needed) have a "*MH-E " prefix
497 ;; (so they can be programmatically removed in mh-quit), and the variable
498 ;; names have the form mh-.*-buffer.
499 (defconst mh-folders-buffer "*MH-E Folders*") ;folder list
500 (defconst mh-info-buffer "*MH-E Info*") ;version information buffer
501 (defconst mh-log-buffer "*MH-E Log*") ;output of MH commands and so on
502 (defconst mh-recipients-buffer "*MH-E Recipients*") ;killed when draft sent
503 (defconst mh-sequences-buffer "*MH-E Sequences*") ;sequences list
505 ;; Window configuration before MH-E command.
506 (defvar mh-previous-window-config nil)
508 ;;Non-nil means next SPC or whatever goes to next undeleted message.
509 (defvar mh-page-to-next-msg-flag nil)
511 ;;; Internal variables local to a folder.
513 ;; Name of current folder, a string.
514 (defvar mh-current-folder nil)
516 ;; Buffer that displays message for this folder.
517 (defvar mh-show-buffer nil)
519 ;; Full path of directory for this folder.
520 (defvar mh-folder-filename nil)
522 ;;Number of msgs in buffer.
523 (defvar mh-msg-count nil)
525 ;; If non-nil, show the message in a separate window.
526 (defvar mh-showing-mode nil)
528 (defvar mh-show-mode-map (make-sparse-keymap)
529 "Keymap used by the show buffer.")
531 (defvar mh-show-folder-buffer nil
532 "Keeps track of folder whose message is being displayed.")
534 (defvar mh-logo-cache nil)
536 (defun mh-logo-display ()
537 "Modify mode line to display MH-E logo."
538 (when (fboundp 'find-image)
539 (add-text-properties
541 `(display ,(or mh-logo-cache
542 (setq mh-logo-cache
543 (find-image '((:type xpm :ascent center
544 :file "mh-logo.xpm"))))))
545 (car mode-line-buffer-identification))))
547 ;;; This holds a documentation string used by describe-mode.
548 (defun mh-showing-mode (&optional arg)
549 "Change whether messages should be displayed.
550 With arg, display messages iff ARG is positive."
551 (setq mh-showing-mode
552 (if (null arg)
553 (not mh-showing-mode)
554 (> (prefix-numeric-value arg) 0))))
556 ;; The sequences of this folder. An alist of (seq . msgs).
557 (defvar mh-seq-list nil)
559 ;; List of displayed messages to be removed from the Unseen sequence.
560 (defvar mh-seen-list nil)
562 ;; If non-nil, show buffer contains message with all headers.
563 ;; If nil, show buffer contains message processed normally.
564 ;; Showing message with headers or normally.
565 (defvar mh-showing-with-headers nil)
568 ;;; MH-E macros
570 (defmacro with-mh-folder-updating (save-modification-flag &rest body)
571 "Format is (with-mh-folder-updating (SAVE-MODIFICATION-FLAG) &body BODY).
572 Execute BODY, which can modify the folder buffer without having to
573 worry about file locking or the read-only flag, and return its result.
574 If SAVE-MODIFICATION-FLAG is non-nil, the buffer's modification
575 flag is unchanged, otherwise it is cleared."
576 (setq save-modification-flag (car save-modification-flag)) ; CL style
577 `(prog1
578 (let ((mh-folder-updating-mod-flag (buffer-modified-p))
579 (buffer-read-only nil)
580 (buffer-file-name nil)) ;don't let the buffer get locked
581 (prog1
582 (progn
583 ,@body)
584 (mh-set-folder-modified-p mh-folder-updating-mod-flag)))
585 ,@(if (not save-modification-flag)
586 '((mh-set-folder-modified-p nil)))))
588 (put 'with-mh-folder-updating 'lisp-indent-hook 1)
590 (defmacro mh-in-show-buffer (show-buffer &rest body)
591 "Format is (mh-in-show-buffer (SHOW-BUFFER) &body BODY).
592 Display buffer SHOW-BUFFER in other window and execute BODY in it.
593 Stronger than `save-excursion', weaker than `save-window-excursion'."
594 (setq show-buffer (car show-buffer)) ; CL style
595 `(let ((mh-in-show-buffer-saved-window (selected-window)))
596 (switch-to-buffer-other-window ,show-buffer)
597 (if mh-bury-show-buffer-flag (bury-buffer (current-buffer)))
598 (unwind-protect
599 (progn
600 ,@body)
601 (select-window mh-in-show-buffer-saved-window))))
603 (put 'mh-in-show-buffer 'lisp-indent-hook 1)
605 (defmacro mh-make-seq (name msgs)
606 "Create sequence NAME with the given MSGS."
607 (list 'cons name msgs))
609 (defmacro mh-seq-name (sequence)
610 "Extract sequence name from the given SEQUENCE."
611 (list 'car sequence))
613 (defmacro mh-seq-msgs (sequence)
614 "Extract messages from the given SEQUENCE."
615 (list 'cdr sequence))
617 (defun mh-recenter (arg)
618 "Like recenter but with three improvements:
619 - At the end of the buffer it tries to show fewer empty lines.
620 - operates only if the current buffer is in the selected window.
621 (Commands like `save-some-buffers' can make this false.)
622 - nil ARG means recenter as if prefix argument had been given."
623 (cond ((not (eq (get-buffer-window (current-buffer)) (selected-window)))
624 nil)
625 ((= (point-max) (save-excursion
626 (forward-line (- (/ (window-height) 2) 2))
627 (point)))
628 (let ((lines-from-end 2))
629 (save-excursion
630 (while (> (point-max) (progn (forward-line) (point)))
631 (incf lines-from-end)))
632 (recenter (- lines-from-end))))
633 ;; '(4) is the same as C-u prefix argument.
634 (t (recenter (or arg '(4))))))
636 (defun mh-start-of-uncleaned-message ()
637 "Position uninteresting headers off the top of the window."
638 (let ((case-fold-search t))
639 (re-search-forward
640 "^To:\\|^Cc:\\|^From:\\|^Subject:\\|^Date:" nil t)
641 (beginning-of-line)
642 (mh-recenter 0)))
644 (defun mh-invalidate-show-buffer ()
645 "Invalidate the show buffer so we must update it to use it."
646 (if (get-buffer mh-show-buffer)
647 (save-excursion
648 (set-buffer mh-show-buffer)
649 (mh-unvisit-file))))
651 (defun mh-unvisit-file ()
652 "Separate current buffer from the message file it was visiting."
653 (or (not (buffer-modified-p))
654 (null buffer-file-name) ;we've been here before
655 (yes-or-no-p (format "Message %s modified; flush changes? "
656 (file-name-nondirectory buffer-file-name)))
657 (error "Flushing changes not confirmed"))
658 (clear-visited-file-modtime)
659 (unlock-buffer)
660 (setq buffer-file-name nil))
662 ;;;###mh-autoload
663 (defun mh-get-msg-num (error-if-no-message)
664 "Return the message number of the displayed message.
665 If the argument ERROR-IF-NO-MESSAGE is non-nil, then complain if the cursor is
666 not pointing to a message."
667 (save-excursion
668 (beginning-of-line)
669 (cond ((looking-at mh-scan-msg-number-regexp)
670 (string-to-int (buffer-substring (match-beginning 1)
671 (match-end 1))))
672 (error-if-no-message
673 (error "Cursor not pointing to message"))
674 (t nil))))
676 (defun mh-folder-name-p (name)
677 "Return non-nil if NAME is the name of a folder.
678 A name (a string or symbol) can be a folder name if it begins with \"+\"."
679 (if (symbolp name)
680 (eq (aref (symbol-name name) 0) ?+)
681 (and (> (length name) 0)
682 (eq (aref name 0) ?+))))
685 (defun mh-expand-file-name (filename &optional default)
686 "Expand FILENAME like `expand-file-name', but also handle MH folder names.
687 Any filename that starts with '+' is treated as a folder name.
688 See `expand-file-name' for description of DEFAULT."
689 (if (mh-folder-name-p filename)
690 (expand-file-name (substring filename 1) mh-user-path)
691 (expand-file-name filename default)))
694 (defun mh-msg-filename (msg &optional folder)
695 "Return the file name of MSG in FOLDER (default current folder)."
696 (expand-file-name (int-to-string msg)
697 (if folder
698 (mh-expand-file-name folder)
699 mh-folder-filename)))
701 ;;; Infrastructure to generate show-buffer functions from folder functions
702 ;;; XEmacs does not have deactivate-mark? What is the equivalent of
703 ;;; transient-mark-mode for XEmacs? Should we be restoring the mark in the
704 ;;; folder buffer after the operation has been carried out.
705 (defmacro mh-defun-show-buffer (function original-function
706 &optional dont-return)
707 "Define FUNCTION to run ORIGINAL-FUNCTION in folder buffer.
708 If the buffer we start in is still visible and DONT-RETURN is nil then switch
709 to it after that."
710 `(defun ,function ()
711 ,(format "Calls %s from the message's folder.\n%s\nSee `%s' for more info.\n"
712 original-function
713 (if dont-return ""
714 "When function completes, returns to the show buffer if it is
715 still visible.\n")
716 original-function)
717 (interactive)
718 (when (buffer-live-p (get-buffer mh-show-folder-buffer))
719 (let ((config (current-window-configuration))
720 (folder-buffer mh-show-folder-buffer)
721 (normal-exit nil)
722 ,@(if dont-return () '((cur-buffer-name (buffer-name)))))
723 (pop-to-buffer mh-show-folder-buffer nil)
724 (unless (equal (buffer-name
725 (window-buffer (frame-first-window (selected-frame))))
726 folder-buffer)
727 (delete-other-windows))
728 (mh-goto-cur-msg t)
729 (and (fboundp 'deactivate-mark) (deactivate-mark))
730 (unwind-protect
731 (prog1 (call-interactively (function ,original-function))
732 (setq normal-exit t))
733 (and (fboundp 'deactivate-mark) (deactivate-mark))
734 (cond ((not normal-exit)
735 (set-window-configuration config))
736 ,(if dont-return
737 `(t (setq mh-previous-window-config config))
738 `((and (get-buffer cur-buffer-name)
739 (window-live-p (get-buffer-window
740 (get-buffer cur-buffer-name))))
741 (pop-to-buffer (get-buffer cur-buffer-name) nil)))))))))
743 ;;; Generate interactive functions for the show buffer from the corresponding
744 ;;; folder functions.
745 (mh-defun-show-buffer mh-show-previous-undeleted-msg
746 mh-previous-undeleted-msg)
747 (mh-defun-show-buffer mh-show-next-undeleted-msg
748 mh-next-undeleted-msg)
749 (mh-defun-show-buffer mh-show-quit mh-quit)
750 (mh-defun-show-buffer mh-show-delete-msg mh-delete-msg)
751 (mh-defun-show-buffer mh-show-refile-msg mh-refile-msg)
752 (mh-defun-show-buffer mh-show-undo mh-undo)
753 (mh-defun-show-buffer mh-show-execute-commands mh-execute-commands)
754 (mh-defun-show-buffer mh-show-reply mh-reply t)
755 (mh-defun-show-buffer mh-show-redistribute mh-redistribute)
756 (mh-defun-show-buffer mh-show-forward mh-forward t)
757 (mh-defun-show-buffer mh-show-header-display mh-header-display)
758 (mh-defun-show-buffer mh-show-refile-or-write-again
759 mh-refile-or-write-again)
760 (mh-defun-show-buffer mh-show-show mh-show)
761 (mh-defun-show-buffer mh-show-write-message-to-file
762 mh-write-msg-to-file)
763 (mh-defun-show-buffer mh-show-extract-rejected-mail
764 mh-extract-rejected-mail t)
765 (mh-defun-show-buffer mh-show-delete-msg-no-motion
766 mh-delete-msg-no-motion)
767 (mh-defun-show-buffer mh-show-first-msg mh-first-msg)
768 (mh-defun-show-buffer mh-show-last-msg mh-last-msg)
769 (mh-defun-show-buffer mh-show-copy-msg mh-copy-msg)
770 (mh-defun-show-buffer mh-show-edit-again mh-edit-again t)
771 (mh-defun-show-buffer mh-show-goto-msg mh-goto-msg)
772 (mh-defun-show-buffer mh-show-inc-folder mh-inc-folder)
773 (mh-defun-show-buffer mh-show-delete-subject-or-thread
774 mh-delete-subject-or-thread)
775 (mh-defun-show-buffer mh-show-delete-subject mh-delete-subject)
776 (mh-defun-show-buffer mh-show-print-msg mh-print-msg)
777 (mh-defun-show-buffer mh-show-send mh-send t)
778 (mh-defun-show-buffer mh-show-toggle-showing mh-toggle-showing t)
779 (mh-defun-show-buffer mh-show-pipe-msg mh-pipe-msg t)
780 (mh-defun-show-buffer mh-show-sort-folder mh-sort-folder)
781 (mh-defun-show-buffer mh-show-visit-folder mh-visit-folder t)
782 (mh-defun-show-buffer mh-show-rescan-folder mh-rescan-folder)
783 (mh-defun-show-buffer mh-show-pack-folder mh-pack-folder)
784 (mh-defun-show-buffer mh-show-kill-folder mh-kill-folder t)
785 (mh-defun-show-buffer mh-show-list-folders mh-list-folders t)
786 (mh-defun-show-buffer mh-show-search-folder mh-search-folder t)
787 (mh-defun-show-buffer mh-show-undo-folder mh-undo-folder)
788 (mh-defun-show-buffer mh-show-delete-msg-from-seq
789 mh-delete-msg-from-seq)
790 (mh-defun-show-buffer mh-show-delete-seq mh-delete-seq)
791 (mh-defun-show-buffer mh-show-list-sequences mh-list-sequences)
792 (mh-defun-show-buffer mh-show-narrow-to-seq mh-narrow-to-seq)
793 (mh-defun-show-buffer mh-show-put-msg-in-seq mh-put-msg-in-seq)
794 (mh-defun-show-buffer mh-show-msg-is-in-seq mh-msg-is-in-seq)
795 (mh-defun-show-buffer mh-show-widen mh-widen)
796 (mh-defun-show-buffer mh-show-narrow-to-subject
797 mh-narrow-to-subject)
798 (mh-defun-show-buffer mh-show-store-msg mh-store-msg)
799 (mh-defun-show-buffer mh-show-page-digest mh-page-digest)
800 (mh-defun-show-buffer mh-show-page-digest-backwards
801 mh-page-digest-backwards)
802 (mh-defun-show-buffer mh-show-burst-digest mh-burst-digest)
803 (mh-defun-show-buffer mh-show-page-msg mh-page-msg)
804 (mh-defun-show-buffer mh-show-previous-page mh-previous-page)
805 (mh-defun-show-buffer mh-show-modify mh-modify t)
806 (mh-defun-show-buffer mh-show-next-button mh-next-button)
807 (mh-defun-show-buffer mh-show-prev-button mh-prev-button)
808 (mh-defun-show-buffer mh-show-toggle-mime-part mh-folder-toggle-mime-part)
809 (mh-defun-show-buffer mh-show-save-mime-part mh-folder-save-mime-part)
810 (mh-defun-show-buffer mh-show-inline-mime-part mh-folder-inline-mime-part)
811 (mh-defun-show-buffer mh-show-toggle-threads mh-toggle-threads)
812 (mh-defun-show-buffer mh-show-thread-delete mh-thread-delete)
813 (mh-defun-show-buffer mh-show-thread-refile mh-thread-refile)
814 (mh-defun-show-buffer mh-show-update-sequences mh-update-sequences)
815 (mh-defun-show-buffer mh-show-next-unread-msg mh-next-unread-msg)
816 (mh-defun-show-buffer mh-show-previous-unread-msg mh-previous-unread-msg)
817 (mh-defun-show-buffer mh-show-thread-ancestor mh-thread-ancestor)
818 (mh-defun-show-buffer mh-show-thread-next-sibling mh-thread-next-sibling)
819 (mh-defun-show-buffer mh-show-thread-previous-sibling
820 mh-thread-previous-sibling)
821 (mh-defun-show-buffer mh-show-index-visit-folder mh-index-visit-folder t)
823 ;;; Populate mh-show-mode-map
824 (gnus-define-keys mh-show-mode-map
825 " " mh-show-page-msg
826 "!" mh-show-refile-or-write-again
827 "," mh-show-header-display
828 "." mh-show-show
829 ">" mh-show-write-message-to-file
830 "?" mh-help
831 "E" mh-show-extract-rejected-mail
832 "M" mh-show-modify
833 "\177" mh-show-previous-page
834 "\C-d" mh-show-delete-msg-no-motion
835 "\t" mh-show-next-button
836 [backtab] mh-show-prev-button
837 "\M-\t" mh-show-prev-button
838 "\ed" mh-show-redistribute
839 "^" mh-show-refile-msg
840 "c" mh-show-copy-msg
841 "d" mh-show-delete-msg
842 "e" mh-show-edit-again
843 "f" mh-show-forward
844 "g" mh-show-goto-msg
845 "i" mh-show-inc-folder
846 "k" mh-show-delete-subject-or-thread
847 "l" mh-show-print-msg
848 "m" mh-show-send
849 "n" mh-show-next-undeleted-msg
850 "\M-n" mh-show-next-unread-msg
851 "o" mh-show-refile-msg
852 "p" mh-show-previous-undeleted-msg
853 "\M-p" mh-show-previous-unread-msg
854 "q" mh-show-quit
855 "r" mh-show-reply
856 "s" mh-show-send
857 "t" mh-show-toggle-showing
858 "u" mh-show-undo
859 "x" mh-show-execute-commands
860 "v" mh-show-index-visit-folder
861 "|" mh-show-pipe-msg)
863 (gnus-define-keys (mh-show-folder-map "F" mh-show-mode-map)
864 "?" mh-prefix-help
865 "S" mh-show-sort-folder
866 "f" mh-show-visit-folder
867 "i" mh-index-search
868 "k" mh-show-kill-folder
869 "l" mh-show-list-folders
870 "o" mh-show-visit-folder
871 "r" mh-show-rescan-folder
872 "s" mh-show-search-folder
873 "t" mh-show-toggle-threads
874 "u" mh-show-undo-folder
875 "v" mh-show-visit-folder)
877 (gnus-define-keys (mh-show-sequence-map "S" mh-show-mode-map)
878 "?" mh-prefix-help
879 "d" mh-show-delete-msg-from-seq
880 "k" mh-show-delete-seq
881 "l" mh-show-list-sequences
882 "n" mh-show-narrow-to-seq
883 "p" mh-show-put-msg-in-seq
884 "s" mh-show-msg-is-in-seq
885 "w" mh-show-widen)
887 (gnus-define-keys (mh-show-thread-map "T" mh-show-mode-map)
888 "?" mh-prefix-help
889 "u" mh-show-thread-ancestor
890 "p" mh-show-thread-previous-sibling
891 "n" mh-show-thread-next-sibling
892 "t" mh-show-toggle-threads
893 "d" mh-show-thread-delete
894 "o" mh-show-thread-refile)
896 (gnus-define-keys (mh-show-limit-map "/" mh-show-mode-map)
897 "?" mh-prefix-help
898 "s" mh-show-narrow-to-subject
899 "w" mh-show-widen)
901 (gnus-define-keys (mh-show-extract-map "X" mh-show-mode-map)
902 "?" mh-prefix-help
903 "s" mh-show-store-msg
904 "u" mh-show-store-msg)
906 ;; Untested...
907 (gnus-define-keys (mh-show-digest-map "D" mh-show-mode-map)
908 "?" mh-prefix-help
909 " " mh-show-page-digest
910 "\177" mh-show-page-digest-backwards
911 "b" mh-show-burst-digest)
913 (gnus-define-keys (mh-show-mime-map "K" mh-show-mode-map)
914 "?" mh-prefix-help
915 "a" mh-mime-save-parts
916 "v" mh-show-toggle-mime-part
917 "o" mh-show-save-mime-part
918 "i" mh-show-inline-mime-part
919 "\t" mh-show-next-button
920 [backtab] mh-show-prev-button
921 "\M-\t" mh-show-prev-button)
923 (easy-menu-define
924 mh-show-sequence-menu mh-show-mode-map "Menu for MH-E folder-sequence."
925 '("Sequence"
926 ["Add Message to Sequence..." mh-show-put-msg-in-seq t]
927 ["List Sequences for Message" mh-show-msg-is-in-seq t]
928 ["Delete Message from Sequence..." mh-show-delete-msg-from-seq t]
929 ["List Sequences in Folder..." mh-show-list-sequences t]
930 ["Delete Sequence..." mh-show-delete-seq t]
931 ["Narrow to Sequence..." mh-show-narrow-to-seq t]
932 ["Widen from Sequence" mh-show-widen t]
933 "--"
934 ["Narrow to Subject Sequence" mh-show-narrow-to-subject t]
935 ["Delete Rest of Same Subject" mh-show-delete-subject t]
936 "--"
937 ["Push State Out to MH" mh-show-update-sequences t]))
939 (easy-menu-define
940 mh-show-message-menu mh-show-mode-map "Menu for MH-E folder-message."
941 '("Message"
942 ["Show Message" mh-show-show t]
943 ["Show Message with Header" mh-show-header-display t]
944 ["Next Message" mh-show-next-undeleted-msg t]
945 ["Previous Message" mh-show-previous-undeleted-msg t]
946 ["Go to First Message" mh-show-first-msg t]
947 ["Go to Last Message" mh-show-last-msg t]
948 ["Go to Message by Number..." mh-show-goto-msg t]
949 ["Modify Message" mh-show-modify t]
950 ["Delete Message" mh-show-delete-msg t]
951 ["Refile Message" mh-show-refile-msg t]
952 ["Undo Delete/Refile" mh-show-undo t]
953 ["Process Delete/Refile" mh-show-execute-commands t]
954 "--"
955 ["Compose a New Message" mh-send t]
956 ["Reply to Message..." mh-show-reply t]
957 ["Forward Message..." mh-show-forward t]
958 ["Redistribute Message..." mh-show-redistribute t]
959 ["Edit Message Again" mh-show-edit-again t]
960 ["Re-edit a Bounced Message" mh-show-extract-rejected-mail t]
961 "--"
962 ["Copy Message to Folder..." mh-show-copy-msg t]
963 ["Print Message" mh-show-print-msg t]
964 ["Write Message to File..." mh-show-write-msg-to-file t]
965 ["Pipe Message to Command..." mh-show-pipe-msg t]
966 ["Unpack Uuencoded Message..." mh-show-store-msg t]
967 ["Burst Digest Message" mh-show-burst-digest t]))
969 (easy-menu-define
970 mh-show-folder-menu mh-show-mode-map "Menu for MH-E folder."
971 '("Folder"
972 ["Incorporate New Mail" mh-show-inc-folder t]
973 ["Toggle Show/Folder" mh-show-toggle-showing t]
974 ["Execute Delete/Refile" mh-show-execute-commands t]
975 ["Rescan Folder" mh-show-rescan-folder t]
976 ["Thread Folder" mh-show-toggle-threads t]
977 ["Pack Folder" mh-show-pack-folder t]
978 ["Sort Folder" mh-show-sort-folder t]
979 "--"
980 ["List Folders" mh-show-list-folders t]
981 ["Visit a Folder..." mh-show-visit-folder t]
982 ["Search a Folder..." mh-show-search-folder t]
983 ["Indexed Search..." mh-index-search t]
984 "--"
985 ["Quit MH-E" mh-quit t]))
988 ;;; Ensure new buffers won't get this mode if default-major-mode is nil.
989 (put 'mh-show-mode 'mode-class 'special)
991 (define-derived-mode mh-show-mode text-mode "MH-Show"
992 "Major mode for showing messages in MH-E.\\<mh-show-mode-map>
993 The value of `mh-show-mode-hook' is a list of functions to
994 be called, with no arguments, upon entry to this mode."
995 (set (make-local-variable 'mail-header-separator) mh-mail-header-separator)
996 (setq paragraph-start (default-value 'paragraph-start))
997 (mh-show-unquote-From)
998 (mh-show-xface)
999 (mh-show-addr)
1000 (make-local-variable 'font-lock-defaults)
1001 ;;(set (make-local-variable 'font-lock-support-mode) nil)
1002 (cond
1003 ((equal mh-highlight-citation-p 'font-lock)
1004 (setq font-lock-defaults '(mh-show-font-lock-keywords-with-cite t)))
1005 ((equal mh-highlight-citation-p 'gnus)
1006 (setq font-lock-defaults '((mh-show-font-lock-keywords)
1007 t nil nil nil
1008 (font-lock-fontify-region-function
1009 . mh-show-font-lock-fontify-region)))
1010 (mh-gnus-article-highlight-citation))
1012 (setq font-lock-defaults '(mh-show-font-lock-keywords t))))
1013 (if (and mh-xemacs-flag
1014 font-lock-auto-fontify)
1015 (turn-on-font-lock))
1016 (if (and (boundp 'tool-bar-mode) tool-bar-mode)
1017 (set (make-local-variable 'tool-bar-map) mh-show-tool-bar-map))
1018 (when mh-decode-mime-flag
1019 (add-hook 'kill-buffer-hook 'mh-mime-cleanup nil t))
1020 (easy-menu-add mh-show-sequence-menu)
1021 (easy-menu-add mh-show-message-menu)
1022 (easy-menu-add mh-show-folder-menu)
1023 (make-local-variable 'mh-show-folder-buffer)
1024 (buffer-disable-undo)
1025 (setq buffer-read-only t)
1026 (use-local-map mh-show-mode-map)
1027 (run-hooks 'mh-show-mode-hook))
1029 (defun mh-show-addr ()
1030 "Use `goto-address'."
1031 (when mh-show-use-goto-addr-flag
1032 (if (not (featurep 'goto-addr))
1033 (load "goto-addr" t t))
1034 (if (fboundp 'goto-address)
1035 (goto-address))))
1037 (defvar mh-show-xface-function
1038 (cond ((and mh-xemacs-flag (locate-library "x-face"))
1039 (load "x-face" t t)
1040 (if (fboundp 'x-face-xmas-wl-display-x-face)
1041 #'x-face-xmas-wl-display-x-face
1042 #'ignore))
1043 ((and (not mh-xemacs-flag) (>= emacs-major-version 21))
1044 (load "x-face-e21" t t)
1045 (if (fboundp 'x-face-decode-message-header)
1046 #'x-face-decode-message-header
1047 #'ignore))
1048 (t #'ignore))
1049 "Determine at run time what function should be called to display X-Face.")
1051 (defun mh-show-xface ()
1052 "Display X-Face."
1053 (when (and mh-show-use-xface-flag
1054 (or mh-decode-mime-flag mhl-formfile
1055 mh-clean-message-header-flag))
1056 (funcall mh-show-xface-function)))
1058 (defun mh-maybe-show (&optional msg)
1059 "Display message at cursor, but only if in show mode.
1060 If optional arg MSG is non-nil, display that message instead."
1061 (if mh-showing-mode (mh-show msg)))
1063 (defun mh-show (&optional message)
1064 "Show message at cursor.
1065 If optional argument MESSAGE is non-nil, display that message instead.
1066 Force a two-window display with the folder window on top (size
1067 `mh-summary-height') and the show buffer below it.
1068 If the message is already visible, display the start of the message.
1070 Display of the message is controlled by setting the variables
1071 `mh-clean-message-header-flag' and `mhl-formfile'. The default behavior is
1072 to scroll uninteresting headers off the top of the window.
1073 Type \"\\[mh-header-display]\" to see the message with all its headers."
1074 (interactive)
1075 (and mh-showing-with-headers
1076 (or mhl-formfile mh-clean-message-header-flag)
1077 (mh-invalidate-show-buffer))
1078 (mh-show-msg message))
1080 (defun mh-show-mouse (EVENT)
1081 "Move point to mouse EVENT and show message."
1082 (interactive "e")
1083 (mouse-set-point EVENT)
1084 (mh-show))
1086 (defun mh-show-msg (msg)
1087 "Show MSG.
1088 The value of `mh-show-hook' is a list of functions to be called, with no
1089 arguments, after the message has been displayed."
1090 (if (not msg)
1091 (setq msg (mh-get-msg-num t)))
1092 (mh-showing-mode t)
1093 (setq mh-page-to-next-msg-flag nil)
1094 (let ((folder mh-current-folder)
1095 (clean-message-header mh-clean-message-header-flag)
1096 (show-window (get-buffer-window mh-show-buffer)))
1097 (if (not (eq (next-window (minibuffer-window)) (selected-window)))
1098 (delete-other-windows)) ; force ourself to the top window
1099 (mh-in-show-buffer (mh-show-buffer)
1100 (if (and show-window
1101 (equal (mh-msg-filename msg folder) buffer-file-name))
1102 (progn ;just back up to start
1103 (goto-char (point-min))
1104 (if (not clean-message-header)
1105 (mh-start-of-uncleaned-message)))
1106 (mh-display-msg msg folder))))
1107 (if (not (= (1+ (window-height)) (frame-height))) ;not horizontally split
1108 (shrink-window (- (window-height) mh-summary-height)))
1109 (mh-recenter nil)
1110 (if (not (memq msg mh-seen-list))
1111 (setq mh-seen-list (cons msg mh-seen-list)))
1112 (when mh-update-sequences-after-mh-show-flag
1113 (mh-update-sequences))
1114 (run-hooks 'mh-show-hook))
1116 (defun mh-modify (&optional message)
1117 "Edit message at cursor.
1118 If optional argument MESSAGE is non-nil, edit that message instead.
1119 Force a two-window display with the folder window on top (size
1120 `mh-summary-height') and the message editing buffer below it.
1122 The message is displayed in raw form."
1123 (interactive)
1124 (let* ((message (or message (mh-get-msg-num t)))
1125 (msg-filename (mh-msg-filename message))
1126 edit-buffer)
1127 (when (not (file-exists-p msg-filename))
1128 (error "Message %d does not exist" message))
1130 ;; Invalidate the show buffer if it is showing the same message that is
1131 ;; to be edited.
1132 (when (and (buffer-live-p (get-buffer mh-show-buffer))
1133 (equal (save-excursion (set-buffer mh-show-buffer)
1134 buffer-file-name)
1135 msg-filename))
1136 (mh-invalidate-show-buffer))
1138 ;; Edit message
1139 (find-file msg-filename)
1140 (setq edit-buffer (current-buffer))
1142 ;; Set buffer properties
1143 (mh-letter-mode)
1144 (use-local-map text-mode-map)
1146 ;; Just show the edit buffer...
1147 (delete-other-windows)
1148 (switch-to-buffer edit-buffer)))
1150 (defun mh-decode-content-transfer-encoded-message ()
1151 "Run mimencode on message body, if needed."
1152 (let ((case-fold-search t)
1153 (header-end (mail-header-end)))
1154 (goto-char (point-min))
1155 (when (re-search-forward "^content-transfer-encoding: " header-end t)
1156 (let ((enc (buffer-substring-no-properties (point) (line-end-position)))
1157 cmdline)
1158 (setq cmdline
1159 (cond ((string-match "base64" enc) (list "-u" "-b" "-p"))
1160 ((string-match "quoted-printable" enc) (list "-u" "-q"))
1161 (t nil)))
1162 (when cmdline
1163 (beginning-of-line)
1164 (insert "Removed-")
1165 (setq header-end (mail-header-end))
1166 (goto-char (1+ header-end))
1167 (apply #'call-process-region (1+ header-end) (point-max) "mimencode"
1168 t t nil cmdline))))))
1170 (defun mh-show-unquote-From ()
1171 "Decode >From at beginning of lines for `mh-show-mode'."
1172 (save-excursion
1173 (let ((modified (buffer-modified-p))
1174 (case-fold-search nil))
1175 (goto-char (mail-header-end))
1176 (while (re-search-forward "^>From" nil t)
1177 (replace-match "From"))
1178 (set-buffer-modified-p modified))))
1180 (defun mh-msg-folder (folder-name)
1181 "Return the name of the buffer for FOLDER-NAME."
1182 folder-name)
1184 (defun mh-display-msg (msg-num folder-name)
1185 "Display MSG-NUM of FOLDER-NAME.
1186 Sets the current buffer to the show buffer."
1187 (let ((folder (mh-msg-folder folder-name)))
1188 (set-buffer folder)
1189 ;; When Gnus uses external displayers it has to keep handles longer. So
1190 ;; we will delete these handles when mh-quit is called on the folder. It
1191 ;; would be nicer if there are weak pointers in emacs lisp, then we could
1192 ;; get the garbage collector to do this for us.
1193 (unless (mh-buffer-data)
1194 (setf (mh-buffer-data) (mh-make-buffer-data)))
1195 ;; Bind variables in folder buffer in case they are local
1196 (let ((formfile mhl-formfile)
1197 (clean-message-header mh-clean-message-header-flag)
1198 (invisible-headers mh-invisible-headers)
1199 (visible-headers mh-visible-headers)
1200 (msg-filename (mh-msg-filename msg-num folder-name))
1201 (show-buffer mh-show-buffer)
1202 (mm-inline-media-tests mh-mm-inline-media-tests))
1203 (if (not (file-exists-p msg-filename))
1204 (error "Message %d does not exist" msg-num))
1205 (if (and (> mh-show-maximum-size 0)
1206 (> (elt (file-attributes msg-filename) 7)
1207 mh-show-maximum-size)
1208 (not (y-or-n-p
1209 (format
1210 "Message %d (%d bytes) exceeds %d bytes. Display it? "
1211 msg-num (elt (file-attributes msg-filename) 7)
1212 mh-show-maximum-size))))
1213 (error "Message %d not displayed" msg-num))
1214 (set-buffer show-buffer)
1215 (cond ((not (equal msg-filename buffer-file-name))
1216 (mh-unvisit-file)
1217 (setq buffer-read-only nil)
1218 (erase-buffer)
1219 ;; Changing contents, so this hook needs to be reinitialized.
1220 ;; pgp.el uses this.
1221 (if (boundp 'write-contents-hooks) ;Emacs 19
1222 (kill-local-variable 'write-contents-hooks))
1223 (if formfile
1224 (mh-exec-lib-cmd-output "mhl" "-nobell" "-noclear"
1225 (if (stringp formfile)
1226 (list "-form" formfile))
1227 msg-filename)
1228 (insert-file-contents-literally msg-filename))
1229 (if mh-decode-content-transfer-encoded-message-flag
1230 (mh-decode-content-transfer-encoded-message))
1231 ;; Cleanup old mime handles
1232 (mh-mime-cleanup)
1233 ;; Use mm to display buffer
1234 (when (and mh-decode-mime-flag (not formfile))
1235 (mh-add-missing-mime-version-header)
1236 (setf (mh-buffer-data) (mh-make-buffer-data))
1237 (mh-mime-display))
1238 ;; Header cleanup
1239 (goto-char (point-min))
1240 (cond (clean-message-header
1241 (mh-clean-msg-header (point-min)
1242 invisible-headers
1243 visible-headers)
1244 (goto-char (point-min)))
1246 (mh-start-of-uncleaned-message)))
1247 ;; the parts of visiting we want to do (no locking)
1248 (or (eq buffer-undo-list t) ;don't save undo info for prev msgs
1249 (setq buffer-undo-list nil))
1250 (set-buffer-auto-saved)
1251 ;; the parts of set-visited-file-name we want to do (no locking)
1252 (setq buffer-file-name msg-filename)
1253 (setq buffer-backed-up nil)
1254 (auto-save-mode 1)
1255 (set-mark nil)
1256 (mh-show-mode)
1257 (unwind-protect
1258 (when (and mh-decode-mime-flag (not formfile))
1259 (setq buffer-read-only nil)
1260 (mh-display-smileys)
1261 (mh-display-emphasis))
1262 (setq buffer-read-only t))
1263 (set-buffer-modified-p nil)
1264 (setq mh-show-folder-buffer folder)
1265 (setq mode-line-buffer-identification
1266 (list (format mh-show-buffer-mode-line-buffer-id
1267 folder-name msg-num)))
1268 (mh-logo-display)
1269 (set-buffer folder)
1270 (setq mh-showing-with-headers nil))))))
1272 (defun mh-clean-msg-header (start invisible-headers visible-headers)
1273 "Flush extraneous lines in message header.
1274 Header is cleaned from START to the end of the message header.
1275 INVISIBLE-HEADERS contains a regular expression specifying lines to delete
1276 from the header. VISIBLE-HEADERS contains a regular expression specifying the
1277 lines to display. INVISIBLE-HEADERS is ignored if VISIBLE-HEADERS is non-nil."
1278 (let ((case-fold-search t)
1279 (after-change-functions nil)) ;Work around emacs-20 font-lock bug
1280 ;causing an endless loop.
1281 (save-restriction
1282 (goto-char start)
1283 (if (search-forward "\n\n" nil 'move)
1284 (backward-char 1))
1285 (narrow-to-region start (point))
1286 (goto-char (point-min))
1287 (if visible-headers
1288 (while (< (point) (point-max))
1289 (cond ((looking-at visible-headers)
1290 (forward-line 1)
1291 (while (looking-at "[ \t]") (forward-line 1)))
1293 (mh-delete-line 1)
1294 (while (looking-at "[ \t]")
1295 (mh-delete-line 1)))))
1296 (while (re-search-forward invisible-headers nil t)
1297 (beginning-of-line)
1298 (mh-delete-line 1)
1299 (while (looking-at "[ \t]")
1300 (mh-delete-line 1))))
1301 (unlock-buffer))))
1303 (defun mh-delete-line (lines)
1304 "Delete the next LINES lines."
1305 (delete-region (point) (progn (forward-line lines) (point))))
1307 (defun mh-notate (msg notation offset)
1308 "Mark MSG with the character NOTATION at position OFFSET.
1309 Null MSG means the message at cursor."
1310 (save-excursion
1311 (if (or (null msg)
1312 (mh-goto-msg msg t t))
1313 (with-mh-folder-updating (t)
1314 (beginning-of-line)
1315 (forward-char offset)
1316 (delete-char 1)
1317 (insert notation)))))
1319 (defun mh-find-msg-get-num (step)
1320 "Return the message number of the message nearest the cursor.
1321 Jumps over non-message lines, such as inc errors.
1322 If we have to search, STEP tells whether to search forward or backward."
1323 (or (mh-get-msg-num nil)
1324 (let ((msg-num nil)
1325 (nreverses 0))
1326 (while (and (not msg-num)
1327 (< nreverses 2))
1328 (cond ((eobp)
1329 (setq step -1)
1330 (setq nreverses (1+ nreverses)))
1331 ((bobp)
1332 (setq step 1)
1333 (setq nreverses (1+ nreverses))))
1334 (forward-line step)
1335 (setq msg-num (mh-get-msg-num nil)))
1336 msg-num)))
1338 (defun mh-goto-msg (number &optional no-error-if-no-message dont-show)
1339 "Position the cursor at message NUMBER.
1340 Optional non-nil second argument NO-ERROR-IF-NO-MESSAGE means return nil
1341 instead of signaling an error if message does not exist; in this case, the
1342 cursor is positioned near where the message would have been.
1343 Non-nil third argument DONT-SHOW means not to show the message."
1344 (interactive "NGo to message: ")
1345 (setq number (prefix-numeric-value number))
1346 (let ((point (point))
1347 (return-value t))
1348 (goto-char (point-min))
1349 (unless (re-search-forward (format "^[ ]*%s[^0-9]+" number) nil t)
1350 (goto-char point)
1351 (unless no-error-if-no-message
1352 (error "No message %d" number))
1353 (setq return-value nil))
1354 (beginning-of-line)
1355 (or dont-show (not return-value) (mh-maybe-show number))
1356 return-value))
1358 (defun mh-msg-search-pat (n)
1359 "Return a search pattern for message N in the scan listing."
1360 (format mh-scan-msg-search-regexp n))
1362 (defun mh-get-profile-field (field)
1363 "Find and return the value of FIELD in the current buffer.
1364 Returns nil if the field is not in the buffer."
1365 (let ((case-fold-search t))
1366 (goto-char (point-min))
1367 (cond ((not (re-search-forward (format "^%s" field) nil t)) nil)
1368 ((looking-at "[\t ]*$") nil)
1370 (re-search-forward "[\t ]*\\([^\t \n].*\\)$" nil t)
1371 (let ((start (match-beginning 1)))
1372 (end-of-line)
1373 (buffer-substring start (point)))))))
1375 (defvar mail-user-agent)
1376 (defvar read-mail-command)
1378 (defvar mh-find-path-run nil
1379 "Non-nil if `mh-find-path' has been run already.")
1381 (defun mh-find-path ()
1382 "Set `mh-progs', `mh-lib', and `mh-lib-progs' variables.
1383 Set `mh-user-path', `mh-draft-folder', `mh-unseen-seq', `mh-previous-seq',
1384 `mh-inbox' from user's MH profile.
1385 The value of `mh-find-path-hook' is a list of functions to be called, with no
1386 arguments, after these variable have been set."
1387 (mh-find-progs)
1388 (unless mh-find-path-run
1389 (setq mh-find-path-run t)
1390 (setq read-mail-command 'mh-rmail)
1391 (setq mail-user-agent 'mh-e-user-agent))
1392 (save-excursion
1393 ;; Be sure profile is fully expanded before switching buffers
1394 (let ((profile (expand-file-name (or (getenv "MH") "~/.mh_profile"))))
1395 (set-buffer (get-buffer-create mh-temp-buffer))
1396 (setq buffer-offer-save nil) ;for people who set default to t
1397 (erase-buffer)
1398 (condition-case err
1399 (insert-file-contents profile)
1400 (file-error
1401 (mh-install profile err)))
1402 (setq mh-user-path (mh-get-profile-field "Path:"))
1403 (if (not mh-user-path)
1404 (setq mh-user-path "Mail"))
1405 (setq mh-user-path
1406 (file-name-as-directory
1407 (expand-file-name mh-user-path (expand-file-name "~"))))
1408 (setq mh-draft-folder (mh-get-profile-field "Draft-Folder:"))
1409 (if mh-draft-folder
1410 (progn
1411 (if (not (mh-folder-name-p mh-draft-folder))
1412 (setq mh-draft-folder (format "+%s" mh-draft-folder)))
1413 (if (not (file-exists-p (mh-expand-file-name mh-draft-folder)))
1414 (error "Draft folder \"%s\" not found. Create it and try again"
1415 (mh-expand-file-name mh-draft-folder)))))
1416 (setq mh-inbox (mh-get-profile-field "Inbox:"))
1417 (cond ((not mh-inbox)
1418 (setq mh-inbox "+inbox"))
1419 ((not (mh-folder-name-p mh-inbox))
1420 (setq mh-inbox (format "+%s" mh-inbox))))
1421 (setq mh-unseen-seq (mh-get-profile-field "Unseen-Sequence:"))
1422 (if mh-unseen-seq
1423 (setq mh-unseen-seq (intern mh-unseen-seq))
1424 (setq mh-unseen-seq 'unseen)) ;old MH default?
1425 (setq mh-previous-seq (mh-get-profile-field "Previous-Sequence:"))
1426 (if mh-previous-seq
1427 (setq mh-previous-seq (intern mh-previous-seq)))
1428 (run-hooks 'mh-find-path-hook))))
1430 (defun mh-file-command-p (file)
1431 "Return t if file FILE is the name of a executable regular file."
1432 (and (file-regular-p file) (file-executable-p file)))
1434 (defun mh-find-progs ()
1435 "Find the directories for the installed MH/nmh binaries and config files.
1436 Set the `mh-progs' and `mh-lib', and `mh-lib-progs' variables to the
1437 directory names and set `mh-nmh-flag' if we detect nmh instead of MH."
1438 (unless (and mh-progs mh-lib mh-lib-progs)
1439 (let ((path (or (mh-path-search exec-path "mhparam")
1440 (mh-path-search '("/usr/local/nmh/bin" ; nmh default
1441 "/usr/local/bin/mh/"
1442 "/usr/local/mh/"
1443 "/usr/bin/mh/" ;Ultrix 4.2, Linux
1444 "/usr/new/mh/" ;Ultrix <4.2
1445 "/usr/contrib/mh/bin/" ;BSDI
1446 "/usr/pkg/bin/" ; NetBSD
1447 "/usr/local/bin/"
1449 "mhparam"))))
1450 (if (not path)
1451 (error "Unable to find the `mhparam' command"))
1452 (save-excursion
1453 (let ((tmp-buffer (get-buffer-create mh-temp-buffer)))
1454 (set-buffer tmp-buffer)
1455 (unwind-protect
1456 (progn
1457 (call-process (expand-file-name "mhparam" path)
1458 nil '(t nil) nil "libdir" "etcdir")
1459 (goto-char (point-min))
1460 (if (search-forward-regexp "^libdir:\\s-\\(\\S-+\\)\\s-*$"
1461 nil t)
1462 (setq mh-lib-progs (match-string 1)
1463 mh-lib mh-lib-progs
1464 mh-progs path))
1465 (goto-char (point-min))
1466 (if (search-forward-regexp "^etcdir:\\s-\\(\\S-+\\)\\s-*$"
1467 nil t)
1468 (setq mh-lib (match-string 1)
1469 mh-nmh-flag t)))
1470 (kill-buffer tmp-buffer))))
1471 (unless (and mh-progs mh-lib mh-lib-progs)
1472 (error "Unable to determine paths from `mhparam' command")))))
1474 (defun mh-path-search (path file)
1475 "Search PATH, a list of directory names, for FILE.
1476 Returns the element of PATH that contains FILE, or nil if not found."
1477 (while (and path
1478 (not (funcall 'mh-file-command-p
1479 (expand-file-name file (car path)))))
1480 (setq path (cdr path)))
1481 (car path))
1483 (defvar mh-no-install nil) ;do not run install-mh
1485 (defun mh-install (profile error-val)
1486 "Initialize the MH environment.
1487 This is called if we fail to read the PROFILE file. ERROR-VAL is the error
1488 that made this call necessary."
1489 (if (or (getenv "MH")
1490 (file-exists-p profile)
1491 mh-no-install)
1492 (signal (car error-val)
1493 (list (format "Cannot read MH profile \"%s\"" profile)
1494 (car (cdr (cdr error-val))))))
1495 ;; The "install-mh" command will output a short note which
1496 ;; mh-exec-cmd will display to the user.
1497 ;; The MH 5 version of install-mh might try prompt the user
1498 ;; for information, which would fail here.
1499 (mh-exec-cmd (expand-file-name "install-mh" mh-lib-progs) "-auto")
1500 ;; now try again to read the profile file
1501 (erase-buffer)
1502 (condition-case err
1503 (insert-file-contents profile)
1504 (file-error
1505 (signal (car err) ;re-signal with more specific msg
1506 (list (format "Cannot read MH profile \"%s\"" profile)
1507 (car (cdr (cdr err))))))))
1509 (defun mh-set-folder-modified-p (flag)
1510 "Mark current folder as modified or unmodified according to FLAG."
1511 (set-buffer-modified-p flag))
1513 (defun mh-find-seq (name)
1514 "Return sequence NAME."
1515 (assoc name mh-seq-list))
1517 (defun mh-seq-to-msgs (seq)
1518 "Return a list of the messages in SEQ."
1519 (mh-seq-msgs (mh-find-seq seq)))
1521 (defun mh-update-scan-format (fmt width)
1522 "Return a scan format with the (msg) width in the FMT replaced with WIDTH.
1524 The message number width portion of the format is discovered using
1525 `mh-scan-msg-format-regexp'. Its replacement is controlled with
1526 `mh-scan-msg-format-string'."
1527 (or (and
1528 (string-match mh-scan-msg-format-regexp fmt)
1529 (let ((begin (match-beginning 1))
1530 (end (match-end 1)))
1531 (concat (substring fmt 0 begin)
1532 (format mh-scan-msg-format-string width)
1533 (substring fmt end))))
1534 fmt))
1536 (defun mh-message-number-width (folder)
1537 "Return the widest message number in this FOLDER."
1538 (or mh-progs (mh-find-path))
1539 (let ((tmp-buffer (get-buffer-create mh-temp-buffer))
1540 (width 0))
1541 (save-excursion
1542 (set-buffer tmp-buffer)
1543 (erase-buffer)
1544 (apply 'call-process
1545 (expand-file-name "scan" mh-progs) nil '(t nil) nil
1546 (list folder "last" "-format" "%(msg)"))
1547 (goto-char (point-min))
1548 (if (re-search-forward mh-scan-msg-number-regexp nil 0 1)
1549 (setq width (length (buffer-substring
1550 (match-beginning 1) (match-end 1))))))
1551 width))
1553 (defun mh-add-msgs-to-seq (msgs seq &optional internal-flag dont-annotate-flag)
1554 "Add MSGS to SEQ.
1555 Remove duplicates and keep sequence sorted. If optional INTERNAL-FLAG is
1556 non-nil, do not mark the message in the scan listing or inform MH of the
1557 addition.
1559 If DONT-ANNOTATE-FLAG is non-nil then the annotations in the folder buffer are
1560 not updated."
1561 (let ((entry (mh-find-seq seq)))
1562 (if (and msgs (atom msgs)) (setq msgs (list msgs)))
1563 (if (null entry)
1564 (setq mh-seq-list
1565 (cons (mh-make-seq seq (mh-canonicalize-sequence msgs))
1566 mh-seq-list))
1567 (if msgs (setcdr entry (mh-canonicalize-sequence
1568 (append msgs (mh-seq-msgs entry))))))
1569 (cond ((not internal-flag)
1570 (mh-add-to-sequence seq msgs)
1571 (unless dont-annotate-flag
1572 (mh-notate-seq seq mh-note-seq (1+ mh-cmd-note)))))))
1574 (defun mh-canonicalize-sequence (msgs)
1575 "Sort MSGS in decreasing order and remove duplicates."
1576 (let* ((sorted-msgs (sort (copy-sequence msgs) '>))
1577 (head sorted-msgs))
1578 (while (cdr head)
1579 (if (= (car head) (cadr head))
1580 (setcdr head (cddr head))
1581 (setq head (cdr head))))
1582 sorted-msgs))
1584 (defvar mh-sub-folders-cache (make-hash-table :test #'equal))
1586 (defun mh-normalize-folder-name (folder &optional empty-string-okay
1587 dont-remove-trailing-slash)
1588 "Normalizes FOLDER name.
1589 Makes sure that two '/' characters never occur next to each other. Also all
1590 occurrences of \"..\" and \".\" are suitably processed. So \"+inbox/../news\"
1591 will be normalized to \"+news\".
1593 If optional argument EMPTY-STRING-OKAY is nil then a '+' is added at the
1594 front if FOLDER lacks one. If non-nil and FOLDER is the empty string then
1595 nothing is added.
1597 If optional argument DONT-REMOVE-TRAILING-SLASH is non-nil then a trailing '/'
1598 if present is retained (if present), otherwise it is removed."
1599 (when (stringp folder)
1600 ;; Replace two or more consecutive '/' characters with a single '/'
1601 (while (string-match "//" folder)
1602 (setq folder (replace-match "/" nil t folder)))
1603 (let* ((length (length folder))
1604 (trailing-slash-present (and (> length 0)
1605 (equal (aref folder (1- length)) ?/))))
1606 (let ((components (split-string folder "/"))
1607 (result ()))
1608 ;; Remove .. and . from the pathname.
1609 (dolist (component components)
1610 (cond ((and (equal component "..") result)
1611 (pop result))
1612 ((equal component ".."))
1613 ((equal component "."))
1614 (t (push component result))))
1615 (setq folder "")
1616 (dolist (component result)
1617 (setq folder (concat component "/" folder)))
1618 ;; Remove trailing '/' if needed.
1619 (unless (and trailing-slash-present dont-remove-trailing-slash)
1620 (when (not (equal folder ""))
1621 (setq folder (substring folder 0 (1- (length folder))))))))
1622 (cond ((and empty-string-okay (equal folder "")))
1623 ((equal folder "") (setq folder "+"))
1624 ((not (equal (aref folder 0) ?+)) (setq folder (concat "+" folder)))))
1625 folder)
1627 (defun mh-sub-folders (folder &optional add-trailing-slash-flag)
1628 "Find the subfolders of FOLDER.
1629 The function avoids running folders unnecessarily by caching the results of
1630 the actual folders call.
1632 If optional argument ADD-TRAILING-SLASH-FLAG is non-nil then a slash is added
1633 to each of the sub-folder names that may have nested folders within them."
1634 (let* ((folder (mh-normalize-folder-name folder))
1635 (match (gethash folder mh-sub-folders-cache 'no-result))
1636 (sub-folders (cond ((eq match 'no-result)
1637 (setf (gethash folder mh-sub-folders-cache)
1638 (mh-sub-folders-actual folder)))
1639 (t match))))
1640 (if add-trailing-slash-flag
1641 (mapcar #'(lambda (x)
1642 (if (cdr x) (cons (concat (car x) "/") (cdr x)) x))
1643 sub-folders)
1644 sub-folders)))
1646 (defun mh-sub-folders-actual (folder)
1647 "Execute the command folders to return the sub-folders of FOLDER.
1648 Filters out the folder names that start with \".\" so that directories that
1649 aren't usually mail folders are hidden."
1650 (let ((arg-list `(,(expand-file-name "folders" mh-progs)
1651 nil (t nil) nil "-noheader" "-norecurse" "-nototal"
1652 ,@(if (stringp folder) (list folder) ())))
1653 (results ())
1654 (current-folder (concat
1655 (with-temp-buffer
1656 (call-process (expand-file-name "folder" mh-progs)
1657 nil '(t nil) nil "-fast")
1658 (buffer-substring (point-min) (1- (point-max))))
1659 "+")))
1660 (with-temp-buffer
1661 (apply #'call-process arg-list)
1662 (goto-char (point-min))
1663 (while (not (and (eolp) (bolp)))
1664 (goto-char (line-end-position))
1665 (let ((has-pos (search-backward " has " (line-beginning-position) t)))
1666 (when (integerp has-pos)
1667 (while (equal (char-after has-pos) ? )
1668 (decf has-pos))
1669 (incf has-pos)
1670 (let* ((name (buffer-substring (line-beginning-position) has-pos))
1671 (first-char (aref name 0))
1672 (last-char (aref name (1- (length name)))))
1673 (unless (member first-char '(?. ?# ?,))
1674 (when (and (equal last-char ?+) (equal name current-folder))
1675 (setq name (substring name 0 (1- (length name)))))
1676 (push
1677 (cons name
1678 (search-forward "(others)" (line-end-position) t))
1679 results))))
1680 (forward-line 1))))
1681 (setq results (nreverse results))
1682 (when (stringp folder)
1683 (setq results (cdr results))
1684 (let ((folder-name-len (length (format "%s/" (substring folder 1)))))
1685 (setq results (mapcar (lambda (f)
1686 (cons (substring (car f) folder-name-len)
1687 (cdr f)))
1688 results))))
1689 results))
1691 (defun mh-remove-from-sub-folders-cache (folder)
1692 "Remove FOLDER and its parent from `mh-sub-folders-cache'.
1693 FOLDER should be unconditionally removed from the cache. Also the last ancestor
1694 of FOLDER present in the cache must be removed as well.
1696 To see why this is needed assume we have a folder +foo which has a single
1697 sub-folder qux. Now we create the folder +foo/bar/baz. Here we will need to
1698 invalidate the cached sub-folders of +foo, otherwise completion on +foo won't
1699 tell us about the option +foo/bar!"
1700 (remhash folder mh-sub-folders-cache)
1701 (block ancestor-found
1702 (let ((parent folder)
1703 (one-ancestor-found nil)
1704 last-slash)
1705 (while (setq last-slash (mh-search-from-end ?/ parent))
1706 (setq parent (substring parent 0 last-slash))
1707 (unless (eq (gethash parent mh-sub-folders-cache 'none) 'none)
1708 (remhash parent mh-sub-folders-cache)
1709 (if one-ancestor-found
1710 (return-from ancestor-found)
1711 (setq one-ancestor-found t))))
1712 (remhash nil mh-sub-folders-cache))))
1714 (defvar mh-folder-hist nil)
1715 (defvar mh-speed-folder-map)
1716 (defvar mh-folder-completion-map (copy-keymap minibuffer-local-completion-map))
1717 (define-key mh-folder-completion-map " " 'minibuffer-complete)
1719 (defun mh-folder-completion-function (name predicate flag)
1720 "Programmable completion for folder names.
1721 NAME is the partial folder name that has been input. PREDICATE if non-nil is a
1722 function that is used to filter the possible choices and FLAG determines
1723 whether the completion is over."
1724 (let* ((orig-name name)
1725 (name (mh-normalize-folder-name name nil t))
1726 (last-slash (mh-search-from-end ?/ name))
1727 (last-complete (if last-slash (substring name 0 last-slash) nil))
1728 (remainder (cond (last-complete (substring name (1+ last-slash)))
1729 ((and (> (length name) 0) (equal (aref name 0) ?+))
1730 (substring name 1))
1731 (t ""))))
1732 (cond ((eq flag nil)
1733 (let ((try-res (try-completion
1734 name
1735 (mapcar (lambda (x)
1736 (cons (if (not last-complete)
1737 (concat "+" (car x))
1738 (concat last-complete "/" (car x)))
1739 (cdr x)))
1740 (mh-sub-folders last-complete t))
1741 predicate)))
1742 (cond ((eq try-res nil) nil)
1743 ((and (eq try-res t) (equal name orig-name)) t)
1744 ((eq try-res t) name)
1745 (t try-res))))
1746 ((eq flag t)
1747 (all-completions
1748 remainder (mh-sub-folders last-complete t) predicate))
1749 ((eq flag 'lambda)
1750 (file-exists-p
1751 (concat mh-user-path
1752 (substring (mh-normalize-folder-name name) 1)))))))
1754 (defun mh-folder-completing-read (prompt default)
1755 "Read folder name with PROMPT and default result DEFAULT."
1756 (mh-normalize-folder-name
1757 (let ((minibuffer-local-completion-map mh-folder-completion-map))
1758 (completing-read prompt 'mh-folder-completion-function nil nil nil
1759 'mh-folder-hist default))
1762 (defun mh-prompt-for-folder (prompt default can-create
1763 &optional default-string allow-root-folder-flag)
1764 "Prompt for a folder name with PROMPT.
1765 Returns the folder's name as a string. DEFAULT is used if the folder exists
1766 and the user types return. If the CAN-CREATE flag is t, then a folder is
1767 created if it doesn't already exist. If optional argument DEFAULT-STRING is
1768 non-nil, use it in the prompt instead of DEFAULT. If ALLOW-ROOT-FOLDER-FLAG is
1769 non-nil then the function will accept the folder +, which means all folders
1770 when used in searching."
1771 (if (null default)
1772 (setq default ""))
1773 (let* ((default-string (cond (default-string (format " [%s]? "
1774 default-string))
1775 ((equal "" default) "? ")
1776 (t (format " [%s]? " default))))
1777 (prompt (format "%s folder%s" prompt default-string))
1778 read-name folder-name)
1779 (while (and (setq read-name (mh-folder-completing-read prompt default))
1780 (equal read-name "")
1781 (equal default "")))
1782 (cond ((or (equal read-name "")
1783 (and (equal read-name "+") (not allow-root-folder-flag)))
1784 (setq read-name default))
1785 ((not (mh-folder-name-p read-name))
1786 (setq read-name (format "+%s" read-name))))
1787 (if (or (not read-name) (equal "" read-name))
1788 (error "No folder specified"))
1789 (setq folder-name read-name)
1790 (cond ((and (> (length folder-name) 0)
1791 (eq (aref folder-name (1- (length folder-name))) ?/))
1792 (setq folder-name (substring folder-name 0 -1))))
1793 (let ((new-file-flag
1794 (not (file-exists-p (mh-expand-file-name folder-name)))))
1795 (cond ((and new-file-flag
1796 (y-or-n-p
1797 (format "Folder %s does not exist. Create it? "
1798 folder-name)))
1799 (message "Creating %s" folder-name)
1800 (mh-exec-cmd-error nil "folder" folder-name)
1801 (mh-remove-from-sub-folders-cache folder-name)
1802 (when (boundp 'mh-speed-folder-map)
1803 (mh-speed-add-folder folder-name))
1804 (message "Creating %s...done" folder-name))
1805 (new-file-flag
1806 (error "Folder %s is not created" folder-name))
1807 ((not (file-directory-p (mh-expand-file-name folder-name)))
1808 (error "\"%s\" is not a directory"
1809 (mh-expand-file-name folder-name)))))
1810 folder-name))
1812 ;;; Issue commands to MH.
1814 (defun mh-exec-cmd (command &rest args)
1815 "Execute mh-command COMMAND with ARGS.
1816 The side effects are what is desired.
1817 Any output is assumed to be an error and is shown to the user.
1818 The output is not read or parsed by MH-E."
1819 (save-excursion
1820 (set-buffer (get-buffer-create mh-log-buffer))
1821 (erase-buffer)
1822 (apply 'call-process
1823 (expand-file-name command mh-progs) nil t nil
1824 (mh-list-to-string args))
1825 (if (> (buffer-size) 0)
1826 (save-window-excursion
1827 (switch-to-buffer-other-window mh-log-buffer)
1828 (sit-for 5)))))
1830 (defun mh-exec-cmd-error (env command &rest args)
1831 "In environment ENV, execute mh-command COMMAND with ARGS.
1832 ENV is nil or a string of space-separated \"var=value\" elements.
1833 Signals an error if process does not complete successfully."
1834 (save-excursion
1835 (set-buffer (get-buffer-create mh-temp-buffer))
1836 (erase-buffer)
1837 (let ((status
1838 (if env
1839 ;; the shell hacks necessary here shows just how broken Unix is
1840 (apply 'call-process "/bin/sh" nil t nil "-c"
1841 (format "%s %s ${1+\"$@\"}"
1843 (expand-file-name command mh-progs))
1844 command
1845 (mh-list-to-string args))
1846 (apply 'call-process
1847 (expand-file-name command mh-progs) nil t nil
1848 (mh-list-to-string args)))))
1849 (mh-handle-process-error command status))))
1851 (defun mh-exec-cmd-daemon (command filter &rest args)
1852 "Execute MH command COMMAND in the background.
1854 If FILTER is non-nil then it is used to process the output otherwise the
1855 default filter `mh-process-daemon' is used. See `set-process-filter' for more
1856 details of FILTER.
1858 ARGS are passed to COMMAND as command line arguments."
1859 (save-excursion
1860 (set-buffer (get-buffer-create mh-log-buffer))
1861 (erase-buffer))
1862 (let* ((process-connection-type nil)
1863 (process (apply 'start-process
1864 command nil
1865 (expand-file-name command mh-progs)
1866 (mh-list-to-string args))))
1867 (set-process-filter process (or filter 'mh-process-daemon))))
1869 (defun mh-process-daemon (process output)
1870 "PROCESS daemon that puts OUTPUT into a temporary buffer.
1871 Any output from the process is displayed in an asynchronous pop-up window."
1872 (set-buffer (get-buffer-create mh-log-buffer))
1873 (insert-before-markers output)
1874 (display-buffer mh-log-buffer))
1876 (defun mh-exec-cmd-quiet (raise-error command &rest args)
1877 "Signal RAISE-ERROR if COMMAND with ARGS fails.
1878 Execute MH command COMMAND with ARGS. ARGS is a list of strings.
1879 Return at start of mh-temp buffer, where output can be parsed and used.
1880 Returns value of `call-process', which is 0 for success, unless RAISE-ERROR is
1881 non-nil, in which case an error is signaled if `call-process' returns non-0."
1882 (set-buffer (get-buffer-create mh-temp-buffer))
1883 (erase-buffer)
1884 (let ((value
1885 (apply 'call-process
1886 (expand-file-name command mh-progs) nil t nil
1887 args)))
1888 (goto-char (point-min))
1889 (if raise-error
1890 (mh-handle-process-error command value)
1891 value)))
1893 (defun mh-profile-component (component)
1894 "Return COMPONENT value from mhparam, or nil if unset."
1895 (save-excursion
1896 (mh-exec-cmd-quiet nil "mhparam" "-components" component)
1897 (mh-get-profile-field (concat component ":"))))
1899 (defun mh-exchange-point-and-mark-preserving-active-mark ()
1900 "Put the mark where point is now, and point where the mark is now.
1901 This command works even when the mark is not active, and preserves whether the
1902 mark is active or not."
1903 (interactive nil)
1904 (let ((is-active (and (boundp 'mark-active) mark-active)))
1905 (let ((omark (mark t)))
1906 (if (null omark)
1907 (error "No mark set in this buffer"))
1908 (set-mark (point))
1909 (goto-char omark)
1910 (if (boundp 'mark-active)
1911 (setq mark-active is-active))
1912 nil)))
1914 (defun mh-exec-cmd-output (command display &rest args)
1915 "Execute MH command COMMAND with DISPLAY flag and ARGS.
1916 Put the output into buffer after point. Set mark after inserted text.
1917 Output is expected to be shown to user, not parsed by MH-E."
1918 (push-mark (point) t)
1919 (apply 'call-process
1920 (expand-file-name command mh-progs) nil t display
1921 (mh-list-to-string args))
1923 ;; The following is used instead of 'exchange-point-and-mark because the
1924 ;; latter activates the current region (between point and mark), which
1925 ;; turns on highlighting. So prior to this bug fix, doing "inc" would
1926 ;; highlight a region containing the new messages, which is undesirable.
1927 ;; The bug wasn't seen in emacs21 but still occurred in XEmacs21.4.
1928 (mh-exchange-point-and-mark-preserving-active-mark))
1930 (defun mh-exec-lib-cmd-output (command &rest args)
1931 "Execute MH library command COMMAND with ARGS.
1932 Put the output into buffer after point. Set mark after inserted text."
1933 (apply 'mh-exec-cmd-output (expand-file-name command mh-lib-progs) nil args))
1935 (defun mh-handle-process-error (command status)
1936 "Raise error if COMMAND returned non-zero STATUS, otherwise return STATUS.
1937 STATUS is return value from `call-process'.
1938 Program output is in current buffer.
1939 If output is too long to include in error message, display the buffer."
1940 (cond ((eq status 0) ;success
1941 status)
1942 ((stringp status) ;kill string
1943 (error "%s: %s" command status))
1944 (t ;exit code
1945 (cond
1946 ((= (buffer-size) 0) ;program produced no error message
1947 (error "%s: exit code %d" command status))
1949 ;; will error message fit on one line?
1950 (goto-line 2)
1951 (if (and (< (buffer-size) (frame-width))
1952 (eobp))
1953 (error "%s"
1954 (buffer-substring 1 (progn (goto-char 1)
1955 (end-of-line)
1956 (point))))
1957 (display-buffer (current-buffer))
1958 (error "%s failed with status %d. See error message in other window"
1959 command status)))))))
1961 (defun mh-list-to-string (l)
1962 "Flatten the list L and make every element of the new list into a string."
1963 (nreverse (mh-list-to-string-1 l)))
1965 (defun mh-list-to-string-1 (l)
1966 "Flatten the list L and make every element of the new list into a string."
1967 (let ((new-list nil))
1968 (while l
1969 (cond ((null (car l)))
1970 ((symbolp (car l))
1971 (setq new-list (cons (symbol-name (car l)) new-list)))
1972 ((numberp (car l))
1973 (setq new-list (cons (int-to-string (car l)) new-list)))
1974 ((equal (car l) ""))
1975 ((stringp (car l)) (setq new-list (cons (car l) new-list)))
1976 ((listp (car l))
1977 (setq new-list (nconc (mh-list-to-string-1 (car l))
1978 new-list)))
1979 (t (error "Bad element in mh-list-to-string: %s" (car l))))
1980 (setq l (cdr l)))
1981 new-list))
1983 (provide 'mh-utils)
1985 ;;; Local Variables:
1986 ;;; indent-tabs-mode: nil
1987 ;;; sentence-end-double-space: nil
1988 ;;; End:
1990 ;;; mh-utils.el ends here