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