1 ;;; mh-utils.el --- MH-E code needed for both sending and reading
3 ;; Copyright (C) 1993, 1995, 1997,
4 ;; 2000, 2001, 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
6 ;; Author: Bill Wohler <wohler@newt.com>
7 ;; Maintainer: Bill Wohler <wohler@newt.com>
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26 ;; Boston, MA 02110-1301, USA.
30 ;; Internal support for MH-E package.
37 (defvar recursive-load-depth-limit)
38 (if (and (boundp 'recursive-load-depth-limit)
39 (integerp recursive-load-depth-limit)
40 (< recursive-load-depth-limit 50))
41 (setq recursive-load-depth-limit 50)))
43 (eval-when-compile (require 'mh-acros))
48 (require 'mh-customize)
53 ;; Non-fatal dependencies
55 (load "mm-decode" t t)
64 (autoload 'gnus-article-highlight-citation "gnus-cite")
65 (autoload 'message-fetch-field "message")
66 (autoload 'message-tokenize-header "message")
67 (unless (fboundp 'make-hash-table)
68 (autoload 'make-hash-table "cl"))
74 (defun mh-search-from-end (char string)
75 "Return the position of last occurrence of CHAR in STRING.
76 If CHAR is not present in STRING then return nil. The function is
77 used in lieu of `search' in the CL package."
78 (loop for index from (1- (length string)) downto 0
79 when (equal (aref string index) char) return index
82 ;; Additional header fields that might someday be added:
83 ;; "Sender: " "Reply-to: "
89 (defvar mh-scan-msg-number-regexp "^ *\\([0-9]+\\)"
90 "This regular expression extracts the message number.
92 It must match from the beginning of the line. Note that the
93 message number must be placed in a parenthesized expression as in
94 the default of \"^ *\\\\([0-9]+\\\\)\".")
96 (defvar mh-scan-msg-overflow-regexp "^[?0-9][0-9]"
97 "This regular expression matches overflowed message numbers.")
99 (defvar mh-scan-msg-format-regexp "%\\([0-9]*\\)(msg)"
100 "This regular expression finds the message number width in a scan format.
102 Note that the message number must be placed in a parenthesized
103 expression as in the default of \"%\\\\([0-9]*\\\\)(msg)\". This
104 variable is only consulted if `mh-scan-format-file' is set to
105 \"Use MH-E scan Format\".")
107 (defvar mh-scan-msg-format-string "%d"
108 "This is a format string for width of the message number in a scan format.
110 Use \"0%d\" for zero-filled message numbers. This variable is only
111 consulted if `mh-scan-format-file' is set to \"Use MH-E scan
114 (defvar mh-scan-msg-search-regexp "^[^0-9]*%d[^0-9]"
115 "This regular expression matches a particular message.
117 It is a format string; use \"%d\" to represent the location of the
118 message number within the expression as in the default of
119 \"^[^0-9]*%d[^0-9]\".")
121 (defvar mh-cmd-note 4
122 "Column for notations.
124 This variable should be set with the function `mh-set-cmd-note'.
125 This variable may be updated dynamically if
126 `mh-adaptive-cmd-note-flag' is on.
128 Note that columns in Emacs start with 0.")
129 (make-variable-buffer-local 'mh-cmd-note)
131 (defvar mh-note-seq ?%
132 "Messages in a user-defined sequence are marked by this character.
134 Messages in the \"search\" sequence are marked by this character as
139 (defvar mh-show-buffer-mode-line-buffer-id " {show-%s} %d"
140 "Format string to produce `mode-line-buffer-identification' for show buffers.
142 First argument is folder name. Second is message number.")
146 (defvar mh-mail-header-separator "--------"
147 "*Line used by MH to separate headers from text in messages being composed.
149 This variable should not be used directly in programs. Programs
150 should use `mail-header-separator' instead.
151 `mail-header-separator' is initialized to
152 `mh-mail-header-separator' in `mh-letter-mode'; in other
153 contexts, you may have to perform this initialization yourself.
155 Do not make this a regular expression as it may be the argument
156 to `insert' and it is passed through `regexp-quote' before being
157 used by functions like `re-search-forward'.")
159 (defvar mh-signature-separator-regexp "^-- $"
160 "This regular expression matches the signature separator.
161 See `mh-signature-separator'.")
163 (defvar mh-signature-separator "-- \n"
164 "Text of a signature separator.
166 A signature separator is used to separate the body of a message
167 from the signature. This can be used by user agents such as MH-E
168 to render the signature differently or to suppress the inclusion
169 of the signature in a reply. Use `mh-signature-separator-regexp'
170 when searching for a separator.")
172 (defun mh-signature-separator-p ()
173 "Return non-nil if buffer includes \"^-- $\"."
175 (goto-char (point-min))
176 (re-search-forward mh-signature-separator-regexp nil t)))
178 ;; Variables for MIME display
180 ;; Structure to keep track of MIME handles on a per buffer basis.
181 (mh-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
186 (parts-count 0) ; The button number is generated from
188 (part-index-hash (make-hash-table))) ; Avoid incrementing the part number
189 ; for nested messages
191 ;; This has to be a macro, since we do: (setf (mh-buffer-data) ...)
192 (defmacro mh-buffer-data ()
193 "Convenience macro to get the MIME data structures of the current buffer."
194 `(gethash (current-buffer) mh-globals-hash))
196 (defvar mh-globals-hash (make-hash-table)
197 "Keeps track of MIME data on a per buffer basis.")
199 (defvar mh-pgp-support-flag (not (not (locate-library "mml2015")))
200 "Non-nil means PGP support is available.")
202 (defvar mh-mm-inline-media-tests
206 (mm-valid-and-fit-image-p 'jpeg handle)))
210 (mm-valid-and-fit-image-p 'png handle)))
214 (mm-valid-and-fit-image-p 'gif handle)))
218 (mm-valid-and-fit-image-p 'tiff handle)) )
222 (mm-valid-and-fit-image-p 'xbm handle)))
226 (mm-valid-and-fit-image-p 'xbm handle)))
230 (mm-valid-and-fit-image-p 'xpm handle)))
234 (mm-valid-and-fit-image-p 'xpm handle)))
238 (mm-valid-and-fit-image-p 'bmp handle)))
239 ("image/x-portable-bitmap"
242 (mm-valid-and-fit-image-p 'pbm handle)))
243 ("text/plain" mm-inline-text identity)
244 ("text/enriched" mm-inline-text identity)
245 ("text/richtext" mm-inline-text identity)
246 ("text/x-patch" mm-display-patch-inline
248 (locate-library "diff-mode")))
249 ("application/emacs-lisp" mm-display-elisp-inline identity)
250 ("application/x-emacs-lisp" mm-display-elisp-inline identity)
252 ,(if (fboundp 'mm-inline-text-html) 'mm-inline-text-html 'mm-inline-text)
254 (or (and (boundp 'mm-inline-text-html-renderer)
255 mm-inline-text-html-renderer)
256 (and (boundp 'mm-text-html-renderer) mm-text-html-renderer))))
260 (or (featurep 'vcard)
261 (locate-library "vcard"))))
262 ("message/delivery-status" mm-inline-text identity)
263 ("message/rfc822" mh-mm-inline-message identity)
264 ;;("message/partial" mm-inline-partial identity)
265 ;;("message/external-body" mm-inline-external-body identity)
266 ("text/.*" mm-inline-text identity)
267 ("audio/wav" mm-inline-audio
269 (and (or (featurep 'nas-sound) (featurep 'native-sound))
270 (device-sound-enabled-p))))
274 (and (or (featurep 'nas-sound) (featurep 'native-sound))
275 (device-sound-enabled-p))))
276 ("application/pgp-signature" ignore identity)
277 ("application/x-pkcs7-signature" ignore identity)
278 ("application/pkcs7-signature" ignore identity)
279 ("application/x-pkcs7-mime" ignore identity)
280 ("application/pkcs7-mime" ignore identity)
281 ("multipart/alternative" ignore identity)
282 ("multipart/mixed" ignore identity)
283 ("multipart/related" ignore identity)
284 ;; Disable audio and image
285 ("audio/.*" ignore ignore)
286 ("image/.*" ignore ignore)
287 ;; Default to displaying as text
288 (".*" mm-inline-text mm-readable-p))
289 "Alist of media types/tests saying whether types can be displayed inline.")
291 ;; Copy of `goto-address-mail-regexp'
292 (defvar mh-address-mail-regexp
293 "[-a-zA-Z0-9._]+@\\([-a-zA-z0-9_]+\\.\\)+[a-zA-Z0-9]+"
294 "A regular expression probably matching an e-mail address.")
296 ;; From goto-addr.el, which we don't want to force-load on users.
298 (defun mh-goto-address-find-address-at-point ()
299 "Find e-mail address around or before point.
301 Then search backwards to beginning of line for the start of an
302 e-mail address. If no e-mail address found, return nil."
303 (re-search-backward "[^-_A-z0-9.@]" (line-beginning-position) 'lim)
304 (if (or (looking-at mh-address-mail-regexp) ; already at start
305 (and (re-search-forward mh-address-mail-regexp
306 (line-end-position) 'lim)
307 (goto-char (match-beginning 0))))
308 (match-string-no-properties 0)))
310 (defun mh-mail-header-end ()
311 "Substitute for `mail-header-end' that doesn't widen the buffer.
313 In MH-E we frequently need to find the end of headers in nested
314 messages, where the buffer has been narrowed. This function works
317 ;; XXX: The following replaces a call to rfc822-goto-eoh. Occasionally,
318 ;; mail headers that MH-E has to read contains lines of the form:
319 ;; From xxx@yyy Mon May 10 11:48:07 2004
320 ;; In this situation, rfc822-goto-eoh doesn't go to the end of the
321 ;; header. The replacement allows From_ lines in the mail header.
322 (goto-char (point-min))
323 (loop for p = (re-search-forward
324 "^\\([:\n]\\|[^: \t\n]+[ \t\n]\\)" nil 'move)
325 do (cond ((null p) (return))
326 (t (goto-char (match-beginning 0))
327 (unless (looking-at "From ") (return))
331 (defun mh-in-header-p ()
332 "Return non-nil if the point is in the header of a draft message."
333 (< (point) (mh-mail-header-end)))
335 (defun mh-header-field-beginning ()
336 "Move to the beginning of the current header field.
337 Handles RFC 822 continuation lines."
339 (while (looking-at "^[ \t]")
342 (defun mh-header-field-end ()
343 "Move to the end of the current header field.
344 Handles RFC 822 continuation lines."
346 (while (looking-at "^[ \t]")
348 (backward-char 1)) ;to end of previous line
350 (defun mh-letter-header-font-lock (limit)
351 "Return the entire mail header to font-lock.
352 Argument LIMIT limits search."
353 (if (= (point) limit)
355 (let* ((mail-header-end (save-match-data (mh-mail-header-end)))
356 (lesser-limit (if (< mail-header-end limit) mail-header-end limit)))
357 (when (mh-in-header-p)
358 (set-match-data (list 1 lesser-limit))
359 (goto-char lesser-limit)
362 (defun mh-header-field-font-lock (field limit)
363 "Return the value of a header field FIELD to font-lock.
364 Argument LIMIT limits search."
365 (if (= (point) limit)
367 (let* ((mail-header-end (mh-mail-header-end))
368 (lesser-limit (if (< mail-header-end limit) mail-header-end limit))
369 (case-fold-search t))
370 (when (and (< (point) mail-header-end) ;Only within header
371 (re-search-forward (format "^%s" field) lesser-limit t))
372 (let ((match-one-b (match-beginning 0))
373 (match-one-e (match-end 0)))
374 (mh-header-field-end)
375 (if (> (point) limit) ;Don't search for end beyond limit
377 (set-match-data (list match-one-b match-one-e
378 (1+ match-one-e) (point)))
381 (defun mh-header-to-font-lock (limit)
382 "Return the value of a header field To to font-lock.
383 Argument LIMIT limits search."
384 (mh-header-field-font-lock "To:" limit))
386 (defun mh-header-cc-font-lock (limit)
387 "Return the value of a header field cc to font-lock.
388 Argument LIMIT limits search."
389 (mh-header-field-font-lock "cc:" limit))
391 (defun mh-header-subject-font-lock (limit)
392 "Return the value of a header field Subject to font-lock.
393 Argument LIMIT limits search."
394 (mh-header-field-font-lock "Subject:" limit))
397 ;; Otherwise byte-compilation fails on `mh-show-font-lock-keywords-with-cite'
398 (defvar mh-show-font-lock-keywords
399 '(("^\\(From:\\|Sender:\\)\\(.*\\)"
402 (mh-header-to-font-lock
405 (mh-header-cc-font-lock
408 ("^\\(Reply-To:\\|Return-Path:\\)\\(.*\\)$"
411 (mh-header-subject-font-lock
413 (1 'mh-show-subject))
414 ("^\\(Apparently-To:\\|Newsgroups:\\)\\(.*\\)"
417 ("^\\(In-reply-to\\|Date\\):\\(.*\\)$"
420 (mh-letter-header-font-lock
421 (0 'mh-show-header append t)))
422 "Additional expressions to highlight in MH-Show buffers."))
424 (defvar mh-show-font-lock-keywords-with-cite
426 (let* ((cite-chars "[>|}]")
427 (cite-prefix "A-Za-z")
428 (cite-suffix (concat cite-prefix "0-9_.@-`'\"")))
430 mh-show-font-lock-keywords
432 ;; Use MATCH-ANCHORED to effectively anchor the regexp left side.
434 (,(concat "\\=[ \t]*"
435 "\\(\\([" cite-prefix "]+[" cite-suffix "]*\\)?"
436 "\\(" cite-chars "[ \t]*\\)\\)+"
438 (beginning-of-line) (end-of-line)
439 (2 font-lock-constant-face nil t)
440 (4 font-lock-comment-face nil t)))))))
441 "Additional expressions to highlight in MH-Show buffers.")
443 (defvar mh-letter-font-lock-keywords
444 `(,@mh-show-font-lock-keywords-with-cite
445 (mh-font-lock-field-data
446 (1 'mh-letter-header-field prepend t)))
447 "Additional expressions to highlight in MH-Letter buffers.")
449 (defun mh-show-font-lock-fontify-region (beg end loudly)
450 "Limit font-lock in `mh-show-mode' to the header.
452 Used when the option `mh-highlight-citation-style' is set to
453 \"Gnus\", leaving the body to be dealt with by Gnus highlighting.
454 The region between BEG and END is given over to be fontified and
455 LOUDLY controls if a user sees a message about the fontification
457 (let ((header-end (mh-mail-header-end)))
459 ((and (< beg header-end)(< end header-end))
460 (font-lock-default-fontify-region beg end loudly))
461 ((and (< beg header-end)(>= end header-end))
462 (font-lock-default-fontify-region beg header-end loudly))
466 ;; Needed to help shush the byte-compiler.
471 (require 'gnus-cite)))
473 (defun mh-gnus-article-highlight-citation ()
474 "Highlight cited text in current buffer using Gnus."
476 ;; Requiring gnus-cite should have been sufficient. However for Emacs21.1,
477 ;; recursive-load-depth-limit is only 10, so an error occurs. Also it may be
478 ;; better to have an autoload at top-level (though that won't work because
479 ;; of recursive-load-depth-limit). That gets rid of a compiler warning as
481 (unless mh-xemacs-flag
483 (require 'gnus-cite))
484 ;; Don't allow Gnus to create buttons while highlighting, maybe this is bad
486 (flet ((gnus-article-add-button (&rest args) nil))
487 (let* ((modified (buffer-modified-p))
488 (gnus-article-buffer (buffer-name))
489 (gnus-cite-face-list `(,@(cdr gnus-cite-face-list)
490 ,(car gnus-cite-face-list))))
491 (gnus-article-highlight-citation t)
492 (set-buffer-modified-p modified))))
496 ;;; Internal bookkeeping variables:
498 (defvar mh-user-path nil
499 "Cached value of the \"Path:\" MH profile component.
500 User's mail folder directory.")
502 (defvar mh-draft-folder nil
503 "Cached value of the \"Draft-Folder:\" MH profile component.
504 Name of folder containing draft messages.
505 Nil means do not use a draft folder.")
507 (defvar mh-unseen-seq nil
508 "Cached value of the \"Unseen-Sequence:\" MH profile component.
509 Name of the Unseen sequence.")
511 (defvar mh-previous-seq nil
512 "Cached value of the \"Previous-Sequence:\" MH profile component.
513 Name of the Previous sequence.")
516 "Cached value of the \"Inbox:\" MH profile component.
517 Set to \"+inbox\" if no such component.
518 Name of the Inbox folder.")
520 ;; The names of ephemeral buffers have a " *mh-" prefix (so that they are
521 ;; hidden and can be programmatically removed in mh-quit), and the variable
522 ;; names have the form mh-temp-.*-buffer.
523 (defconst mh-temp-buffer " *mh-temp*") ;scratch
524 (defconst mh-temp-fetch-buffer " *mh-fetch*") ;wget/curl/fetch output
526 ;; The names of MH-E buffers that are not ephemeral and can be used by the
527 ;; user (and deleted by the user when no longer needed) have a "*MH-E " prefix
528 ;; (so they can be programmatically removed in mh-quit), and the variable
529 ;; names have the form mh-.*-buffer.
530 (defconst mh-aliases-buffer "*MH-E Aliases*") ;alias lookups
531 (defconst mh-folders-buffer "*MH-E Folders*") ;folder list
532 (defconst mh-help-buffer "*MH-E Help*") ;quick help
533 (defconst mh-info-buffer "*MH-E Info*") ;version information buffer
534 (defconst mh-log-buffer "*MH-E Log*") ;output of MH commands and so on
535 (defconst mh-mail-delivery-buffer "*MH-E Mail Delivery*") ;mail delivery log
536 (defconst mh-recipients-buffer "*MH-E Recipients*") ;killed when draft sent
537 (defconst mh-sequences-buffer "*MH-E Sequences*") ;sequences list
539 (defvar mh-log-buffer-lines 100
540 "Number of lines to keep in `mh-log-buffer'.")
542 (defvar mh-previous-window-config nil
543 "Window configuration before MH-E command.")
545 (defvar mh-page-to-next-msg-flag nil
546 "Non-nil means next SPC or whatever goes to next undeleted message.")
550 ;;; Internal variables local to a folder.
552 (defvar mh-current-folder nil
553 "Name of current folder, a string.")
555 (defvar mh-show-buffer nil
556 "Buffer that displays message for this folder.")
558 (defvar mh-folder-filename nil
559 "Full path of directory for this folder.")
561 (defvar mh-msg-count nil
562 "Number of msgs in buffer.")
564 (defvar mh-showing-mode nil
565 "If non-nil, show the message in a separate window.")
567 (defvar mh-show-mode-map (make-sparse-keymap)
568 "Keymap used by the show buffer.")
570 (defvar mh-show-folder-buffer nil
571 "Keeps track of folder whose message is being displayed.")
573 (defvar mh-logo-cache nil)
575 (defun mh-logo-display ()
576 "Modify mode line to display MH-E logo."
580 `(display ,(or mh-logo-cache
582 (mh-funcall-if-exists
583 find-image '((:type xpm :ascent center
584 :file "mh-logo.xpm"))))))
585 (car mode-line-buffer-identification)))
587 (setq modeline-buffer-identification
589 (if mh-modeline-glyph
590 (cons modeline-buffer-id-left-extent mh-modeline-glyph)
591 (cons modeline-buffer-id-left-extent "XEmacs%N:"))
592 (cons modeline-buffer-id-right-extent " %17b")))))
594 (defun mh-showing-mode (&optional arg)
595 "Change whether messages should be displayed.
597 With ARG, display messages iff ARG is positive."
598 (setq mh-showing-mode
600 (not mh-showing-mode)
601 (> (prefix-numeric-value arg) 0))))
603 (defvar mh-seq-list nil
604 "Alist of this folder's sequences.
605 Elements have the form (SEQUENCE . MESSAGES).")
607 (defvar mh-seen-list nil
608 "List of displayed messages to be removed from the \"Unseen\" sequence.")
610 (defvar mh-showing-with-headers nil
611 "If non-nil, MH-Show buffer contains message with all header fields.
612 If nil, MH-Show buffer contains message processed normally.")
618 (defmacro with-mh-folder-updating (save-modification-flag &rest body)
619 "Format is (with-mh-folder-updating (SAVE-MODIFICATION-FLAG) &body BODY).
620 Execute BODY, which can modify the folder buffer without having to
621 worry about file locking or the read-only flag, and return its result.
622 If SAVE-MODIFICATION-FLAG is non-nil, the buffer's modification flag
623 is unchanged, otherwise it is cleared."
624 (setq save-modification-flag (car save-modification-flag)) ; CL style
626 (let ((mh-folder-updating-mod-flag (buffer-modified-p))
627 (buffer-read-only nil)
628 (buffer-file-name nil)) ;don't let the buffer get locked
632 (mh-set-folder-modified-p mh-folder-updating-mod-flag)))
633 ,@(if (not save-modification-flag)
634 '((mh-set-folder-modified-p nil)))))
636 (put 'with-mh-folder-updating 'lisp-indent-hook 'defun)
638 (defmacro mh-in-show-buffer (show-buffer &rest body)
639 "Format is (mh-in-show-buffer (SHOW-BUFFER) &body BODY).
640 Display buffer SHOW-BUFFER in other window and execute BODY in it.
641 Stronger than `save-excursion', weaker than `save-window-excursion'."
642 (setq show-buffer (car show-buffer)) ; CL style
643 `(let ((mh-in-show-buffer-saved-window (selected-window)))
644 (switch-to-buffer-other-window ,show-buffer)
645 (if mh-bury-show-buffer-flag (bury-buffer (current-buffer)))
649 (select-window mh-in-show-buffer-saved-window))))
651 (put 'mh-in-show-buffer 'lisp-indent-hook 'defun)
653 (defmacro mh-do-at-event-location (event &rest body)
654 "Switch to the location of EVENT and execute BODY.
655 After BODY has been executed return to original window. The
656 modification flag of the buffer in the event window is
658 (let ((event-window (make-symbol "event-window"))
659 (event-position (make-symbol "event-position"))
660 (original-window (make-symbol "original-window"))
661 (original-position (make-symbol "original-position"))
662 (modified-flag (make-symbol "modified-flag")))
664 (let* ((,event-window
665 (or (mh-funcall-if-exists posn-window (event-start ,event))
666 (mh-funcall-if-exists event-window ,event)))
668 (or (mh-funcall-if-exists posn-point (event-start ,event))
669 (mh-funcall-if-exists event-closest-point ,event)))
670 (,original-window (selected-window))
671 (,original-position (progn
672 (set-buffer (window-buffer ,event-window))
673 (set-marker (make-marker) (point))))
674 (,modified-flag (buffer-modified-p))
675 (buffer-read-only nil))
676 (unwind-protect (progn
677 (select-window ,event-window)
678 (goto-char ,event-position)
680 (set-buffer-modified-p ,modified-flag)
681 (goto-char ,original-position)
682 (set-marker ,original-position nil)
683 (select-window ,original-window))))))
685 (put 'mh-do-at-event-location 'lisp-indent-hook 'defun)
687 (defmacro mh-make-seq (name msgs)
688 "Create sequence NAME with the given MSGS."
689 (list 'cons name msgs))
691 (defmacro mh-seq-name (sequence)
692 "Extract sequence name from the given SEQUENCE."
693 (list 'car sequence))
695 (defmacro mh-seq-msgs (sequence)
696 "Extract messages from the given SEQUENCE."
697 (list 'cdr sequence))
699 (defun mh-recenter (arg)
700 "Like recenter but with three improvements:
702 - At the end of the buffer it tries to show fewer empty lines.
704 - operates only if the current buffer is in the selected window.
705 (Commands like `save-some-buffers' can make this false.)
707 - nil ARG means recenter as if prefix argument had been given."
708 (cond ((not (eq (get-buffer-window (current-buffer)) (selected-window)))
710 ((= (point-max) (save-excursion
711 (forward-line (- (/ (window-height) 2) 2))
713 (let ((lines-from-end 2))
715 (while (> (point-max) (progn (forward-line) (point)))
716 (incf lines-from-end)))
717 (recenter (- lines-from-end))))
718 ;; '(4) is the same as C-u prefix argument.
719 (t (recenter (or arg '(4))))))
721 (defun mh-start-of-uncleaned-message ()
722 "Position uninteresting headers off the top of the window."
723 (let ((case-fold-search t))
725 "^To:\\|^Cc:\\|^From:\\|^Subject:\\|^Date:" nil t)
729 (defun mh-invalidate-show-buffer ()
730 "Invalidate the show buffer so we must update it to use it."
731 (if (get-buffer mh-show-buffer)
733 (set-buffer mh-show-buffer)
736 (defun mh-unvisit-file ()
737 "Separate current buffer from the message file it was visiting."
738 (or (not (buffer-modified-p))
739 (null buffer-file-name) ;we've been here before
740 (yes-or-no-p (format "Message %s modified; flush changes? "
741 (file-name-nondirectory buffer-file-name)))
742 (error "Flushing changes not confirmed"))
743 (clear-visited-file-modtime)
745 (setq buffer-file-name nil))
747 (defun mh-get-msg-num (error-if-no-message)
748 "Return the message number of the displayed message.
749 If the argument ERROR-IF-NO-MESSAGE is non-nil, then complain if
750 the cursor is not pointing to a message."
753 (cond ((looking-at mh-scan-msg-number-regexp)
754 (string-to-number (buffer-substring (match-beginning 1)
757 (error "Cursor not pointing to message"))
760 (defun mh-folder-name-p (name)
761 "Return non-nil if NAME is the name of a folder.
762 A name (a string or symbol) can be a folder name if it begins
765 (eq (aref (symbol-name name) 0) ?+)
766 (and (> (length name) 0)
767 (eq (aref name 0) ?+))))
770 (defun mh-expand-file-name (filename &optional default)
771 "Expand FILENAME like `expand-file-name', but also handle MH folder names.
772 Any filename that starts with '+' is treated as a folder name.
773 See `expand-file-name' for description of DEFAULT."
774 (if (mh-folder-name-p filename)
775 (expand-file-name (substring filename 1) mh-user-path)
776 (expand-file-name filename default)))
779 (defun mh-msg-filename (msg &optional folder)
780 "Return the file name of MSG in FOLDER (default current folder)."
781 (expand-file-name (int-to-string msg)
783 (mh-expand-file-name folder)
784 mh-folder-filename)))
786 ;; Infrastructure to generate show-buffer functions from folder functions
787 ;; XEmacs does not have deactivate-mark? What is the equivalent of
788 ;; transient-mark-mode for XEmacs? Should we be restoring the mark in the
789 ;; folder buffer after the operation has been carried out.
790 (defmacro mh-defun-show-buffer (function original-function
791 &optional dont-return)
792 "Define FUNCTION to run ORIGINAL-FUNCTION in folder buffer.
793 If the buffer we start in is still visible and DONT-RETURN is nil
794 then switch to it after that."
796 ,(format "Calls %s from the message's folder.\n%s\nSee \"%s\" for more info.\n"
799 "When function completes, returns to the show buffer if it is
803 (when (buffer-live-p (get-buffer mh-show-folder-buffer))
804 (let ((config (current-window-configuration))
805 (folder-buffer mh-show-folder-buffer)
807 ,@(if dont-return () '((cur-buffer-name (buffer-name)))))
808 (pop-to-buffer mh-show-folder-buffer nil)
809 (unless (equal (buffer-name
810 (window-buffer (frame-first-window (selected-frame))))
812 (delete-other-windows))
814 (mh-funcall-if-exists deactivate-mark)
816 (prog1 (call-interactively (function ,original-function))
817 (setq normal-exit t))
818 (mh-funcall-if-exists deactivate-mark)
819 (when (eq major-mode 'mh-folder-mode)
820 (mh-funcall-if-exists hl-line-highlight))
821 (cond ((not normal-exit)
822 (set-window-configuration config))
824 `(t (setq mh-previous-window-config config))
825 `((and (get-buffer cur-buffer-name)
826 (window-live-p (get-buffer-window
827 (get-buffer cur-buffer-name))))
828 (pop-to-buffer (get-buffer cur-buffer-name) nil)))))))))
830 ;; Generate interactive functions for the show buffer from the corresponding
832 (mh-defun-show-buffer mh-show-previous-undeleted-msg
833 mh-previous-undeleted-msg)
834 (mh-defun-show-buffer mh-show-next-undeleted-msg
835 mh-next-undeleted-msg)
836 (mh-defun-show-buffer mh-show-quit mh-quit)
837 (mh-defun-show-buffer mh-show-delete-msg mh-delete-msg)
838 (mh-defun-show-buffer mh-show-refile-msg mh-refile-msg)
839 (mh-defun-show-buffer mh-show-undo mh-undo)
840 (mh-defun-show-buffer mh-show-execute-commands mh-execute-commands)
841 (mh-defun-show-buffer mh-show-reply mh-reply t)
842 (mh-defun-show-buffer mh-show-redistribute mh-redistribute)
843 (mh-defun-show-buffer mh-show-forward mh-forward t)
844 (mh-defun-show-buffer mh-show-header-display mh-header-display)
845 (mh-defun-show-buffer mh-show-refile-or-write-again
846 mh-refile-or-write-again)
847 (mh-defun-show-buffer mh-show-show mh-show)
848 (mh-defun-show-buffer mh-show-write-message-to-file
849 mh-write-msg-to-file)
850 (mh-defun-show-buffer mh-show-extract-rejected-mail
851 mh-extract-rejected-mail t)
852 (mh-defun-show-buffer mh-show-delete-msg-no-motion
853 mh-delete-msg-no-motion)
854 (mh-defun-show-buffer mh-show-first-msg mh-first-msg)
855 (mh-defun-show-buffer mh-show-last-msg mh-last-msg)
856 (mh-defun-show-buffer mh-show-copy-msg mh-copy-msg)
857 (mh-defun-show-buffer mh-show-edit-again mh-edit-again t)
858 (mh-defun-show-buffer mh-show-goto-msg mh-goto-msg)
859 (mh-defun-show-buffer mh-show-inc-folder mh-inc-folder)
860 (mh-defun-show-buffer mh-show-delete-subject-or-thread
861 mh-delete-subject-or-thread)
862 (mh-defun-show-buffer mh-show-delete-subject mh-delete-subject)
863 (mh-defun-show-buffer mh-show-print-msg mh-print-msg)
864 (mh-defun-show-buffer mh-show-send mh-send t)
865 (mh-defun-show-buffer mh-show-toggle-showing mh-toggle-showing t)
866 (mh-defun-show-buffer mh-show-pipe-msg mh-pipe-msg t)
867 (mh-defun-show-buffer mh-show-sort-folder mh-sort-folder)
868 (mh-defun-show-buffer mh-show-visit-folder mh-visit-folder t)
869 (mh-defun-show-buffer mh-show-rescan-folder mh-rescan-folder)
870 (mh-defun-show-buffer mh-show-pack-folder mh-pack-folder)
871 (mh-defun-show-buffer mh-show-kill-folder mh-kill-folder t)
872 (mh-defun-show-buffer mh-show-list-folders mh-list-folders t)
873 (mh-defun-show-buffer mh-show-search-folder mh-search-folder t)
874 (mh-defun-show-buffer mh-show-undo-folder mh-undo-folder)
875 (mh-defun-show-buffer mh-show-delete-msg-from-seq
876 mh-delete-msg-from-seq)
877 (mh-defun-show-buffer mh-show-delete-seq mh-delete-seq)
878 (mh-defun-show-buffer mh-show-list-sequences mh-list-sequences)
879 (mh-defun-show-buffer mh-show-narrow-to-seq mh-narrow-to-seq)
880 (mh-defun-show-buffer mh-show-put-msg-in-seq mh-put-msg-in-seq)
881 (mh-defun-show-buffer mh-show-msg-is-in-seq mh-msg-is-in-seq)
882 (mh-defun-show-buffer mh-show-widen mh-widen)
883 (mh-defun-show-buffer mh-show-narrow-to-subject mh-narrow-to-subject)
884 (mh-defun-show-buffer mh-show-narrow-to-from mh-narrow-to-from)
885 (mh-defun-show-buffer mh-show-narrow-to-cc mh-narrow-to-cc)
886 (mh-defun-show-buffer mh-show-narrow-to-range mh-narrow-to-range)
887 (mh-defun-show-buffer mh-show-narrow-to-to mh-narrow-to-to)
888 (mh-defun-show-buffer mh-show-store-msg mh-store-msg)
889 (mh-defun-show-buffer mh-show-page-digest mh-page-digest)
890 (mh-defun-show-buffer mh-show-page-digest-backwards
891 mh-page-digest-backwards)
892 (mh-defun-show-buffer mh-show-burst-digest mh-burst-digest)
893 (mh-defun-show-buffer mh-show-page-msg mh-page-msg)
894 (mh-defun-show-buffer mh-show-previous-page mh-previous-page)
895 (mh-defun-show-buffer mh-show-modify mh-modify t)
896 (mh-defun-show-buffer mh-show-next-button mh-next-button)
897 (mh-defun-show-buffer mh-show-prev-button mh-prev-button)
898 (mh-defun-show-buffer mh-show-toggle-mime-part mh-folder-toggle-mime-part)
899 (mh-defun-show-buffer mh-show-save-mime-part mh-folder-save-mime-part)
900 (mh-defun-show-buffer mh-show-inline-mime-part mh-folder-inline-mime-part)
901 (mh-defun-show-buffer mh-show-toggle-threads mh-toggle-threads)
902 (mh-defun-show-buffer mh-show-thread-delete mh-thread-delete)
903 (mh-defun-show-buffer mh-show-thread-refile mh-thread-refile)
904 (mh-defun-show-buffer mh-show-update-sequences mh-update-sequences)
905 (mh-defun-show-buffer mh-show-next-unread-msg mh-next-unread-msg)
906 (mh-defun-show-buffer mh-show-previous-unread-msg mh-previous-unread-msg)
907 (mh-defun-show-buffer mh-show-thread-ancestor mh-thread-ancestor)
908 (mh-defun-show-buffer mh-show-thread-next-sibling mh-thread-next-sibling)
909 (mh-defun-show-buffer mh-show-thread-previous-sibling
910 mh-thread-previous-sibling)
911 (mh-defun-show-buffer mh-show-index-visit-folder mh-index-visit-folder t)
912 (mh-defun-show-buffer mh-show-toggle-tick mh-toggle-tick)
913 (mh-defun-show-buffer mh-show-narrow-to-tick mh-narrow-to-tick)
914 (mh-defun-show-buffer mh-show-junk-blacklist mh-junk-blacklist)
915 (mh-defun-show-buffer mh-show-junk-whitelist mh-junk-whitelist)
916 (mh-defun-show-buffer mh-show-index-new-messages mh-index-new-messages)
917 (mh-defun-show-buffer mh-show-index-ticked-messages mh-index-ticked-messages)
918 (mh-defun-show-buffer mh-show-index-sequenced-messages
919 mh-index-sequenced-messages)
920 (mh-defun-show-buffer mh-show-catchup mh-catchup)
921 (mh-defun-show-buffer mh-show-ps-print-toggle-color mh-ps-print-toggle-color)
922 (mh-defun-show-buffer mh-show-ps-print-toggle-faces mh-ps-print-toggle-faces)
923 (mh-defun-show-buffer mh-show-ps-print-msg-file mh-ps-print-msg-file)
924 (mh-defun-show-buffer mh-show-ps-print-msg mh-ps-print-msg)
925 (mh-defun-show-buffer mh-show-toggle-mime-buttons mh-toggle-mime-buttons)
926 (mh-defun-show-buffer mh-show-display-with-external-viewer
927 mh-display-with-external-viewer)
931 ;;; Build mh-show-mode keymaps
933 (gnus-define-keys mh-show-mode-map
935 "!" mh-show-refile-or-write-again
936 "'" mh-show-toggle-tick
937 "," mh-show-header-display
939 ">" mh-show-write-message-to-file
941 "E" mh-show-extract-rejected-mail
943 "\177" mh-show-previous-page
944 "\C-d" mh-show-delete-msg-no-motion
945 "\t" mh-show-next-button
946 [backtab] mh-show-prev-button
947 "\M-\t" mh-show-prev-button
948 "\ed" mh-show-redistribute
949 "^" mh-show-refile-msg
951 "d" mh-show-delete-msg
952 "e" mh-show-edit-again
955 "i" mh-show-inc-folder
956 "k" mh-show-delete-subject-or-thread
958 "n" mh-show-next-undeleted-msg
959 "\M-n" mh-show-next-unread-msg
960 "o" mh-show-refile-msg
961 "p" mh-show-previous-undeleted-msg
962 "\M-p" mh-show-previous-unread-msg
966 "t" mh-show-toggle-showing
968 "x" mh-show-execute-commands
969 "v" mh-show-index-visit-folder
970 "|" mh-show-pipe-msg)
972 (gnus-define-keys (mh-show-folder-map "F" mh-show-mode-map)
974 "'" mh-index-ticked-messages
975 "S" mh-show-sort-folder
977 "f" mh-show-visit-folder
979 "k" mh-show-kill-folder
980 "l" mh-show-list-folders
981 "n" mh-index-new-messages
982 "o" mh-show-visit-folder
983 "q" mh-show-index-sequenced-messages
984 "r" mh-show-rescan-folder
985 "s" mh-show-search-folder
986 "t" mh-show-toggle-threads
987 "u" mh-show-undo-folder
988 "v" mh-show-visit-folder)
990 (gnus-define-keys (mh-show-sequence-map "S" mh-show-mode-map)
991 "'" mh-show-narrow-to-tick
993 "d" mh-show-delete-msg-from-seq
994 "k" mh-show-delete-seq
995 "l" mh-show-list-sequences
996 "n" mh-show-narrow-to-seq
997 "p" mh-show-put-msg-in-seq
998 "s" mh-show-msg-is-in-seq
1001 (define-key mh-show-mode-map "I" mh-inc-spool-map)
1003 (gnus-define-keys (mh-show-junk-map "J" mh-show-mode-map)
1005 "b" mh-show-junk-blacklist
1006 "w" mh-show-junk-whitelist)
1008 (gnus-define-keys (mh-show-ps-print-map "P" mh-show-mode-map)
1010 "C" mh-show-ps-print-toggle-color
1011 "F" mh-show-ps-print-toggle-faces
1012 "f" mh-show-ps-print-msg-file
1013 "l" mh-show-print-msg
1014 "p" mh-show-ps-print-msg)
1016 (gnus-define-keys (mh-show-thread-map "T" mh-show-mode-map)
1018 "u" mh-show-thread-ancestor
1019 "p" mh-show-thread-previous-sibling
1020 "n" mh-show-thread-next-sibling
1021 "t" mh-show-toggle-threads
1022 "d" mh-show-thread-delete
1023 "o" mh-show-thread-refile)
1025 (gnus-define-keys (mh-show-limit-map "/" mh-show-mode-map)
1026 "'" mh-show-narrow-to-tick
1028 "c" mh-show-narrow-to-cc
1029 "f" mh-show-narrow-to-from
1030 "r" mh-show-narrow-to-range
1031 "s" mh-show-narrow-to-subject
1032 "t" mh-show-narrow-to-to
1035 (gnus-define-keys (mh-show-extract-map "X" mh-show-mode-map)
1037 "s" mh-show-store-msg
1038 "u" mh-show-store-msg)
1041 (gnus-define-keys (mh-show-digest-map "D" mh-show-mode-map)
1043 " " mh-show-page-digest
1044 "\177" mh-show-page-digest-backwards
1045 "b" mh-show-burst-digest)
1047 (gnus-define-keys (mh-show-mime-map "K" mh-show-mode-map)
1049 "a" mh-mime-save-parts
1050 "e" mh-show-display-with-external-viewer
1051 "v" mh-show-toggle-mime-part
1052 "o" mh-show-save-mime-part
1053 "i" mh-show-inline-mime-part
1054 "t" mh-show-toggle-mime-buttons
1055 "\t" mh-show-next-button
1056 [backtab] mh-show-prev-button
1057 "\M-\t" mh-show-prev-button)
1060 mh-show-sequence-menu mh-show-mode-map "Menu for MH-E folder-sequence."
1062 ["Add Message to Sequence..." mh-show-put-msg-in-seq t]
1063 ["List Sequences for Message" mh-show-msg-is-in-seq t]
1064 ["Delete Message from Sequence..." mh-show-delete-msg-from-seq t]
1065 ["List Sequences in Folder..." mh-show-list-sequences t]
1066 ["Delete Sequence..." mh-show-delete-seq t]
1067 ["Narrow to Sequence..." mh-show-narrow-to-seq t]
1068 ["Widen from Sequence" mh-show-widen t]
1070 ["Narrow to Subject Sequence" mh-show-narrow-to-subject t]
1071 ["Narrow to Tick Sequence" mh-show-narrow-to-tick
1073 (set-buffer mh-show-folder-buffer)
1074 (and mh-tick-seq (mh-seq-msgs (mh-find-seq mh-tick-seq))))]
1075 ["Delete Rest of Same Subject" mh-show-delete-subject t]
1076 ["Toggle Tick Mark" mh-show-toggle-tick t]
1078 ["Push State Out to MH" mh-show-update-sequences t]))
1081 mh-show-message-menu mh-show-mode-map "Menu for MH-E folder-message."
1083 ["Show Message" mh-show-show t]
1084 ["Show Message with Header" mh-show-header-display t]
1085 ["Next Message" mh-show-next-undeleted-msg t]
1086 ["Previous Message" mh-show-previous-undeleted-msg t]
1087 ["Go to First Message" mh-show-first-msg t]
1088 ["Go to Last Message" mh-show-last-msg t]
1089 ["Go to Message by Number..." mh-show-goto-msg t]
1090 ["Modify Message" mh-show-modify t]
1091 ["Delete Message" mh-show-delete-msg t]
1092 ["Refile Message" mh-show-refile-msg t]
1093 ["Undo Delete/Refile" mh-show-undo t]
1094 ["Process Delete/Refile" mh-show-execute-commands t]
1096 ["Compose a New Message" mh-send t]
1097 ["Reply to Message..." mh-show-reply t]
1098 ["Forward Message..." mh-show-forward t]
1099 ["Redistribute Message..." mh-show-redistribute t]
1100 ["Edit Message Again" mh-show-edit-again t]
1101 ["Re-edit a Bounced Message" mh-show-extract-rejected-mail t]
1103 ["Copy Message to Folder..." mh-show-copy-msg t]
1104 ["Print Message" mh-show-print-msg t]
1105 ["Write Message to File..." mh-show-write-msg-to-file t]
1106 ["Pipe Message to Command..." mh-show-pipe-msg t]
1107 ["Unpack Uuencoded Message..." mh-show-store-msg t]
1108 ["Burst Digest Message" mh-show-burst-digest t]))
1111 mh-show-folder-menu mh-show-mode-map "Menu for MH-E folder."
1113 ["Incorporate New Mail" mh-show-inc-folder t]
1114 ["Toggle Show/Folder" mh-show-toggle-showing t]
1115 ["Execute Delete/Refile" mh-show-execute-commands t]
1116 ["Rescan Folder" mh-show-rescan-folder t]
1117 ["Thread Folder" mh-show-toggle-threads t]
1118 ["Pack Folder" mh-show-pack-folder t]
1119 ["Sort Folder" mh-show-sort-folder t]
1121 ["List Folders" mh-show-list-folders t]
1122 ["Visit a Folder..." mh-show-visit-folder t]
1123 ["View New Messages" mh-show-index-new-messages t]
1124 ["Search a Folder..." mh-show-search-folder t]
1125 ["Indexed Search..." mh-index-search t]
1127 ["Quit MH-E" mh-quit t]))
1130 ;; Ensure new buffers won't get this mode if default-major-mode is nil.
1131 (put 'mh-show-mode 'mode-class 'special)
1135 (defvar font-lock-auto-fontify)
1136 (defvar font-lock-defaults)
1137 (defvar tool-bar-map))
1139 (define-derived-mode mh-show-mode text-mode "MH-Show"
1140 "Major mode for showing messages in MH-E.\\<mh-show-mode-map>
1142 The hook `mh-show-mode-hook' is called upon entry to this mode.
1144 See also `mh-folder-mode'.
1146 \\{mh-show-mode-map}"
1147 (set (make-local-variable 'mail-header-separator) mh-mail-header-separator)
1148 (setq paragraph-start (default-value 'paragraph-start))
1149 (mh-show-unquote-From)
1152 (setq buffer-invisibility-spec '((vanish . t) t))
1153 (set (make-local-variable 'line-move-ignore-invisible) t)
1154 (make-local-variable 'font-lock-defaults)
1155 ;;(set (make-local-variable 'font-lock-support-mode) nil)
1157 ((equal mh-highlight-citation-style 'font-lock)
1158 (setq font-lock-defaults '(mh-show-font-lock-keywords-with-cite t)))
1159 ((equal mh-highlight-citation-style 'gnus)
1160 (setq font-lock-defaults '((mh-show-font-lock-keywords)
1162 (font-lock-fontify-region-function
1163 . mh-show-font-lock-fontify-region)))
1164 (mh-gnus-article-highlight-citation))
1166 (setq font-lock-defaults '(mh-show-font-lock-keywords t))))
1167 (if (and mh-xemacs-flag
1168 font-lock-auto-fontify)
1169 (turn-on-font-lock))
1170 (set (make-local-variable 'tool-bar-map) mh-show-tool-bar-map)
1171 (mh-funcall-if-exists mh-tool-bar-init :show)
1172 (when mh-decode-mime-flag
1173 (mh-make-local-hook 'kill-buffer-hook)
1174 (add-hook 'kill-buffer-hook 'mh-mime-cleanup nil t))
1175 (easy-menu-add mh-show-sequence-menu)
1176 (easy-menu-add mh-show-message-menu)
1177 (easy-menu-add mh-show-folder-menu)
1178 (make-local-variable 'mh-show-folder-buffer)
1179 (buffer-disable-undo)
1180 (setq buffer-read-only t)
1181 (use-local-map mh-show-mode-map))
1183 (defun mh-show-addr ()
1184 "Use `goto-address'."
1185 (when mh-show-use-goto-addr-flag
1186 (if (not (featurep 'goto-addr))
1187 (load "goto-addr" t t))
1188 (if (fboundp 'goto-address)
1193 ;; X-Face and Face display
1194 (defvar mh-show-xface-function
1195 (cond ((and mh-xemacs-flag (locate-library "x-face") (not (featurep 'xface)))
1197 #'mh-face-display-function)
1198 ((>= emacs-major-version 21)
1199 #'mh-face-display-function)
1201 "Determine at run time what function should be called to display X-Face.")
1203 (defvar mh-uncompface-executable
1204 (and (fboundp 'executable-find) (executable-find "uncompface")))
1206 (defun mh-face-to-png (data)
1207 "Convert base64 encoded DATA to png image."
1210 (ignore-errors (base64-decode-region (point-min) (point-max)))
1213 (defun mh-uncompface (data)
1214 "Run DATA through `uncompface' to generate bitmap."
1217 (when (and mh-uncompface-executable
1218 (equal (call-process-region (point-min) (point-max)
1219 mh-uncompface-executable t '(t nil))
1224 (defun mh-icontopbm ()
1225 "Elisp substitute for `icontopbm'."
1226 (goto-char (point-min))
1227 (let ((end (point-max)))
1228 (while (re-search-forward "0x\\(..\\)\\(..\\)," nil t)
1230 (goto-char (point-max))
1231 (insert (string-to-number (match-string 1) 16))
1232 (insert (string-to-number (match-string 2) 16))))
1233 (delete-region (point-min) end)
1234 (goto-char (point-min))
1235 (insert "P4\n48 48\n")))
1237 (mh-do-in-xemacs (defvar default-enable-multibyte-characters))
1239 (defmacro mh-face-foreground-compat (face &optional frame inherit)
1240 "Return the foreground color name of FACE, or nil if unspecified.
1241 See documentation for `face-foreground' for a description of the
1242 arguments FACE, FRAME, and INHERIT.
1244 Calls `face-foreground' correctly in older environments. Versions
1245 of Emacs prior to version 22 lacked an INHERIT argument which
1246 when t tells `face-foreground' to consider an inherited value for
1247 the foreground if the face does not define one itself."
1248 (if (>= emacs-major-version 22)
1249 `(face-foreground ,face ,frame ,inherit)
1250 `(face-foreground ,face ,frame)))
1252 (defmacro mh-face-background-compat (face &optional frame inherit)
1253 "Return the background color name of face, or nil if unspecified.
1254 See documentation for `back-foreground' for a description of the
1255 arguments FACE, FRAME, and INHERIT.
1257 Calls `face-background' correctly in older environments. Versions
1258 of Emacs prior to version 22 lacked an INHERIT argument which
1259 when t tells `face-background' to consider an inherited value for
1260 the background if the face does not define one itself."
1261 (if (>= emacs-major-version 22)
1262 `(face-background ,face ,frame ,inherit)
1263 `(face-background ,face ,frame)))
1265 (defun mh-face-display-function ()
1266 "Display a Face, X-Face, or X-Image-URL header field.
1267 If more than one of these are present, then the first one found
1268 in this order is used."
1270 (goto-char (point-min))
1271 (re-search-forward "\n\n" (point-max) t)
1272 (narrow-to-region (point-min) (point))
1273 (let* ((case-fold-search t)
1274 (default-enable-multibyte-characters nil)
1275 (face (message-fetch-field "face" t))
1276 (x-face (message-fetch-field "x-face" t))
1277 (url (message-fetch-field "x-image-url" t))
1279 (cond (face (setq raw (mh-face-to-png face)
1281 (x-face (setq raw (mh-uncompface x-face)
1283 (url (setq type 'url))
1284 (t (multiple-value-setq (type raw) (mh-picon-get-image))))
1286 (goto-char (point-min))
1287 (when (re-search-forward "^from:" (point-max) t)
1291 (mh-x-image-url-display url)
1292 (mh-funcall-if-exists
1293 insert-image (create-image
1296 (mh-face-foreground-compat 'mh-show-xface nil t)
1298 (mh-face-background-compat 'mh-show-xface nil t))
1304 (mh-x-image-url-display url))
1306 (when (featurep 'png)
1307 (set-extent-begin-glyph
1308 (make-extent (point) (point))
1309 (make-glyph (vector 'png ':data (mh-face-to-png face))))))
1310 ;; Try internal xface support if available...
1311 ((and (eq type 'pbm) (featurep 'xface))
1313 (set-extent-begin-glyph
1314 (make-extent (point) (point))
1315 (make-glyph (vector 'xface ':data (concat "X-Face: " x-face))))
1317 ;; Otherwise try external support with x-face...
1318 ((and (eq type 'pbm)
1319 (fboundp 'x-face-xmas-wl-display-x-face)
1320 (fboundp 'executable-find) (executable-find "uncompface"))
1321 (mh-funcall-if-exists x-face-xmas-wl-display-x-face))
1323 ((and raw (member type '(xpm xbm gif)))
1324 (when (featurep type)
1325 (set-extent-begin-glyph
1326 (make-extent (point) (point))
1327 (make-glyph (vector type ':data raw))))))
1328 (when raw (insert " "))))))))
1330 (defun mh-show-xface ()
1332 (when (and window-system mh-show-use-xface-flag
1333 (or mh-decode-mime-flag mh-mhl-format-file
1334 mh-clean-message-header-flag))
1335 (funcall mh-show-xface-function)))
1341 ;; XXX: This should be customizable. As a side-effect of setting this
1342 ;; variable, arrange to reset mh-picon-existing-directory-list to 'unset.
1343 (defvar mh-picon-directory-list
1344 '("~/.picons" "~/.picons/users" "~/.picons/usenix" "~/.picons/news"
1345 "~/.picons/domains" "~/.picons/misc"
1346 "/usr/share/picons/" "/usr/share/picons/users" "/usr/share/picons/usenix"
1347 "/usr/share/picons/news" "/usr/share/picons/domains"
1348 "/usr/share/picons/misc")
1349 "List of directories where picons reside.
1350 The directories are searched for in the order they appear in the list.")
1352 (defvar mh-picon-existing-directory-list 'unset
1353 "List of directories to search in.")
1355 (defvar mh-picon-cache (make-hash-table :test #'equal))
1357 (defvar mh-picon-image-types
1358 (loop for type in '(xpm xbm gif)
1359 when (or (mh-do-in-gnu-emacs
1361 (mh-funcall-if-exists image-type-available-p type)))
1362 (mh-do-in-xemacs (featurep type)))
1365 (defun mh-picon-set-directory-list ()
1366 "Update `mh-picon-existing-directory-list' if needed."
1367 (when (eq mh-picon-existing-directory-list 'unset)
1368 (setq mh-picon-existing-directory-list
1369 (loop for x in mh-picon-directory-list
1370 when (file-directory-p x) collect x))))
1372 (defun* mh-picon-get-image ()
1373 "Find the best possible match and return contents."
1374 (mh-picon-set-directory-list)
1376 (let* ((from-field (ignore-errors (car (message-tokenize-header
1377 (mh-get-header-field "from:")))))
1378 (from (car (ignore-errors
1379 (mh-funcall-if-exists ietf-drums-parse-address
1382 (string-match "\\([^+]*\\)\\(+.*\\)?@\\(.*\\)" from)
1383 (downcase (match-string 3 from))))
1384 (user (and host (downcase (match-string 1 from))))
1385 (canonical-address (format "%s@%s" user host))
1386 (cached-value (gethash canonical-address mh-picon-cache))
1387 (host-list (and host (delete "" (split-string host "\\."))))
1389 (cond (cached-value (return-from mh-picon-get-image cached-value))
1390 ((not host-list) (return-from mh-picon-get-image nil)))
1394 (loop for dir in mh-picon-existing-directory-list
1395 do (loop for type in mh-picon-image-types
1397 for file1 = (format "%s/%s.%s"
1398 dir canonical-address type)
1399 when (file-exists-p file1)
1400 do (return-from 'loop file1)
1402 for file2 = (format "%s/%s.%s" dir user type)
1403 when (file-exists-p file2)
1404 do (return-from 'loop file2)
1406 for file3 = (format "%s/%s.%s" dir host type)
1407 when (file-exists-p file3)
1408 do (return-from 'loop file3)))
1410 ;; Search order for user@foo.net:
1411 ;; [path]net/foo/user
1412 ;; [path]net/foo/user/face
1414 ;; [path]net/user/face
1415 ;; [path]net/foo/unknown
1416 ;; [path]net/foo/unknown/face
1417 ;; [path]net/unknown
1418 ;; [path]net/unknown/face
1419 (loop for u in (list user "unknown")
1420 do (loop for dir in mh-picon-existing-directory-list
1421 do (loop for x on host-list by #'cdr
1422 for y = (mh-picon-generate-path x u dir)
1423 do (loop for type in mh-picon-image-types
1424 for z1 = (format "%s.%s" y type)
1425 when (file-exists-p z1)
1426 do (return-from 'loop z1)
1427 for z2 = (format "%s/face.%s"
1429 when (file-exists-p z2)
1430 do (return-from 'loop z2)))))))
1431 (setf (gethash canonical-address mh-picon-cache)
1432 (mh-picon-file-contents match)))))
1434 (defun mh-picon-file-contents (file)
1435 "Return details about FILE.
1436 A list of consisting of a symbol for the type of the file and the
1437 file contents as a string is returned. If FILE is nil, then both
1438 elements of the list are nil."
1441 (let ((type (and (string-match ".*\\.\\(...\\)$" file)
1442 (intern (match-string 1 file)))))
1443 (insert-file-contents-literally file)
1444 (values type (buffer-string))))
1447 (defun mh-picon-generate-path (host-list user directory)
1448 "Generate the image file path.
1449 HOST-LIST is the parsed host address of the email address, USER
1450 the username and DIRECTORY is the directory relative to which the
1453 for elem in host-list
1454 do (setq acc (format "%s/%s" elem acc))
1455 finally return (format "%s/%s%s" directory acc user)))
1459 ;; X-Image-URL display
1461 (defvar mh-x-image-cache-directory nil
1462 "Directory where X-Image-URL images are cached.")
1463 (defvar mh-x-image-scaling-function
1464 (cond ((executable-find "convert")
1465 'mh-x-image-scale-with-convert)
1466 ((and (executable-find "anytopnm") (executable-find "pnmscale")
1467 (executable-find "pnmtopng"))
1468 'mh-x-image-scale-with-pnm)
1470 "Function to use to scale image to proper size.")
1471 (defvar mh-wget-executable nil)
1472 (defvar mh-wget-choice
1473 (or (and (setq mh-wget-executable (executable-find "wget")) 'wget)
1474 (and (setq mh-wget-executable (executable-find "fetch")) 'fetch)
1475 (and (setq mh-wget-executable (executable-find "curl")) 'curl)))
1476 (defvar mh-wget-option
1477 (cdr (assoc mh-wget-choice '((curl . "-o") (fetch . "-o") (wget . "-O")))))
1478 (defvar mh-x-image-temp-file nil)
1479 (defvar mh-x-image-url nil)
1480 (defvar mh-x-image-marker nil)
1481 (defvar mh-x-image-url-cache-file nil)
1483 ;; Functions to scale image to proper size
1484 (defun mh-x-image-scale-with-pnm (input output)
1485 "Scale image in INPUT file and write to OUTPUT file using pnm tools."
1486 (let ((res (shell-command-to-string
1487 (format "anytopnm < %s | pnmscale -xysize 96 48 | pnmtopng > %s"
1489 (unless (equal res "")
1490 (delete-file output))))
1492 (defun mh-x-image-scale-with-convert (input output)
1493 "Scale image in INPUT file and write to OUTPUT file using ImageMagick."
1494 (call-process "convert" nil nil nil "-geometry" "96x48" input output))
1496 (defun mh-x-image-url-cache-canonicalize (url)
1498 Replace the ?/ character with a ?! character and append .png."
1499 (format "%s/%s.png" mh-x-image-cache-directory
1502 (mh-replace-string "/" "!")
1505 (defun mh-x-image-set-download-state (file data)
1506 "Setup a symbolic link from FILE to DATA."
1508 (make-symbolic-link (symbol-name data) file t)
1509 (delete-file file)))
1511 (defun mh-x-image-get-download-state (file)
1512 "Check the state of FILE by following any symbolic links."
1513 (unless (file-exists-p mh-x-image-cache-directory)
1514 (call-process "mkdir" nil nil nil mh-x-image-cache-directory))
1515 (cond ((file-symlink-p file)
1516 (intern (file-name-nondirectory (file-chase-links file))))
1517 ((not (file-exists-p file)) nil)
1520 (defun mh-x-image-url-fetch-image (url cache-file marker sentinel)
1521 "Fetch and display the image specified by URL.
1522 After the image is fetched, it is stored in CACHE-FILE. It will
1523 be displayed in a buffer and position specified by MARKER. The
1524 actual display is carried out by the SENTINEL function."
1525 (if mh-wget-executable
1526 (let ((buffer (get-buffer-create (generate-new-buffer-name
1527 mh-temp-fetch-buffer)))
1528 (filename (or (mh-funcall-if-exists make-temp-file "mhe-fetch")
1529 (expand-file-name (make-temp-name "~/mhe-fetch")))))
1532 (set (make-local-variable 'mh-x-image-url-cache-file) cache-file)
1533 (set (make-local-variable 'mh-x-image-marker) marker)
1534 (set (make-local-variable 'mh-x-image-temp-file) filename))
1535 (set-process-sentinel
1536 (start-process "*mh-x-image-url-fetch*" buffer
1537 mh-wget-executable mh-wget-option filename url)
1539 ;; Temporary failure
1540 (mh-x-image-set-download-state cache-file 'try-again)))
1542 (defun mh-x-image-display (image marker)
1543 "Display IMAGE at MARKER."
1545 (set-buffer (marker-buffer marker))
1546 (let ((buffer-read-only nil)
1547 (default-enable-multibyte-characters nil)
1548 (buffer-modified-flag (buffer-modified-p)))
1550 (when (and (file-readable-p image) (not (file-symlink-p image))
1551 (eq marker mh-x-image-marker))
1554 (mh-funcall-if-exists insert-image (create-image image 'png)))
1556 (when (featurep 'png)
1557 (set-extent-begin-glyph
1558 (make-extent (point) (point))
1560 (vector 'png ':data (with-temp-buffer
1561 (insert-file-contents-literally image)
1562 (buffer-string))))))))
1563 (set-buffer-modified-p buffer-modified-flag)))))
1565 (defun mh-x-image-scale-and-display (process change)
1566 "When the wget PROCESS terminates scale and display image.
1567 The argument CHANGE is ignored."
1568 (when (eq (process-status process) 'exit)
1569 (let (marker temp-file cache-filename wget-buffer)
1571 (set-buffer (setq wget-buffer (process-buffer process)))
1572 (setq marker mh-x-image-marker
1573 cache-filename mh-x-image-url-cache-file
1574 temp-file mh-x-image-temp-file))
1576 ;; Check if we have `convert'
1577 ((eq mh-x-image-scaling-function 'ignore)
1578 (message "The \"convert\" program is needed to display X-Image-URL")
1579 (mh-x-image-set-download-state cache-filename 'try-again))
1580 ;; Scale fetched image
1581 ((and (funcall mh-x-image-scaling-function temp-file cache-filename)
1583 ;; Attempt to display image if we have it
1584 ((file-exists-p cache-filename)
1585 (mh-x-image-display cache-filename marker))
1586 ;; We didn't find the image. Should we try to display it the next time?
1587 (t (mh-x-image-set-download-state cache-filename 'try-again)))
1589 (set-marker marker nil)
1590 (delete-process process)
1591 (kill-buffer wget-buffer)
1592 (delete-file temp-file)))))
1594 (defun mh-x-image-url-sane-p (url)
1595 "Check if URL is something sensible."
1596 (let ((len (length url)))
1597 (cond ((< len 5) nil)
1598 ((not (equal (substring url 0 5) "http:")) nil)
1602 (defun mh-x-image-url-display (url)
1603 "Display image from location URL.
1604 If the URL isn't present in the cache then it is fetched with wget."
1605 (let* ((cache-filename (mh-x-image-url-cache-canonicalize url))
1606 (state (mh-x-image-get-download-state cache-filename))
1607 (marker (set-marker (make-marker) (point))))
1608 (set (make-local-variable 'mh-x-image-marker) marker)
1609 (cond ((not (mh-x-image-url-sane-p url)))
1611 (mh-x-image-display cache-filename marker))
1612 ((or (not mh-wget-executable)
1613 (eq mh-x-image-scaling-function 'ignore)))
1615 ((not mh-fetch-x-image-url)
1616 (set-marker marker nil))
1617 ((eq state 'try-again)
1618 (mh-x-image-set-download-state cache-filename nil)
1619 (mh-x-image-url-fetch-image url cache-filename marker
1620 'mh-x-image-scale-and-display))
1621 ((and (eq mh-fetch-x-image-url 'ask)
1622 (not (y-or-n-p (format "Fetch %s? " url))))
1623 (mh-x-image-set-download-state cache-filename 'never))
1625 (mh-x-image-url-fetch-image url cache-filename marker
1626 'mh-x-image-scale-and-display)))))
1630 (defun mh-maybe-show (&optional msg)
1631 "Display message at cursor, but only if in show mode.
1632 If optional arg MSG is non-nil, display that message instead."
1633 (if mh-showing-mode (mh-show msg)))
1635 (defun mh-show (&optional message redisplay-flag)
1636 "Display message\\<mh-folder-mode-map>.
1638 If the message under the cursor is already displayed, this command
1639 scrolls to the beginning of the message. MH-E normally hides a lot of
1640 the superfluous header fields that mailers add to a message, but if
1641 you wish to see all of them, use the command \\[mh-header-display].
1643 Two hooks can be used to control how messages are displayed. The
1644 first hook, `mh-show-mode-hook', is called early on in the
1645 process of the message display. It is usually used to perform
1646 some action on the message's content. The second hook,
1647 `mh-show-hook', is the last thing called after messages are
1648 displayed. It's used to affect the behavior of MH-E in general or
1649 when `mh-show-mode-hook' is too early.
1651 From a program, optional argument MESSAGE can be used to display an
1652 alternative message. The optional argument REDISPLAY-FLAG forces the
1653 redisplay of the message even if the show buffer was already
1654 displaying the correct message.
1656 See the \"mh-show\" customization group for a litany of options that
1657 control what displayed messages look like."
1658 (interactive (list nil t))
1659 (when (or redisplay-flag
1660 (and mh-showing-with-headers
1661 (or mh-mhl-format-file mh-clean-message-header-flag)))
1662 (mh-invalidate-show-buffer))
1663 (mh-show-msg message))
1665 (defun mh-show-mouse (event)
1666 "Move point to mouse EVENT and show message."
1668 (mouse-set-point event)
1671 (defun mh-summary-height ()
1672 "Return ideal value for the variable `mh-summary-height'.
1673 The current frame height is taken into consideration."
1674 (or (and (fboundp 'frame-height)
1675 (> (frame-height) 24)
1676 (min 10 (/ (frame-height) 6)))
1679 (defun mh-show-msg (msg)
1682 The hook `mh-show-hook' is called after the message has been
1685 (setq msg (mh-get-msg-num t)))
1687 (setq mh-page-to-next-msg-flag nil)
1688 (let ((folder mh-current-folder)
1689 (folders (list mh-current-folder))
1690 (clean-message-header mh-clean-message-header-flag)
1691 (show-window (get-buffer-window mh-show-buffer))
1692 (display-mime-buttons-flag mh-display-buttons-for-inline-parts-flag))
1693 (if (not (eq (next-window (minibuffer-window)) (selected-window)))
1694 (delete-other-windows)) ; force ourself to the top window
1695 (mh-in-show-buffer (mh-show-buffer)
1696 (setq mh-display-buttons-for-inline-parts-flag display-mime-buttons-flag)
1697 (if (and show-window
1698 (equal (mh-msg-filename msg folder) buffer-file-name))
1699 (progn ;just back up to start
1700 (goto-char (point-min))
1701 (if (not clean-message-header)
1702 (mh-start-of-uncleaned-message)))
1703 (mh-display-msg msg folder)))
1704 (if (not (= (1+ (window-height)) (frame-height))) ;not horizontally split
1705 (shrink-window (- (window-height) (or mh-summary-height
1706 (mh-summary-height)))))
1708 ;; The following line is a nop which forces update of the scan line so
1709 ;; that font-lock will update it (if needed)...
1710 (mh-notate nil nil mh-cmd-note)
1711 (if (not (memq msg mh-seen-list))
1712 (setq mh-seen-list (cons msg mh-seen-list)))
1713 (when mh-update-sequences-after-mh-show-flag
1714 (mh-update-sequences)
1717 (append (mh-index-delete-from-sequence mh-unseen-seq (list msg))
1719 (when (mh-speed-flists-active-p)
1720 (apply #'mh-speed-flists t folders)))
1721 (run-hooks 'mh-show-hook)))
1723 (defun mh-modify (&optional message)
1726 There are times when you need to edit a message. For example, you
1727 may need to fix a broken Content-Type header field. You can do
1728 this with this command. It displays the raw message in an
1729 editable buffer. When you are done editing, save and kill the
1730 buffer as you would any other.
1732 From a program, edit MESSAGE; nil means edit current message."
1734 (let* ((message (or message (mh-get-msg-num t)))
1735 (msg-filename (mh-msg-filename message))
1737 (when (not (file-exists-p msg-filename))
1738 (error "Message %d does not exist" message))
1740 ;; Invalidate the show buffer if it is showing the same message that is
1742 (when (and (buffer-live-p (get-buffer mh-show-buffer))
1743 (equal (save-excursion (set-buffer mh-show-buffer)
1746 (mh-invalidate-show-buffer))
1749 (find-file msg-filename)
1750 (setq edit-buffer (current-buffer))
1752 ;; Set buffer properties
1754 (use-local-map text-mode-map)
1756 ;; Just show the edit buffer...
1757 (delete-other-windows)
1758 (switch-to-buffer edit-buffer)))
1760 (defun mh-show-unquote-From ()
1761 "Decode >From at beginning of lines for `mh-show-mode'."
1763 (let ((modified (buffer-modified-p))
1764 (case-fold-search nil)
1765 (buffer-read-only nil))
1766 (goto-char (mh-mail-header-end))
1767 (while (re-search-forward "^>From" nil t)
1768 (replace-match "From"))
1769 (set-buffer-modified-p modified))))
1771 (defun mh-msg-folder (folder-name)
1772 "Return the name of the buffer for FOLDER-NAME."
1775 (defun mh-display-msg (msg-num folder-name)
1776 "Display MSG-NUM of FOLDER-NAME.
1777 Sets the current buffer to the show buffer."
1778 (let ((folder (mh-msg-folder folder-name)))
1780 ;; When Gnus uses external displayers it has to keep handles longer. So
1781 ;; we will delete these handles when mh-quit is called on the folder. It
1782 ;; would be nicer if there are weak pointers in emacs lisp, then we could
1783 ;; get the garbage collector to do this for us.
1784 (unless (mh-buffer-data)
1785 (setf (mh-buffer-data) (mh-make-buffer-data)))
1786 ;; Bind variables in folder buffer in case they are local
1787 (let ((formfile mh-mhl-format-file)
1788 (clean-message-header mh-clean-message-header-flag)
1789 (invisible-headers mh-invisible-header-fields-compiled)
1790 (visible-headers nil)
1791 (msg-filename (mh-msg-filename msg-num folder-name))
1792 (show-buffer mh-show-buffer)
1793 (mm-inline-media-tests mh-mm-inline-media-tests))
1794 (if (not (file-exists-p msg-filename))
1795 (error "Message %d does not exist" msg-num))
1796 (if (and (> mh-show-maximum-size 0)
1797 (> (elt (file-attributes msg-filename) 7)
1798 mh-show-maximum-size)
1801 "Message %d (%d bytes) exceeds %d bytes. Display it? "
1802 msg-num (elt (file-attributes msg-filename) 7)
1803 mh-show-maximum-size))))
1804 (error "Message %d not displayed" msg-num))
1805 (set-buffer show-buffer)
1806 (cond ((not (equal msg-filename buffer-file-name))
1808 (setq buffer-read-only nil)
1809 ;; Cleanup old mime handles
1812 ;; Changing contents, so this hook needs to be reinitialized.
1813 ;; pgp.el uses this.
1814 (if (boundp 'write-contents-hooks) ;Emacs 19
1815 (kill-local-variable 'write-contents-hooks))
1817 (mh-exec-lib-cmd-output "mhl" "-nobell" "-noclear"
1818 (if (stringp formfile)
1819 (list "-form" formfile))
1821 (insert-file-contents-literally msg-filename))
1822 ;; Use mm to display buffer
1823 (when (and mh-decode-mime-flag (not formfile))
1824 (mh-add-missing-mime-version-header)
1825 (setf (mh-buffer-data) (mh-make-buffer-data))
1829 (goto-char (point-min))
1830 (cond (clean-message-header
1831 (mh-clean-msg-header (point-min)
1834 (goto-char (point-min)))
1836 (mh-start-of-uncleaned-message)))
1837 (mh-decode-message-header)
1838 ;; the parts of visiting we want to do (no locking)
1839 (or (eq buffer-undo-list t) ;don't save undo info for prev msgs
1840 (setq buffer-undo-list nil))
1841 (set-buffer-auto-saved)
1842 ;; the parts of set-visited-file-name we want to do (no locking)
1843 (setq buffer-file-name msg-filename)
1844 (setq buffer-backed-up nil)
1848 (when (and mh-decode-mime-flag (not formfile))
1849 (setq buffer-read-only nil)
1850 (mh-display-smileys)
1851 (mh-display-emphasis))
1852 (setq buffer-read-only t))
1853 (set-buffer-modified-p nil)
1854 (setq mh-show-folder-buffer folder)
1855 (setq mode-line-buffer-identification
1856 (list (format mh-show-buffer-mode-line-buffer-id
1857 folder-name msg-num)))
1860 (setq mh-showing-with-headers nil))))))
1862 (defun mh-clean-msg-header (start invisible-headers visible-headers)
1863 "Flush extraneous lines in message header.
1865 Header is cleaned from START to the end of the message header.
1866 INVISIBLE-HEADERS contains a regular expression specifying lines
1867 to delete from the header. VISIBLE-HEADERS contains a regular
1868 expression specifying the lines to display. INVISIBLE-HEADERS is
1869 ignored if VISIBLE-HEADERS is non-nil."
1870 ;; XXX Note that MH-E no longer supports the `mh-visible-headers'
1871 ;; variable, so this function could be trimmed of this feature too."
1872 (let ((case-fold-search t)
1873 (buffer-read-only nil))
1876 (if (search-forward "\n\n" nil 'move)
1878 (narrow-to-region start (point))
1879 (goto-char (point-min))
1881 (while (< (point) (point-max))
1882 (cond ((looking-at visible-headers)
1884 (while (looking-at "[ \t]") (forward-line 1)))
1887 (while (looking-at "[ \t]")
1888 (mh-delete-line 1)))))
1889 (while (re-search-forward invisible-headers nil t)
1892 (while (looking-at "[ \t]")
1893 (mh-delete-line 1)))))
1894 (let ((mh-compose-skipped-header-fields ()))
1895 (mh-letter-hide-all-skipped-fields))
1898 (defun mh-delete-line (lines)
1899 "Delete the next LINES lines."
1900 (delete-region (point) (progn (forward-line lines) (point))))
1902 (defun mh-notate (msg notation offset)
1903 "Mark MSG with the character NOTATION at position OFFSET.
1904 Null MSG means the message at cursor.
1905 If NOTATION is nil then no change in the buffer occurs."
1908 (mh-goto-msg msg t t))
1909 (with-mh-folder-updating (t)
1911 (forward-char offset)
1912 (let* ((change-stack-flag
1914 (+ mh-cmd-note mh-scan-field-destination-offset))
1915 (not (eq notation mh-note-seq))))
1916 (msg (and change-stack-flag (or msg (mh-get-msg-num nil))))
1917 (stack (and msg (gethash msg mh-sequence-notation-history)))
1918 (notation (or notation (char-after))))
1920 ;; The presence of the stack tells us that we don't need to
1921 ;; notate the message, since the notation would be replaced
1922 ;; by a sequence notation. So we will just put the notation
1923 ;; at the bottom of the stack. If the sequence is deleted,
1924 ;; the correct notation will be shown.
1925 (setf (gethash msg mh-sequence-notation-history)
1926 (reverse (cons notation (cdr (reverse stack)))))
1927 ;; Since we don't have any sequence notations in the way, just
1928 ;; notate the scan line.
1931 (when change-stack-flag
1932 (mh-thread-update-scan-line-map msg notation offset)))))))
1934 (defun mh-goto-msg (number &optional no-error-if-no-message dont-show)
1935 "Go to a message\\<mh-folder-mode-map>.
1937 You can enter the message NUMBER either before or after typing
1938 \\[mh-goto-msg]. In the latter case, Emacs prompts you.
1940 In a program, optional non-nil second argument NO-ERROR-IF-NO-MESSAGE
1941 means return nil instead of signaling an error if message does not
1942 exist\; in this case, the cursor is positioned near where the message
1943 would have been. Non-nil third argument DONT-SHOW means not to show
1945 (interactive "NGo to message: ")
1946 (setq number (prefix-numeric-value number))
1947 (let ((point (point))
1949 (goto-char (point-min))
1950 (unless (re-search-forward (format mh-scan-msg-search-regexp number) nil t)
1952 (unless no-error-if-no-message
1953 (error "No message %d" number))
1954 (setq return-value nil))
1956 (or dont-show (not return-value) (mh-maybe-show number))
1959 (defun mh-get-profile-field (field)
1960 "Find and return the value of FIELD in the current buffer.
1961 Returns nil if the field is not in the buffer."
1962 (let ((case-fold-search t))
1963 (goto-char (point-min))
1964 (cond ((not (re-search-forward (format "^%s" field) nil t)) nil)
1965 ((looking-at "[\t ]*$") nil)
1967 (re-search-forward "[\t ]*\\([^\t \n].*\\)$" nil t)
1968 (let ((start (match-beginning 1)))
1970 (buffer-substring start (point)))))))
1972 (defvar mh-find-path-run nil
1973 "Non-nil if `mh-find-path' has been run already.")
1975 (defun mh-find-path ()
1976 "Set variables from user's MH profile.
1978 This function sets `mh-user-path' from your \"Path:\" MH profile
1979 component (but defaults to \"Mail\" if one isn't present),
1980 `mh-draft-folder' from \"Draft-Folder:\", `mh-unseen-seq' from
1981 \"Unseen-Sequence:\", `mh-previous-seq' from
1982 \"Previous-Sequence:\", and `mh-inbox' from \"Inbox:\" (defaults
1985 The hook `mh-find-path-hook' is run after these variables have
1986 been set. This hook can be used the change the value of these
1987 variables if you need to run with different values between MH and
1990 (unless mh-find-path-run
1991 (setq mh-find-path-run t)
1993 ;; Be sure profile is fully expanded before switching buffers
1994 (let ((profile (expand-file-name (or (getenv "MH") "~/.mh_profile"))))
1995 (set-buffer (get-buffer-create mh-temp-buffer))
1996 (setq buffer-offer-save nil) ;for people who set default to t
1999 (insert-file-contents profile)
2001 (mh-install profile err)))
2002 (setq mh-user-path (mh-get-profile-field "Path:"))
2003 (if (not mh-user-path)
2004 (setq mh-user-path "Mail"))
2006 (file-name-as-directory
2007 (expand-file-name mh-user-path (expand-file-name "~"))))
2008 (unless mh-x-image-cache-directory
2009 (setq mh-x-image-cache-directory
2010 (expand-file-name ".mhe-x-image-cache" mh-user-path)))
2011 (setq mh-draft-folder (mh-get-profile-field "Draft-Folder:"))
2014 (if (not (mh-folder-name-p mh-draft-folder))
2015 (setq mh-draft-folder (format "+%s" mh-draft-folder)))
2016 (if (not (file-exists-p (mh-expand-file-name mh-draft-folder)))
2018 "Draft folder \"%s\" not found. Create it and try again"
2019 (mh-expand-file-name mh-draft-folder)))))
2020 (setq mh-inbox (mh-get-profile-field "Inbox:"))
2021 (cond ((not mh-inbox)
2022 (setq mh-inbox "+inbox"))
2023 ((not (mh-folder-name-p mh-inbox))
2024 (setq mh-inbox (format "+%s" mh-inbox))))
2025 (setq mh-unseen-seq (mh-get-profile-field "Unseen-Sequence:"))
2027 (setq mh-unseen-seq (intern mh-unseen-seq))
2028 (setq mh-unseen-seq 'unseen)) ;old MH default?
2029 (setq mh-previous-seq (mh-get-profile-field "Previous-Sequence:"))
2031 (setq mh-previous-seq (intern mh-previous-seq)))
2032 (run-hooks 'mh-find-path-hook)
2033 (mh-collect-folder-names)))))
2035 (defun mh-file-command-p (file)
2036 "Return t if file FILE is the name of a executable regular file."
2037 (and (file-regular-p file) (file-executable-p file)))
2039 (defvar mh-no-install nil) ;do not run install-mh
2041 (defun mh-install (profile error-val)
2042 "Initialize the MH environment.
2043 This is called if we fail to read the PROFILE file. ERROR-VAL is
2044 the error that made this call necessary."
2045 (if (or (getenv "MH")
2046 (file-exists-p profile)
2048 (signal (car error-val)
2049 (list (format "Cannot read MH profile \"%s\"" profile)
2050 (car (cdr (cdr error-val))))))
2051 ;; The "install-mh" command will output a short note which
2052 ;; mh-exec-cmd will display to the user.
2053 ;; The MH 5 version of install-mh might try prompt the user
2054 ;; for information, which would fail here.
2055 (mh-exec-cmd (expand-file-name "install-mh" mh-lib-progs) "-auto")
2056 ;; now try again to read the profile file
2059 (insert-file-contents profile)
2061 (signal (car err) ;re-signal with more specific msg
2062 (list (format "Cannot read MH profile \"%s\"" profile)
2063 (car (cdr (cdr err))))))))
2065 (defun mh-set-folder-modified-p (flag)
2066 "Mark current folder as modified or unmodified according to FLAG."
2067 (set-buffer-modified-p flag))
2069 (defun mh-find-seq (name)
2070 "Return sequence NAME."
2071 (assoc name mh-seq-list))
2073 (defun mh-seq-to-msgs (seq)
2074 "Return a list of the messages in SEQ."
2075 (mh-seq-msgs (mh-find-seq seq)))
2077 (defun mh-update-scan-format (fmt width)
2078 "Return a scan format with the (msg) width in the FMT replaced with WIDTH.
2080 The message number width portion of the format is discovered
2081 using `mh-scan-msg-format-regexp'. Its replacement is controlled
2082 with `mh-scan-msg-format-string'."
2084 (string-match mh-scan-msg-format-regexp fmt)
2085 (let ((begin (match-beginning 1))
2086 (end (match-end 1)))
2087 (concat (substring fmt 0 begin)
2088 (format mh-scan-msg-format-string width)
2089 (substring fmt end))))
2092 (defun mh-msg-num-width (folder)
2093 "Return the width of the largest message number in this FOLDER."
2094 (or mh-progs (mh-find-path))
2095 (let ((tmp-buffer (get-buffer-create mh-temp-buffer))
2098 (set-buffer tmp-buffer)
2100 (apply 'call-process
2101 (expand-file-name mh-scan-prog mh-progs) nil '(t nil) nil
2102 (list folder "last" "-format" "%(msg)"))
2103 (goto-char (point-min))
2104 (if (re-search-forward mh-scan-msg-number-regexp nil 0 1)
2105 (setq width (length (buffer-substring
2106 (match-beginning 1) (match-end 1))))))
2109 (defun mh-add-msgs-to-seq (msgs seq &optional internal-flag dont-annotate-flag)
2112 Remove duplicates and keep sequence sorted. If optional
2113 INTERNAL-FLAG is non-nil, do not mark the message in the scan
2114 listing or inform MH of the addition.
2116 If DONT-ANNOTATE-FLAG is non-nil then the annotations in the
2117 folder buffer are not updated."
2118 (let ((entry (mh-find-seq seq))
2119 (internal-seq-flag (mh-internal-seq seq)))
2120 (if (and msgs (atom msgs)) (setq msgs (list msgs)))
2123 (cons (mh-make-seq seq (mh-canonicalize-sequence msgs))
2125 (if msgs (setcdr entry (mh-canonicalize-sequence
2126 (append msgs (mh-seq-msgs entry))))))
2127 (unless internal-flag
2128 (mh-add-to-sequence seq msgs)
2129 (when (not dont-annotate-flag)
2130 (mh-iterate-on-range msg msgs
2131 (unless (memq msg (cdr entry))
2132 (mh-add-sequence-notation msg internal-seq-flag)))))))
2134 (defun mh-canonicalize-sequence (msgs)
2135 "Sort MSGS in decreasing order and remove duplicates."
2136 (let* ((sorted-msgs (sort (copy-sequence msgs) '>))
2139 (if (= (car head) (cadr head))
2140 (setcdr head (cddr head))
2141 (setq head (cdr head))))
2144 (defvar mh-sub-folders-cache (make-hash-table :test #'equal))
2145 (defvar mh-current-folder-name nil)
2146 (defvar mh-flists-partial-line "")
2147 (defvar mh-flists-process nil)
2149 ;; Initialize mh-sub-folders-cache...
2150 (defun mh-collect-folder-names ()
2151 "Collect folder names by running \"flists\"."
2152 (unless mh-flists-process
2153 (setq mh-flists-process
2154 (mh-exec-cmd-daemon "folders" 'mh-collect-folder-names-filter
2155 "-recurse" "-fast"))))
2157 (defun mh-collect-folder-names-filter (process output)
2159 PROCESS is the flists process that was run to collect folder
2160 names and the function is called when OUTPUT is available."
2162 (prevailing-match-data (match-data))
2165 (while (setq line-end (string-match "\n" output position))
2166 (setq folder (format "+%s%s"
2167 mh-flists-partial-line
2168 (substring output position line-end)))
2169 (setq mh-flists-partial-line "")
2170 (unless (equal (aref folder 1) ?.)
2171 (mh-populate-sub-folders-cache folder))
2172 (setq position (1+ line-end)))
2173 (set-match-data prevailing-match-data))
2174 (setq mh-flists-partial-line (substring output position))))
2176 (defun mh-populate-sub-folders-cache (folder)
2177 "Tell `mh-sub-folders-cache' about FOLDER."
2178 (let* ((last-slash (mh-search-from-end ?/ folder))
2179 (child1 (substring folder (1+ (or last-slash 0))))
2180 (parent (and last-slash (substring folder 0 last-slash)))
2181 (parent-slash (and parent (mh-search-from-end ?/ parent)))
2182 (child2 (and parent (substring parent (1+ (or parent-slash 0)))))
2183 (grand-parent (and parent-slash (substring parent 0 parent-slash)))
2184 (cache-entry (gethash parent mh-sub-folders-cache)))
2185 (unless (loop for x in cache-entry when (equal (car x) child1) return t
2187 (push (list child1) cache-entry)
2188 (setf (gethash parent mh-sub-folders-cache)
2189 (sort cache-entry (lambda (x y) (string< (car x) (car y)))))
2191 (loop for x in (gethash grand-parent mh-sub-folders-cache)
2192 when (equal (car x) child2)
2193 do (progn (setf (cdr x) t) (return)))))))
2195 (defun mh-normalize-folder-name (folder &optional empty-string-okay
2196 dont-remove-trailing-slash)
2197 "Normalizes FOLDER name.
2199 Makes sure that two '/' characters never occur next to each
2200 other. Also all occurrences of \"..\" and \".\" are suitably
2201 processed. So \"+inbox/../news\" will be normalized to \"+news\".
2203 If optional argument EMPTY-STRING-OKAY is nil then a '+' is added
2204 at the front if FOLDER lacks one. If non-nil and FOLDER is the
2205 empty string then nothing is added.
2207 If optional argument DONT-REMOVE-TRAILING-SLASH is non-nil then a
2208 trailing '/' if present is retained (if present), otherwise it is
2210 (when (stringp folder)
2211 ;; Replace two or more consecutive '/' characters with a single '/'
2212 (while (string-match "//" folder)
2213 (setq folder (replace-match "/" nil t folder)))
2214 (let* ((length (length folder))
2215 (trailing-slash-present (and (> length 0)
2216 (equal (aref folder (1- length)) ?/)))
2217 (leading-slash-present (and (> length 0)
2218 (equal (aref folder 0) ?/))))
2219 (when (and (> length 0) (equal (aref folder 0) ?@)
2220 (stringp mh-current-folder-name))
2221 (setq folder (format "%s/%s/" mh-current-folder-name
2222 (substring folder 1))))
2223 ;; XXX: Purge empty strings from the list that split-string returns. In
2224 ;; XEmacs, (split-string "+foo/" "/") returns ("+foo" "") while in GNU
2225 ;; Emacs it returns ("+foo"). In the code it is assumed that the
2226 ;; components list has no empty strings.
2227 (let ((components (delete "" (split-string folder "/")))
2229 ;; Remove .. and . from the pathname.
2230 (dolist (component components)
2231 (cond ((and (equal component "..") result)
2233 ((equal component ".."))
2234 ((equal component "."))
2235 (t (push component result))))
2237 (dolist (component result)
2238 (setq folder (concat component "/" folder)))
2239 ;; Remove trailing '/' if needed.
2240 (unless (and trailing-slash-present dont-remove-trailing-slash)
2241 (when (not (equal folder ""))
2242 (setq folder (substring folder 0 (1- (length folder))))))
2243 (when leading-slash-present
2244 (setq folder (concat "/" folder)))))
2245 (cond ((and empty-string-okay (equal folder "")))
2246 ((equal folder "") (setq folder "+"))
2247 ((not (equal (aref folder 0) ?+)) (setq folder (concat "+" folder)))))
2250 (defun mh-sub-folders (folder &optional add-trailing-slash-flag)
2251 "Find the subfolders of FOLDER.
2252 The function avoids running folders unnecessarily by caching the
2253 results of the actual folders call.
2255 If optional argument ADD-TRAILING-SLASH-FLAG is non-nil then a
2256 slash is added to each of the sub-folder names that may have
2257 nested folders within them."
2258 (let* ((folder (mh-normalize-folder-name folder))
2259 (match (gethash folder mh-sub-folders-cache 'no-result))
2260 (sub-folders (cond ((eq match 'no-result)
2261 (setf (gethash folder mh-sub-folders-cache)
2262 (mh-sub-folders-actual folder)))
2264 (if add-trailing-slash-flag
2265 (mapcar #'(lambda (x)
2266 (if (cdr x) (cons (concat (car x) "/") (cdr x)) x))
2270 (defun mh-sub-folders-actual (folder)
2271 "Execute the command folders to return the sub-folders of FOLDER.
2272 Filters out the folder names that start with \".\" so that
2273 directories that aren't usually mail folders are hidden."
2274 (let ((arg-list `(,(expand-file-name "folders" mh-progs)
2275 nil (t nil) nil "-noheader" "-norecurse" "-nototal"
2276 ,@(if (stringp folder) (list folder) ())))
2278 (current-folder (concat
2280 (call-process (expand-file-name "folder" mh-progs)
2281 nil '(t nil) nil "-fast")
2282 (buffer-substring (point-min) (1- (point-max))))
2285 (apply #'call-process arg-list)
2286 (goto-char (point-min))
2287 (while (not (and (eolp) (bolp)))
2288 (goto-char (line-end-position))
2289 (let ((start-pos (line-beginning-position))
2290 (has-pos (search-backward " has " (line-beginning-position) t)))
2291 (when (integerp has-pos)
2292 (while (equal (char-after has-pos) ? )
2295 (while (equal (char-after start-pos) ? )
2297 (let* ((name (buffer-substring start-pos has-pos))
2298 (first-char (aref name 0))
2299 (last-char (aref name (1- (length name)))))
2300 (unless (member first-char '(?. ?# ?,))
2301 (when (and (equal last-char ?+) (equal name current-folder))
2302 (setq name (substring name 0 (1- (length name)))))
2305 (search-forward "(others)" (line-end-position) t))
2308 (setq results (nreverse results))
2309 (when (stringp folder)
2310 (setq results (cdr results))
2311 (let ((folder-name-len (length (format "%s/" (substring folder 1)))))
2312 (setq results (mapcar (lambda (f)
2313 (cons (substring (car f) folder-name-len)
2318 (defun mh-remove-from-sub-folders-cache (folder)
2319 "Remove FOLDER and its parent from `mh-sub-folders-cache'.
2320 FOLDER should be unconditionally removed from the cache. Also the
2321 last ancestor of FOLDER present in the cache must be removed as
2324 To see why this is needed assume we have a folder +foo which has
2325 a single sub-folder qux. Now we create the folder +foo/bar/baz.
2326 Here we will need to invalidate the cached sub-folders of +foo,
2327 otherwise completion on +foo won't tell us about the option
2329 (remhash folder mh-sub-folders-cache)
2330 (block ancestor-found
2331 (let ((parent folder)
2332 (one-ancestor-found nil)
2334 (while (setq last-slash (mh-search-from-end ?/ parent))
2335 (setq parent (substring parent 0 last-slash))
2336 (unless (eq (gethash parent mh-sub-folders-cache 'none) 'none)
2337 (remhash parent mh-sub-folders-cache)
2338 (if one-ancestor-found
2339 (return-from ancestor-found)
2340 (setq one-ancestor-found t))))
2341 (remhash nil mh-sub-folders-cache))))
2343 (defvar mh-folder-hist nil)
2347 (defvar mh-speed-folder-map)
2348 (defvar mh-speed-flists-cache))
2350 (defvar mh-allow-root-folder-flag nil
2351 "Non-nil means \"+\" is an acceptable folder name.
2352 This variable is used to communicate with
2353 `mh-folder-completion-function'. That function can have exactly
2354 three arguments so we bind this variable to t or nil.
2356 This variable should never be set.")
2358 (defvar mh-folder-completion-map (copy-keymap minibuffer-local-completion-map))
2359 (define-key mh-folder-completion-map " " 'minibuffer-complete) ;Why???
2361 (defvar mh-speed-flists-inhibit-flag nil)
2363 (defun mh-speed-flists-active-p ()
2364 "Check if speedbar is running with message counts enabled."
2365 (and (featurep 'mh-speed)
2366 (not mh-speed-flists-inhibit-flag)
2367 (> (hash-table-count mh-speed-flists-cache) 0)))
2369 (defun mh-folder-completion-function (name predicate flag)
2370 "Programmable completion for folder names.
2371 NAME is the partial folder name that has been input. PREDICATE if
2372 non-nil is a function that is used to filter the possible choices
2373 and FLAG determines whether the completion is over."
2374 (let* ((orig-name name)
2375 (name (mh-normalize-folder-name name nil t))
2376 (last-slash (mh-search-from-end ?/ name))
2377 (last-complete (if last-slash (substring name 0 last-slash) nil))
2378 (remainder (cond (last-complete (substring name (1+ last-slash)))
2379 ((and (> (length name) 0) (equal (aref name 0) ?+))
2382 (cond ((eq flag nil)
2383 (let ((try-res (try-completion
2386 (cons (if (not last-complete)
2387 (concat "+" (car x))
2388 (concat last-complete "/" (car x)))
2390 (mh-sub-folders last-complete t))
2392 (cond ((eq try-res nil) nil)
2393 ((and (eq try-res t) (equal name orig-name)) t)
2394 ((eq try-res t) name)
2398 remainder (mh-sub-folders last-complete t) predicate))
2400 (let ((path (concat mh-user-path
2401 (substring (mh-normalize-folder-name name) 1))))
2402 (cond (mh-allow-root-folder-flag (file-exists-p path))
2403 ((equal path mh-user-path) nil)
2404 (t (file-exists-p path))))))))
2406 (defun mh-folder-completing-read (prompt default allow-root-folder-flag)
2407 "Read folder name with PROMPT and default result DEFAULT.
2408 If ALLOW-ROOT-FOLDER-FLAG is non-nil then \"+\" is allowed to be
2409 a folder name corresponding to `mh-user-path'."
2410 (mh-normalize-folder-name
2411 (let ((minibuffer-completing-file-name t)
2412 (completion-root-regexp "^[+/]")
2413 (minibuffer-local-completion-map mh-folder-completion-map)
2414 (mh-allow-root-folder-flag allow-root-folder-flag))
2415 (completing-read prompt 'mh-folder-completion-function nil nil nil
2416 'mh-folder-hist default))
2419 (defun mh-prompt-for-folder (prompt default can-create
2420 &optional default-string allow-root-folder-flag)
2421 "Prompt for a folder name with PROMPT.
2422 Returns the folder's name as a string. DEFAULT is used if the
2423 folder exists and the user types return. If the CAN-CREATE flag
2424 is t, then a folder is created if it doesn't already exist. If
2425 optional argument DEFAULT-STRING is non-nil, use it in the prompt
2426 instead of DEFAULT. If ALLOW-ROOT-FOLDER-FLAG is non-nil then the
2427 function will accept the folder +, which means all folders when
2431 (let* ((default-string (cond (default-string (format " (default %s)" default-string))
2432 ((equal "" default) "")
2433 (t (format " (default %s)" default))))
2434 (prompt (format "%s folder%s: " prompt default-string))
2435 (mh-current-folder-name mh-current-folder)
2436 read-name folder-name)
2437 (while (and (setq read-name (mh-folder-completing-read
2438 prompt default allow-root-folder-flag))
2439 (equal read-name "")
2440 (equal default "")))
2441 (cond ((or (equal read-name "")
2442 (and (equal read-name "+") (not allow-root-folder-flag)))
2443 (setq read-name default))
2444 ((not (mh-folder-name-p read-name))
2445 (setq read-name (format "+%s" read-name))))
2446 (if (or (not read-name) (equal "" read-name))
2447 (error "No folder specified"))
2448 (setq folder-name read-name)
2449 (cond ((and (> (length folder-name) 0)
2450 (eq (aref folder-name (1- (length folder-name))) ?/))
2451 (setq folder-name (substring folder-name 0 -1))))
2452 (let* ((last-slash (mh-search-from-end ?/ folder-name))
2453 (parent (and last-slash (substring folder-name 0 last-slash)))
2454 (child (if last-slash
2455 (substring folder-name (1+ last-slash))
2456 (substring folder-name 1))))
2457 (unless (member child
2458 (mapcar #'car (gethash parent mh-sub-folders-cache)))
2459 (mh-remove-from-sub-folders-cache folder-name)))
2460 (let ((new-file-flag
2461 (not (file-exists-p (mh-expand-file-name folder-name)))))
2462 (cond ((and new-file-flag
2465 (format "Folder %s does not exist. Create it? "
2467 (message "Creating %s" folder-name)
2468 (mh-exec-cmd-error nil "folder" folder-name)
2469 (mh-remove-from-sub-folders-cache folder-name)
2470 (when (boundp 'mh-speed-folder-map)
2471 (mh-speed-add-folder folder-name))
2472 (message "Creating %s...done" folder-name))
2474 (error "Folder %s does not exist" folder-name))
2475 ((not (file-directory-p (mh-expand-file-name folder-name)))
2476 (error "%s is not a directory"
2477 (mh-expand-file-name folder-name)))))
2480 (defun mh-truncate-log-buffer ()
2481 "If `mh-log-buffer' is too big then truncate it.
2482 If the number of lines in `mh-log-buffer' exceeds
2483 `mh-log-buffer-lines' then keep only the last
2484 `mh-log-buffer-lines'. As a side effect the point is set to the
2485 end of the log buffer.
2487 The function returns the size of the final size of the log buffer."
2488 (with-current-buffer (get-buffer-create mh-log-buffer)
2489 (goto-char (point-max))
2491 (when (equal (forward-line (- mh-log-buffer-lines)) 0)
2492 (delete-region (point-min) (point))))
2495 (and (equal (forward-line -1) 0) (equal (char-after) ?
\f))))
2501 ;;; Issue commands to MH.
2503 (defun mh-exec-cmd (command &rest args)
2504 "Execute mh-command COMMAND with ARGS.
2505 The side effects are what is desired. Any output is assumed to be
2506 an error and is shown to the user. The output is not read or
2509 (set-buffer (get-buffer-create mh-log-buffer))
2510 (let* ((initial-size (mh-truncate-log-buffer))
2512 (args (mh-list-to-string args)))
2513 (apply 'call-process (expand-file-name command mh-progs) nil t nil args)
2514 (when (> (buffer-size) initial-size)
2517 (insert "Errors when executing: " command)
2518 (loop for arg in args do (insert " " arg))
2520 (save-window-excursion
2521 (switch-to-buffer-other-window mh-log-buffer)
2524 (defun mh-exec-cmd-error (env command &rest args)
2525 "In environment ENV, execute mh-command COMMAND with ARGS.
2526 ENV is nil or a string of space-separated \"var=value\" elements.
2527 Signals an error if process does not complete successfully."
2529 (set-buffer (get-buffer-create mh-temp-buffer))
2531 (let ((process-environment process-environment))
2532 ;; XXX: We should purge the list that split-string returns of empty
2533 ;; strings. This can happen in XEmacs if leading or trailing spaces
2535 (dolist (elem (if (stringp env) (split-string env " ") ()))
2536 (push elem process-environment))
2537 (mh-handle-process-error
2538 command (apply #'call-process (expand-file-name command mh-progs)
2539 nil t nil (mh-list-to-string args))))))
2541 (defun mh-exec-cmd-daemon (command filter &rest args)
2542 "Execute MH command COMMAND in the background.
2544 If FILTER is non-nil then it is used to process the output
2545 otherwise the default filter `mh-process-daemon' is used. See
2546 `set-process-filter' for more details of FILTER.
2548 ARGS are passed to COMMAND as command line arguments."
2550 (set-buffer (get-buffer-create mh-log-buffer))
2551 (mh-truncate-log-buffer))
2552 (let* ((process-connection-type nil)
2553 (process (apply 'start-process
2555 (expand-file-name command mh-progs)
2556 (mh-list-to-string args))))
2557 (set-process-filter process (or filter 'mh-process-daemon))
2560 (defun mh-exec-cmd-env-daemon (env command filter &rest args)
2561 "In ennvironment ENV, execute mh-command COMMAND in the background.
2563 ENV is nil or a string of space-separated \"var=value\" elements.
2564 Signals an error if process does not complete successfully.
2566 If FILTER is non-nil then it is used to process the output
2567 otherwise the default filter `mh-process-daemon' is used. See
2568 `set-process-filter' for more details of FILTER.
2570 ARGS are passed to COMMAND as command line arguments."
2571 (let ((process-environment process-environment))
2572 (dolist (elem (if (stringp env) (split-string env " ") ()))
2573 (push elem process-environment))
2574 (apply #'mh-exec-cmd-daemon command filter args)))
2576 (defun mh-process-daemon (process output)
2577 "PROCESS daemon that puts OUTPUT into a temporary buffer.
2578 Any output from the process is displayed in an asynchronous
2580 (with-current-buffer (get-buffer-create mh-log-buffer)
2581 (insert-before-markers output)
2582 (display-buffer mh-log-buffer)))
2584 (defun mh-exec-cmd-quiet (raise-error command &rest args)
2585 "Signal RAISE-ERROR if COMMAND with ARGS fails.
2586 Execute MH command COMMAND with ARGS. ARGS is a list of strings.
2587 Return at start of mh-temp buffer, where output can be parsed and
2589 Returns value of `call-process', which is 0 for success, unless
2590 RAISE-ERROR is non-nil, in which case an error is signaled if
2591 `call-process' returns non-0."
2592 (set-buffer (get-buffer-create mh-temp-buffer))
2595 (apply 'call-process
2596 (expand-file-name command mh-progs) nil t nil
2598 (goto-char (point-min))
2600 (mh-handle-process-error command value)
2603 (defun mh-profile-component (component)
2604 "Return COMPONENT value from mhparam, or nil if unset."
2606 (mh-exec-cmd-quiet nil "mhparam" "-components" component)
2607 (mh-get-profile-field (concat component ":"))))
2609 (eval-when-compile (defvar mark-active)) ;shush compiler
2611 (defun mh-exchange-point-and-mark-preserving-active-mark ()
2612 "Put the mark where point is now, and point where the mark is now.
2613 This command works even when the mark is not active, and
2614 preserves whether the mark is active or not."
2616 (let ((is-active (and (boundp 'mark-active) mark-active)))
2617 (let ((omark (mark t)))
2619 (error "No mark set in this buffer"))
2622 (if (boundp 'mark-active)
2623 (setq mark-active is-active))
2626 (defun mh-exec-cmd-output (command display &rest args)
2627 "Execute MH command COMMAND with DISPLAY flag and ARGS.
2628 Put the output into buffer after point.
2629 Set mark after inserted text.
2630 Output is expected to be shown to user, not parsed by MH-E."
2631 (push-mark (point) t)
2632 (apply 'call-process
2633 (expand-file-name command mh-progs) nil t display
2634 (mh-list-to-string args))
2636 ;; The following is used instead of 'exchange-point-and-mark because the
2637 ;; latter activates the current region (between point and mark), which
2638 ;; turns on highlighting. So prior to this bug fix, doing "inc" would
2639 ;; highlight a region containing the new messages, which is undesirable.
2640 ;; The bug wasn't seen in emacs21 but still occurred in XEmacs21.4.
2641 (mh-exchange-point-and-mark-preserving-active-mark))
2643 (defun mh-exec-lib-cmd-output (command &rest args)
2644 "Execute MH library command COMMAND with ARGS.
2645 Put the output into buffer after point.
2646 Set mark after inserted text."
2647 (apply 'mh-exec-cmd-output (expand-file-name command mh-lib-progs) nil args))
2649 (defun mh-handle-process-error (command status)
2650 "Raise error if COMMAND returned non-zero STATUS, otherwise return STATUS."
2651 (if (equal status 0)
2653 (goto-char (point-min))
2654 (insert (if (integerp status)
2655 (format "%s: exit code %d\n" command status)
2656 (format "%s: %s\n" command status)))
2658 (let ((error-message (buffer-substring (point-min) (point-max))))
2659 (set-buffer (get-buffer-create mh-log-buffer))
2660 (mh-truncate-log-buffer)
2661 (insert error-message)))
2662 (error "%s failed, check buffer %s for error message"
2663 command mh-log-buffer)))
2665 (defun mh-list-to-string (l)
2666 "Flatten the list L and make every element of the new list into a string."
2667 (nreverse (mh-list-to-string-1 l)))
2669 (defun mh-list-to-string-1 (l)
2670 "Flatten the list L and make every element of the new list into a string."
2671 (let ((new-list nil))
2673 (cond ((null (car l)))
2675 (setq new-list (cons (symbol-name (car l)) new-list)))
2677 (setq new-list (cons (int-to-string (car l)) new-list)))
2678 ((equal (car l) ""))
2679 ((stringp (car l)) (setq new-list (cons (car l) new-list)))
2681 (setq new-list (nconc (mh-list-to-string-1 (car l))
2683 (t (error "Bad element in `mh-list-to-string': %s" (car l))))
2687 (defun mh-replace-string (old new)
2688 "Replace all occurrences of OLD with NEW in the current buffer.
2689 Ignores case when searching for OLD."
2690 (goto-char (point-min))
2691 (let ((case-fold-search t))
2692 (while (search-forward old nil t)
2693 (replace-match new t t))))
2698 ;; indent-tabs-mode: nil
2699 ;; sentence-end-double-space: nil
2702 ;; arch-tag: 1af39fdf-f66f-4b06-9b48-18a7656c8e36
2703 ;;; mh-utils.el ends here