gnus-cite.el 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282
  1. ;;; gnus-cite.el --- parse citations in articles for Gnus
  2. ;; Copyright (C) 1995-2012 Free Software Foundation, Inc.
  3. ;; Author: Per Abhiddenware
  4. ;; This file is part of GNU Emacs.
  5. ;; GNU Emacs is free software: you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; GNU Emacs is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;;; Code:
  17. (eval-when-compile (require 'cl))
  18. (eval-when-compile
  19. (when (featurep 'xemacs)
  20. (require 'easy-mmode))) ; for `define-minor-mode'
  21. (require 'gnus)
  22. (require 'gnus-range)
  23. (require 'gnus-art)
  24. (require 'message) ; for message-cite-prefix-regexp
  25. ;;; Customization:
  26. (defgroup gnus-cite nil
  27. "Citation."
  28. :prefix "gnus-cite-"
  29. :link '(custom-manual "(gnus)Article Highlighting")
  30. :group 'gnus-article)
  31. (defcustom gnus-cited-opened-text-button-line-format "%(%{[-]%}%)\n"
  32. "Format of opened cited text buttons."
  33. :group 'gnus-cite
  34. :type 'string)
  35. (defcustom gnus-cited-closed-text-button-line-format "%(%{[+]%}%)\n"
  36. "Format of closed cited text buttons."
  37. :group 'gnus-cite
  38. :type 'string)
  39. (defcustom gnus-cited-lines-visible nil
  40. "The number of lines of hidden cited text to remain visible.
  41. Or a pair (cons) of numbers which are the number of lines at the top
  42. and bottom of the text, respectively, to remain visible."
  43. :group 'gnus-cite
  44. :type '(choice (const :tag "none" nil)
  45. integer
  46. (cons :tag "Top and Bottom" integer integer)))
  47. (defcustom gnus-cite-parse-max-size 25000
  48. "Maximum article size (in bytes) where parsing citations is allowed.
  49. Set it to nil to parse all articles."
  50. :group 'gnus-cite
  51. :type '(choice (const :tag "all" nil)
  52. integer))
  53. (defcustom gnus-cite-max-prefix 20
  54. "Maximum possible length for a citation prefix."
  55. :group 'gnus-cite
  56. :type 'integer)
  57. (defcustom gnus-supercite-regexp
  58. (concat "^\\(" message-cite-prefix-regexp "\\)? *"
  59. ">>>>> +\"\\([^\"\n]+\\)\" +==")
  60. "*Regexp matching normal Supercite attribution lines.
  61. The first grouping must match prefixes added by other packages."
  62. :group 'gnus-cite
  63. :type 'regexp)
  64. (defcustom gnus-supercite-secondary-regexp "^.*\"\\([^\"\n]+\\)\" +=="
  65. "Regexp matching mangled Supercite attribution lines.
  66. The first regexp group should match the Supercite attribution."
  67. :group 'gnus-cite
  68. :type 'regexp)
  69. (defcustom gnus-cite-minimum-match-count 2
  70. "Minimum number of identical prefixes before we believe it's a citation."
  71. :group 'gnus-cite
  72. :type 'integer)
  73. ;; Some Microsoft products put in a citation that extends to the
  74. ;; remainder of the message:
  75. ;;
  76. ;; -----Original Message-----
  77. ;; From: ...
  78. ;; To: ...
  79. ;; Sent: ... [date, in non-RFC-2822 format]
  80. ;; Subject: ...
  81. ;;
  82. ;; Cited message, with no prefixes
  83. ;;
  84. ;; The four headers are always the same. But note they are prone to
  85. ;; folding without additional indentation.
  86. ;;
  87. ;; Others use "----- Original Message -----" instead, and properly quote
  88. ;; the body using "> ". This style is handled without special cases.
  89. (defcustom gnus-cite-attribution-prefix
  90. "In article\\|in <\\|On \\(Mon\\|Tue\\|Wed\\|Thu\\|Fri\\|Sat\\|Sun\\),\\|----- ?Original Message ?-----"
  91. "*Regexp matching the beginning of an attribution line."
  92. :group 'gnus-cite
  93. :type 'regexp)
  94. (defcustom gnus-cite-attribution-suffix
  95. "\\(\\(wrote\\|writes\\|said\\|says\\|>\\)\\(:\\|\\.\\.\\.\\)\\|----- ?Original Message ?-----\\)[ \t]*$"
  96. "*Regexp matching the end of an attribution line.
  97. The text matching the first grouping will be used as a button."
  98. :group 'gnus-cite
  99. :type 'regexp)
  100. (defcustom gnus-cite-unsightly-citation-regexp
  101. "^-----Original Message-----\nFrom: \\(.+\n\\)+\n"
  102. "Regexp matching Microsoft-type rest-of-message citations."
  103. :version "22.1"
  104. :group 'gnus-cite
  105. :type 'regexp)
  106. (defcustom gnus-cite-ignore-quoted-from t
  107. "Non-nil means don't regard lines beginning with \">From \" as cited text.
  108. Those lines may have been quoted by MTAs in order not to mix up with
  109. the envelope From line."
  110. :version "22.1"
  111. :group 'gnus-cite
  112. :type 'boolean)
  113. (defface gnus-cite-attribution '((t (:italic t)))
  114. "Face used for attribution lines."
  115. :group 'gnus-cite)
  116. ;; backward-compatibility alias
  117. (put 'gnus-cite-attribution-face 'face-alias 'gnus-cite-attribution)
  118. (put 'gnus-cite-attribution-face 'obsolete-face "22.1")
  119. (defcustom gnus-cite-attribution-face 'gnus-cite-attribution
  120. "Face used for attribution lines.
  121. It is merged with the face for the cited text belonging to the attribution."
  122. :version "22.1"
  123. :group 'gnus-cite
  124. :type 'face)
  125. (defface gnus-cite-1 '((((class color)
  126. (background dark))
  127. (:foreground "light blue"))
  128. (((class color)
  129. (background light))
  130. (:foreground "MidnightBlue"))
  131. (t
  132. (:italic t)))
  133. "Citation face."
  134. :group 'gnus-cite)
  135. ;; backward-compatibility alias
  136. (put 'gnus-cite-face-1 'face-alias 'gnus-cite-1)
  137. (put 'gnus-cite-face-1 'obsolete-face "22.1")
  138. (defface gnus-cite-2 '((((class color)
  139. (background dark))
  140. (:foreground "light cyan"))
  141. (((class color)
  142. (background light))
  143. (:foreground "firebrick"))
  144. (t
  145. (:italic t)))
  146. "Citation face."
  147. :group 'gnus-cite)
  148. ;; backward-compatibility alias
  149. (put 'gnus-cite-face-2 'face-alias 'gnus-cite-2)
  150. (put 'gnus-cite-face-2 'obsolete-face "22.1")
  151. (defface gnus-cite-3 '((((class color)
  152. (background dark))
  153. (:foreground "light yellow"))
  154. (((class color)
  155. (background light))
  156. (:foreground "dark green"))
  157. (t
  158. (:italic t)))
  159. "Citation face."
  160. :group 'gnus-cite)
  161. ;; backward-compatibility alias
  162. (put 'gnus-cite-face-3 'face-alias 'gnus-cite-3)
  163. (put 'gnus-cite-face-3 'obsolete-face "22.1")
  164. (defface gnus-cite-4 '((((class color)
  165. (background dark))
  166. (:foreground "light pink"))
  167. (((class color)
  168. (background light))
  169. (:foreground "OrangeRed"))
  170. (t
  171. (:italic t)))
  172. "Citation face."
  173. :group 'gnus-cite)
  174. ;; backward-compatibility alias
  175. (put 'gnus-cite-face-4 'face-alias 'gnus-cite-4)
  176. (put 'gnus-cite-face-4 'obsolete-face "22.1")
  177. (defface gnus-cite-5 '((((class color)
  178. (background dark))
  179. (:foreground "pale green"))
  180. (((class color)
  181. (background light))
  182. (:foreground "dark khaki"))
  183. (t
  184. (:italic t)))
  185. "Citation face."
  186. :group 'gnus-cite)
  187. ;; backward-compatibility alias
  188. (put 'gnus-cite-face-5 'face-alias 'gnus-cite-5)
  189. (put 'gnus-cite-face-5 'obsolete-face "22.1")
  190. (defface gnus-cite-6 '((((class color)
  191. (background dark))
  192. (:foreground "beige"))
  193. (((class color)
  194. (background light))
  195. (:foreground "dark violet"))
  196. (t
  197. (:italic t)))
  198. "Citation face."
  199. :group 'gnus-cite)
  200. ;; backward-compatibility alias
  201. (put 'gnus-cite-face-6 'face-alias 'gnus-cite-6)
  202. (put 'gnus-cite-face-6 'obsolete-face "22.1")
  203. (defface gnus-cite-7 '((((class color)
  204. (background dark))
  205. (:foreground "orange"))
  206. (((class color)
  207. (background light))
  208. (:foreground "SteelBlue4"))
  209. (t
  210. (:italic t)))
  211. "Citation face."
  212. :group 'gnus-cite)
  213. ;; backward-compatibility alias
  214. (put 'gnus-cite-face-7 'face-alias 'gnus-cite-7)
  215. (put 'gnus-cite-face-7 'obsolete-face "22.1")
  216. (defface gnus-cite-8 '((((class color)
  217. (background dark))
  218. (:foreground "magenta"))
  219. (((class color)
  220. (background light))
  221. (:foreground "magenta"))
  222. (t
  223. (:italic t)))
  224. "Citation face."
  225. :group 'gnus-cite)
  226. ;; backward-compatibility alias
  227. (put 'gnus-cite-face-8 'face-alias 'gnus-cite-8)
  228. (put 'gnus-cite-face-8 'obsolete-face "22.1")
  229. (defface gnus-cite-9 '((((class color)
  230. (background dark))
  231. (:foreground "violet"))
  232. (((class color)
  233. (background light))
  234. (:foreground "violet"))
  235. (t
  236. (:italic t)))
  237. "Citation face."
  238. :group 'gnus-cite)
  239. ;; backward-compatibility alias
  240. (put 'gnus-cite-face-9 'face-alias 'gnus-cite-9)
  241. (put 'gnus-cite-face-9 'obsolete-face "22.1")
  242. (defface gnus-cite-10 '((((class color)
  243. (background dark))
  244. (:foreground "plum1"))
  245. (((class color)
  246. (background light))
  247. (:foreground "medium purple"))
  248. (t
  249. (:italic t)))
  250. "Citation face."
  251. :group 'gnus-cite)
  252. ;; backward-compatibility alias
  253. (put 'gnus-cite-face-10 'face-alias 'gnus-cite-10)
  254. (put 'gnus-cite-face-10 'obsolete-face "22.1")
  255. (defface gnus-cite-11 '((((class color)
  256. (background dark))
  257. (:foreground "turquoise"))
  258. (((class color)
  259. (background light))
  260. (:foreground "turquoise"))
  261. (t
  262. (:italic t)))
  263. "Citation face."
  264. :group 'gnus-cite)
  265. ;; backward-compatibility alias
  266. (put 'gnus-cite-face-11 'face-alias 'gnus-cite-11)
  267. (put 'gnus-cite-face-11 'obsolete-face "22.1")
  268. (defcustom gnus-cite-face-list
  269. '(gnus-cite-1 gnus-cite-2 gnus-cite-3 gnus-cite-4 gnus-cite-5 gnus-cite-6
  270. gnus-cite-7 gnus-cite-8 gnus-cite-9 gnus-cite-10 gnus-cite-11)
  271. "*List of faces used for highlighting citations.
  272. When there are citations from multiple articles in the same message,
  273. Gnus will try to give each citation from each article its own face.
  274. This should make it easier to see who wrote what."
  275. :group 'gnus-cite
  276. :type '(repeat face)
  277. :set (lambda (symbol value)
  278. (prog1
  279. (custom-set-default symbol value)
  280. (if (boundp 'gnus-message-max-citation-depth)
  281. (setq gnus-message-max-citation-depth (length value)))
  282. (if (boundp 'gnus-message-citation-keywords)
  283. (setq gnus-message-citation-keywords
  284. `((gnus-message-search-citation-line
  285. ,@(let ((list nil)
  286. (count 1))
  287. (dolist (face value (nreverse list))
  288. (push (list count (list 'quote face) 'prepend t)
  289. list)
  290. (setq count (1+ count)))))))))))
  291. (defcustom gnus-cite-hide-percentage 50
  292. "Only hide excess citation if above this percentage of the body."
  293. :group 'gnus-cite
  294. :type 'number)
  295. (defcustom gnus-cite-hide-absolute 10
  296. "Only hide excess citation if above this number of lines in the body."
  297. :group 'gnus-cite
  298. :type 'integer)
  299. (defcustom gnus-cite-blank-line-after-header t
  300. "If non-nil, put a blank line between the citation header and the button."
  301. :group 'gnus-cite
  302. :type 'boolean)
  303. ;; This has to go here because its default value depends on
  304. ;; gnus-cite-face-list.
  305. (defcustom gnus-article-boring-faces (cons 'gnus-signature gnus-cite-face-list)
  306. "List of faces that are not worth reading.
  307. If an article has more pages below the one you are looking at, but
  308. nothing on those pages is a word of at least three letters that is not
  309. in a boring face, then the pages will be skipped."
  310. :type '(repeat face)
  311. :group 'gnus-article-hiding)
  312. ;;; Internal Variables:
  313. (defvar gnus-cite-article nil)
  314. (defvar gnus-cite-overlay-list nil)
  315. (defvar gnus-cite-prefix-alist nil)
  316. ;; Alist of citation prefixes.
  317. ;; The cdr is a list of lines with that prefix.
  318. (defvar gnus-cite-attribution-alist nil)
  319. ;; Alist of attribution lines.
  320. ;; The car is a line number.
  321. ;; The cdr is the prefix for the citation started by that line.
  322. (defvar gnus-cite-loose-prefix-alist nil)
  323. ;; Alist of citation prefixes that have no matching attribution.
  324. ;; The cdr is a list of lines with that prefix.
  325. (defvar gnus-cite-loose-attribution-alist nil)
  326. ;; Alist of attribution lines that have no matching citation.
  327. ;; Each member has the form (WROTE IN PREFIX TAG), where
  328. ;; WROTE: is the attribution line number
  329. ;; IN: is the line number of the previous line if part of the same attribution,
  330. ;; PREFIX: Is the citation prefix of the attribution line(s), and
  331. ;; TAG: Is a Supercite tag, if any.
  332. (defvar gnus-cited-opened-text-button-line-format-alist
  333. `((?b (marker-position beg) ?d)
  334. (?e (marker-position end) ?d)
  335. (?n (count-lines beg end) ?d)
  336. (?l (- end beg) ?d)))
  337. (defvar gnus-cited-opened-text-button-line-format-spec nil)
  338. (defvar gnus-cited-closed-text-button-line-format-alist
  339. gnus-cited-opened-text-button-line-format-alist)
  340. (defvar gnus-cited-closed-text-button-line-format-spec nil)
  341. ;;; Commands:
  342. (defun gnus-article-highlight-citation (&optional force same-buffer)
  343. "Highlight cited text.
  344. Each citation in the article will be highlighted with a different face.
  345. The faces are taken from `gnus-cite-face-list'.
  346. Attribution lines are highlighted with the same face as the
  347. corresponding citation merged with the face `gnus-cite-attribution'.
  348. Text is considered cited if at least `gnus-cite-minimum-match-count'
  349. lines matches `message-cite-prefix-regexp' with the same prefix.
  350. Lines matching `gnus-cite-attribution-suffix' and perhaps
  351. `gnus-cite-attribution-prefix' are considered attribution lines."
  352. (interactive (list 'force))
  353. (with-current-buffer (if same-buffer (current-buffer) gnus-article-buffer)
  354. (gnus-cite-parse-maybe force)
  355. (let ((buffer-read-only nil)
  356. (alist gnus-cite-prefix-alist)
  357. (faces gnus-cite-face-list)
  358. (inhibit-point-motion-hooks t)
  359. face entry prefix skip numbers number face-alist)
  360. ;; Loop through citation prefixes.
  361. (while alist
  362. (setq entry (car alist)
  363. alist (cdr alist)
  364. prefix (car entry)
  365. numbers (cdr entry)
  366. face (car faces)
  367. faces (or (cdr faces) gnus-cite-face-list)
  368. face-alist (cons (cons prefix face) face-alist))
  369. (while numbers
  370. (setq number (car numbers)
  371. numbers (cdr numbers))
  372. (and (not (assq number gnus-cite-attribution-alist))
  373. (not (assq number gnus-cite-loose-attribution-alist))
  374. (gnus-cite-add-face number prefix face))))
  375. ;; Loop through attribution lines.
  376. (setq alist gnus-cite-attribution-alist)
  377. (while alist
  378. (setq entry (car alist)
  379. alist (cdr alist)
  380. number (car entry)
  381. prefix (cdr entry)
  382. skip (gnus-cite-find-prefix number)
  383. face (cdr (assoc prefix face-alist)))
  384. ;; Add attribution button.
  385. (goto-char (point-min))
  386. (forward-line (1- number))
  387. (when (re-search-forward gnus-cite-attribution-suffix
  388. (point-at-eol)
  389. t)
  390. (gnus-article-add-button (match-beginning 1) (match-end 1)
  391. 'gnus-cite-toggle prefix))
  392. ;; Highlight attribution line.
  393. (gnus-cite-add-face number skip face)
  394. (gnus-cite-add-face number skip gnus-cite-attribution-face))
  395. ;; Loop through attribution lines.
  396. (setq alist gnus-cite-loose-attribution-alist)
  397. (while alist
  398. (setq entry (car alist)
  399. alist (cdr alist)
  400. number (car entry)
  401. skip (gnus-cite-find-prefix number))
  402. (gnus-cite-add-face number skip gnus-cite-attribution-face)))))
  403. (defun gnus-dissect-cited-text ()
  404. "Dissect the article buffer looking for cited text."
  405. (with-current-buffer gnus-article-buffer
  406. (gnus-cite-parse-maybe nil t)
  407. (let ((alist gnus-cite-prefix-alist)
  408. prefix numbers number marks m)
  409. ;; Loop through citation prefixes.
  410. (while alist
  411. (setq numbers (pop alist)
  412. prefix (pop numbers))
  413. (while numbers
  414. (setq number (pop numbers))
  415. (goto-char (point-min))
  416. (forward-line number)
  417. (push (cons (point-marker) "") marks)
  418. (while (and numbers
  419. (= (1- number) (car numbers)))
  420. (setq number (pop numbers)))
  421. (goto-char (point-min))
  422. (forward-line (1- number))
  423. (push (cons (point-marker) prefix) marks)))
  424. ;; Skip to the beginning of the body.
  425. (article-goto-body)
  426. (push (cons (point-marker) "") marks)
  427. ;; Find the end of the body.
  428. (goto-char (point-max))
  429. (gnus-article-search-signature)
  430. (push (cons (point-marker) "") marks)
  431. ;; Sort the marks.
  432. (setq marks (sort marks 'car-less-than-car))
  433. (let ((omarks marks))
  434. (setq marks nil)
  435. (while (cdr omarks)
  436. (if (= (caar omarks) (caadr omarks))
  437. (progn
  438. (unless (equal (cdar omarks) "")
  439. (push (car omarks) marks))
  440. (unless (equal (cdadr omarks) "")
  441. (push (cadr omarks) marks))
  442. (unless (and (equal (cdar omarks) "")
  443. (equal (cdadr omarks) "")
  444. (not (cddr omarks)))
  445. (setq omarks (cdr omarks))))
  446. (push (car omarks) marks))
  447. (setq omarks (cdr omarks)))
  448. (when (car omarks)
  449. (push (car omarks) marks))
  450. (setq marks (setq m (nreverse marks)))
  451. (while (cddr m)
  452. (if (and (equal (cdadr m) "")
  453. (equal (cdar m) (cdaddr m))
  454. (goto-char (caadr m))
  455. (forward-line 1)
  456. (= (point) (caaddr m)))
  457. (setcdr m (cdddr m))
  458. (setq m (cdr m))))
  459. marks))))
  460. (defun gnus-article-fill-cited-long-lines ()
  461. (gnus-article-fill-cited-article nil t))
  462. (defun gnus-article-fill-cited-article (&optional width long-lines)
  463. "Do word wrapping in the current article.
  464. If WIDTH (the numerical prefix), use that text width when
  465. filling. If LONG-LINES, only fill sections that have lines
  466. longer than the frame width."
  467. (interactive "P")
  468. (with-current-buffer gnus-article-buffer
  469. (let ((buffer-read-only nil)
  470. (inhibit-point-motion-hooks t)
  471. (marks (gnus-dissect-cited-text))
  472. (adaptive-fill-mode nil)
  473. (filladapt-mode nil)
  474. (fill-column (if width (prefix-numeric-value width) fill-column)))
  475. (save-restriction
  476. (while (cdr marks)
  477. (narrow-to-region (caar marks) (caadr marks))
  478. (let ((adaptive-fill-regexp
  479. (concat "^" (regexp-quote (cdar marks)) " *"))
  480. (fill-prefix
  481. (if (string= (cdar marks) "") ""
  482. (concat (cdar marks) " ")))
  483. (do-fill (not long-lines))
  484. use-hard-newlines)
  485. (unless do-fill
  486. (setq do-fill (gnus-article-foldable-buffer (cdar marks))))
  487. ;; Note: the XEmacs version of `fill-region' inserts a newline
  488. ;; unless the region ends with a newline.
  489. (when do-fill
  490. (if (not long-lines)
  491. (fill-region (point-min) (point-max))
  492. (goto-char (point-min))
  493. (while (not (eobp))
  494. (end-of-line)
  495. (when (prog1
  496. (> (current-column) (window-width))
  497. (forward-line 1))
  498. (save-restriction
  499. (narrow-to-region (line-beginning-position 0) (point))
  500. (fill-region (point-min) (point-max))))))))
  501. (set-marker (caar marks) nil)
  502. (setq marks (cdr marks)))
  503. (when marks
  504. (set-marker (caar marks) nil))
  505. ;; All this information is now incorrect.
  506. (setq gnus-cite-prefix-alist nil
  507. gnus-cite-attribution-alist nil
  508. gnus-cite-loose-prefix-alist nil
  509. gnus-cite-loose-attribution-alist nil
  510. gnus-cite-article nil)))))
  511. (defun gnus-article-foldable-buffer (prefix)
  512. (let ((do-fill nil)
  513. columns)
  514. (goto-char (point-min))
  515. (while (not (eobp))
  516. (unless (> (length prefix) (- (point-max) (point)))
  517. (forward-char (length prefix)))
  518. (skip-chars-forward " \t")
  519. (unless (eolp)
  520. (let ((elem (assq (current-column) columns)))
  521. (unless elem
  522. (setq elem (cons (current-column) 0))
  523. (push elem columns))
  524. (setcdr elem (1+ (cdr elem)))))
  525. (end-of-line)
  526. (when (> (current-column) (window-width))
  527. (setq do-fill t))
  528. (forward-line 1))
  529. (and do-fill
  530. ;; We know know that there are long lines here, but does this look
  531. ;; like code? Check for ragged edges on the left.
  532. (< (length columns) 3))))
  533. (defun gnus-article-hide-citation (&optional arg force)
  534. "Toggle hiding of all cited text except attribution lines.
  535. See the documentation for `gnus-article-highlight-citation'.
  536. If given a negative prefix, always show; if given a positive prefix,
  537. always hide."
  538. (interactive (append (gnus-article-hidden-arg) (list 'force)))
  539. (gnus-set-format 'cited-opened-text-button t)
  540. (gnus-set-format 'cited-closed-text-button t)
  541. (with-current-buffer gnus-article-buffer
  542. (let ((buffer-read-only nil)
  543. marks
  544. (inhibit-point-motion-hooks t)
  545. (props (nconc (list 'article-type 'cite)
  546. gnus-hidden-properties))
  547. (point (point-min))
  548. found beg end start)
  549. (while (setq point
  550. (text-property-any point (point-max)
  551. 'gnus-callback
  552. 'gnus-article-toggle-cited-text))
  553. (setq found t)
  554. (goto-char point)
  555. (gnus-article-toggle-cited-text
  556. (get-text-property point 'gnus-data) arg)
  557. (forward-line 1)
  558. (setq point (point)))
  559. (unless found
  560. (setq marks (gnus-dissect-cited-text))
  561. (while marks
  562. (setq beg nil
  563. end nil)
  564. (while (and marks (string= (cdar marks) ""))
  565. (setq marks (cdr marks)))
  566. (when marks
  567. (setq beg (caar marks)))
  568. (while (and marks (not (string= (cdar marks) "")))
  569. (setq marks (cdr marks)))
  570. (when marks
  571. (setq end (caar marks)))
  572. ;; Skip past lines we want to leave visible.
  573. (when (and beg end gnus-cited-lines-visible)
  574. (goto-char beg)
  575. (forward-line (if (consp gnus-cited-lines-visible)
  576. (car gnus-cited-lines-visible)
  577. gnus-cited-lines-visible))
  578. (if (>= (point) end)
  579. (setq beg nil)
  580. (setq beg (point-marker))
  581. (when (consp gnus-cited-lines-visible)
  582. (goto-char end)
  583. (forward-line (- (cdr gnus-cited-lines-visible)))
  584. (if (<= (point) beg)
  585. (setq beg nil)
  586. (setq end (point-marker))))))
  587. (when (and beg end)
  588. (gnus-add-wash-type 'cite)
  589. ;; We use markers for the end-points to facilitate later
  590. ;; wrapping and mangling of text.
  591. (setq beg (set-marker (make-marker) beg)
  592. end (set-marker (make-marker) end))
  593. (gnus-add-text-properties-when 'article-type nil beg end props)
  594. (goto-char beg)
  595. (when (and gnus-cite-blank-line-after-header
  596. (not (save-excursion (search-backward "\n\n" nil t))))
  597. (insert "\n"))
  598. (put-text-property
  599. (setq start (point-marker))
  600. (progn
  601. (gnus-article-add-button
  602. (point)
  603. (progn (eval gnus-cited-closed-text-button-line-format-spec)
  604. (point))
  605. `gnus-article-toggle-cited-text
  606. (list (cons beg end) start))
  607. (point))
  608. 'article-type 'annotation)
  609. (set-marker beg (point))))))))
  610. (defun gnus-article-toggle-cited-text (args &optional arg)
  611. "Toggle hiding the text in REGION.
  612. ARG can be nil or a number. Positive means hide, negative
  613. means show, nil means toggle."
  614. (let* ((region (car args))
  615. (beg (car region))
  616. (end (cdr region))
  617. (start (cadr args))
  618. (hidden
  619. (text-property-any beg (1- end) 'article-type 'cite))
  620. (inhibit-point-motion-hooks t)
  621. buffer-read-only)
  622. (when (or (null arg)
  623. (zerop arg)
  624. (and (> arg 0) (not hidden))
  625. (and (< arg 0) hidden))
  626. (if hidden
  627. (progn
  628. ;; Can't remove 'cite from g-a-wash-types here because
  629. ;; multiple citations may be hidden -jas
  630. (gnus-remove-text-properties-when
  631. 'article-type 'cite beg end
  632. (cons 'article-type (cons 'cite
  633. gnus-hidden-properties))))
  634. (gnus-add-wash-type 'cite)
  635. (gnus-add-text-properties-when
  636. 'article-type nil beg end
  637. (cons 'article-type (cons 'cite
  638. gnus-hidden-properties))))
  639. (let ((gnus-article-mime-handle-alist-1 gnus-article-mime-handle-alist))
  640. (gnus-set-mode-line 'article))
  641. (save-excursion
  642. (goto-char start)
  643. (gnus-delete-line)
  644. (put-text-property
  645. (point)
  646. (progn
  647. (gnus-article-add-button
  648. (point)
  649. (progn (eval
  650. (if hidden
  651. gnus-cited-opened-text-button-line-format-spec
  652. gnus-cited-closed-text-button-line-format-spec))
  653. (point))
  654. `gnus-article-toggle-cited-text
  655. args)
  656. (point))
  657. 'article-type 'annotation)))))
  658. (defun gnus-article-hide-citation-maybe (&optional arg force)
  659. "Toggle hiding of cited text that has an attribution line.
  660. If given a negative prefix, always show; if given a positive prefix,
  661. always hide.
  662. This will do nothing unless at least `gnus-cite-hide-percentage'
  663. percent and at least `gnus-cite-hide-absolute' lines of the body is
  664. cited text with attributions. When called interactively, these two
  665. variables are ignored.
  666. See also the documentation for `gnus-article-highlight-citation'."
  667. (interactive (append (gnus-article-hidden-arg) '(force)))
  668. (with-current-buffer gnus-article-buffer
  669. (gnus-delete-wash-type 'cite)
  670. (unless (gnus-article-check-hidden-text 'cite arg)
  671. (save-excursion
  672. (gnus-cite-parse-maybe force)
  673. (article-goto-body)
  674. (let ((start (point))
  675. (atts gnus-cite-attribution-alist)
  676. (buffer-read-only nil)
  677. (inhibit-point-motion-hooks t)
  678. (hidden 0)
  679. total)
  680. (goto-char (point-max))
  681. (gnus-article-search-signature)
  682. (setq total (count-lines start (point)))
  683. (while atts
  684. (setq hidden (+ hidden (length (cdr (assoc (cdar atts)
  685. gnus-cite-prefix-alist))))
  686. atts (cdr atts)))
  687. (when (or force
  688. (and (> (* 100 hidden) (* gnus-cite-hide-percentage total))
  689. (> hidden gnus-cite-hide-absolute)))
  690. (gnus-add-wash-type 'cite)
  691. (setq atts gnus-cite-attribution-alist)
  692. (while atts
  693. (setq total (cdr (assoc (cdar atts) gnus-cite-prefix-alist))
  694. atts (cdr atts))
  695. (while total
  696. (setq hidden (car total)
  697. total (cdr total))
  698. (goto-char (point-min))
  699. (forward-line (1- hidden))
  700. (unless (assq hidden gnus-cite-attribution-alist)
  701. (gnus-add-text-properties
  702. (point) (progn (forward-line 1) (point))
  703. (nconc (list 'article-type 'cite)
  704. gnus-hidden-properties)))))))))
  705. (gnus-set-mode-line 'article)))
  706. (defun gnus-article-hide-citation-in-followups ()
  707. "Hide cited text in non-root articles."
  708. (interactive)
  709. (with-current-buffer gnus-article-buffer
  710. (let ((article (cdr gnus-article-current)))
  711. (unless (with-current-buffer gnus-summary-buffer
  712. (gnus-article-displayed-root-p article))
  713. (gnus-article-hide-citation)))))
  714. ;;; Internal functions:
  715. (defun gnus-cite-parse-maybe (&optional force no-overlay)
  716. "Always parse the buffer."
  717. (gnus-cite-localize)
  718. ;;Reset parser information.
  719. (setq gnus-cite-prefix-alist nil
  720. gnus-cite-attribution-alist nil
  721. gnus-cite-loose-prefix-alist nil
  722. gnus-cite-loose-attribution-alist nil)
  723. (unless no-overlay
  724. (gnus-cite-delete-overlays))
  725. ;; Parse if not too large.
  726. (if (and gnus-cite-parse-max-size
  727. (> (buffer-size) gnus-cite-parse-max-size))
  728. ()
  729. (setq gnus-cite-article (cons (car gnus-article-current)
  730. (cdr gnus-article-current)))
  731. (gnus-cite-parse-wrapper)))
  732. (defun gnus-cite-delete-overlays ()
  733. (dolist (overlay gnus-cite-overlay-list)
  734. (ignore-errors
  735. (when (or (not (gnus-overlay-end overlay))
  736. (and (>= (gnus-overlay-end overlay) (point-min))
  737. (<= (gnus-overlay-end overlay) (point-max))))
  738. (setq gnus-cite-overlay-list (delete overlay gnus-cite-overlay-list))
  739. (ignore-errors
  740. (gnus-delete-overlay overlay))))))
  741. (defun gnus-cite-parse-wrapper ()
  742. ;; Wrap chopped gnus-cite-parse.
  743. (article-goto-body)
  744. (let ((inhibit-point-motion-hooks t))
  745. (save-excursion
  746. (gnus-cite-parse-attributions))
  747. (save-excursion
  748. (gnus-cite-parse))
  749. (save-excursion
  750. (gnus-cite-connect-attributions))))
  751. (defun gnus-cite-parse ()
  752. ;; Parse and connect citation prefixes and attribution lines.
  753. ;; Parse current buffer searching for citation prefixes.
  754. (let ((line (1+ (count-lines (point-min) (point))))
  755. (case-fold-search t)
  756. (max (save-excursion
  757. (goto-char (point-max))
  758. (gnus-article-search-signature)
  759. (point)))
  760. (prefix-regexp (concat "^\\(" message-cite-prefix-regexp "\\)"))
  761. alist entry start begin end numbers prefix guess-limit)
  762. ;; Get all potential prefixes in `alist'.
  763. (while (< (point) max)
  764. ;; Each line.
  765. (setq begin (point)
  766. guess-limit (progn (skip-chars-forward "^> \t\r\n") (point))
  767. end (point-at-bol 2)
  768. start end)
  769. (goto-char begin)
  770. ;; Ignore standard Supercite attribution prefix.
  771. (when (and (< guess-limit (+ begin gnus-cite-max-prefix))
  772. (looking-at gnus-supercite-regexp))
  773. (if (match-end 1)
  774. (setq end (1+ (match-end 1)))
  775. (setq end (1+ begin))))
  776. ;; Ignore very long prefixes.
  777. (when (> end (+ begin gnus-cite-max-prefix))
  778. (setq end (+ begin gnus-cite-max-prefix)))
  779. ;; Ignore quoted envelope From_.
  780. (when (and gnus-cite-ignore-quoted-from
  781. (prog2
  782. (setq case-fold-search nil)
  783. (looking-at ">From ")
  784. (setq case-fold-search t)))
  785. (setq end (1+ begin)))
  786. (while (re-search-forward prefix-regexp (1- end) t)
  787. ;; Each prefix.
  788. (setq end (match-end 0)
  789. prefix (buffer-substring begin end))
  790. (set-text-properties 0 (length prefix) nil prefix)
  791. (setq entry (assoc prefix alist))
  792. (if entry
  793. (setcdr entry (cons line (cdr entry)))
  794. (push (list prefix line) alist))
  795. (goto-char begin))
  796. (goto-char start)
  797. (setq line (1+ line)))
  798. ;; Horrible special case for some Microsoft mailers.
  799. (goto-char (point-min))
  800. (setq start t begin nil entry nil)
  801. (while start
  802. ;; Assume this search ends up at the beginning of a line.
  803. (if (re-search-forward gnus-cite-unsightly-citation-regexp max t)
  804. (progn
  805. (when (number-or-marker-p start)
  806. (setq begin (count-lines (point-min) start)
  807. end (count-lines (point-min) (match-beginning 0))))
  808. (setq start (match-end 0)))
  809. (when (number-or-marker-p start)
  810. (setq begin (count-lines (point-min) start)
  811. end (count-lines (point-min) max)))
  812. (setq start nil))
  813. (when begin
  814. (while (< begin end)
  815. ;; Need to do 1+ because we're in the bol.
  816. (push (setq begin (1+ begin)) entry))))
  817. (when entry
  818. (push (cons "" entry) alist))
  819. ;; We got all the potential prefixes. Now create
  820. ;; `gnus-cite-prefix-alist' containing the oldest prefix for each
  821. ;; line that appears at least `gnus-cite-minimum-match-count'
  822. ;; times. First sort them by length. Longer is older.
  823. (setq alist (sort alist (lambda (a b)
  824. (> (length (car a)) (length (car b))))))
  825. (while alist
  826. (setq entry (car alist)
  827. prefix (car entry)
  828. numbers (cdr entry)
  829. alist (cdr alist))
  830. (cond ((null numbers)
  831. ;; No lines with this prefix that wasn't also part of
  832. ;; a longer prefix.
  833. )
  834. ((< (length numbers) gnus-cite-minimum-match-count)
  835. ;; Too few lines with this prefix. We keep it a bit
  836. ;; longer in case it is an exact match for an attribution
  837. ;; line, but we don't remove the line from other
  838. ;; prefixes.
  839. (push entry gnus-cite-prefix-alist))
  840. (t
  841. (push entry
  842. gnus-cite-prefix-alist)
  843. ;; Remove articles from other prefixes.
  844. (let ((loop alist)
  845. current)
  846. (while loop
  847. (setq current (car loop)
  848. loop (cdr loop))
  849. (setcdr current
  850. (gnus-set-difference (cdr current) numbers)))))))))
  851. (defun gnus-cite-parse-attributions ()
  852. (let (al-alist)
  853. ;; Parse attributions
  854. (while (re-search-forward gnus-cite-attribution-suffix (point-max) t)
  855. (let* ((start (match-beginning 0))
  856. (end (match-end 0))
  857. (wrote (count-lines (point-min) end))
  858. (prefix (gnus-cite-find-prefix wrote))
  859. ;; Check previous line for an attribution leader.
  860. (tag (progn
  861. (beginning-of-line 1)
  862. (when (looking-at gnus-supercite-secondary-regexp)
  863. (buffer-substring (match-beginning 1)
  864. (match-end 1)))))
  865. (in (progn
  866. (goto-char start)
  867. (and (re-search-backward gnus-cite-attribution-prefix
  868. (save-excursion
  869. (beginning-of-line 0)
  870. (point))
  871. t)
  872. (not (re-search-forward gnus-cite-attribution-suffix
  873. start t))
  874. (count-lines (point-min) (1+ (point)))))))
  875. (when (eq wrote in)
  876. (setq in nil))
  877. (goto-char end)
  878. ;; don't add duplicates
  879. (let ((al (buffer-substring (save-excursion (beginning-of-line 0)
  880. (1+ (point)))
  881. end)))
  882. (when (not (assoc al al-alist))
  883. (push (list wrote in prefix tag)
  884. gnus-cite-loose-attribution-alist)
  885. (push (cons al t) al-alist)))))))
  886. (defun gnus-cite-connect-attributions ()
  887. ;; Connect attributions to citations
  888. ;; No citations have been connected to attribution lines yet.
  889. (setq gnus-cite-loose-prefix-alist (append gnus-cite-prefix-alist nil))
  890. ;; Parse current buffer searching for attribution lines.
  891. ;; Find exact supercite citations.
  892. (gnus-cite-match-attributions 'small nil
  893. (lambda (prefix tag)
  894. (when tag
  895. (concat "\\`"
  896. (regexp-quote prefix) "[ \t]*"
  897. (regexp-quote tag) ">"))))
  898. ;; Find loose supercite citations after attributions.
  899. (gnus-cite-match-attributions 'small t
  900. (lambda (prefix tag)
  901. (when tag
  902. (concat "\\<"
  903. (regexp-quote tag)
  904. "\\>"))))
  905. ;; Find loose supercite citations anywhere.
  906. (gnus-cite-match-attributions 'small nil
  907. (lambda (prefix tag)
  908. (when tag
  909. (concat "\\<"
  910. (regexp-quote tag)
  911. "\\>"))))
  912. ;; Find nested citations after attributions.
  913. (gnus-cite-match-attributions 'small-if-unique t
  914. (lambda (prefix tag)
  915. (concat "\\`" (regexp-quote prefix) ".+")))
  916. ;; Find nested citations anywhere.
  917. (gnus-cite-match-attributions 'small nil
  918. (lambda (prefix tag)
  919. (concat "\\`" (regexp-quote prefix) ".+")))
  920. ;; Remove loose prefixes with too few lines.
  921. (let ((alist gnus-cite-loose-prefix-alist)
  922. entry)
  923. (while alist
  924. (setq entry (car alist)
  925. alist (cdr alist))
  926. (when (< (length (cdr entry)) gnus-cite-minimum-match-count)
  927. (setq gnus-cite-prefix-alist
  928. (delq entry gnus-cite-prefix-alist)
  929. gnus-cite-loose-prefix-alist
  930. (delq entry gnus-cite-loose-prefix-alist)))))
  931. ;; Find flat attributions.
  932. (gnus-cite-match-attributions 'first t nil)
  933. ;; Find any attributions (are we getting desperate yet?).
  934. (gnus-cite-match-attributions 'first nil nil))
  935. (defun gnus-cite-match-attributions (sort after fun)
  936. ;; Match all loose attributions and citations (SORT AFTER FUN) .
  937. ;;
  938. ;; If SORT is `small', the citation with the shortest prefix will be
  939. ;; used, if it is `first' the first prefix will be used, if it is
  940. ;; `small-if-unique' the shortest prefix will be used if the
  941. ;; attribution line does not share its own prefix with other
  942. ;; loose attribution lines, otherwise the first prefix will be used.
  943. ;;
  944. ;; If AFTER is non-nil, only citations after the attribution line
  945. ;; will be considered.
  946. ;;
  947. ;; If FUN is non-nil, it will be called with the arguments (WROTE
  948. ;; PREFIX TAG) and expected to return a regular expression. Only
  949. ;; citations whose prefix matches the regular expression will be
  950. ;; considered.
  951. ;;
  952. ;; WROTE is the attribution line number.
  953. ;; PREFIX is the attribution line prefix.
  954. ;; TAG is the Supercite tag on the attribution line.
  955. (let ((atts gnus-cite-loose-attribution-alist)
  956. (case-fold-search t)
  957. att wrote in prefix tag regexp limit smallest best size)
  958. (while atts
  959. (setq att (car atts)
  960. atts (cdr atts)
  961. wrote (nth 0 att)
  962. in (nth 1 att)
  963. prefix (nth 2 att)
  964. tag (nth 3 att)
  965. regexp (if fun (funcall fun prefix tag) "")
  966. size (cond ((eq sort 'small) t)
  967. ((eq sort 'first) nil)
  968. (t (< (length (gnus-cite-find-loose prefix)) 2)))
  969. limit (if after wrote -1)
  970. smallest 1000000
  971. best nil)
  972. (let ((cites gnus-cite-loose-prefix-alist)
  973. cite candidate numbers first compare)
  974. (while cites
  975. (setq cite (car cites)
  976. cites (cdr cites)
  977. candidate (car cite)
  978. numbers (cdr cite)
  979. first (apply 'min numbers)
  980. compare (if size (length candidate) first))
  981. (and (> first limit)
  982. regexp
  983. (string-match regexp candidate)
  984. (< compare smallest)
  985. (setq best cite
  986. smallest compare))))
  987. (if (null best)
  988. ()
  989. (setq gnus-cite-loose-attribution-alist
  990. (delq att gnus-cite-loose-attribution-alist))
  991. (push (cons wrote (car best)) gnus-cite-attribution-alist)
  992. (when in
  993. (push (cons in (car best)) gnus-cite-attribution-alist))
  994. (when (memq best gnus-cite-loose-prefix-alist)
  995. (let ((loop gnus-cite-prefix-alist)
  996. (numbers (cdr best))
  997. current)
  998. (setq gnus-cite-loose-prefix-alist
  999. (delq best gnus-cite-loose-prefix-alist))
  1000. (while loop
  1001. (setq current (car loop)
  1002. loop (cdr loop))
  1003. (if (eq current best)
  1004. ()
  1005. (setcdr current (gnus-set-difference (cdr current) numbers))
  1006. (when (null (cdr current))
  1007. (setq gnus-cite-loose-prefix-alist
  1008. (delq current gnus-cite-loose-prefix-alist)
  1009. atts (delq current atts)))))))))))
  1010. (defun gnus-cite-find-loose (prefix)
  1011. ;; Return a list of loose attribution lines prefixed by PREFIX.
  1012. (let* ((atts gnus-cite-loose-attribution-alist)
  1013. att line lines)
  1014. (while atts
  1015. (setq att (car atts)
  1016. line (car att)
  1017. atts (cdr atts))
  1018. (when (string-equal (gnus-cite-find-prefix line) prefix)
  1019. (push line lines)))
  1020. lines))
  1021. (defun gnus-cite-add-face (number prefix face)
  1022. ;; At line NUMBER, ignore PREFIX and add FACE to the rest of the line.
  1023. (when face
  1024. (let ((inhibit-point-motion-hooks t)
  1025. from to overlay)
  1026. (goto-char (point-min))
  1027. (when (zerop (forward-line (1- number)))
  1028. (forward-char (length prefix))
  1029. (skip-chars-forward " \t")
  1030. (setq from (point))
  1031. (end-of-line 1)
  1032. (skip-chars-backward " \t")
  1033. (setq to (point))
  1034. (when (< from to)
  1035. (push (setq overlay (gnus-make-overlay from to))
  1036. gnus-cite-overlay-list)
  1037. (gnus-overlay-put overlay 'evaporate t)
  1038. (gnus-overlay-put overlay 'face face))))))
  1039. (defun gnus-cite-toggle (prefix)
  1040. (with-current-buffer gnus-article-buffer
  1041. (gnus-cite-parse-maybe nil t)
  1042. (let ((buffer-read-only nil)
  1043. (numbers (cdr (assoc prefix gnus-cite-prefix-alist)))
  1044. (inhibit-point-motion-hooks t)
  1045. number)
  1046. (while numbers
  1047. (setq number (car numbers)
  1048. numbers (cdr numbers))
  1049. (goto-char (point-min))
  1050. (forward-line (1- number))
  1051. (cond ((get-text-property (point) 'invisible)
  1052. ;; Can't remove 'cite from g-a-wash-types here because
  1053. ;; multiple citations may be hidden -jas
  1054. (remove-text-properties (point) (progn (forward-line 1) (point))
  1055. gnus-hidden-properties))
  1056. ((assq number gnus-cite-attribution-alist))
  1057. (t
  1058. (gnus-add-wash-type 'cite)
  1059. (gnus-add-text-properties
  1060. (point) (progn (forward-line 1) (point))
  1061. (nconc (list 'article-type 'cite)
  1062. gnus-hidden-properties))))
  1063. (let ((gnus-article-mime-handle-alist-1
  1064. gnus-article-mime-handle-alist))
  1065. (gnus-set-mode-line 'article))))))
  1066. (defun gnus-cite-find-prefix (line)
  1067. ;; Return citation prefix for LINE.
  1068. (let ((alist gnus-cite-prefix-alist)
  1069. (prefix "")
  1070. entry)
  1071. (while alist
  1072. (setq entry (car alist)
  1073. alist (cdr alist))
  1074. (when (memq line (cdr entry))
  1075. (setq prefix (car entry))))
  1076. prefix))
  1077. (defun gnus-cite-localize ()
  1078. "Make the citation variables local to the article buffer."
  1079. (let ((vars '(gnus-cite-article
  1080. gnus-cite-overlay-list gnus-cite-prefix-alist
  1081. gnus-cite-attribution-alist gnus-cite-loose-prefix-alist
  1082. gnus-cite-loose-attribution-alist)))
  1083. (while vars
  1084. (make-local-variable (pop vars)))))
  1085. (defun gnus-cited-line-p ()
  1086. "Say whether the current line is a cited line."
  1087. (save-excursion
  1088. (beginning-of-line)
  1089. (let ((found nil))
  1090. (dolist (prefix (mapcar 'car gnus-cite-prefix-alist))
  1091. (when (string= (buffer-substring (point) (+ (length prefix) (point)))
  1092. prefix)
  1093. (setq found t)))
  1094. found)))
  1095. ;; Highlighting of different citation levels in message-mode.
  1096. ;; - message-cite-prefix will be overridden if this is enabled.
  1097. (defvar gnus-message-max-citation-depth
  1098. (length gnus-cite-face-list)
  1099. "Maximum supported level of citation.")
  1100. (defvar gnus-message-cite-prefix-regexp
  1101. (concat "^\\(?:" message-cite-prefix-regexp "\\)"))
  1102. (defun gnus-message-search-citation-line (limit)
  1103. "Search for a cited line and set match data accordingly.
  1104. Returns nil if there is no such line before LIMIT, t otherwise."
  1105. (when (re-search-forward gnus-message-cite-prefix-regexp limit t)
  1106. (let ((cdepth (min (length (apply 'concat
  1107. (split-string
  1108. (match-string-no-properties 0)
  1109. "[ \t [:alnum:]]+")))
  1110. gnus-message-max-citation-depth))
  1111. (mlist (make-list (* (1+ gnus-message-max-citation-depth) 2) nil))
  1112. (start (point-at-bol))
  1113. (end (point-at-eol)))
  1114. (setcar mlist start)
  1115. (setcar (cdr mlist) end)
  1116. (setcar (nthcdr (* cdepth 2) mlist) start)
  1117. (setcar (nthcdr (1+ (* cdepth 2)) mlist) end)
  1118. (set-match-data mlist))
  1119. t))
  1120. (defvar gnus-message-citation-keywords
  1121. ;; eval-when-compile ;; This breaks in XEmacs
  1122. `((gnus-message-search-citation-line
  1123. ,@(let ((list nil)
  1124. (count 1))
  1125. ;; (require 'gnus-cite)
  1126. (dolist (face gnus-cite-face-list (nreverse list))
  1127. (push (list count (list 'quote face) 'prepend t) list)
  1128. (setq count (1+ count)))))) ;;
  1129. "Keywords for highlighting different levels of message citations.")
  1130. (defvar font-lock-defaults-computed)
  1131. (defvar font-lock-keywords)
  1132. (defvar font-lock-set-defaults)
  1133. (eval-and-compile
  1134. (unless (featurep 'xemacs)
  1135. (autoload 'font-lock-set-defaults "font-lock")))
  1136. (define-minor-mode gnus-message-citation-mode
  1137. "Minor mode providing more font-lock support for nested citations.
  1138. When enabled, it automatically turns on `font-lock-mode'."
  1139. nil ;; init-value
  1140. "" ;; lighter
  1141. nil ;; keymap
  1142. (when (eq major-mode 'message-mode)
  1143. (let ((defaults (car (if (featurep 'xemacs)
  1144. (get 'message-mode 'font-lock-defaults)
  1145. font-lock-defaults)))
  1146. default keywords)
  1147. (while defaults
  1148. (setq default (if (consp defaults)
  1149. (pop defaults)
  1150. (prog1
  1151. defaults
  1152. (setq defaults nil))))
  1153. (if gnus-message-citation-mode
  1154. ;; `gnus-message-citation-keywords' should be the last
  1155. ;; elements of the keywords because the others are unlikely
  1156. ;; to have the OVERRIDE flags -- XEmacs applies a keyword
  1157. ;; having no OVERRIDE flag to matched text even if it has
  1158. ;; already other faces, while Emacs doesn't.
  1159. (set (make-local-variable default)
  1160. (append (default-value default)
  1161. gnus-message-citation-keywords))
  1162. (kill-local-variable default))))
  1163. ;; Force `font-lock-set-defaults' to update `font-lock-keywords'.
  1164. (if (featurep 'xemacs)
  1165. (progn
  1166. (require 'font-lock)
  1167. (setq font-lock-defaults-computed nil
  1168. font-lock-keywords nil))
  1169. (setq font-lock-set-defaults nil))
  1170. (font-lock-set-defaults)
  1171. (cond ((symbol-value 'font-lock-mode)
  1172. (font-lock-fontify-buffer))
  1173. (gnus-message-citation-mode
  1174. (font-lock-mode 1)))))
  1175. (defun turn-on-gnus-message-citation-mode ()
  1176. "Turn on `gnus-message-citation-mode'."
  1177. (gnus-message-citation-mode 1))
  1178. (defun turn-off-gnus-message-citation-mode ()
  1179. "Turn off `gnus-message-citation-mode'."
  1180. (gnus-message-citation-mode -1))
  1181. (gnus-ems-redefine)
  1182. (provide 'gnus-cite)
  1183. ;; Local Variables:
  1184. ;; coding: iso-8859-1
  1185. ;; End:
  1186. ;;; gnus-cite.el ends here