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