mh-e.el 111 KB
Newer Older
Richard M. Stallman's avatar
Richard M. Stallman committed
1 2
;;; mh-e.el --- GNU Emacs interface to the MH mail system

Bill Wohler's avatar
Bill Wohler committed
3 4 5
;; Copyright (C) 1985, 1986, 1987, 1988,
;;  1990, 1992, 1993, 1994, 1995, 1997, 1999,
;;  2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
Richard M. Stallman's avatar
Richard M. Stallman committed
6

Bill Wohler's avatar
Bill Wohler committed
7
;; Author: Bill Wohler <wohler@newt.com>
Gerd Moellmann's avatar
Gerd Moellmann committed
8
;; Maintainer: Bill Wohler <wohler@newt.com>
9
;; Version: 7.85+cvs
Richard M. Stallman's avatar
Richard M. Stallman committed
10 11
;; Keywords: mail

12
;; This file is part of GNU Emacs.
Karl Heuer's avatar
Karl Heuer committed
13 14

;; GNU Emacs is free software; you can redistribute it and/or modify
Richard M. Stallman's avatar
Richard M. Stallman committed
15 16 17 18
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.

Karl Heuer's avatar
Karl Heuer committed
19
;; GNU Emacs is distributed in the hope that it will be useful,
Richard M. Stallman's avatar
Richard M. Stallman committed
20 21 22 23 24
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
Erik Naggum's avatar
Erik Naggum committed
25
;; along with GNU Emacs; see the file COPYING.  If not, write to the
Lute Kamstra's avatar
Lute Kamstra committed
26 27
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
Richard M. Stallman's avatar
Richard M. Stallman committed
28 29 30

;;; Commentary:

Bill Wohler's avatar
Bill Wohler committed
31 32 33 34
;; How to Use:
;;   M-x mh-rmail to read mail.  Type C-h m there for a list of commands.
;;   C-u M-x mh-rmail to visit any folder.
;;   M-x mh-smail to send mail.  From within the mail reader, "m" works, too.
Erik Naggum's avatar
Erik Naggum committed
35

