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