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