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