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