]> code.delx.au - gnu-emacs/blob - lisp/mh-e/mh-scan.el
Merge from trunk.
[gnu-emacs] / lisp / mh-e / mh-scan.el
1 ;;; mh-scan.el --- MH-E scan line constants and utilities
2
3 ;; Copyright (C) 1993, 1995, 1997, 2000-2012 Free Software Foundation, Inc.
4
5 ;; Author: Bill Wohler <wohler@newt.com>
6 ;; Maintainer: Bill Wohler <wohler@newt.com>
7 ;; Keywords: mail
8 ;; See: mh-e.el
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24
25 ;;; Commentary:
26
27 ;; This file contains constants and a few functions for interpreting
28 ;; scan lines.
29
30 ;;; Change Log:
31
32 ;;; Code:
33
34 (require 'mh-e)
35
36 \f
37
38 ;;; Scan Formats
39
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.
45
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).
65
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.")
71
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).
91
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.")
97
98 \f
99
100 ;;; Regular Expressions
101
102 ;; Alphabetical.
103
104 (defvar mh-scan-body-regexp "\\(<<\\([^\n]+\\)?\\)"
105 "This regular expression matches the message body fragment.
106
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'.")
113
114 (defvar mh-scan-blacklisted-msg-regexp "^\\( *[0-9]+\\)B"
115 "This regular expression matches blacklisted (spam) messages.
116
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
121
122 \"^\\\\( *[0-9]+\\\\)B\".
123
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'.")
129
130 (defvar mh-scan-cur-msg-number-regexp "^\\( *[0-9]+\\+\\).*"
131 "This regular expression matches the current message.
132
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
137
138 \"^\\\\( *[0-9]+\\\\+\\\\).*\".
139
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'.")
146
147 (defvar mh-scan-date-regexp "\\([0-9][0-9]/[0-9][0-9]\\)"
148 "This regular expression matches a valid date.
149
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'.")
157
158 (defvar mh-scan-deleted-msg-regexp "^\\( *[0-9]+\\)D"
159 "This regular expression matches deleted messages.
160
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
165
166 \"^\\\\( *[0-9]+\\\\)D\".
167
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'.")
173
174 (defvar mh-scan-good-msg-regexp "^\\( *[0-9]+\\)[^^DBW0-9]"
175 "This regular expression matches \"good\" messages.
176
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
181
182 \"^\\\\( *[0-9]+\\\\)[^^DBW0-9]\".
183
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.")
189
190 (defvar mh-scan-msg-format-regexp "%\\([0-9]*\\)(msg)"
191 "This regular expression finds the message number width in a scan format.
192
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\".")
197
198 (defvar mh-scan-msg-format-string "%d"
199 "This is a format string for width of the message number in a scan format.
200
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\".")
204
205 (defvar mh-scan-msg-number-regexp "^ *\\([0-9]+\\)"
206 "This regular expression extracts the message number.
207
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]+\\\\)\".")
211
212 (defvar mh-scan-msg-overflow-regexp "^[?0-9][0-9]"
213 "This regular expression matches overflowed message numbers.")
214
215 (defvar mh-scan-msg-search-regexp "^[^0-9]*%d[^0-9]"
216 "This regular expression matches a particular message.
217
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]\".")
221
222 (defvar mh-scan-rcpt-regexp "\\(To:\\)\\(..............\\)"
223 "This regular expression specifies the recipient in messages you sent.
224
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'")
234
235 (defvar mh-scan-refiled-msg-regexp "^\\( *[0-9]+\\)\\^"
236 "This regular expression matches refiled messages.
237
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
242
243 \"^\\\\( *[0-9]+\\\\)\\\\^\".
244
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'.")
250
251 (defvar mh-scan-sent-to-me-sender-regexp
252 "^ *[0-9]+.\\([bct]\\).....[ ]*\\(..................\\)"
253 "This regular expression matches messages sent to us.
254
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
260
261 ^ *[0-9]+.\\\\([bct]\\\\).....[ ]*\\\\(..................\\\\)
262
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'.")
267
268 (defvar mh-scan-subject-regexp
269 "^ *[0-9]+........[ ]*...................\\([Rr][Ee]\\(\\[[0-9]+\\]\\)?:\\s-*\\)*\\([^<\n]*\\)"
270 "This regular expression matches the subject.
271
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
282
283 ^ *[0-9]+........[ ]*...................
284 \\\\([Rr][Ee]\\\\(\\\\\\=[[0-9]+\\\\]\\\\)?:\\\\s-*\\\\)*
285 \\\\([^<\\n]*\\\\)
286
287 This regular expression should be correct as it is needed by
288 non-fontification functions.")
289
290 (defvar mh-scan-valid-regexp "^ *[0-9]"
291 "This regular expression describes a valid scan line.
292
293 This is used to eliminate error messages that are occasionally
294 produced by \"inc\".")
295
296 (defvar mh-scan-whitelisted-msg-regexp "^\\( *[0-9]+\\)W"
297 "This regular expression matches whitelisted (non-spam) messages.
298
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
303
304 \"^\\\\( *[0-9]+\\\\)W\".
305
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'.")
311
312 \f
313
314 ;;; Widths, Offsets and Columns
315
316 (defvar mh-cmd-note 4
317 "Column for notations.
318
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.
322
323 Note that columns in Emacs start with 0.")
324 (make-variable-buffer-local 'mh-cmd-note)
325
326 (defvar mh-scan-cmd-note-width 1
327 "Number of columns consumed by the cmd-note field in `mh-scan-format'.
328
329 This column will have one of the values: \" \", \"^\", \"D\", \"B\", \"W\", \"+\", where
330
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.")
337
338 (defvar mh-scan-destination-width 1
339 "Number of columns consumed by the destination field in `mh-scan-format'.
340
341 This column will have one of \" \", \"%\", \"-\", \"t\", \"c\", \"b\", or \"n\"
342 in it.
343
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.")
351
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.")
355
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.")
360
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.")
365
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.")
369
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.")
373
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.")
380
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.")
384
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.")
393
394 \f
395
396 ;;; Notation
397
398 ;; Alphabetical.
399
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'.")
403
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'.")
407
408 (defvar mh-note-copied ?C
409 "Messages that have been copied are marked by this character.")
410
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'.")
414
415 (defvar mh-note-dist ?R
416 "Messages that have been redistributed are marked by this character.")
417
418 (defvar mh-note-forw ?F
419 "Messages that have been forwarded are marked by this character.")
420
421 (defvar mh-note-printed ?P
422 "Messages that have been printed are marked by this character.")
423
424 (defvar mh-note-refiled ?^
425 "Messages that have been refiled are marked by this character.
426 See also `mh-scan-refiled-msg-regexp'.")
427
428 (defvar mh-note-repl ?-
429 "Messages that have been replied to are marked by this character.")
430
431 (defvar mh-note-seq ?%
432 "Messages in a user-defined sequence are marked by this character.
433
434 Messages in the \"search\" sequence are marked by this character as
435 well.")
436
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'.")
440
441 \f
442
443 ;;; Utilities
444
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)
449
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)
454
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))
460
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))))
472
473 (defun mh-update-scan-format (fmt width)
474 "Return a scan format with the (msg) width in the FMT replaced with WIDTH.
475
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))
487
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))
504
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.
509
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\"")))
520
521 (provide 'mh-scan)
522
523 ;; Local Variables:
524 ;; indent-tabs-mode: nil
525 ;; sentence-end-double-space: nil
526 ;; End:
527
528 ;;; mh-scan.el ends here