]> code.delx.au - gnu-emacs/blob - lisp/mh-e/mh-e.el
(mh-folder-mode): Bind desktop-save-buffer to t.
[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 (make-local-variable 'desktop-save-buffer)
1552 (setq desktop-save-buffer t)
1553 (mh-make-local-vars
1554 'mh-current-folder (buffer-name) ; Name of folder, a string
1555 'mh-show-buffer (format "show-%s" (buffer-name)) ; Buffer that displays msgs
1556 'mh-folder-filename ; e.g. "/usr/foobar/Mail/inbox/"
1557 (file-name-as-directory (mh-expand-file-name (buffer-name)))
1558 'mh-arrow-marker (make-marker) ; Marker where arrow is displayed
1559 'overlay-arrow-position nil ; Allow for simultaneous display in
1560 'overlay-arrow-string ">" ; different MH-E buffers.
1561 'mh-showing-mode nil ; Show message also?
1562 'mh-delete-list nil ; List of msgs nums to delete
1563 'mh-refile-list nil ; List of folder names in mh-seq-list
1564 'mh-seq-list nil ; Alist of (seq . msgs) nums
1565 'mh-seen-list nil ; List of displayed messages
1566 'mh-next-direction 'forward ; Direction to move to next message
1567 'mh-narrowed-to-seq nil ; Sequence display is narrowed to
1568 'mh-tick-seq-changed-when-narrowed-flag nil
1569 ; Tick seq changed while narrowed
1570 'mh-view-ops () ; Stack that keeps track of the order
1571 ; in which narrowing/threading has been
1572 ; carried out.
1573 'mh-index-data nil ; If the folder was created by a call
1574 ; to mh-index-search this contains info
1575 ; about the search results.
1576 'mh-index-previous-search nil ; Previous folder and search-regexp
1577 'mh-index-msg-checksum-map nil ; msg -> checksum map
1578 'mh-index-checksum-origin-map nil ; checksum -> ( orig-folder, orig-msg )
1579 'mh-first-msg-num nil ; Number of first msg in buffer
1580 'mh-last-msg-num nil ; Number of last msg in buffer
1581 'mh-msg-count nil ; Number of msgs in buffer
1582 'mh-mode-line-annotation nil ; Indicates message range
1583 'mh-previous-window-config nil) ; Previous window configuration
1584 (mh-remove-xemacs-horizontal-scrollbar)
1585 (setq truncate-lines t)
1586 (auto-save-mode -1)
1587 (setq buffer-offer-save t)
1588 (mh-make-local-hook (mh-write-file-functions-compat))
1589 (add-hook (mh-write-file-functions-compat) 'mh-execute-commands nil t)
1590 (make-local-variable 'revert-buffer-function)
1591 (make-local-variable 'hl-line-mode) ; avoid pollution
1592 (mh-funcall-if-exists hl-line-mode 1)
1593 (setq revert-buffer-function 'mh-undo-folder)
1594 (or (assq 'mh-showing-mode minor-mode-alist)
1595 (setq minor-mode-alist
1596 (cons '(mh-showing-mode " Show") minor-mode-alist)))
1597 (easy-menu-add mh-folder-sequence-menu)
1598 (easy-menu-add mh-folder-message-menu)
1599 (easy-menu-add mh-folder-folder-menu)
1600 (if (and (boundp 'tool-bar-mode) tool-bar-mode)
1601 (set (make-local-variable 'tool-bar-map) mh-folder-tool-bar-map))
1602 (mh-funcall-if-exists mh-toolbar-init :folder)
1603 (if (and mh-xemacs-flag
1604 font-lock-auto-fontify)
1605 (turn-on-font-lock))) ; Force font-lock in XEmacs.
1606
1607 (defun mh-make-local-vars (&rest pairs)
1608 "Initialize local variables according to the variable-value PAIRS."
1609
1610 (while pairs
1611 (set (make-local-variable (car pairs)) (car (cdr pairs)))
1612 (setq pairs (cdr (cdr pairs)))))
1613
1614 (defun mh-scan-folder (folder range &optional dont-exec-pending)
1615 "Scan the FOLDER over the RANGE.
1616 If the optional argument DONT-EXEC-PENDING is non-nil then pending deletes and
1617 refiles aren't carried out.
1618 Return in the folder's buffer."
1619 (cond ((null (get-buffer folder))
1620 (mh-make-folder folder))
1621 (t
1622 (unless dont-exec-pending
1623 (mh-process-or-undo-commands folder)
1624 (mh-reset-threads-and-narrowing))
1625 (switch-to-buffer folder)))
1626 (mh-regenerate-headers range)
1627 (if (zerop (buffer-size))
1628 (if (equal range "all")
1629 (message "Folder %s is empty" folder)
1630 (message "No messages in %s, range %s" folder range))
1631 (mh-goto-cur-msg))
1632 (when (mh-outstanding-commands-p)
1633 (mh-notate-deleted-and-refiled)))
1634
1635 (defun mh-set-cmd-note (width)
1636 "Set `mh-cmd-note' to WIDTH characters (minimum of 2).
1637
1638 If `mh-scan-format-file' specifies nil or a filename, then this function
1639 will NOT update `mh-cmd-note'."
1640 ;; Add one to the width to always have whitespace in column zero.
1641 (setq width (max (1+ width) 2))
1642 (if (and (equal mh-scan-format-file t)
1643 (not (eq mh-cmd-note width)))
1644 (setq mh-cmd-note width))
1645 mh-cmd-note)
1646
1647 (defun mh-regenerate-headers (range &optional update)
1648 "Scan folder over range RANGE.
1649 If UPDATE, append the scan lines, otherwise replace."
1650 (let ((folder mh-current-folder)
1651 (range (if (and range (atom range)) (list range) range))
1652 scan-start)
1653 (message "Scanning %s..." folder)
1654 (with-mh-folder-updating (nil)
1655 (if update
1656 (goto-char (point-max))
1657 (delete-region (point-min) (point-max))
1658 (if mh-adaptive-cmd-note-flag
1659 (mh-set-cmd-note (mh-message-number-width folder))))
1660 (setq scan-start (point))
1661 (apply #'mh-exec-cmd-output
1662 mh-scan-prog nil
1663 (mh-scan-format)
1664 "-noclear" "-noheader"
1665 "-width" (window-width)
1666 folder range)
1667 (goto-char scan-start)
1668 (cond ((looking-at "scan: no messages in")
1669 (keep-lines mh-scan-valid-regexp)) ; Flush random scan lines
1670 ((looking-at "scan: bad message list ")
1671 (keep-lines mh-scan-valid-regexp))
1672 ((looking-at "scan: ")) ; Keep error messages
1673 (t
1674 (keep-lines mh-scan-valid-regexp))) ; Flush random scan lines
1675 (setq mh-seq-list (mh-read-folder-sequences folder nil))
1676 (mh-notate-user-sequences)
1677 (or update
1678 (setq mh-mode-line-annotation
1679 (if (equal range '("all"))
1680 nil
1681 mh-partial-folder-mode-line-annotation)))
1682 (mh-make-folder-mode-line))
1683 (message "Scanning %s...done" folder)))
1684
1685 (defun mh-generate-new-cmd-note (folder)
1686 "Fix the `mh-cmd-note' value for this FOLDER.
1687
1688 After doing an `mh-get-new-mail' operation in this FOLDER, at least
1689 one line that looks like a truncated message number was found.
1690
1691 Remove the text added by the last `mh-inc' command. It should be the
1692 messages cur-last. Call `mh-set-cmd-note' with the widest message number
1693 in FOLDER.
1694
1695 Reformat the message number width on each line in the buffer and trim
1696 the line length to fit in the window.
1697
1698 Rescan the FOLDER in the range cur-last in order to display the
1699 messages that were removed earlier. They should all fit in the scan
1700 line now with no message truncation."
1701 (save-excursion
1702 (let ((maxcol (1- (window-width)))
1703 (old-cmd-note mh-cmd-note)
1704 mh-cmd-note-fmt
1705 msgnum)
1706 ;; Nuke all of the lines just added by the last inc
1707 (delete-char (- (point-max) (point)))
1708 ;; Update the current buffer to reflect the new mh-cmd-note
1709 ;; value needed to display messages.
1710 (mh-set-cmd-note (mh-message-number-width folder))
1711 (setq mh-cmd-note-fmt (concat "%" (format "%d" mh-cmd-note) "d"))
1712 ;; Cleanup the messages that are in the buffer right now
1713 (goto-char (point-min))
1714 (cond ((memq 'unthread mh-view-ops)
1715 (mh-thread-add-spaces (- mh-cmd-note old-cmd-note)))
1716 (t (while (re-search-forward mh-scan-msg-number-regexp nil 0 1)
1717 ;; reformat the number to fix in mh-cmd-note columns
1718 (setq msgnum (string-to-number
1719 (buffer-substring
1720 (match-beginning 1) (match-end 1))))
1721 (replace-match (format mh-cmd-note-fmt msgnum))
1722 ;; trim the line to fix in the window
1723 (end-of-line)
1724 (let ((eol (point)))
1725 (move-to-column maxcol)
1726 (if (<= (point) eol)
1727 (delete-char (- eol (point))))))))
1728 ;; now re-read the lost messages
1729 (goto-char (point-max))
1730 (prog1 (point)
1731 (mh-regenerate-headers "cur-last" t)))))
1732
1733 (defun mh-get-new-mail (maildrop-name)
1734 "Read new mail from MAILDROP-NAME into the current buffer.
1735 Return in the current buffer."
1736 (let ((point-before-inc (point))
1737 (folder mh-current-folder)
1738 (new-mail-flag nil))
1739 (with-mh-folder-updating (t)
1740 (if maildrop-name
1741 (message "inc %s -file %s..." folder maildrop-name)
1742 (message "inc %s..." folder))
1743 (setq mh-next-direction 'forward)
1744 (goto-char (point-max))
1745 (let ((start-of-inc (point)))
1746 (mh-remove-cur-notation)
1747 (if maildrop-name
1748 ;; I think MH 5 used "-ms-file" instead of "-file",
1749 ;; which would make inc'ing from maildrops fail.
1750 (mh-exec-cmd-output mh-inc-prog nil folder
1751 (mh-scan-format)
1752 "-file" (expand-file-name maildrop-name)
1753 "-width" (window-width)
1754 "-truncate")
1755 (mh-exec-cmd-output mh-inc-prog nil
1756 (mh-scan-format)
1757 "-width" (window-width)))
1758 (if maildrop-name
1759 (message "inc %s -file %s...done" folder maildrop-name)
1760 (message "inc %s...done" folder))
1761 (goto-char start-of-inc)
1762 (cond ((save-excursion
1763 (re-search-forward "^inc: no mail" nil t))
1764 (message "No new mail%s%s" (if maildrop-name " in " "")
1765 (if maildrop-name maildrop-name "")))
1766 ((and (when mh-narrowed-to-seq
1767 (let ((saved-text (buffer-substring-no-properties
1768 start-of-inc (point-max))))
1769 (delete-region start-of-inc (point-max))
1770 (unwind-protect (mh-widen)
1771 (goto-char (point-max))
1772 (setq start-of-inc (point))
1773 (insert saved-text)
1774 (goto-char start-of-inc))))
1775 nil))
1776 ((re-search-forward "^inc:" nil t) ; Error messages
1777 (error "Error incorporating mail"))
1778 ((and
1779 (equal mh-scan-format-file t)
1780 mh-adaptive-cmd-note-flag
1781 ;; Have we reached an edge condition?
1782 (save-excursion
1783 (re-search-forward mh-scan-msg-overflow-regexp nil 0 1))
1784 (setq start-of-inc (mh-generate-new-cmd-note folder))
1785 nil))
1786 (t
1787 (setq new-mail-flag t)))
1788 (keep-lines mh-scan-valid-regexp) ; Flush random scan lines
1789 (setq mh-seq-list (mh-read-folder-sequences folder t))
1790 (when (equal (point-max) start-of-inc)
1791 (mh-notate-cur))
1792 (mh-notate-user-sequences)
1793 (if new-mail-flag
1794 (progn
1795 (mh-make-folder-mode-line)
1796 (when (mh-speed-flists-active-p)
1797 (mh-speed-flists t mh-current-folder))
1798 (when (memq 'unthread mh-view-ops)
1799 (mh-thread-inc folder start-of-inc))
1800 (mh-goto-cur-msg))
1801 (goto-char point-before-inc))))))
1802
1803 (defun mh-make-folder-mode-line (&optional ignored)
1804 "Set the fields of the mode line for a folder buffer.
1805 The optional argument is now obsolete and IGNORED. It used to be used to pass
1806 in what is now stored in the buffer-local variable `mh-mode-line-annotation'."
1807 (save-excursion
1808 (save-window-excursion
1809 (mh-first-msg)
1810 (let ((new-first-msg-num (mh-get-msg-num nil)))
1811 (when (or (not (memq 'unthread mh-view-ops))
1812 (null mh-first-msg-num)
1813 (null new-first-msg-num)
1814 (< new-first-msg-num mh-first-msg-num))
1815 (setq mh-first-msg-num new-first-msg-num)))
1816 (mh-last-msg)
1817 (let ((new-last-msg-num (mh-get-msg-num nil)))
1818 (when (or (not (memq 'unthread mh-view-ops))
1819 (null mh-last-msg-num)
1820 (null new-last-msg-num)
1821 (> new-last-msg-num mh-last-msg-num))
1822 (setq mh-last-msg-num new-last-msg-num)))
1823 (setq mh-msg-count (if mh-first-msg-num
1824 (count-lines (point-min) (point-max))
1825 0))
1826 (setq mode-line-buffer-identification
1827 (list (format " {%%b%s} %s msg%s"
1828 (if mh-mode-line-annotation
1829 (format "/%s" mh-mode-line-annotation)
1830 "")
1831 (if (zerop mh-msg-count)
1832 "no"
1833 (format "%d" mh-msg-count))
1834 (if (zerop mh-msg-count)
1835 "s"
1836 (cond ((> mh-msg-count 1)
1837 (format "s (%d-%d)" mh-first-msg-num
1838 mh-last-msg-num))
1839 (mh-first-msg-num
1840 (format " (%d)" mh-first-msg-num))
1841 (""))))))
1842 (mh-logo-display))))
1843
1844 (defun mh-unmark-all-headers (remove-all-flags)
1845 "Remove all '+' flags from the folder listing.
1846 With non-nil argument REMOVE-ALL-FLAGS, remove all 'D', '^' and '%' flags too.
1847 Optimized for speed (i.e., no regular expressions)."
1848 (save-excursion
1849 (let ((case-fold-search nil)
1850 (last-line (1- (point-max)))
1851 char)
1852 (mh-first-msg)
1853 (while (<= (point) last-line)
1854 (forward-char mh-cmd-note)
1855 (setq char (following-char))
1856 (if (or (and remove-all-flags
1857 (or (= char (aref mh-note-deleted 0))
1858 (= char (aref mh-note-refiled 0))))
1859 (= char (aref mh-note-cur 0)))
1860 (progn
1861 (delete-char 1)
1862 (insert " ")))
1863 (if remove-all-flags
1864 (progn
1865 (forward-char 1)
1866 (if (= (following-char) (aref mh-note-seq 0))
1867 (progn
1868 (delete-char 1)
1869 (insert " ")))))
1870 (forward-line)))))
1871
1872 (defun mh-remove-cur-notation ()
1873 "Remove old cur notation."
1874 (let ((cur-msg (car (mh-seq-to-msgs 'cur))))
1875 (save-excursion
1876 (when (and cur-msg
1877 (mh-goto-msg cur-msg t t)
1878 (looking-at mh-scan-cur-msg-number-regexp))
1879 (mh-notate nil ? mh-cmd-note)
1880 (setq overlay-arrow-position nil)))))
1881
1882 (defun mh-remove-all-notation ()
1883 "Remove all notations on all scan lines that MH-E introduces."
1884 (save-excursion
1885 (setq overlay-arrow-position nil)
1886 (goto-char (point-min))
1887 (while (not (eobp))
1888 (unless (or (equal (char-after) ?+) (eolp))
1889 (mh-notate nil ? mh-cmd-note)
1890 (when (eq (char-after (+ (point) mh-cmd-note 1)) (elt mh-note-seq 0))
1891 (mh-notate nil ? (1+ mh-cmd-note))))
1892 (forward-line))))
1893
1894 ;;;###mh-autoload
1895 (defun mh-goto-cur-msg (&optional minimal-changes-flag)
1896 "Position the cursor at the current message.
1897 When optional argument MINIMAL-CHANGES-FLAG is non-nil, the function doesn't
1898 recenter the folder buffer."
1899 (let ((cur-msg (car (mh-seq-to-msgs 'cur))))
1900 (cond ((and cur-msg
1901 (mh-goto-msg cur-msg t t))
1902 (unless minimal-changes-flag
1903 (mh-notate-cur)
1904 (mh-recenter 0)
1905 (mh-maybe-show cur-msg)))
1906 (t
1907 (setq overlay-arrow-position nil)
1908 (message "No current message")))))
1909
1910 (defun mh-process-or-undo-commands (folder)
1911 "If FOLDER has outstanding commands, then either process or discard them.
1912 Called by functions like `mh-sort-folder', so also invalidate show buffer."
1913 (set-buffer folder)
1914 (if (mh-outstanding-commands-p)
1915 (if (or mh-do-not-confirm-flag
1916 (y-or-n-p
1917 "Process outstanding deletes and refiles? "))
1918 (mh-process-commands folder)
1919 (set-buffer folder)
1920 (mh-undo-folder)))
1921 (mh-update-unseen)
1922 (mh-invalidate-show-buffer))
1923
1924 (defun mh-process-commands (folder)
1925 "Process outstanding commands for FOLDER.
1926 The value of `mh-folder-updated-hook' is a list of functions to be called,
1927 with no arguments, before the commands are processed."
1928 (message "Processing deletes and refiles for %s..." folder)
1929 (set-buffer folder)
1930 (with-mh-folder-updating (nil)
1931 ;; Run the hook while the lists are still valid
1932 (run-hooks 'mh-folder-updated-hook)
1933
1934 ;; Update the unseen sequence if it exists
1935 (mh-update-unseen)
1936
1937 (let ((redraw-needed-flag mh-index-data))
1938 ;; Remove invalid scan lines if we are in an index folder and then remove
1939 ;; the real messages
1940 (when mh-index-data
1941 (mh-index-delete-folder-headers)
1942 (mh-index-execute-commands))
1943
1944 ;; Then refile messages
1945 (mh-mapc #'(lambda (folder-msg-list)
1946 (let ((dest-folder (symbol-name (car folder-msg-list)))
1947 (msgs (cdr folder-msg-list)))
1948 (setq redraw-needed-flag t)
1949 (apply #'mh-exec-cmd
1950 "refile" "-src" folder dest-folder
1951 (mh-coalesce-msg-list msgs))
1952 (mh-delete-scan-msgs msgs)))
1953 mh-refile-list)
1954 (setq mh-refile-list ())
1955
1956 ;; Now delete messages
1957 (cond (mh-delete-list
1958 (setq redraw-needed-flag t)
1959 (apply 'mh-exec-cmd "rmm" folder
1960 (mh-coalesce-msg-list mh-delete-list))
1961 (mh-delete-scan-msgs mh-delete-list)
1962 (setq mh-delete-list nil)))
1963
1964 ;; Don't need to remove sequences since delete and refile do so.
1965 ;; Mark cur message
1966 (if (> (buffer-size) 0)
1967 (mh-define-sequence 'cur (list (or (mh-get-msg-num nil) "last"))))
1968
1969 ;; Redraw folder buffer if needed
1970 (when (and redraw-needed-flag)
1971 (when (mh-speed-flists-active-p)
1972 (mh-speed-flists t mh-current-folder))
1973 (cond ((memq 'unthread mh-view-ops) (mh-thread-inc folder (point-max)))
1974 (mh-index-data (mh-index-insert-folder-headers)))))
1975
1976 (and (buffer-file-name (get-buffer mh-show-buffer))
1977 (not (file-exists-p (buffer-file-name (get-buffer mh-show-buffer))))
1978 ;; If "inc" were to put a new msg in this file,
1979 ;; we would not notice, so mark it invalid now.
1980 (mh-invalidate-show-buffer))
1981
1982 (setq mh-seq-list (mh-read-folder-sequences mh-current-folder nil))
1983 (mh-unmark-all-headers t)
1984 (mh-notate-user-sequences)
1985 (message "Processing deletes and refiles for %s...done" folder)))
1986
1987 (defun mh-update-unseen ()
1988 "Synchronize the unseen sequence with MH.
1989 Return non-nil iff the MH folder was set.
1990 The value of `mh-unseen-updated-hook' is a list of functions to be called,
1991 with no arguments, after the unseen sequence is updated."
1992 (if mh-seen-list
1993 (let* ((unseen-seq (mh-find-seq mh-unseen-seq))
1994 (unseen-msgs (mh-seq-msgs unseen-seq)))
1995 (if unseen-msgs
1996 (progn
1997 (mh-undefine-sequence mh-unseen-seq mh-seen-list)
1998 (run-hooks 'mh-unseen-updated-hook)
1999 (while mh-seen-list
2000 (setq unseen-msgs (delq (car mh-seen-list) unseen-msgs))
2001 (setq mh-seen-list (cdr mh-seen-list)))
2002 (setcdr unseen-seq unseen-msgs)
2003 t) ;since we set the folder
2004 (setq mh-seen-list nil)))))
2005
2006 (defun mh-delete-scan-msgs (msgs)
2007 "Delete the scan listing lines for MSGS."
2008 (save-excursion
2009 (while msgs
2010 (when (mh-goto-msg (car msgs) t t)
2011 (when (memq 'unthread mh-view-ops)
2012 (mh-thread-forget-message (car msgs)))
2013 (mh-delete-line 1))
2014 (setq msgs (cdr msgs)))))
2015
2016 (defun mh-outstanding-commands-p ()
2017 "Return non-nil if there are outstanding deletes or refiles."
2018 (or mh-delete-list mh-refile-list))
2019
2020 (defun mh-coalesce-msg-list (messages)
2021 "Given a list of MESSAGES, return a list of message number ranges.
2022 This is the inverse of `mh-read-msg-list', which expands ranges.
2023 Message lists passed to MH programs should be processed by this function
2024 to avoid exceeding system command line argument limits."
2025 (let ((msgs (sort (copy-sequence messages) 'mh-greaterp))
2026 (range-high nil)
2027 (prev -1)
2028 (ranges nil))
2029 (while prev
2030 (if range-high
2031 (if (or (not (numberp prev))
2032 (not (equal (car msgs) (1- prev))))
2033 (progn ;non-sequential, flush old range
2034 (if (eq prev range-high)
2035 (setq ranges (cons range-high ranges))
2036 (setq ranges (cons (format "%s-%s" prev range-high) ranges)))
2037 (setq range-high nil))))
2038 (or range-high
2039 (setq range-high (car msgs))) ;start new or first range
2040 (setq prev (car msgs))
2041 (setq msgs (cdr msgs)))
2042 ranges))
2043
2044 (defun mh-greaterp (msg1 msg2)
2045 "Return the greater of two message indicators MSG1 and MSG2.
2046 Strings are \"smaller\" than numbers.
2047 Legal values are things like \"cur\", \"last\", 1, and 1820."
2048 (if (numberp msg1)
2049 (if (numberp msg2)
2050 (> msg1 msg2)
2051 t)
2052 (if (numberp msg2)
2053 nil
2054 (string-lessp msg2 msg1))))
2055
2056 (defun mh-lessp (msg1 msg2)
2057 "Return the lesser of two message indicators MSG1 and MSG2.
2058 Strings are \"smaller\" than numbers.
2059 Legal values are things like \"cur\", \"last\", 1, and 1820."
2060 (not (mh-greaterp msg1 msg2)))
2061
2062 \f
2063
2064 ;;; Basic sequence handling
2065
2066 (defun mh-delete-seq-locally (seq)
2067 "Remove MH-E's record of SEQ."
2068 (let ((entry (mh-find-seq seq)))
2069 (setq mh-seq-list (delq entry mh-seq-list))))
2070
2071 (defun mh-read-folder-sequences (folder save-refiles)
2072 "Read and return the predefined sequences for a FOLDER.
2073 If SAVE-REFILES is non-nil, then keep the sequences
2074 that note messages to be refiled."
2075 (let ((seqs ()))
2076 (cond (save-refiles
2077 (mh-mapc (function (lambda (seq) ; Save the refiling sequences
2078 (if (mh-folder-name-p (mh-seq-name seq))
2079 (setq seqs (cons seq seqs)))))
2080 mh-seq-list)))
2081 (save-excursion
2082 (if (eq 0 (mh-exec-cmd-quiet nil "mark" folder "-list"))
2083 (progn
2084 ;; look for name in line of form "cur: 4" or "myseq (private): 23"
2085 (while (re-search-forward "^[^: ]+" nil t)
2086 (setq seqs (cons (mh-make-seq (intern (buffer-substring
2087 (match-beginning 0)
2088 (match-end 0)))
2089 (mh-read-msg-list))
2090 seqs)))
2091 (delete-region (point-min) (point))))) ; avoid race with
2092 ; mh-process-daemon
2093 seqs))
2094
2095 (defun mh-read-msg-list ()
2096 "Return a list of message numbers from point to the end of the line.
2097 Expands ranges into set of individual numbers."
2098 (let ((msgs ())
2099 (end-of-line (save-excursion (end-of-line) (point)))
2100 num)
2101 (while (re-search-forward "[0-9]+" end-of-line t)
2102 (setq num (string-to-int (buffer-substring (match-beginning 0)
2103 (match-end 0))))
2104 (cond ((looking-at "-") ; Message range
2105 (forward-char 1)
2106 (re-search-forward "[0-9]+" end-of-line t)
2107 (let ((num2 (string-to-int (buffer-substring (match-beginning 0)
2108 (match-end 0)))))
2109 (if (< num2 num)
2110 (error "Bad message range: %d-%d" num num2))
2111 (while (<= num num2)
2112 (setq msgs (cons num msgs))
2113 (setq num (1+ num)))))
2114 ((not (zerop num)) ;"pick" outputs "0" to mean no match
2115 (setq msgs (cons num msgs)))))
2116 msgs))
2117
2118 (defun mh-notate-user-sequences (&optional msg-or-seq)
2119 "Mark user-defined sequences in the messages specified by MSG-OR-SEQ.
2120 The optional argument MSG-OR-SEQ can be a message number, a list of message
2121 numbers, a sequence, a region in a cons cell, or nil in which case all
2122 messages in the folder buffer are notated."
2123 (unless msg-or-seq
2124 (setq msg-or-seq (cons (point-min) (point-max))))
2125 (let ((seqs mh-seq-list)
2126 (msg-hash (make-hash-table))
2127 (tick-msgs (and mh-tick-seq (mh-seq-msgs (mh-find-seq mh-tick-seq)))))
2128 (dolist (seq seqs)
2129 (unless (mh-internal-seq (mh-seq-name seq))
2130 (dolist (msg (mh-seq-msgs seq))
2131 (setf (gethash msg msg-hash) t))))
2132 (mh-iterate-on-msg-or-seq msg msg-or-seq
2133 (when (gethash msg msg-hash)
2134 (mh-notate nil mh-note-seq (1+ mh-cmd-note)))
2135 (mh-notate-tick msg tick-msgs))))
2136
2137 (defun mh-internal-seq (name)
2138 "Return non-nil if NAME is the name of an internal MH-E sequence."
2139 (or (memq name '(answered cur deleted forwarded printed))
2140 (eq name mh-unseen-seq)
2141 (and mh-tick-seq (eq name mh-tick-seq))
2142 (eq name mh-previous-seq)
2143 (mh-folder-name-p name)))
2144
2145 (defun mh-delete-msg-from-seq (msg-or-seq sequence &optional internal-flag)
2146 "Delete MSG-OR-SEQ from SEQUENCE.
2147 Default value of MSG-OR-SEQ is the displayed message.
2148 If optional prefix argument is provided, then prompt for the message sequence.
2149 If variable `transient-mark-mode' is non-nil and the mark is active, then the
2150 selected region is deleted from SEQUENCE..
2151 In a program, MSG-OR-SEQ can be a message number, a list of message numbers, a
2152 region in a cons cell, or a sequence; optional third arg INTERNAL-FLAG non-nil
2153 means do not inform MH of the change."
2154 (interactive (list (mh-interactive-msg-or-seq "Delete")
2155 (mh-read-seq-default "Delete from" t)
2156 nil))
2157 (let ((entry (mh-find-seq sequence)))
2158 (when entry
2159 (mh-iterate-on-msg-or-seq msg msg-or-seq
2160 (when (memq msg (mh-seq-msgs entry))
2161 (mh-notate nil ? (1+ mh-cmd-note)))
2162 (mh-delete-a-msg-from-seq msg sequence internal-flag)
2163 (mh-clear-text-properties nil))
2164 (mh-notate-user-sequences msg-or-seq)
2165 (when (and (eq sequence mh-unseen-seq) (mh-speed-flists-active-p))
2166 (mh-speed-flists t mh-current-folder)))))
2167
2168 (defun mh-delete-a-msg-from-seq (msg sequence internal-flag)
2169 "Delete MSG from SEQUENCE.
2170 If INTERNAL-FLAG is non-nil, then do not inform MH of the change."
2171 (let ((entry (mh-find-seq sequence)))
2172 (when (and entry (memq msg (mh-seq-msgs entry)))
2173 (if (not internal-flag)
2174 (mh-undefine-sequence sequence (list msg)))
2175 (setcdr entry (delq msg (mh-seq-msgs entry))))))
2176
2177 (defun mh-clear-text-properties (message)
2178 "Clear all text properties (except mh-tick) from the scan line for MESSAGE."
2179 (save-excursion
2180 (with-mh-folder-updating (t)
2181 (when (or (not message) (mh-goto-msg message t t))
2182 (beginning-of-line)
2183 (let ((tick-property (get-text-property (point) 'mh-tick)))
2184 (set-text-properties (point) (line-end-position) nil)
2185 (when tick-property
2186 (add-text-properties (point) (line-end-position)
2187 `(mh-tick ,tick-property))))))))
2188
2189 (defun mh-undefine-sequence (seq msgs)
2190 "Remove from the SEQ the list of MSGS."
2191 (prog1 (mh-exec-cmd "mark" mh-current-folder "-delete"
2192 "-sequence" (symbol-name seq)
2193 (mh-coalesce-msg-list msgs))
2194 (when (and (eq seq mh-unseen-seq) (mh-speed-flists-active-p))
2195 (mh-speed-flists t mh-current-folder))))
2196
2197 (defun mh-define-sequence (seq msgs)
2198 "Define the SEQ to contain the list of MSGS.
2199 Do not mark pseudo-sequences or empty sequences.
2200 Signals an error if SEQ is an illegal name."
2201 (if (and msgs
2202 (not (mh-folder-name-p seq)))
2203 (save-excursion
2204 (mh-exec-cmd-error nil "mark" mh-current-folder "-add" "-zero"
2205 "-sequence" (symbol-name seq)
2206 (mh-coalesce-msg-list msgs)))))
2207
2208 (defun mh-map-over-seqs (function seq-list)
2209 "Apply FUNCTION to each sequence in SEQ-LIST.
2210 The sequence name and the list of messages are passed as arguments."
2211 (while seq-list
2212 (funcall function
2213 (mh-seq-name (car seq-list))
2214 (mh-seq-msgs (car seq-list)))
2215 (setq seq-list (cdr seq-list))))
2216
2217 (defun mh-notate-if-in-one-seq (msg character offset seq)
2218 "Notate MSG.
2219 The CHARACTER is placed at the given OFFSET from the beginning of the listing.
2220 The notation is performed if the MSG is only in SEQ."
2221 (let ((in-seqs (mh-seq-containing-msg msg nil)))
2222 (if (and (eq seq (car in-seqs)) (null (cdr in-seqs)))
2223 (mh-notate msg character offset))))
2224
2225 (defun mh-seq-containing-msg (msg &optional include-internal-flag)
2226 "Return a list of the sequences containing MSG.
2227 If INCLUDE-INTERNAL-FLAG non-nil, include MH-E internal sequences in list."
2228 (let ((l mh-seq-list)
2229 (seqs ()))
2230 (while l
2231 (and (memq msg (mh-seq-msgs (car l)))
2232 (or include-internal-flag
2233 (not (mh-internal-seq (mh-seq-name (car l)))))
2234 (setq seqs (cons (mh-seq-name (car l)) seqs)))
2235 (setq l (cdr l)))
2236 seqs))
2237
2238 \f
2239
2240 ;;; User prompting commands.
2241
2242 (defun mh-read-msg-range (folder &optional always-prompt-flag)
2243 "Prompt for message range from FOLDER.
2244 If optional second argument ALWAYS-PROMPT-FLAG is non-nil then always ask for
2245 range."
2246 (multiple-value-bind (total unseen) (mh-folder-size folder)
2247 (cond
2248 ((and (not always-prompt-flag) (numberp unseen) (> unseen 0))
2249 (list (symbol-name mh-unseen-seq)))
2250 ((or (null mh-large-folder) (not (numberp total)))
2251 (list "all"))
2252 ((and (numberp total) (or always-prompt-flag (> total mh-large-folder)))
2253 (let* ((prompt
2254 (format "Range or number of messages to read (default: %s): "
2255 total))
2256 (in (read-string prompt nil nil (number-to-string total))))
2257 (cond ((string-match "^[ \f\t\n\r\v]*[0-9]+[ \f\t\n\r\v]*$" in)
2258 (list (format "last:%s" (car (read-from-string in)))))
2259 ((equal in "") (list "all"))
2260 (t (split-string in)))))
2261 (t (list "all")))))
2262
2263 \f
2264
2265 ;;; Build the folder-mode keymap:
2266
2267 (suppress-keymap mh-folder-mode-map)
2268
2269 ;; Use defalias to make sure the documented primary key bindings
2270 ;; appear in menu lists.
2271 (defalias 'mh-alt-show 'mh-show)
2272 (defalias 'mh-alt-refile-msg 'mh-refile-msg)
2273 (defalias 'mh-alt-send 'mh-send)
2274 (defalias 'mh-alt-visit-folder 'mh-visit-folder)
2275
2276 ;; Save the `b' binding for a future `back'. Maybe?
2277 (gnus-define-keys mh-folder-mode-map
2278 " " mh-page-msg
2279 "!" mh-refile-or-write-again
2280 "'" mh-toggle-tick
2281 "," mh-header-display
2282 "." mh-alt-show
2283 ">" mh-write-msg-to-file
2284 "?" mh-help
2285 "E" mh-extract-rejected-mail
2286 "M" mh-modify
2287 "\177" mh-previous-page
2288 "\C-d" mh-delete-msg-no-motion
2289 "\t" mh-index-next-folder
2290 [backtab] mh-index-previous-folder
2291 "\M-\t" mh-index-previous-folder
2292 "\e<" mh-first-msg
2293 "\e>" mh-last-msg
2294 "\ed" mh-redistribute
2295 "\r" mh-show
2296 "^" mh-alt-refile-msg
2297 "c" mh-copy-msg
2298 "d" mh-delete-msg
2299 "e" mh-edit-again
2300 "f" mh-forward
2301 "g" mh-goto-msg
2302 "i" mh-inc-folder
2303 "k" mh-delete-subject-or-thread
2304 "l" mh-print-msg
2305 "m" mh-alt-send
2306 "n" mh-next-undeleted-msg
2307 "\M-n" mh-next-unread-msg
2308 "o" mh-refile-msg
2309 "p" mh-previous-undeleted-msg
2310 "\M-p" mh-previous-unread-msg
2311 "q" mh-quit
2312 "r" mh-reply
2313 "s" mh-send
2314 "t" mh-toggle-showing
2315 "u" mh-undo
2316 "v" mh-index-visit-folder
2317 "x" mh-execute-commands
2318 "|" mh-pipe-msg)
2319
2320 (gnus-define-keys (mh-folder-map "F" mh-folder-mode-map)
2321 "?" mh-prefix-help
2322 "S" mh-sort-folder
2323 "f" mh-alt-visit-folder
2324 "i" mh-index-search
2325 "k" mh-kill-folder
2326 "l" mh-list-folders
2327 "n" mh-index-new-messages
2328 "o" mh-alt-visit-folder
2329 "p" mh-pack-folder
2330 "r" mh-rescan-folder
2331 "s" mh-search-folder
2332 "u" mh-undo-folder
2333 "v" mh-visit-folder)
2334
2335 (define-key mh-folder-mode-map "I" mh-inc-spool-map)
2336
2337 (gnus-define-keys (mh-junk-map "J" mh-folder-mode-map)
2338 "?" mh-prefix-help
2339 "b" mh-junk-blacklist
2340 "w" mh-junk-whitelist)
2341
2342 (gnus-define-keys (mh-sequence-map "S" mh-folder-mode-map)
2343 "?" mh-prefix-help
2344 "d" mh-delete-msg-from-seq
2345 "k" mh-delete-seq
2346 "l" mh-list-sequences
2347 "n" mh-narrow-to-seq
2348 "p" mh-put-msg-in-seq
2349 "s" mh-msg-is-in-seq
2350 "w" mh-widen)
2351
2352 (gnus-define-keys (mh-thread-map "T" mh-folder-mode-map)
2353 "?" mh-prefix-help
2354 "u" mh-thread-ancestor
2355 "p" mh-thread-previous-sibling
2356 "n" mh-thread-next-sibling
2357 "t" mh-toggle-threads
2358 "d" mh-thread-delete
2359 "o" mh-thread-refile)
2360
2361 (gnus-define-keys (mh-limit-map "/" mh-folder-mode-map)
2362 "'" mh-narrow-to-tick
2363 "?" mh-prefix-help
2364 "s" mh-narrow-to-subject
2365 "w" mh-widen)
2366
2367 (gnus-define-keys (mh-extract-map "X" mh-folder-mode-map)
2368 "?" mh-prefix-help
2369 "s" mh-store-msg ;shar
2370 "u" mh-store-msg) ;uuencode
2371
2372 (gnus-define-keys (mh-digest-map "D" mh-folder-mode-map)
2373 " " mh-page-digest
2374 "?" mh-prefix-help
2375 "\177" mh-page-digest-backwards
2376 "b" mh-burst-digest)
2377
2378 (gnus-define-keys (mh-mime-map "K" mh-folder-mode-map)
2379 "?" mh-prefix-help
2380 "a" mh-mime-save-parts
2381 "i" mh-folder-inline-mime-part
2382 "o" mh-folder-save-mime-part
2383 "v" mh-folder-toggle-mime-part
2384 "\t" mh-next-button
2385 [backtab] mh-prev-button
2386 "\M-\t" mh-prev-button)
2387
2388 (cond
2389 (mh-xemacs-flag
2390 (define-key mh-folder-mode-map [button2] 'mh-show-mouse))
2391 (t
2392 (define-key mh-folder-mode-map [mouse-2] 'mh-show-mouse)))
2393
2394 ;; "C-c /" prefix is used in mh-folder-mode by pgp.el and mailcrypt
2395
2396 \f
2397
2398 ;;; Help Messages
2399
2400 ;;; If you add a new prefix, add appropriate text to the nil key.
2401 ;;;
2402 ;;; In general, messages are grouped logically. Taking the main commands for
2403 ;;; example, the first line is "ways to view messages," the second line is
2404 ;;; "things you can do with messages", and the third is "composing" messages.
2405 ;;;
2406 ;;; When adding a new prefix, ensure that the help message contains "what" the
2407 ;;; prefix is for. For example, if the word "folder" were not present in the
2408 ;;; `F' entry, it would not be clear what these commands operated upon.
2409 (defvar mh-help-messages
2410 '((nil "[i]nc, [.]show, [,]show all, [n]ext, [p]revious,\n"
2411 "[d]elete, [o]refile, e[x]ecute,\n"
2412 "[s]end, [r]eply.\n"
2413 "Prefix characters:\n [F]older, [S]equence, [J]unk, MIME [K]eys,"
2414 "\n [T]hread, / Limit, e[X]tract, [D]igest, [I]nc spools.")
2415
2416 (?F "[l]ist, [v]isit folder;\n"
2417 "[t]hread; [s]earch; [i]ndexed search;\n"
2418 "[p]ack; [S]ort; [r]escan; [k]ill")
2419 (?S "[p]ut message in sequence, [n]arrow, [w]iden,\n"
2420 "[s]equences, [l]ist,\n"
2421 "[d]elete message from sequence, [k]ill sequence")
2422 (?T "[t]oggle, [d]elete, [o]refile thread")
2423 (?/ "Limit to [s]ubject; [w]iden")
2424 (?X "un[s]har, [u]udecode message")
2425 (?D "[b]urst digest")
2426 (?K "[v]iew, [i]nline, [o]utput/save MIME part; save [a]ll parts; \n"
2427 "[TAB] next; [SHIFT-TAB] previous")
2428 (?J "[b]lacklist, [w]hitelist message"))
2429 "Key binding cheat sheet.
2430
2431 This is an associative array which is used to show the most common commands.
2432 The key is a prefix char. The value is one or more strings which are
2433 concatenated together and displayed in the minibuffer if ? is pressed after
2434 the prefix character. The special key nil is used to display the
2435 non-prefixed commands.
2436
2437 The substitutions described in `substitute-command-keys' are performed as
2438 well.")
2439
2440 \f
2441
2442 (dolist (mess '("^Cursor not pointing to message$"
2443 "^There is no other window$"))
2444 (add-to-list 'debug-ignored-errors mess))
2445
2446 ;;;; Desktop support
2447
2448 ;;;###autoload
2449 (defun mh-restore-desktop-buffer (desktop-buffer-file-name
2450 desktop-buffer-name
2451 desktop-buffer-misc)
2452 "Restore an mh folder buffer specified in a desktop file."
2453 (mh-find-path)
2454 (mh-visit-folder desktop-buffer-name)
2455 (current-buffer))
2456
2457 (provide 'mh-e)
2458
2459 ;;; Local Variables:
2460 ;;; indent-tabs-mode: nil
2461 ;;; sentence-end-double-space: nil
2462 ;;; End:
2463
2464 ;;; arch-tag: cce884de-bd37-4104-9963-e4439d5ed22b
2465 ;;; mh-e.el ends here