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