Add isearch-yank-symbol-or-char
[emacs.git] / lisp / mh-e / mh-scan.el
blobf3ad7c68bad08a98ace02d772ee52184469c0fcc
1 ;;; mh-scan.el --- MH-E scan line constants and utilities
3 ;; Copyright (C) 1993, 1995, 1997, 2000-2018 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 <https://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 (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).
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)
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).
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)
105 ;;; Regular Expressions
107 ;; Alphabetical.
109 (defvar mh-scan-body-regexp "\\(<<\\([^\n]+\\)?\\)"
110 "This regular expression matches the message body fragment.
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'.")
119 (defvar mh-scan-blacklisted-msg-regexp "^\\( *[0-9]+\\)B"
120 "This regular expression matches blacklisted (spam) messages.
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
127 \"^\\\\( *[0-9]+\\\\)B\".
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'.")
135 (defvar mh-scan-cur-msg-number-regexp "^\\( *[0-9]+\\+\\).*"
136 "This regular expression matches the current message.
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
143 \"^\\\\( *[0-9]+\\\\+\\\\).*\".
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'.")
152 (defvar mh-scan-date-regexp "\\([0-9][0-9]/[0-9][0-9]\\)"
153 "This regular expression matches a valid date.
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'.")
163 (defvar mh-scan-deleted-msg-regexp "^\\( *[0-9]+\\)D"
164 "This regular expression matches deleted messages.
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
171 \"^\\\\( *[0-9]+\\\\)D\".
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'.")
179 (defvar mh-scan-good-msg-regexp "^\\( *[0-9]+\\)[^^DBW0-9]"
180 "This regular expression matches \"good\" messages.
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
187 \"^\\\\( *[0-9]+\\\\)[^^DBW0-9]\".
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.")
195 (defvar mh-scan-msg-format-regexp "%\\([0-9]*\\)(msg)"
196 "This regular expression finds the message number width in a scan format.
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\".")
203 (defvar mh-scan-msg-format-string "%d"
204 "This is a format string for width of the message number in a scan format.
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\".")
210 (defvar mh-scan-msg-number-regexp "^ *\\([0-9]+\\)"
211 "This regular expression extracts the message number.
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]+\\\\)\".")
217 (defvar mh-scan-msg-overflow-regexp "^[?0-9][0-9]"
218 "This regular expression matches overflowed message numbers.")
220 (defvar mh-scan-msg-search-regexp "^[^0-9]*%d[^0-9]"
221 "This regular expression matches a particular message.
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]\".")
227 (defvar mh-scan-rcpt-regexp "\\(To:\\)\\(..............\\)"
228 "This regular expression specifies the recipient in messages you sent.
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'")
240 (defvar mh-scan-refiled-msg-regexp "^\\( *[0-9]+\\)\\^"
241 "This regular expression matches refiled messages.
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
248 \"^\\\\( *[0-9]+\\\\)\\\\^\".
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'.")
256 (defvar mh-scan-sent-to-me-sender-regexp
257 "^ *[0-9]+.\\([bct]\\).....[ ]*\\(..................\\)"
258 "This regular expression matches messages sent to us.
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
266 ^ *[0-9]+.\\\\([bct]\\\\).....[ ]*\\\\(..................\\\\)
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'.")
273 (defvar mh-scan-subject-regexp
274 "^ *[0-9]+........[ ]*...................\\([Rr][Ee]\\(\\[[0-9]+\\]\\)?:\\s-*\\)*\\([^<\n]*\\)"
275 "This regular expression matches the subject.
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
288 ^ *[0-9]+........[ ]*...................
289 \\\\([Rr][Ee]\\\\(\\\\\\=[[0-9]+\\\\]\\\\)?:\\\\s-*\\\\)*
290 \\\\([^<\\n]*\\\\)
292 This regular expression should be correct as it is needed by
293 non-fontification functions.")
295 (defvar mh-scan-valid-regexp "^ *[0-9]"
296 "This regular expression describes a valid scan line.
298 This is used to eliminate error messages that are occasionally
299 produced by \"inc\".")
301 (defvar mh-scan-whitelisted-msg-regexp "^\\( *[0-9]+\\)W"
302 "This regular expression matches whitelisted (non-spam) messages.
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
309 \"^\\\\( *[0-9]+\\\\)W\".
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'.")
319 ;;; Widths, Offsets and Columns
321 (defvar mh-cmd-note 4
322 "Column for notations.
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.
328 Note that columns in Emacs start with 0.")
329 (make-variable-buffer-local 'mh-cmd-note)
331 (defvar mh-scan-cmd-note-width 1
332 "Number of columns consumed by the cmd-note field in `mh-scan-format'.
334 This column will have one of the values: \" \", \"^\", \"D\", \"B\", \"W\", \"+\", where
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.")
343 (defvar mh-scan-destination-width 1
344 "Number of columns consumed by the destination field in `mh-scan-format'.
346 This column will have one of \" \", \"%\", \"-\", \"t\", \"c\", \"b\", or \"n\"
347 in it.
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.")
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.")
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.")
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.")
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.")
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.")
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.")
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.")
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.")
401 ;;; Notation
403 ;; Alphabetical.
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'.")
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'.")
413 (defvar mh-note-copied ?C
414 "Messages that have been copied are marked by this character.")
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'.")
420 (defvar mh-note-dist ?R
421 "Messages that have been redistributed are marked by this character.")
423 (defvar mh-note-forw ?F
424 "Messages that have been forwarded are marked by this character.")
426 (defvar mh-note-printed ?P
427 "Messages that have been printed are marked by this character.")
429 (defvar mh-note-refiled ?^
430 "Messages that have been refiled are marked by this character.
431 See also `mh-scan-refiled-msg-regexp'.")
433 (defvar mh-note-repl ?-
434 "Messages that have been replied to are marked by this character.")
436 (defvar mh-note-seq ?%
437 "Messages in a user-defined sequence are marked by this character.
439 Messages in the \"search\" sequence are marked by this character as
440 well.")
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'.")
448 ;;; Utilities
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)
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)
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))
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))))
478 (defun mh-update-scan-format (fmt width)
479 "Return a scan format with the (msg) width in the FMT replaced with WIDTH.
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))
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))
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.
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\"")))
526 (provide 'mh-scan)
528 ;; Local Variables:
529 ;; indent-tabs-mode: nil
530 ;; sentence-end-double-space: nil
531 ;; End:
533 ;;; mh-scan.el ends here