mh-e.el 99.9 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
;; Copyright (C) 1985, 86, 87, 88, 90, 92, 93, 94, 95, 97, 1999,
;;  2000, 01, 02, 2003 Free Software Foundation, Inc.
Richard M. Stallman's avatar
Richard M. Stallman committed
5

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

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

;; GNU Emacs is free software; you can redistribute it and/or modify
Richard M. Stallman's avatar
Richard M. Stallman committed
14 15 16 17
;; 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
18
;; GNU Emacs is distributed in the hope that it will be useful,
Richard M. Stallman's avatar
Richard M. Stallman committed
19 20 21 22 23
;; 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
24 25 26
;; along with GNU Emacs; see the file COPYING.  If not, write to the
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
Richard M. Stallman's avatar
Richard M. Stallman committed
27 28 29

;;; Commentary:

Bill Wohler's avatar
Bill Wohler committed
30 31 32 33
;; 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
34

Bill Wohler's avatar
Bill Wohler committed
35 36 37 38
;; 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
39

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

Bill Wohler's avatar
Bill Wohler committed
42 43
;; 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
44
;; Questions posting there for information on getting MH and MH-E:
Bill Wohler's avatar
Bill Wohler committed
45
;;   http://www.faqs.org/faqs/mail/mh-faq/part1/preamble.html
Erik Naggum's avatar
Erik Naggum committed
46

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

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

Bill Wohler's avatar
Bill Wohler committed
53
;; 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
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72

;; 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
73

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

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

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

