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