Merge from emacs--rel--22
[bpt/emacs.git] / lisp / mh-e / mh-folder.el
1 ;;; mh-folder.el --- MH-Folder mode
2
3 ;; Copyright (C) 2002, 2003, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
4
5 ;; Author: Bill Wohler <wohler@newt.com>
6 ;; Maintainer: Bill Wohler <wohler@newt.com>
7 ;; Keywords: mail
8 ;; See: mh-e.el
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24
25 ;;; Commentary:
26
27 ;; Mode for browsing folders
28
29 ;;; Change Log:
30
31 ;;; Code:
32
33 (require 'mh-e)
34 (require 'mh-scan)
35 (mh-require-cl)
36
37 ;; Dynamically-created functions not found in mh-loaddefs.el.
38 (autoload 'mh-tool-bar-folder-buttons-init "mh-tool-bar")
39 (autoload 'mh-tool-bar-init "mh-tool-bar")
40
41 (require 'gnus-util)
42 (autoload 'message-fetch-field "message")
43
44 \f
45
46 ;;; MH-E Entry Points
47
48 ;;;###autoload
49 (defun mh-rmail (&optional arg)
50 "Incorporate new mail with MH.
51 Scan an MH folder if ARG is non-nil.
52
53 This function is an entry point to MH-E, the Emacs interface to
54 the MH mail system."
55 (interactive "P")
56 (mh-find-path)
57 (if arg
58 (call-interactively 'mh-visit-folder)
59 (unless (get-buffer mh-inbox)
60 (mh-visit-folder mh-inbox (symbol-name mh-unseen-seq)))
61 (mh-inc-folder)))
62
63 ;;;###autoload
64 (defun mh-nmail (&optional arg)
65 "Check for new mail in inbox folder.
66 Scan an MH folder if ARG is non-nil.
67
68 This function is an entry point to MH-E, the Emacs interface to
69 the MH mail system."
70 (interactive "P")
71 (mh-find-path) ; init mh-inbox
72 (if arg
73 (call-interactively 'mh-visit-folder)
74 (mh-visit-folder mh-inbox)))
75 \f
76
77 ;;; Desktop Integration
78
79 ;; desktop-buffer-mode-handlers appeared in Emacs 22.
80 (if (fboundp 'desktop-buffer-mode-handlers)
81 (add-to-list 'desktop-buffer-mode-handlers
82 '(mh-folder-mode . mh-restore-desktop-buffer)))
83
84 (defun mh-restore-desktop-buffer (desktop-buffer-file-name
85 desktop-buffer-name
86 desktop-buffer-misc)
87 "Restore an MH folder buffer specified in a desktop file.
88 When desktop creates a buffer, DESKTOP-BUFFER-FILE-NAME holds the
89 file name to visit, DESKTOP-BUFFER-NAME holds the desired buffer
90 name, and DESKTOP-BUFFER-MISC holds a list of miscellaneous info
91 used by the `desktop-buffer-handlers' functions."
92 (mh-find-path)
93 (mh-visit-folder desktop-buffer-name)
94 (current-buffer))
95
96 \f
97
98 ;;; Variables
99
100 (defvar mh-folder-filename nil
101 "Full path of directory for this folder.")
102
103 (defvar mh-partial-folder-mode-line-annotation "select"
104 "Annotation when displaying part of a folder.
105 The string is displayed after the folder's name. nil for no
106 annotation.")
107
108 (defvar mh-last-destination nil
109 "Destination of last refile or write command.")
110
111 (defvar mh-last-destination-folder nil
112 "Destination of last refile command.")
113
114 (defvar mh-last-destination-write nil
115 "Destination of last write command.")
116
117 (defvar mh-first-msg-num nil
118 "Number of first message in buffer.")
119
120 (defvar mh-last-msg-num nil
121 "Number of last msg in buffer.")
122
123 (defvar mh-msg-count nil
124 "Number of msgs in buffer.")
125
126 \f
127
128 ;;; Sequence Menu
129
130 (easy-menu-define
131 mh-folder-sequence-menu mh-folder-mode-map "Menu for MH-E folder-sequence."
132 '("Sequence"
133 ["Add Message to Sequence..." mh-put-msg-in-seq (mh-get-msg-num nil)]
134 ["List Sequences for Message" mh-msg-is-in-seq (mh-get-msg-num nil)]
135 ["Delete Message from Sequence..." mh-delete-msg-from-seq
136 (mh-get-msg-num nil)]
137 ["List Sequences in Folder..." mh-list-sequences t]
138 ["Delete Sequence..." mh-delete-seq t]
139 ["Narrow to Sequence..." mh-narrow-to-seq t]
140 ["Widen from Sequence" mh-widen mh-folder-view-stack]
141 "--"
142 ["Narrow to Subject Sequence" mh-narrow-to-subject t]
143 ["Narrow to Tick Sequence" mh-narrow-to-tick
144 (and mh-tick-seq (mh-seq-msgs (mh-find-seq mh-tick-seq)))]
145 ["Delete Rest of Same Subject" mh-delete-subject t]
146 ["Toggle Tick Mark" mh-toggle-tick t]
147 "--"
148 ["Push State Out to MH" mh-update-sequences t]))
149
150 ;;; Message Menu
151
152 (easy-menu-define
153 mh-folder-message-menu mh-folder-mode-map "Menu for MH-E folder-message."
154 '("Message"
155 ["Show Message" mh-show (mh-get-msg-num nil)]
156 ["Show Message with Header" mh-header-display (mh-get-msg-num nil)]
157 ["Show Message with Preferred Alternative"
158 mh-show-preferred-alternative (mh-get-msg-num nil)]
159 ["Next Message" mh-next-undeleted-msg t]
160 ["Previous Message" mh-previous-undeleted-msg t]
161 ["Go to First Message" mh-first-msg t]
162 ["Go to Last Message" mh-last-msg t]
163 ["Go to Message by Number..." mh-goto-msg t]
164 ["Modify Message" mh-modify t]
165 ["Delete Message" mh-delete-msg (mh-get-msg-num nil)]
166 ["Refile Message" mh-refile-msg (mh-get-msg-num nil)]
167 ["Undo Delete/Refile" mh-undo (mh-outstanding-commands-p)]
168 ["Execute Delete/Refile" mh-execute-commands
169 (mh-outstanding-commands-p)]
170 "--"
171 ["Compose a New Message" mh-send t]
172 ["Reply to Message..." mh-reply (mh-get-msg-num nil)]
173 ["Forward Message..." mh-forward (mh-get-msg-num nil)]
174 ["Redistribute Message..." mh-redistribute (mh-get-msg-num nil)]
175 ["Edit Message Again" mh-edit-again (mh-get-msg-num nil)]
176 ["Re-edit a Bounced Message" mh-extract-rejected-mail t]
177 "--"
178 ["Copy Message to Folder..." mh-copy-msg (mh-get-msg-num nil)]
179 ["Print Message" mh-print-msg (mh-get-msg-num nil)]
180 ["Write Message to File..." mh-write-msg-to-file
181 (mh-get-msg-num nil)]
182 ["Pipe Message to Command..." mh-pipe-msg (mh-get-msg-num nil)]
183 ["Unpack Uuencoded Message..." mh-store-msg (mh-get-msg-num nil)]
184 ["Burst Digest Message" mh-burst-digest (mh-get-msg-num nil)]))
185
186 ;;; Folder Menu
187
188 (easy-menu-define
189 mh-folder-folder-menu mh-folder-mode-map "Menu for MH-E folder."
190 '("Folder"
191 ["Incorporate New Mail" mh-inc-folder t]
192 ["Toggle Show/Folder" mh-toggle-showing t]
193 ["Execute Delete/Refile" mh-execute-commands
194 (mh-outstanding-commands-p)]
195 ["Rescan Folder" mh-rescan-folder t]
196 ["Thread Folder" mh-toggle-threads
197 (not (memq 'unthread mh-view-ops))]
198 ["Pack Folder" mh-pack-folder t]
199 ["Sort Folder" mh-sort-folder t]
200 "--"
201 ["List Folders" mh-list-folders t]
202 ["Visit a Folder..." mh-visit-folder t]
203 ["View New Messages" mh-index-new-messages t]
204 ["Search..." mh-search t]
205 "--"
206 ["Quit MH-E" mh-quit t]))
207
208 \f
209
210 ;;; MH-Folder Keys
211
212 (suppress-keymap mh-folder-mode-map)
213
214 ;; Use defalias to make sure the documented primary key bindings
215 ;; appear in menu lists.
216 (defalias 'mh-alt-show 'mh-show)
217 (defalias 'mh-alt-refile-msg 'mh-refile-msg)
218 (defalias 'mh-alt-send 'mh-send)
219 (defalias 'mh-alt-visit-folder 'mh-visit-folder)
220
221 ;; Save the "b" binding for a future `back'. Maybe?
222 (gnus-define-keys mh-folder-mode-map
223 " " mh-page-msg
224 "!" mh-refile-or-write-again
225 "'" mh-toggle-tick
226 "," mh-header-display
227 "." mh-alt-show
228 ":" mh-show-preferred-alternative
229 ";" mh-toggle-mh-decode-mime-flag
230 ">" mh-write-msg-to-file
231 "?" mh-help
232 "E" mh-extract-rejected-mail
233 "M" mh-modify
234 "\177" mh-previous-page
235 "\C-d" mh-delete-msg-no-motion
236 "\t" mh-index-next-folder
237 [backtab] mh-index-previous-folder
238 "\M-\t" mh-index-previous-folder
239 "\e<" mh-first-msg
240 "\e>" mh-last-msg
241 "\ed" mh-redistribute
242 "\r" mh-show
243 "^" mh-alt-refile-msg
244 "c" mh-copy-msg
245 "d" mh-delete-msg
246 "e" mh-edit-again
247 "f" mh-forward
248 "g" mh-goto-msg
249 "i" mh-inc-folder
250 "k" mh-delete-subject-or-thread
251 "m" mh-alt-send
252 "n" mh-next-undeleted-msg
253 "\M-n" mh-next-unread-msg
254 "o" mh-refile-msg
255 "p" mh-previous-undeleted-msg
256 "\M-p" mh-previous-unread-msg
257 "q" mh-quit
258 "r" mh-reply
259 "s" mh-send
260 "t" mh-toggle-showing
261 "u" mh-undo
262 "v" mh-index-visit-folder
263 "x" mh-execute-commands
264 "|" mh-pipe-msg)
265
266 (gnus-define-keys (mh-folder-map "F" mh-folder-mode-map)
267 "?" mh-prefix-help
268 "'" mh-index-ticked-messages
269 "S" mh-sort-folder
270 "c" mh-catchup
271 "f" mh-alt-visit-folder
272 "k" mh-kill-folder
273 "l" mh-list-folders
274 "n" mh-index-new-messages
275 "o" mh-alt-visit-folder
276 "p" mh-pack-folder
277 "q" mh-index-sequenced-messages
278 "r" mh-rescan-folder
279 "s" mh-search
280 "u" mh-undo-folder
281 "v" mh-visit-folder)
282
283 (define-key mh-folder-mode-map "I" mh-inc-spool-map)
284
285 (gnus-define-keys (mh-junk-map "J" mh-folder-mode-map)
286 "?" mh-prefix-help
287 "b" mh-junk-blacklist
288 "w" mh-junk-whitelist)
289
290 (gnus-define-keys (mh-ps-print-map "P" mh-folder-mode-map)
291 "?" mh-prefix-help
292 "C" mh-ps-print-toggle-color
293 "F" mh-ps-print-toggle-faces
294 "f" mh-ps-print-msg-file
295 "l" mh-print-msg
296 "p" mh-ps-print-msg)
297
298 (gnus-define-keys (mh-sequence-map "S" mh-folder-mode-map)
299 "'" mh-narrow-to-tick
300 "?" mh-prefix-help
301 "d" mh-delete-msg-from-seq
302 "k" mh-delete-seq
303 "l" mh-list-sequences
304 "n" mh-narrow-to-seq
305 "p" mh-put-msg-in-seq
306 "s" mh-msg-is-in-seq
307 "w" mh-widen)
308
309 (gnus-define-keys (mh-thread-map "T" mh-folder-mode-map)
310 "?" mh-prefix-help
311 "u" mh-thread-ancestor
312 "p" mh-thread-previous-sibling
313 "n" mh-thread-next-sibling
314 "t" mh-toggle-threads
315 "d" mh-thread-delete
316 "o" mh-thread-refile)
317
318 (gnus-define-keys (mh-limit-map "/" mh-folder-mode-map)
319 "'" mh-narrow-to-tick
320 "?" mh-prefix-help
321 "c" mh-narrow-to-cc
322 "g" mh-narrow-to-range
323 "m" mh-narrow-to-from
324 "s" mh-narrow-to-subject
325 "t" mh-narrow-to-to
326 "w" mh-widen)
327
328 (gnus-define-keys (mh-extract-map "X" mh-folder-mode-map)
329 "?" mh-prefix-help
330 "s" mh-store-msg ;shar
331 "u" mh-store-msg) ;uuencode
332
333 (gnus-define-keys (mh-digest-map "D" mh-folder-mode-map)
334 " " mh-page-digest
335 "?" mh-prefix-help
336 "\177" mh-page-digest-backwards
337 "b" mh-burst-digest)
338
339 (gnus-define-keys (mh-mime-map "K" mh-folder-mode-map)
340 "?" mh-prefix-help
341 "a" mh-mime-save-parts
342 "e" mh-display-with-external-viewer
343 "i" mh-folder-inline-mime-part
344 "o" mh-folder-save-mime-part
345 "t" mh-toggle-mime-buttons
346 "v" mh-folder-toggle-mime-part
347 "\t" mh-next-button
348 [backtab] mh-prev-button
349 "\M-\t" mh-prev-button)
350
351 (cond
352 ((featurep 'xemacs)
353 (define-key mh-folder-mode-map [button2] 'mh-show-mouse))
354 (t
355 (define-key mh-folder-mode-map [mouse-2] 'mh-show-mouse)))
356
357 ;; "C-c /" prefix is used in mh-folder-mode by pgp.el and mailcrypt
358
359 \f
360
361 ;;; MH-Folder Help Messages
362
363 ;; If you add a new prefix, add appropriate text to the nil key.
364
365 ;; In general, messages are grouped logically. Taking the main commands for
366 ;; example, the first line is "ways to view messages," the second line is
367 ;; "things you can do with messages", and the third is "composing" messages.
368
369 ;; When adding a new prefix, ensure that the help message contains "what" the
370 ;; prefix is for. For example, if the word "folder" were not present in the
371 ;; "F" entry, it would not be clear what these commands operated upon.
372 (defvar mh-folder-mode-help-messages
373 '((nil "[i]nc, [.]show, [,]show all, [n]ext, [p]revious,\n"
374 "[d]elete, [o]refile, e[x]ecute,\n"
375 "[s]end, [r]eply,\n"
376 "[;]toggle MIME decoding.\n"
377 "Prefix characters:\n [F]older, [S]equence, [J]unk, MIME [K]eys,"
378 "\n [T]hread, [/]limit, e[X]tract, [D]igest, [I]nc spools.")
379
380 (?F "[l]ist; [v]isit folder;\n"
381 "[n]ew messages; [']ticked messages; [s]earch;\n"
382 "[p]ack; [S]ort; [r]escan; [k]ill")
383 (?P "[p]rint message to [f]ile; old-style [l]pr printing;\n"
384 "Toggle printing of [C]olors, [F]aces")
385 (?S "[p]ut message in sequence, [n]arrow, [']narrow to ticked, [w]iden,\n"
386 "[s]equences, [l]ist,\n"
387 "[d]elete message from sequence, [k]ill sequence")
388 (?T "[t]oggle, [d]elete, [o]refile thread")
389 (?/ "Limit to [c]c, ran[g]e, fro[m], [s]ubject, [t]o; [w]iden")
390 (?X "un[s]har, [u]udecode message")
391 (?D "[b]urst digest")
392 (?K "[v]iew, [i]nline, [o]utput/save MIME part; save [a]ll parts; \n"
393 "[TAB] next; [SHIFT-TAB] previous")
394 (?J "[b]lacklist, [w]hitelist message"))
395 "Key binding cheat sheet.
396 See `mh-set-help'.")
397
398 \f
399
400 ;;; MH-Folder Font Lock
401
402 (defvar mh-folder-font-lock-keywords
403 (list
404 ;; Folders when displaying index buffer
405 (list "^\\+.*"
406 '(0 'mh-search-folder))
407 ;; Marked for deletion
408 (list (concat mh-scan-deleted-msg-regexp ".*")
409 '(0 'mh-folder-deleted))
410 ;; Marked for refile
411 (list (concat mh-scan-refiled-msg-regexp ".*")
412 '(0 'mh-folder-refiled))
413 ;; After subject
414 (list mh-scan-body-regexp
415 '(1 'mh-folder-body nil t))
416 ;; Subject
417 '(mh-folder-font-lock-subject
418 (1 'mh-folder-followup append t)
419 (2 'mh-folder-subject append t))
420 ;; Current message number
421 (list mh-scan-cur-msg-number-regexp
422 '(1 'mh-folder-cur-msg-number))
423 ;; Message number
424 (list mh-scan-good-msg-regexp
425 '(1 'mh-folder-msg-number))
426 ;; Date
427 (list mh-scan-date-regexp
428 '(1 'mh-folder-date))
429 ;; Messages from me (To:)
430 (list mh-scan-rcpt-regexp
431 '(1 'mh-folder-to)
432 '(2 'mh-folder-address))
433 ;; Messages to me
434 (list mh-scan-sent-to-me-sender-regexp
435 '(1 'mh-folder-sent-to-me-hint)
436 '(2 'mh-folder-sent-to-me-sender)))
437 "Keywords (regular expressions) used to fontify the MH-Folder buffer.")
438
439 (defun mh-folder-font-lock-subject (limit)
440 "Return MH-E scan subject strings to font-lock between point and LIMIT."
441 (if (not (re-search-forward mh-scan-subject-regexp limit t))
442 nil
443 (if (match-beginning 1)
444 (set-match-data (list (match-beginning 1) (match-end 3)
445 (match-beginning 1) (match-end 3) nil nil))
446 (set-match-data (list (match-beginning 3) (match-end 3)
447 nil nil (match-beginning 3) (match-end 3))))
448 t))
449
450 ;; Fontify unseen messages in bold.
451
452 (defmacro mh-generate-sequence-font-lock (seq prefix face)
453 "Generate the appropriate code to fontify messages in SEQ.
454 PREFIX is used to generate unique names for the variables and
455 functions defined by the macro. So a different prefix should be
456 provided for every invocation.
457 FACE is the font-lock face used to display the matching scan lines."
458 (let ((cache (intern (format "mh-folder-%s-seq-cache" prefix)))
459 (func (intern (format "mh-folder-font-lock-%s" prefix))))
460 `(progn
461 (defvar ,cache nil
462 "Internal cache variable used for font-lock in MH-E.
463 Should only be non-nil through font-lock stepping, and nil once
464 font-lock is done highlighting.")
465 (make-variable-buffer-local ',cache)
466
467 (defun ,func (limit)
468 "Return unseen message lines to font-lock between point and LIMIT."
469 (if (not ,cache) (setq ,cache (mh-seq-msgs (mh-find-seq ,seq))))
470 (let ((cur-msg (mh-get-msg-num nil)))
471 (cond ((not ,cache)
472 nil)
473 ((>= (point) limit) ;Presumably at end of buffer
474 (setq ,cache nil)
475 nil)
476 ((member cur-msg ,cache)
477 (let ((bpoint (progn (beginning-of-line)(point)))
478 (epoint (progn (forward-line 1)(point))))
479 (if (<= limit (point)) (setq ,cache nil))
480 (set-match-data (list bpoint epoint bpoint epoint))
481 t))
482 (t
483 ;; move forward one line at a time, checking each message
484 (while (and (= 0 (forward-line 1))
485 (> limit (point))
486 (not (member (mh-get-msg-num nil) ,cache))))
487 ;; Examine how we must have exited the loop...
488 (let ((cur-msg (mh-get-msg-num nil)))
489 (cond ((or (<= limit (point))
490 (not (member cur-msg ,cache)))
491 (setq ,cache nil)
492 nil)
493 ((member cur-msg ,cache)
494 (let ((bpoint (progn (beginning-of-line) (point)))
495 (epoint (progn (forward-line 1) (point))))
496 (if (<= limit (point)) (setq ,cache nil))
497 (set-match-data
498 (list bpoint epoint bpoint epoint))
499 t))))))))
500
501 (setq mh-folder-font-lock-keywords
502 (append mh-folder-font-lock-keywords
503 (list (list ',func (list 1 '',face 'prepend t))))))))
504
505 (mh-generate-sequence-font-lock mh-unseen-seq unseen bold)
506 (mh-generate-sequence-font-lock mh-tick-seq tick mh-folder-tick)
507
508 \f
509
510 ;;; MH-Folder Mode
511
512 (defmacro mh-remove-xemacs-horizontal-scrollbar ()
513 "Get rid of the horizontal scrollbar that XEmacs insists on putting in."
514 (when (featurep 'xemacs)
515 `(if (and (featurep 'scrollbar)
516 (fboundp 'set-specifier))
517 (set-specifier horizontal-scrollbar-visible-p nil
518 (cons (current-buffer) nil)))))
519
520 ;; Register mh-folder-mode as supporting which-function-mode...
521 (mh-require 'which-func nil t)
522 (when (boundp 'which-func-modes)
523 (add-to-list 'which-func-modes 'mh-folder-mode))
524
525 ;; Shush compiler.
526 (defvar desktop-save-buffer)
527 (defvar font-lock-auto-fontify)
528 (defvar font-lock-defaults) ; XEmacs
529
530 ;; Ensure new buffers won't get this mode if default-major-mode is nil.
531 (put 'mh-folder-mode 'mode-class 'special)
532
533 ;; Autoload cookie needed by desktop.el
534 ;;;###autoload
535 (define-derived-mode mh-folder-mode fundamental-mode "MH-Folder"
536 "Major MH-E mode for \"editing\" an MH folder scan listing.\\<mh-folder-mode-map>
537
538 You can show the message the cursor is pointing to, and step through
539 the messages. Messages can be marked for deletion or refiling into
540 another folder; these commands are executed all at once with a
541 separate command.
542
543 Options that control this mode can be changed with
544 \\[customize-group]; specify the \"mh\" group. In particular, please
545 see the `mh-scan-format-file' option if you wish to modify scan's
546 format.
547
548 When a folder is visited, the hook `mh-folder-mode-hook' is run.
549
550 Ranges
551 ======
552 Many commands that operate on individual messages, such as
553 `mh-forward' or `mh-refile-msg' take a RANGE argument. This argument
554 can be used in several ways.
555
556 If you provide the prefix argument (\\[universal-argument]) to
557 these commands, then you will be prompted for the message range.
558 This can be any valid MH range which can include messages,
559 sequences, and the abbreviations (described in the mh(1) man
560 page):
561
562 <num1>-<num2>
563 Indicates all messages in the range <num1> to <num2>, inclusive.
564 The range must be nonempty.
565
566 <num>:N
567 <num>:+N
568 <num>:-N
569 Up to N messages beginning with (or ending with) message num. Num
570 may be any of the predefined symbols: first, prev, cur, next or
571 last.
572
573 first:N
574 prev:N
575 next:N
576 last:N
577 The first, previous, next or last messages, if they exist.
578
579 all
580 All of the messages.
581
582 For example, a range that shows all of these things is `1 2 3
583 5-10 last:5 unseen'.
584
585 If the option `transient-mark-mode' is set to t and you set a
586 region in the MH-Folder buffer, then the MH-E command will
587 perform the operation on all messages in that region.
588
589 \\{mh-folder-mode-map}"
590 (mh-do-in-gnu-emacs
591 (unless mh-folder-tool-bar-map
592 (mh-tool-bar-folder-buttons-init))
593 (set (make-local-variable 'tool-bar-map) mh-folder-tool-bar-map))
594 (mh-do-in-xemacs
595 (mh-tool-bar-init :folder))
596 (make-local-variable 'font-lock-defaults)
597 (setq font-lock-defaults '(mh-folder-font-lock-keywords t))
598 (make-local-variable 'desktop-save-buffer)
599 (setq desktop-save-buffer t)
600 (mh-make-local-vars
601 'mh-colors-available-flag (mh-colors-available-p)
602 ; Do we have colors available
603 'mh-current-folder (buffer-name) ; Name of folder, a string
604 'mh-show-buffer (format "show-%s" (buffer-name)) ; Buffer that displays msgs
605 'mh-folder-filename ; e.g. "/usr/foobar/Mail/inbox/"
606 (file-name-as-directory (mh-expand-file-name (buffer-name)))
607 'mh-display-buttons-for-inline-parts-flag
608 mh-display-buttons-for-inline-parts-flag ; Allow for display of buttons to
609 ; be toggled.
610 'mh-arrow-marker (make-marker) ; Marker where arrow is displayed
611 'overlay-arrow-position nil ; Allow for simultaneous display in
612 'overlay-arrow-string ">" ; different MH-E buffers.
613 'mh-showing-mode nil ; Show message also?
614 'mh-delete-list nil ; List of msgs nums to delete
615 'mh-refile-list nil ; List of folder names in mh-seq-list
616 'mh-seq-list nil ; Alist of (seq . msgs) nums
617 'mh-seen-list nil ; List of displayed messages
618 'mh-next-direction 'forward ; Direction to move to next message
619 'mh-view-ops () ; Stack that keeps track of the order
620 ; in which narrowing/threading has been
621 ; carried out.
622 'mh-folder-view-stack () ; Stack of previous views of the
623 ; folder.
624 'mh-index-data nil ; If the folder was created by a call
625 ; to mh-search, this contains info
626 ; about the search results.
627 'mh-index-previous-search nil ; folder, indexer, search-regexp
628 'mh-index-msg-checksum-map nil ; msg -> checksum map
629 'mh-index-checksum-origin-map nil ; checksum -> ( orig-folder, orig-msg )
630 'mh-index-sequence-search-flag nil ; folder resulted from sequence search
631 'mh-first-msg-num nil ; Number of first msg in buffer
632 'mh-last-msg-num nil ; Number of last msg in buffer
633 'mh-msg-count nil ; Number of msgs in buffer
634 'mh-mode-line-annotation nil ; Indicates message range
635 'mh-sequence-notation-history (make-hash-table)
636 ; Remember what is overwritten by
637 ; mh-note-seq.
638 'imenu-create-index-function 'mh-index-create-imenu-index
639 ; Setup imenu support
640 'mh-previous-window-config nil) ; Previous window configuration
641 (mh-remove-xemacs-horizontal-scrollbar)
642 (setq truncate-lines t)
643 (auto-save-mode -1)
644 (setq buffer-offer-save t)
645 (mh-make-local-hook (mh-write-file-functions))
646 (add-hook (mh-write-file-functions) 'mh-execute-commands nil t)
647 (make-local-variable 'revert-buffer-function)
648 (make-local-variable 'hl-line-mode) ; avoid pollution
649 (mh-funcall-if-exists hl-line-mode 1)
650 (setq revert-buffer-function 'mh-undo-folder)
651 (add-to-list 'minor-mode-alist '(mh-showing-mode " Show"))
652 (easy-menu-add mh-folder-sequence-menu)
653 (easy-menu-add mh-folder-message-menu)
654 (easy-menu-add mh-folder-folder-menu)
655 (mh-inc-spool-make)
656 (mh-set-help mh-folder-mode-help-messages)
657 (if (and (featurep 'xemacs)
658 font-lock-auto-fontify)
659 (turn-on-font-lock))) ; Force font-lock in XEmacs.
660
661 \f
662
663 ;;; MH-Folder Commands
664
665 ;; Alphabetical.
666 ;; See also mh-comp.el, mh-junk.el, mh-mime.el, mh-print.el,
667 ;; mh-search.el, and mh-seq.el.
668
669 ;;;###mh-autoload
670 (defun mh-delete-msg (range)
671 "Delete RANGE\\<mh-folder-mode-map>.
672
673 To mark a message for deletion, use this command. A \"D\" is
674 placed by the message in the scan window, and the next undeleted
675 message is displayed. If the previous command had been
676 \\[mh-previous-undeleted-msg], then the next message displayed is
677 the first undeleted message previous to the message just deleted.
678 Use \\[mh-next-undeleted-msg] to force subsequent
679 \\[mh-delete-msg] commands to move forward to the next undeleted
680 message after deleting the message under the cursor.
681
682 The hook `mh-delete-msg-hook' is called after you mark a message
683 for deletion. For example, a past maintainer of MH-E used this
684 once when he kept statistics on his mail usage.
685
686 Check the documentation of `mh-interactive-range' to see how
687 RANGE is read in interactive use."
688 (interactive (list (mh-interactive-range "Delete")))
689 (mh-delete-msg-no-motion range)
690 (if (looking-at mh-scan-deleted-msg-regexp)
691 (mh-next-msg)))
692
693 ;;;###mh-autoload
694 (defun mh-delete-msg-no-motion (range)
695 "Delete RANGE, don't move to next message.
696
697 This command marks the RANGE for deletion but leaves the cursor
698 at the current message in case you wish to perform other
699 operations on the message.
700
701 Check the documentation of `mh-interactive-range' to see how
702 RANGE is read in interactive use."
703 (interactive (list (mh-interactive-range "Delete")))
704 (mh-iterate-on-range () range
705 (mh-delete-a-msg nil)))
706
707 ;;;###mh-autoload
708 (defun mh-execute-commands ()
709 "Process outstanding delete and refile requests\\<mh-folder-mode-map>.
710
711 If you've marked messages to be deleted or refiled and you want
712 to go ahead and delete or refile the messages, use this command.
713 Many MH-E commands that may affect the numbering of the
714 messages (such as \\[mh-rescan-folder] or \\[mh-pack-folder])
715 will ask if you want to process refiles or deletes first and then
716 either run this command for you or undo the pending refiles and
717 deletes.
718
719 This function runs `mh-before-commands-processed-hook' before the
720 commands are processed and `mh-after-commands-processed-hook'
721 after the commands are processed."
722 (interactive)
723 (if mh-folder-view-stack (mh-widen t))
724 (mh-process-commands mh-current-folder)
725 (mh-set-scan-mode)
726 (mh-goto-cur-msg) ; after mh-set-scan-mode for efficiency
727 (mh-make-folder-mode-line)
728 t) ; return t for write-file-functions
729
730 ;;;###mh-autoload
731 (defun mh-first-msg ()
732 "Display first message."
733 (interactive)
734 (goto-char (point-min))
735 (while (and (not (eobp)) (not (looking-at mh-scan-valid-regexp)))
736 (forward-line 1)))
737
738 ;;;###mh-autoload
739 (defun mh-goto-msg (number &optional no-error-if-no-message dont-show)
740 "Go to a message\\<mh-folder-mode-map>.
741
742 You can enter the message NUMBER either before or after typing
743 \\[mh-goto-msg]. In the latter case, Emacs prompts you.
744
745 In a program, optional non-nil second argument NO-ERROR-IF-NO-MESSAGE
746 means return nil instead of signaling an error if message does not
747 exist\; in this case, the cursor is positioned near where the message
748 would have been. Non-nil third argument DONT-SHOW means not to show
749 the message."
750 (interactive "NGo to message: ")
751 (setq number (prefix-numeric-value number))
752 (let ((point (point))
753 (return-value t))
754 (goto-char (point-min))
755 (unless (re-search-forward (format (mh-scan-msg-search-regexp) number)
756 nil t)
757 (goto-char point)
758 (unless no-error-if-no-message
759 (error "No message %d" number))
760 (setq return-value nil))
761 (beginning-of-line)
762 (or dont-show (not return-value) (mh-maybe-show number))
763 return-value))
764
765 ;;;###mh-autoload
766 (defun mh-inc-folder (&optional file folder)
767 "Incorporate new mail into a folder.
768
769 You can incorporate mail from any file into the current folder by
770 specifying a prefix argument; you'll be prompted for the name of
771 the FILE to use as well as the destination FOLDER
772
773 The hook `mh-inc-folder-hook' is run after incorporating new
774 mail.
775
776 Do not call this function from outside MH-E; use \\[mh-rmail]
777 instead."
778 (interactive (list (if current-prefix-arg
779 (expand-file-name
780 (read-file-name "inc mail from file: "
781 mh-user-path)))
782 (if current-prefix-arg
783 (mh-prompt-for-folder "inc mail into" mh-inbox t))))
784 (if (not folder)
785 (setq folder mh-inbox))
786 (let ((threading-needed-flag nil))
787 (let ((config (current-window-configuration)))
788 (when (and mh-show-buffer (get-buffer mh-show-buffer))
789 (delete-windows-on mh-show-buffer))
790 (cond ((not (get-buffer folder))
791 (mh-make-folder folder)
792 (setq threading-needed-flag mh-show-threads-flag)
793 (setq mh-previous-window-config config))
794 ((not (eq (current-buffer) (get-buffer folder)))
795 (switch-to-buffer folder)
796 (setq mh-previous-window-config config))))
797 (mh-get-new-mail file)
798 (when (and threading-needed-flag
799 (save-excursion
800 (goto-char (point-min))
801 (or (null mh-large-folder)
802 (not (equal (forward-line (1+ mh-large-folder)) 0))
803 (and (message "Not threading since the number of messages exceeds `mh-large-folder'")
804 nil))))
805 (mh-toggle-threads))
806 (beginning-of-line)
807 (if (and mh-showing-mode (looking-at mh-scan-valid-regexp)) (mh-show))
808 (run-hooks 'mh-inc-folder-hook)))
809
810 ;;;###mh-autoload
811 (defun mh-last-msg ()
812 "Display last message."
813 (interactive)
814 (goto-char (point-max))
815 (while (and (not (bobp)) (not (looking-at mh-scan-valid-regexp)))
816 (forward-line -1))
817 (mh-recenter nil))
818
819 ;;;###mh-autoload
820 (defun mh-modify (&optional message)
821 "Edit message.
822
823 There are times when you need to edit a message. For example, you
824 may need to fix a broken Content-Type header field. You can do
825 this with this command. It displays the raw message in an
826 editable buffer. When you are done editing, save and kill the
827 buffer as you would any other.
828
829 From a program, edit MESSAGE; nil means edit current message."
830 (interactive)
831 (let* ((message (or message (mh-get-msg-num t)))
832 (msg-filename (mh-msg-filename message))
833 edit-buffer)
834 (when (not (file-exists-p msg-filename))
835 (error "Message %d does not exist" message))
836
837 ;; Invalidate the show buffer if it is showing the same message that is
838 ;; to be edited.
839 (when (and (buffer-live-p (get-buffer mh-show-buffer))
840 (equal (save-excursion (set-buffer mh-show-buffer)
841 buffer-file-name)
842 msg-filename))
843 (mh-invalidate-show-buffer))
844
845 ;; Edit message
846 (find-file msg-filename)
847 (setq edit-buffer (current-buffer))
848
849 ;; Set buffer properties
850 (mh-letter-mode)
851 (use-local-map text-mode-map)
852
853 ;; Just show the edit buffer...
854 (delete-other-windows)
855 (switch-to-buffer edit-buffer)))
856
857 ;;;###mh-autoload
858 (defun mh-next-button (&optional backward-flag)
859 "Go to the next button.
860
861 If the end of the buffer is reached then the search wraps over to
862 the start of the buffer.
863
864 If an optional prefix argument BACKWARD-FLAG is given, the cursor
865 will move to the previous button."
866 (interactive (list current-prefix-arg))
867 (unless mh-showing-mode
868 (mh-show))
869 (mh-in-show-buffer (mh-show-buffer)
870 (mh-goto-next-button backward-flag)))
871
872 ;;;###mh-autoload
873 (defun mh-next-undeleted-msg (&optional count wait-after-complaining-flag)
874 "Display next message.
875
876 This command can be given a prefix argument COUNT to specify how
877 many unread messages to skip.
878
879 In a program, pause for a second after printing message if we are
880 at the last undeleted message and optional argument
881 WAIT-AFTER-COMPLAINING-FLAG is non-nil."
882 (interactive "p")
883 (setq mh-next-direction 'forward)
884 (forward-line 1)
885 (cond ((re-search-forward mh-scan-good-msg-regexp nil t count)
886 (beginning-of-line)
887 (mh-maybe-show))
888 (t (forward-line -1)
889 (message "No more undeleted messages")
890 (if wait-after-complaining-flag (sit-for 1)))))
891
892 ;;;###mh-autoload
893 (defun mh-next-unread-msg (&optional count)
894 "Display next unread message.
895
896 This command can be given a prefix argument COUNT to specify how
897 many unread messages to skip."
898 (interactive "p")
899 (unless (> count 0)
900 (error "The function `mh-next-unread-msg' expects positive argument"))
901 (setq count (1- count))
902 (let ((unread-sequence (reverse (cdr (assoc mh-unseen-seq mh-seq-list))))
903 (cur-msg (mh-get-msg-num nil)))
904 (cond ((and (not cur-msg) (not (bobp))
905 ;; If we are at the end of the buffer back up one line and go
906 ;; to unread message after that.
907 (progn
908 (forward-line -1)
909 (setq cur-msg (mh-get-msg-num nil)))
910 nil))
911 ((or (null unread-sequence) (not cur-msg))
912 ;; No unread message or there aren't any messages in buffer...
913 (message "No more unread messages"))
914 ((progn
915 ;; Skip messages
916 (while (and unread-sequence (>= cur-msg (car unread-sequence)))
917 (setq unread-sequence (cdr unread-sequence)))
918 (while (> count 0)
919 (setq unread-sequence (cdr unread-sequence))
920 (setq count (1- count)))
921 (not (car unread-sequence)))
922 (message "No more unread messages"))
923 (t (loop for msg in unread-sequence
924 when (mh-goto-msg msg t) return nil
925 finally (message "No more unread messages"))))))
926
927 ;;;###mh-autoload
928 (defun mh-page-msg (&optional lines)
929 "Display next page in message.
930
931 You can give this command a prefix argument that specifies the
932 number of LINES to scroll. This command will also show the next
933 undeleted message if it is used at the bottom of a message."
934 (interactive "P")
935 (if mh-showing-mode
936 (if mh-page-to-next-msg-flag
937 (if (equal mh-next-direction 'backward)
938 (mh-previous-undeleted-msg)
939 (mh-next-undeleted-msg))
940 (if (mh-in-show-buffer (mh-show-buffer)
941 (pos-visible-in-window-p (point-max)))
942 (progn
943 (message
944 "End of message (Type %s to read %s undeleted message)"
945 (single-key-description last-input-event)
946 (if (equal mh-next-direction 'backward)
947 "previous"
948 "next"))
949 (setq mh-page-to-next-msg-flag t))
950 (scroll-other-window lines)))
951 (mh-show)))
952
953 ;;;###mh-autoload
954 (defun mh-prev-button ()
955 "Go to the previous button.
956
957 If the beginning of the buffer is reached then the search wraps
958 over to the end of the buffer."
959 (interactive)
960 (mh-next-button t))
961
962 ;;;###mh-autoload
963 (defun mh-previous-page (&optional lines)
964 "Display next page in message.
965
966 You can give this command a prefix argument that specifies the
967 number of LINES to scroll."
968 (interactive "P")
969 (mh-in-show-buffer (mh-show-buffer)
970 (scroll-down lines)))
971
972 ;;;###mh-autoload
973 (defun mh-previous-undeleted-msg (&optional count wait-after-complaining-flag)
974 "Display previous message.
975
976 This command can be given a prefix argument COUNT to specify how
977 many unread messages to skip.
978
979 In a program, pause for a second after printing message if we are
980 at the last undeleted message and optional argument
981 WAIT-AFTER-COMPLAINING-FLAG is non-nil."
982 (interactive "p")
983 (setq mh-next-direction 'backward)
984 (beginning-of-line)
985 (cond ((re-search-backward mh-scan-good-msg-regexp nil t count)
986 (mh-maybe-show))
987 (t (message "No previous undeleted message")
988 (if wait-after-complaining-flag (sit-for 1)))))
989
990 ;;;###mh-autoload
991 (defun mh-previous-unread-msg (&optional count)
992 "Display previous unread message.
993
994 This command can be given a prefix argument COUNT to specify how
995 many unread messages to skip."
996 (interactive "p")
997 (unless (> count 0)
998 (error "The function `mh-previous-unread-msg' expects positive argument"))
999 (setq count (1- count))
1000 (let ((unread-sequence (cdr (assoc mh-unseen-seq mh-seq-list)))
1001 (cur-msg (mh-get-msg-num nil)))
1002 (cond ((and (not cur-msg) (not (bobp))
1003 ;; If we are at the end of the buffer back up one line and go
1004 ;; to unread message after that.
1005 (progn
1006 (forward-line -1)
1007 (setq cur-msg (mh-get-msg-num nil)))
1008 nil))
1009 ((or (null unread-sequence) (not cur-msg))
1010 ;; No unread message or there aren't any messages in buffer...
1011 (message "No more unread messages"))
1012 ((progn
1013 ;; Skip count messages...
1014 (while (and unread-sequence (>= (car unread-sequence) cur-msg))
1015 (setq unread-sequence (cdr unread-sequence)))
1016 (while (> count 0)
1017 (setq unread-sequence (cdr unread-sequence))
1018 (setq count (1- count)))
1019 (not (car unread-sequence)))
1020 (message "No more unread messages"))
1021 (t (loop for msg in unread-sequence
1022 when (mh-goto-msg msg t) return nil
1023 finally (message "No more unread messages"))))))
1024
1025 ;;;###mh-autoload
1026 (defun mh-quit ()
1027 "Quit the current MH-E folder.
1028
1029 When you want to quit using MH-E and go back to editing, you can use
1030 this command. This buries the buffers of the current MH-E folder and
1031 restores the buffers that were present when you first ran
1032 \\[mh-rmail]. It also removes any MH-E working buffers whose name
1033 begins with \" *mh-\" or \"*MH-E \". You can later restore your MH-E
1034 session by selecting the \"+inbox\" buffer or by running \\[mh-rmail]
1035 again.
1036
1037 The two hooks `mh-before-quit-hook' and `mh-quit-hook' are called by
1038 this function. The former one is called before the quit occurs, so you
1039 might use it to perform any MH-E operations; you could perform some
1040 query and abort the quit or call `mh-execute-commands', for example.
1041 The latter is not run in an MH-E context, so you might use it to
1042 modify the window setup."
1043 (interactive)
1044 (run-hooks 'mh-before-quit-hook)
1045 (let ((show-buffer (get-buffer mh-show-buffer)))
1046 (when show-buffer
1047 (kill-buffer show-buffer)))
1048 (mh-update-sequences)
1049 (mh-destroy-postponed-handles)
1050 (bury-buffer (current-buffer))
1051
1052 ;; Delete all MH-E temporary and working buffers.
1053 (dolist (buffer (buffer-list))
1054 (when (or (string-match "^ \\*mh-" (buffer-name buffer))
1055 (string-match "^\\*MH-E " (buffer-name buffer)))
1056 (kill-buffer buffer)))
1057
1058 (if mh-previous-window-config
1059 (set-window-configuration mh-previous-window-config))
1060 (run-hooks 'mh-quit-hook))
1061
1062 ;;;###mh-autoload
1063 (defun mh-refile-msg (range folder &optional dont-update-last-destination-flag)
1064 "Refile (output) RANGE into FOLDER.
1065
1066 You are prompted for the folder name. Note that this command can also
1067 be used to create folders. If you specify a folder that does not
1068 exist, you will be prompted to create it.
1069
1070 The hook `mh-refile-msg-hook' is called after a message is marked to
1071 be refiled.
1072
1073 Check the documentation of `mh-interactive-range' to see how RANGE is
1074 read in interactive use.
1075
1076 In a program, the variables `mh-last-destination' and
1077 `mh-last-destination-folder' are not updated if
1078 DONT-UPDATE-LAST-DESTINATION-FLAG is non-nil."
1079 (interactive (list (mh-interactive-range "Refile")
1080 (intern (mh-prompt-for-refile-folder))))
1081 (unless dont-update-last-destination-flag
1082 (setq mh-last-destination (cons 'refile folder)
1083 mh-last-destination-folder mh-last-destination))
1084 (mh-iterate-on-range () range
1085 (mh-refile-a-msg nil folder))
1086 (when (looking-at mh-scan-refiled-msg-regexp) (mh-next-msg)))
1087
1088 ;;;###mh-autoload
1089 (defun mh-refile-or-write-again (range &optional interactive-flag)
1090 "Repeat last output command.
1091
1092 If you are refiling several messages into the same folder, you
1093 can use this command to repeat the last
1094 refile (\\[mh-refile-msg]) or write (\\[mh-write-msg-to-file]).
1095 You can use a range.
1096
1097 Check the documentation of `mh-interactive-range' to see how RANGE is
1098 read in interactive use.
1099
1100 In a program, a non-nil INTERACTIVE-FLAG means that the function was
1101 called interactively."
1102 (interactive (list (mh-interactive-range "Redo") t))
1103 (if (null mh-last-destination)
1104 (error "No previous refile or write"))
1105 (cond ((eq (car mh-last-destination) 'refile)
1106 (mh-refile-msg range (cdr mh-last-destination))
1107 (message "Destination folder: %s" (cdr mh-last-destination)))
1108 (t
1109 (mh-iterate-on-range msg range
1110 (apply 'mh-write-msg-to-file msg (cdr mh-last-destination)))
1111 (mh-next-msg interactive-flag))))
1112
1113 ;;;###mh-autoload
1114 (defun mh-rescan-folder (&optional range dont-exec-pending)
1115 "Rescan folder\\<mh-folder-mode-map>.
1116
1117 This command is useful to grab all messages in your \"+inbox\" after
1118 processing your new mail for the first time. If you don't want to
1119 rescan the entire folder, this command will accept a RANGE. Check the
1120 documentation of `mh-interactive-range' to see how RANGE is read in
1121 interactive use.
1122
1123 This command will ask if you want to process refiles or deletes first
1124 and then either run \\[mh-execute-commands] for you or undo the
1125 pending refiles and deletes.
1126
1127 In a program, the processing of outstanding commands is not performed
1128 if DONT-EXEC-PENDING is non-nil."
1129 (interactive (list (if current-prefix-arg
1130 (mh-read-range "Rescan" mh-current-folder t nil t
1131 mh-interpret-number-as-range-flag)
1132 nil)))
1133 (setq mh-next-direction 'forward)
1134 (let ((threaded-flag (memq 'unthread mh-view-ops))
1135 (msg-num (mh-get-msg-num nil)))
1136 (mh-scan-folder mh-current-folder (or range "all") dont-exec-pending)
1137 ;; If there isn't a cur sequence, mh-scan-folder goes to the first message.
1138 ;; Try to stay where we were.
1139 (if (null (car (mh-seq-to-msgs 'cur)))
1140 (mh-goto-msg msg-num t t))
1141 (cond (threaded-flag (mh-toggle-threads))
1142 (mh-index-data (mh-index-insert-folder-headers)))))
1143
1144 (defun mh-show-mouse (event)
1145 "Move point to mouse EVENT and show message."
1146 (interactive "e")
1147 (mouse-set-point event)
1148 (mh-show))
1149
1150 ;;;###mh-autoload
1151 (defun mh-toggle-showing ()
1152 "Toggle between MH-Folder and MH-Folder Show modes.
1153
1154 This command switches between MH-Folder mode and MH-Folder Show
1155 mode. MH-Folder mode turns off the associated show buffer so that
1156 you can perform operations on the messages quickly without
1157 reading them. This is an excellent way to prune out your junk
1158 mail or to refile a group of messages to another folder for later
1159 examination."
1160 (interactive)
1161 (if mh-showing-mode
1162 (mh-set-scan-mode)
1163 (mh-show)))
1164
1165 ;;;###mh-autoload
1166 (defun mh-undo (range)
1167 "Undo pending deletes or refiles in RANGE.
1168
1169 If you've deleted a message or refiled it, but changed your mind,
1170 you can cancel the action before you've executed it. Use this
1171 command to undo a refile on or deletion of a single message. You
1172 can also undo refiles and deletes for messages that are found in
1173 a given RANGE.
1174
1175 Check the documentation of `mh-interactive-range' to see how
1176 RANGE is read in interactive use."
1177 (interactive (list (mh-interactive-range "Undo")))
1178 (cond ((numberp range)
1179 (let ((original-position (point)))
1180 (beginning-of-line)
1181 (while (not (or (looking-at mh-scan-deleted-msg-regexp)
1182 (looking-at mh-scan-refiled-msg-regexp)
1183 (and (eq mh-next-direction 'forward) (bobp))
1184 (and (eq mh-next-direction 'backward)
1185 (save-excursion (forward-line) (eobp)))))
1186 (forward-line (if (eq mh-next-direction 'forward) -1 1)))
1187 (if (or (looking-at mh-scan-deleted-msg-regexp)
1188 (looking-at mh-scan-refiled-msg-regexp))
1189 (progn
1190 (mh-undo-msg (mh-get-msg-num t))
1191 (mh-maybe-show))
1192 (goto-char original-position)
1193 (error "Nothing to undo"))))
1194 (t (mh-iterate-on-range () range
1195 (mh-undo-msg nil))))
1196 (if (not (mh-outstanding-commands-p))
1197 (mh-set-folder-modified-p nil)))
1198
1199 ;;;###mh-autoload
1200 (defun mh-visit-folder (folder &optional range index-data)
1201 "Visit FOLDER.
1202
1203 When you want to read the messages that you have refiled into folders,
1204 use this command to visit the folder. You are prompted for the folder
1205 name.
1206
1207 The folder buffer will show just unseen messages if there are any;
1208 otherwise, it will show all the messages in the buffer as long there
1209 are fewer than `mh-large-folder' messages. If there are more, then you
1210 are prompted for a range of messages to scan.
1211
1212 You can provide a prefix argument in order to specify a RANGE of
1213 messages to show when you visit the folder. In this case, regions are
1214 not used to specify the range and `mh-large-folder' is ignored. Check
1215 the documentation of `mh-interactive-range' to see how RANGE is read
1216 in interactive use.
1217
1218 Note that this command can also be used to create folders. If you
1219 specify a folder that does not exist, you will be prompted to create
1220 it.
1221
1222 Do not call this function from outside MH-E; use \\[mh-rmail] instead.
1223
1224 If, in a program, RANGE is nil (the default), then all messages in
1225 FOLDER are displayed. If an index buffer is being created then
1226 INDEX-DATA is used to initialize the index buffer specific data
1227 structures."
1228 (interactive (let ((folder-name (mh-prompt-for-folder "Visit" mh-inbox t)))
1229 (list folder-name
1230 (mh-read-range "Scan" folder-name t nil
1231 current-prefix-arg
1232 mh-interpret-number-as-range-flag))))
1233 (let ((config (current-window-configuration))
1234 (current-buffer (current-buffer))
1235 (threaded-view-flag mh-show-threads-flag))
1236 (delete-other-windows)
1237 (save-excursion
1238 (when (get-buffer folder)
1239 (set-buffer folder)
1240 (setq threaded-view-flag (memq 'unthread mh-view-ops))))
1241 (when index-data
1242 (mh-make-folder folder)
1243 (setq mh-index-data (car index-data)
1244 mh-index-msg-checksum-map (make-hash-table :test #'equal)
1245 mh-index-checksum-origin-map (make-hash-table :test #'equal))
1246 (mh-index-update-maps folder (cadr index-data))
1247 (mh-index-create-sequences))
1248 (mh-scan-folder folder (or range "all"))
1249 (cond ((and threaded-view-flag
1250 (save-excursion
1251 (goto-char (point-min))
1252 (or (null mh-large-folder)
1253 (not (equal (forward-line (1+ mh-large-folder)) 0))
1254 (and (message "Not threading since the number of messages exceeds `mh-large-folder'")
1255 nil))))
1256 (mh-toggle-threads))
1257 (mh-index-data
1258 (mh-index-insert-folder-headers)))
1259 (unless (eq current-buffer (current-buffer))
1260 (setq mh-previous-window-config config)))
1261 nil)
1262
1263 ;;;###mh-autoload
1264 (defun mh-write-msg-to-file (message file no-header)
1265 "Append MESSAGE to end of FILE\\<mh-folder-mode-map>.
1266
1267 You are prompted for the filename. If the file already exists,
1268 the message is appended to it. You can also write the message to
1269 the file without the header by specifying a prefix argument
1270 NO-HEADER. Subsequent writes to the same file can be made with
1271 the command \\[mh-refile-or-write-again]."
1272 (interactive
1273 (list (mh-get-msg-num t)
1274 (let ((default-dir (if (eq 'write (car mh-last-destination-write))
1275 (file-name-directory
1276 (car (cdr mh-last-destination-write)))
1277 default-directory)))
1278 (read-file-name (format "Save message%s in file: "
1279 (if current-prefix-arg " body" ""))
1280 default-dir
1281 (if (eq 'write (car mh-last-destination-write))
1282 (car (cdr mh-last-destination-write))
1283 (expand-file-name "mail.out" default-dir))))
1284 current-prefix-arg))
1285 (let ((msg-file-to-output (mh-msg-filename message))
1286 (output-file (mh-expand-file-name file)))
1287 (setq mh-last-destination (list 'write file (if no-header 'no-header))
1288 mh-last-destination-write mh-last-destination)
1289 (save-excursion
1290 (set-buffer (get-buffer-create mh-temp-buffer))
1291 (erase-buffer)
1292 (insert-file-contents msg-file-to-output)
1293 (goto-char (point-min))
1294 (if no-header (search-forward "\n\n"))
1295 (append-to-file (point) (point-max) output-file))))
1296
1297 ;;;###mh-autoload
1298 (defun mh-update-sequences ()
1299 "Flush MH-E's state out to MH.
1300
1301 This function updates the sequence specified by your
1302 \"Unseen-Sequence:\" profile component, \"cur\", and the sequence
1303 listed by the `mh-tick-seq' option which is \"tick\" by default.
1304 The message at the cursor is used for \"cur\"."
1305 (interactive)
1306 ;; mh-update-sequences is the opposite of mh-read-folder-sequences,
1307 ;; which updates MH-E's state from MH.
1308 (let ((folder-set (mh-update-unseen))
1309 (new-cur (mh-get-msg-num nil)))
1310 (if new-cur
1311 (let ((seq-entry (mh-find-seq 'cur)))
1312 (mh-remove-cur-notation)
1313 (setcdr seq-entry
1314 (list new-cur)) ;delete-seq-locally, add-msgs-to-seq
1315 (mh-define-sequence 'cur (list new-cur))
1316 (beginning-of-line)
1317 (if (looking-at mh-scan-good-msg-regexp)
1318 (mh-notate-cur)))
1319 (or folder-set
1320 (save-excursion
1321 ;; psg - mh-current-folder is nil if mh-summary-height < 4 !
1322 ;; So I added this sanity check.
1323 (if (stringp mh-current-folder)
1324 (mh-exec-cmd-quiet t "folder" mh-current-folder "-fast")
1325 (mh-exec-cmd-quiet t "folder" "-fast")))))))
1326
1327 \f
1328
1329 ;;; Support Routines
1330
1331 (defun mh-get-new-mail (maildrop-name)
1332 "Read new mail from MAILDROP-NAME into the current buffer.
1333 Return in the current buffer."
1334 (let ((point-before-inc (point))
1335 (folder mh-current-folder)
1336 (new-mail-flag nil))
1337 (with-mh-folder-updating (t)
1338 (if maildrop-name
1339 (message "inc %s -file %s..." folder maildrop-name)
1340 (message "inc %s..." folder))
1341 (setq mh-next-direction 'forward)
1342 (goto-char (point-max))
1343 (mh-remove-cur-notation)
1344 (let ((start-of-inc (point)))
1345 (if maildrop-name
1346 ;; I think MH 5 used "-ms-file" instead of "-file",
1347 ;; which would make inc'ing from maildrops fail.
1348 (mh-exec-cmd-output mh-inc-prog nil folder
1349 (mh-scan-format)
1350 "-file" (expand-file-name maildrop-name)
1351 "-width" (window-width)
1352 "-truncate")
1353 (mh-exec-cmd-output mh-inc-prog nil
1354 (mh-scan-format)
1355 "-width" (window-width)))
1356 (if maildrop-name
1357 (message "inc %s -file %s...done" folder maildrop-name)
1358 (message "inc %s...done" folder))
1359 (goto-char start-of-inc)
1360 (cond ((save-excursion
1361 (re-search-forward "^inc: no mail" nil t))
1362 (message "No new mail%s%s" (if maildrop-name " in " "")
1363 (if maildrop-name maildrop-name "")))
1364 ((and (when mh-folder-view-stack
1365 (let ((saved-text (buffer-substring-no-properties
1366 start-of-inc (point-max))))
1367 (delete-region start-of-inc (point-max))
1368 (unwind-protect (mh-widen t)
1369 (mh-remove-cur-notation)
1370 (goto-char (point-max))
1371 (setq start-of-inc (point))
1372 (insert saved-text)
1373 (goto-char start-of-inc))))
1374 nil))
1375 ((re-search-forward "^inc:" nil t) ; Error messages
1376 (error "Error incorporating mail"))
1377 ((and
1378 (equal mh-scan-format-file t)
1379 mh-adaptive-cmd-note-flag
1380 ;; Have we reached an edge condition?
1381 (save-excursion
1382 (re-search-forward mh-scan-msg-overflow-regexp nil 0 1))
1383 (setq start-of-inc (mh-generate-new-cmd-note folder))
1384 nil))
1385 (t
1386 (setq new-mail-flag t)))
1387 (keep-lines mh-scan-valid-regexp) ; Flush random scan lines
1388 (let* ((sequences (mh-read-folder-sequences folder t))
1389 (new-cur (assoc 'cur sequences))
1390 (new-unseen (assoc mh-unseen-seq sequences)))
1391 (unless (assoc 'cur mh-seq-list)
1392 (push (list 'cur) mh-seq-list))
1393 (unless (assoc mh-unseen-seq mh-seq-list)
1394 (push (list mh-unseen-seq) mh-seq-list))
1395 (setcdr (assoc 'cur mh-seq-list) (cdr new-cur))
1396 (setcdr (assoc mh-unseen-seq mh-seq-list) (cdr new-unseen)))
1397 (when (equal (point-max) start-of-inc)
1398 (mh-notate-cur))
1399 (if new-mail-flag
1400 (progn
1401 (mh-make-folder-mode-line)
1402 (when (mh-speed-flists-active-p)
1403 (mh-speed-flists t mh-current-folder))
1404 (when (memq 'unthread mh-view-ops)
1405 (mh-thread-inc folder start-of-inc))
1406 (mh-goto-cur-msg))
1407 (goto-char point-before-inc))
1408 (mh-notate-user-sequences (cons start-of-inc (point-max)))))))
1409
1410 (defun mh-generate-new-cmd-note (folder)
1411 "Fix the `mh-cmd-note' value for this FOLDER.
1412
1413 After doing an `mh-get-new-mail' operation in this FOLDER, at least
1414 one line that looks like a truncated message number was found.
1415
1416 Remove the text added by the last `mh-inc' command. It should be the
1417 messages cur-last. Call `mh-set-cmd-note', adjusting the notation
1418 column with the width of the largest message number in FOLDER.
1419
1420 Reformat the message number width on each line in the buffer and trim
1421 the line length to fit in the window.
1422
1423 Rescan the FOLDER in the range cur-last in order to display the
1424 messages that were removed earlier. They should all fit in the scan
1425 line now with no message truncation."
1426 (save-excursion
1427 (let ((maxcol (1- (window-width)))
1428 (old-cmd-note mh-cmd-note)
1429 mh-cmd-note-fmt
1430 msgnum)
1431 ;; Nuke all of the lines just added by the last inc
1432 (delete-char (- (point-max) (point)))
1433 ;; Update the current buffer to reflect the new mh-cmd-note
1434 ;; value needed to display messages.
1435 (mh-set-cmd-note (mh-msg-num-width-to-column (mh-msg-num-width folder)))
1436 (setq mh-cmd-note-fmt (concat "%" (format "%d" mh-cmd-note) "d"))
1437 ;; Cleanup the messages that are in the buffer right now
1438 (goto-char (point-min))
1439 (cond ((memq 'unthread mh-view-ops)
1440 (mh-thread-add-spaces (- mh-cmd-note old-cmd-note)))
1441 (t (while (re-search-forward (mh-scan-msg-number-regexp) nil 0 1)
1442 ;; reformat the number to fix in mh-cmd-note columns
1443 (setq msgnum (string-to-number
1444 (buffer-substring
1445 (match-beginning 1) (match-end 1))))
1446 (replace-match (format mh-cmd-note-fmt msgnum))
1447 ;; trim the line to fix in the window
1448 (end-of-line)
1449 (let ((eol (point)))
1450 (move-to-column maxcol)
1451 (if (<= (point) eol)
1452 (delete-char (- eol (point))))))))
1453 ;; now re-read the lost messages
1454 (goto-char (point-max))
1455 (prog1 (point)
1456 (mh-regenerate-headers "cur-last" t)))))
1457
1458 ;;;###mh-autoload
1459 (defun mh-goto-cur-msg (&optional minimal-changes-flag)
1460 "Position the cursor at the current message.
1461 When optional argument MINIMAL-CHANGES-FLAG is non-nil, the
1462 function doesn't recenter the folder buffer."
1463 (let ((cur-msg (car (mh-seq-to-msgs 'cur))))
1464 (cond ((and cur-msg
1465 (mh-goto-msg cur-msg t t))
1466 (unless minimal-changes-flag
1467 (mh-notate-cur)
1468 (mh-recenter 0)
1469 (mh-maybe-show cur-msg)))
1470 (t
1471 (setq overlay-arrow-position nil)
1472 (message "No current message")))))
1473
1474 ;;;###mh-autoload
1475 (defun mh-recenter (arg)
1476 "Like recenter but with three improvements:
1477
1478 - At the end of the buffer it tries to show fewer empty lines.
1479
1480 - operates only if the current buffer is in the selected window.
1481 (Commands like `save-some-buffers' can make this false.)
1482
1483 - nil ARG means recenter as if prefix argument had been given."
1484 (cond ((not (eq (get-buffer-window (current-buffer)) (selected-window)))
1485 nil)
1486 ((= (point-max) (save-excursion
1487 (forward-line (- (/ (window-height) 2) 2))
1488 (point)))
1489 (let ((lines-from-end 2))
1490 (save-excursion
1491 (while (> (point-max) (progn (forward-line) (point)))
1492 (incf lines-from-end)))
1493 (recenter (- lines-from-end))))
1494 ;; '(4) is the same as C-u prefix argument.
1495 (t (recenter (or arg '(4))))))
1496
1497 (defun mh-update-unseen ()
1498 "Synchronize the unseen sequence with MH.
1499 Return non-nil if the MH folder was set.
1500 The hook `mh-unseen-updated-hook' is called after the unseen sequence
1501 is updated."
1502 (if mh-seen-list
1503 (let* ((unseen-seq (mh-find-seq mh-unseen-seq))
1504 (unseen-msgs (mh-seq-msgs unseen-seq)))
1505 (if unseen-msgs
1506 (progn
1507 (mh-undefine-sequence mh-unseen-seq mh-seen-list)
1508 (run-hooks 'mh-unseen-updated-hook)
1509 (while mh-seen-list
1510 (setq unseen-msgs (delq (car mh-seen-list) unseen-msgs))
1511 (setq mh-seen-list (cdr mh-seen-list)))
1512 (setcdr unseen-seq unseen-msgs)
1513 t) ;since we set the folder
1514 (setq mh-seen-list nil)))))
1515
1516 ;;;###mh-autoload
1517 (defun mh-outstanding-commands-p ()
1518 "Return non-nil if there are outstanding deletes or refiles."
1519 (save-excursion
1520 (when (eq major-mode 'mh-show-mode)
1521 (set-buffer mh-show-folder-buffer))
1522 (or mh-delete-list mh-refile-list)))
1523
1524 ;;;###mh-autoload
1525 (defun mh-set-folder-modified-p (flag)
1526 "Mark current folder as modified or unmodified according to FLAG."
1527 (set-buffer-modified-p flag))
1528
1529 (defun mh-process-commands (folder)
1530 "Process outstanding commands for FOLDER.
1531
1532 This function runs `mh-before-commands-processed-hook' before the
1533 commands are processed and `mh-after-commands-processed-hook'
1534 after the commands are processed."
1535 (message "Processing deletes and refiles for %s..." folder)
1536 (set-buffer folder)
1537 (with-mh-folder-updating (nil)
1538 ;; Run the before hook -- the refile and delete lists are still valid
1539 (run-hooks 'mh-before-commands-processed-hook)
1540
1541 ;; Update the unseen sequence if it exists
1542 (mh-update-unseen)
1543
1544 (let ((redraw-needed-flag mh-index-data)
1545 (folders-changed (list mh-current-folder))
1546 (seq-map (and mh-refile-list mh-refile-preserves-sequences-flag
1547 (mh-create-sequence-map mh-seq-list)))
1548 (dest-map (and mh-refile-list mh-refile-preserves-sequences-flag
1549 (make-hash-table))))
1550 ;; Remove invalid scan lines if we are in an index folder and then remove
1551 ;; the real messages
1552 (when mh-index-data
1553 (mh-index-delete-folder-headers)
1554 (setq folders-changed
1555 (append folders-changed (mh-index-execute-commands))))
1556
1557 ;; Then refile messages
1558 (mh-mapc #'(lambda (folder-msg-list)
1559 (let* ((dest-folder (symbol-name (car folder-msg-list)))
1560 (last (car (mh-translate-range dest-folder "last")))
1561 (msgs (cdr folder-msg-list)))
1562 (push dest-folder folders-changed)
1563 (setq redraw-needed-flag t)
1564 (apply #'mh-exec-cmd
1565 "refile" "-src" folder dest-folder
1566 (mh-coalesce-msg-list msgs))
1567 (mh-delete-scan-msgs msgs)
1568 ;; Preserve sequences in destination folder...
1569 (when mh-refile-preserves-sequences-flag
1570 (clrhash dest-map)
1571 (loop for i from (1+ (or last 0))
1572 for msg in (sort (copy-sequence msgs) #'<)
1573 do (loop for seq-name in (gethash msg seq-map)
1574 do (push i (gethash seq-name dest-map))))
1575 (maphash
1576 #'(lambda (seq msgs)
1577 ;; Can't be run in the background, since the
1578 ;; current folder is changed by mark this could
1579 ;; lead to a race condition with the next refile.
1580 (apply #'mh-exec-cmd "mark"
1581 "-sequence" (symbol-name seq) dest-folder
1582 "-add" (mapcar #'(lambda (x) (format "%s" x))
1583 (mh-coalesce-msg-list msgs))))
1584 dest-map))))
1585 mh-refile-list)
1586 (setq mh-refile-list ())
1587
1588 ;; Now delete messages
1589 (cond (mh-delete-list
1590 (setq redraw-needed-flag t)
1591 (apply 'mh-exec-cmd "rmm" folder
1592 (mh-coalesce-msg-list mh-delete-list))
1593 (mh-delete-scan-msgs mh-delete-list)
1594 (setq mh-delete-list nil)))
1595
1596 ;; Don't need to remove sequences since delete and refile do so.
1597 ;; Mark cur message
1598 (if (> (buffer-size) 0)
1599 (mh-define-sequence 'cur (list (or (mh-get-msg-num nil) "last"))))
1600
1601 ;; Redraw folder buffer if needed
1602 (when (and redraw-needed-flag)
1603 (when (mh-speed-flists-active-p)
1604 (apply #'mh-speed-flists t folders-changed))
1605 (cond ((memq 'unthread mh-view-ops) (mh-thread-inc folder (point-max)))
1606 (mh-index-data (mh-index-insert-folder-headers))))
1607
1608 (and (buffer-file-name (get-buffer mh-show-buffer))
1609 (not (file-exists-p (buffer-file-name (get-buffer mh-show-buffer))))
1610 ;; If "inc" were to put a new msg in this file,
1611 ;; we would not notice, so mark it invalid now.
1612 (mh-invalidate-show-buffer))
1613
1614 (setq mh-seq-list (mh-read-folder-sequences mh-current-folder nil))
1615 (mh-remove-all-notation)
1616 (mh-notate-user-sequences)
1617
1618 ;; Run the after hook -- now folders-changed is valid,
1619 ;; but not the lists of specific messages.
1620 (let ((mh-folders-changed folders-changed))
1621 (run-hooks 'mh-after-commands-processed-hook)))
1622
1623 (message "Processing deletes and refiles for %s...done" folder)))
1624
1625 (defun mh-delete-scan-msgs (msgs)
1626 "Delete the scan listing lines for MSGS."
1627 (save-excursion
1628 (while msgs
1629 (when (mh-goto-msg (car msgs) t t)
1630 (when (memq 'unthread mh-view-ops)
1631 (mh-thread-forget-message (car msgs)))
1632 (mh-delete-line 1))
1633 (setq msgs (cdr msgs)))))
1634
1635 (defun mh-set-scan-mode ()
1636 "Display the scan listing buffer, but do not show a message."
1637 (if (get-buffer mh-show-buffer)
1638 (delete-windows-on mh-show-buffer))
1639 (mh-showing-mode 0)
1640 (force-mode-line-update)
1641 (if mh-recenter-summary-flag
1642 (mh-recenter nil)))
1643
1644 ;;;###mh-autoload
1645 (defun mh-make-folder-mode-line (&optional ignored)
1646 "Set the fields of the mode line for a folder buffer.
1647 The optional argument is now obsolete and IGNORED. It used to be
1648 used to pass in what is now stored in the buffer-local variable
1649 `mh-mode-line-annotation'."
1650 (save-excursion
1651 (save-window-excursion
1652 (mh-first-msg)
1653 (let ((new-first-msg-num (mh-get-msg-num nil)))
1654 (when (or (not (memq 'unthread mh-view-ops))
1655 (null mh-first-msg-num)
1656 (null new-first-msg-num)
1657 (< new-first-msg-num mh-first-msg-num))
1658 (setq mh-first-msg-num new-first-msg-num)))
1659 (mh-last-msg)
1660 (let ((new-last-msg-num (mh-get-msg-num nil)))
1661 (when (or (not (memq 'unthread mh-view-ops))
1662 (null mh-last-msg-num)
1663 (null new-last-msg-num)
1664 (> new-last-msg-num mh-last-msg-num))
1665 (setq mh-last-msg-num new-last-msg-num)))
1666 (setq mh-msg-count (if mh-first-msg-num
1667 (count-lines (point-min) (point-max))
1668 0))
1669 (setq mode-line-buffer-identification
1670 (list (format " {%%b%s} %s msg%s"
1671 (if mh-mode-line-annotation
1672 (format "/%s" mh-mode-line-annotation)
1673 "")
1674 (if (zerop mh-msg-count)
1675 "no"
1676 (format "%d" mh-msg-count))
1677 (if (zerop mh-msg-count)
1678 "s"
1679 (cond ((> mh-msg-count 1)
1680 (format "s (%d-%d)" mh-first-msg-num
1681 mh-last-msg-num))
1682 (mh-first-msg-num
1683 (format " (%d)" mh-first-msg-num))
1684 (""))))))
1685 (mh-logo-display))))
1686
1687 ;;;###mh-autoload
1688 (defun mh-scan-folder (folder range &optional dont-exec-pending)
1689 "Scan FOLDER over RANGE.
1690
1691 After the scan is performed, switch to the buffer associated with
1692 FOLDER.
1693
1694 Check the documentation of `mh-interactive-range' to see how RANGE is
1695 read in interactive use.
1696
1697 The processing of outstanding commands is not performed if
1698 DONT-EXEC-PENDING is non-nil."
1699 (when (stringp range)
1700 (setq range (delete "" (split-string range "[ \t\n]"))))
1701 (cond ((null (get-buffer folder))
1702 (mh-make-folder folder))
1703 (t
1704 (unless dont-exec-pending
1705 (mh-process-or-undo-commands folder)
1706 (mh-reset-threads-and-narrowing))
1707 (switch-to-buffer folder)))
1708 (mh-regenerate-headers range)
1709 (if (zerop (buffer-size))
1710 (if (equal range "all")
1711 (message "Folder %s is empty" folder)
1712 (message "No messages in %s, range %s" folder range))
1713 (mh-goto-cur-msg))
1714 (when (mh-outstanding-commands-p)
1715 (mh-notate-deleted-and-refiled)))
1716
1717 ;;;###mh-autoload
1718 (defun mh-process-or-undo-commands (folder)
1719 "If FOLDER has outstanding commands, then either process or discard them.
1720 Called by functions like `mh-sort-folder', so also invalidate
1721 show buffer."
1722 (set-buffer folder)
1723 (if (mh-outstanding-commands-p)
1724 (if (or mh-do-not-confirm-flag
1725 (y-or-n-p
1726 "Process outstanding deletes and refiles? "))
1727 (mh-process-commands folder)
1728 (set-buffer folder)
1729 (mh-undo-folder)))
1730 (mh-update-unseen)
1731 (mh-invalidate-show-buffer))
1732
1733 ;;;###mh-autoload
1734 (defun mh-regenerate-headers (range &optional update)
1735 "Scan folder over RANGE.
1736 If UPDATE, append the scan lines, otherwise replace."
1737 (let ((folder mh-current-folder)
1738 (range (if (and range (atom range)) (list range) range))
1739 scan-start)
1740 (message "Scanning %s..." folder)
1741 (mh-remove-all-notation)
1742 (with-mh-folder-updating (nil)
1743 (if update
1744 (goto-char (point-max))
1745 (delete-region (point-min) (point-max))
1746 (if mh-adaptive-cmd-note-flag
1747 (mh-set-cmd-note (mh-msg-num-width-to-column (mh-msg-num-width
1748 folder)))))
1749 (setq scan-start (point))
1750 (apply #'mh-exec-cmd-output
1751 mh-scan-prog nil
1752 (mh-scan-format)
1753 "-noclear" "-noheader"
1754 "-width" (window-width)
1755 folder range)
1756 (goto-char scan-start)
1757 (cond ((looking-at "scan: no messages in")
1758 (keep-lines mh-scan-valid-regexp)) ; Flush random scan lines
1759 ((looking-at (if (mh-variant-p 'mu-mh)
1760 "scan: message set .* does not exist"
1761 "scan: bad message list "))
1762 (keep-lines mh-scan-valid-regexp))
1763 ((looking-at "scan: ")) ; Keep error messages
1764 (t
1765 (keep-lines mh-scan-valid-regexp))) ; Flush random scan lines
1766 (setq mh-seq-list (mh-read-folder-sequences folder nil))
1767 (mh-notate-user-sequences)
1768 (or update
1769 (setq mh-mode-line-annotation
1770 (if (equal range '("all"))
1771 nil
1772 mh-partial-folder-mode-line-annotation)))
1773 (mh-make-folder-mode-line))
1774 (message "Scanning %s...done" folder)))
1775
1776 ;;;###mh-autoload
1777 (defun mh-reset-threads-and-narrowing ()
1778 "Reset all variables pertaining to threads and narrowing.
1779 Also removes all content from the folder buffer."
1780 (setq mh-view-ops ())
1781 (setq mh-folder-view-stack ())
1782 (setq mh-thread-scan-line-map-stack ())
1783 (let ((buffer-read-only nil)) (erase-buffer)))
1784
1785 (defun mh-make-folder (name)
1786 "Create a new mail folder called NAME.
1787 Make it the current folder."
1788 (switch-to-buffer name)
1789 (setq buffer-read-only nil)
1790 (erase-buffer)
1791 (if mh-adaptive-cmd-note-flag
1792 (mh-set-cmd-note (mh-msg-num-width-to-column (mh-msg-num-width name))))
1793 (setq buffer-read-only t)
1794 (mh-folder-mode)
1795 (mh-set-folder-modified-p nil)
1796 (setq buffer-file-name mh-folder-filename)
1797 (when (and (not mh-index-data)
1798 (file-exists-p (concat buffer-file-name mh-index-data-file)))
1799 (mh-index-read-data))
1800 (mh-make-folder-mode-line))
1801
1802 ;;;###mh-autoload
1803 (defun mh-next-msg (&optional wait-after-complaining-flag)
1804 "Move backward or forward to the next undeleted message in the buffer.
1805 If optional argument WAIT-AFTER-COMPLAINING-FLAG is non-nil and
1806 we are at the last message, then wait for a second after telling
1807 the user that there aren't any more unread messages."
1808 (if (eq mh-next-direction 'forward)
1809 (mh-next-undeleted-msg 1 wait-after-complaining-flag)
1810 (mh-previous-undeleted-msg 1 wait-after-complaining-flag)))
1811
1812 ;;;###mh-autoload
1813 (defun mh-prompt-for-refile-folder ()
1814 "Prompt the user for a folder in which the message should be filed.
1815 The folder is returned as a string.
1816
1817 The default folder name is generated by the option
1818 `mh-default-folder-for-message-function' if it is non-nil or
1819 `mh-folder-from-address'."
1820 (mh-prompt-for-folder
1821 "Destination"
1822 (let ((refile-file (ignore-errors (mh-msg-filename (mh-get-msg-num t)))))
1823 (if (null refile-file) ""
1824 (save-excursion
1825 (set-buffer (get-buffer-create mh-temp-buffer))
1826 (erase-buffer)
1827 (insert-file-contents refile-file)
1828 (or (and mh-default-folder-for-message-function
1829 (let ((buffer-file-name refile-file))
1830 (funcall mh-default-folder-for-message-function)))
1831 (mh-folder-from-address)
1832 (and (eq 'refile (car mh-last-destination-folder))
1833 (symbol-name (cdr mh-last-destination-folder)))
1834 ""))))
1835 t))
1836
1837 ;;;###mh-autoload
1838 (defun mh-folder-from-address ()
1839 "Derive folder name from sender.
1840
1841 The name of the folder is derived as follows:
1842
1843 a) The folder name associated with the first address found in
1844 the list `mh-default-folder-list' is used. Each element in
1845 this list contains a \"Check Recipient\" item. If this item is
1846 turned on, then the address is checked against the recipient
1847 instead of the sender. This is useful for mailing lists.
1848
1849 b) An alias prefixed by `mh-default-folder-prefix'
1850 corresponding to the address is used. The prefix is used to
1851 prevent clutter in your mail directory.
1852
1853 Return nil if a folder name was not derived, or if the variable
1854 `mh-default-folder-must-exist-flag' is t and the folder does not
1855 exist."
1856 ;; Loop for all entries in mh-default-folder-list
1857 (save-restriction
1858 (goto-char (point-min))
1859 (re-search-forward "\n\n" nil 'limit)
1860 (narrow-to-region (point-min) (point))
1861 (let ((to/cc (concat (or (message-fetch-field "to") "") ", "
1862 (or (message-fetch-field "cc") "")))
1863 (from (or (message-fetch-field "from") ""))
1864 folder-name)
1865 (setq folder-name
1866 (loop for list in mh-default-folder-list
1867 when (string-match (nth 0 list) (if (nth 2 list) to/cc from))
1868 return (nth 1 list)
1869 finally return nil))
1870
1871 ;; Make sure a result from `mh-default-folder-list' begins with "+"
1872 ;; since 'mh-expand-file-name below depends on it
1873 (when (and folder-name (not (eq (aref folder-name 0) ?+)))
1874 (setq folder-name (concat "+" folder-name)))
1875
1876 ;; If not, is there an alias for the address?
1877 (when (not folder-name)
1878 (let* ((from-header (mh-extract-from-header-value))
1879 (address (and from-header
1880 (nth 1 (mail-extract-address-components
1881 from-header))))
1882 (alias (and address (mh-alias-address-to-alias address))))
1883 (when alias
1884 (setq folder-name
1885 (and alias (concat "+" mh-default-folder-prefix alias))))))
1886
1887 ;; If mh-default-folder-must-exist-flag set, check that folder exists.
1888 (if (and folder-name
1889 (or (not mh-default-folder-must-exist-flag)
1890 (file-exists-p (mh-expand-file-name folder-name))))
1891 folder-name))))
1892
1893 ;;;###mh-autoload
1894 (defun mh-delete-a-msg (message)
1895 "Delete MESSAGE.
1896 If MESSAGE is nil then the message at point is deleted.
1897 The hook `mh-delete-msg-hook' is called after you mark a message
1898 for deletion. For example, a past maintainer of MH-E used this
1899 once when he kept statistics on his mail usage."
1900 (save-excursion
1901 (if (numberp message)
1902 (mh-goto-msg message nil t)
1903 (beginning-of-line)
1904 (setq message (mh-get-msg-num t)))
1905 (if (looking-at mh-scan-refiled-msg-regexp)
1906 (error "Message %d is refiled; undo refile before deleting" message))
1907 (if (looking-at mh-scan-deleted-msg-regexp)
1908 nil
1909 (mh-set-folder-modified-p t)
1910 (setq mh-delete-list (cons message mh-delete-list))
1911 (mh-notate nil mh-note-deleted mh-cmd-note)
1912 (run-hooks 'mh-delete-msg-hook))))
1913
1914 ;;;###mh-autoload
1915 (defun mh-refile-a-msg (message folder)
1916 "Refile MESSAGE in FOLDER.
1917 If MESSAGE is nil then the message at point is refiled.
1918 Folder is a symbol, not a string.
1919 The hook `mh-refile-msg-hook' is called after a message is marked to
1920 be refiled."
1921 (save-excursion
1922 (if (numberp message)
1923 (mh-goto-msg message nil t)
1924 (beginning-of-line)
1925 (setq message (mh-get-msg-num t)))
1926 (cond ((looking-at mh-scan-deleted-msg-regexp)
1927 (error "Message %d is deleted; undo delete before moving" message))
1928 ((looking-at mh-scan-refiled-msg-regexp)
1929 (if (y-or-n-p
1930 (format "Message %d already refiled; copy to %s as well? "
1931 message folder))
1932 (mh-exec-cmd "refile" (mh-get-msg-num t) "-link"
1933 "-src" mh-current-folder
1934 (symbol-name folder))
1935 (message "Message not copied")))
1936 (t
1937 (mh-set-folder-modified-p t)
1938 (cond ((null (assoc folder mh-refile-list))
1939 (push (list folder message) mh-refile-list))
1940 ((not (member message (cdr (assoc folder mh-refile-list))))
1941 (push message (cdr (assoc folder mh-refile-list)))))
1942 (mh-notate nil mh-note-refiled mh-cmd-note)
1943 (run-hooks 'mh-refile-msg-hook)))))
1944
1945 (defun mh-undo-msg (msg)
1946 "Undo the deletion or refile of one MSG.
1947 If MSG is nil then act on the message at point"
1948 (save-excursion
1949 (if (numberp msg)
1950 (mh-goto-msg msg t t)
1951 (beginning-of-line)
1952 (setq msg (mh-get-msg-num t)))
1953 (cond ((memq msg mh-delete-list)
1954 (setq mh-delete-list (delq msg mh-delete-list)))
1955 (t
1956 (dolist (folder-msg-list mh-refile-list)
1957 (setf (cdr folder-msg-list) (remove msg (cdr folder-msg-list))))
1958 (setq mh-refile-list (loop for x in mh-refile-list
1959 unless (null (cdr x)) collect x))))
1960 (mh-notate nil ? mh-cmd-note)))
1961
1962 ;;;###mh-autoload
1963 (defun mh-msg-filename (msg &optional folder)
1964 "Return the file name of MSG in FOLDER (default current folder)."
1965 (expand-file-name (int-to-string msg)
1966 (if folder
1967 (mh-expand-file-name folder)
1968 mh-folder-filename)))
1969
1970 (provide 'mh-folder)
1971
1972 ;; Local Variables:
1973 ;; indent-tabs-mode: nil
1974 ;; sentence-end-double-space: nil
1975 ;; End:
1976
1977 ;; arch-tag: aa97b758-d4f6-4c86-bc5a-1950921da1e7
1978 ;;; mh-folder.el ends here