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