mh-scan.el 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. ;;; mh-scan.el --- MH-E scan line constants and utilities
  2. ;; Copyright (C) 1993, 1995, 1997, 2000-2012 Free Software Foundation, Inc.
  3. ;; Author: Bill Wohler <wohler@newt.com>
  4. ;; Maintainer: Bill Wohler <wohler@newt.com>
  5. ;; Keywords: mail
  6. ;; See: mh-e.el
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; This file contains constants and a few functions for interpreting
  20. ;; scan lines.
  21. ;;; Change Log:
  22. ;;; Code:
  23. (require 'mh-e)
  24. ;;; Scan Formats
  25. ;; The following scan formats are passed to the scan program if the setting of
  26. ;; `mh-scan-format-file' is t. They are identical except the later one makes
  27. ;; use of the nmh `decode' function to decode RFC 2047 encodings. If you just
  28. ;; want to change the column of the notations, use the `mh-set-cmd-note'
  29. ;; function.
  30. (defvar mh-scan-format-mh
  31. (concat
  32. "%4(msg)"
  33. "%<(cur)+%| %>"
  34. "%<{replied}-"
  35. "%?(nonnull(comp{to}))%<(mymbox{to})t%>"
  36. "%?(nonnull(comp{cc}))%<(mymbox{cc})c%>"
  37. "%?(nonnull(comp{bcc}))%<(mymbox{bcc})b%>"
  38. "%?(nonnull(comp{newsgroups}))n%>"
  39. "%<(zero) %>"
  40. "%02(mon{date})/%02(mday{date})%<{date} %|*%>"
  41. "%<(mymbox{from})%<{to}To:%14(friendly{to})%>%>"
  42. "%<(zero)%17(friendly{from})%> "
  43. "%{subject}%<{body}<<%{body}%>")
  44. "*Scan format string for MH.
  45. This string is passed to the scan program via the -format
  46. argument. This format is identical to the default except that
  47. additional hints for fontification have been added to the fifth
  48. column (remember that in Emacs, the first column is 0).
  49. The values of the fifth column, in priority order, are: \"-\" if
  50. the message has been replied to, t if an address on the To: line
  51. matches one of the mailboxes of the current user, \"c\" if the Cc:
  52. line matches, \"b\" if the Bcc: line matches, and \"n\" if a
  53. non-empty Newsgroups: header is present.")
  54. (defvar mh-scan-format-nmh
  55. (concat
  56. "%4(msg)"
  57. "%<(cur)+%| %>"
  58. "%<{replied}-"
  59. "%?(nonnull(comp{to}))%<(mymbox{to})t%>"
  60. "%?(nonnull(comp{cc}))%<(mymbox{cc})c%>"
  61. "%?(nonnull(comp{bcc}))%<(mymbox{bcc})b%>"
  62. "%?(nonnull(comp{newsgroups}))n%>"
  63. "%<(zero) %>"
  64. "%02(mon{date})/%02(mday{date})%<{date} %|*%>"
  65. "%<(mymbox{from})%<{to}To:%14(decode(friendly{to}))%>%>"
  66. "%<(zero)%17(decode(friendly{from}))%> "
  67. "%(decode{subject})%<{body}<<%{body}%>")
  68. "*Scan format string for nmh.
  69. This string is passed to the scan program via the -format arg.
  70. This format is identical to the default except that additional
  71. hints for fontification have been added to the fifth
  72. column (remember that in Emacs, the first column is 0).
  73. The values of the fifth column, in priority order, are: \"-\" if
  74. the message has been replied to, t if an address on the To: field
  75. matches one of the mailboxes of the current user, \"c\" if the Cc:
  76. field matches, \"b\" if the Bcc: field matches, and \"n\" if a
  77. non-empty Newsgroups: field is present.")
  78. ;;; Regular Expressions
  79. ;; Alphabetical.
  80. (defvar mh-scan-body-regexp "\\(<<\\([^\n]+\\)?\\)"
  81. "This regular expression matches the message body fragment.
  82. Note that the default setting of `mh-folder-font-lock-keywords'
  83. expects this expression to contain at least one parenthesized
  84. expression which matches the body text as in the default of
  85. \"\\\\(<<\\\\([^\\n]+\\\\)?\\\\)\". If this regular expression is
  86. not correct, the body fragment will not be highlighted with the
  87. face `mh-folder-body'.")
  88. (defvar mh-scan-cur-msg-number-regexp "^\\( *[0-9]+\\+\\).*"
  89. "This regular expression matches the current message.
  90. It must match from the beginning of the line. Note that the
  91. default setting of `mh-folder-font-lock-keywords' expects this
  92. expression to contain at least one parenthesized expression which
  93. matches the message number as in the default of
  94. \"^\\\\( *[0-9]+\\\\+\\\\).*\".
  95. This expression includes the leading space and current message
  96. marker \"+\" within the parenthesis since it looks better to
  97. highlight these items as well. The highlighting is done with the
  98. face `mh-folder-cur-msg-number'. This regular expression should
  99. be correct as it is needed by non-fontification functions. See
  100. also `mh-note-cur'.")
  101. (defvar mh-scan-date-regexp "\\([0-9][0-9]/[0-9][0-9]\\)"
  102. "This regular expression matches a valid date.
  103. It must not be anchored to the beginning or the end of the line.
  104. Note that the default setting of `mh-folder-font-lock-keywords'
  105. expects this expression to contain only one parenthesized
  106. expression which matches the date field as in the default of
  107. \"\\\\([0-9][0-9]/[0-9][0-9]\\\\)\"}. If this regular expression
  108. is not correct, the date will not be highlighted with the face
  109. `mh-folder-date'.")
  110. (defvar mh-scan-deleted-msg-regexp "^\\( *[0-9]+\\)D"
  111. "This regular expression matches deleted messages.
  112. It must match from the beginning of the line. Note that the
  113. default setting of `mh-folder-font-lock-keywords' expects this
  114. expression to contain at least one parenthesized expression which
  115. matches the message number as in the default of
  116. \"^\\\\( *[0-9]+\\\\)D\".
  117. This expression includes the leading space within the parenthesis
  118. since it looks better to highlight it as well. The highlighting
  119. is done with the face `mh-folder-deleted'. This regular
  120. expression should be correct as it is needed by non-fontification
  121. functions. See also `mh-note-deleted'.")
  122. (defvar mh-scan-good-msg-regexp "^\\( *[0-9]+\\)[^D^0-9]"
  123. "This regular expression matches \"good\" messages.
  124. It must match from the beginning of the line. Note that the
  125. default setting of `mh-folder-font-lock-keywords' expects this
  126. expression to contain at least one parenthesized expression which
  127. matches the message number as in the default of
  128. \"^\\\\( *[0-9]+\\\\)[^D^0-9]\".
  129. This expression includes the leading space within the parenthesis
  130. since it looks better to highlight it as well. The highlighting
  131. is done with the face `mh-folder-msg-number'. This regular
  132. expression should be correct as it is needed by non-fontification
  133. functions.")
  134. (defvar mh-scan-msg-format-regexp "%\\([0-9]*\\)(msg)"
  135. "This regular expression finds the message number width in a scan format.
  136. Note that the message number must be placed in a parenthesized
  137. expression as in the default of \"%\\\\([0-9]*\\\\)(msg)\". This
  138. variable is only consulted if `mh-scan-format-file' is set to
  139. \"Use MH-E scan Format\".")
  140. (defvar mh-scan-msg-format-string "%d"
  141. "This is a format string for width of the message number in a scan format.
  142. Use \"0%d\" for zero-filled message numbers. This variable is only
  143. consulted if `mh-scan-format-file' is set to \"Use MH-E scan
  144. Format\".")
  145. (defvar mh-scan-msg-number-regexp "^ *\\([0-9]+\\)"
  146. "This regular expression extracts the message number.
  147. It must match from the beginning of the line. Note that the
  148. message number must be placed in a parenthesized expression as in
  149. the default of \"^ *\\\\([0-9]+\\\\)\".")
  150. (defvar mh-scan-msg-overflow-regexp "^[?0-9][0-9]"
  151. "This regular expression matches overflowed message numbers.")
  152. (defvar mh-scan-msg-search-regexp "^[^0-9]*%d[^0-9]"
  153. "This regular expression matches a particular message.
  154. It is a format string; use \"%d\" to represent the location of the
  155. message number within the expression as in the default of
  156. \"^[^0-9]*%d[^0-9]\".")
  157. (defvar mh-scan-rcpt-regexp "\\(To:\\)\\(..............\\)"
  158. "This regular expression specifies the recipient in messages you sent.
  159. Note that the default setting of `mh-folder-font-lock-keywords'
  160. expects this expression to contain two parenthesized expressions.
  161. The first is expected to match the \"To:\" that the default scan
  162. format file generates. The second is expected to match the
  163. recipient's name as in the default of
  164. \"\\\\(To:\\\\)\\\\(..............\\\\)\". If this regular
  165. expression is not correct, the \"To:\" string will not be
  166. highlighted with the face `mh-folder-to' and the recipient will
  167. not be highlighted with the face `mh-folder-address'")
  168. (defvar mh-scan-refiled-msg-regexp "^\\( *[0-9]+\\)\\^"
  169. "This regular expression matches refiled messages.
  170. It must match from the beginning of the line. Note that the
  171. default setting of `mh-folder-font-lock-keywords' expects this
  172. expression to contain at least one parenthesized expression which
  173. matches the message number as in the default of
  174. \"^\\\\( *[0-9]+\\\\)\\\\^\".
  175. This expression includes the leading space within the parenthesis
  176. since it looks better to highlight it as well. The highlighting
  177. is done with the face `mh-folder-refiled'. This regular
  178. expression should be correct as it is needed by non-fontification
  179. functions. See also `mh-note-refiled'.")
  180. (defvar mh-scan-sent-to-me-sender-regexp
  181. "^ *[0-9]+.\\([bct]\\).....[ ]*\\(..................\\)"
  182. "This regular expression matches messages sent to us.
  183. Note that the default setting of `mh-folder-font-lock-keywords'
  184. expects this expression to contain at least two parenthesized
  185. expressions. The first should match the fontification hint (see
  186. `mh-scan-format-nmh') and the second should match the user name
  187. as in the default of
  188. ^ *[0-9]+.\\\\([bct]\\\\).....[ ]*\\\\(..................\\\\)
  189. If this regular expression is not correct, the notation hints
  190. will not be highlighted with the face
  191. `mh-mh-folder-sent-to-me-hint' and the sender will not be
  192. highlighted with the face `mh-folder-sent-to-me-sender'.")
  193. (defvar mh-scan-subject-regexp
  194. "^ *[0-9]+........[ ]*...................\\([Rr][Ee]\\(\\[[0-9]+\\]\\)?:\\s-*\\)*\\([^<\n]*\\)"
  195. "This regular expression matches the subject.
  196. It must match from the beginning of the line. Note that the
  197. default setting of `mh-folder-font-lock-keywords' expects this
  198. expression to contain at least three parenthesized expressions.
  199. The first is expected to match the \"Re:\" string, if any, and is
  200. highlighted with the face `mh-folder-followup'. The second
  201. matches an optional bracketed number after \"Re:\", such as in
  202. \"Re[2]:\" (and is thus a sub-expression of the first expression)
  203. and the third is expected to match the subject line itself which
  204. is highlighted with the face `mh-folder-subject'. For example,
  205. the default (broken on multiple lines for readability) is
  206. ^ *[0-9]+........[ ]*...................
  207. \\\\([Rr][Ee]\\\\(\\\\\\=[[0-9]+\\\\]\\\\)?:\\\\s-*\\\\)*
  208. \\\\([^<\\n]*\\\\)
  209. This regular expression should be correct as it is needed by
  210. non-fontification functions.")
  211. (defvar mh-scan-valid-regexp "^ *[0-9]"
  212. "This regular expression describes a valid scan line.
  213. This is used to eliminate error messages that are occasionally
  214. produced by \"inc\".")
  215. ;;; Widths, Offsets and Columns
  216. (defvar mh-cmd-note 4
  217. "Column for notations.
  218. This variable should be set with the function `mh-set-cmd-note'.
  219. This variable may be updated dynamically if
  220. `mh-adaptive-cmd-note-flag' is on.
  221. Note that columns in Emacs start with 0.")
  222. (make-variable-buffer-local 'mh-cmd-note)
  223. (defvar mh-scan-cmd-note-width 1
  224. "Number of columns consumed by the cmd-note field in `mh-scan-format'.
  225. This column will have one of the values: \" \", \"D\", \"^\", \"+\", where
  226. \" \" is the default value,
  227. \"D\" is the `mh-note-deleted' character,
  228. \"^\" is the `mh-note-refiled' character, and
  229. \"+\" is the `mh-note-cur' character.")
  230. (defvar mh-scan-destination-width 1
  231. "Number of columns consumed by the destination field in `mh-scan-format'.
  232. This column will have one of \" \", \"%\", \"-\", \"t\", \"c\", \"b\", or \"n\"
  233. in it.
  234. \" \" blank space is the default character.
  235. \"%\" indicates that the message in a named MH sequence.
  236. \"-\" indicates that the message has been annotated with a replied field.
  237. \"t\" indicates that the message contains mymbox in the To: field.
  238. \"c\" indicates that the message contains mymbox in the Cc: field.
  239. \"b\" indicates that the message contains mymbox in the Bcc: field.
  240. \"n\" indicates that the message contains a Newsgroups: field.")
  241. (defvar mh-scan-date-width 5
  242. "Number of columns consumed by the date field in `mh-scan-format'.
  243. This column will typically be of the form mm/dd.")
  244. (defvar mh-scan-date-flag-width 1
  245. "Number of columns consumed to flag (in)valid dates in `mh-scan-format'.
  246. This column will have \" \" for valid and \"*\" for invalid or
  247. missing dates.")
  248. (defvar mh-scan-from-mbox-width 17
  249. "Number of columns consumed with the \"From:\" line in `mh-scan-format'.
  250. This column will have a friendly name or e-mail address of the
  251. originator, or a \"To: address\" for outgoing e-mail messages.")
  252. (defvar mh-scan-from-mbox-sep-width 2
  253. "Number of columns consumed by whitespace after from-mbox in `mh-scan-format'.
  254. This column will only ever have spaces in it.")
  255. (defvar mh-scan-field-destination-offset
  256. (+ mh-scan-cmd-note-width)
  257. "The offset from the `mh-cmd-note' for the destination column.")
  258. (defvar mh-scan-field-from-start-offset
  259. (+ mh-scan-cmd-note-width
  260. mh-scan-destination-width
  261. mh-scan-date-width
  262. mh-scan-date-flag-width)
  263. "The offset from the `mh-cmd-note' to find the start of \"From:\" address.")
  264. (defvar mh-scan-field-from-end-offset
  265. (+ mh-scan-field-from-start-offset mh-scan-from-mbox-width)
  266. "The offset from the `mh-cmd-note' to find the end of \"From:\" address.")
  267. (defvar mh-scan-field-subject-start-offset
  268. (+ mh-scan-cmd-note-width
  269. mh-scan-destination-width
  270. mh-scan-date-width
  271. mh-scan-date-flag-width
  272. mh-scan-from-mbox-width
  273. mh-scan-from-mbox-sep-width)
  274. "The offset from the `mh-cmd-note' to find the start of the subject.")
  275. ;;; Notation
  276. ;; Alphabetical.
  277. (defvar mh-note-cur ?+
  278. "The current message (in MH, not in MH-E) is marked by this character.
  279. See also `mh-scan-cur-msg-number-regexp'.")
  280. (defvar mh-note-copied ?C
  281. "Messages that have been copied are marked by this character.")
  282. (defvar mh-note-deleted ?D
  283. "Messages that have been deleted are marked by this character.
  284. See also `mh-scan-deleted-msg-regexp'.")
  285. (defvar mh-note-dist ?R
  286. "Messages that have been redistributed are marked by this character.")
  287. (defvar mh-note-forw ?F
  288. "Messages that have been forwarded are marked by this character.")
  289. (defvar mh-note-printed ?P
  290. "Messages that have been printed are marked by this character.")
  291. (defvar mh-note-refiled ?^
  292. "Messages that have been refiled are marked by this character.
  293. See also `mh-scan-refiled-msg-regexp'.")
  294. (defvar mh-note-repl ?-
  295. "Messages that have been replied to are marked by this character.")
  296. (defvar mh-note-seq ?%
  297. "Messages in a user-defined sequence are marked by this character.
  298. Messages in the \"search\" sequence are marked by this character as
  299. well.")
  300. ;;; Utilities
  301. ;;;###mh-autoload
  302. (defun mh-scan-msg-number-regexp ()
  303. "Return value of variable `mh-scan-msg-number-regexp'."
  304. mh-scan-msg-number-regexp)
  305. ;;;###mh-autoload
  306. (defun mh-scan-msg-search-regexp ()
  307. "Return value of variable `mh-scan-msg-search-regexp'."
  308. mh-scan-msg-search-regexp)
  309. ;;;###mh-autoload
  310. (defun mh-set-cmd-note (column)
  311. "Set `mh-cmd-note' to COLUMN.
  312. Note that columns in Emacs start with 0."
  313. (setq mh-cmd-note column))
  314. ;;;###mh-autoload
  315. (defun mh-scan-format ()
  316. "Return the output format argument for the scan program."
  317. (if (equal mh-scan-format-file t)
  318. (list "-format" (if (mh-variant-p 'nmh 'gnu-mh)
  319. (list (mh-update-scan-format
  320. mh-scan-format-nmh mh-cmd-note))
  321. (list (mh-update-scan-format
  322. mh-scan-format-mh mh-cmd-note))))
  323. (if (not (equal mh-scan-format-file nil))
  324. (list "-form" mh-scan-format-file))))
  325. (defun mh-update-scan-format (fmt width)
  326. "Return a scan format with the (msg) width in the FMT replaced with WIDTH.
  327. The message number width portion of the format is discovered
  328. using `mh-scan-msg-format-regexp'. Its replacement is controlled
  329. with `mh-scan-msg-format-string'."
  330. (or (and
  331. (string-match mh-scan-msg-format-regexp fmt)
  332. (let ((begin (match-beginning 1))
  333. (end (match-end 1)))
  334. (concat (substring fmt 0 begin)
  335. (format mh-scan-msg-format-string width)
  336. (substring fmt end))))
  337. fmt))
  338. ;;;###mh-autoload
  339. (defun mh-msg-num-width (folder)
  340. "Return the width of the largest message number in this FOLDER."
  341. (or mh-progs (mh-find-path))
  342. (let ((tmp-buffer (get-buffer-create mh-temp-buffer))
  343. (width 0))
  344. (with-current-buffer tmp-buffer
  345. (erase-buffer)
  346. (apply 'call-process
  347. (expand-file-name mh-scan-prog mh-progs) nil '(t nil) nil
  348. (list folder "last" "-format" "%(msg)"))
  349. (goto-char (point-min))
  350. (if (re-search-forward mh-scan-msg-number-regexp nil 0 1)
  351. (setq width (length (buffer-substring
  352. (match-beginning 1) (match-end 1))))))
  353. width))
  354. ;;;###mh-autoload
  355. (defun mh-msg-num-width-to-column (width)
  356. "Return the column for notations given message number WIDTH.
  357. Note that columns in Emacs start with 0.
  358. If `mh-scan-format-file' is set to \"Use MH-E scan Format\" this
  359. means that either `mh-scan-format-mh' or `mh-scan-format-nmh' are
  360. in use. This function therefore assumes that the first column is
  361. empty (to provide room for the cursor), the following WIDTH
  362. columns contain the message number, and the column for notations
  363. comes after that."
  364. (if (eq mh-scan-format-file t)
  365. (max (1+ width) 2)
  366. (error "%s %s" "Can't call `mh-msg-num-width-to-column' when"
  367. "`mh-scan-format-file' is not set to \"Use MH-E scan Format\"")))
  368. (provide 'mh-scan)
  369. ;; Local Variables:
  370. ;; indent-tabs-mode: nil
  371. ;; sentence-end-double-space: nil
  372. ;; End:
  373. ;;; mh-scan.el ends here