Merge from trunk.
[emacs.git] / lisp / mh-e / mh-scan.el
blob30bcf9f4647532af71e16c4dd852d4873b7b434d
1 ;;; mh-scan.el --- MH-E scan line constants and utilities
3 ;; Copyright (C) 1993, 1995, 1997, 2000-2012 Free Software Foundation, Inc.
5 ;; Author: Bill Wohler <wohler@newt.com>
6 ;; Maintainer: Bill Wohler <wohler@newt.com>
7 ;; Keywords: mail
8 ;; See: mh-e.el
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;; This file contains constants and a few functions for interpreting
28 ;; scan lines.
30 ;;; Change Log:
32 ;;; Code:
34 (require 'mh-e)
38 ;;; Scan Formats
40 ;; The following scan formats are passed to the scan program if the setting of
41 ;; `mh-scan-format-file' is t. They are identical except the later one makes
42 ;; use of the nmh `decode' function to decode RFC 2047 encodings. If you just
43 ;; want to change the column of the notations, use the `mh-set-cmd-note'
44 ;; function.
46 (defvar mh-scan-format-mh
47 (concat
48 "%4(msg)"
49 "%<(cur)+%| %>"
50 "%<{replied}-"
51 "%?(nonnull(comp{to}))%<(mymbox{to})t%>"
52 "%?(nonnull(comp{cc}))%<(mymbox{cc})c%>"
53 "%?(nonnull(comp{bcc}))%<(mymbox{bcc})b%>"
54 "%?(nonnull(comp{newsgroups}))n%>"
55 "%<(zero) %>"
56 "%02(mon{date})/%02(mday{date})%<{date} %|*%>"
57 "%<(mymbox{from})%<{to}To:%14(friendly{to})%>%>"
58 "%<(zero)%17(friendly{from})%> "
59 "%{subject}%<{body}<<%{body}%>")
60 "*Scan format string for MH.
61 This string is passed to the scan program via the -format
62 argument. This format is identical to the default except that
63 additional hints for fontification have been added to the fifth
64 column (remember that in Emacs, the first column is 0).
66 The values of the fifth column, in priority order, are: \"-\" if
67 the message has been replied to, t if an address on the To: line
68 matches one of the mailboxes of the current user, \"c\" if the Cc:
69 line matches, \"b\" if the Bcc: line matches, and \"n\" if a
70 non-empty Newsgroups: header is present.")
72 (defvar mh-scan-format-nmh
73 (concat
74 "%4(msg)"
75 "%<(cur)+%| %>"
76 "%<{replied}-"
77 "%?(nonnull(comp{to}))%<(mymbox{to})t%>"
78 "%?(nonnull(comp{cc}))%<(mymbox{cc})c%>"
79 "%?(nonnull(comp{bcc}))%<(mymbox{bcc})b%>"
80 "%?(nonnull(comp{newsgroups}))n%>"
81 "%<(zero) %>"
82 "%02(mon{date})/%02(mday{date})%<{date} %|*%>"
83 "%<(mymbox{from})%<{to}To:%14(decode(friendly{to}))%>%>"
84 "%<(zero)%17(decode(friendly{from}))%> "
85 "%(decode{subject})%<{body}<<%{body}%>")
86 "*Scan format string for nmh.
87 This string is passed to the scan program via the -format arg.
88 This format is identical to the default except that additional
89 hints for fontification have been added to the fifth
90 column (remember that in Emacs, the first column is 0).
92 The values of the fifth column, in priority order, are: \"-\" if
93 the message has been replied to, t if an address on the To: field
94 matches one of the mailboxes of the current user, \"c\" if the Cc:
95 field matches, \"b\" if the Bcc: field matches, and \"n\" if a
96 non-empty Newsgroups: field is present.")
100 ;;; Regular Expressions
102 ;; Alphabetical.
104 (defvar mh-scan-body-regexp "\\(<<\\([^\n]+\\)?\\)"
105 "This regular expression matches the message body fragment.
107 Note that the default setting of `mh-folder-font-lock-keywords'
108 expects this expression to contain at least one parenthesized
109 expression which matches the body text as in the default of
110 \"\\\\(<<\\\\([^\\n]+\\\\)?\\\\)\". If this regular expression is
111 not correct, the body fragment will not be highlighted with the
112 face `mh-folder-body'.")
114 (defvar mh-scan-blacklisted-msg-regexp "^\\( *[0-9]+\\)B"
115 "This regular expression matches blacklisted (spam) messages.
117 It must match from the beginning of the line. Note that the
118 default setting of `mh-folder-font-lock-keywords' expects this
119 expression to contain at least one parenthesized expression which
120 matches the message number as in the default of
122 \"^\\\\( *[0-9]+\\\\)B\".
124 This expression includes the leading space within parenthesis
125 since it looks better to highlight it as well. The highlighting
126 is done with the face `mh-folder-blacklisted'. This regular
127 expression should be correct as it is needed by non-fontification
128 functions. See also `mh-note-blacklisted'.")
130 (defvar mh-scan-cur-msg-number-regexp "^\\( *[0-9]+\\+\\).*"
131 "This regular expression matches the current message.
133 It must match from the beginning of the line. Note that the
134 default setting of `mh-folder-font-lock-keywords' expects this
135 expression to contain at least one parenthesized expression which
136 matches the message number as in the default of
138 \"^\\\\( *[0-9]+\\\\+\\\\).*\".
140 This expression includes the leading space and current message
141 marker \"+\" within the parenthesis since it looks better to
142 highlight these items as well. The highlighting is done with the
143 face `mh-folder-cur-msg-number'. This regular expression should
144 be correct as it is needed by non-fontification functions. See
145 also `mh-note-cur'.")
147 (defvar mh-scan-date-regexp "\\([0-9][0-9]/[0-9][0-9]\\)"
148 "This regular expression matches a valid date.
150 It must not be anchored to the beginning or the end of the line.
151 Note that the default setting of `mh-folder-font-lock-keywords'
152 expects this expression to contain only one parenthesized
153 expression which matches the date field as in the default of
154 \"\\\\([0-9][0-9]/[0-9][0-9]\\\\)\"}. If this regular expression
155 is not correct, the date will not be highlighted with the face
156 `mh-folder-date'.")
158 (defvar mh-scan-deleted-msg-regexp "^\\( *[0-9]+\\)D"
159 "This regular expression matches deleted messages.
161 It must match from the beginning of the line. Note that the
162 default setting of `mh-folder-font-lock-keywords' expects this
163 expression to contain at least one parenthesized expression which
164 matches the message number as in the default of
166 \"^\\\\( *[0-9]+\\\\)D\".
168 This expression includes the leading space within the parenthesis
169 since it looks better to highlight it as well. The highlighting
170 is done with the face `mh-folder-deleted'. This regular
171 expression should be correct as it is needed by non-fontification
172 functions. See also `mh-note-deleted'.")
174 (defvar mh-scan-good-msg-regexp "^\\( *[0-9]+\\)[^^DBW0-9]"
175 "This regular expression matches \"good\" messages.
177 It must match from the beginning of the line. Note that the
178 default setting of `mh-folder-font-lock-keywords' expects this
179 expression to contain at least one parenthesized expression which
180 matches the message number as in the default of
182 \"^\\\\( *[0-9]+\\\\)[^^DBW0-9]\".
184 This expression includes the leading space within the parenthesis
185 since it looks better to highlight it as well. The highlighting
186 is done with the face `mh-folder-msg-number'. This regular
187 expression should be correct as it is needed by non-fontification
188 functions.")
190 (defvar mh-scan-msg-format-regexp "%\\([0-9]*\\)(msg)"
191 "This regular expression finds the message number width in a scan format.
193 Note that the message number must be placed in a parenthesized
194 expression as in the default of \"%\\\\([0-9]*\\\\)(msg)\". This
195 variable is only consulted if `mh-scan-format-file' is set to
196 \"Use MH-E scan Format\".")
198 (defvar mh-scan-msg-format-string "%d"
199 "This is a format string for width of the message number in a scan format.
201 Use \"0%d\" for zero-filled message numbers. This variable is only
202 consulted if `mh-scan-format-file' is set to \"Use MH-E scan
203 Format\".")
205 (defvar mh-scan-msg-number-regexp "^ *\\([0-9]+\\)"
206 "This regular expression extracts the message number.
208 It must match from the beginning of the line. Note that the
209 message number must be placed in a parenthesized expression as in
210 the default of \"^ *\\\\([0-9]+\\\\)\".")
212 (defvar mh-scan-msg-overflow-regexp "^[?0-9][0-9]"
213 "This regular expression matches overflowed message numbers.")
215 (defvar mh-scan-msg-search-regexp "^[^0-9]*%d[^0-9]"
216 "This regular expression matches a particular message.
218 It is a format string; use \"%d\" to represent the location of the
219 message number within the expression as in the default of
220 \"^[^0-9]*%d[^0-9]\".")
222 (defvar mh-scan-rcpt-regexp "\\(To:\\)\\(..............\\)"
223 "This regular expression specifies the recipient in messages you sent.
225 Note that the default setting of `mh-folder-font-lock-keywords'
226 expects this expression to contain two parenthesized expressions.
227 The first is expected to match the \"To:\" that the default scan
228 format file generates. The second is expected to match the
229 recipient's name as in the default of
230 \"\\\\(To:\\\\)\\\\(..............\\\\)\". If this regular
231 expression is not correct, the \"To:\" string will not be
232 highlighted with the face `mh-folder-to' and the recipient will
233 not be highlighted with the face `mh-folder-address'")
235 (defvar mh-scan-refiled-msg-regexp "^\\( *[0-9]+\\)\\^"
236 "This regular expression matches refiled messages.
238 It must match from the beginning of the line. Note that the
239 default setting of `mh-folder-font-lock-keywords' expects this
240 expression to contain at least one parenthesized expression which
241 matches the message number as in the default of
243 \"^\\\\( *[0-9]+\\\\)\\\\^\".
245 This expression includes the leading space within the parenthesis
246 since it looks better to highlight it as well. The highlighting
247 is done with the face `mh-folder-refiled'. This regular
248 expression should be correct as it is needed by non-fontification
249 functions. See also `mh-note-refiled'.")
251 (defvar mh-scan-sent-to-me-sender-regexp
252 "^ *[0-9]+.\\([bct]\\).....[ ]*\\(..................\\)"
253 "This regular expression matches messages sent to us.
255 Note that the default setting of `mh-folder-font-lock-keywords'
256 expects this expression to contain at least two parenthesized
257 expressions. The first should match the fontification hint (see
258 `mh-scan-format-nmh') and the second should match the user name
259 as in the default of
261 ^ *[0-9]+.\\\\([bct]\\\\).....[ ]*\\\\(..................\\\\)
263 If this regular expression is not correct, the notation hints
264 will not be highlighted with the face
265 `mh-mh-folder-sent-to-me-hint' and the sender will not be
266 highlighted with the face `mh-folder-sent-to-me-sender'.")
268 (defvar mh-scan-subject-regexp
269 "^ *[0-9]+........[ ]*...................\\([Rr][Ee]\\(\\[[0-9]+\\]\\)?:\\s-*\\)*\\([^<\n]*\\)"
270 "This regular expression matches the subject.
272 It must match from the beginning of the line. Note that the
273 default setting of `mh-folder-font-lock-keywords' expects this
274 expression to contain at least three parenthesized expressions.
275 The first is expected to match the \"Re:\" string, if any, and is
276 highlighted with the face `mh-folder-followup'. The second
277 matches an optional bracketed number after \"Re:\", such as in
278 \"Re[2]:\" (and is thus a sub-expression of the first expression)
279 and the third is expected to match the subject line itself which
280 is highlighted with the face `mh-folder-subject'. For example,
281 the default (broken on multiple lines for readability) is
283 ^ *[0-9]+........[ ]*...................
284 \\\\([Rr][Ee]\\\\(\\\\\\=[[0-9]+\\\\]\\\\)?:\\\\s-*\\\\)*
285 \\\\([^<\\n]*\\\\)
287 This regular expression should be correct as it is needed by
288 non-fontification functions.")
290 (defvar mh-scan-valid-regexp "^ *[0-9]"
291 "This regular expression describes a valid scan line.
293 This is used to eliminate error messages that are occasionally
294 produced by \"inc\".")
296 (defvar mh-scan-whitelisted-msg-regexp "^\\( *[0-9]+\\)W"
297 "This regular expression matches whitelisted (non-spam) messages.
299 It must match from the beginning of the line. Note that the
300 default setting of `mh-folder-font-lock-keywords' expects this
301 expression to contain at least one parenthesized expression which
302 matches the message number as in the default of
304 \"^\\\\( *[0-9]+\\\\)W\".
306 This expression includes the leading space within parenthesis
307 since it looks better to highlight it as well. The highlighting
308 is done with the face `mh-folder-whitelisted'. This regular
309 expression should be correct as it is needed by non-fontification
310 functions. See also `mh-note-whitelisted'.")
314 ;;; Widths, Offsets and Columns
316 (defvar mh-cmd-note 4
317 "Column for notations.
319 This variable should be set with the function `mh-set-cmd-note'.
320 This variable may be updated dynamically if
321 `mh-adaptive-cmd-note-flag' is on.
323 Note that columns in Emacs start with 0.")
324 (make-variable-buffer-local 'mh-cmd-note)
326 (defvar mh-scan-cmd-note-width 1
327 "Number of columns consumed by the cmd-note field in `mh-scan-format'.
329 This column will have one of the values: \" \", \"^\", \"D\", \"B\", \"W\", \"+\", where
331 \" \" is the default value,
332 \"^\" is the `mh-note-refiled' character,
333 \"D\" is the `mh-note-deleted' character,
334 \"B\" is the `mh-note-blacklisted' character,
335 \"W\" is the `mh-note-whitelisted' character, and
336 \"+\" is the `mh-note-cur' character.")
338 (defvar mh-scan-destination-width 1
339 "Number of columns consumed by the destination field in `mh-scan-format'.
341 This column will have one of \" \", \"%\", \"-\", \"t\", \"c\", \"b\", or \"n\"
342 in it.
344 \" \" blank space is the default character.
345 \"%\" indicates that the message in a named MH sequence.
346 \"-\" indicates that the message has been annotated with a replied field.
347 \"t\" indicates that the message contains mymbox in the To: field.
348 \"c\" indicates that the message contains mymbox in the Cc: field.
349 \"b\" indicates that the message contains mymbox in the Bcc: field.
350 \"n\" indicates that the message contains a Newsgroups: field.")
352 (defvar mh-scan-date-width 5
353 "Number of columns consumed by the date field in `mh-scan-format'.
354 This column will typically be of the form mm/dd.")
356 (defvar mh-scan-date-flag-width 1
357 "Number of columns consumed to flag (in)valid dates in `mh-scan-format'.
358 This column will have \" \" for valid and \"*\" for invalid or
359 missing dates.")
361 (defvar mh-scan-from-mbox-width 17
362 "Number of columns consumed with the \"From:\" line in `mh-scan-format'.
363 This column will have a friendly name or e-mail address of the
364 originator, or a \"To: address\" for outgoing e-mail messages.")
366 (defvar mh-scan-from-mbox-sep-width 2
367 "Number of columns consumed by whitespace after from-mbox in `mh-scan-format'.
368 This column will only ever have spaces in it.")
370 (defvar mh-scan-field-destination-offset
371 (+ mh-scan-cmd-note-width)
372 "The offset from the `mh-cmd-note' for the destination column.")
374 (defvar mh-scan-field-from-start-offset
375 (+ mh-scan-cmd-note-width
376 mh-scan-destination-width
377 mh-scan-date-width
378 mh-scan-date-flag-width)
379 "The offset from the `mh-cmd-note' to find the start of \"From:\" address.")
381 (defvar mh-scan-field-from-end-offset
382 (+ mh-scan-field-from-start-offset mh-scan-from-mbox-width)
383 "The offset from the `mh-cmd-note' to find the end of \"From:\" address.")
385 (defvar mh-scan-field-subject-start-offset
386 (+ mh-scan-cmd-note-width
387 mh-scan-destination-width
388 mh-scan-date-width
389 mh-scan-date-flag-width
390 mh-scan-from-mbox-width
391 mh-scan-from-mbox-sep-width)
392 "The offset from the `mh-cmd-note' to find the start of the subject.")
396 ;;; Notation
398 ;; Alphabetical.
400 (defvar mh-note-blacklisted ?B
401 "Messages that have been blacklisted are marked by this character.
402 See also `mh-scan-blacklisted-msg-regexp'.")
404 (defvar mh-note-cur ?+
405 "The current message (in MH, not in MH-E) is marked by this character.
406 See also `mh-scan-cur-msg-number-regexp'.")
408 (defvar mh-note-copied ?C
409 "Messages that have been copied are marked by this character.")
411 (defvar mh-note-deleted ?D
412 "Messages that have been deleted are marked by this character.
413 See also `mh-scan-deleted-msg-regexp'.")
415 (defvar mh-note-dist ?R
416 "Messages that have been redistributed are marked by this character.")
418 (defvar mh-note-forw ?F
419 "Messages that have been forwarded are marked by this character.")
421 (defvar mh-note-printed ?P
422 "Messages that have been printed are marked by this character.")
424 (defvar mh-note-refiled ?^
425 "Messages that have been refiled are marked by this character.
426 See also `mh-scan-refiled-msg-regexp'.")
428 (defvar mh-note-repl ?-
429 "Messages that have been replied to are marked by this character.")
431 (defvar mh-note-seq ?%
432 "Messages in a user-defined sequence are marked by this character.
434 Messages in the \"search\" sequence are marked by this character as
435 well.")
437 (defvar mh-note-whitelisted ?W
438 "Messages that have been whitelisted are marked by this character.
439 See also `mh-scan-whitelisted-msg-regexp'.")
443 ;;; Utilities
445 ;;;###mh-autoload
446 (defun mh-scan-msg-number-regexp ()
447 "Return value of variable `mh-scan-msg-number-regexp'."
448 mh-scan-msg-number-regexp)
450 ;;;###mh-autoload
451 (defun mh-scan-msg-search-regexp ()
452 "Return value of variable `mh-scan-msg-search-regexp'."
453 mh-scan-msg-search-regexp)
455 ;;;###mh-autoload
456 (defun mh-set-cmd-note (column)
457 "Set `mh-cmd-note' to COLUMN.
458 Note that columns in Emacs start with 0."
459 (setq mh-cmd-note column))
461 ;;;###mh-autoload
462 (defun mh-scan-format ()
463 "Return the output format argument for the scan program."
464 (if (equal mh-scan-format-file t)
465 (list "-format" (if (mh-variant-p 'nmh 'gnu-mh)
466 (list (mh-update-scan-format
467 mh-scan-format-nmh mh-cmd-note))
468 (list (mh-update-scan-format
469 mh-scan-format-mh mh-cmd-note))))
470 (if (not (equal mh-scan-format-file nil))
471 (list "-form" mh-scan-format-file))))
473 (defun mh-update-scan-format (fmt width)
474 "Return a scan format with the (msg) width in the FMT replaced with WIDTH.
476 The message number width portion of the format is discovered
477 using `mh-scan-msg-format-regexp'. Its replacement is controlled
478 with `mh-scan-msg-format-string'."
479 (or (and
480 (string-match mh-scan-msg-format-regexp fmt)
481 (let ((begin (match-beginning 1))
482 (end (match-end 1)))
483 (concat (substring fmt 0 begin)
484 (format mh-scan-msg-format-string width)
485 (substring fmt end))))
486 fmt))
488 ;;;###mh-autoload
489 (defun mh-msg-num-width (folder)
490 "Return the width of the largest message number in this FOLDER."
491 (or mh-progs (mh-find-path))
492 (let ((tmp-buffer (get-buffer-create mh-temp-buffer))
493 (width 0))
494 (with-current-buffer tmp-buffer
495 (erase-buffer)
496 (apply 'call-process
497 (expand-file-name mh-scan-prog mh-progs) nil '(t nil) nil
498 (list folder "last" "-format" "%(msg)"))
499 (goto-char (point-min))
500 (if (re-search-forward mh-scan-msg-number-regexp nil 0 1)
501 (setq width (length (buffer-substring
502 (match-beginning 1) (match-end 1))))))
503 width))
505 ;;;###mh-autoload
506 (defun mh-msg-num-width-to-column (width)
507 "Return the column for notations given message number WIDTH.
508 Note that columns in Emacs start with 0.
510 If `mh-scan-format-file' is set to \"Use MH-E scan Format\" this
511 means that either `mh-scan-format-mh' or `mh-scan-format-nmh' are
512 in use. This function therefore assumes that the first column is
513 empty (to provide room for the cursor), the following WIDTH
514 columns contain the message number, and the column for notations
515 comes after that."
516 (if (eq mh-scan-format-file t)
517 (max (1+ width) 2)
518 (error "%s %s" "Can't call `mh-msg-num-width-to-column' when"
519 "`mh-scan-format-file' is not set to \"Use MH-E scan Format\"")))
521 (provide 'mh-scan)
523 ;; Local Variables:
524 ;; indent-tabs-mode: nil
525 ;; sentence-end-double-space: nil
526 ;; End:
528 ;;; mh-scan.el ends here