Bill Wohler's avatar
Bill Wohler committed
36 37 38 39
;; Your .emacs might benefit from these bindings:
;;   (global-set-key "\C-cr" 'mh-rmail)
;;   (global-set-key "\C-xm" 'mh-smail)
;;   (global-set-key "\C-x4m" 'mh-smail-other-window)
Erik Naggum's avatar
Erik Naggum committed
40

Bill Wohler's avatar
Bill Wohler committed
41
;; MH (Message Handler) is a powerful mail reader.
Erik Naggum's avatar
Erik Naggum committed
42

Bill Wohler's avatar
Bill Wohler committed
43 44
;; The MH newsgroup is comp.mail.mh; the mailing list is mh-users@ics.uci.edu
;; (send to mh-users-request to be added). See the monthly Frequently Asked
Bill Wohler's avatar
Bill Wohler committed
45
;; Questions posting there for information on getting MH and MH-E:
Bill Wohler's avatar
Bill Wohler committed
46
;;   http://www.faqs.org/faqs/mail/mh-faq/part1/preamble.html
Erik Naggum's avatar
Erik Naggum committed
47

Bill Wohler's avatar
Bill Wohler committed
48
;; N.B. MH must have been compiled with the MHE compiler flag or several
Bill Wohler's avatar
Bill Wohler committed
49
;; features necessary for MH-E will be missing from MH commands, specifically
Erik Naggum's avatar
Erik Naggum committed
50 51
;; the -build switch to repl and forw.

Bill Wohler's avatar
Bill Wohler committed
52
;; MH-E is an Emacs interface to the MH mail system.
Bill Wohler's avatar
Bill Wohler committed
53

Bill Wohler's avatar
Bill Wohler committed
54
;; MH-E is supported in GNU Emacs 20 and 21, with MH 6.8.4 and nmh 1.0.4.
Bill Wohler's avatar
Bill Wohler committed
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73

;; Mailing Lists:
;;   mh-e-users@lists.sourceforge.net
;;   mh-e-announce@lists.sourceforge.net
;;   mh-e-devel@lists.sourceforge.net
;;
;;   Subscribe by sending a "subscribe" message to
;;   <list>-request@lists.sourceforge.net, or by using the web interface at
;;   https://sourceforge.net/mail/?group_id=13357

;; Bug Reports:
;;   https://sourceforge.net/tracker/?group_id=13357&atid=113357
;;   Include the output of M-x mh-version in any bug report.

;; Feature Requests:
;;   https://sourceforge.net/tracker/?atid=363357&group_id=13357&func=browse

;; Support:
;;   https://sourceforge.net/tracker/?group_id=13357&atid=213357
Erik Naggum's avatar
Erik Naggum committed
74

Erik Naggum's avatar
Erik Naggum committed
75
;;; Change Log:
Erik Naggum's avatar
Erik Naggum committed
76 77 78

;; Original version for Gosling emacs by Brian Reid, Stanford, 1982.
;; Modified by James Larus, BBN, July 1984 and UCB, 1984 & 1985.
Bill Wohler's avatar
Bill Wohler committed
79 80 81 82
;; Rewritten for GNU Emacs, James Larus, 1985.
;; Modified by Stephen Gildea, 1988.
;; Maintenance picked up by Bill Wohler and the
;; SourceForge Crew <http://mh-e.sourceforge.net/>, 2001.
Bill Wohler's avatar
Bill Wohler committed
83

Richard M. Stallman's avatar
Richard M. Stallman committed
84 85
;;; Code:

Bill Wohler's avatar
Bill Wohler committed
86
(provide 'mh-e)
Bill Wohler's avatar
Bill Wohler committed
87

Bill Wohler's avatar
Bill Wohler committed
88 89 90 91
(eval-when-compile (require 'mh-acros))
(mh-require-cl)
(require 'mh-utils)
(require 'mh-init)
Bill Wohler's avatar
Bill Wohler committed
92
(require 'mh-inc)
Bill Wohler's avatar
Bill Wohler committed
93
(require 'mh-seq)
Bill Wohler's avatar
Bill Wohler committed
94 95 96
(require 'gnus-util)
(require 'easymenu)

Bill Wohler's avatar
Bill Wohler committed
97 98 99
;; Shush the byte-compiler
(defvar font-lock-auto-fontify)
(defvar font-lock-defaults)
Bill Wohler's avatar
Bill Wohler committed
100

101
(defconst mh-version "7.85+cvs" "Version number of MH-E.")
Richard M. Stallman's avatar
Richard M. Stallman committed
102 103 104

(defvar mh-partial-folder-mode-line-annotation "select"
  "Annotation when displaying part of a folder.
Bill Wohler's avatar
Bill Wohler committed
105
The string is displayed after the folder's name.  nil for no annotation.")
Bill Wohler's avatar
Bill Wohler committed
106

Bill Wohler's avatar
Bill Wohler committed
107

Bill Wohler's avatar
Bill Wohler committed
108

Bill Wohler's avatar
Bill Wohler committed
109 110
;;; Scan Line Formats

Bill Wohler's avatar
Bill Wohler committed
111 112 113
;; Parameterize MH-E to work with different scan formats.  The defaults work
;; with the standard MH scan listings, in which the first 4 characters on
;; the line are the message number, followed by two places for notations.
Bill Wohler's avatar
Bill Wohler committed
114

Bill Wohler's avatar
Bill Wohler committed
115 116 117
;; The following scan formats are passed to the scan program if the setting of
;; `mh-scan-format-file' is t. They are identical except the later one makes
;; use of the nmh `decode' function to decode RFC 2047 encodings. If you just
118
;; want to change the column of the notations, use the `mh-set-cmd-note'
Bill Wohler's avatar
Bill Wohler committed
119
;; function.
Bill Wohler's avatar
Bill Wohler committed
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134

(defvar mh-scan-format-mh
  (concat
   "%4(msg)"
   "%<(cur)+%| %>"
   "%<{replied}-"
   "%?(nonnull(comp{to}))%<(mymbox{to})t%>"
   "%?(nonnull(comp{cc}))%<(mymbox{cc})c%>"
   "%?(nonnull(comp{bcc}))%<(mymbox{bcc})b%>"
   "%?(nonnull(comp{newsgroups}))n%>"
   "%<(zero) %>"
   "%02(mon{date})/%02(mday{date})%<{date} %|*%>"
   "%<(mymbox{from})%<{to}To:%14(friendly{to})%>%>"
   "%<(zero)%17(friendly{from})%>  "
   "%{subject}%<{body}<<%{body}%>")
135
  "*Scan format string for MH.
136
This string is passed to the scan program via the -format argument.
Bill Wohler's avatar
Bill Wohler committed
137
This format is identical to the default except that additional hints for
Bill Wohler's avatar
Bill Wohler committed
138 139
fontification have been added to the fifth column (remember that in Emacs, the
first column is 0).
Bill Wohler's avatar
Bill Wohler committed
140

Bill Wohler's avatar
Bill Wohler committed
141 142 143 144
The values of the fifth column, in priority order, are: `-' if the message has
been replied to, t if an address on the To: line matches one of the
mailboxes of the current user, `c' if the Cc: line matches, `b' if the Bcc:
line matches, and `n' if a non-empty Newsgroups: header is present.")
Bill Wohler's avatar
Bill Wohler committed
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159

(defvar mh-scan-format-nmh
  (concat
   "%4(msg)"
   "%<(cur)+%| %>"
   "%<{replied}-"
   "%?(nonnull(comp{to}))%<(mymbox{to})t%>"
   "%?(nonnull(comp{cc}))%<(mymbox{cc})c%>"
   "%?(nonnull(comp{bcc}))%<(mymbox{bcc})b%>"
   "%?(nonnull(comp{newsgroups}))n%>"
   "%<(zero) %>"
   "%02(mon{date})/%02(mday{date})%<{date} %|*%>"
   "%<(mymbox{from})%<{to}To:%14(decode(friendly{to}))%>%>"
   "%<(zero)%17(decode(friendly{from}))%>  "
   "%(decode{subject})%<{body}<<%{body}%>")
Bill Wohler's avatar
Bill Wohler committed
160 161
  "*Scan format string for nmh.
This string is passed to the scan program via the -format arg.
Bill Wohler's avatar
Bill Wohler committed
162
This format is identical to the default except that additional hints for
Bill Wohler's avatar
Bill Wohler committed
163 164
fontification have been added to the fifth column (remember that in Emacs, the
first column is 0).
Bill Wohler's avatar
Bill Wohler committed
165

Bill Wohler's avatar
Bill Wohler committed
166 167 168 169 170 171
The values of the fifth column, in priority order, are: `-' if the message has
been replied to, t if an address on the To: field matches one of the
mailboxes of the current user, `c' if the Cc: field matches, `b' if the Bcc:
field matches, and `n' if a non-empty Newsgroups: field is present.")

(defvar mh-note-deleted ?D
172
  "Messages that have been deleted are marked by this character.
Bill Wohler's avatar
Bill Wohler committed
173 174 175
See also `mh-scan-deleted-msg-regexp'.")

(defvar mh-note-refiled ?^
176
  "Messages that have been refiled are marked by this character.
Bill Wohler's avatar
Bill Wohler committed
177 178 179
See also `mh-scan-refiled-msg-regexp'.")

(defvar mh-note-cur ?+
180
  "The current message (in MH, not in MH-E) is marked by this character.
Bill Wohler's avatar
Bill Wohler committed
181
See also `mh-scan-cur-msg-number-regexp'.")
Bill Wohler's avatar
Bill Wohler committed
182

Bill Wohler's avatar
Bill Wohler committed
183
(defvar mh-scan-good-msg-regexp  "^\\( *[0-9]+\\)[^D^0-9]"
184 185 186 187
  "This regular expression matches \"good\" messages.
It must match from the beginning of the line. Note that the default setting of
`mh-folder-font-lock-keywords' expects this expression to contain at least one
parenthesized expression which matches the message number as in the default of
188 189
\"^\\\\( *[0-9]+\\\\)[^D^0-9]\". This expression includes the leading space
within the parenthesis since it looks better to highlight it as well. This
190
regular expression should be correct as it is needed by non-fontification
191
functions.")
Bill Wohler's avatar
Bill Wohler committed
192

Bill Wohler's avatar
Bill Wohler committed
193
(defvar mh-scan-deleted-msg-regexp "^\\( *[0-9]+\\)D"
194 195 196 197
  "This regular expression matches deleted messages.
It must match from the beginning of the line. Note that the default setting of
`mh-folder-font-lock-keywords' expects this expression to contain at least one
parenthesized expression which matches the message number as in the default of
198 199
\"^\\\\( *[0-9]+\\\\)D\". This expression includes the leading space within
the parenthesis since it looks better to highlight it as well. This regular
200 201
expression should be correct as it is needed by non-fontification functions.
See also `mh-note-deleted'.")
Bill Wohler's avatar
Bill Wohler committed
202

Bill Wohler's avatar
Bill Wohler committed
203
(defvar mh-scan-refiled-msg-regexp  "^\\( *[0-9]+\\)\\^"
204 205 206 207
  "This regular expression matches refiled messages.
It must match from the beginning of the line. Note that the default setting of
`mh-folder-font-lock-keywords' expects this expression to contain at least one
parenthesized expression which matches the message number as in the default of
208 209
\"^\\\\( *[0-9]+\\\\)\\\\^\". This expression includes the leading space
within the parenthesis since it looks better to highlight it as well. This
210
regular expression should be correct as it is needed by non-fontification
211
functions. See also `mh-note-refiled'.")
Bill Wohler's avatar
Bill Wohler committed
212 213

(defvar mh-scan-valid-regexp "^ *[0-9]"
214 215 216
  "This regular expression describes a valid scan line.
This is used to eliminate error messages that are occasionally produced by
\"inc\".")
Bill Wohler's avatar
Bill Wohler committed
217

Bill Wohler's avatar
Bill Wohler committed
218
(defvar mh-scan-cur-msg-number-regexp "^\\( *[0-9]+\\+\\).*"
219 220 221 222
  "This regular expression matches the current message.
It must match from the beginning of the line. Note that the default setting of
`mh-folder-font-lock-keywords' expects this expression to contain at least one
parenthesized expression which matches the message number as in the default of
223 224 225
\"^\\\\( *[0-9]+\\\\+\\\\).*\". This expression includes the leading space and
current message marker \"+\" within the parenthesis since it looks better to
highlight these items as well. This regular expression should be correct as it
226
is needed by non-fontification functions. See also `mh-note-cur'.")
Bill Wohler's avatar
Bill Wohler committed
227 228

(defvar mh-scan-date-regexp "\\([0-9][0-9]/[0-9][0-9]\\)"
229 230 231 232 233 234 235
  "This regular expression matches a valid date.
It must not be anchored to the beginning or the end of the line. Note that the
default setting of `mh-folder-font-lock-keywords' expects this expression to
contain only one parenthesized expression which matches the date field as in
the default of \"\\\\([0-9][0-9]/[0-9][0-9]\\\\)\"}. If this regular
expression is not correct, the date will not be highlighted. See also
`mh-scan-format-regexp'.")
Bill Wohler's avatar
Bill Wohler committed
236 237

(defvar mh-scan-rcpt-regexp  "\\(To:\\)\\(..............\\)"
238
  "This regular expression specifies the recipient in messages you sent.
Bill Wohler's avatar
Bill Wohler committed
239 240 241 242
Note that the default setting of `mh-folder-font-lock-keywords'
expects this expression to contain two parenthesized expressions. The
first is expected to match the `To:' that the default scan format
file generates. The second is expected to match the recipient's name
243 244
as in the default of \"\\\\(To:\\\\)\\\\(..............\\\\)\". If this
regular expression is not correct, the recipient will not be highlighted.")
Bill Wohler's avatar
Bill Wohler committed
245 246

(defvar mh-scan-body-regexp "\\(<<\\([^\n]+\\)?\\)"
247
  "This regular expression matches the message body fragment.
Bill Wohler's avatar
Bill Wohler committed
248 249
Note that the default setting of `mh-folder-font-lock-keywords' expects this
expression to contain at least one parenthesized expression which matches the
250 251
body text as in the default of \"\\\\(<<\\\\([^\\n]+\\\\)?\\\\)\". If this
regular expression is not correct, the body fragment will not be highlighted.")
Bill Wohler's avatar
Bill Wohler committed
252 253

(defvar mh-scan-subject-regexp
Bill Wohler's avatar
Bill Wohler committed
254
  "^ *[0-9]+........[ ]*...................\\([Rr][Ee]\\(\\[[0-9]+\\]\\)?:\\s-*\\)*\\([^<\n]*\\)"
255 256 257 258 259 260 261 262 263 264 265
  "This regular expression matches the subject.
It must match from the beginning of the line. Note that the default setting
of `mh-folder-font-lock-keywords' expects this expression to contain at least
three parenthesized expressions. The first is expected to match the `Re:'
string, if any. The second matches an optional bracketed number after `Re:',
such as in `Re[2]:' (and is thus a sub-expression of the first expression) and
the third is expected to match the subject line itself as in the default of
\(broken on multiple lines for readability):
  ^ *[0-9]+........[ ]*...................
  \\\\([Rr][Ee]\\\\(\\\\\\=[[0-9]+\\\\]\\\\)?:\\\\s-*\\\\)*
  \\\\([^<\\n]*\\\\)
266
This regular expression should be correct as it is needed by non-fontification
267
functions.")
Bill Wohler's avatar
Bill Wohler committed
268 269

(defvar mh-scan-format-regexp
Bill Wohler's avatar
Bill Wohler committed
270
  (concat "\\([bct]\\)" mh-scan-date-regexp " *\\(..................\\)")
271
  "This regular expression matches the output of scan.
Bill Wohler's avatar
Bill Wohler committed
272 273
Note that the default setting of `mh-folder-font-lock-keywords' expects this
expression to contain at least three parenthesized expressions. The first
274 275 276 277 278 279
should match the fontification hint (see `mh-scan-format-nmh'), the second is
found in `mh-scan-date-regexp', and the third should match the user name as in
the default of \"(concat \"\\\\([bct]\\\\)\" mh-scan-date-regexp
                         \"*\\\\(..................\\\\)\")\".
If this regular expression is not correct, the notation hints and the sender
will not be highlighted.")
Bill Wohler's avatar
Bill Wohler committed
280

Bill Wohler's avatar
Bill Wohler committed
281 282


Bill Wohler's avatar
Bill Wohler committed
283 284
(defvar mh-folder-font-lock-keywords
  (list
Bill Wohler's avatar
Bill Wohler committed
285 286 287
   ;; Folders when displaying index buffer
   (list "^\\+.*"
         '(0 mh-index-folder-face))
Bill Wohler's avatar
Bill Wohler committed
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
   ;; Marked for deletion
   (list (concat mh-scan-deleted-msg-regexp ".*")
         '(0 mh-folder-deleted-face))
   ;; Marked for refile
   (list (concat mh-scan-refiled-msg-regexp ".*")
         '(0 mh-folder-refiled-face))
   ;;after subj
   (list mh-scan-body-regexp '(1 mh-folder-body-face nil t))
   '(mh-folder-font-lock-subject
     (1 mh-folder-followup-face append t)
     (2 mh-folder-subject-face append t))
   ;;current msg
   (list mh-scan-cur-msg-number-regexp
         '(1 mh-folder-cur-msg-number-face))
   (list mh-scan-good-msg-regexp
Bill Wohler's avatar
Bill Wohler committed
303 304
         '(1 mh-folder-msg-number-face)) ;; Msg number
   (list mh-scan-date-regexp '(1 mh-folder-date-face)) ;; Date
Bill Wohler's avatar
Bill Wohler committed
305
   (list mh-scan-rcpt-regexp
Bill Wohler's avatar
Bill Wohler committed
306 307
         '(1 mh-folder-to-face) ;; To:
         '(2 mh-folder-address-face)) ;; address
Bill Wohler's avatar
Bill Wohler committed
308 309 310
   ;; scan font-lock name
   (list mh-scan-format-regexp
         '(1 mh-folder-date-face)
Bill Wohler's avatar
Bill Wohler committed
311
         '(3 mh-folder-scan-format-face)))
312
  "Keywords (regular expressions) used to fontify the MH-Folder buffer.")
Bill Wohler's avatar
Bill Wohler committed
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349

(defvar mh-scan-cmd-note-width 1
  "Number of columns consumed by the cmd-note field in `mh-scan-format'.
This column will have one of the values: ` ', `D', `^', `+' and where
` ' is the default value,
`D' is the `mh-note-deleted' character,
`^' is the `mh-note-refiled' character, and
`+' is the `mh-note-cur' character.")

(defvar mh-scan-destination-width 1
  "Number of columns consumed by the destination field in `mh-scan-format'.
This column will have one of ' ', '%', '-', 't', 'c', 'b', or `n' in it.
A ' ' blank space is the default character.
A '%' indicates that the message in in a named MH sequence.
A '-' indicates that the message has been annotated with a replied field.
A 't' indicates that the message contains mymbox in the To: field.
A 'c' indicates that the message contains mymbox in the Cc: field.
A 'b' indicates that the message contains mymbox in the Bcc: field.
A 'n' indicates that the message contains a Newsgroups: field.")

(defvar mh-scan-date-width 5
  "Number of columns consumed by the date field in `mh-scan-format'.
This column will typically be of the form mm/dd.")

(defvar mh-scan-date-flag-width 1
  "Number of columns consumed to flag (in)valid dates in `mh-scan-format'.
This column will have ` ' for valid and `*' for invalid or missing dates.")

(defvar mh-scan-from-mbox-width 17
  "Number of columns consumed with the \"From:\" line in `mh-scan-format'.
This column will have a friendly name or e-mail address of the
originator, or a \"To: address\" for outgoing e-mail messages.")

(defvar mh-scan-from-mbox-sep-width 2
  "Number of columns consumed by whitespace after from-mbox in `mh-scan-format'.
This column will only ever have spaces in it.")

350 351 352 353
(defvar mh-scan-field-destination-offset
  (+ mh-scan-cmd-note-width)
  "The offset from the `mh-cmd-note' for the destination column.")

Bill Wohler's avatar
Bill Wohler committed
354
(defvar mh-scan-field-from-start-offset
Bill Wohler's avatar
Bill Wohler committed
355 356 357 358 359
  (+ mh-scan-cmd-note-width
     mh-scan-destination-width
     mh-scan-date-width
     mh-scan-date-flag-width)
  "The offset from the `mh-cmd-note' to find the start of \"From:\" address.")
Bill Wohler's avatar
Bill Wohler committed
360 361

(defvar mh-scan-field-from-end-offset
Bill Wohler's avatar
Bill Wohler committed
362 363
  (+ mh-scan-field-from-start-offset mh-scan-from-mbox-width)
  "The offset from the `mh-cmd-note' to find the end of \"From:\" address.")
Bill Wohler's avatar
Bill Wohler committed
364 365 366 367 368 369 370 371 372

(defvar mh-scan-field-subject-start-offset
  (+ mh-scan-cmd-note-width
     mh-scan-destination-width
     mh-scan-date-width
     mh-scan-date-flag-width
     mh-scan-from-mbox-width
     mh-scan-from-mbox-sep-width)
  "The offset from the `mh-cmd-note' to find the start of the subject.")
Bill Wohler's avatar
Bill Wohler committed
373 374

(defun mh-folder-font-lock-subject (limit)
Bill Wohler's avatar
Bill Wohler committed
375
  "Return MH-E scan subject strings to font-lock between point and LIMIT."
Bill Wohler's avatar
Bill Wohler committed
376 377 378
  (if (not (re-search-forward mh-scan-subject-regexp limit t))
      nil
    (if (match-beginning 1)
Bill Wohler's avatar
Bill Wohler committed
379 380 381 382
        (set-match-data (list (match-beginning 1) (match-end 3)
                              (match-beginning 1) (match-end 3) nil nil))
      (set-match-data (list (match-beginning 3) (match-end 3)
                            nil nil (match-beginning 3) (match-end 3))))
Bill Wohler's avatar
Bill Wohler committed
383 384
    t))

Bill Wohler's avatar
Bill Wohler committed
385 386 387 388


;; Fontifify unseen mesages in bold.

Bill Wohler's avatar
Bill Wohler committed
389 390 391 392 393 394 395 396 397 398 399
(defmacro mh-generate-sequence-font-lock (seq prefix face)
  "Generate the appropriate code to fontify messages in SEQ.
PREFIX is used to generate unique names for the variables and functions
defined by the macro. So a different prefix should be provided for every
invocation.
FACE is the font-lock face used to display the matching scan lines."
  (let ((cache (intern (format "mh-folder-%s-seq-cache" prefix)))
        (func (intern (format "mh-folder-font-lock-%s" prefix))))
    `(progn
       (defvar ,cache nil
         "Internal cache variable used for font-lock in MH-E.
Bill Wohler's avatar
Bill Wohler committed
400 401
Should only be non-nil through font-lock stepping, and nil once font-lock
is done highlighting.")
Bill Wohler's avatar
Bill Wohler committed
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
       (make-variable-buffer-local ',cache)

       (defun ,func (limit)
         "Return unseen message lines to font-lock between point and LIMIT."
         (if (not ,cache) (setq ,cache (mh-seq-msgs (mh-find-seq ,seq))))
         (let ((cur-msg (mh-get-msg-num nil)))
           (cond ((not ,cache)
                  nil)
                 ((>= (point) limit)              ;Presumably at end of buffer
                  (setq ,cache nil)
                  nil)
                 ((member cur-msg ,cache)
                  (let ((bpoint (progn (beginning-of-line)(point)))
                        (epoint (progn (forward-line 1)(point))))
                    (if (<= limit (point)) (setq  ,cache nil))
                    (set-match-data (list bpoint epoint bpoint epoint))
                    t))
                 (t
                  ;; move forward one line at a time, checking each message
                  (while (and (= 0 (forward-line 1))
                              (> limit (point))
                              (not (member (mh-get-msg-num nil) ,cache))))
                  ;; Examine how we must have exited the loop...
                  (let ((cur-msg (mh-get-msg-num nil)))
                    (cond ((or (<= limit (point))
                               (not (member cur-msg ,cache)))
                           (setq ,cache nil)
                           nil)
                          ((member cur-msg ,cache)
                           (let ((bpoint (progn (beginning-of-line) (point)))
                                 (epoint (progn (forward-line 1) (point))))
                             (if (<= limit (point)) (setq ,cache nil))
                             (set-match-data
                              (list bpoint epoint bpoint epoint))
                             t))))))))

       (setq mh-folder-font-lock-keywords
             (append mh-folder-font-lock-keywords
                     (list (list ',func (list 1 '',face 'prepend t))))))))

(mh-generate-sequence-font-lock mh-unseen-seq unseen bold)
443
(mh-generate-sequence-font-lock mh-tick-seq tick mh-folder-tick)
Bill Wohler's avatar
Bill Wohler committed
444 445


Richard M. Stallman's avatar
Richard M. Stallman committed
446 447 448

;;; Internal variables:

Bill Wohler's avatar
Bill Wohler committed
449 450
(defvar mh-last-destination nil)        ;Destination of last refile or write
                                        ;command.
Bill Wohler's avatar
Bill Wohler committed
451 452
(defvar mh-last-destination-folder nil) ;Destination of last refile command.
(defvar mh-last-destination-write nil)  ;Destination of last write command.
Richard M. Stallman's avatar
Richard M. Stallman committed
453 454 455 456

(defvar mh-folder-mode-map (make-keymap)
  "Keymap for MH folders.")

Bill Wohler's avatar
Bill Wohler committed
457 458
(defvar mh-arrow-marker nil)            ;Marker for arrow display in fringe.

Bill Wohler's avatar
Bill Wohler committed
459
(defvar mh-delete-list nil)             ;List of msg numbers to delete.
Richard M. Stallman's avatar
Richard M. Stallman committed
460

Bill Wohler's avatar
Bill Wohler committed
461
(defvar mh-refile-list nil)             ;List of folder names in mh-seq-list.
Richard M. Stallman's avatar
Richard M. Stallman committed
462

463 464
(defvar mh-folders-changed nil)         ;For mh-after-commands-processed-hook.

Bill Wohler's avatar
Bill Wohler committed
465
(defvar mh-next-direction 'forward)     ;Direction to move to next message.
Richard M. Stallman's avatar
Richard M. Stallman committed
466

Bill Wohler's avatar
Bill Wohler committed
467 468
(defvar mh-view-ops ())                 ;Stack of ops that change the folder
                                        ;view (such as narrowing or threading).
Bill Wohler's avatar
Bill Wohler committed
469
(defvar mh-folder-view-stack ())        ;Stack of previous folder views.
Richard M. Stallman's avatar
Richard M. Stallman committed
470

Bill Wohler's avatar
Bill Wohler committed
471 472 473 474
(defvar mh-index-data nil)              ;Info about index search results
(defvar mh-index-previous-search nil)
(defvar mh-index-msg-checksum-map nil)
(defvar mh-index-checksum-origin-map nil)
Bill Wohler's avatar
Bill Wohler committed
475
(defvar mh-index-sequence-search-flag nil)
Bill Wohler's avatar
Bill Wohler committed
476 477

(defvar mh-first-msg-num nil)           ;Number of first msg in buffer.
Richard M. Stallman's avatar
Richard M. Stallman committed
478

Bill Wohler's avatar
Bill Wohler committed
479
(defvar mh-last-msg-num nil)            ;Number of last msg in buffer.
Richard M. Stallman's avatar
Richard M. Stallman committed
480

Bill Wohler's avatar
Bill Wohler committed
481
(defvar mh-mode-line-annotation nil)    ;Message range displayed in buffer.
Richard M. Stallman's avatar
Richard M. Stallman committed
482

Bill Wohler's avatar
Bill Wohler committed
483 484 485 486
(defvar mh-sequence-notation-history nil)
                                        ;Rememeber original notation that
                                        ;is overwritten by `mh-note-seq'.

Bill Wohler's avatar
Bill Wohler committed
487 488
(defvar mh-colors-available-flag nil)   ;Are colors available?

Bill Wohler's avatar
Bill Wohler committed
489 490


Richard M. Stallman's avatar
Richard M. Stallman committed
491 492
;;; Macros and generic functions:

Bill Wohler's avatar
Bill Wohler committed
493 494
(defun mh-mapc (function list)
  "Apply FUNCTION to each element of LIST for side effects only."
Richard M. Stallman's avatar
Richard M. Stallman committed
495
  (while list
Bill Wohler's avatar
Bill Wohler committed
496
    (funcall function (car list))
Richard M. Stallman's avatar
Richard M. Stallman committed
497 498
    (setq list (cdr list))))

Bill Wohler's avatar
Bill Wohler committed
499
(defun mh-scan-format ()
Bill Wohler's avatar
Bill Wohler committed
500
  "Return the output format argument for the scan program."
Bill Wohler's avatar
Bill Wohler committed
501
  (if (equal mh-scan-format-file t)
Bill Wohler's avatar
Bill Wohler committed
502
      (list "-format" (if (mh-variant-p 'nmh 'mu-mh)
Bill Wohler's avatar
Bill Wohler committed
503 504 505 506
                          (list (mh-update-scan-format
                                 mh-scan-format-nmh mh-cmd-note))
                        (list (mh-update-scan-format
                               mh-scan-format-mh mh-cmd-note))))
Bill Wohler's avatar
Bill Wohler committed
507
    (if (not (equal mh-scan-format-file nil))
Bill Wohler's avatar
Bill Wohler committed
508
        (list "-form" mh-scan-format-file))))
Bill Wohler's avatar
Bill Wohler committed
509

Richard M. Stallman's avatar
Richard M. Stallman committed
510 511 512 513 514 515


;;; Entry points:

;;;###autoload
(defun mh-rmail (&optional arg)
Bill Wohler's avatar
Bill Wohler committed
516
  "Inc(orporate) new mail with MH.
Bill Wohler's avatar
Bill Wohler committed
517
Scan an MH folder if ARG is non-nil. This function is an entry point to MH-E,
Bill Wohler's avatar
Bill Wohler committed
518
the Emacs interface to the MH mail system."
Richard M. Stallman's avatar
Richard M. Stallman committed
519 520 521 522
  (interactive "P")
  (mh-find-path)
  (if arg
      (call-interactively 'mh-visit-folder)
Bill Wohler's avatar
Bill Wohler committed
523 524
    (unless (get-buffer mh-inbox)
      (mh-visit-folder mh-inbox (symbol-name mh-unseen-seq)))
Bill Wohler's avatar
Bill Wohler committed
525
    (mh-inc-folder)))
Richard M. Stallman's avatar
Richard M. Stallman committed
526

Bill Wohler's avatar
Bill Wohler committed
527 528 529
;;;###autoload
(defun mh-nmail (&optional arg)
  "Check for new mail in inbox folder.
Bill Wohler's avatar
Bill Wohler committed
530
Scan an MH folder if ARG is non-nil. This function is an entry point to MH-E,
Bill Wohler's avatar
Bill Wohler committed
531
the Emacs interface to the MH mail system."
Bill Wohler's avatar
Bill Wohler committed
532
  (interactive "P")
Bill Wohler's avatar
Bill Wohler committed
533
  (mh-find-path)                        ; init mh-inbox
Bill Wohler's avatar
Bill Wohler committed
534 535 536
  (if arg
      (call-interactively 'mh-visit-folder)
    (mh-visit-folder mh-inbox)))
Richard M. Stallman's avatar
Richard M. Stallman committed
537 538 539



Bill Wohler's avatar
Bill Wohler committed
540
;;; User executable MH-E commands:
Richard M. Stallman's avatar
Richard M. Stallman committed
541

Bill Wohler's avatar
Bill Wohler committed
542
(defun mh-delete-msg (range)
543
  "Delete RANGE\\<mh-folder-mode-map>.
544 545 546 547 548 549 550 551

To mark a message for deletion, use this command. A \"D\" is placed by the
message in the scan window, and the next undeleted message is displayed. If
the previous command had been \\[mh-previous-undeleted-msg], then the next
message displayed is the first undeleted message previous to the message just
deleted. Use \\[mh-next-undeleted-msg] to force subsequent \\[mh-delete-msg]
commands to move forward to the next undeleted message after deleting the
message under the cursor.
Bill Wohler's avatar
Bill Wohler committed
552 553 554 555 556 557 558 559

Check the documentation of `mh-interactive-range' to see how RANGE is read in
interactive use."
  (interactive (list (mh-interactive-range "Delete")))
  (mh-delete-msg-no-motion range)
  (if (looking-at mh-scan-deleted-msg-regexp) (mh-next-msg)))

(defun mh-delete-msg-no-motion (range)
560
  "Delete RANGE, don't move to next message.
561

562 563
This command marks the RANGE for deletion but leaves the cursor at the current
message in case you wish to perform other operations on the message.
Bill Wohler's avatar
Bill Wohler committed
564 565 566 567 568

Check the documentation of `mh-interactive-range' to see how RANGE is read in
interactive use."
  (interactive (list (mh-interactive-range "Delete")))
  (mh-iterate-on-range () range
Bill Wohler's avatar
Bill Wohler committed
569
    (mh-delete-a-msg nil)))
Richard M. Stallman's avatar
Richard M. Stallman committed
570 571 572 573

(defun mh-execute-commands ()
  "Process outstanding delete and refile requests."
  (interactive)
Bill Wohler's avatar
Bill Wohler committed
574
  (if mh-folder-view-stack (mh-widen t))
Richard M. Stallman's avatar
Richard M. Stallman committed
575 576
  (mh-process-commands mh-current-folder)
  (mh-set-scan-mode)
Bill Wohler's avatar
Bill Wohler committed
577
  (mh-goto-cur-msg)                    ; after mh-set-scan-mode for efficiency
Richard M. Stallman's avatar
Richard M. Stallman committed
578
  (mh-make-folder-mode-line)
Bill Wohler's avatar
Bill Wohler committed
579
  t)                                    ; return t for write-file-functions
Richard M. Stallman's avatar
Richard M. Stallman committed
580 581

(defun mh-first-msg ()
582
  "Display first message."
Richard M. Stallman's avatar
Richard M. Stallman committed
583
  (interactive)
Karl Heuer's avatar
Karl Heuer committed
584
  (goto-char (point-min))
Bill Wohler's avatar
Bill Wohler committed
585
  (while (and (not (eobp)) (not (looking-at mh-scan-valid-regexp)))
Karl Heuer's avatar
Karl Heuer committed
586
    (forward-line 1)))
Richard M. Stallman's avatar
Richard M. Stallman committed
587 588

(defun mh-header-display ()
589 590 591
  "Display message with all header fields\\<mh-folder-mode-map>.

Use the command \\[mh-show] to show the message normally again."
Richard M. Stallman's avatar
Richard M. Stallman committed
592 593
  (interactive)
  (and (not mh-showing-with-headers)
594
       (or mh-mhl-format-file mh-clean-message-header-flag)
Richard M. Stallman's avatar
Richard M. Stallman committed
595
       (mh-invalidate-show-buffer))
Bill Wohler's avatar
Bill Wohler committed
596
  (let ((mh-decode-mime-flag nil)
597
        (mh-mhl-format-file nil)
Bill Wohler's avatar
Bill Wohler committed
598
        (mh-clean-message-header-flag nil))
Richard M. Stallman's avatar
Richard M. Stallman committed
599 600 601 602 603 604
    (mh-show-msg nil)
    (mh-in-show-buffer (mh-show-buffer)
      (goto-char (point-min))
      (mh-recenter 0))
    (setq mh-showing-with-headers t)))

Bill Wohler's avatar
Bill Wohler committed
605
(defun mh-inc-folder (&optional maildrop-name folder)
Karl Heuer's avatar
Karl Heuer committed
606
  "Inc(orporate)s new mail into the Inbox folder.
Bill Wohler's avatar
Bill Wohler committed
607
Optional argument MAILDROP-NAME specifies an alternate maildrop from the
Bill Wohler's avatar
Bill Wohler committed
608 609
default. The optional argument FOLDER specifies where to incorporate mail
instead of the default named by `mh-inbox'.
Bill Wohler's avatar
Bill Wohler committed
610 611 612
The value of `mh-inc-folder-hook' is a list of functions to be called, with no
arguments, after incorporating new mail.
Do not call this function from outside MH-E; use \\[mh-rmail] instead."
Richard M. Stallman's avatar
Richard M. Stallman committed
613
  (interactive (list (if current-prefix-arg
Bill Wohler's avatar
Bill Wohler committed
614 615
                         (expand-file-name
                          (read-file-name "inc mail from file: "
Bill Wohler's avatar
Bill Wohler committed
616 617 618 619 620
                                          mh-user-path)))
                     (if current-prefix-arg
                         (mh-prompt-for-folder "inc mail into" mh-inbox t))))
  (if (not folder)
      (setq folder mh-inbox))
Bill Wohler's avatar
Bill Wohler committed
621 622
  (let ((threading-needed-flag nil))
    (let ((config (current-window-configuration)))
Bill Wohler's avatar
Bill Wohler committed
623 624
      (when (and mh-show-buffer (get-buffer mh-show-buffer))
        (delete-windows-on mh-show-buffer))
Bill Wohler's avatar
Bill Wohler committed
625 626 627 628 629 630 631
      (cond ((not (get-buffer folder))
             (mh-make-folder folder)
             (setq threading-needed-flag mh-show-threads-flag)
             (setq mh-previous-window-config config))
            ((not (eq (current-buffer) (get-buffer folder)))
             (switch-to-buffer folder)
             (setq mh-previous-window-config config))))
Bill Wohler's avatar
Bill Wohler committed
632 633 634 635 636
    (mh-get-new-mail maildrop-name)
    (when (and threading-needed-flag
               (save-excursion
                 (goto-char (point-min))
                 (or (null mh-large-folder)
Bill Wohler's avatar
Bill Wohler committed
637
                     (not (equal (forward-line (1+ mh-large-folder)) 0))
Bill Wohler's avatar
Bill Wohler committed
638 639 640
                     (and (message "Not threading since the number of messages exceeds `mh-large-folder'")
                          nil))))
      (mh-toggle-threads))
Bill Wohler's avatar
Bill Wohler committed
641 642
    (beginning-of-line)
    (if (and mh-showing-mode (looking-at mh-scan-valid-regexp)) (mh-show))
Bill Wohler's avatar
Bill Wohler committed
643
    (run-hooks 'mh-inc-folder-hook)))
Richard M. Stallman's avatar
Richard M. Stallman committed
644 645

(defun mh-last-msg ()
646
  "Display last message."
Richard M. Stallman's avatar
Richard M. Stallman committed
647 648
  (interactive)
  (goto-char (point-max))
Bill Wohler's avatar
Bill Wohler committed
649
  (while (and (not (bobp)) (not (looking-at mh-scan-valid-regexp)))
Bill Wohler's avatar
Bill Wohler committed
650 651
    (forward-line -1))
  (mh-recenter nil))
Richard M. Stallman's avatar
Richard M. Stallman committed
652

653 654 655 656 657 658 659 660 661
(defun mh-next-undeleted-msg (&optional count wait-after-complaining-flag)
  "Display next message.

This command can be given a prefix argument COUNT to specify how many unread
messages to skip.

In a program, pause for a second after printing message if we are at the last
undeleted message and optional argument WAIT-AFTER-COMPLAINING-FLAG is
non-nil."
Karl Heuer's avatar
Karl Heuer committed
662
  (interactive "p")
Richard M. Stallman's avatar
Richard M. Stallman committed
663
  (setq mh-next-direction 'forward)
Karl Heuer's avatar
Karl Heuer committed
664
  (forward-line 1)
665
  (cond ((re-search-forward mh-scan-good-msg-regexp nil t count)
Bill Wohler's avatar
Bill Wohler committed
666 667 668
         (beginning-of-line)
         (mh-maybe-show))
        (t (forward-line -1)
Bill Wohler's avatar
Bill Wohler committed
669 670
           (message "No more undeleted messages")
           (if wait-after-complaining-flag (sit-for 1)))))
Richard M. Stallman's avatar
Richard M. Stallman committed
671

Bill Wohler's avatar
Bill Wohler committed
672
(defun mh-folder-from-address ()
Bill Wohler's avatar
Bill Wohler committed
673 674 675
  "Derive folder name from sender.

The name of the folder is derived as follows:
Bill Wohler's avatar
Bill Wohler committed
676

Bill Wohler's avatar
Bill Wohler committed
677 678 679 680 681
  a) The folder name associated with the first address found in the list
     `mh-default-folder-list' is used. Each element in this list contains a
     `Check Recipient' item. If this item is turned on, then the address is
     checked against the recipient instead of the sender. This is useful for
     mailing lists.
Bill Wohler's avatar
Bill Wohler committed
682

Bill Wohler's avatar
Bill Wohler committed
683 684 685
  b) An alias prefixed by `mh-default-folder-prefix' corresponding to the
     address is used. The prefix is used to prevent clutter in your mail
     directory.
Bill Wohler's avatar
Bill Wohler committed
686

Bill Wohler's avatar
Bill Wohler committed
687 688
Return nil if a folder name was not derived, or if the variable
`mh-default-folder-must-exist-flag' is t and the folder does not exist."
Bill Wohler's avatar
Bill Wohler committed
689
  ;; Loop for all entries in mh-default-folder-list
Bill Wohler's avatar
Bill Wohler committed
690 691
  (save-restriction
    (goto-char (point-min))
Bill Wohler's avatar
Bill Wohler committed
692
    (re-search-forward "\n\n" nil 'limit)
Bill Wohler's avatar
Bill Wohler committed
693 694 695 696 697 698 699 700 701 702
    (narrow-to-region (point-min) (point))
    (let ((to/cc (concat (or (message-fetch-field "to") "") ", "
                         (or (message-fetch-field "cc") "")))
          (from (or (message-fetch-field "from") ""))
          folder-name)
      (setq folder-name
            (loop for list in mh-default-folder-list
                  when (string-match (nth 0 list) (if (nth 2 list) to/cc from))
                  return (nth 1 list)
                  finally return nil))
Bill Wohler's avatar
Bill Wohler committed
703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724

      ;; Make sure a result from `mh-default-folder-list' begins with "+"
      ;; since 'mh-expand-file-name below depends on it
      (when (and folder-name (not (eq (aref folder-name 0) ?+)))
        (setq folder-name (concat "+" folder-name)))

      ;; If not, is there an alias for the address?
      (when (not folder-name)
        (let* ((from-header (mh-extract-from-header-value))
               (address (and from-header
                             (nth 1 (mail-extract-address-components
                                     from-header))))
               (alias (and address (mh-alias-address-to-alias address))))
          (when alias
            (setq folder-name
                  (and alias (concat "+" mh-default-folder-prefix alias))))))

      ;; If mh-default-folder-must-exist-flag set, check that folder exists.
      (if (and folder-name
               (or (not mh-default-folder-must-exist-flag)
                   (file-exists-p (mh-expand-file-name folder-name))))
          folder-name))))
Bill Wohler's avatar
Bill Wohler committed
725 726 727 728 729

(defun mh-prompt-for-refile-folder ()
  "Prompt the user for a folder in which the message should be filed.
The folder is returned as a string.

Bill Wohler's avatar
Bill Wohler committed
730 731 732
The default folder name is generated by the option
`mh-default-folder-for-message-function' if it is non-nil or
`mh-folder-from-address'."
Bill Wohler's avatar
Bill Wohler committed
733 734
  (mh-prompt-for-folder
   "Destination"
Bill Wohler's avatar
Bill Wohler committed
735 736 737 738 739 740 741 742 743 744 745 746 747
   (let ((refile-file (ignore-errors (mh-msg-filename (mh-get-msg-num t)))))
     (if (null refile-file) ""
       (save-excursion
         (set-buffer (get-buffer-create mh-temp-buffer))
         (erase-buffer)
         (insert-file-contents refile-file)
         (or (and mh-default-folder-for-message-function
                  (let ((buffer-file-name refile-file))
                    (funcall mh-default-folder-for-message-function)))
             (mh-folder-from-address)
             (and (eq 'refile (car mh-last-destination-folder))
                  (symbol-name (cdr mh-last-destination-folder)))
             ""))))
Bill Wohler's avatar
Bill Wohler committed
748 749
   t))

Bill Wohler's avatar
Bill Wohler committed
750
(defun mh-refile-msg (range folder &optional dont-update-last-destination-flag)
751 752 753 754 755 756 757 758
  "Refile (output) RANGE into FOLDER.

You are prompted for the folder name. Note that this command can also be used
to create folders. If you specify a folder that does not exist, you will be
prompted to create it.

The hook `mh-refile-msg-hook' is called after a message is marked to be
refiled.
Bill Wohler's avatar
Bill Wohler committed
759 760 761

Check the documentation of `mh-interactive-range' to see how RANGE is read in
interactive use.
Bill Wohler's avatar
Bill Wohler committed
762

763 764
If DONT-UPDATE-LAST-DESTINATION-FLAG is non-nil, then the variables
`mh-last-destination' and `mh-last-destination-folder' are not updated."
Bill Wohler's avatar
Bill Wohler committed
765
  (interactive (list (mh-interactive-range "Refile")
Bill Wohler's avatar
Bill Wohler committed
766 767 768 769
                     (intern (mh-prompt-for-refile-folder))))
  (unless dont-update-last-destination-flag
    (setq mh-last-destination (cons 'refile folder)
          mh-last-destination-folder mh-last-destination))
Bill Wohler's avatar
Bill Wohler committed
770
  (mh-iterate-on-range () range
Bill Wohler's avatar
Bill Wohler committed
771
    (mh-refile-a-msg nil folder))
Bill Wohler's avatar
Bill Wohler committed
772
  (when (looking-at mh-scan-refiled-msg-regexp) (mh-next-msg)))
Richard M. Stallman's avatar
Richard M. Stallman committed
773

Bill Wohler's avatar
Bill Wohler committed
774
(defun mh-refile-or-write-again (range &optional interactive-flag)
775 776 777
  "Repeat last output command.

If you are refiling several messages into the same folder, you can use this
778
command to repeat the last refile or write. You can use a range.
779 780 781 782 783 784

Check the documentation of `mh-interactive-range' to see how RANGE is read in
interactive use.

In a program, a non-nil INTERACTIVE-FLAG means that the function was called
interactively."
Bill Wohler's avatar
Bill Wohler committed
785
  (interactive (list (mh-interactive-range "Redo") t))
Richard M. Stallman's avatar
Richard M. Stallman committed
786 787
  (if (null mh-last-destination)
      (error "No previous refile or write"))
788 789 790 791 792 793 794 795
  (cond ((eq (car mh-last-destination) 'refile)
         (mh-refile-msg range (cdr mh-last-destination))
         (message "%s" (format "Destination folder: %s"
                               (cdr mh-last-destination))))
        (t
         (mh-iterate-on-range msg range
           (apply 'mh-write-msg-to-file msg (cdr mh-last-destination)))
         (mh-next-msg interactive-flag))))
Richard M. Stallman's avatar
Richard M. Stallman committed
796 797

(defun mh-quit ()
Bill Wohler's avatar
Bill Wohler committed
798 799 800 801 802
  "Quit the current MH-E folder.
Restore the previous window configuration, if one exists.
The value of `mh-before-quit-hook' is a list of functions to be called, with
no arguments, immediately upon entry to this function.
The value of `mh-quit-hook' is a list of functions to be called, with no
Bill Wohler's avatar
Bill Wohler committed
803 804 805
arguments, upon exit of this function.
MH-E working buffers (whose name begins with \" *mh-\" or \"*MH-E \") are
killed."
Richard M. Stallman's avatar
Richard M. Stallman committed
806
  (interactive)
Bill Wohler's avatar
Bill Wohler committed
807
  (run-hooks 'mh-before-quit-hook)
Bill Wohler's avatar
Bill Wohler committed
808 809 810
  (let ((show-buffer (get-buffer mh-show-buffer)))
    (when show-buffer
      (kill-buffer show-buffer)))
Karl Heuer's avatar
Karl Heuer committed
811
  (mh-update-sequences)
Bill Wohler's avatar
Bill Wohler committed
812
  (mh-destroy-postponed-handles)
813
  (bury-buffer (current-buffer))
Bill Wohler's avatar
Bill Wohler committed
814 815 816 817 818 819 820

  ;; Delete all MH-E temporary and working buffers.
  (dolist (buffer (buffer-list))
    (when (or (string-match "^ \\*mh-" (buffer-name buffer))
              (string-match "^\\*MH-E " (buffer-name buffer)))
      (kill-buffer buffer)))

Richard M. Stallman's avatar
Richard M. Stallman committed
821 822 823 824
  (if mh-previous-window-config
      (set-window-configuration mh-previous-window-config))
  (run-hooks 'mh-quit-hook))

825 826 827 828 829 830
(defun mh-page-msg (&optional lines)
  "Display next page in message.

You can give this command a prefix argument that specifies the number of LINES
to scroll. This command will also show the next undeleted message if it is
used at the bottom of a message."
Richard M. Stallman's avatar
Richard M. Stallman committed
831
  (interactive "P")
Bill Wohler's avatar
Bill Wohler committed
832
  (if mh-showing-mode
Bill Wohler's avatar
Bill Wohler committed
833
      (if mh-page-to-next-msg-flag
Bill Wohler's avatar
Bill Wohler committed
834 835 836 837 838 839
          (if (equal mh-next-direction 'backward)
              (mh-previous-undeleted-msg)
            (mh-next-undeleted-msg))
        (if (mh-in-show-buffer (mh-show-buffer)
              (pos-visible-in-window-p (point-max)))
            (progn
840
              (message
841 842 843 844 845
               "End of message (Type %s to read %s undeleted message)"
               (single-key-description last-input-event)
               (if (equal mh-next-direction 'backward)
                   "previous"
                 "next"))
Bill Wohler's avatar
Bill Wohler committed
846
              (setq mh-page-to-next-msg-flag t))
847
          (scroll-other-window lines)))
Bill Wohler's avatar
Bill Wohler committed
848
    (mh-show)))
Richard M. Stallman's avatar
Richard M. Stallman committed
849

850 851 852 853 854
(defun mh-previous-page (&optional lines)
  "Display next page in message.

You can give this command a prefix argument that specifies the number of LINES
to scroll."
Richard M. Stallman's avatar
Richard M. Stallman committed
855 856
  (interactive "P")
  (mh-in-show-buffer (mh-show-buffer)
857
    (scroll-down lines)))
Richard M. Stallman's avatar
Richard M. Stallman committed
858

859 860 861 862 863 864 865 866 867
(defun mh-previous-undeleted-msg (&optional count wait-after-complaining-flag)
  "Display previous message.

This command can be given a prefix argument COUNT to specify how many unread
messages to skip.

In a program, pause for a second after printing message if we are at the last
undeleted message and optional argument WAIT-AFTER-COMPLAINING-FLAG is
non-nil."
Richard M. Stallman's avatar
Richard M. Stallman committed
868 869 870
  (interactive "p")
  (setq mh-next-direction 'backward)
  (beginning-of-line)
871
  (cond ((re-search-backward mh-scan-good-msg-regexp nil t count)
Bill Wohler's avatar
Bill Wohler committed
872
         (mh-maybe-show))
Bill Wohler's avatar
Bill Wohler committed
873 874
        (t (message "No previous undeleted message")
           (if wait-after-complaining-flag (sit-for 1)))))
Bill Wohler's avatar
Bill Wohler committed
875 876

(defun mh-previous-unread-msg (&optional count)
877 878 879 880
  "Display previous unread message.

This command can be given a prefix argument COUNT to specify how many unread
messages to skip."
Bill Wohler's avatar
Bill Wohler committed
881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905
  (interactive "p")
  (unless (> count 0)
    (error "The function mh-previous-unread-msg expects positive argument"))
  (setq count (1- count))
  (let ((unread-sequence (cdr (assoc mh-unseen-seq mh-seq-list)))
        (cur-msg (mh-get-msg-num nil)))
    (cond ((and (not cur-msg) (not (bobp))
                ;; If we are at the end of the buffer back up one line and go
                ;; to unread message after that.
                (progn
                  (forward-line -1)
                  (setq cur-msg (mh-get-msg-num nil)))
                nil))
          ((or (null unread-sequence) (not cur-msg))
           ;; No unread message or there aren't any messages in buffer...
           (message "No more unread messages"))
          ((progn
             ;; Skip count messages...
             (while (and unread-sequence (>= (car unread-sequence) cur-msg))
               (setq unread-sequence (cdr unread-sequence)))
             (while (> count 0)
               (setq unread-sequence (cdr unread-sequence))
               (setq count (1- count)))
             (not (car unread-sequence)))
           (message "No more unread messages"))
Bill Wohler's avatar
Bill Wohler committed
906 907 908
          (t (loop for msg in unread-sequence
                   when (mh-goto-msg msg t) return nil
                   finally (message "No more unread messages"))))))
Bill Wohler's avatar
Bill Wohler committed
909 910 911 912 913 914 915 916 917 918 919 920 921 922

(defun mh-goto-next-button (backward-flag &optional criterion)
  "Search for next button satisfying criterion.
If BACKWARD-FLAG is non-nil search backward in the buffer for a mime button. If
CRITERION is a function or a symbol which has a function binding then that
function must return non-nil at the button we stop."
  (unless (or (and (symbolp criterion) (fboundp criterion))
              (functionp criterion))
    (setq criterion (lambda (x) t)))
  ;; Move to the next button in the buffer satisfying criterion
  (goto-char (or (save-excursion
                   (beginning-of-line)
                   ;; Find point before current button
                   (let ((point-before-current-button
Bill Wohler's avatar
Bill Wohler committed
923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951
                          (save-excursion
                            (while (get-text-property (point) 'mh-data)
                              (unless (= (forward-line
                                          (if backward-flag 1 -1))
                                         0)
                                (if backward-flag
                                    (goto-char (point-min))
                                  (goto-char (point-max)))))
                            (point))))
                     ;; Skip over current button
                     (while (and (get-text-property (point) 'mh-data)
                                 (not (if backward-flag (bobp) (eobp))))
                       (forward-line (if backward-flag -1 1)))
                     ;; Stop at next MIME button if any exists.
                     (block loop
                       (while (/= (progn
                                    (unless (= (forward-line
                                                (if backward-flag -1 1))
                                               0)
                                      (if backward-flag
                                          (goto-char (point-max))
                                        (goto-char (point-min)))
                                      (beginning-of-line))
                                    (point))
                                  point-before-current-button)
                         (when (and (get-text-property (point) 'mh-data)
                                    (funcall criterion (point)))
                           (return-from loop (point))))
                       nil)))
Bill Wohler's avatar
Bill Wohler committed
952 953 954
                 (point))))

(defun mh-next-button (&optional backward-flag)
955 956 957 958 959 960 961
  "Go to the next button.

If the end of the buffer is reached then the search wraps over to the start of
the buffer.

If an optional prefix argument BACKWARD-FLAG is given, the cursor will move to
the previous button."
Bill Wohler's avatar
Bill Wohler committed
962 963 964 965 966
  (interactive (list current-prefix-arg))
  (unless mh-showing-mode
    (mh-show))
  (mh-in-show-buffer (mh-show-buffer)
    (mh-goto-next-button backward-flag)))
Richard M. Stallman's avatar
Richard M. Stallman committed
967

Bill Wohler's avatar
Bill Wohler committed
968
(defun mh-prev-button ()
969 970 971 972
  "Go to the previous button.

If the beginning of the buffer is reached then the search wraps over to the
end of the buffer."
Bill Wohler's avatar
Bill Wohler committed
973 974 975 976 977 978 979 980 981 982 983 984 985
  (interactive)
  (mh-next-button t))

(defun mh-folder-mime-action (part-index action include-security-flag)
  "Go to PART-INDEX and carry out ACTION.
If PART-INDEX is nil then go to the next part in the buffer. The search for
the next buffer wraps around if end of buffer is reached. If argument
INCLUDE-SECURITY-FLAG is non-nil then include security info buttons when
searching for a suitable parts."
  (unless mh-showing-mode
    (mh-show))
  (mh-in-show-buffer (mh-show-buffer)
    (let ((criterion
Bill Wohler's avatar
Bill Wohler committed
986 987 988 989 990 991 992 993
           (cond (part-index
                  (lambda (p)
                    (let ((part (get-text-property p 'mh-part)))
                      (and (integerp part) (= part part-index)))))
                 (t (lambda (p)
                      (if include-security-flag
                          (get-text-property p 'mh-data)
                        (integerp (get-text-property p 'mh-part)))))))
Bill Wohler's avatar
Bill Wohler committed
994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009
          (point (point)))
      (cond ((and (get-text-property point 'mh-part)
                  (or (null part-index)
                      (= (get-text-property point 'mh-part) part-index)))
             (funcall action))
            ((and (get-text-property point 'mh-data)
                  include-security-flag
                  (null part-index))
             (funcall action))
            (t
             (mh-goto-next-button nil criterion)
             (if (= (point) point)
                 (message "No matching MIME part found")
               (funcall action)))))))

(defun mh-folder-toggle-mime-part (part-index)
1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
  "View attachment.

This command displays (or hides) the attachment associated with the button
under the cursor. If the cursor is not located over a button, then the cursor
first moves to the next button, wrapping to the beginning of the message if
necessary. This command has the advantage over related commands of working
from the MH-Folder buffer.

You can also provide a numeric prefix argument PART-INDEX to view the
attachment labeled with that number. If Emacs does not know how to display the
attachment, then Emacs offers to save the attachment in a file."
Bill Wohler's avatar
Bill Wohler committed
1021 1022 1023 1024 1025
  (interactive "P")
  (when (consp part-index) (setq part-index (car part-index)))
  (mh-folder-mime-action part-index #'mh-press-button t))

(defun mh-folder-inline-mime-part (part-index)
1026 1027 1028 1029 1030 1031 1032 1033 1034 1035
  "Show attachment verbatim.

You can view the raw contents of an attachment with this command. This command
displays (or hides) the contents of the attachment associated with the button
under the cursor verbatim. If the cursor is not located over a button, then
the cursor first moves to the next button, wrapping to the beginning of the
message if necessary.

You can also provide a numeric prefix argument PART-INDEX to view the
attachment labeled with that number."
Bill Wohler's avatar
Bill Wohler committed
1036 1037 1038 1039 1040
  (interactive "P")
  (when (consp part-index) (setq part-index (car part-index)))
  (mh-folder-mime-action part-index #'mh-mime-inline-part nil))

(defun mh-folder-save-mime-part (part-index)
1041 1042 1043