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