Update copyright year to 2014 by running admin/update-copyright.
[emacs.git] / lisp / mh-e / mh-scan.el
blobd9913741e20e418d6f689c199e0d5feb394760da
1 ;;; mh-scan.el --- MH-E scan line constants and utilities
3 ;; Copyright (C) 1993, 1995, 1997, 2000-2014 Free Software Foundation,
4 ;; Inc.
6 ;; Author: Bill Wohler <wohler@newt.com>
7 ;; Maintainer: Bill Wohler <wohler@newt.com>
8 ;; Keywords: mail
9 ;; See: mh-e.el
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;;; Commentary:
28 ;; This file contains constants and a few functions for interpreting
29 ;; scan lines.
31 ;;; Change Log:
33 ;;; Code:
35 (require 'mh-e)
39 ;;; Scan Formats
41 ;; The following scan formats are passed to the scan program if the setting of
42 ;; `mh-scan-format-file' is t. They are identical except the later one makes
43 ;; use of the nmh `decode' function to decode RFC 2047 encodings. If you just
44 ;; want to change the column of the notations, use the `mh-set-cmd-note'
45 ;; function.
47 (defvar mh-scan-format-mh
48 (concat
49 "%4(msg)"
50 "%<(cur)+%| %>"
51 "%<{replied}-"
52 "%?(nonnull(comp{to}))%<(mymbox{to})t%>"
53 "%?(nonnull(comp{cc}))%<(mymbox{cc})c%>"
54 "%?(nonnull(comp{bcc}))%<(mymbox{bcc})b%>"
55 "%?(nonnull(comp{newsgroups}))n%>"
56 "%<(zero) %>"
57 "%02(mon{date})/%02(mday{date})%<{date} %|*%>"
58 "%<(mymbox{from})%<{to}To:%14(friendly{to})%>%>"
59 "%<(zero)%17(friendly{from})%> "
60 "%{subject}%<{body}<<%{body}%>")
61 "*Scan format string for MH.
62 This string is passed to the scan program via the -format
63 argument. This format is identical to the default except that
64 additional hints for fontification have been added to the fifth
65 column (remember that in Emacs, the first column is 0).
67 The values of the fifth column, in priority order, are: \"-\" if
68 the message has been replied to, t if an address on the To: line
69 matches one of the mailboxes of the current user, \"c\" if the Cc:
70 line matches, \"b\" if the Bcc: line matches, and \"n\" if a
71 non-empty Newsgroups: header is present.")
73 (defvar mh-scan-format-nmh
74 (concat
75 "%4(msg)"
76 "%<(cur)+%| %>"
77 "%<{replied}-"
78 "%?(nonnull(comp{to}))%<(mymbox{to})t%>"
79 "%?(nonnull(comp{cc}))%<(mymbox{cc})c%>"
80 "%?(nonnull(comp{bcc}))%<(mymbox{bcc})b%>"
81 "%?(nonnull(comp{newsgroups}))n%>"
82 "%<(zero) %>"
83 "%02(mon{date})/%02(mday{date})%<{date} %|*%>"
84 "%<(mymbox{from})%<{to}To:%14(decode(friendly{to}))%>%>"
85 "%<(zero)%17(decode(friendly{from}))%> "
86 "%(decode{subject})%<{body}<<%{body}%>")
87 "*Scan format string for nmh.
88 This string is passed to the scan program via the -format arg.
89 This format is identical to the default except that additional
90 hints for fontification have been added to the fifth
91 column (remember that in Emacs, the first column is 0).
93 The values of the fifth column, in priority order, are: \"-\" if
94 the message has been replied to, t if an address on the To: field
95 matches one of the mailboxes of the current user, \"c\" if the Cc:
96 field matches, \"b\" if the Bcc: field matches, and \"n\" if a
97 non-empty Newsgroups: field is present.")
101 ;;; Regular Expressions
103 ;; Alphabetical.
105 (defvar mh-scan-body-regexp "\\(<<\\([^\n]+\\)?\\)"
106 "This regular expression matches the message body fragment.
108 Note that the default setting of `mh-folder-font-lock-keywords'
109 expects this expression to contain at least one parenthesized
110 expression which matches the body text as in the default of
111 \"\\\\(<<\\\\([^\\n]+\\\\)?\\\\)\". If this regular expression is
112 not correct, the body fragment will not be highlighted with the
113 face `mh-folder-body'.")
115 (defvar mh-scan-blacklisted-msg-regexp "^\\( *[0-9]+\\)B"
116 "This regular expression matches blacklisted (spam) messages.
118 It must match from the beginning of the line. Note that the
119 default setting of `mh-folder-font-lock-keywords' expects this
120 expression to contain at least one parenthesized expression which
121 matches the message number as in the default of
123 \"^\\\\( *[0-9]+\\\\)B\".
125 This expression includes the leading space within parenthesis
126 since it looks better to highlight it as well. The highlighting
127 is done with the face `mh-folder-blacklisted'. This regular
128 expression should be correct as it is needed by non-fontification
129 functions. See also `mh-note-blacklisted'.")
131 (defvar mh-scan-cur-msg-number-regexp "^\\( *[0-9]+\\+\\).*"
132 "This regular expression matches the current message.
134 It must match from the beginning of the line. Note that the
135 default setting of `mh-folder-font-lock-keywords' expects this
136 expression to contain at least one parenthesized expression which
137 matches the message number as in the default of
139 \"^\\\\( *[0-9]+\\\\+\\\\).*\".
141 This expression includes the leading space and current message
142 marker \"+\" within the parenthesis since it looks better to
143 highlight these items as well. The highlighting is done with the
144 face `mh-folder-cur-msg-number'. This regular expression should
145 be correct as it is needed by non-fontification functions. See
146 also `mh-note-cur'.")
148 (defvar mh-scan-date-regexp "\\([0-9][0-9]/[0-9][0-9]\\)"
149 "This regular expression matches a valid date.
151 It must not be anchored to the beginning or the end of the line.
152 Note that the default setting of `mh-folder-font-lock-keywords'
153 expects this expression to contain only one parenthesized
154 expression which matches the date field as in the default of
155 \"\\\\([0-9][0-9]/[0-9][0-9]\\\\)\"}. If this regular expression
156 is not correct, the date will not be highlighted with the face
157 `mh-folder-date'.")
159 (defvar mh-scan-deleted-msg-regexp "^\\( *[0-9]+\\)D"
160 "This regular expression matches deleted messages.
162 It must match from the beginning of the line. Note that the
163 default setting of `mh-folder-font-lock-keywords' expects this
164 expression to contain at least one parenthesized expression which
165 matches the message number as in the default of
167 \"^\\\\( *[0-9]+\\\\)D\".
169 This expression includes the leading space within the parenthesis
170 since it looks better to highlight it as well. The highlighting
171 is done with the face `mh-folder-deleted'. This regular
172 expression should be correct as it is needed by non-fontification
173 functions. See also `mh-note-deleted'.")
175 (defvar mh-scan-good-msg-regexp "^\\( *[0-9]+\\)[^^DBW0-9]"
176 "This regular expression matches \"good\" messages.
178 It must match from the beginning of the line. Note that the
179 default setting of `mh-folder-font-lock-keywords' expects this
180 expression to contain at least one parenthesized expression which
181 matches the message number as in the default of
183 \"^\\\\( *[0-9]+\\\\)[^^DBW0-9]\".
185 This expression includes the leading space within the parenthesis
186 since it looks better to highlight it as well. The highlighting
187 is done with the face `mh-folder-msg-number'. This regular
188 expression should be correct as it is needed by non-fontification
189 functions.")
191 (defvar mh-scan-msg-format-regexp "%\\([0-9]*\\)(msg)"
192 "This regular expression finds the message number width in a scan format.
194 Note that the message number must be placed in a parenthesized
195 expression as in the default of \"%\\\\([0-9]*\\\\)(msg)\". This
196 variable is only consulted if `mh-scan-format-file' is set to
197 \"Use MH-E scan Format\".")
199 (defvar mh-scan-msg-format-string "%d"
200 "This is a format string for width of the message number in a scan format.
202 Use \"0%d\" for zero-filled message numbers. This variable is only
203 consulted if `mh-scan-format-file' is set to \"Use MH-E scan
204 Format\".")
206 (defvar mh-scan-msg-number-regexp "^ *\\([0-9]+\\)"
207 "This regular expression extracts the message number.
209 It must match from the beginning of the line. Note that the
210 message number must be placed in a parenthesized expression as in
211 the default of \"^ *\\\\([0-9]+\\\\)\".")
213 (defvar mh-scan-msg-overflow-regexp "^[?0-9][0-9]"
214 "This regular expression matches overflowed message numbers.")
216 (defvar mh-scan-msg-search-regexp "^[^0-9]*%d[^0-9]"
217 "This regular expression matches a particular message.
219 It is a format string; use \"%d\" to represent the location of the
220 message number within the expression as in the default of
221 \"^[^0-9]*%d[^0-9]\".")
223 (defvar mh-scan-rcpt-regexp "\\(To:\\)\\(..............\\)"
224 "This regular expression specifies the recipient in messages you sent.
226 Note that the default setting of `mh-folder-font-lock-keywords'
227 expects this expression to contain two parenthesized expressions.
228 The first is expected to match the \"To:\" that the default scan
229 format file generates. The second is expected to match the
230 recipient's name as in the default of
231 \"\\\\(To:\\\\)\\\\(..............\\\\)\". If this regular
232 expression is not correct, the \"To:\" string will not be
233 highlighted with the face `mh-folder-to' and the recipient will
234 not be highlighted with the face `mh-folder-address'")
236 (defvar mh-scan-refiled-msg-regexp "^\\( *[0-9]+\\)\\^"
237 "This regular expression matches refiled messages.
239 It must match from the beginning of the line. Note that the
240 default setting of `mh-folder-font-lock-keywords' expects this
241 expression to contain at least one parenthesized expression which
242 matches the message number as in the default of
244 \"^\\\\( *[0-9]+\\\\)\\\\^\".
246 This expression includes the leading space within the parenthesis
247 since it looks better to highlight it as well. The highlighting
248 is done with the face `mh-folder-refiled'. This regular
249 expression should be correct as it is needed by non-fontification
250 functions. See also `mh-note-refiled'.")
252 (defvar mh-scan-sent-to-me-sender-regexp
253 "^ *[0-9]+.\\([bct]\\).....[ ]*\\(..................\\)"
254 "This regular expression matches messages sent to us.
256 Note that the default setting of `mh-folder-font-lock-keywords'
257 expects this expression to contain at least two parenthesized
258 expressions. The first should match the fontification hint (see
259 `mh-scan-format-nmh') and the second should match the user name
260 as in the default of
262 ^ *[0-9]+.\\\\([bct]\\\\).....[ ]*\\\\(..................\\\\)
264 If this regular expression is not correct, the notation hints
265 will not be highlighted with the face
266 `mh-mh-folder-sent-to-me-hint' and the sender will not be
267 highlighted with the face `mh-folder-sent-to-me-sender'.")
269 (defvar mh-scan-subject-regexp
270 "^ *[0-9]+........[ ]*...................\\([Rr][Ee]\\(\\[[0-9]+\\]\\)?:\\s-*\\)*\\([^<\n]*\\)"
271 "This regular expression matches the subject.
273 It must match from the beginning of the line. Note that the
274 default setting of `mh-folder-font-lock-keywords' expects this
275 expression to contain at least three parenthesized expressions.
276 The first is expected to match the \"Re:\" string, if any, and is
277 highlighted with the face `mh-folder-followup'. The second
278 matches an optional bracketed number after \"Re:\", such as in
279 \"Re[2]:\" (and is thus a sub-expression of the first expression)
280 and the third is expected to match the subject line itself which
281 is highlighted with the face `mh-folder-subject'. For example,
282 the default (broken on multiple lines for readability) is
284 ^ *[0-9]+........[ ]*...................
285 \\\\([Rr][Ee]\\\\(\\\\\\=[[0-9]+\\\\]\\\\)?:\\\\s-*\\\\)*
286 \\\\([^<\\n]*\\\\)
288 This regular expression should be correct as it is needed by
289 non-fontification functions.")
291 (defvar mh-scan-valid-regexp "^ *[0-9]"
292 "This regular expression describes a valid scan line.
294 This is used to eliminate error messages that are occasionally
295 produced by \"inc\".")
297 (defvar mh-scan-whitelisted-msg-regexp "^\\( *[0-9]+\\)W"
298 "This regular expression matches whitelisted (non-spam) messages.
300 It must match from the beginning of the line. Note that the
301 default setting of `mh-folder-font-lock-keywords' expects this
302 expression to contain at least one parenthesized expression which
303 matches the message number as in the default of
305 \"^\\\\( *[0-9]+\\\\)W\".
307 This expression includes the leading space within parenthesis
308 since it looks better to highlight it as well. The highlighting
309 is done with the face `mh-folder-whitelisted'. This regular
310 expression should be correct as it is needed by non-fontification
311 functions. See also `mh-note-whitelisted'.")
315 ;;; Widths, Offsets and Columns
317 (defvar mh-cmd-note 4
318 "Column for notations.
320 This variable should be set with the function `mh-set-cmd-note'.
321 This variable may be updated dynamically if
322 `mh-adaptive-cmd-note-flag' is on.
324 Note that columns in Emacs start with 0.")
325 (make-variable-buffer-local 'mh-cmd-note)
327 (defvar mh-scan-cmd-note-width 1
328 "Number of columns consumed by the cmd-note field in `mh-scan-format'.
330 This column will have one of the values: \" \", \"^\", \"D\", \"B\", \"W\", \"+\", where
332 \" \" is the default value,
333 \"^\" is the `mh-note-refiled' character,
334 \"D\" is the `mh-note-deleted' character,
335 \"B\" is the `mh-note-blacklisted' character,
336 \"W\" is the `mh-note-whitelisted' character, and
337 \"+\" is the `mh-note-cur' character.")
339 (defvar mh-scan-destination-width 1
340 "Number of columns consumed by the destination field in `mh-scan-format'.
342 This column will have one of \" \", \"%\", \"-\", \"t\", \"c\", \"b\", or \"n\"
343 in it.
345 \" \" blank space is the default character.
346 \"%\" indicates that the message in a named MH sequence.
347 \"-\" indicates that the message has been annotated with a replied field.
348 \"t\" indicates that the message contains mymbox in the To: field.
349 \"c\" indicates that the message contains mymbox in the Cc: field.
350 \"b\" indicates that the message contains mymbox in the Bcc: field.
351 \"n\" indicates that the message contains a Newsgroups: field.")
353 (defvar mh-scan-date-width 5
354 "Number of columns consumed by the date field in `mh-scan-format'.
355 This column will typically be of the form mm/dd.")
357 (defvar mh-scan-date-flag-width 1
358 "Number of columns consumed to flag (in)valid dates in `mh-scan-format'.
359 This column will have \" \" for valid and \"*\" for invalid or
360 missing dates.")
362 (defvar mh-scan-from-mbox-width 17
363 "Number of columns consumed with the \"From:\" line in `mh-scan-format'.
364 This column will have a friendly name or e-mail address of the
365 originator, or a \"To: address\" for outgoing e-mail messages.")
367 (defvar mh-scan-from-mbox-sep-width 2
368 "Number of columns consumed by whitespace after from-mbox in `mh-scan-format'.
369 This column will only ever have spaces in it.")
371 (defvar mh-scan-field-destination-offset
372 (+ mh-scan-cmd-note-width)
373 "The offset from the `mh-cmd-note' for the destination column.")
375 (defvar mh-scan-field-from-start-offset
376 (+ mh-scan-cmd-note-width
377 mh-scan-destination-width
378 mh-scan-date-width
379 mh-scan-date-flag-width)
380 "The offset from the `mh-cmd-note' to find the start of \"From:\" address.")
382 (defvar mh-scan-field-from-end-offset
383 (+ mh-scan-field-from-start-offset mh-scan-from-mbox-width)
384 "The offset from the `mh-cmd-note' to find the end of \"From:\" address.")
386 (defvar mh-scan-field-subject-start-offset
387 (+ mh-scan-cmd-note-width
388 mh-scan-destination-width
389 mh-scan-date-width
390 mh-scan-date-flag-width
391 mh-scan-from-mbox-width
392 mh-scan-from-mbox-sep-width)
393 "The offset from the `mh-cmd-note' to find the start of the subject.")
397 ;;; Notation
399 ;; Alphabetical.
401 (defvar mh-note-blacklisted ?B
402 "Messages that have been blacklisted are marked by this character.
403 See also `mh-scan-blacklisted-msg-regexp'.")
405 (defvar mh-note-cur ?+
406 "The current message (in MH, not in MH-E) is marked by this character.
407 See also `mh-scan-cur-msg-number-regexp'.")
409 (defvar mh-note-copied ?C
410 "Messages that have been copied are marked by this character.")
412 (defvar mh-note-deleted ?D
413 "Messages that have been deleted are marked by this character.
414 See also `mh-scan-deleted-msg-regexp'.")
416 (defvar mh-note-dist ?R
417 "Messages that have been redistributed are marked by this character.")
419 (defvar mh-note-forw ?F
420 "Messages that have been forwarded are marked by this character.")
422 (defvar mh-note-printed ?P
423 "Messages that have been printed are marked by this character.")
425 (defvar mh-note-refiled ?^
426 "Messages that have been refiled are marked by this character.
427 See also `mh-scan-refiled-msg-regexp'.")
429 (defvar mh-note-repl ?-
430 "Messages that have been replied to are marked by this character.")
432 (defvar mh-note-seq ?%
433 "Messages in a user-defined sequence are marked by this character.
435 Messages in the \"search\" sequence are marked by this character as
436 well.")
438 (defvar mh-note-whitelisted ?W
439 "Messages that have been whitelisted are marked by this character.
440 See also `mh-scan-whitelisted-msg-regexp'.")
444 ;;; Utilities
446 ;;;###mh-autoload
447 (defun mh-scan-msg-number-regexp ()
448 "Return value of variable `mh-scan-msg-number-regexp'."
449 mh-scan-msg-number-regexp)
451 ;;;###mh-autoload
452 (defun mh-scan-msg-search-regexp ()
453 "Return value of variable `mh-scan-msg-search-regexp'."
454 mh-scan-msg-search-regexp)
456 ;;;###mh-autoload
457 (defun mh-set-cmd-note (column)
458 "Set `mh-cmd-note' to COLUMN.
459 Note that columns in Emacs start with 0."
460 (setq mh-cmd-note column))
462 ;;;###mh-autoload
463 (defun mh-scan-format ()
464 "Return the output format argument for the scan program."
465 (if (equal mh-scan-format-file t)
466 (list "-format" (if (mh-variant-p 'nmh 'gnu-mh)
467 (list (mh-update-scan-format
468 mh-scan-format-nmh mh-cmd-note))
469 (list (mh-update-scan-format
470 mh-scan-format-mh mh-cmd-note))))
471 (if (not (equal mh-scan-format-file nil))
472 (list "-form" mh-scan-format-file))))
474 (defun mh-update-scan-format (fmt width)
475 "Return a scan format with the (msg) width in the FMT replaced with WIDTH.
477 The message number width portion of the format is discovered
478 using `mh-scan-msg-format-regexp'. Its replacement is controlled
479 with `mh-scan-msg-format-string'."
480 (or (and
481 (string-match mh-scan-msg-format-regexp fmt)
482 (let ((begin (match-beginning 1))
483 (end (match-end 1)))
484 (concat (substring fmt 0 begin)
485 (format mh-scan-msg-format-string width)
486 (substring fmt end))))
487 fmt))
489 ;;;###mh-autoload
490 (defun mh-msg-num-width (folder)
491 "Return the width of the largest message number in this FOLDER."
492 (or mh-progs (mh-find-path))
493 (let ((tmp-buffer (get-buffer-create mh-temp-buffer))
494 (width 0))
495 (with-current-buffer tmp-buffer
496 (erase-buffer)
497 (apply 'call-process
498 (expand-file-name mh-scan-prog mh-progs) nil '(t nil) nil
499 (list folder "last" "-format" "%(msg)"))
500 (goto-char (point-min))
501 (if (re-search-forward mh-scan-msg-number-regexp nil 0 1)
502 (setq width (length (buffer-substring
503 (match-beginning 1) (match-end 1))))))
504 width))
506 ;;;###mh-autoload
507 (defun mh-msg-num-width-to-column (width)
508 "Return the column for notations given message number WIDTH.
509 Note that columns in Emacs start with 0.
511 If `mh-scan-format-file' is set to \"Use MH-E scan Format\" this
512 means that either `mh-scan-format-mh' or `mh-scan-format-nmh' are
513 in use. This function therefore assumes that the first column is
514 empty (to provide room for the cursor), the following WIDTH
515 columns contain the message number, and the column for notations
516 comes after that."
517 (if (eq mh-scan-format-file t)
518 (max (1+ width) 2)
519 (error "%s %s" "Can't call `mh-msg-num-width-to-column' when"
520 "`mh-scan-format-file' is not set to \"Use MH-E scan Format\"")))
522 (provide 'mh-scan)
524 ;; Local Variables:
525 ;; indent-tabs-mode: nil
526 ;; sentence-end-double-space: nil
527 ;; End:
529 ;;; mh-scan.el ends here