]> code.delx.au - gnu-emacs/blob - lisp/mh-e/mh-e.el
* mh-acros.el:
[gnu-emacs] / lisp / mh-e / mh-e.el
1 ;;; mh-e.el --- GNU Emacs interface to the MH mail system
2
3 ;; Copyright (C) 1985, 1986, 1987, 1988,
4 ;; 1990, 1992, 1993, 1994, 1995, 1997, 1999,
5 ;; 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
6
7 ;; Author: Bill Wohler <wohler@newt.com>
8 ;; Maintainer: Bill Wohler <wohler@newt.com>
9 ;; Version: 7.85+cvs
10 ;; Keywords: mail
11
12 ;; This file is part of GNU Emacs.
13
14 ;; GNU Emacs is free software; you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; any later version.
18
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
23
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs; see the file COPYING. If not, write to the
26 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
27 ;; Boston, MA 02110-1301, USA.
28
29 ;;; Commentary:
30
31 ;; How to Use:
32 ;; M-x mh-rmail to read mail. Type C-h m there for a list of commands.
33 ;; C-u M-x mh-rmail to visit any folder.
34 ;; M-x mh-smail to send mail. From within the mail reader, "m" works, too.
35
36 ;; Your .emacs might benefit from these bindings:
37 ;; (global-set-key "\C-cr" 'mh-rmail)
38 ;; (global-set-key "\C-xm" 'mh-smail)
39 ;; (global-set-key "\C-x4m" 'mh-smail-other-window)
40
41 ;; MH (Message Handler) is a powerful mail reader.
42
43 ;; The MH newsgroup is comp.mail.mh; the mailing list is mh-users@ics.uci.edu
44 ;; (send to mh-users-request to be added). See the monthly Frequently Asked
45 ;; Questions posting there for information on getting MH and MH-E:
46 ;; http://www.faqs.org/faqs/mail/mh-faq/part1/preamble.html
47
48 ;; N.B. MH must have been compiled with the MHE compiler flag or several
49 ;; features necessary for MH-E will be missing from MH commands, specifically
50 ;; the -build switch to repl and forw.
51
52 ;; MH-E is an Emacs interface to the MH mail system.
53
54 ;; MH-E is supported in GNU Emacs 20 and 21, with MH 6.8.4 and nmh 1.0.4.
55
56 ;; Mailing Lists:
57 ;; mh-e-users@lists.sourceforge.net
58 ;; mh-e-announce@lists.sourceforge.net
59 ;; mh-e-devel@lists.sourceforge.net
60 ;;
61 ;; Subscribe by sending a "subscribe" message to
62 ;; <list>-request@lists.sourceforge.net, or by using the web interface at
63 ;; https://sourceforge.net/mail/?group_id=13357
64
65 ;; Bug Reports:
66 ;; https://sourceforge.net/tracker/?group_id=13357&atid=113357
67 ;; Include the output of M-x mh-version in any bug report.
68
69 ;; Feature Requests:
70 ;; https://sourceforge.net/tracker/?atid=363357&group_id=13357&func=browse
71
72 ;; Support:
73 ;; https://sourceforge.net/tracker/?group_id=13357&atid=213357
74
75 ;;; Change Log:
76
77 ;; Original version for Gosling emacs by Brian Reid, Stanford, 1982.
78 ;; Modified by James Larus, BBN, July 1984 and UCB, 1984 & 1985.
79 ;; Rewritten for GNU Emacs, James Larus, 1985.
80 ;; Modified by Stephen Gildea, 1988.
81 ;; Maintenance picked up by Bill Wohler and the
82 ;; SourceForge Crew <http://mh-e.sourceforge.net/>, 2001.
83
84 ;;; Code:
85
86 (provide 'mh-e)
87
88 (eval-when-compile (require 'mh-acros))
89 (mh-require-cl)
90 (require 'mh-utils)
91 (require 'mh-init)
92 (require 'mh-inc)
93 (require 'mh-seq)
94 (require 'gnus-util)
95 (require 'easymenu)
96
97 ;; Shush the byte-compiler
98 (defvar font-lock-auto-fontify)
99 (defvar font-lock-defaults)
100
101 (defconst mh-version "7.85+cvs" "Version number of MH-E.")
102
103 (defvar mh-partial-folder-mode-line-annotation "select"
104 "Annotation when displaying part of a folder.
105 The string is displayed after the folder's name. nil for no annotation.")
106
107 \f
108
109 ;;; Scan Line Formats
110
111 ;; Parameterize MH-E to work with different scan formats. The defaults work
112 ;; with the standard MH scan listings, in which the first 4 characters on
113 ;; the line are the message number, followed by two places for notations.
114
115 ;; The following scan formats are passed to the scan program if the setting of
116 ;; `mh-scan-format-file' is t. They are identical except the later one makes
117 ;; use of the nmh `decode' function to decode RFC 2047 encodings. If you just
118 ;; want to change the column of the notations, use the `mh-set-cmd-note'
119 ;; function.
120
121 (defvar mh-scan-format-mh
122 (concat
123 "%4(msg)"
124 "%<(cur)+%| %>"
125 "%<{replied}-"
126 "%?(nonnull(comp{to}))%<(mymbox{to})t%>"
127 "%?(nonnull(comp{cc}))%<(mymbox{cc})c%>"
128 "%?(nonnull(comp{bcc}))%<(mymbox{bcc})b%>"
129 "%?(nonnull(comp{newsgroups}))n%>"
130 "%<(zero) %>"
131 "%02(mon{date})/%02(mday{date})%<{date} %|*%>"
132 "%<(mymbox{from})%<{to}To:%14(friendly{to})%>%>"
133 "%<(zero)%17(friendly{from})%> "
134 "%{subject}%<{body}<<%{body}%>")
135 "*Scan format string for MH.
136 This string is passed to the scan program via the -format argument.
137 This format is identical to the default except that additional hints for
138 fontification have been added to the fifth column (remember that in Emacs, the
139 first column is 0).
140
141 The values of the fifth column, in priority order, are: `-' if the message has
142 been replied to, t if an address on the To: line matches one of the
143 mailboxes of the current user, `c' if the Cc: line matches, `b' if the Bcc:
144 line matches, and `n' if a non-empty Newsgroups: header is present.")
145
146 (defvar mh-scan-format-nmh
147 (concat
148 "%4(msg)"
149 "%<(cur)+%| %>"
150 "%<{replied}-"
151 "%?(nonnull(comp{to}))%<(mymbox{to})t%>"
152 "%?(nonnull(comp{cc}))%<(mymbox{cc})c%>"
153 "%?(nonnull(comp{bcc}))%<(mymbox{bcc})b%>"
154 "%?(nonnull(comp{newsgroups}))n%>"
155 "%<(zero) %>"
156 "%02(mon{date})/%02(mday{date})%<{date} %|*%>"
157 "%<(mymbox{from})%<{to}To:%14(decode(friendly{to}))%>%>"
158 "%<(zero)%17(decode(friendly{from}))%> "
159 "%(decode{subject})%<{body}<<%{body}%>")
160 "*Scan format string for nmh.
161 This string is passed to the scan program via the -format arg.
162 This format is identical to the default except that additional hints for
163 fontification have been added to the fifth column (remember that in Emacs, the
164 first column is 0).
165
166 The values of the fifth column, in priority order, are: `-' if the message has
167 been replied to, t if an address on the To: field matches one of the
168 mailboxes of the current user, `c' if the Cc: field matches, `b' if the Bcc:
169 field matches, and `n' if a non-empty Newsgroups: field is present.")
170
171 (defvar mh-note-deleted ?D
172 "Messages that have been deleted are marked by this character.
173 See also `mh-scan-deleted-msg-regexp'.")
174
175 (defvar mh-note-refiled ?^
176 "Messages that have been refiled are marked by this character.
177 See also `mh-scan-refiled-msg-regexp'.")
178
179 (defvar mh-note-cur ?+
180 "The current message (in MH, not in MH-E) is marked by this character.
181 See also `mh-scan-cur-msg-number-regexp'.")
182
183 (defvar mh-scan-good-msg-regexp "^\\( *[0-9]+\\)[^D^0-9]"
184 "This regular expression matches \"good\" messages.
185 It must match from the beginning of the line. Note that the default setting of
186 `mh-folder-font-lock-keywords' expects this expression to contain at least one
187 parenthesized expression which matches the message number as in the default of
188 \"^\\\\( *[0-9]+\\\\)[^D^0-9]\". This expression includes the leading space
189 within the parenthesis since it looks better to highlight it as well. This
190 regular expression should be correct as it is needed by non-fontification
191 functions.")
192
193 (defvar mh-scan-deleted-msg-regexp "^\\( *[0-9]+\\)D"
194 "This regular expression matches deleted messages.
195 It must match from the beginning of the line. Note that the default setting of
196 `mh-folder-font-lock-keywords' expects this expression to contain at least one
197 parenthesized expression which matches the message number as in the default of
198 \"^\\\\( *[0-9]+\\\\)D\". This expression includes the leading space within
199 the parenthesis since it looks better to highlight it as well. This regular
200 expression should be correct as it is needed by non-fontification functions.
201 See also `mh-note-deleted'.")
202
203 (defvar mh-scan-refiled-msg-regexp "^\\( *[0-9]+\\)\\^"
204 "This regular expression matches refiled messages.
205 It must match from the beginning of the line. Note that the default setting of
206 `mh-folder-font-lock-keywords' expects this expression to contain at least one
207 parenthesized expression which matches the message number as in the default of
208 \"^\\\\( *[0-9]+\\\\)\\\\^\". This expression includes the leading space
209 within the parenthesis since it looks better to highlight it as well. This
210 regular expression should be correct as it is needed by non-fontification
211 functions. See also `mh-note-refiled'.")
212
213 (defvar mh-scan-valid-regexp "^ *[0-9]"
214 "This regular expression describes a valid scan line.
215 This is used to eliminate error messages that are occasionally produced by
216 \"inc\".")
217
218 (defvar mh-scan-cur-msg-number-regexp "^\\( *[0-9]+\\+\\).*"
219 "This regular expression matches the current message.
220 It must match from the beginning of the line. Note that the default setting of
221 `mh-folder-font-lock-keywords' expects this expression to contain at least one
222 parenthesized expression which matches the message number as in the default of
223 \"^\\\\( *[0-9]+\\\\+\\\\).*\". This expression includes the leading space and
224 current message marker \"+\" within the parenthesis since it looks better to
225 highlight these items as well. This regular expression should be correct as it
226 is needed by non-fontification functions. See also `mh-note-cur'.")
227
228 (defvar mh-scan-date-regexp "\\([0-9][0-9]/[0-9][0-9]\\)"
229 "This regular expression matches a valid date.
230 It must not be anchored to the beginning or the end of the line. Note that the
231 default setting of `mh-folder-font-lock-keywords' expects this expression to
232 contain only one parenthesized expression which matches the date field as in
233 the default of \"\\\\([0-9][0-9]/[0-9][0-9]\\\\)\"}. If this regular
234 expression is not correct, the date will not be highlighted. See also
235 `mh-scan-format-regexp'.")
236
237 (defvar mh-scan-rcpt-regexp "\\(To:\\)\\(..............\\)"
238 "This regular expression specifies the recipient in messages you sent.
239 Note that the default setting of `mh-folder-font-lock-keywords'
240 expects this expression to contain two parenthesized expressions. The
241 first is expected to match the `To:' that the default scan format
242 file generates. The second is expected to match the recipient's name
243 as in the default of \"\\\\(To:\\\\)\\\\(..............\\\\)\". If this
244 regular expression is not correct, the recipient will not be highlighted.")
245
246 (defvar mh-scan-body-regexp "\\(<<\\([^\n]+\\)?\\)"
247 "This regular expression matches the message body fragment.
248 Note that the default setting of `mh-folder-font-lock-keywords' expects this
249 expression to contain at least one parenthesized expression which matches the
250 body text as in the default of \"\\\\(<<\\\\([^\\n]+\\\\)?\\\\)\". If this
251 regular expression is not correct, the body fragment will not be highlighted.")
252
253 (defvar mh-scan-subject-regexp
254 "^ *[0-9]+........[ ]*...................\\([Rr][Ee]\\(\\[[0-9]+\\]\\)?:\\s-*\\)*\\([^<\n]*\\)"
255 "This regular expression matches the subject.
256 It must match from the beginning of the line. Note that the default setting
257 of `mh-folder-font-lock-keywords' expects this expression to contain at least
258 three parenthesized expressions. The first is expected to match the `Re:'
259 string, if any. The second matches an optional bracketed number after `Re:',
260 such as in `Re[2]:' (and is thus a sub-expression of the first expression) and
261 the third is expected to match the subject line itself as in the default of
262 \(broken on multiple lines for readability):
263 ^ *[0-9]+........[ ]*...................
264 \\\\([Rr][Ee]\\\\(\\\\\\=[[0-9]+\\\\]\\\\)?:\\\\s-*\\\\)*
265 \\\\([^<\\n]*\\\\)
266 This regular expression should be correct as it is needed by non-fontification
267 functions.")
268
269 (defvar mh-scan-format-regexp
270 (concat "\\([bct]\\)" mh-scan-date-regexp " *\\(..................\\)")
271 "This regular expression matches the output of scan.
272 Note that the default setting of `mh-folder-font-lock-keywords' expects this
273 expression to contain at least three parenthesized expressions. The first
274 should match the fontification hint (see `mh-scan-format-nmh'), the second is
275 found in `mh-scan-date-regexp', and the third should match the user name as in
276 the default of \"(concat \"\\\\([bct]\\\\)\" mh-scan-date-regexp
277 \"*\\\\(..................\\\\)\")\".
278 If this regular expression is not correct, the notation hints and the sender
279 will not be highlighted.")
280
281 \f
282
283 (defvar mh-folder-font-lock-keywords
284 (list
285 ;; Folders when displaying index buffer
286 (list "^\\+.*"
287 '(0 mh-index-folder-face))
288 ;; Marked for deletion
289 (list (concat mh-scan-deleted-msg-regexp ".*")
290 '(0 mh-folder-deleted-face))
291 ;; Marked for refile
292 (list (concat mh-scan-refiled-msg-regexp ".*")
293 '(0 mh-folder-refiled-face))
294 ;;after subj
295 (list mh-scan-body-regexp '(1 mh-folder-body-face nil t))
296 '(mh-folder-font-lock-subject
297 (1 mh-folder-followup-face append t)
298 (2 mh-folder-subject-face append t))
299 ;;current msg
300 (list mh-scan-cur-msg-number-regexp
301 '(1 mh-folder-cur-msg-number-face))
302 (list mh-scan-good-msg-regexp
303 '(1 mh-folder-msg-number-face)) ;; Msg number
304 (list mh-scan-date-regexp '(1 mh-folder-date-face)) ;; Date
305 (list mh-scan-rcpt-regexp
306 '(1 mh-folder-to-face) ;; To:
307 '(2 mh-folder-address-face)) ;; address
308 ;; scan font-lock name
309 (list mh-scan-format-regexp
310 '(1 mh-folder-date-face)
311 '(3 mh-folder-scan-format-face)))
312 "Keywords (regular expressions) used to fontify the MH-Folder buffer.")
313
314 (defvar mh-scan-cmd-note-width 1
315 "Number of columns consumed by the cmd-note field in `mh-scan-format'.
316 This column will have one of the values: ` ', `D', `^', `+' and where
317 ` ' is the default value,
318 `D' is the `mh-note-deleted' character,
319 `^' is the `mh-note-refiled' character, and
320 `+' is the `mh-note-cur' character.")
321
322 (defvar mh-scan-destination-width 1
323 "Number of columns consumed by the destination field in `mh-scan-format'.
324 This column will have one of ' ', '%', '-', 't', 'c', 'b', or `n' in it.
325 A ' ' blank space is the default character.
326 A '%' indicates that the message in in a named MH sequence.
327 A '-' indicates that the message has been annotated with a replied field.
328 A 't' indicates that the message contains mymbox in the To: field.
329 A 'c' indicates that the message contains mymbox in the Cc: field.
330 A 'b' indicates that the message contains mymbox in the Bcc: field.
331 A 'n' indicates that the message contains a Newsgroups: field.")
332
333 (defvar mh-scan-date-width 5
334 "Number of columns consumed by the date field in `mh-scan-format'.
335 This column will typically be of the form mm/dd.")
336
337 (defvar mh-scan-date-flag-width 1
338 "Number of columns consumed to flag (in)valid dates in `mh-scan-format'.
339 This column will have ` ' for valid and `*' for invalid or missing dates.")
340
341 (defvar mh-scan-from-mbox-width 17
342 "Number of columns consumed with the \"From:\" line in `mh-scan-format'.
343 This column will have a friendly name or e-mail address of the
344 originator, or a \"To: address\" for outgoing e-mail messages.")
345
346 (defvar mh-scan-from-mbox-sep-width 2
347 "Number of columns consumed by whitespace after from-mbox in `mh-scan-format'.
348 This column will only ever have spaces in it.")
349
350 (defvar mh-scan-field-destination-offset
351 (+ mh-scan-cmd-note-width)
352 "The offset from the `mh-cmd-note' for the destination column.")
353
354 (defvar mh-scan-field-from-start-offset
355 (+ mh-scan-cmd-note-width
356 mh-scan-destination-width
357 mh-scan-date-width
358 mh-scan-date-flag-width)
359 "The offset from the `mh-cmd-note' to find the start of \"From:\" address.")
360
361 (defvar mh-scan-field-from-end-offset
362 (+ mh-scan-field-from-start-offset mh-scan-from-mbox-width)
363 "The offset from the `mh-cmd-note' to find the end of \"From:\" address.")
364
365 (defvar mh-scan-field-subject-start-offset
366 (+ mh-scan-cmd-note-width
367 mh-scan-destination-width
368 mh-scan-date-width
369 mh-scan-date-flag-width
370 mh-scan-from-mbox-width
371 mh-scan-from-mbox-sep-width)
372 "The offset from the `mh-cmd-note' to find the start of the subject.")
373
374 (defun mh-folder-font-lock-subject (limit)
375 "Return MH-E scan subject strings to font-lock between point and LIMIT."
376 (if (not (re-search-forward mh-scan-subject-regexp limit t))
377 nil
378 (if (match-beginning 1)
379 (set-match-data (list (match-beginning 1) (match-end 3)
380 (match-beginning 1) (match-end 3) nil nil))
381 (set-match-data (list (match-beginning 3) (match-end 3)
382 nil nil (match-beginning 3) (match-end 3))))
383 t))
384
385 \f
386
387 ;; Fontifify unseen mesages in bold.
388
389 (defmacro mh-generate-sequence-font-lock (seq prefix face)
390 "Generate the appropriate code to fontify messages in SEQ.
391 PREFIX is used to generate unique names for the variables and functions
392 defined by the macro. So a different prefix should be provided for every
393 invocation.
394 FACE is the font-lock face used to display the matching scan lines."
395 (let ((cache (intern (format "mh-folder-%s-seq-cache" prefix)))
396 (func (intern (format "mh-folder-font-lock-%s" prefix))))
397 `(progn
398 (defvar ,cache nil
399 "Internal cache variable used for font-lock in MH-E.
400 Should only be non-nil through font-lock stepping, and nil once font-lock
401 is done highlighting.")
402 (make-variable-buffer-local ',cache)
403
404 (defun ,func (limit)
405 "Return unseen message lines to font-lock between point and LIMIT."
406 (if (not ,cache) (setq ,cache (mh-seq-msgs (mh-find-seq ,seq))))
407 (let ((cur-msg (mh-get-msg-num nil)))
408 (cond ((not ,cache)
409 nil)
410 ((>= (point) limit) ;Presumably at end of buffer
411 (setq ,cache nil)
412 nil)
413 ((member cur-msg ,cache)
414 (let ((bpoint (progn (beginning-of-line)(point)))
415 (epoint (progn (forward-line 1)(point))))
416 (if (<= limit (point)) (setq ,cache nil))
417 (set-match-data (list bpoint epoint bpoint epoint))
418 t))
419 (t
420 ;; move forward one line at a time, checking each message
421 (while (and (= 0 (forward-line 1))
422 (> limit (point))
423 (not (member (mh-get-msg-num nil) ,cache))))
424 ;; Examine how we must have exited the loop...
425 (let ((cur-msg (mh-get-msg-num nil)))
426 (cond ((or (<= limit (point))
427 (not (member cur-msg ,cache)))
428 (setq ,cache nil)
429 nil)
430 ((member cur-msg ,cache)
431 (let ((bpoint (progn (beginning-of-line) (point)))
432 (epoint (progn (forward-line 1) (point))))
433 (if (<= limit (point)) (setq ,cache nil))
434 (set-match-data
435 (list bpoint epoint bpoint epoint))
436 t))))))))
437
438 (setq mh-folder-font-lock-keywords
439 (append mh-folder-font-lock-keywords
440 (list (list ',func (list 1 '',face 'prepend t))))))))
441
442 (mh-generate-sequence-font-lock mh-unseen-seq unseen bold)
443 (mh-generate-sequence-font-lock mh-tick-seq tick mh-folder-tick)
444
445 \f
446
447 ;;; Internal variables:
448
449 (defvar mh-last-destination nil) ;Destination of last refile or write
450 ;command.
451 (defvar mh-last-destination-folder nil) ;Destination of last refile command.
452 (defvar mh-last-destination-write nil) ;Destination of last write command.
453
454 (defvar mh-folder-mode-map (make-keymap)
455 "Keymap for MH folders.")
456
457 (defvar mh-arrow-marker nil) ;Marker for arrow display in fringe.
458
459 (defvar mh-delete-list nil) ;List of msg numbers to delete.
460
461 (defvar mh-refile-list nil) ;List of folder names in mh-seq-list.
462
463 (defvar mh-next-direction 'forward) ;Direction to move to next message.
464
465 (defvar mh-view-ops ()) ;Stack of ops that change the folder
466 ;view (such as narrowing or threading).
467 (defvar mh-folder-view-stack ()) ;Stack of previous folder views.
468
469 (defvar mh-index-data nil) ;Info about index search results
470 (defvar mh-index-previous-search nil)
471 (defvar mh-index-msg-checksum-map nil)
472 (defvar mh-index-checksum-origin-map nil)
473 (defvar mh-index-sequence-search-flag nil)
474
475 (defvar mh-first-msg-num nil) ;Number of first msg in buffer.
476
477 (defvar mh-last-msg-num nil) ;Number of last msg in buffer.
478
479 (defvar mh-mode-line-annotation nil) ;Message range displayed in buffer.
480
481 (defvar mh-sequence-notation-history nil)
482 ;Rememeber original notation that
483 ;is overwritten by `mh-note-seq'.
484
485 (defvar mh-colors-available-flag nil) ;Are colors available?
486
487 \f
488
489 ;;; Macros and generic functions:
490
491 (defun mh-mapc (function list)
492 "Apply FUNCTION to each element of LIST for side effects only."
493 (while list
494 (funcall function (car list))
495 (setq list (cdr list))))
496
497 (defun mh-scan-format ()
498 "Return the output format argument for the scan program."
499 (if (equal mh-scan-format-file t)
500 (list "-format" (if (mh-variant-p 'nmh 'mu-mh)
501 (list (mh-update-scan-format
502 mh-scan-format-nmh mh-cmd-note))
503 (list (mh-update-scan-format
504 mh-scan-format-mh mh-cmd-note))))
505 (if (not (equal mh-scan-format-file nil))
506 (list "-form" mh-scan-format-file))))
507
508 \f
509
510 ;;; Entry points:
511
512 ;;;###autoload
513 (defun mh-rmail (&optional arg)
514 "Inc(orporate) new mail with MH.
515 Scan an MH folder if ARG is non-nil. This function is an entry point to MH-E,
516 the Emacs interface to the MH mail system."
517 (interactive "P")
518 (mh-find-path)
519 (if arg
520 (call-interactively 'mh-visit-folder)
521 (unless (get-buffer mh-inbox)
522 (mh-visit-folder mh-inbox (symbol-name mh-unseen-seq)))
523 (mh-inc-folder)))
524
525 ;;;###autoload
526 (defun mh-nmail (&optional arg)
527 "Check for new mail in inbox folder.
528 Scan an MH folder if ARG is non-nil. This function is an entry point to MH-E,
529 the Emacs interface to the MH mail system."
530 (interactive "P")
531 (mh-find-path) ; init mh-inbox
532 (if arg
533 (call-interactively 'mh-visit-folder)
534 (mh-visit-folder mh-inbox)))
535
536 \f
537
538 ;;; User executable MH-E commands:
539
540 (defun mh-delete-msg (range)
541 "Delete RANGE\\<mh-folder-mode-map>.
542
543 To mark a message for deletion, use this command. A \"D\" is placed by the
544 message in the scan window, and the next undeleted message is displayed. If
545 the previous command had been \\[mh-previous-undeleted-msg], then the next
546 message displayed is the first undeleted message previous to the message just
547 deleted. Use \\[mh-next-undeleted-msg] to force subsequent \\[mh-delete-msg]
548 commands to move forward to the next undeleted message after deleting the
549 message under the cursor.
550
551 Check the documentation of `mh-interactive-range' to see how RANGE is read in
552 interactive use."
553 (interactive (list (mh-interactive-range "Delete")))
554 (mh-delete-msg-no-motion range)
555 (if (looking-at mh-scan-deleted-msg-regexp) (mh-next-msg)))
556
557 (defun mh-delete-msg-no-motion (range)
558 "Delete RANGE, don't move to next message.
559
560 This command marks the RANGE for deletion but leaves the cursor at the current
561 message in case you wish to perform other operations on the message.
562
563 Check the documentation of `mh-interactive-range' to see how RANGE is read in
564 interactive use."
565 (interactive (list (mh-interactive-range "Delete")))
566 (mh-iterate-on-range () range
567 (mh-delete-a-msg nil)))
568
569 (defun mh-execute-commands ()
570 "Process outstanding delete and refile requests."
571 (interactive)
572 (if mh-folder-view-stack (mh-widen t))
573 (mh-process-commands mh-current-folder)
574 (mh-set-scan-mode)
575 (mh-goto-cur-msg) ; after mh-set-scan-mode for efficiency
576 (mh-make-folder-mode-line)
577 t) ; return t for write-file-functions
578
579 (defun mh-first-msg ()
580 "Display first message."
581 (interactive)
582 (goto-char (point-min))
583 (while (and (not (eobp)) (not (looking-at mh-scan-valid-regexp)))
584 (forward-line 1)))
585
586 (defun mh-header-display ()
587 "Display message with all header fields\\<mh-folder-mode-map>.
588
589 Use the command \\[mh-show] to show the message normally again."
590 (interactive)
591 (and (not mh-showing-with-headers)
592 (or mh-mhl-format-file mh-clean-message-header-flag)
593 (mh-invalidate-show-buffer))
594 (let ((mh-decode-mime-flag nil)
595 (mh-mhl-format-file nil)
596 (mh-clean-message-header-flag nil))
597 (mh-show-msg nil)
598 (mh-in-show-buffer (mh-show-buffer)
599 (goto-char (point-min))
600 (mh-recenter 0))
601 (setq mh-showing-with-headers t)))
602
603 (defun mh-inc-folder (&optional maildrop-name folder)
604 "Inc(orporate)s new mail into the Inbox folder.
605 Optional argument MAILDROP-NAME specifies an alternate maildrop from the
606 default. The optional argument FOLDER specifies where to incorporate mail
607 instead of the default named by `mh-inbox'.
608 The value of `mh-inc-folder-hook' is a list of functions to be called, with no
609 arguments, after incorporating new mail.
610 Do not call this function from outside MH-E; use \\[mh-rmail] instead."
611 (interactive (list (if current-prefix-arg
612 (expand-file-name
613 (read-file-name "inc mail from file: "
614 mh-user-path)))
615 (if current-prefix-arg
616 (mh-prompt-for-folder "inc mail into" mh-inbox t))))
617 (if (not folder)
618 (setq folder mh-inbox))
619 (let ((threading-needed-flag nil))
620 (let ((config (current-window-configuration)))
621 (when (and mh-show-buffer (get-buffer mh-show-buffer))
622 (delete-windows-on mh-show-buffer))
623 (cond ((not (get-buffer folder))
624 (mh-make-folder folder)
625 (setq threading-needed-flag mh-show-threads-flag)
626 (setq mh-previous-window-config config))
627 ((not (eq (current-buffer) (get-buffer folder)))
628 (switch-to-buffer folder)
629 (setq mh-previous-window-config config))))
630 (mh-get-new-mail maildrop-name)
631 (when (and threading-needed-flag
632 (save-excursion
633 (goto-char (point-min))
634 (or (null mh-large-folder)
635 (not (equal (forward-line (1+ mh-large-folder)) 0))
636 (and (message "Not threading since the number of messages exceeds `mh-large-folder'")
637 nil))))
638 (mh-toggle-threads))
639 (beginning-of-line)
640 (if (and mh-showing-mode (looking-at mh-scan-valid-regexp)) (mh-show))
641 (run-hooks 'mh-inc-folder-hook)))
642
643 (defun mh-last-msg ()
644 "Display last message."
645 (interactive)
646 (goto-char (point-max))
647 (while (and (not (bobp)) (not (looking-at mh-scan-valid-regexp)))
648 (forward-line -1))
649 (mh-recenter nil))
650
651 (defun mh-next-undeleted-msg (&optional count wait-after-complaining-flag)
652 "Display next message.
653
654 This command can be given a prefix argument COUNT to specify how many unread
655 messages to skip.
656
657 In a program, pause for a second after printing message if we are at the last
658 undeleted message and optional argument WAIT-AFTER-COMPLAINING-FLAG is
659 non-nil."
660 (interactive "p")
661 (setq mh-next-direction 'forward)
662 (forward-line 1)
663 (cond ((re-search-forward mh-scan-good-msg-regexp nil t count)
664 (beginning-of-line)
665 (mh-maybe-show))
666 (t (forward-line -1)
667 (message "No more undeleted messages")
668 (if wait-after-complaining-flag (sit-for 1)))))
669
670 (defun mh-folder-from-address ()
671 "Derive folder name from sender.
672
673 The name of the folder is derived as follows:
674
675 a) The folder name associated with the first address found in the list
676 `mh-default-folder-list' is used. Each element in this list contains a
677 `Check Recipient' item. If this item is turned on, then the address is
678 checked against the recipient instead of the sender. This is useful for
679 mailing lists.
680
681 b) An alias prefixed by `mh-default-folder-prefix' corresponding to the
682 address is used. The prefix is used to prevent clutter in your mail
683 directory.
684
685 Return nil if a folder name was not derived, or if the variable
686 `mh-default-folder-must-exist-flag' is t and the folder does not exist."
687 ;; Loop for all entries in mh-default-folder-list
688 (save-restriction
689 (goto-char (point-min))
690 (re-search-forward "\n\n" nil 'limit)
691 (narrow-to-region (point-min) (point))
692 (let ((to/cc (concat (or (message-fetch-field "to") "") ", "
693 (or (message-fetch-field "cc") "")))
694 (from (or (message-fetch-field "from") ""))
695 folder-name)
696 (setq folder-name
697 (loop for list in mh-default-folder-list
698 when (string-match (nth 0 list) (if (nth 2 list) to/cc from))
699 return (nth 1 list)
700 finally return nil))
701
702 ;; Make sure a result from `mh-default-folder-list' begins with "+"
703 ;; since 'mh-expand-file-name below depends on it
704 (when (and folder-name (not (eq (aref folder-name 0) ?+)))
705 (setq folder-name (concat "+" folder-name)))
706
707 ;; If not, is there an alias for the address?
708 (when (not folder-name)
709 (let* ((from-header (mh-extract-from-header-value))
710 (address (and from-header
711 (nth 1 (mail-extract-address-components
712 from-header))))
713 (alias (and address (mh-alias-address-to-alias address))))
714 (when alias
715 (setq folder-name
716 (and alias (concat "+" mh-default-folder-prefix alias))))))
717
718 ;; If mh-default-folder-must-exist-flag set, check that folder exists.
719 (if (and folder-name
720 (or (not mh-default-folder-must-exist-flag)
721 (file-exists-p (mh-expand-file-name folder-name))))
722 folder-name))))
723
724 (defun mh-prompt-for-refile-folder ()
725 "Prompt the user for a folder in which the message should be filed.
726 The folder is returned as a string.
727
728 The default folder name is generated by the option
729 `mh-default-folder-for-message-function' if it is non-nil or
730 `mh-folder-from-address'."
731 (mh-prompt-for-folder
732 "Destination"
733 (let ((refile-file (ignore-errors (mh-msg-filename (mh-get-msg-num t)))))
734 (if (null refile-file) ""
735 (save-excursion
736 (set-buffer (get-buffer-create mh-temp-buffer))
737 (erase-buffer)
738 (insert-file-contents refile-file)
739 (or (and mh-default-folder-for-message-function
740 (let ((buffer-file-name refile-file))
741 (funcall mh-default-folder-for-message-function)))
742 (mh-folder-from-address)
743 (and (eq 'refile (car mh-last-destination-folder))
744 (symbol-name (cdr mh-last-destination-folder)))
745 ""))))
746 t))
747
748 (defun mh-refile-msg (range folder &optional dont-update-last-destination-flag)
749 "Refile (output) RANGE into FOLDER.
750
751 You are prompted for the folder name. Note that this command can also be used
752 to create folders. If you specify a folder that does not exist, you will be
753 prompted to create it.
754
755 The hook `mh-refile-msg-hook' is called after a message is marked to be
756 refiled.
757
758 Check the documentation of `mh-interactive-range' to see how RANGE is read in
759 interactive use.
760
761 If DONT-UPDATE-LAST-DESTINATION-FLAG is non-nil, then the variables
762 `mh-last-destination' and `mh-last-destination-folder' are not updated."
763 (interactive (list (mh-interactive-range "Refile")
764 (intern (mh-prompt-for-refile-folder))))
765 (unless dont-update-last-destination-flag
766 (setq mh-last-destination (cons 'refile folder)
767 mh-last-destination-folder mh-last-destination))
768 (mh-iterate-on-range () range
769 (mh-refile-a-msg nil folder))
770 (when (looking-at mh-scan-refiled-msg-regexp) (mh-next-msg)))
771
772 (defun mh-refile-or-write-again (range &optional interactive-flag)
773 "Repeat last output command.
774
775 If you are refiling several messages into the same folder, you can use this
776 command to repeat the last refile or write. You can use a range.
777
778 Check the documentation of `mh-interactive-range' to see how RANGE is read in
779 interactive use.
780
781 In a program, a non-nil INTERACTIVE-FLAG means that the function was called
782 interactively."
783 (interactive (list (mh-interactive-range "Redo") t))
784 (if (null mh-last-destination)
785 (error "No previous refile or write"))
786 (cond ((eq (car mh-last-destination) 'refile)
787 (mh-refile-msg range (cdr mh-last-destination))
788 (message "%s" (format "Destination folder: %s"
789 (cdr mh-last-destination))))
790 (t
791 (mh-iterate-on-range msg range
792 (apply 'mh-write-msg-to-file msg (cdr mh-last-destination)))
793 (mh-next-msg interactive-flag))))
794
795 (defun mh-quit ()
796 "Quit the current MH-E folder.
797 Restore the previous window configuration, if one exists.
798 The value of `mh-before-quit-hook' is a list of functions to be called, with
799 no arguments, immediately upon entry to this function.
800 The value of `mh-quit-hook' is a list of functions to be called, with no
801 arguments, upon exit of this function.
802 MH-E working buffers (whose name begins with \" *mh-\" or \"*MH-E \") are
803 killed."
804 (interactive)
805 (run-hooks 'mh-before-quit-hook)
806 (let ((show-buffer (get-buffer mh-show-buffer)))
807 (when show-buffer
808 (kill-buffer show-buffer)))
809 (mh-update-sequences)
810 (mh-destroy-postponed-handles)
811 (bury-buffer (current-buffer))
812
813 ;; Delete all MH-E temporary and working buffers.
814 (dolist (buffer (buffer-list))
815 (when (or (string-match "^ \\*mh-" (buffer-name buffer))
816 (string-match "^\\*MH-E " (buffer-name buffer)))
817 (kill-buffer buffer)))
818
819 (if mh-previous-window-config
820 (set-window-configuration mh-previous-window-config))
821 (run-hooks 'mh-quit-hook))
822
823 (defun mh-page-msg (&optional lines)
824 "Display next page in message.
825
826 You can give this command a prefix argument that specifies the number of LINES
827 to scroll. This command will also show the next undeleted message if it is
828 used at the bottom of a message."
829 (interactive "P")
830 (if mh-showing-mode
831 (if mh-page-to-next-msg-flag
832 (if (equal mh-next-direction 'backward)
833 (mh-previous-undeleted-msg)
834 (mh-next-undeleted-msg))
835 (if (mh-in-show-buffer (mh-show-buffer)
836 (pos-visible-in-window-p (point-max)))
837 (progn
838 (message
839 "End of message (Type %s to read %s undeleted message)"
840 (single-key-description last-input-event)
841 (if (equal mh-next-direction 'backward)
842 "previous"
843 "next"))
844 (setq mh-page-to-next-msg-flag t))
845 (scroll-other-window lines)))
846 (mh-show)))
847
848 (defun mh-previous-page (&optional lines)
849 "Display next page in message.
850
851 You can give this command a prefix argument that specifies the number of LINES
852 to scroll."
853 (interactive "P")
854 (mh-in-show-buffer (mh-show-buffer)
855 (scroll-down lines)))
856
857 (defun mh-previous-undeleted-msg (&optional count wait-after-complaining-flag)
858 "Display previous message.
859
860 This command can be given a prefix argument COUNT to specify how many unread
861 messages to skip.
862
863 In a program, pause for a second after printing message if we are at the last
864 undeleted message and optional argument WAIT-AFTER-COMPLAINING-FLAG is
865 non-nil."
866 (interactive "p")
867 (setq mh-next-direction 'backward)
868 (beginning-of-line)
869 (cond ((re-search-backward mh-scan-good-msg-regexp nil t count)
870 (mh-maybe-show))
871 (t (message "No previous undeleted message")
872 (if wait-after-complaining-flag (sit-for 1)))))
873
874 (defun mh-previous-unread-msg (&optional count)
875 "Display previous unread message.
876
877 This command can be given a prefix argument COUNT to specify how many unread
878 messages to skip."
879 (interactive "p")
880 (unless (> count 0)
881 (error "The function mh-previous-unread-msg expects positive argument"))
882 (setq count (1- count))
883 (let ((unread-sequence (cdr (assoc mh-unseen-seq mh-seq-list)))
884 (cur-msg (mh-get-msg-num nil)))
885 (cond ((and (not cur-msg) (not (bobp))
886 ;; If we are at the end of the buffer back up one line and go
887 ;; to unread message after that.
888 (progn
889 (forward-line -1)
890 (setq cur-msg (mh-get-msg-num nil)))
891 nil))
892 ((or (null unread-sequence) (not cur-msg))
893 ;; No unread message or there aren't any messages in buffer...
894 (message "No more unread messages"))
895 ((progn
896 ;; Skip count messages...
897 (while (and unread-sequence (>= (car unread-sequence) cur-msg))
898 (setq unread-sequence (cdr unread-sequence)))
899 (while (> count 0)
900 (setq unread-sequence (cdr unread-sequence))
901 (setq count (1- count)))
902 (not (car unread-sequence)))
903 (message "No more unread messages"))
904 (t (loop for msg in unread-sequence
905 when (mh-goto-msg msg t) return nil
906 finally (message "No more unread messages"))))))
907
908 (defun mh-goto-next-button (backward-flag &optional criterion)
909 "Search for next button satisfying criterion.
910 If BACKWARD-FLAG is non-nil search backward in the buffer for a mime button. If
911 CRITERION is a function or a symbol which has a function binding then that
912 function must return non-nil at the button we stop."
913 (unless (or (and (symbolp criterion) (fboundp criterion))
914 (functionp criterion))
915 (setq criterion (lambda (x) t)))
916 ;; Move to the next button in the buffer satisfying criterion
917 (goto-char (or (save-excursion
918 (beginning-of-line)
919 ;; Find point before current button
920 (let ((point-before-current-button
921 (save-excursion
922 (while (get-text-property (point) 'mh-data)
923 (unless (= (forward-line
924 (if backward-flag 1 -1))
925 0)
926 (if backward-flag
927 (goto-char (point-min))
928 (goto-char (point-max)))))
929 (point))))
930 ;; Skip over current button
931 (while (and (get-text-property (point) 'mh-data)
932 (not (if backward-flag (bobp) (eobp))))
933 (forward-line (if backward-flag -1 1)))
934 ;; Stop at next MIME button if any exists.
935 (block loop
936 (while (/= (progn
937 (unless (= (forward-line
938 (if backward-flag -1 1))
939 0)
940 (if backward-flag
941 (goto-char (point-max))
942 (goto-char (point-min)))
943 (beginning-of-line))
944 (point))
945 point-before-current-button)
946 (when (and (get-text-property (point) 'mh-data)
947 (funcall criterion (point)))
948 (return-from loop (point))))
949 nil)))
950 (point))))
951
952 (defun mh-next-button (&optional backward-flag)
953 "Go to the next button.
954
955 If the end of the buffer is reached then the search wraps over to the start of
956 the buffer.
957
958 If an optional prefix argument BACKWARD-FLAG is given, the cursor will move to
959 the previous button."
960 (interactive (list current-prefix-arg))
961 (unless mh-showing-mode
962 (mh-show))
963 (mh-in-show-buffer (mh-show-buffer)
964 (mh-goto-next-button backward-flag)))
965
966 (defun mh-prev-button ()
967 "Go to the previous button.
968
969 If the beginning of the buffer is reached then the search wraps over to the
970 end of the buffer."
971 (interactive)
972 (mh-next-button t))
973
974 (defun mh-folder-mime-action (part-index action include-security-flag)
975 "Go to PART-INDEX and carry out ACTION.
976 If PART-INDEX is nil then go to the next part in the buffer. The search for
977 the next buffer wraps around if end of buffer is reached. If argument
978 INCLUDE-SECURITY-FLAG is non-nil then include security info buttons when
979 searching for a suitable parts."
980 (unless mh-showing-mode
981 (mh-show))
982 (mh-in-show-buffer (mh-show-buffer)
983 (let ((criterion
984 (cond (part-index
985 (lambda (p)
986 (let ((part (get-text-property p 'mh-part)))
987 (and (integerp part) (= part part-index)))))
988 (t (lambda (p)
989 (if include-security-flag
990 (get-text-property p 'mh-data)
991 (integerp (get-text-property p 'mh-part)))))))
992 (point (point)))
993 (cond ((and (get-text-property point 'mh-part)
994 (or (null part-index)
995 (= (get-text-property point 'mh-part) part-index)))
996 (funcall action))
997 ((and (get-text-property point 'mh-data)
998 include-security-flag
999 (null part-index))
1000 (funcall action))
1001 (t
1002 (mh-goto-next-button nil criterion)
1003 (if (= (point) point)
1004 (message "No matching MIME part found")
1005 (funcall action)))))))
1006
1007 (defun mh-folder-toggle-mime-part (part-index)
1008 "View attachment.
1009
1010 This command displays (or hides) the attachment associated with the button
1011 under the cursor. If the cursor is not located over a button, then the cursor
1012 first moves to the next button, wrapping to the beginning of the message if
1013 necessary. This command has the advantage over related commands of working
1014 from the MH-Folder buffer.
1015
1016 You can also provide a numeric prefix argument PART-INDEX to view the
1017 attachment labeled with that number. If Emacs does not know how to display the
1018 attachment, then Emacs offers to save the attachment in a file."
1019 (interactive "P")
1020 (when (consp part-index) (setq part-index (car part-index)))
1021 (mh-folder-mime-action part-index #'mh-press-button t))
1022
1023 (defun mh-folder-inline-mime-part (part-index)
1024 "Show attachment verbatim.
1025
1026 You can view the raw contents of an attachment with this command. This command
1027 displays (or hides) the contents of the attachment associated with the button
1028 under the cursor verbatim. If the cursor is not located over a button, then
1029 the cursor first moves to the next button, wrapping to the beginning of the
1030 message if necessary.
1031
1032 You can also provide a numeric prefix argument PART-INDEX to view the
1033 attachment labeled with that number."
1034 (interactive "P")
1035 (when (consp part-index) (setq part-index (car part-index)))
1036 (mh-folder-mime-action part-index #'mh-mime-inline-part nil))
1037
1038 (defun mh-folder-save-mime-part (part-index)
1039 "Save (output) attachment.
1040
1041 This command saves the attachment associated with the button under the cursor.
1042 If the cursor is not located over a button, then the cursor first moves to the
1043 next button, wrapping to the beginning of the message if necessary.
1044
1045 You can also provide a numeric prefix argument PART-INDEX to save the
1046 attachment labeled with that number.
1047
1048 This command prompts you for a filename and suggests a specific name if it is
1049 available."
1050 (interactive "P")
1051 (when (consp part-index) (setq part-index (car part-index)))
1052 (mh-folder-mime-action part-index #'mh-mime-save-part nil))
1053
1054 (defvar mh-thread-scan-line-map-stack)
1055
1056 (defun mh-reset-threads-and-narrowing ()
1057 "Reset all variables pertaining to threads and narrowing.
1058 Also removes all content from the folder buffer."
1059 (setq mh-view-ops ())
1060 (setq mh-folder-view-stack ())
1061 (setq mh-thread-scan-line-map-stack ())
1062 (let ((buffer-read-only nil)) (erase-buffer)))
1063
1064 (defun mh-rescan-folder (&optional range dont-exec-pending)
1065 "Rescan folder\\<mh-folder-mode-map>.
1066
1067 This command is useful to grab all messages in your \"+inbox\" after
1068 processing your new mail for the first time. If you don't want to rescan the
1069 entire folder, this command will accept a RANGE. Check the documentation of
1070 `mh-interactive-range' to see how RANGE is read in interactive use.
1071
1072 This command will ask if you want to process refiles or deletes first and then
1073 either run \\[mh-execute-commands] for you or undo the pending refiles and
1074 deletes, which are lost.
1075
1076 In a program, the processing of outstanding commands is not performed if
1077 DONT-EXEC-PENDING is non-nil."
1078 (interactive (list (if current-prefix-arg
1079 (mh-read-range "Rescan" mh-current-folder t nil t
1080 mh-interpret-number-as-range-flag)
1081 nil)))
1082 (setq mh-next-direction 'forward)
1083 (let ((threaded-flag (memq 'unthread mh-view-ops)))
1084 (mh-scan-folder mh-current-folder (or range "all") dont-exec-pending)
1085 (cond (threaded-flag (mh-toggle-threads))
1086 (mh-index-data (mh-index-insert-folder-headers)))))
1087
1088 (defun mh-write-msg-to-file (message file no-header)
1089 "Append MESSAGE to end of FILE\\<mh-folder-mode-map>.
1090
1091 You are prompted for the filename. If the file already exists, the message is
1092 appended to it. You can also write the message to the file without the header
1093 by specifying a prefix argument NO-HEADER. Subsequent writes to the same file
1094 can be made with the command \\[mh-refile-or-write-again]."
1095 (interactive
1096 (list (mh-get-msg-num t)
1097 (let ((default-dir (if (eq 'write (car mh-last-destination-write))
1098 (file-name-directory
1099 (car (cdr mh-last-destination-write)))
1100 default-directory)))
1101 (read-file-name (format "Save message%s in file: "
1102 (if current-prefix-arg " body" ""))
1103 default-dir
1104 (if (eq 'write (car mh-last-destination-write))
1105 (car (cdr mh-last-destination-write))
1106 (expand-file-name "mail.out" default-dir))))
1107 current-prefix-arg))
1108 (let ((msg-file-to-output (mh-msg-filename message))
1109 (output-file (mh-expand-file-name file)))
1110 (setq mh-last-destination (list 'write file (if no-header 'no-header))
1111 mh-last-destination-write mh-last-destination)
1112 (save-excursion
1113 (set-buffer (get-buffer-create mh-temp-buffer))
1114 (erase-buffer)
1115 (insert-file-contents msg-file-to-output)
1116 (goto-char (point-min))
1117 (if no-header (search-forward "\n\n"))
1118 (append-to-file (point) (point-max) output-file))))
1119
1120 (defun mh-toggle-showing ()
1121 "Toggle the scanning mode/showing mode of displaying messages."
1122 (interactive)
1123 (if mh-showing-mode
1124 (mh-set-scan-mode)
1125 (mh-show)))
1126
1127 (defun mh-undo (range)
1128 "Undo pending deletes or refiles in RANGE.
1129
1130 If you've deleted a message or refiled it, but changed your mind, you can
1131 cancel the action before you've executed it. Use this command to undo a refile
1132 on or deletion of a single message. You can also undo refiles and deletes for
1133 messages that are found in a given RANGE.
1134
1135 Check the documentation of `mh-interactive-range' to see how RANGE is read in
1136 interactive use."
1137 (interactive (list (mh-interactive-range "Undo")))
1138 (cond ((numberp range)
1139 (let ((original-position (point)))
1140 (beginning-of-line)
1141 (while (not (or (looking-at mh-scan-deleted-msg-regexp)
1142 (looking-at mh-scan-refiled-msg-regexp)
1143 (and (eq mh-next-direction 'forward) (bobp))
1144 (and (eq mh-next-direction 'backward)
1145 (save-excursion (forward-line) (eobp)))))
1146 (forward-line (if (eq mh-next-direction 'forward) -1 1)))
1147 (if (or (looking-at mh-scan-deleted-msg-regexp)
1148 (looking-at mh-scan-refiled-msg-regexp))
1149 (progn
1150 (mh-undo-msg (mh-get-msg-num t))
1151 (mh-maybe-show))
1152 (goto-char original-position)
1153 (error "Nothing to undo"))))
1154 (t (mh-iterate-on-range () range
1155 (mh-undo-msg nil))))
1156 (if (not (mh-outstanding-commands-p))
1157 (mh-set-folder-modified-p nil)))
1158
1159
1160 (defun mh-folder-line-matches-show-buffer-p ()
1161 "Return t if the message under point in folder-mode is in the show buffer.
1162 Return nil in any other circumstance (no message under point, no show buffer,
1163 the message in the show buffer doesn't match."
1164 (and (eq major-mode 'mh-folder-mode)
1165 (mh-get-msg-num nil)
1166 mh-show-buffer
1167 (get-buffer mh-show-buffer)
1168 (buffer-file-name (get-buffer mh-show-buffer))
1169 (string-match ".*/\\([0-9]+\\)$"
1170 (buffer-file-name (get-buffer mh-show-buffer)))
1171 (string-equal
1172 (match-string 1 (buffer-file-name (get-buffer mh-show-buffer)))
1173 (int-to-string (mh-get-msg-num nil)))))
1174
1175 (eval-when-compile (require 'gnus))
1176
1177 (defmacro mh-macro-expansion-time-gnus-version ()
1178 "Return Gnus version available at macro expansion time.
1179 The macro evaluates the Gnus version at macro expansion time. If MH-E was
1180 compiled then macro expansion happens at compile time."
1181 gnus-version)
1182
1183 (defun mh-run-time-gnus-version ()
1184 "Return Gnus version available at run time."
1185 (require 'gnus)
1186 gnus-version)
1187
1188 ;;;###autoload
1189 (defun mh-version ()
1190 "Display version information about MH-E and the MH mail handling system."
1191 (interactive)
1192 (set-buffer (get-buffer-create mh-info-buffer))
1193 (erase-buffer)
1194 ;; MH-E version.
1195 (insert "MH-E " mh-version "\n\n")
1196 ;; MH-E compilation details.
1197 (insert "MH-E compilation details:\n")
1198 (let* ((compiled-mhe (byte-code-function-p (symbol-function 'mh-version)))
1199 (gnus-compiled-version (if compiled-mhe
1200 (mh-macro-expansion-time-gnus-version)
1201 "N/A")))
1202 (insert " Byte compiled:\t\t" (if compiled-mhe "yes" "no") "\n"
1203 " Gnus (compile-time):\t" gnus-compiled-version "\n"
1204 " Gnus (run-time):\t" (mh-run-time-gnus-version) "\n\n"))
1205 ;; Emacs version.
1206 (insert (emacs-version) "\n\n")
1207 ;; MH version.
1208 (if mh-variant-in-use
1209 (insert mh-variant-in-use "\n"
1210 " mh-progs:\t" mh-progs "\n"
1211 " mh-lib:\t" mh-lib "\n"
1212 " mh-lib-progs:\t" mh-lib-progs "\n\n")
1213 (insert "No MH variant detected\n"))
1214 ;; Linux version.
1215 (condition-case ()
1216 (call-process "uname" nil t nil "-a")
1217 (file-error))
1218 (goto-char (point-min))
1219 (display-buffer mh-info-buffer))
1220
1221 (defun mh-parse-flist-output-line (line &optional current-folder)
1222 "Parse LINE to generate folder name, unseen messages and total messages.
1223 If CURRENT-FOLDER is non-nil then it contains the current folder name and it is
1224 used to avoid problems in corner cases involving folders whose names end with a
1225 '+' character."
1226 (with-temp-buffer
1227 (insert line)
1228 (goto-char (point-max))
1229 (let (folder unseen total p)
1230 (when (search-backward " out of " (point-min) t)
1231 (setq total (read-from-string
1232 (buffer-substring-no-properties
1233 (match-end 0) (line-end-position))))
1234 (when (search-backward " in sequence " (point-min) t)
1235 (setq p (point))
1236 (when (search-backward " has " (point-min) t)
1237 (setq unseen (read-from-string (buffer-substring-no-properties
1238 (match-end 0) p)))
1239 (while (eq (char-after) ? )
1240 (backward-char))
1241 (setq folder (buffer-substring-no-properties
1242 (point-min) (1+ (point))))
1243 (when (and (equal (aref folder (1- (length folder))) ?+)
1244 (equal current-folder folder))
1245 (setq folder (substring folder 0 (1- (length folder)))))
1246 (values (format "+%s" folder) (car unseen) (car total))))))))
1247
1248 (defun mh-folder-size-folder (folder)
1249 "Find size of FOLDER using `folder'."
1250 (with-temp-buffer
1251 (let ((u (length (cdr (assoc mh-unseen-seq
1252 (mh-read-folder-sequences folder nil))))))
1253 (call-process (expand-file-name "folder" mh-progs) nil t nil
1254 "-norecurse" folder)
1255 (goto-char (point-min))
1256 (if (re-search-forward " has \\([0-9]+\\) " nil t)
1257 (values (car (read-from-string (match-string 1))) u folder)
1258 (values 0 u folder)))))
1259
1260 (defun mh-folder-size-flist (folder)
1261 "Find size of FOLDER using `flist'."
1262 (with-temp-buffer
1263 (call-process (expand-file-name "flist" mh-progs) nil t nil "-showzero"
1264 "-norecurse" folder "-sequence" (symbol-name mh-unseen-seq))
1265 (goto-char (point-min))
1266 (multiple-value-bind (folder unseen total)
1267 (mh-parse-flist-output-line
1268 (buffer-substring (point) (line-end-position)))
1269 (values total unseen folder))))
1270
1271 (defun mh-folder-size (folder)
1272 "Find size of FOLDER."
1273 (if mh-flists-present-flag
1274 (mh-folder-size-flist folder)
1275 (mh-folder-size-folder folder)))
1276
1277 (defun mh-visit-folder (folder &optional range index-data)
1278 "Visit FOLDER.
1279
1280 When you want to read the messages that you have refiled into folders, use
1281 this command to visit the folder. You are prompted for the folder name.
1282
1283 The folder buffer will show just unseen messages if there are any; otherwise,
1284 it will show all the messages in the buffer as long there are fewer than
1285 `mh-large-folder' messages. If there are more, then you are prompted for a
1286 range of messages to scan.
1287
1288 You can provide a prefix argument in order to specify a RANGE of messages to
1289 show when you visit the folder. In this case, regions are not used to specify
1290 the range and `mh-large-folder' is ignored. Check the documentation of
1291 `mh-interactive-range' to see how RANGE is read in interactive use.
1292
1293 Note that this command can also be used to create folders. If you specify a
1294 folder that does not exist, you will be prompted to create it.
1295
1296 Do not call this function from outside MH-E; use \\[mh-rmail] instead.
1297
1298 If, in a program, RANGE is nil (the default), then all messages in FOLDER are
1299 displayed. If an index buffer is being created then INDEX-DATA is used to
1300 initialize the index buffer specific data structures."
1301 (interactive (let ((folder-name (mh-prompt-for-folder "Visit" mh-inbox t)))
1302 (list folder-name
1303 (mh-read-range "Scan" folder-name t nil
1304 current-prefix-arg
1305 mh-interpret-number-as-range-flag))))
1306 (let ((config (current-window-configuration))
1307 (current-buffer (current-buffer))
1308 (threaded-view-flag mh-show-threads-flag))
1309 (delete-other-windows)
1310 (save-excursion
1311 (when (get-buffer folder)
1312 (set-buffer folder)
1313 (setq threaded-view-flag (memq 'unthread mh-view-ops))))
1314 (when index-data
1315 (mh-make-folder folder)
1316 (setq mh-index-data (car index-data)
1317 mh-index-msg-checksum-map (make-hash-table :test #'equal)
1318 mh-index-checksum-origin-map (make-hash-table :test #'equal))
1319 (mh-index-update-maps folder (cadr index-data))
1320 (mh-index-create-sequences))
1321 (mh-scan-folder folder (or range "all"))
1322 (cond ((and threaded-view-flag
1323 (save-excursion
1324 (goto-char (point-min))
1325 (or (null mh-large-folder)
1326 (not (equal (forward-line (1+ mh-large-folder)) 0))
1327 (and (message "Not threading since the number of messages exceeds `mh-large-folder'")
1328 nil))))
1329 (mh-toggle-threads))
1330 (mh-index-data
1331 (mh-index-insert-folder-headers)))
1332 (unless (eq current-buffer (current-buffer))
1333 (setq mh-previous-window-config config)))
1334 nil)
1335
1336
1337 (defun mh-update-sequences ()
1338 "Flush MH-E's state out to MH.
1339 This function updates the sequence specified by your \"Unseen-Sequence:\"
1340 profile component, \"cur\", and the sequence listed by the `mh-tick-seq'
1341 option which is \"tick\" by default. The message at the cursor is used for
1342 \"cur\"."
1343 (interactive)
1344 ;; mh-update-sequences is the opposite of mh-read-folder-sequences,
1345 ;; which updates MH-E's state from MH.
1346 (let ((folder-set (mh-update-unseen))
1347 (new-cur (mh-get-msg-num nil)))
1348 (if new-cur
1349 (let ((seq-entry (mh-find-seq 'cur)))
1350 (mh-remove-cur-notation)
1351 (setcdr seq-entry
1352 (list new-cur)) ;delete-seq-locally, add-msgs-to-seq
1353 (mh-define-sequence 'cur (list new-cur))
1354 (beginning-of-line)
1355 (if (looking-at mh-scan-good-msg-regexp)
1356 (mh-notate-cur)))
1357 (or folder-set
1358 (save-excursion
1359 ;; psg - mh-current-folder is nil if mh-summary-height < 4 !
1360 ;; So I added this sanity check.
1361 (if (stringp mh-current-folder)
1362 (mh-exec-cmd-quiet t "folder" mh-current-folder "-fast")
1363 (mh-exec-cmd-quiet t "folder" "-fast")))))))
1364
1365 \f
1366
1367 ;;; Support routines.
1368
1369 (defun mh-delete-a-msg (msg)
1370 "Delete the MSG.
1371 If MSG is nil then the message at point is deleted.
1372
1373 The value of `mh-delete-msg-hook' is a list of functions to be called, with no
1374 arguments, after the message has been deleted."
1375 (save-excursion
1376 (if (numberp msg)
1377 (mh-goto-msg msg nil t)
1378 (beginning-of-line)
1379 (setq msg (mh-get-msg-num t)))
1380 (if (looking-at mh-scan-refiled-msg-regexp)
1381 (error "Message %d is refiled. Undo refile before deleting" msg))
1382 (if (looking-at mh-scan-deleted-msg-regexp)
1383 nil
1384 (mh-set-folder-modified-p t)
1385 (setq mh-delete-list (cons msg mh-delete-list))
1386 (mh-notate nil mh-note-deleted mh-cmd-note)
1387 (run-hooks 'mh-delete-msg-hook))))
1388
1389 (defun mh-refile-a-msg (msg folder)
1390 "Refile MSG in FOLDER.
1391 If MSG is nil then the message at point is refiled.
1392
1393 Folder is a symbol, not a string.
1394 The value of `mh-refile-msg-hook' is a list of functions to be called, with no
1395 arguments, after the message has been refiled."
1396 (save-excursion
1397 (if (numberp msg)
1398 (mh-goto-msg msg nil t)
1399 (beginning-of-line)
1400 (setq msg (mh-get-msg-num t)))
1401 (cond ((looking-at mh-scan-deleted-msg-regexp)
1402 (error "Message %d is deleted. Undo delete before moving" msg))
1403 ((looking-at mh-scan-refiled-msg-regexp)
1404 (if (y-or-n-p
1405 (format "Message %d already refiled. Copy to %s as well? "
1406 msg folder))
1407 (mh-exec-cmd "refile" (mh-get-msg-num t) "-link"
1408 "-src" mh-current-folder
1409 (symbol-name folder))
1410 (message "Message not copied")))
1411 (t
1412 (mh-set-folder-modified-p t)
1413 (cond ((null (assoc folder mh-refile-list))
1414 (push (list folder msg) mh-refile-list))
1415 ((not (member msg (cdr (assoc folder mh-refile-list))))
1416 (push msg (cdr (assoc folder mh-refile-list)))))
1417 (mh-notate nil mh-note-refiled mh-cmd-note)
1418 (run-hooks 'mh-refile-msg-hook)))))
1419
1420 (defun mh-next-msg (&optional wait-after-complaining-flag)
1421 "Move backward or forward to the next undeleted message in the buffer.
1422 If optional argument WAIT-AFTER-COMPLAINING-FLAG is non-nil and we are at the
1423 last message, then wait for a second after telling the user that there aren't
1424 any more unread messages."
1425 (if (eq mh-next-direction 'forward)
1426 (mh-next-undeleted-msg 1 wait-after-complaining-flag)
1427 (mh-previous-undeleted-msg 1 wait-after-complaining-flag)))
1428
1429 (defun mh-next-unread-msg (&optional count)
1430 "Display next unread message.
1431
1432 This command can be given a prefix argument COUNT to specify how many unread
1433 messages to skip."
1434 (interactive "p")
1435 (unless (> count 0)
1436 (error "The function mh-next-unread-msg expects positive argument"))
1437 (setq count (1- count))
1438 (let ((unread-sequence (reverse (cdr (assoc mh-unseen-seq mh-seq-list))))
1439 (cur-msg (mh-get-msg-num nil)))
1440 (cond ((and (not cur-msg) (not (bobp))
1441 ;; If we are at the end of the buffer back up one line and go
1442 ;; to unread message after that.
1443 (progn
1444 (forward-line -1)
1445 (setq cur-msg (mh-get-msg-num nil)))
1446 nil))
1447 ((or (null unread-sequence) (not cur-msg))
1448 ;; No unread message or there aren't any messages in buffer...
1449 (message "No more unread messages"))
1450 ((progn
1451 ;; Skip messages
1452 (while (and unread-sequence (>= cur-msg (car unread-sequence)))
1453 (setq unread-sequence (cdr unread-sequence)))
1454 (while (> count 0)
1455 (setq unread-sequence (cdr unread-sequence))
1456 (setq count (1- count)))
1457 (not (car unread-sequence)))
1458 (message "No more unread messages"))
1459 (t (loop for msg in unread-sequence
1460 when (mh-goto-msg msg t) return nil
1461 finally (message "No more unread messages"))))))
1462
1463 (defun mh-set-scan-mode ()
1464 "Display the scan listing buffer, but do not show a message."
1465 (if (get-buffer mh-show-buffer)
1466 (delete-windows-on mh-show-buffer))
1467 (mh-showing-mode 0)
1468 (force-mode-line-update)
1469 (if mh-recenter-summary-flag
1470 (mh-recenter nil)))
1471
1472 (defun mh-undo-msg (msg)
1473 "Undo the deletion or refile of one MSG.
1474 If MSG is nil then act on the message at point"
1475 (save-excursion
1476 (if (numberp msg)
1477 (mh-goto-msg msg t t)
1478 (beginning-of-line)
1479 (setq msg (mh-get-msg-num t)))
1480 (cond ((memq msg mh-delete-list)
1481 (setq mh-delete-list (delq msg mh-delete-list)))
1482 (t
1483 (dolist (folder-msg-list mh-refile-list)
1484 (setf (cdr folder-msg-list) (remove msg (cdr folder-msg-list))))
1485 (setq mh-refile-list (loop for x in mh-refile-list
1486 unless (null (cdr x)) collect x))))
1487 (mh-notate nil ? mh-cmd-note)))
1488
1489 \f
1490
1491 ;;; The folder data abstraction.
1492
1493 (defvar mh-index-data-file ".mhe_index"
1494 "MH-E specific file where index seach info is stored.")
1495
1496 (defun mh-make-folder (name)
1497 "Create a new mail folder called NAME.
1498 Make it the current folder."
1499 (switch-to-buffer name)
1500 (setq buffer-read-only nil)
1501 (erase-buffer)
1502 (if mh-adaptive-cmd-note-flag
1503 (mh-set-cmd-note (mh-msg-num-width-to-column (mh-msg-num-width name))))
1504 (setq buffer-read-only t)
1505 (mh-folder-mode)
1506 (mh-set-folder-modified-p nil)
1507 (setq buffer-file-name mh-folder-filename)
1508 (when (and (not mh-index-data)
1509 (file-exists-p (concat buffer-file-name mh-index-data-file)))
1510 (mh-index-read-data))
1511 (mh-make-folder-mode-line))
1512
1513 ;; Ensure new buffers won't get this mode if default-major-mode is nil.
1514 (put 'mh-folder-mode 'mode-class 'special)
1515
1516 \f
1517
1518 ;;; Build mh-folder-mode menu
1519
1520 ;; Menu extracted from mh-menubar.el V1.1 (31 July 2001)
1521 ;; Menus for folder mode: folder, message, sequence (in that order)
1522 ;; folder-mode "Sequence" menu
1523 (easy-menu-define
1524 mh-folder-sequence-menu mh-folder-mode-map "Menu for MH-E folder-sequence."
1525 '("Sequence"
1526 ["Add Message to Sequence..." mh-put-msg-in-seq (mh-get-msg-num nil)]
1527 ["List Sequences for Message" mh-msg-is-in-seq (mh-get-msg-num nil)]
1528 ["Delete Message from Sequence..." mh-delete-msg-from-seq
1529 (mh-get-msg-num nil)]
1530 ["List Sequences in Folder..." mh-list-sequences t]
1531 ["Delete Sequence..." mh-delete-seq t]
1532 ["Narrow to Sequence..." mh-narrow-to-seq t]
1533 ["Widen from Sequence" mh-widen mh-folder-view-stack]
1534 "--"
1535 ["Narrow to Subject Sequence" mh-narrow-to-subject t]
1536 ["Narrow to Tick Sequence" mh-narrow-to-tick
1537 (and mh-tick-seq (mh-seq-msgs (mh-find-seq mh-tick-seq)))]
1538 ["Delete Rest of Same Subject" mh-delete-subject t]
1539 ["Toggle Tick Mark" mh-toggle-tick t]
1540 "--"
1541 ["Push State Out to MH" mh-update-sequences t]))
1542
1543 ;; folder-mode "Message" menu
1544 (easy-menu-define
1545 mh-folder-message-menu mh-folder-mode-map "Menu for MH-E folder-message."
1546 '("Message"
1547 ["Show Message" mh-show (mh-get-msg-num nil)]
1548 ["Show Message with Header" mh-header-display (mh-get-msg-num nil)]
1549 ["Next Message" mh-next-undeleted-msg t]
1550 ["Previous Message" mh-previous-undeleted-msg t]
1551 ["Go to First Message" mh-first-msg t]
1552 ["Go to Last Message" mh-last-msg t]
1553 ["Go to Message by Number..." mh-goto-msg t]
1554 ["Modify Message" mh-modify t]
1555 ["Delete Message" mh-delete-msg (mh-get-msg-num nil)]
1556 ["Refile Message" mh-refile-msg (mh-get-msg-num nil)]
1557 ["Undo Delete/Refile" mh-undo (mh-outstanding-commands-p)]
1558 ["Execute Delete/Refile" mh-execute-commands
1559 (mh-outstanding-commands-p)]
1560 "--"
1561 ["Compose a New Message" mh-send t]
1562 ["Reply to Message..." mh-reply (mh-get-msg-num nil)]
1563 ["Forward Message..." mh-forward (mh-get-msg-num nil)]
1564 ["Redistribute Message..." mh-redistribute (mh-get-msg-num nil)]
1565 ["Edit Message Again" mh-edit-again (mh-get-msg-num nil)]
1566 ["Re-edit a Bounced Message" mh-extract-rejected-mail t]
1567 "--"
1568 ["Copy Message to Folder..." mh-copy-msg (mh-get-msg-num nil)]
1569 ["Print Message" mh-print-msg (mh-get-msg-num nil)]
1570 ["Write Message to File..." mh-write-msg-to-file
1571 (mh-get-msg-num nil)]
1572 ["Pipe Message to Command..." mh-pipe-msg (mh-get-msg-num nil)]
1573 ["Unpack Uuencoded Message..." mh-store-msg (mh-get-msg-num nil)]
1574 ["Burst Digest Message" mh-burst-digest (mh-get-msg-num nil)]))
1575
1576 ;; folder-mode "Folder" menu
1577 (easy-menu-define
1578 mh-folder-folder-menu mh-folder-mode-map "Menu for MH-E folder."
1579 '("Folder"
1580 ["Incorporate New Mail" mh-inc-folder t]
1581 ["Toggle Show/Folder" mh-toggle-showing t]
1582 ["Execute Delete/Refile" mh-execute-commands
1583 (mh-outstanding-commands-p)]
1584 ["Rescan Folder" mh-rescan-folder t]
1585 ["Thread Folder" mh-toggle-threads
1586 (not (memq 'unthread mh-view-ops))]
1587 ["Pack Folder" mh-pack-folder t]
1588 ["Sort Folder" mh-sort-folder t]
1589 "--"
1590 ["List Folders" mh-list-folders t]
1591 ["Visit a Folder..." mh-visit-folder t]
1592 ["View New Messages" mh-index-new-messages t]
1593 ["Search a Folder..." mh-search-folder t]
1594 ["Indexed Search..." mh-index-search t]
1595 "--"
1596 ["Quit MH-E" mh-quit t]))
1597
1598 \f
1599
1600 (defmacro mh-remove-xemacs-horizontal-scrollbar ()
1601 "Get rid of the horizontal scrollbar that XEmacs insists on putting in."
1602 (when mh-xemacs-flag
1603 `(if (and (featurep 'scrollbar)
1604 (fboundp 'set-specifier))
1605 (set-specifier horizontal-scrollbar-visible-p nil
1606 (cons (current-buffer) nil)))))
1607
1608 (defmacro mh-write-file-functions-compat ()
1609 "Return `write-file-functions' if it exists.
1610 Otherwise return `local-write-file-hooks'. This macro exists purely for
1611 compatibility. The former symbol is used in Emacs 21.4 onward while the latter
1612 is used in previous versions and XEmacs."
1613 (if (boundp 'write-file-functions)
1614 ''write-file-functions ;Emacs 21.4
1615 ''local-write-file-hooks)) ;<Emacs 21.4, XEmacs
1616
1617 ;; Avoid compiler warnings in non-bleeding edge versions of Emacs.
1618 (eval-when-compile
1619 (defvar tool-bar-mode)
1620 (defvar tool-bar-map)
1621 (defvar desktop-save-buffer)) ;Emacs 21.4
1622
1623 ;; Register mh-folder-mode as supporting which-function-mode...
1624 (load "which-func" t t)
1625 (when (and (boundp 'which-func-modes)
1626 (not (member 'mh-folder-mode which-func-modes)))
1627 (push 'mh-folder-mode which-func-modes))
1628
1629 (defvar mh-folder-buttons-init-flag nil)
1630
1631 ;; Autoload cookie needed by desktop.el
1632 ;;;###autoload
1633 (define-derived-mode mh-folder-mode fundamental-mode "MH-Folder"
1634 "Major MH-E mode for \"editing\" an MH folder scan listing.\\<mh-folder-mode-map>
1635
1636 You can show the message the cursor is pointing to, and step through the
1637 messages. Messages can be marked for deletion or refiling into another
1638 folder; these commands are executed all at once with a separate command.
1639
1640 Options that control this mode can be changed with \\[customize-group];
1641 specify the \"mh\" group. In particular, please see the `mh-scan-format-file'
1642 option if you wish to modify scan's format.
1643
1644 When a folder is visited, the hook `mh-folder-mode-hook' is run.
1645
1646 Ranges
1647 ======
1648 Many commands that operate on individual messages, such as `mh-forward' or
1649 `mh-refile-msg' take a RANGE argument. This argument can be used in several
1650 ways.
1651
1652 If you provide the prefix argument (\\[universal-argument]) to these commands,
1653 then you will be prompted for the message range. This can be any valid MH
1654 range which can include messages, sequences, and the abbreviations (described
1655 in the mh(1) man page):
1656
1657 <num1>-<num2>
1658 Indicates all messages in the range <num1> to <num2>, inclusive. The range
1659 must be nonempty.
1660
1661 `<num>:N'
1662 `<num>:+N'
1663 `<num>:-N'
1664 Up to N messages beginning with (or ending with) message num. Num may be
1665 any of the predefined symbols: first, prev, cur, next or last.
1666
1667 `first:N'
1668 `prev:N'
1669 `next:N'
1670 `last:N'
1671 The first, previous, next or last messages, if they exist.
1672
1673 `all'
1674 All of the messages.
1675
1676 For example, a range that shows all of these things is `1 2 3 5-10 last:5
1677 unseen'.
1678
1679 If the option `transient-mark-mode' is set to t and you set a region in the
1680 MH-Folder buffer, then the MH-E command will perform the operation on all
1681 messages in that region.
1682
1683 \\{mh-folder-mode-map}"
1684 (mh-do-in-gnu-emacs
1685 (unless mh-folder-buttons-init-flag
1686 (mh-tool-bar-folder-buttons-init)
1687 (setq mh-folder-buttons-init-flag t)))
1688 (make-local-variable 'font-lock-defaults)
1689 (setq font-lock-defaults '(mh-folder-font-lock-keywords t))
1690 (make-local-variable 'desktop-save-buffer)
1691 (setq desktop-save-buffer t)
1692 (mh-make-local-vars
1693 'mh-colors-available-flag (mh-colors-available-p)
1694 ; Do we have colors available
1695 'mh-current-folder (buffer-name) ; Name of folder, a string
1696 'mh-show-buffer (format "show-%s" (buffer-name)) ; Buffer that displays msgs
1697 'mh-folder-filename ; e.g. "/usr/foobar/Mail/inbox/"
1698 (file-name-as-directory (mh-expand-file-name (buffer-name)))
1699 'mh-display-buttons-for-inline-parts-flag
1700 mh-display-buttons-for-inline-parts-flag ; Allow for display of buttons to
1701 ; be toggled.
1702 'mh-arrow-marker (make-marker) ; Marker where arrow is displayed
1703 'overlay-arrow-position nil ; Allow for simultaneous display in
1704 'overlay-arrow-string ">" ; different MH-E buffers.
1705 'mh-showing-mode nil ; Show message also?
1706 'mh-delete-list nil ; List of msgs nums to delete
1707 'mh-refile-list nil ; List of folder names in mh-seq-list
1708 'mh-seq-list nil ; Alist of (seq . msgs) nums
1709 'mh-seen-list nil ; List of displayed messages
1710 'mh-next-direction 'forward ; Direction to move to next message
1711 'mh-view-ops () ; Stack that keeps track of the order
1712 ; in which narrowing/threading has been
1713 ; carried out.
1714 'mh-folder-view-stack () ; Stack of previous views of the
1715 ; folder.
1716 'mh-index-data nil ; If the folder was created by a call
1717 ; to mh-index-search this contains info
1718 ; about the search results.
1719 'mh-index-previous-search nil ; Previous folder and search-regexp
1720 'mh-index-msg-checksum-map nil ; msg -> checksum map
1721 'mh-index-checksum-origin-map nil ; checksum -> ( orig-folder, orig-msg )
1722 'mh-index-sequence-search-flag nil ; folder resulted from sequence search
1723 'mh-first-msg-num nil ; Number of first msg in buffer
1724 'mh-last-msg-num nil ; Number of last msg in buffer
1725 'mh-msg-count nil ; Number of msgs in buffer
1726 'mh-mode-line-annotation nil ; Indicates message range
1727 'mh-sequence-notation-history (make-hash-table)
1728 ; Remember what is overwritten by
1729 ; mh-note-seq.
1730 'imenu-create-index-function 'mh-index-create-imenu-index
1731 ; Setup imenu support
1732 'mh-previous-window-config nil) ; Previous window configuration
1733 (mh-remove-xemacs-horizontal-scrollbar)
1734 (setq truncate-lines t)
1735 (auto-save-mode -1)
1736 (setq buffer-offer-save t)
1737 (mh-make-local-hook (mh-write-file-functions-compat))
1738 (add-hook (mh-write-file-functions-compat) 'mh-execute-commands nil t)
1739 (make-local-variable 'revert-buffer-function)
1740 (make-local-variable 'hl-line-mode) ; avoid pollution
1741 (mh-funcall-if-exists hl-line-mode 1)
1742 (setq revert-buffer-function 'mh-undo-folder)
1743 (or (assq 'mh-showing-mode minor-mode-alist)
1744 (setq minor-mode-alist
1745 (cons '(mh-showing-mode " Show") minor-mode-alist)))
1746 (easy-menu-add mh-folder-sequence-menu)
1747 (easy-menu-add mh-folder-message-menu)
1748 (easy-menu-add mh-folder-folder-menu)
1749 (set (make-local-variable 'tool-bar-map) mh-folder-tool-bar-map)
1750 (mh-funcall-if-exists mh-tool-bar-init :folder)
1751 (if (and mh-xemacs-flag
1752 font-lock-auto-fontify)
1753 (turn-on-font-lock))) ; Force font-lock in XEmacs.
1754
1755 (defun mh-toggle-mime-buttons ()
1756 "Toggle option `mh-display-buttons-for-inline-parts-flag'."
1757 (interactive)
1758 (setq mh-display-buttons-for-inline-parts-flag
1759 (not mh-display-buttons-for-inline-parts-flag))
1760 (mh-show nil t))
1761
1762 (defun mh-colors-available-p ()
1763 "Check if colors are available in the Emacs being used."
1764 (or mh-xemacs-flag
1765 (let ((color-cells
1766 (or (ignore-errors (mh-funcall-if-exists display-color-cells))
1767 (ignore-errors (mh-funcall-if-exists
1768 x-display-color-cells)))))
1769 (and (numberp color-cells) (>= color-cells 8)))))
1770
1771 (defun mh-colors-in-use-p ()
1772 "Check if colors are being used in the folder buffer."
1773 (and mh-colors-available-flag font-lock-mode))
1774
1775 (defun mh-make-local-vars (&rest pairs)
1776 "Initialize local variables according to the variable-value PAIRS."
1777
1778 (while pairs
1779 (set (make-local-variable (car pairs)) (car (cdr pairs)))
1780 (setq pairs (cdr (cdr pairs)))))
1781
1782 (defun mh-restore-desktop-buffer (desktop-buffer-file-name
1783 desktop-buffer-name
1784 desktop-buffer-misc)
1785 "Restore an MH folder buffer specified in a desktop file.
1786 When desktop creates a buffer, DESKTOP-BUFFER-FILE-NAME holds the file name to
1787 visit, DESKTOP-BUFFER-NAME holds the desired buffer name, and
1788 DESKTOP-BUFFER-MISC holds a list of miscellaneous info used by the
1789 `desktop-buffer-handlers' functions."
1790 (mh-find-path)
1791 (mh-visit-folder desktop-buffer-name)
1792 (current-buffer))
1793
1794 ;; desktop-buffer-mode-handlers appeared in Emacs 22.
1795 (if (fboundp 'desktop-buffer-mode-handlers)
1796 (add-to-list 'desktop-buffer-mode-handlers
1797 '(mh-folder-mode . mh-restore-desktop-buffer)))
1798
1799 (defun mh-scan-folder (folder range &optional dont-exec-pending)
1800 "Scan FOLDER over RANGE.
1801
1802 After the scan is performed, switch to the buffer associated with FOLDER.
1803
1804 Check the documentation of `mh-interactive-range' to see how RANGE is read in
1805 interactive use.
1806
1807 The processing of outstanding commands is not performed if DONT-EXEC-PENDING
1808 is non-nil."
1809 (when (stringp range)
1810 (setq range (delete "" (split-string range "[ \t\n]"))))
1811 (cond ((null (get-buffer folder))
1812 (mh-make-folder folder))
1813 (t
1814 (unless dont-exec-pending
1815 (mh-process-or-undo-commands folder)
1816 (mh-reset-threads-and-narrowing))
1817 (switch-to-buffer folder)))
1818 (mh-regenerate-headers range)
1819 (if (zerop (buffer-size))
1820 (if (equal range "all")
1821 (message "Folder %s is empty" folder)
1822 (message "No messages in %s, range %s" folder range))
1823 (mh-goto-cur-msg))
1824 (when (mh-outstanding-commands-p)
1825 (mh-notate-deleted-and-refiled)))
1826
1827 (defun mh-msg-num-width-to-column (width)
1828 "Return the column for notations given message number WIDTH.
1829 Note that columns in Emacs start with 0.
1830
1831 If `mh-scan-format-file' is set to \"Use MH-E scan Format\" this means that
1832 either `mh-scan-format-mh' or `mh-scan-format-nmh' are in use. This function
1833 therefore assumes that the first column is empty (to provide room for the
1834 cursor), the following WIDTH columns contain the message number, and the
1835 column for notations comes after that."
1836 (if (eq mh-scan-format-file t)
1837 (max (1+ width) 2)
1838 (error "%s %s" "Can't call mh-msg-num-width-to-column"
1839 "when mh-scan-format-file is not t")))
1840
1841 (defun mh-set-cmd-note (column)
1842 "Set `mh-cmd-note' to COLUMN.
1843 Note that columns in Emacs start with 0."
1844 (setq mh-cmd-note column))
1845
1846 (defun mh-regenerate-headers (range &optional update)
1847 "Scan folder over RANGE.
1848 If UPDATE, append the scan lines, otherwise replace."
1849 (let ((folder mh-current-folder)
1850 (range (if (and range (atom range)) (list range) range))
1851 scan-start)
1852 (message "Scanning %s..." folder)
1853 (mh-remove-all-notation)
1854 (with-mh-folder-updating (nil)
1855 (if update
1856 (goto-char (point-max))
1857 (delete-region (point-min) (point-max))
1858 (if mh-adaptive-cmd-note-flag
1859 (mh-set-cmd-note (mh-msg-num-width-to-column (mh-msg-num-width
1860 folder)))))
1861 (setq scan-start (point))
1862 (apply #'mh-exec-cmd-output
1863 mh-scan-prog nil
1864 (mh-scan-format)
1865 "-noclear" "-noheader"
1866 "-width" (window-width)
1867 folder range)
1868 (goto-char scan-start)
1869 (cond ((looking-at "scan: no messages in")
1870 (keep-lines mh-scan-valid-regexp)) ; Flush random scan lines
1871 ((looking-at (if (mh-variant-p 'mu-mh)
1872 "scan: message set .* does not exist"
1873 "scan: bad message list "))
1874 (keep-lines mh-scan-valid-regexp))
1875 ((looking-at "scan: ")) ; Keep error messages
1876 (t
1877 (keep-lines mh-scan-valid-regexp))) ; Flush random scan lines
1878 (setq mh-seq-list (mh-read-folder-sequences folder nil))
1879 (mh-notate-user-sequences)
1880 (or update
1881 (setq mh-mode-line-annotation
1882 (if (equal range '("all"))
1883 nil
1884 mh-partial-folder-mode-line-annotation)))
1885 (mh-make-folder-mode-line))
1886 (message "Scanning %s...done" folder)))
1887
1888 (defun mh-generate-new-cmd-note (folder)
1889 "Fix the `mh-cmd-note' value for this FOLDER.
1890
1891 After doing an `mh-get-new-mail' operation in this FOLDER, at least
1892 one line that looks like a truncated message number was found.
1893
1894 Remove the text added by the last `mh-inc' command. It should be the messages
1895 cur-last. Call `mh-set-cmd-note', adjusting the notation column with the width
1896 of the largest message number in FOLDER.
1897
1898 Reformat the message number width on each line in the buffer and trim
1899 the line length to fit in the window.
1900
1901 Rescan the FOLDER in the range cur-last in order to display the
1902 messages that were removed earlier. They should all fit in the scan
1903 line now with no message truncation."
1904 (save-excursion
1905 (let ((maxcol (1- (window-width)))
1906 (old-cmd-note mh-cmd-note)
1907 mh-cmd-note-fmt
1908 msgnum)
1909 ;; Nuke all of the lines just added by the last inc
1910 (delete-char (- (point-max) (point)))
1911 ;; Update the current buffer to reflect the new mh-cmd-note
1912 ;; value needed to display messages.
1913 (mh-set-cmd-note (mh-msg-num-width-to-column (mh-msg-num-width folder)))
1914 (setq mh-cmd-note-fmt (concat "%" (format "%d" mh-cmd-note) "d"))
1915 ;; Cleanup the messages that are in the buffer right now
1916 (goto-char (point-min))
1917 (cond ((memq 'unthread mh-view-ops)
1918 (mh-thread-add-spaces (- mh-cmd-note old-cmd-note)))
1919 (t (while (re-search-forward mh-scan-msg-number-regexp nil 0 1)
1920 ;; reformat the number to fix in mh-cmd-note columns
1921 (setq msgnum (string-to-number
1922 (buffer-substring
1923 (match-beginning 1) (match-end 1))))
1924 (replace-match (format mh-cmd-note-fmt msgnum))
1925 ;; trim the line to fix in the window
1926 (end-of-line)
1927 (let ((eol (point)))
1928 (move-to-column maxcol)
1929 (if (<= (point) eol)
1930 (delete-char (- eol (point))))))))
1931 ;; now re-read the lost messages
1932 (goto-char (point-max))
1933 (prog1 (point)
1934 (mh-regenerate-headers "cur-last" t)))))
1935
1936 (defun mh-get-new-mail (maildrop-name)
1937 "Read new mail from MAILDROP-NAME into the current buffer.
1938 Return in the current buffer."
1939 (let ((point-before-inc (point))
1940 (folder mh-current-folder)
1941 (new-mail-flag nil))
1942 (with-mh-folder-updating (t)
1943 (if maildrop-name
1944 (message "inc %s -file %s..." folder maildrop-name)
1945 (message "inc %s..." folder))
1946 (setq mh-next-direction 'forward)
1947 (goto-char (point-max))
1948 (mh-remove-cur-notation)
1949 (let ((start-of-inc (point)))
1950 (if maildrop-name
1951 ;; I think MH 5 used "-ms-file" instead of "-file",
1952 ;; which would make inc'ing from maildrops fail.
1953 (mh-exec-cmd-output mh-inc-prog nil folder
1954 (mh-scan-format)
1955 "-file" (expand-file-name maildrop-name)
1956 "-width" (window-width)
1957 "-truncate")
1958 (mh-exec-cmd-output mh-inc-prog nil
1959 (mh-scan-format)
1960 "-width" (window-width)))
1961 (if maildrop-name
1962 (message "inc %s -file %s...done" folder maildrop-name)
1963 (message "inc %s...done" folder))
1964 (goto-char start-of-inc)
1965 (cond ((save-excursion
1966 (re-search-forward "^inc: no mail" nil t))
1967 (message "No new mail%s%s" (if maildrop-name " in " "")
1968 (if maildrop-name maildrop-name "")))
1969 ((and (when mh-folder-view-stack
1970 (let ((saved-text (buffer-substring-no-properties
1971 start-of-inc (point-max))))
1972 (delete-region start-of-inc (point-max))
1973 (unwind-protect (mh-widen t)
1974 (mh-remove-cur-notation)
1975 (goto-char (point-max))
1976 (setq start-of-inc (point))
1977 (insert saved-text)
1978 (goto-char start-of-inc))))
1979 nil))
1980 ((re-search-forward "^inc:" nil t) ; Error messages
1981 (error "Error incorporating mail"))
1982 ((and
1983 (equal mh-scan-format-file t)
1984 mh-adaptive-cmd-note-flag
1985 ;; Have we reached an edge condition?
1986 (save-excursion
1987 (re-search-forward mh-scan-msg-overflow-regexp nil 0 1))
1988 (setq start-of-inc (mh-generate-new-cmd-note folder))
1989 nil))
1990 (t
1991 (setq new-mail-flag t)))
1992 (keep-lines mh-scan-valid-regexp) ; Flush random scan lines
1993 (let* ((sequences (mh-read-folder-sequences folder t))
1994 (new-cur (assoc 'cur sequences))
1995 (new-unseen (assoc mh-unseen-seq sequences)))
1996 (unless (assoc 'cur mh-seq-list)
1997 (push (list 'cur) mh-seq-list))
1998 (unless (assoc mh-unseen-seq mh-seq-list)
1999 (push (list mh-unseen-seq) mh-seq-list))
2000 (setcdr (assoc 'cur mh-seq-list) (cdr new-cur))
2001 (setcdr (assoc mh-unseen-seq mh-seq-list) (cdr new-unseen)))
2002 (when (equal (point-max) start-of-inc)
2003 (mh-notate-cur))
2004 (if new-mail-flag
2005 (progn
2006 (mh-make-folder-mode-line)
2007 (when (mh-speed-flists-active-p)
2008 (mh-speed-flists t mh-current-folder))
2009 (when (memq 'unthread mh-view-ops)
2010 (mh-thread-inc folder start-of-inc))
2011 (mh-goto-cur-msg))
2012 (goto-char point-before-inc))
2013 (mh-notate-user-sequences (cons start-of-inc (point-max)))))))
2014
2015 (defun mh-make-folder-mode-line (&optional ignored)
2016 "Set the fields of the mode line for a folder buffer.
2017 The optional argument is now obsolete and IGNORED. It used to be used to pass
2018 in what is now stored in the buffer-local variable `mh-mode-line-annotation'."
2019 (save-excursion
2020 (save-window-excursion
2021 (mh-first-msg)
2022 (let ((new-first-msg-num (mh-get-msg-num nil)))
2023 (when (or (not (memq 'unthread mh-view-ops))
2024 (null mh-first-msg-num)
2025 (null new-first-msg-num)
2026 (< new-first-msg-num mh-first-msg-num))
2027 (setq mh-first-msg-num new-first-msg-num)))
2028 (mh-last-msg)
2029 (let ((new-last-msg-num (mh-get-msg-num nil)))
2030 (when (or (not (memq 'unthread mh-view-ops))
2031 (null mh-last-msg-num)
2032 (null new-last-msg-num)
2033 (> new-last-msg-num mh-last-msg-num))
2034 (setq mh-last-msg-num new-last-msg-num)))
2035 (setq mh-msg-count (if mh-first-msg-num
2036 (count-lines (point-min) (point-max))
2037 0))
2038 (setq mode-line-buffer-identification
2039 (list (format " {%%b%s} %s msg%s"
2040 (if mh-mode-line-annotation
2041 (format "/%s" mh-mode-line-annotation)
2042 "")
2043 (if (zerop mh-msg-count)
2044 "no"
2045 (format "%d" mh-msg-count))
2046 (if (zerop mh-msg-count)
2047 "s"
2048 (cond ((> mh-msg-count 1)
2049 (format "s (%d-%d)" mh-first-msg-num
2050 mh-last-msg-num))
2051 (mh-first-msg-num
2052 (format " (%d)" mh-first-msg-num))
2053 (""))))))
2054 (mh-logo-display))))
2055
2056 (defun mh-add-sequence-notation (msg internal-seq-flag)
2057 "Add sequence notation to the MSG on the current line.
2058 If INTERNAL-SEQ-FLAG is non-nil, then refontify the scan line if font-lock is
2059 turned on."
2060 (with-mh-folder-updating (t)
2061 (save-excursion
2062 (beginning-of-line)
2063 (if internal-seq-flag
2064 (progn
2065 ;; Change the buffer so that if transient-mark-mode is active
2066 ;; and there is an active region it will get deactivated as in
2067 ;; the case of user sequences.
2068 (mh-notate nil nil mh-cmd-note)
2069 (when font-lock-mode
2070 (font-lock-fontify-region (point) (line-end-position))))
2071 (forward-char (+ mh-cmd-note mh-scan-field-destination-offset))
2072 (let ((stack (gethash msg mh-sequence-notation-history)))
2073 (setf (gethash msg mh-sequence-notation-history)
2074 (cons (char-after) stack)))
2075 (mh-notate nil mh-note-seq
2076 (+ mh-cmd-note mh-scan-field-destination-offset))))))
2077
2078 (defun mh-remove-sequence-notation (msg internal-seq-flag &optional all)
2079 "Remove sequence notation from the MSG on the current line.
2080 If INTERNAL-SEQ-FLAG is non-nil, then `font-lock' was used to highlight the
2081 sequence. In that case, no notation needs to be removed. Otherwise the effect
2082 of inserting `mh-note-seq' needs to be reversed.
2083 If ALL is non-nil, then all sequence marks on the scan line are removed."
2084 (with-mh-folder-updating (t)
2085 ;; This takes care of internal sequences...
2086 (mh-notate nil nil mh-cmd-note)
2087 (unless internal-seq-flag
2088 ;; ... and this takes care of user sequences.
2089 (let ((stack (gethash msg mh-sequence-notation-history)))
2090 (while (and all (cdr stack))
2091 (setq stack (cdr stack)))
2092 (when stack
2093 (save-excursion
2094 (beginning-of-line)
2095 (forward-char (+ mh-cmd-note mh-scan-field-destination-offset))
2096 (delete-char 1)
2097 (insert (car stack))))
2098 (setf (gethash msg mh-sequence-notation-history) (cdr stack))))))
2099
2100 (defun mh-remove-cur-notation ()
2101 "Remove old cur notation."
2102 (let ((cur-msg (car (mh-seq-to-msgs 'cur))))
2103 (save-excursion
2104 (when (and cur-msg
2105 (mh-goto-msg cur-msg t t)
2106 (looking-at mh-scan-cur-msg-number-regexp))
2107 (mh-notate nil ? mh-cmd-note)
2108 (setq overlay-arrow-position nil)))))
2109
2110 (defun mh-remove-all-notation ()
2111 "Remove all notations on all scan lines that MH-E introduces."
2112 (save-excursion
2113 (setq overlay-arrow-position nil)
2114 (goto-char (point-min))
2115 (mh-iterate-on-range msg (cons (point-min) (point-max))
2116 (mh-notate nil ? mh-cmd-note)
2117 (mh-remove-sequence-notation msg nil t))
2118 (clrhash mh-sequence-notation-history)))
2119
2120
2121 (defun mh-goto-cur-msg (&optional minimal-changes-flag)
2122 "Position the cursor at the current message.
2123 When optional argument MINIMAL-CHANGES-FLAG is non-nil, the function doesn't
2124 recenter the folder buffer."
2125 (let ((cur-msg (car (mh-seq-to-msgs 'cur))))
2126 (cond ((and cur-msg
2127 (mh-goto-msg cur-msg t t))
2128 (unless minimal-changes-flag
2129 (mh-notate-cur)
2130 (mh-recenter 0)
2131 (mh-maybe-show cur-msg)))
2132 (t
2133 (setq overlay-arrow-position nil)
2134 (message "No current message")))))
2135
2136 (defun mh-process-or-undo-commands (folder)
2137 "If FOLDER has outstanding commands, then either process or discard them.
2138 Called by functions like `mh-sort-folder', so also invalidate show buffer."
2139 (set-buffer folder)
2140 (if (mh-outstanding-commands-p)
2141 (if (or mh-do-not-confirm-flag
2142 (y-or-n-p
2143 "Process outstanding deletes and refiles? "))
2144 (mh-process-commands folder)
2145 (set-buffer folder)
2146 (mh-undo-folder)))
2147 (mh-update-unseen)
2148 (mh-invalidate-show-buffer))
2149
2150 (defun mh-process-commands (folder)
2151 "Process outstanding commands for FOLDER.
2152 The value of `mh-folder-updated-hook' is a list of functions to be called,
2153 with no arguments, before the commands are processed."
2154 (message "Processing deletes and refiles for %s..." folder)
2155 (set-buffer folder)
2156 (with-mh-folder-updating (nil)
2157 ;; Run the hook while the lists are still valid
2158 (run-hooks 'mh-folder-updated-hook)
2159
2160 ;; Update the unseen sequence if it exists
2161 (mh-update-unseen)
2162
2163 (let ((redraw-needed-flag mh-index-data)
2164 (folders-changed (list mh-current-folder))
2165 (seq-map (and mh-refile-list mh-refile-preserves-sequences-flag
2166 (mh-create-sequence-map mh-seq-list)))
2167 (dest-map (and mh-refile-list mh-refile-preserves-sequences-flag
2168 (make-hash-table))))
2169 ;; Remove invalid scan lines if we are in an index folder and then remove
2170 ;; the real messages
2171 (when mh-index-data
2172 (mh-index-delete-folder-headers)
2173 (setq folders-changed
2174 (append folders-changed (mh-index-execute-commands))))
2175
2176 ;; Then refile messages
2177 (mh-mapc #'(lambda (folder-msg-list)
2178 (let* ((dest-folder (symbol-name (car folder-msg-list)))
2179 (last (car (mh-translate-range dest-folder "last")))
2180 (msgs (cdr folder-msg-list)))
2181 (push dest-folder folders-changed)
2182 (setq redraw-needed-flag t)
2183 (apply #'mh-exec-cmd
2184 "refile" "-src" folder dest-folder
2185 (mh-coalesce-msg-list msgs))
2186 (mh-delete-scan-msgs msgs)
2187 ;; Preserve sequences in destination folder...
2188 (when mh-refile-preserves-sequences-flag
2189 (clrhash dest-map)
2190 (loop for i from (1+ (or last 0))
2191 for msg in (sort (copy-sequence msgs) #'<)
2192 do (loop for seq-name in (gethash msg seq-map)
2193 do (push i (gethash seq-name dest-map))))
2194 (maphash
2195 #'(lambda (seq msgs)
2196 ;; Can't be run in the background, since the
2197 ;; current folder is changed by mark this could
2198 ;; lead to a race condition with the next refile.
2199 (apply #'mh-exec-cmd "mark"
2200 "-sequence" (symbol-name seq) dest-folder
2201 "-add" (mapcar #'(lambda (x) (format "%s" x))
2202 (mh-coalesce-msg-list msgs))))
2203 dest-map))))
2204 mh-refile-list)
2205 (setq mh-refile-list ())
2206
2207 ;; Now delete messages
2208 (cond (mh-delete-list
2209 (setq redraw-needed-flag t)
2210 (apply 'mh-exec-cmd "rmm" folder
2211 (mh-coalesce-msg-list mh-delete-list))
2212 (mh-delete-scan-msgs mh-delete-list)
2213 (setq mh-delete-list nil)))
2214
2215 ;; Don't need to remove sequences since delete and refile do so.
2216 ;; Mark cur message
2217 (if (> (buffer-size) 0)
2218 (mh-define-sequence 'cur (list (or (mh-get-msg-num nil) "last"))))
2219
2220 ;; Redraw folder buffer if needed
2221 (when (and redraw-needed-flag)
2222 (when (mh-speed-flists-active-p)
2223 (apply #'mh-speed-flists t folders-changed))
2224 (cond ((memq 'unthread mh-view-ops) (mh-thread-inc folder (point-max)))
2225 (mh-index-data (mh-index-insert-folder-headers)))))
2226
2227 (and (buffer-file-name (get-buffer mh-show-buffer))
2228 (not (file-exists-p (buffer-file-name (get-buffer mh-show-buffer))))
2229 ;; If "inc" were to put a new msg in this file,
2230 ;; we would not notice, so mark it invalid now.
2231 (mh-invalidate-show-buffer))
2232
2233 (setq mh-seq-list (mh-read-folder-sequences mh-current-folder nil))
2234 (mh-remove-all-notation)
2235 (mh-notate-user-sequences)
2236 (message "Processing deletes and refiles for %s...done" folder)))
2237
2238 (defun mh-update-unseen ()
2239 "Synchronize the unseen sequence with MH.
2240 Return non-nil iff the MH folder was set.
2241 The value of `mh-unseen-updated-hook' is a list of functions to be called,
2242 with no arguments, after the unseen sequence is updated."
2243 (if mh-seen-list
2244 (let* ((unseen-seq (mh-find-seq mh-unseen-seq))
2245 (unseen-msgs (mh-seq-msgs unseen-seq)))
2246 (if unseen-msgs
2247 (progn
2248 (mh-undefine-sequence mh-unseen-seq mh-seen-list)
2249 (run-hooks 'mh-unseen-updated-hook)
2250 (while mh-seen-list
2251 (setq unseen-msgs (delq (car mh-seen-list) unseen-msgs))
2252 (setq mh-seen-list (cdr mh-seen-list)))
2253 (setcdr unseen-seq unseen-msgs)
2254 t) ;since we set the folder
2255 (setq mh-seen-list nil)))))
2256
2257 (defun mh-delete-scan-msgs (msgs)
2258 "Delete the scan listing lines for MSGS."
2259 (save-excursion
2260 (while msgs
2261 (when (mh-goto-msg (car msgs) t t)
2262 (when (memq 'unthread mh-view-ops)
2263 (mh-thread-forget-message (car msgs)))
2264 (mh-delete-line 1))
2265 (setq msgs (cdr msgs)))))
2266
2267 (defun mh-outstanding-commands-p ()
2268 "Return non-nil if there are outstanding deletes or refiles."
2269 (save-excursion
2270 (when (eq major-mode 'mh-show-mode)
2271 (set-buffer mh-show-folder-buffer))
2272 (or mh-delete-list mh-refile-list)))
2273
2274 (defun mh-coalesce-msg-list (messages)
2275 "Given a list of MESSAGES, return a list of message number ranges.
2276 This is the inverse of `mh-read-msg-list', which expands ranges.
2277 Message lists passed to MH programs should be processed by this function
2278 to avoid exceeding system command line argument limits."
2279 (let ((msgs (sort (copy-sequence messages) 'mh-greaterp))
2280 (range-high nil)
2281 (prev -1)
2282 (ranges nil))
2283 (while prev
2284 (if range-high
2285 (if (or (not (numberp prev))
2286 (not (equal (car msgs) (1- prev))))
2287 (progn ;non-sequential, flush old range
2288 (if (eq prev range-high)
2289 (setq ranges (cons range-high ranges))
2290 (setq ranges (cons (format "%s-%s" prev range-high) ranges)))
2291 (setq range-high nil))))
2292 (or range-high
2293 (setq range-high (car msgs))) ;start new or first range
2294 (setq prev (car msgs))
2295 (setq msgs (cdr msgs)))
2296 ranges))
2297
2298 (defun mh-greaterp (msg1 msg2)
2299 "Return the greater of two message indicators MSG1 and MSG2.
2300 Strings are \"smaller\" than numbers.
2301 Valid values are things like \"cur\", \"last\", 1, and 1820."
2302 (if (numberp msg1)
2303 (if (numberp msg2)
2304 (> msg1 msg2)
2305 t)
2306 (if (numberp msg2)
2307 nil
2308 (string-lessp msg2 msg1))))
2309
2310 (defun mh-lessp (msg1 msg2)
2311 "Return the lesser of two message indicators MSG1 and MSG2.
2312 Strings are \"smaller\" than numbers.
2313 Valid values are things like \"cur\", \"last\", 1, and 1820."
2314 (not (mh-greaterp msg1 msg2)))
2315
2316 \f
2317
2318 ;;; Basic sequence handling
2319
2320 (defun mh-delete-seq-locally (seq)
2321 "Remove MH-E's record of SEQ."
2322 (let ((entry (mh-find-seq seq)))
2323 (setq mh-seq-list (delq entry mh-seq-list))))
2324
2325 (defun mh-read-folder-sequences (folder save-refiles)
2326 "Read and return the predefined sequences for a FOLDER.
2327 If SAVE-REFILES is non-nil, then keep the sequences
2328 that note messages to be refiled."
2329 (let ((seqs ()))
2330 (cond (save-refiles
2331 (mh-mapc (function (lambda (seq) ; Save the refiling sequences
2332 (if (mh-folder-name-p (mh-seq-name seq))
2333 (setq seqs (cons seq seqs)))))
2334 mh-seq-list)))
2335 (save-excursion
2336 (if (eq 0 (mh-exec-cmd-quiet nil "mark" folder "-list"))
2337 (progn
2338 ;; look for name in line of form "cur: 4" or "myseq (private): 23"
2339 (while (re-search-forward "^[^: ]+" nil t)
2340 (setq seqs (cons (mh-make-seq (intern (buffer-substring
2341 (match-beginning 0)
2342 (match-end 0)))
2343 (mh-read-msg-list))
2344 seqs)))
2345 (delete-region (point-min) (point))))) ; avoid race with
2346 ; mh-process-daemon
2347 seqs))
2348
2349 (defun mh-read-msg-list ()
2350 "Return a list of message numbers from point to the end of the line.
2351 Expands ranges into set of individual numbers."
2352 (let ((msgs ())
2353 (end-of-line (save-excursion (end-of-line) (point)))
2354 num)
2355 (while (re-search-forward "[0-9]+" end-of-line t)
2356 (setq num (string-to-number (buffer-substring (match-beginning 0)
2357 (match-end 0))))
2358 (cond ((looking-at "-") ; Message range
2359 (forward-char 1)
2360 (re-search-forward "[0-9]+" end-of-line t)
2361 (let ((num2 (string-to-number
2362 (buffer-substring (match-beginning 0)
2363 (match-end 0)))))
2364 (if (< num2 num)
2365 (error "Bad message range: %d-%d" num num2))
2366 (while (<= num num2)
2367 (setq msgs (cons num msgs))
2368 (setq num (1+ num)))))
2369 ((not (zerop num)) ;"pick" outputs "0" to mean no match
2370 (setq msgs (cons num msgs)))))
2371 msgs))
2372
2373 (defun mh-notate-user-sequences (&optional range)
2374 "Mark user-defined sequences in RANGE.
2375
2376 Check the documentation of `mh-interactive-range' to see how RANGE is read in
2377 interactive use; if nil all messages are notated."
2378 (unless range
2379 (setq range (cons (point-min) (point-max))))
2380 (let ((seqs mh-seq-list)
2381 (msg-hash (make-hash-table)))
2382 (dolist (seq seqs)
2383 (dolist (msg (mh-seq-msgs seq))
2384 (push (car seq) (gethash msg msg-hash))))
2385 (mh-iterate-on-range msg range
2386 (loop for seq in (gethash msg msg-hash)
2387 do (mh-add-sequence-notation msg (mh-internal-seq seq))))))
2388
2389 (defvar mh-internal-seqs '(answered cur deleted forwarded printed))
2390
2391 (defun mh-internal-seq (name)
2392 "Return non-nil if NAME is the name of an internal MH-E sequence."
2393 (or (memq name mh-internal-seqs)
2394 (eq name mh-unseen-seq)
2395 (and (mh-colors-in-use-p) mh-tick-seq (eq name mh-tick-seq))
2396 (eq name mh-previous-seq)
2397 (mh-folder-name-p name)))
2398
2399 (defun mh-valid-seq-p (name)
2400 "Return non-nil if NAME is a valid MH sequence name."
2401 (and (symbolp name)
2402 (string-match "^[a-zA-Z][a-zA-Z0-9]*$" (symbol-name name))))
2403
2404 (defun mh-delete-msg-from-seq (range sequence &optional internal-flag)
2405 "Delete RANGE from SEQUENCE.
2406
2407 Check the documentation of `mh-interactive-range' to see how RANGE is read in
2408 interactive use.
2409
2410 In a program, non-nil INTERNAL-FLAG means do not inform MH of the change."
2411 (interactive (list (mh-interactive-range "Delete")
2412 (mh-read-seq-default "Delete from" t)
2413 nil))
2414 (let ((entry (mh-find-seq sequence))
2415 (user-sequence-flag (not (mh-internal-seq sequence)))
2416 (folders-changed (list mh-current-folder))
2417 (msg-list ()))
2418 (when entry
2419 (mh-iterate-on-range msg range
2420 (push msg msg-list)
2421 ;; Calling "mark" repeatedly takes too long. So we will pretend here
2422 ;; that we are just modifying an internal sequence...
2423 (when (memq msg (cdr entry))
2424 (mh-remove-sequence-notation msg (not user-sequence-flag)))
2425 (mh-delete-a-msg-from-seq msg sequence t))
2426 ;; ... and here we will "mark" all the messages at one go.
2427 (unless internal-flag (mh-undefine-sequence sequence msg-list))
2428 (when (and mh-index-data (not internal-flag))
2429 (setq folders-changed
2430 (append folders-changed
2431 (mh-index-delete-from-sequence sequence msg-list))))
2432 (when (and (eq sequence mh-unseen-seq) (mh-speed-flists-active-p))
2433 (apply #'mh-speed-flists t folders-changed)))))
2434
2435 (defun mh-catchup (range)
2436 "Delete RANGE from the \"unseen\" sequence.
2437
2438 Check the documentation of `mh-interactive-range' to see how RANGE is read in
2439 interactive use."
2440 (interactive (list (mh-interactive-range "Catchup"
2441 (cons (point-min) (point-max)))))
2442 (mh-delete-msg-from-seq range mh-unseen-seq))
2443
2444 (defun mh-delete-a-msg-from-seq (msg sequence internal-flag)
2445 "Delete MSG from SEQUENCE.
2446 If INTERNAL-FLAG is non-nil, then do not inform MH of the change."
2447 (let ((entry (mh-find-seq sequence)))
2448 (when (and entry (memq msg (mh-seq-msgs entry)))
2449 (if (not internal-flag)
2450 (mh-undefine-sequence sequence (list msg)))
2451 (setcdr entry (delq msg (mh-seq-msgs entry))))))
2452
2453 (defun mh-undefine-sequence (seq msgs)
2454 "Remove from the SEQ the list of MSGS."
2455 (when (and (mh-valid-seq-p seq) msgs)
2456 (apply #'mh-exec-cmd "mark" mh-current-folder "-delete"
2457 "-sequence" (symbol-name seq) (mh-coalesce-msg-list msgs))))
2458
2459 (defun mh-define-sequence (seq msgs)
2460 "Define the SEQ to contain the list of MSGS.
2461 Do not mark pseudo-sequences or empty sequences.
2462 Signals an error if SEQ is an invalid name."
2463 (if (and msgs
2464 (mh-valid-seq-p seq)
2465 (not (mh-folder-name-p seq)))
2466 (save-excursion
2467 (mh-exec-cmd-error nil "mark" mh-current-folder "-add" "-zero"
2468 "-sequence" (symbol-name seq)
2469 (mh-coalesce-msg-list msgs)))))
2470
2471 (defun mh-seq-containing-msg (msg &optional include-internal-flag)
2472 "Return a list of the sequences containing MSG.
2473 If INCLUDE-INTERNAL-FLAG non-nil, include MH-E internal sequences in list."
2474 (let ((l mh-seq-list)
2475 (seqs ()))
2476 (while l
2477 (and (memq msg (mh-seq-msgs (car l)))
2478 (or include-internal-flag
2479 (not (mh-internal-seq (mh-seq-name (car l)))))
2480 (setq seqs (cons (mh-seq-name (car l)) seqs)))
2481 (setq l (cdr l)))
2482 seqs))
2483
2484 \f
2485
2486 ;;; Build mh-folder-mode keymap:
2487
2488 (suppress-keymap mh-folder-mode-map)
2489
2490 ;; Use defalias to make sure the documented primary key bindings
2491 ;; appear in menu lists.
2492 (defalias 'mh-alt-show 'mh-show)
2493 (defalias 'mh-alt-refile-msg 'mh-refile-msg)
2494 (defalias 'mh-alt-send 'mh-send)
2495 (defalias 'mh-alt-visit-folder 'mh-visit-folder)
2496
2497 ;; Save the `b' binding for a future `back'. Maybe?
2498 (gnus-define-keys mh-folder-mode-map
2499 " " mh-page-msg
2500 "!" mh-refile-or-write-again
2501 "'" mh-toggle-tick
2502 "," mh-header-display
2503 "." mh-alt-show
2504 ";" mh-toggle-mh-decode-mime-flag
2505 ">" mh-write-msg-to-file
2506 "?" mh-help
2507 "E" mh-extract-rejected-mail
2508 "M" mh-modify
2509 "\177" mh-previous-page
2510 "\C-d" mh-delete-msg-no-motion
2511 "\t" mh-index-next-folder
2512 [backtab] mh-index-previous-folder
2513 "\M-\t" mh-index-previous-folder
2514 "\e<" mh-first-msg
2515 "\e>" mh-last-msg
2516 "\ed" mh-redistribute
2517 "\r" mh-show
2518 "^" mh-alt-refile-msg
2519 "c" mh-copy-msg
2520 "d" mh-delete-msg
2521 "e" mh-edit-again
2522 "f" mh-forward
2523 "g" mh-goto-msg
2524 "i" mh-inc-folder
2525 "k" mh-delete-subject-or-thread
2526 "m" mh-alt-send
2527 "n" mh-next-undeleted-msg
2528 "\M-n" mh-next-unread-msg
2529 "o" mh-refile-msg
2530 "p" mh-previous-undeleted-msg
2531 "\M-p" mh-previous-unread-msg
2532 "q" mh-quit
2533 "r" mh-reply
2534 "s" mh-send
2535 "t" mh-toggle-showing
2536 "u" mh-undo
2537 "v" mh-index-visit-folder
2538 "x" mh-execute-commands
2539 "|" mh-pipe-msg)
2540
2541 (gnus-define-keys (mh-folder-map "F" mh-folder-mode-map)
2542 "?" mh-prefix-help
2543 "'" mh-index-ticked-messages
2544 "S" mh-sort-folder
2545 "c" mh-catchup
2546 "f" mh-alt-visit-folder
2547 "i" mh-index-search
2548 "k" mh-kill-folder
2549 "l" mh-list-folders
2550 "n" mh-index-new-messages
2551 "o" mh-alt-visit-folder
2552 "p" mh-pack-folder
2553 "q" mh-index-sequenced-messages
2554 "r" mh-rescan-folder
2555 "s" mh-search-folder
2556 "u" mh-undo-folder
2557 "v" mh-visit-folder)
2558
2559 (define-key mh-folder-mode-map "I" mh-inc-spool-map)
2560
2561 (gnus-define-keys (mh-junk-map "J" mh-folder-mode-map)
2562 "?" mh-prefix-help
2563 "b" mh-junk-blacklist
2564 "w" mh-junk-whitelist)
2565
2566 (gnus-define-keys (mh-ps-print-map "P" mh-folder-mode-map)
2567 "?" mh-prefix-help
2568 "C" mh-ps-print-toggle-color
2569 "F" mh-ps-print-toggle-faces
2570 "f" mh-ps-print-msg-file
2571 "l" mh-print-msg
2572 "p" mh-ps-print-msg)
2573
2574 (gnus-define-keys (mh-sequence-map "S" mh-folder-mode-map)
2575 "'" mh-narrow-to-tick
2576 "?" mh-prefix-help
2577 "d" mh-delete-msg-from-seq
2578 "k" mh-delete-seq
2579 "l" mh-list-sequences
2580 "n" mh-narrow-to-seq
2581 "p" mh-put-msg-in-seq
2582 "s" mh-msg-is-in-seq
2583 "w" mh-widen)
2584
2585 (gnus-define-keys (mh-thread-map "T" mh-folder-mode-map)
2586 "?" mh-prefix-help
2587 "u" mh-thread-ancestor
2588 "p" mh-thread-previous-sibling
2589 "n" mh-thread-next-sibling
2590 "t" mh-toggle-threads
2591 "d" mh-thread-delete
2592 "o" mh-thread-refile)
2593
2594 (gnus-define-keys (mh-limit-map "/" mh-folder-mode-map)
2595 "'" mh-narrow-to-tick
2596 "?" mh-prefix-help
2597 "c" mh-narrow-to-cc
2598 "f" mh-narrow-to-from
2599 "r" mh-narrow-to-range
2600 "s" mh-narrow-to-subject
2601 "t" mh-narrow-to-to
2602 "w" mh-widen)
2603
2604 (gnus-define-keys (mh-extract-map "X" mh-folder-mode-map)
2605 "?" mh-prefix-help
2606 "s" mh-store-msg ;shar
2607 "u" mh-store-msg) ;uuencode
2608
2609 (gnus-define-keys (mh-digest-map "D" mh-folder-mode-map)
2610 " " mh-page-digest
2611 "?" mh-prefix-help
2612 "\177" mh-page-digest-backwards
2613 "b" mh-burst-digest)
2614
2615 (gnus-define-keys (mh-mime-map "K" mh-folder-mode-map)
2616 "?" mh-prefix-help
2617 "a" mh-mime-save-parts
2618 "e" mh-display-with-external-viewer
2619 "i" mh-folder-inline-mime-part
2620 "o" mh-folder-save-mime-part
2621 "t" mh-toggle-mime-buttons
2622 "v" mh-folder-toggle-mime-part
2623 "\t" mh-next-button
2624 [backtab] mh-prev-button
2625 "\M-\t" mh-prev-button)
2626
2627 (cond
2628 (mh-xemacs-flag
2629 (define-key mh-folder-mode-map [button2] 'mh-show-mouse))
2630 (t
2631 (define-key mh-folder-mode-map [mouse-2] 'mh-show-mouse)))
2632
2633 ;; "C-c /" prefix is used in mh-folder-mode by pgp.el and mailcrypt
2634
2635 \f
2636
2637 ;;; Help Messages
2638
2639 ;; If you add a new prefix, add appropriate text to the nil key.
2640 ;;
2641 ;; In general, messages are grouped logically. Taking the main commands for
2642 ;; example, the first line is "ways to view messages," the second line is
2643 ;; "things you can do with messages", and the third is "composing" messages.
2644 ;;
2645 ;; When adding a new prefix, ensure that the help message contains "what" the
2646 ;; prefix is for. For example, if the word "folder" were not present in the
2647 ;; `F' entry, it would not be clear what these commands operated upon.
2648 (defvar mh-help-messages
2649 '((nil "[i]nc, [.]show, [,]show all, [n]ext, [p]revious,\n"
2650 "[d]elete, [o]refile, e[x]ecute,\n"
2651 "[s]end, [r]eply,\n"
2652 "[;]toggle MIME decoding.\n"
2653 "Prefix characters:\n [F]older, [S]equence, [J]unk, MIME [K]eys,"
2654 "\n [T]hread, [/]limit, e[X]tract, [D]igest, [I]nc spools.")
2655
2656 (?F "[l]ist; [v]isit folder;\n"
2657 "[n]ew messages; [']ticked messages; [s]earch; [i]ndexed search;\n"
2658 "[p]ack; [S]ort; [r]escan; [k]ill")
2659 (?P "[p]rint message to [f]ile; old-style [l]pr printing;\n"
2660 "Toggle printing of [C]olors, [F]aces")
2661 (?S "[p]ut message in sequence, [n]arrow, [']narrow to ticked, [w]iden,\n"
2662 "[s]equences, [l]ist,\n"
2663 "[d]elete message from sequence, [k]ill sequence")
2664 (?T "[t]oggle, [d]elete, [o]refile thread")
2665 (?/ "Limit to [c]c, [f]rom, [r]ange, [s]ubject, [t]o; [w]iden")
2666 (?X "un[s]har, [u]udecode message")
2667 (?D "[b]urst digest")
2668 (?K "[v]iew, [i]nline, [o]utput/save MIME part; save [a]ll parts; \n"
2669 "[TAB] next; [SHIFT-TAB] previous")
2670 (?J "[b]lacklist, [w]hitelist message"))
2671 "Key binding cheat sheet.
2672
2673 This is an associative array which is used to show the most common commands.
2674 The key is a prefix char. The value is one or more strings which are
2675 concatenated together and displayed in the minibuffer if ? is pressed after
2676 the prefix character. The special key nil is used to display the
2677 non-prefixed commands.
2678
2679 The substitutions described in `substitute-command-keys' are performed as
2680 well.")
2681
2682 \f
2683
2684 (dolist (mess '("^Cursor not pointing to message$"
2685 "^There is no other window$"))
2686 (add-to-list 'debug-ignored-errors mess))
2687
2688 (provide 'mh-e)
2689
2690 ;; Local Variables:
2691 ;; indent-tabs-mode: nil
2692 ;; sentence-end-double-space: nil
2693 ;; End:
2694
2695 ;; arch-tag: cce884de-bd37-4104-9963-e4439d5ed22b
2696 ;;; mh-e.el ends here