Bill Wohler's avatar
Bill Wohler committed
85
(require 'cl)
Bill Wohler's avatar
Bill Wohler committed
86 87 88 89 90 91 92 93

(defvar recursive-load-depth-limit)
(eval-when (compile load eval)
  (if (and (boundp 'recursive-load-depth-limit)
           (integerp recursive-load-depth-limit)
           (> 50 recursive-load-depth-limit))
      (setq recursive-load-depth-limit 50)))

Bill Wohler's avatar
Bill Wohler committed
94
(require 'mh-inc)
Richard M. Stallman's avatar
Richard M. Stallman committed
95
(require 'mh-utils)
Bill Wohler's avatar
Bill Wohler committed
96 97
(require 'gnus-util)
(require 'easymenu)
Bill Wohler's avatar
Bill Wohler committed
98
(if mh-xemacs-flag
Bill Wohler's avatar
Bill Wohler committed
99 100
    (require 'mh-xemacs-compat))

Bill Wohler's avatar
Bill Wohler committed
101 102 103
;; Shush the byte-compiler
(defvar font-lock-auto-fontify)
(defvar font-lock-defaults)
Bill Wohler's avatar
Bill Wohler committed
104

Bill Wohler's avatar
Bill Wohler committed
105
(defconst mh-version "7.3+cvs" "Version number of MH-E.")
Bill Wohler's avatar
Bill Wohler committed
106 107

;;; Autoloads
Bill Wohler's avatar
Bill Wohler committed
108
(autoload 'Info-goto-node "info")
Richard M. Stallman's avatar
Richard M. Stallman committed
109 110


Bill Wohler's avatar
Bill Wohler committed
111

Karl Heuer's avatar
Karl Heuer committed
112 113 114 115 116 117 118 119
(defvar mh-note-deleted "D"
  "String whose first character is used to notate deleted messages.")

(defvar mh-note-refiled "^"
  "String whose first character is used to notate refiled messages.")

(defvar mh-note-cur "+"
  "String whose first character is used to notate the current message.")
Richard M. Stallman's avatar
Richard M. Stallman committed
120 121 122

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

Bill Wohler's avatar
Bill Wohler committed
125
;;; Parameterize MH-E to work with different scan formats.  The defaults work
Bill Wohler's avatar
Bill Wohler committed
126 127 128 129 130 131
;;; 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.

;; The following scan formats are passed to the scan program if the
;; setting of `mh-scan-format-file' above is nil.  They are identical
;; except the later one makes use of the nmh `decode' function to
Bill Wohler's avatar
Bill Wohler committed
132 133
;; decode RFC 2047 encodings. If you just want to change the width of
;; the msg number, use the `mh-set-cmd-note' function.
Bill Wohler's avatar
Bill Wohler committed
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150

(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}%>")
  "*Scan format string for MH, provided to the scan program via the -format arg.
This format is identical to the default except that additional hints for
Bill Wohler's avatar
Bill Wohler committed
151 152
fontification have been added to the fifth column (remember that in Emacs, the
first column is 0).
Bill Wohler's avatar
Bill Wohler committed
153

Bill Wohler's avatar
Bill Wohler committed
154
The values of the fifth column, in priority order, are: `-' if the
Bill Wohler's avatar
Bill Wohler committed
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
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.")

(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
174 175
  "*Scan format string for nmh.
This string is passed to the scan program via the -format arg.
Bill Wohler's avatar
Bill Wohler committed
176
This format is identical to the default except that additional hints for
Bill Wohler's avatar
Bill Wohler committed
177 178
fontification have been added to the fifth column (remember that in Emacs, the
first column is 0).
Bill Wohler's avatar
Bill Wohler committed
179

Bill Wohler's avatar
Bill Wohler committed
180
The values of the fifth column, in priority order, are: `-' if the
Bill Wohler's avatar
Bill Wohler committed
181 182 183 184 185
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
186
(defvar mh-scan-good-msg-regexp  "^\\( *[0-9]+\\)[^D^0-9]"
Bill Wohler's avatar
Bill Wohler committed
187 188 189 190
  "Regexp specifying the scan lines that are 'good' messages.
The default `mh-folder-font-lock-keywords' expects this expression to contain
at least one parenthesized expression which matches the message number.")

Bill Wohler's avatar
Bill Wohler committed
191
(defvar mh-scan-deleted-msg-regexp "^\\( *[0-9]+\\)D"
Bill Wohler's avatar
Bill Wohler committed
192 193 194 195
  "Regexp matching scan lines of deleted messages.
The default `mh-folder-font-lock-keywords' expects this expression to contain
at least one parenthesized expression which matches the message number.")

Bill Wohler's avatar
Bill Wohler committed
196
(defvar mh-scan-refiled-msg-regexp  "^\\( *[0-9]+\\)\\^"
Bill Wohler's avatar
Bill Wohler committed
197 198 199 200 201 202 203
  "Regexp matching scan lines of refiled messages.
The default `mh-folder-font-lock-keywords' expects this expression to contain
at least one parenthesized expression which matches the message number.")

(defvar mh-scan-valid-regexp "^ *[0-9]"
  "Regexp matching scan lines for messages (not error messages).")

Bill Wohler's avatar
Bill Wohler committed
204
(defvar mh-scan-cur-msg-number-regexp "^\\( *[0-9]+\\+\\).*"
Bill Wohler's avatar
Bill Wohler committed
205 206 207 208 209
  "Regexp matching scan line for the current message.
The default `mh-folder-font-lock-keywords' expects this expression to contain
at least one parenthesized expression which matches the message number.
Don't disable this regexp as it's needed by non fontifying functions.")

Bill Wohler's avatar
Bill Wohler committed
210
(defvar mh-scan-cur-msg-regexp "^\\( *[0-9]+\\+DISABLED.*\\)"
Bill Wohler's avatar
Bill Wohler committed
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
  "Regexp matching scan line for the current message.
The default `mh-folder-font-lock-keywords' expects this expression to contain
at least one parenthesized expression which matches the whole line.
To enable this feature, remove the string DISABLED from the regexp.")

(defvar mh-scan-date-regexp "\\([0-9][0-9]/[0-9][0-9]\\)"
  "Regexp matching a valid date in scan lines.
The default `mh-folder-font-lock-keywords' expects this expression to contain
only one parenthesized expression which matches the date field
\(see `mh-scan-format-regexp').")

(defvar mh-scan-rcpt-regexp  "\\(To:\\)\\(..............\\)"
  "Regexp specifying the recipient in scan lines for messages we sent.
The default `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.")

(defvar mh-scan-body-regexp "\\(<<\\([^\n]+\\)?\\)"
  "Regexp matching the message body beginning displayed in scan lines.
The default `mh-folder-font-lock-keywords' expects this expression to contain
at least one parenthesized expression which matches the body text.")

(defvar mh-scan-subject-regexp
Bill Wohler's avatar
Bill Wohler committed
235
  ;;"^ *[0-9]+........[ ]*...................\\([Rr][Ee]:\\s-*\\)*\\([^<\n]*\\)"
Bill Wohler's avatar
Bill Wohler committed
236
  "^ *[0-9]+........[ ]*...................\\([Rr][Ee]\\(\\[[0-9]+\\]\\)?:\\s-*\\)*\\([^<\n]*\\)"
Bill Wohler's avatar
Bill Wohler committed
237 238
  "*Regexp matching the subject string in MH folder mode.
The default `mh-folder-font-lock-keywords' expects this expression to contain
Bill Wohler's avatar
Bill Wohler committed
239 240 241
at least tree 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 the third is expected to match the subject line itself.")
Bill Wohler's avatar
Bill Wohler committed
242 243

(defvar mh-scan-format-regexp
Bill Wohler's avatar
Bill Wohler committed
244 245 246 247
  (concat "\\([bct]\\)" mh-scan-date-regexp " *\\(..................\\)")
  "Regexp matching the output of scan.
The default value is based upon the default values of either
`mh-scan-format-mh' or `mh-scan-format-nmh'.
Bill Wohler's avatar
Bill Wohler committed
248 249 250 251 252
The default `mh-folder-font-lock-keywords' expects this expression to contain
at least three parenthesized expressions. The first should match the
fontification hint, the second is found in `mh-scan-date-regexp', and the
third should match the user name.")

Bill Wohler's avatar
Bill Wohler committed
253 254


Bill Wohler's avatar
Bill Wohler committed
255 256
(defvar mh-folder-font-lock-keywords
  (list
Bill Wohler's avatar
Bill Wohler committed
257 258 259
   ;; Folders when displaying index buffer
   (list "^\\+.*"
         '(0 mh-index-folder-face))
Bill Wohler's avatar
Bill Wohler committed
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
   ;; 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
275 276
         '(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
277
   (list mh-scan-rcpt-regexp
Bill Wohler's avatar
Bill Wohler committed
278 279
         '(1 mh-folder-to-face) ;; To:
         '(2 mh-folder-address-face)) ;; address
Bill Wohler's avatar
Bill Wohler committed
280 281 282 283 284 285 286 287
   ;; scan font-lock name
   (list mh-scan-format-regexp
         '(1 mh-folder-date-face)
         '(3 mh-folder-scan-format-face))
   ;; Current message line
   (list mh-scan-cur-msg-regexp
         '(1 mh-folder-cur-msg-face prepend t))
   ;; Unseen messages in bold
Bill Wohler's avatar
Bill Wohler committed
288
   '(mh-folder-font-lock-unseen (1 'bold append t)))
Bill Wohler's avatar
Bill Wohler committed
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
  "Regexp keywords used to fontify the MH-Folder buffer.")

(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.")

(defvar mh-scan-field-from-start-offset
Bill Wohler's avatar
Bill Wohler committed
328 329 330 331 332
  (+ 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
333 334

(defvar mh-scan-field-from-end-offset
Bill Wohler's avatar
Bill Wohler committed
335 336
  (+ 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
337 338 339 340 341 342 343 344 345

(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
346 347

(defun mh-folder-font-lock-subject (limit)
Bill Wohler's avatar
Bill Wohler committed
348
  "Return MH-E scan subject strings to font-lock between point and LIMIT."
Bill Wohler's avatar
Bill Wohler committed
349 350 351
  (if (not (re-search-forward mh-scan-subject-regexp limit t))
      nil
    (if (match-beginning 1)
Bill Wohler's avatar
Bill Wohler committed
352 353 354 355
        (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
356 357
    t))

Bill Wohler's avatar
Bill Wohler committed
358 359 360 361


;; Fontifify unseen mesages in bold.

Bill Wohler's avatar
Bill Wohler committed
362 363 364 365 366 367 368 369 370
(defvar mh-folder-unseen-seq-name nil
  "Name of unseen sequence.
The default for this is provided by the function `mh-folder-unseen-seq-name'
On nmh systems.")

(defun mh-folder-unseen-seq-name ()
  "Provide name of unseen sequence from mhparam."
  (or mh-progs (mh-find-path))
  (save-excursion
Bill Wohler's avatar
Bill Wohler committed
371 372
    (let ((unseen-seq-name "unseen"))
      (with-temp-buffer
Bill Wohler's avatar
Bill Wohler committed
373 374 375 376 377 378 379
        (unwind-protect
            (progn
              (call-process (expand-file-name "mhparam" mh-progs)
                            nil '(t t) nil "-component" "Unseen-Sequence")
              (goto-char (point-min))
              (if (re-search-forward "Unseen-Sequence: \\(.*\\)$" nil t)
                  (setq unseen-seq-name (match-string 1))))))
Bill Wohler's avatar
Bill Wohler committed
380 381 382 383 384 385 386 387 388 389 390 391
      unseen-seq-name)))

(defun mh-folder-unseen-seq-list ()
  "Return a list of unseen message numbers for current folder."
  (if (not mh-folder-unseen-seq-name)
      (setq mh-folder-unseen-seq-name (mh-folder-unseen-seq-name)))
  (cond
   ((not mh-folder-unseen-seq-name)
    nil)
   (t
    (let ((folder mh-current-folder))
      (save-excursion
Bill Wohler's avatar
Bill Wohler committed
392 393 394 395
        (with-temp-buffer
          (unwind-protect
              (progn
                (call-process (expand-file-name "mark" mh-progs)
Bill Wohler's avatar
Bill Wohler committed
396
                              nil '(t t) nil
Bill Wohler's avatar
Bill Wohler committed
397
                              folder "-seq" mh-folder-unseen-seq-name
Bill Wohler's avatar
Bill Wohler committed
398 399 400
                              "-list")
                (goto-char (point-min))
                (sort (mh-read-msg-list) '<)))))))))
Bill Wohler's avatar
Bill Wohler committed
401 402

(defvar mh-folder-unseen-seq-cache nil
Bill Wohler's avatar
Bill Wohler committed
403
  "Internal cache variable used for font-lock in MH-E.
Bill Wohler's avatar
Bill Wohler committed
404 405 406 407 408 409 410 411 412 413 414 415
Should only be non-nil through font-lock stepping, and nil once font-lock
is done highlighting.")
(make-variable-buffer-local 'mh-folder-unseen-seq-cache)

(defun mh-folder-font-lock-unseen (limit)
  "Return unseen message lines to font-lock between point and LIMIT."
  (if (not mh-folder-unseen-seq-cache)
      (setq mh-folder-unseen-seq-cache (mh-folder-unseen-seq-list)))
  (let ((cur-msg (mh-get-msg-num nil)))
    (cond
     ((not mh-folder-unseen-seq-cache)
      nil)
Bill Wohler's avatar
Bill Wohler committed
416
     ((>= (point) limit)                 ;Presumably at end of buffer
Bill Wohler's avatar
Bill Wohler committed
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
      (setq mh-folder-unseen-seq-cache nil)
      nil)
     ((member cur-msg mh-folder-unseen-seq-cache)
      (let ((bpoint (progn (beginning-of-line)(point)))
            (epoint (progn (forward-line 1)(point))))
        (if (<= limit (point))
            (setq  mh-folder-unseen-seq-cache nil))
        (set-match-data (list bpoint epoint bpoint epoint))
        t))
     (t
      ;; move forward one line at a time, checking each message number.
      (while (and
              (= 0 (forward-line 1))
              (> limit (point))
              (not (member (mh-get-msg-num nil) mh-folder-unseen-seq-cache))))
      ;; Examine how we must have exited the loop...
      (let ((cur-msg (mh-get-msg-num nil)))
        (cond
Bill Wohler's avatar
Bill Wohler committed
435
         ((or (<= limit (point))
Bill Wohler's avatar
Bill Wohler committed
436 437 438 439 440 441 442 443 444 445
              (not (member cur-msg mh-folder-unseen-seq-cache)))
          (setq mh-folder-unseen-seq-cache nil)
          nil)
         ((member cur-msg mh-folder-unseen-seq-cache)
          (let ((bpoint (progn (beginning-of-line)(point)))
                (epoint (progn (forward-line 1)(point))))
            (if (<= limit (point))
                (setq  mh-folder-unseen-seq-cache nil))
            (set-match-data (list bpoint epoint bpoint epoint))
            t))))))))
Bill Wohler's avatar
Bill Wohler committed
446 447


Richard M. Stallman's avatar
Richard M. Stallman committed
448 449 450

;;; Internal variables:

Bill Wohler's avatar
Bill Wohler committed
451 452
(defvar mh-last-destination nil)        ;Destination of last refile or write
                                        ;command.
Bill Wohler's avatar
Bill Wohler committed
453 454
(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
455 456 457 458

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

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

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

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

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-narrowed-to-seq nil)         ;Sequence display is narrowed to or
                                        ;nil if not narrowed.
Bill Wohler's avatar
Bill Wohler committed
469

Bill Wohler's avatar
Bill Wohler committed
470 471 472 473
(defvar mh-tick-seq-changed-when-narrowed-flag nil)
                                        ;Has tick sequence changed while the
                                        ;folder was narrowed to it?

Bill Wohler's avatar
Bill Wohler committed
474 475
(defvar mh-view-ops ())                 ;Stack of ops that change the folder
                                        ;view (such as narrowing or threading).
Richard M. Stallman's avatar
Richard M. Stallman committed
476

Bill Wohler's avatar
Bill Wohler committed
477 478 479 480 481 482
(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)

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

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

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

;;; Macros and generic functions:

Bill Wohler's avatar
Bill Wohler committed
490 491
(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
492
  (while list
Bill Wohler's avatar
Bill Wohler committed
493
    (funcall function (car list))
Richard M. Stallman's avatar
Richard M. Stallman committed
494 495
    (setq list (cdr list))))

Bill Wohler's avatar
Bill Wohler committed
496
(defun mh-scan-format ()
Bill Wohler's avatar
Bill Wohler committed
497
  "Return \"-format\" argument for the scan program."
Bill Wohler's avatar
Bill Wohler committed
498
  (if (equal mh-scan-format-file t)
Bill Wohler's avatar
Bill Wohler committed
499
      (list "-format" (if mh-nmh-flag
Bill Wohler's avatar
Bill Wohler committed
500 501 502 503
                          (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
504
    (if (not (equal mh-scan-format-file nil))
Bill Wohler's avatar
Bill Wohler committed
505
        (list "-format" mh-scan-format-file))))
Bill Wohler's avatar
Bill Wohler committed
506

Richard M. Stallman's avatar
Richard M. Stallman committed
507 508 509 510 511 512


;;; Entry points:

;;;###autoload
(defun mh-rmail (&optional arg)
Bill Wohler's avatar
Bill Wohler committed
513
  "Inc(orporate) new mail with MH.
Bill Wohler's avatar
Bill Wohler committed
514
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
515
the Emacs front end to the MH mail system."
Richard M. Stallman's avatar
Richard M. Stallman committed
516 517 518 519
  (interactive "P")
  (mh-find-path)
  (if arg
      (call-interactively 'mh-visit-folder)
Bill Wohler's avatar
Bill Wohler committed
520 521
    (unless (get-buffer mh-inbox)
      (mh-visit-folder mh-inbox (symbol-name mh-unseen-seq)))
Bill Wohler's avatar
Bill Wohler committed
522
    (mh-inc-folder)))
Richard M. Stallman's avatar
Richard M. Stallman committed
523

Bill Wohler's avatar
Bill Wohler committed
524 525 526
;;;###autoload
(defun mh-nmail (&optional arg)
  "Check for new mail in inbox folder.
Bill Wohler's avatar
Bill Wohler committed
527
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
528 529
the Emacs front end to the MH mail system."
  (interactive "P")
Bill Wohler's avatar
Bill Wohler committed
530
  (mh-find-path)                        ; init mh-inbox
Bill Wohler's avatar
Bill Wohler committed
531 532 533
  (if arg
      (call-interactively 'mh-visit-folder)
    (mh-visit-folder mh-inbox)))
Richard M. Stallman's avatar
Richard M. Stallman committed
534 535 536



Bill Wohler's avatar
Bill Wohler committed
537
;;; User executable MH-E commands:
Richard M. Stallman's avatar
Richard M. Stallman committed
538 539

(defun mh-delete-msg (msg-or-seq)
Bill Wohler's avatar
Bill Wohler committed
540
  "Mark the specified MSG-OR-SEQ for subsequent deletion and move to the next.
Bill Wohler's avatar
Bill Wohler committed
541 542 543 544 545 546 547
Default is the displayed message.
If optional prefix argument is provided, then prompt for the message sequence.
If variable `transient-mark-mode' is non-nil and the mark is active, then the
selected region is marked for deletion.
In a program, MSG-OR-SEQ can be a message number, a list of message numbers, a
region in a cons cell, or a sequence."
  (interactive (list (mh-interactive-msg-or-seq "Delete")))
Richard M. Stallman's avatar
Richard M. Stallman committed
548 549 550 551
  (mh-delete-msg-no-motion msg-or-seq)
  (mh-next-msg))

(defun mh-delete-msg-no-motion (msg-or-seq)
Bill Wohler's avatar
Bill Wohler committed
552
  "Mark the specified MSG-OR-SEQ for subsequent deletion.
Bill Wohler's avatar
Bill Wohler committed
553 554 555 556 557 558 559 560 561
Default is the displayed message.
If optional prefix argument is provided, then prompt for the message sequence.
If variable `transient-mark-mode' is non-nil and the mark is active, then the
selected region is marked for deletion.
In a program, MSG-OR-SEQ can be a message number, a list of message numbers, a
region in a cons cell, or a sequence."
  (interactive (list (mh-interactive-msg-or-seq "Delete")))
  (mh-iterate-on-msg-or-seq () msg-or-seq
    (mh-delete-a-msg nil)))
Richard M. Stallman's avatar
Richard M. Stallman committed
562 563 564 565 566 567 568

(defun mh-execute-commands ()
  "Process outstanding delete and refile requests."
  (interactive)
  (if mh-narrowed-to-seq (mh-widen))
  (mh-process-commands mh-current-folder)
  (mh-set-scan-mode)
Bill Wohler's avatar
Bill Wohler committed
569
  (mh-goto-cur-msg)                    ; after mh-set-scan-mode for efficiency
Richard M. Stallman's avatar
Richard M. Stallman committed
570
  (mh-make-folder-mode-line)
Bill Wohler's avatar
Bill Wohler committed
571
  t)                                    ; return t for write-file-functions
Richard M. Stallman's avatar
Richard M. Stallman committed
572 573 574 575

(defun mh-first-msg ()
  "Move to the first message."
  (interactive)
Karl Heuer's avatar
Karl Heuer committed
576
  (goto-char (point-min))
Bill Wohler's avatar
Bill Wohler committed
577
  (while (and (not (eobp)) (not (looking-at mh-scan-valid-regexp)))
Karl Heuer's avatar
Karl Heuer committed
578
    (forward-line 1)))
Richard M. Stallman's avatar
Richard M. Stallman committed
579 580 581

(defun mh-header-display ()
  "Show the current message with all its headers.
582
Displays headers that might have been suppressed by setting the
Bill Wohler's avatar
Bill Wohler committed
583
variables `mh-clean-message-header-flag' or `mhl-formfile', or by the fallback
584 585
behavior of scrolling uninteresting headers off the top of the window.
Type \"\\[mh-show]\" to show the message normally again."
Richard M. Stallman's avatar
Richard M. Stallman committed
586 587
  (interactive)
  (and (not mh-showing-with-headers)
Bill Wohler's avatar
Bill Wohler committed
588
       (or mhl-formfile mh-clean-message-header-flag)
Richard M. Stallman's avatar
Richard M. Stallman committed
589
       (mh-invalidate-show-buffer))
Bill Wohler's avatar
Bill Wohler committed
590 591
  (let ((mh-decode-mime-flag nil)
        (mhl-formfile nil)
Bill Wohler's avatar
Bill Wohler committed
592
        (mh-clean-message-header-flag nil))
Richard M. Stallman's avatar
Richard M. Stallman committed
593 594 595 596 597 598
    (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
599
(defun mh-inc-folder (&optional maildrop-name folder)
Karl Heuer's avatar
Karl Heuer committed
600
  "Inc(orporate)s new mail into the Inbox folder.
Bill Wohler's avatar
Bill Wohler committed
601
Optional argument MAILDROP-NAME specifies an alternate maildrop from the
Bill Wohler's avatar
Bill Wohler committed
602 603
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
604 605 606
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
607
  (interactive (list (if current-prefix-arg
Bill Wohler's avatar
Bill Wohler committed
608 609
                         (expand-file-name
                          (read-file-name "inc mail from file: "
Bill Wohler's avatar
Bill Wohler committed
610 611 612 613 614
                                          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
615 616
  (let ((threading-needed-flag nil))
    (let ((config (current-window-configuration)))
Bill Wohler's avatar
Bill Wohler committed
617 618 619 620 621 622 623
      (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
624 625 626 627 628 629 630 631 632
    (mh-get-new-mail maildrop-name)
    (when (and threading-needed-flag
               (save-excursion
                 (goto-char (point-min))
                 (or (null mh-large-folder)
                     (not (equal (forward-line mh-large-folder) 0))
                     (and (message "Not threading since the number of messages exceeds `mh-large-folder'")
                          nil))))
      (mh-toggle-threads))
Bill Wohler's avatar
Bill Wohler committed
633 634
    (beginning-of-line)
    (if (and mh-showing-mode (looking-at mh-scan-valid-regexp)) (mh-show))
Bill Wohler's avatar
Bill Wohler committed
635
    (run-hooks 'mh-inc-folder-hook)))
Richard M. Stallman's avatar
Richard M. Stallman committed
636 637 638 639 640

(defun mh-last-msg ()
  "Move to the last message."
  (interactive)
  (goto-char (point-max))
Bill Wohler's avatar
Bill Wohler committed
641
  (while (and (not (bobp)) (not (looking-at mh-scan-valid-regexp)))
Bill Wohler's avatar
Bill Wohler committed
642 643
    (forward-line -1))
  (mh-recenter nil))
Richard M. Stallman's avatar
Richard M. Stallman committed
644

Bill Wohler's avatar
Bill Wohler committed
645 646 647 648
(defun mh-next-undeleted-msg (&optional arg wait-after-complaining-flag)
  "Move to the next undeleted message ARG in window.
If optional argument WAIT-AFTER-COMPLAINING-FLAG is non-nil and we are at the
last undeleted message then pause for a second after printing message."
Karl Heuer's avatar
Karl Heuer committed
649
  (interactive "p")
Richard M. Stallman's avatar
Richard M. Stallman committed
650
  (setq mh-next-direction 'forward)
Karl Heuer's avatar
Karl Heuer committed
651
  (forward-line 1)
Bill Wohler's avatar
Bill Wohler committed
652
  (cond ((re-search-forward mh-scan-good-msg-regexp nil t arg)
Bill Wohler's avatar
Bill Wohler committed
653 654 655
         (beginning-of-line)
         (mh-maybe-show))
        (t (forward-line -1)
Bill Wohler's avatar
Bill Wohler committed
656 657
           (message "No more undeleted messages")
           (if wait-after-complaining-flag (sit-for 1)))))
Richard M. Stallman's avatar
Richard M. Stallman committed
658

Bill Wohler's avatar
Bill Wohler committed
659 660 661 662 663
(defun mh-folder-from-address ()
  "Determine folder name from address in From field.
Takes the address in the From: header field, and returns one of:

  a) The folder name associated with the address in the alist
Bill Wohler's avatar
Bill Wohler committed
664 665 666 667 668
     `mh-default-folder-list'.  If the `Check Recipient' boolean
      is set, then the `mh-default-folder-list' addresses are
      checked against the recipient instead of the originator
      (making possible to use this feature for mailing lists).
      The first match found in `mh-default-folder-list' is used.
Bill Wohler's avatar
Bill Wohler committed
669 670 671 672 673 674

  b) The address' corresponding alias from the user's personal
     aliases file prefixed by `mh-default-folder-prefix'.

Returns nil if the address was not found in either place or if the variable
`mh-default-folder-must-exist-flag' is nil and the folder does not exist."
Bill Wohler's avatar
Bill Wohler committed
675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722
  ;; Loop for all entries in mh-default-folder-list
  (save-excursion
    (let ((folder-name
           (car
            (delq nil
                  (mapcar
                   (lambda (list)
                     (let ((address-regexp (nth 0 list))
                           (folder (nth 1 list))
                           (to-flag (nth 2 list)))
                       (when (or
                              (mh-goto-header-field (if to-flag "To:" "From:"))
                              ; if the To: field is missing, try Cc:
                              (and to-flag (mh-goto-header-field "cc:")))
                         (let ((endfield (save-excursion
                                           (mh-header-field-end)(point))))
                           (if (re-search-forward address-regexp endfield t)
                               folder
                             (when to-flag ;Try Cc: as well
                               (mh-goto-header-field "cc:")
                               (let ((endfield (save-excursion
                                                 (mh-header-field-end)(point))))
                                 (when (re-search-forward
                                        address-regexp endfield t)
                                   folder))))))))
                   mh-default-folder-list)))))

      ;; 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
723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748

(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.

If `mh-default-folder-for-message-function' is a function then the message
being refiled is yanked into a temporary buffer and the function is called to
intelligently guess where the message is to be refiled.

Otherwise, a default folder name is generated by `mh-folder-from-address'."
  (mh-prompt-for-folder
   "Destination"
   (let ((refile-file (mh-msg-filename (mh-get-msg-num t))))
     (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)))
           "")))
   t))

Bill Wohler's avatar
Bill Wohler committed
749 750 751 752 753
(defun mh-refile-msg (msg-or-seq folder
                      &optional dont-update-last-destination-flag)
  "Refile MSG-OR-SEQ into FOLDER.
Default is the displayed message.
If optional prefix argument is provided, then prompt for the message sequence.
Bill Wohler's avatar
Bill Wohler committed
754
If variable `transient-mark-mode' is non-nil and the mark is active, then the
Bill Wohler's avatar
Bill Wohler committed
755 756 757 758 759 760 761 762 763 764 765 766 767 768
selected region is marked for refiling.
In a program, MSG-OR-SEQ can be a message number, a list of message numbers, a
region in a cons cell, or a sequence.

If optional argument DONT-UPDATE-LAST-DESTINATION-FLAG is non-nil then the
variables `mh-last-destination' and `mh-last-destination-folder' are not
updated."
  (interactive (list (mh-interactive-msg-or-seq "Refile")
                     (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))
  (mh-iterate-on-msg-or-seq () msg-or-seq
    (mh-refile-a-msg nil folder))
Richard M. Stallman's avatar
Richard M. Stallman committed
769 770
  (mh-next-msg))

Karl Heuer's avatar
Karl Heuer committed
771
(defun mh-refile-or-write-again (message)
Richard M. Stallman's avatar
Richard M. Stallman committed
772
  "Re-execute the last refile or write command on the given MESSAGE.
Bill Wohler's avatar
Bill Wohler committed
773 774
Default is the displayed message. Use the same folder or file as the previous
refile or write command."
Richard M. Stallman's avatar
Richard M. Stallman committed
775 776 777
  (interactive (list (mh-get-msg-num t)))
  (if (null mh-last-destination)
      (error "No previous refile or write"))
Bill Wohler's avatar
Bill Wohler committed
778 779 780 781 782 783 784 785 786 787
  (let (output)
    (setq output
          (cond ((eq (car mh-last-destination) 'refile)
                 (mh-refile-a-msg message (cdr mh-last-destination))
                 (format "Destination folder: %s" (cdr mh-last-destination)))
                (t
                 (apply 'mh-write-msg-to-file message (cdr mh-last-destination))
                 (format "Destination: %s" (cdr mh-last-destination)))))
    (mh-next-msg (interactive-p))
    (message output)))
Richard M. Stallman's avatar
Richard M. Stallman committed
788 789

(defun mh-quit ()
Bill Wohler's avatar
Bill Wohler committed
790 791 792 793 794
  "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
795 796 797
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
798
  (interactive)
Bill Wohler's avatar
Bill Wohler committed
799
  (run-hooks 'mh-before-quit-hook)
Bill Wohler's avatar
Bill Wohler committed
800 801 802
  (let ((show-buffer (get-buffer mh-show-buffer)))
    (when show-buffer
      (kill-buffer show-buffer)))
Karl Heuer's avatar
Karl Heuer committed
803
  (mh-update-sequences)
Bill Wohler's avatar
Bill Wohler committed
804
  (mh-destroy-postponed-handles)
805
  (bury-buffer (current-buffer))
Bill Wohler's avatar
Bill Wohler committed
806 807 808 809 810 811 812

  ;; 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
813 814 815 816 817 818
  (if mh-previous-window-config
      (set-window-configuration mh-previous-window-config))
  (run-hooks 'mh-quit-hook))

(defun mh-page-msg (&optional arg)
  "Page the displayed message forwards.
Bill Wohler's avatar
Bill Wohler committed
819 820 821
Scrolls ARG lines or a full screen if no argument is supplied. Show buffer
first if not displayed. Show the next undeleted message if looking at the
bottom of the current message."
Richard M. Stallman's avatar
Richard M. Stallman committed
822
  (interactive "P")
Bill Wohler's avatar
Bill Wohler committed
823
  (if mh-showing-mode
Bill Wohler's avatar
Bill Wohler committed
824
      (if mh-page-to-next-msg-flag
Bill Wohler's avatar
Bill Wohler committed
825 826 827 828 829 830 831 832 833 834 835 836 837 838
          (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
              (message (format
                        "End of message (Type %s to read %s undeleted message)"
                        (single-key-description last-input-event)
                        (if (equal mh-next-direction 'backward)
                            "previous"
                          "next")))
              (setq mh-page-to-next-msg-flag t))
          (scroll-other-window arg)))
Bill Wohler's avatar
Bill Wohler committed
839
    (mh-show)))
Richard M. Stallman's avatar
Richard M. Stallman committed
840 841 842 843 844 845 846 847

(defun mh-previous-page (&optional arg)
  "Page the displayed message backwards.
Scrolls ARG lines or a full screen if no argument is supplied."
  (interactive "P")
  (mh-in-show-buffer (mh-show-buffer)
    (scroll-down arg)))

Bill Wohler's avatar
Bill Wohler committed
848 849 850 851
(defun mh-previous-undeleted-msg (&optional arg wait-after-complaining-flag)
  "Move to the previous undeleted message ARG in window.
If optional argument WAIT-AFTER-COMPLAINING-FLAG is non-nil and we are at the
first undeleted message then pause for a second after printing message."
Richard M. Stallman's avatar
Richard M. Stallman committed
852 853 854
  (interactive "p")
  (setq mh-next-direction 'backward)
  (beginning-of-line)
Bill Wohler's avatar
Bill Wohler committed
855
  (cond ((re-search-backward mh-scan-good-msg-regexp nil t arg)
Bill Wohler's avatar
Bill Wohler committed
856
         (mh-maybe-show))
Bill Wohler's avatar
Bill Wohler committed
857 858
        (t (message "No previous undeleted message")
           (if wait-after-complaining-flag (sit-for 1)))))
Bill Wohler's avatar
Bill Wohler committed
859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889

(defun mh-previous-unread-msg (&optional count)
  "Move to previous unread message.
With optional argument COUNT, COUNT-1 unread messages before current message
are skipped."
  (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"))
          (t (mh-goto-msg (car unread-sequence))))))
Bill Wohler's avatar
Bill Wohler committed
890 891 892 893 894 895 896 897 898 899 900 901 902 903

(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
904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932
                          (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
933 934 935 936 937 938 939 940 941 942 943 944 945
                 (point))))

(defun mh-next-button (&optional backward-flag)
  "Go to the next MIME button.
Advance point to the next MIME button in the show buffer. If the end
of buffer is reached then the search wraps over to the start of the
buffer. With prefix argument, BACKWARD-FLAG the point will move to the
previous MIME button."
  (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
946

Bill Wohler's avatar
Bill Wohler committed
947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964
(defun mh-prev-button ()
  "Go to the prev MIME button.
Move point to the previous MIME button in the show buffer. If the beginning
of the buffer is reached then the search wraps over to the end of the
buffer."
  (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
965 966 967 968 969 970 971 972
           (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
973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023
          (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)
  "Toggle display of button.
If point in show buffer is at a button then that part is toggled.
If not at a button and PART-INDEX is non-nil point is moved to that part.
With nil PART-INDEX find the first button after point (search wraps around if
end of buffer is reached) and toggle it."
  (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)
  "Show the raw bytes of MIME part inline.
If point in show buffer is at a mime part then that part is inlined.
If not at a mime-part and PART-INDEX is non-nil point is moved to that part.
With nil PART-INDEX find the first button after point (search wraps around if
end of buffer is reached) and inline it."
  (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)
  "Save MIME part.
If point in show buffer is at a mime part then that part is saved.
If not at a mime-part and PART-INDEX is non-nil point is moved to that part.
With nil PART-INDEX find the first button after point (search wraps around if
end of buffer is reached) and save it."
  (interactive "P")
  (when (consp part-index) (setq part-index (car part-index)))
  (mh-folder-mime-action part-index #'mh-mime-save-part nil))

(defun mh-reset-threads-and-narrowing ()
  "Reset all variables pertaining to threads and narrowing.
Also removes all content from the folder buffer."
  (setq mh-view-ops ())
  (setq mh-narrowed-to-seq nil)
  (let ((buffer-read-only nil)) (erase-buffer)))
Richard M. Stallman's avatar
Richard M. Stallman committed
1024

Bill Wohler's avatar
Bill Wohler committed
1025
(defun mh-rescan-folder (&optional range dont-exec-pending)
Richard M. Stallman's avatar
Richard M. Stallman committed
1026
  "Rescan a folder after optionally processing the outstanding commands.
Bill Wohler's avatar
Bill Wohler committed
1027
If optional prefix argument RANGE is provided, prompt for the range of
Bill Wohler's avatar
Bill Wohler committed
1028 1029 1030
messages to display. Otherwise show the entire folder.
If optional argument DONT-EXEC-PENDING is non-nil then pending deletes and
refiles aren't carried out."
Richard M. Stallman's avatar
Richard M. Stallman committed
1031
  (interactive (list (if current-prefix-arg
Bill Wohler's avatar
Bill Wohler committed
1032 1033
                         (mh-read-msg-range mh-current-folder t)
                       nil)))
Richard M. Stallman's avatar
Richard M. Stallman committed
1034
  (setq mh-next-direction 'forward)
Bill Wohler's avatar
Bill Wohler committed
1035 1036 1037 1038
  (let ((threaded-flag (memq 'unthread mh-view-ops)))
    (mh-scan-folder mh-current-folder (or range "all") dont-exec-pending)
    (cond (threaded-flag (mh-toggle-threads))
          (mh-index-data (mh-index-insert-folder-headers)))))
Richard M. Stallman's avatar
Richard M. Stallman committed
1039 1040

(defun mh-write-msg-to-file (msg file no-headers)
Bill Wohler's avatar
Bill Wohler committed
1041 1042
  "Append MSG to the end of a FILE.
If prefix argument NO-HEADERS is provided, write only the message body.
Richard M. Stallman's avatar
Richard M. Stallman committed
1043 1044 1045
Otherwise send the entire message including the headers."
  (interactive
   (list (mh-get-msg-num t)
Bill Wohler's avatar
Bill Wohler committed
1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056
         (let ((default-dir (if (eq 'write (car mh-last-destination-write))
                                (file-name-directory
                                 (car (cdr mh-last-destination-write)))
                              default-directory)))
           (read-file-name (format "Save message%s in file: "
                                   (if current-prefix-arg " body" ""))
                           default-dir
                           (if (eq 'write (car mh-last-destination-write))
                               (car (cdr mh-last-destination-write))
                             (expand-file-name "mail.out" default-dir))))
         current-prefix-arg))
Karl Heuer's avatar
Karl Heuer committed
1057
  (let ((msg-file-to-output (mh-msg-filename msg))
Bill Wohler's avatar
Bill Wohler committed
1058
        (output-file (mh-expand-file-name file)))
Bill Wohler's avatar
Bill Wohler committed
1059
    (setq mh-last-destination (list 'write file (if no-headers 'no-headers))
Bill Wohler's avatar
Bill Wohler committed
1060
          mh-last-destination-write mh-last-destination)
Richard M. Stallman's avatar
Richard M. Stallman committed
1061
    (save-excursion
Karl Heuer's avatar
Karl Heuer committed
1062
      (set-buffer (get-buffer-create mh-temp-buffer))
Richard M. Stallman's avatar
Richard M. Stallman committed
1063
      (erase-buffer)
Karl Heuer's avatar
Karl Heuer committed
1064
      (insert-file-contents msg-file-to-output)
Richard M. Stallman's avatar
Richard M. Stallman committed
1065 1066 1067 1068 1069 1070 1071
      (goto-char (point-min))
      (if no-headers (search-forward "\n\n"))
      (append-to-file (point) (point-max) output-file))))

(defun mh-toggle-showing ()
  "Toggle the scanning mode/showing mode of displaying messages."
  (interactive)
Bill Wohler's avatar
Bill Wohler committed
1072
  (if mh-showing-mode
Richard M. Stallman's avatar
Richard M. Stallman committed
1073
      (mh-set-scan-mode)
Bill Wohler's avatar
Bill Wohler committed
1074
    (mh-show)))
Richard M. Stallman's avatar
Richard M. Stallman committed
1075 1076

(defun mh-undo (msg-or-seq)
Bill Wohler's avatar
Bill Wohler committed
1077
  "Undo the pending deletion or refile of the specified MSG-OR-SEQ.
Bill Wohler's avatar
Bill Wohler committed
1078 1079
Default is the displayed message.
If optional prefix argument is provided, then prompt for the message sequence.
Bill Wohler's avatar
Bill Wohler committed
1080
If variable `transient-mark-mode' is non-nil and the mark is active, then the
Bill Wohler's avatar
Bill Wohler committed
1081 1082 1083 1084
selected region is unmarked.
In a program, MSG-OR-SEQ can be a message number, a list of message numbers, a
region in a cons cell, or a sequence."
  (interactive (list (mh-interactive-msg-or-seq "Undo")))
Richard M. Stallman's avatar
Richard M. Stallman committed
1085
  (cond ((numberp msg-or-seq)
Bill Wohler's avatar
Bill Wohler committed
1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100
         (let ((original-position (point)))
           (beginning-of-line)
           (while (not (or (looking-at mh-scan-deleted-msg-regexp)
                           (looking-at mh-scan-refiled-msg-regexp)
                           (and (eq mh-next-direction 'forward) (bobp))
                           (and (eq mh-next-direction 'backward)
                                (save-excursion (forward-line) (eobp)))))
             (forward-line (if (eq mh-next-direction 'forward) -1 1)))
           (if (or (looking-at mh-scan-deleted-msg-regexp)
                   (looking-at mh-scan-refiled-msg-regexp))
               (progn
                 (mh-undo-msg (mh-get-msg-num t))
                 (mh-maybe-show))
             (goto-char original-position)
             (error "Nothing to undo"))))
Bill Wohler's avatar
Bill Wohler committed
1101 1102
        (t (mh-iterate-on-msg-or-seq () msg-or-seq
             (mh-undo-msg nil))))
Richard M. Stallman's avatar
Richard M. Stallman committed
1103 1104 1105
  (if (not (mh-outstanding-commands-p))
      (mh-set-folder-modified-p nil)))

Bill Wohler's avatar
Bill Wohler committed
1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134
;;;###mh-autoload
(defun mh-folder-line-matches-show-buffer-p ()
  "Return t if the message under point in folder-mode is in the show buffer.
Return nil in any other circumstance (no message under point, no show buffer,
the message in the show buffer doesn't match."
  (and (eq major-mode 'mh-folder-mode)
       (mh-get-msg-num nil)
       mh-show-buffer
       (get-buffer mh-show-buffer)
       (buffer-file-name (get-buffer mh-show-buffer))
       (string-match ".*/\\([0-9]+\\)$"
                     (buffer-file-name (get-buffer mh-show-buffer)))
       (string-equal
        (match-string 1 (buffer-file-name (get-buffer mh-show-buffer)))
        (int-to-string (mh-get-msg-num nil)))))

(eval-when-compile (require 'gnus))

(defmacro mh-macro-expansion-time-gnus-version ()
  "Return Gnus version available at macro expansion time.
The macro evaluates the Gnus version at macro expansion time. If MH-E was
compiled then macro expansion happens at compile time."
  gnus-version)

(defun mh-run-time-gnus-version ()
  "Return Gnus version available at run time."
  (require 'gnus)
  gnus-version)

Karl Heuer's avatar
Karl Heuer committed
1135
;;;###autoload
Richard M. Stallman's avatar
Richard M. Stallman committed
1136
(defun mh-version ()
Bill Wohler's avatar
Bill Wohler committed
1137
  "Display version information about MH-E and the MH mail handling system."
Richard M. Stallman's avatar
Richard M. Stallman committed
1138 1139
  (interactive)
  (mh-find-progs)
Bill Wohler's avatar
Bill Wohler committed
1140
  (set-buffer (get-buffer-create mh-info-buffer))
Richard M. Stallman's avatar
Richard M. Stallman committed
1141
  (erase-buffer)
Bill Wohler's avatar
Bill Wohler committed
1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154
  ;; MH-E version.
  (insert "MH-E " mh-version "\n\n")
  ;; MH-E compilation details.
  (insert "MH-E compilation details:\n")
  (let* ((compiled-mhe (byte-code-function-p (symbol-function 'mh-version)))
         (gnus-compiled-version (if compiled-mhe
                                    (mh-macro-expansion-time-gnus-version)
                                  "N/A")))
    (insert " Byte compiled:\t\t" (if compiled-mhe "yes" "no") "\n"
            " Gnus (compile-time):\t" gnus-compiled-version "\n"
            " Gnus (run-time):\t" (mh-run-time-gnus-version) "\n\n"))
  ;; Emacs version.
  (insert (emacs-version) "\n\n")
Bill Wohler's avatar
Bill Wohler committed
1155
  ;; MH version.
Richard M. Stallman's avatar
Richard M. Stallman committed
1156 1157
  (let ((help-start (point)))
    (condition-case err-data
Bill Wohler's avatar
Bill Wohler committed
1158
        (mh-exec-cmd-output "inc" nil (if mh-nmh-flag "-version" "-help"))
Bill Wohler's avatar
Bill Wohler committed
1159
      (file-error (insert (mapconcat 'concat (cdr err-data) ": ") "\n")))
Richard M. Stallman's avatar
Richard M. Stallman committed
1160
    (goto-char help-start)
Bill Wohler's avatar
Bill Wohler committed
1161
    (if mh-nmh-flag
Bill Wohler's avatar
Bill Wohler committed
1162
        (search-forward "inc -- " nil t)