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