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