]> code.delx.au - gnu-emacs/blob - lisp/mh-e/mh-scan.el
Ibuffer: Mark buffers by content
[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-2016 Free Software Foundation,
4 ;; Inc.
5
6 ;; Author: Bill Wohler <wohler@newt.com>
7 ;; Maintainer: Bill Wohler <wohler@newt.com>
8 ;; Keywords: mail
9 ;; See: mh-e.el
10
11 ;; This file is part of GNU Emacs.
12
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.
17
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.
22
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/>.
25
26 ;;; Commentary:
27
28 ;; This file contains constants and a few functions for interpreting
29 ;; scan lines.
30
31 ;;; Change Log:
32
33 ;;; Code:
34
35 (require 'mh-e)
36
37 \f
38
39 ;;; Scan Formats
40
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.
46
47 (defcustom 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).
66
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."
72 :group 'mh-scan-line-formats
73 :type 'string)
74
75 (defcustom mh-scan-format-nmh
76 (concat
77 "%4(msg)"
78 "%<(cur)+%| %>"
79 "%<{replied}-"
80 "%?(nonnull(comp{to}))%<(mymbox{to})t%>"
81 "%?(nonnull(comp{cc}))%<(mymbox{cc})c%>"
82 "%?(nonnull(comp{bcc}))%<(mymbox{bcc})b%>"
83 "%?(nonnull(comp{newsgroups}))n%>"
84 "%<(zero) %>"
85 "%02(mon{date})/%02(mday{date})%<{date} %|*%>"
86 "%<(mymbox{from})%<{to}To:%14(decode(friendly{to}))%>%>"
87 "%<(zero)%17(decode(friendly{from}))%> "
88 "%(decode{subject})%<{body}<<%{body}%>")
89 "Scan format string for nmh.
90 This string is passed to the scan program via the -format arg.
91 This format is identical to the default except that additional
92 hints for fontification have been added to the fifth
93 column (remember that in Emacs, the first column is 0).
94
95 The values of the fifth column, in priority order, are: \"-\" if
96 the message has been replied to, t if an address on the To: field
97 matches one of the mailboxes of the current user, \"c\" if the Cc:
98 field matches, \"b\" if the Bcc: field matches, and \"n\" if a
99 non-empty Newsgroups: field is present."
100 :group 'mh-scan-line-formats
101 :type 'string)
102
103 \f
104
105 ;;; Regular Expressions
106
107 ;; Alphabetical.
108
109 (defvar mh-scan-body-regexp "\\(<<\\([^\n]+\\)?\\)"
110 "This regular expression matches the message body fragment.
111
112 Note that the default setting of `mh-folder-font-lock-keywords'
113 expects this expression to contain at least one parenthesized
114 expression which matches the body text as in the default of
115 \"\\\\(<<\\\\([^\\n]+\\\\)?\\\\)\". If this regular expression is
116 not correct, the body fragment will not be highlighted with the
117 face `mh-folder-body'.")
118
119 (defvar mh-scan-blacklisted-msg-regexp "^\\( *[0-9]+\\)B"
120 "This regular expression matches blacklisted (spam) messages.
121
122 It must match from the beginning of the line. Note that the
123 default setting of `mh-folder-font-lock-keywords' expects this
124 expression to contain at least one parenthesized expression which
125 matches the message number as in the default of
126
127 \"^\\\\( *[0-9]+\\\\)B\".
128
129 This expression includes the leading space within parenthesis
130 since it looks better to highlight it as well. The highlighting
131 is done with the face `mh-folder-blacklisted'. This regular
132 expression should be correct as it is needed by non-fontification
133 functions. See also `mh-note-blacklisted'.")
134
135 (defvar mh-scan-cur-msg-number-regexp "^\\( *[0-9]+\\+\\).*"
136 "This regular expression matches the current message.
137
138 It must match from the beginning of the line. Note that the
139 default setting of `mh-folder-font-lock-keywords' expects this
140 expression to contain at least one parenthesized expression which
141 matches the message number as in the default of
142
143 \"^\\\\( *[0-9]+\\\\+\\\\).*\".
144
145 This expression includes the leading space and current message
146 marker \"+\" within the parenthesis since it looks better to
147 highlight these items as well. The highlighting is done with the
148 face `mh-folder-cur-msg-number'. This regular expression should
149 be correct as it is needed by non-fontification functions. See
150 also `mh-note-cur'.")
151
152 (defvar mh-scan-date-regexp "\\([0-9][0-9]/[0-9][0-9]\\)"
153 "This regular expression matches a valid date.
154
155 It must not be anchored to the beginning or the end of the line.
156 Note that the default setting of `mh-folder-font-lock-keywords'
157 expects this expression to contain only one parenthesized
158 expression which matches the date field as in the default of
159 \"\\\\([0-9][0-9]/[0-9][0-9]\\\\)\"}. If this regular expression
160 is not correct, the date will not be highlighted with the face
161 `mh-folder-date'.")
162
163 (defvar mh-scan-deleted-msg-regexp "^\\( *[0-9]+\\)D"
164 "This regular expression matches deleted messages.
165
166 It must match from the beginning of the line. Note that the
167 default setting of `mh-folder-font-lock-keywords' expects this
168 expression to contain at least one parenthesized expression which
169 matches the message number as in the default of
170
171 \"^\\\\( *[0-9]+\\\\)D\".
172
173 This expression includes the leading space within the parenthesis
174 since it looks better to highlight it as well. The highlighting
175 is done with the face `mh-folder-deleted'. This regular
176 expression should be correct as it is needed by non-fontification
177 functions. See also `mh-note-deleted'.")
178
179 (defvar mh-scan-good-msg-regexp "^\\( *[0-9]+\\)[^^DBW0-9]"
180 "This regular expression matches \"good\" messages.
181
182 It must match from the beginning of the line. Note that the
183 default setting of `mh-folder-font-lock-keywords' expects this
184 expression to contain at least one parenthesized expression which
185 matches the message number as in the default of
186
187 \"^\\\\( *[0-9]+\\\\)[^^DBW0-9]\".
188
189 This expression includes the leading space within the parenthesis
190 since it looks better to highlight it as well. The highlighting
191 is done with the face `mh-folder-msg-number'. This regular
192 expression should be correct as it is needed by non-fontification
193 functions.")
194
195 (defvar mh-scan-msg-format-regexp "%\\([0-9]*\\)(msg)"
196 "This regular expression finds the message number width in a scan format.
197
198 Note that the message number must be placed in a parenthesized
199 expression as in the default of \"%\\\\([0-9]*\\\\)(msg)\". This
200 variable is only consulted if `mh-scan-format-file' is set to
201 \"Use MH-E scan Format\".")
202
203 (defvar mh-scan-msg-format-string "%d"
204 "This is a format string for width of the message number in a scan format.
205
206 Use \"0%d\" for zero-filled message numbers. This variable is only
207 consulted if `mh-scan-format-file' is set to \"Use MH-E scan
208 Format\".")
209
210 (defvar mh-scan-msg-number-regexp "^ *\\([0-9]+\\)"
211 "This regular expression extracts the message number.
212
213 It must match from the beginning of the line. Note that the
214 message number must be placed in a parenthesized expression as in
215 the default of \"^ *\\\\([0-9]+\\\\)\".")
216
217 (defvar mh-scan-msg-overflow-regexp "^[?0-9][0-9]"
218 "This regular expression matches overflowed message numbers.")
219
220 (defvar mh-scan-msg-search-regexp "^[^0-9]*%d[^0-9]"
221 "This regular expression matches a particular message.
222
223 It is a format string; use \"%d\" to represent the location of the
224 message number within the expression as in the default of
225 \"^[^0-9]*%d[^0-9]\".")
226
227 (defvar mh-scan-rcpt-regexp "\\(To:\\)\\(..............\\)"
228 "This regular expression specifies the recipient in messages you sent.
229
230 Note that the default setting of `mh-folder-font-lock-keywords'
231 expects this expression to contain two parenthesized expressions.
232 The first is expected to match the \"To:\" that the default scan
233 format file generates. The second is expected to match the
234 recipient's name as in the default of
235 \"\\\\(To:\\\\)\\\\(..............\\\\)\". If this regular
236 expression is not correct, the \"To:\" string will not be
237 highlighted with the face `mh-folder-to' and the recipient will
238 not be highlighted with the face `mh-folder-address'")
239
240 (defvar mh-scan-refiled-msg-regexp "^\\( *[0-9]+\\)\\^"
241 "This regular expression matches refiled messages.
242
243 It must match from the beginning of the line. Note that the
244 default setting of `mh-folder-font-lock-keywords' expects this
245 expression to contain at least one parenthesized expression which
246 matches the message number as in the default of
247
248 \"^\\\\( *[0-9]+\\\\)\\\\^\".
249
250 This expression includes the leading space within the parenthesis
251 since it looks better to highlight it as well. The highlighting
252 is done with the face `mh-folder-refiled'. This regular
253 expression should be correct as it is needed by non-fontification
254 functions. See also `mh-note-refiled'.")
255
256 (defvar mh-scan-sent-to-me-sender-regexp
257 "^ *[0-9]+.\\([bct]\\).....[ ]*\\(..................\\)"
258 "This regular expression matches messages sent to us.
259
260 Note that the default setting of `mh-folder-font-lock-keywords'
261 expects this expression to contain at least two parenthesized
262 expressions. The first should match the fontification hint (see
263 `mh-scan-format-nmh') and the second should match the user name
264 as in the default of
265
266 ^ *[0-9]+.\\\\([bct]\\\\).....[ ]*\\\\(..................\\\\)
267
268 If this regular expression is not correct, the notation hints
269 will not be highlighted with the face
270 `mh-mh-folder-sent-to-me-hint' and the sender will not be
271 highlighted with the face `mh-folder-sent-to-me-sender'.")
272
273 (defvar mh-scan-subject-regexp
274 "^ *[0-9]+........[ ]*...................\\([Rr][Ee]\\(\\[[0-9]+\\]\\)?:\\s-*\\)*\\([^<\n]*\\)"
275 "This regular expression matches the subject.
276
277 It must match from the beginning of the line. Note that the
278 default setting of `mh-folder-font-lock-keywords' expects this
279 expression to contain at least three parenthesized expressions.
280 The first is expected to match the \"Re:\" string, if any, and is
281 highlighted with the face `mh-folder-followup'. The second
282 matches an optional bracketed number after \"Re:\", such as in
283 \"Re[2]:\" (and is thus a sub-expression of the first expression)
284 and the third is expected to match the subject line itself which
285 is highlighted with the face `mh-folder-subject'. For example,
286 the default (broken on multiple lines for readability) is
287
288 ^ *[0-9]+........[ ]*...................
289 \\\\([Rr][Ee]\\\\(\\\\\\=[[0-9]+\\\\]\\\\)?:\\\\s-*\\\\)*
290 \\\\([^<\\n]*\\\\)
291
292 This regular expression should be correct as it is needed by
293 non-fontification functions.")
294
295 (defvar mh-scan-valid-regexp "^ *[0-9]"
296 "This regular expression describes a valid scan line.
297
298 This is used to eliminate error messages that are occasionally
299 produced by \"inc\".")
300
301 (defvar mh-scan-whitelisted-msg-regexp "^\\( *[0-9]+\\)W"
302 "This regular expression matches whitelisted (non-spam) messages.
303
304 It must match from the beginning of the line. Note that the
305 default setting of `mh-folder-font-lock-keywords' expects this
306 expression to contain at least one parenthesized expression which
307 matches the message number as in the default of
308
309 \"^\\\\( *[0-9]+\\\\)W\".
310
311 This expression includes the leading space within parenthesis
312 since it looks better to highlight it as well. The highlighting
313 is done with the face `mh-folder-whitelisted'. This regular
314 expression should be correct as it is needed by non-fontification
315 functions. See also `mh-note-whitelisted'.")
316
317 \f
318
319 ;;; Widths, Offsets and Columns
320
321 (defvar mh-cmd-note 4
322 "Column for notations.
323
324 This variable should be set with the function `mh-set-cmd-note'.
325 This variable may be updated dynamically if
326 `mh-adaptive-cmd-note-flag' is on.
327
328 Note that columns in Emacs start with 0.")
329 (make-variable-buffer-local 'mh-cmd-note)
330
331 (defvar mh-scan-cmd-note-width 1
332 "Number of columns consumed by the cmd-note field in `mh-scan-format'.
333
334 This column will have one of the values: \" \", \"^\", \"D\", \"B\", \"W\", \"+\", where
335
336 \" \" is the default value,
337 \"^\" is the `mh-note-refiled' character,
338 \"D\" is the `mh-note-deleted' character,
339 \"B\" is the `mh-note-blacklisted' character,
340 \"W\" is the `mh-note-whitelisted' character, and
341 \"+\" is the `mh-note-cur' character.")
342
343 (defvar mh-scan-destination-width 1
344 "Number of columns consumed by the destination field in `mh-scan-format'.
345
346 This column will have one of \" \", \"%\", \"-\", \"t\", \"c\", \"b\", or \"n\"
347 in it.
348
349 \" \" blank space is the default character.
350 \"%\" indicates that the message in a named MH sequence.
351 \"-\" indicates that the message has been annotated with a replied field.
352 \"t\" indicates that the message contains mymbox in the To: field.
353 \"c\" indicates that the message contains mymbox in the Cc: field.
354 \"b\" indicates that the message contains mymbox in the Bcc: field.
355 \"n\" indicates that the message contains a Newsgroups: field.")
356
357 (defvar mh-scan-date-width 5
358 "Number of columns consumed by the date field in `mh-scan-format'.
359 This column will typically be of the form mm/dd.")
360
361 (defvar mh-scan-date-flag-width 1
362 "Number of columns consumed to flag (in)valid dates in `mh-scan-format'.
363 This column will have \" \" for valid and \"*\" for invalid or
364 missing dates.")
365
366 (defvar mh-scan-from-mbox-width 17
367 "Number of columns consumed with the \"From:\" line in `mh-scan-format'.
368 This column will have a friendly name or e-mail address of the
369 originator, or a \"To: address\" for outgoing e-mail messages.")
370
371 (defvar mh-scan-from-mbox-sep-width 2
372 "Number of columns consumed by whitespace after from-mbox in `mh-scan-format'.
373 This column will only ever have spaces in it.")
374
375 (defvar mh-scan-field-destination-offset
376 (+ mh-scan-cmd-note-width)
377 "The offset from the `mh-cmd-note' for the destination column.")
378
379 (defvar mh-scan-field-from-start-offset
380 (+ mh-scan-cmd-note-width
381 mh-scan-destination-width
382 mh-scan-date-width
383 mh-scan-date-flag-width)
384 "The offset from the `mh-cmd-note' to find the start of \"From:\" address.")
385
386 (defvar mh-scan-field-from-end-offset
387 (+ mh-scan-field-from-start-offset mh-scan-from-mbox-width)
388 "The offset from the `mh-cmd-note' to find the end of \"From:\" address.")
389
390 (defvar mh-scan-field-subject-start-offset
391 (+ mh-scan-cmd-note-width
392 mh-scan-destination-width
393 mh-scan-date-width
394 mh-scan-date-flag-width
395 mh-scan-from-mbox-width
396 mh-scan-from-mbox-sep-width)
397 "The offset from the `mh-cmd-note' to find the start of the subject.")
398
399 \f
400
401 ;;; Notation
402
403 ;; Alphabetical.
404
405 (defvar mh-note-blacklisted ?B
406 "Messages that have been blacklisted are marked by this character.
407 See also `mh-scan-blacklisted-msg-regexp'.")
408
409 (defvar mh-note-cur ?+
410 "The current message (in MH, not in MH-E) is marked by this character.
411 See also `mh-scan-cur-msg-number-regexp'.")
412
413 (defvar mh-note-copied ?C
414 "Messages that have been copied are marked by this character.")
415
416 (defvar mh-note-deleted ?D
417 "Messages that have been deleted are marked by this character.
418 See also `mh-scan-deleted-msg-regexp'.")
419
420 (defvar mh-note-dist ?R
421 "Messages that have been redistributed are marked by this character.")
422
423 (defvar mh-note-forw ?F
424 "Messages that have been forwarded are marked by this character.")
425
426 (defvar mh-note-printed ?P
427 "Messages that have been printed are marked by this character.")
428
429 (defvar mh-note-refiled ?^
430 "Messages that have been refiled are marked by this character.
431 See also `mh-scan-refiled-msg-regexp'.")
432
433 (defvar mh-note-repl ?-
434 "Messages that have been replied to are marked by this character.")
435
436 (defvar mh-note-seq ?%
437 "Messages in a user-defined sequence are marked by this character.
438
439 Messages in the \"search\" sequence are marked by this character as
440 well.")
441
442 (defvar mh-note-whitelisted ?W
443 "Messages that have been whitelisted are marked by this character.
444 See also `mh-scan-whitelisted-msg-regexp'.")
445
446 \f
447
448 ;;; Utilities
449
450 ;;;###mh-autoload
451 (defun mh-scan-msg-number-regexp ()
452 "Return value of variable `mh-scan-msg-number-regexp'."
453 mh-scan-msg-number-regexp)
454
455 ;;;###mh-autoload
456 (defun mh-scan-msg-search-regexp ()
457 "Return value of variable `mh-scan-msg-search-regexp'."
458 mh-scan-msg-search-regexp)
459
460 ;;;###mh-autoload
461 (defun mh-set-cmd-note (column)
462 "Set `mh-cmd-note' to COLUMN.
463 Note that columns in Emacs start with 0."
464 (setq mh-cmd-note column))
465
466 ;;;###mh-autoload
467 (defun mh-scan-format ()
468 "Return the output format argument for the scan program."
469 (if (equal mh-scan-format-file t)
470 (list "-format" (if (mh-variant-p 'nmh 'gnu-mh)
471 (list (mh-update-scan-format
472 mh-scan-format-nmh mh-cmd-note))
473 (list (mh-update-scan-format
474 mh-scan-format-mh mh-cmd-note))))
475 (if (not (equal mh-scan-format-file nil))
476 (list "-form" mh-scan-format-file))))
477
478 (defun mh-update-scan-format (fmt width)
479 "Return a scan format with the (msg) width in the FMT replaced with WIDTH.
480
481 The message number width portion of the format is discovered
482 using `mh-scan-msg-format-regexp'. Its replacement is controlled
483 with `mh-scan-msg-format-string'."
484 (or (and
485 (string-match mh-scan-msg-format-regexp fmt)
486 (let ((begin (match-beginning 1))
487 (end (match-end 1)))
488 (concat (substring fmt 0 begin)
489 (format mh-scan-msg-format-string width)
490 (substring fmt end))))
491 fmt))
492
493 ;;;###mh-autoload
494 (defun mh-msg-num-width (folder)
495 "Return the width of the largest message number in this FOLDER."
496 (or mh-progs (mh-find-path))
497 (let ((tmp-buffer (get-buffer-create mh-temp-buffer))
498 (width 0))
499 (with-current-buffer tmp-buffer
500 (erase-buffer)
501 (apply 'call-process
502 (expand-file-name mh-scan-prog mh-progs) nil '(t nil) nil
503 (list folder "last" "-format" "%(msg)"))
504 (goto-char (point-min))
505 (if (re-search-forward mh-scan-msg-number-regexp nil 0 1)
506 (setq width (length (buffer-substring
507 (match-beginning 1) (match-end 1))))))
508 width))
509
510 ;;;###mh-autoload
511 (defun mh-msg-num-width-to-column (width)
512 "Return the column for notations given message number WIDTH.
513 Note that columns in Emacs start with 0.
514
515 If `mh-scan-format-file' is set to \"Use MH-E scan Format\" this
516 means that either `mh-scan-format-mh' or `mh-scan-format-nmh' are
517 in use. This function therefore assumes that the first column is
518 empty (to provide room for the cursor), the following WIDTH
519 columns contain the message number, and the column for notations
520 comes after that."
521 (if (eq mh-scan-format-file t)
522 (max (1+ width) 2)
523 (error "%s %s" "Can't call `mh-msg-num-width-to-column' when"
524 "`mh-scan-format-file' is not set to \"Use MH-E scan Format\"")))
525
526 (provide 'mh-scan)
527
528 ;; Local Variables:
529 ;; indent-tabs-mode: nil
530 ;; sentence-end-double-space: nil
531 ;; End:
532
533 ;;; mh-scan.el ends here