2953566a2382ddc787054a3f0f232874dfa388c6
[gnus] / lisp / gnus-cite.el
1 ;;; gnus-cite.el --- parse citations in articles for Gnus
2 ;; Copyright (C) 1995,96 Free Software Foundation, Inc.
3
4 ;; Author: Per Abrahamsen <abraham@iesd.auc.dk>
5 ;; Keywords: news, mail
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 (require 'gnus)
29 (require 'gnus-art)
30 (require 'gnus-range)
31
32 ;;; Customization:
33
34 (defgroup gnus-cite nil
35   "Citation."
36   :group 'article)
37   
38
39 (defcustom gnus-cited-text-button-line-format "%(%{[...]%}%)\n"
40   "Format of cited text buttons."
41   :group 'gnus-cite
42   :type 'string)
43
44 (defcustom gnus-cited-lines-visible nil
45   "The number of lines of hidden cited text to remain visible."
46   :group 'gnus-cite
47   :type '(choice (const :tag "none" nil)
48                  integer))
49
50 (defcustom gnus-cite-parse-max-size 25000
51   "Maximum article size (in bytes) where parsing citations is allowed.
52 Set it to nil to parse all articles."
53   :group 'gnus-cite
54   :type '(choice (const :tag "all" nil)
55                  integer))
56
57 (defcustom gnus-cite-prefix-regexp 
58     "^[]>|:}+ ]*[]>|:}+]\\(.*>\\)?\\|^.*>"
59   "Regexp matching the longest possible citation prefix on a line."
60   :group 'gnus-cite
61   :type 'regexp)
62
63 (defcustom gnus-cite-max-prefix 20
64   "Maximum possible length for a citation prefix."
65   :group 'gnus-cite
66   :type 'integer)
67
68 (defcustom gnus-supercite-regexp 
69   (concat "^\\(" gnus-cite-prefix-regexp "\\)? *"
70           ">>>>> +\"\\([^\"\n]+\\)\" +==")
71   "Regexp matching normal Supercite attribution lines.
72 The first grouping must match prefixes added by other packages."
73   :group 'gnus-cite
74   :type 'regexp)
75
76 (defcustom gnus-supercite-secondary-regexp "^.*\"\\([^\"\n]+\\)\" +=="
77   "Regexp matching mangled Supercite attribution lines.
78 The first regexp group should match the Supercite attribution."
79   :group 'gnus-cite
80   :type 'regexp)
81
82 (defcustom gnus-cite-minimum-match-count 2
83   "Minimum number of identical prefixes before we believe it's a citation."
84   :group 'gnus-cite
85   :type 'integer)
86
87 (defcustom gnus-cite-attribution-prefix "in article\\|in <"
88   "Regexp matching the beginning of an attribution line."
89   :group 'gnus-cite
90   :type 'regexp)
91
92 (defcustom gnus-cite-attribution-suffix
93   "\\(wrote\\|writes\\|said\\|says\\):[ \t]*$"
94   "Regexp matching the end of an attribution line.
95 The text matching the first grouping will be used as a button."
96   :group 'gnus-cite
97   :type 'regexp)
98
99 (defface gnus-cite-attribution-face '((t 
100                                        (:underline t)))
101   "Face used for attribution lines.")
102
103 (defcustom gnus-cite-attribution-face 'gnus-cite-attribution-face
104   "Face used for attribution lines.
105 It is merged with the face for the cited text belonging to the attribution."
106   :group 'gnus-cite
107   :type 'face)
108
109 (defface gnus-cite-face-1 '((((class color)
110                               (background dark))
111                              (:foreground "light blue"))
112                             (((class color)
113                               (background light))
114                              (:foreground "MidnightBlue"))
115                             (t 
116                              (:italic t)))
117   "Citation face.")
118
119 (defface gnus-cite-face-2 '((((class color)
120                               (background dark))
121                              (:foreground "light cyan"))
122                             (((class color)
123                               (background light))
124                              (:foreground "firebrick"))
125                             (t 
126                              (:italic t)))
127   "Citation face.")
128
129 (defface gnus-cite-face-3 '((((class color)
130                               (background dark))
131                              (:foreground "light yellow"))
132                             (((class color)
133                               (background light))
134                              (:foreground "dark green"))
135                             (t 
136                              (:italic t)))
137   "Citation face.")
138
139 (defface gnus-cite-face-4 '((((class color)
140                               (background dark))
141                              (:foreground "light pink"))
142                             (((class color)
143                               (background light))
144                              (:foreground "OrangeRed"))
145                             (t 
146                              (:italic t)))
147   "Citation face.")
148
149 (defface gnus-cite-face-5 '((((class color)
150                               (background dark))
151                              (:foreground "pale green"))
152                             (((class color)
153                               (background light))
154                              (:foreground "dark khaki"))
155                             (t 
156                              (:italic t)))
157   "Citation face.")
158
159 (defface gnus-cite-face-6 '((((class color)
160                               (background dark))
161                              (:foreground "beige"))
162                             (((class color)
163                               (background light))
164                              (:foreground "dark violet"))
165                             (t 
166                              (:italic t)))
167   "Citation face.")
168
169 (defface gnus-cite-face-7 '((((class color)
170                               (background dark))
171                              (:foreground "orange"))
172                             (((class color)
173                               (background light))
174                              (:foreground "SteelBlue4"))
175                             (t 
176                              (:italic t)))
177   "Citation face.")
178
179 (defface gnus-cite-face-8 '((((class color)
180                               (background dark))
181                              (:foreground "magenta"))
182                             (((class color)
183                               (background light))
184                              (:foreground "magenta"))
185                             (t 
186                              (:italic t)))
187   "Citation face.")
188
189 (defface gnus-cite-face-9 '((((class color)
190                               (background dark))
191                              (:foreground "violet"))
192                             (((class color)
193                               (background light))
194                              (:foreground "violet"))
195                             (t 
196                              (:italic t)))
197   "Citation face.")
198
199 (defface gnus-cite-face-10 '((((class color)
200                                (background dark))
201                               (:foreground "medium purple"))
202                              (((class color)
203                                (background light))
204                               (:foreground "medium purple"))
205                              (t 
206                               (:italic t)))
207   "Citation face.")
208
209 (defface gnus-cite-face-11 '((((class color)
210                                (background dark))
211                               (:foreground "turquoise"))
212                              (((class color)
213                                (background light))
214                               (:foreground "turquoise"))
215                              (t 
216                               (:italic t)))
217   "Citation face.")
218
219 (defcustom gnus-cite-face-list 
220   '(gnus-cite-face-1 gnus-cite-face-2 gnus-cite-face-3 gnus-cite-face-4 
221     gnus-cite-face-5 gnus-cite-face-6 gnus-cite-face-7 gnus-cite-face-8 
222     gnus-cite-face-9 gnus-cite-face-10 gnus-cite-face-11)
223   "List of faces used for highlighting citations. 
224
225 When there are citations from multiple articles in the same message,
226 Gnus will try to give each citation from each article its own face.
227 This should make it easier to see who wrote what."
228   :group 'gnus-cite
229   :type '(repeat face))
230
231 (defcustom gnus-cite-hide-percentage 50
232   "Only hide excess citation if above this percentage of the body."
233   :group 'gnus-cite
234   :type 'number)
235
236 (defcustom gnus-cite-hide-absolute 10
237   "Only hide excess citation if above this number of lines in the body."
238   :group 'gnus-cite
239   :type 'integer)
240
241 ;;; Internal Variables:
242
243 (defvar gnus-cite-article nil)
244
245 (defvar gnus-cite-prefix-alist nil)
246 ;; Alist of citation prefixes.  
247 ;; The cdr is a list of lines with that prefix.
248
249 (defvar gnus-cite-attribution-alist nil)
250 ;; Alist of attribution lines.
251 ;; The car is a line number.
252 ;; The cdr is the prefix for the citation started by that line.
253
254 (defvar gnus-cite-loose-prefix-alist nil)
255 ;; Alist of citation prefixes that have no matching attribution.
256 ;; The cdr is a list of lines with that prefix.
257
258 (defvar gnus-cite-loose-attribution-alist nil)
259 ;; Alist of attribution lines that have no matching citation.
260 ;; Each member has the form (WROTE IN PREFIX TAG), where
261 ;; WROTE: is the attribution line number
262 ;; IN: is the line number of the previous line if part of the same attribution,
263 ;; PREFIX: Is the citation prefix of the attribution line(s), and
264 ;; TAG: Is a Supercite tag, if any.
265
266 (defvar gnus-cited-text-button-line-format-alist 
267   `((?b (marker-position beg) ?d)
268     (?e (marker-position end) ?d)
269     (?l (- end beg) ?d)))
270 (defvar gnus-cited-text-button-line-format-spec nil)
271
272 ;;; Commands:
273
274 (defun gnus-article-highlight-citation (&optional force)
275   "Highlight cited text.
276 Each citation in the article will be highlighted with a different face.
277 The faces are taken from `gnus-cite-face-list'.
278 Attribution lines are highlighted with the same face as the
279 corresponding citation merged with `gnus-cite-attribution-face'.
280
281 Text is considered cited if at least `gnus-cite-minimum-match-count'
282 lines matches `gnus-cite-prefix-regexp' with the same prefix.  
283
284 Lines matching `gnus-cite-attribution-suffix' and perhaps
285 `gnus-cite-attribution-prefix' are considered attribution lines."
286   (interactive (list 'force))
287   (save-excursion
288     (set-buffer gnus-article-buffer)
289     (gnus-cite-parse-maybe force)
290     (let ((buffer-read-only nil)
291           (alist gnus-cite-prefix-alist)
292           (faces gnus-cite-face-list)
293           (inhibit-point-motion-hooks t)
294           face entry prefix skip numbers number face-alist)
295       ;; Loop through citation prefixes.
296       (while alist
297         (setq entry (car alist)
298               alist (cdr alist)
299               prefix (car entry)
300               numbers (cdr entry)
301               face (car faces)
302               faces (or (cdr faces) gnus-cite-face-list)
303               face-alist (cons (cons prefix face) face-alist))
304         (while numbers
305           (setq number (car numbers)
306                 numbers (cdr numbers))
307           (and (not (assq number gnus-cite-attribution-alist))
308                (not (assq number gnus-cite-loose-attribution-alist))
309                (gnus-cite-add-face number prefix face))))
310       ;; Loop through attribution lines.
311       (setq alist gnus-cite-attribution-alist)
312       (while alist
313         (setq entry (car alist)
314               alist (cdr alist)
315               number (car entry)
316               prefix (cdr entry)
317               skip (gnus-cite-find-prefix number)
318               face (cdr (assoc prefix face-alist)))
319         ;; Add attribution button.
320         (goto-line number)
321         (when (re-search-forward gnus-cite-attribution-suffix 
322                                  (save-excursion (end-of-line 1) (point))
323                                  t)
324           (gnus-article-add-button (match-beginning 1) (match-end 1)
325                                    'gnus-cite-toggle prefix))
326         ;; Highlight attribution line.
327         (gnus-cite-add-face number skip face)
328         (gnus-cite-add-face number skip gnus-cite-attribution-face))
329       ;; Loop through attribution lines.
330       (setq alist gnus-cite-loose-attribution-alist)
331       (while alist
332         (setq entry (car alist)
333               alist (cdr alist)
334               number (car entry)
335               skip (gnus-cite-find-prefix number))
336         (gnus-cite-add-face number skip gnus-cite-attribution-face)))))
337
338 (defun gnus-dissect-cited-text ()
339   "Dissect the article buffer looking for cited text."
340   (save-excursion
341     (set-buffer gnus-article-buffer)
342     (gnus-cite-parse-maybe)
343     (let ((alist gnus-cite-prefix-alist)
344           prefix numbers number marks m)
345       ;; Loop through citation prefixes.
346       (while alist
347         (setq numbers (pop alist)
348               prefix (pop numbers))
349         (while numbers
350           (setq number (pop numbers))
351           (goto-char (point-min))
352           (forward-line number)
353           (push (cons (point-marker) "") marks)
354           (while (and numbers
355                       (= (1- number) (car numbers)))
356             (setq number (pop numbers)))
357           (goto-char (point-min))
358           (forward-line (1- number))
359           (push (cons (point-marker) prefix) marks)))
360       ;; Skip to the beginning of the body.
361       (goto-char (point-min))
362       (search-forward "\n\n" nil t)
363       (push (cons (point-marker) "") marks)
364       ;; Find the end of the body.
365       (goto-char (point-max))
366       (gnus-article-search-signature)
367       (push (cons (point-marker) "") marks)
368       ;; Sort the marks.
369       (setq marks (sort marks (lambda (m1 m2) (< (car m1) (car m2)))))
370       (let ((omarks marks))
371         (setq marks nil)
372         (while (cdr omarks)
373           (if (= (caar omarks) (caadr omarks))
374               (progn
375                 (unless (equal (cdar omarks) "")
376                   (push (car omarks) marks))
377                 (unless (equal (cdadr omarks) "")
378                   (push (cadr omarks) marks))
379                 (unless (and (equal (cdar omarks) "")
380                              (equal (cdadr omarks) "")
381                              (not (cddr omarks)))
382                   (setq omarks (cdr omarks))))
383             (push (car omarks) marks))
384           (setq omarks (cdr omarks)))
385         (when (car omarks)
386           (push (car omarks) marks))
387         (setq marks (setq m (nreverse marks)))
388         (while (cddr m)
389           (if (and (equal (cdadr m) "")
390                    (equal (cdar m) (cdaddr m))
391                    (goto-char (caadr m))
392                    (forward-line 1)
393                    (= (point) (caaddr m)))
394               (setcdr m (cdddr m))
395             (setq m (cdr m))))
396         marks))))
397             
398 (defun gnus-article-fill-cited-article (&optional force)
399   "Do word wrapping in the current article."
400   (interactive (list t))
401   (save-excursion
402     (set-buffer gnus-article-buffer)
403     (let ((buffer-read-only nil)
404           (inhibit-point-motion-hooks t)
405           (marks (gnus-dissect-cited-text))
406           (adaptive-fill-mode nil))
407       (save-restriction
408         (while (cdr marks)
409           (widen)
410           (narrow-to-region (caar marks) (caadr marks))
411           (let ((adaptive-fill-regexp
412                  (concat "^" (regexp-quote (cdar marks)) " *"))
413                 (fill-prefix (cdar marks)))
414             (fill-region (point-min) (point-max)))
415           (set-marker (caar marks) nil)
416           (setq marks (cdr marks)))
417         (when marks
418           (set-marker (caar marks) nil))
419         ;; All this information is now incorrect.
420         (setq gnus-cite-prefix-alist nil
421               gnus-cite-attribution-alist nil
422               gnus-cite-loose-prefix-alist nil
423               gnus-cite-loose-attribution-alist nil)))))
424
425 (defun gnus-article-hide-citation (&optional arg force)
426   "Toggle hiding of all cited text except attribution lines.
427 See the documentation for `gnus-article-highlight-citation'.
428 If given a negative prefix, always show; if given a positive prefix,
429 always hide."
430   (interactive (append (gnus-article-hidden-arg) (list 'force)))
431   (setq gnus-cited-text-button-line-format-spec 
432         (gnus-parse-format gnus-cited-text-button-line-format 
433                            gnus-cited-text-button-line-format-alist t))
434   (save-excursion
435     (set-buffer gnus-article-buffer)
436     (cond
437      ((gnus-article-check-hidden-text 'cite arg)
438       t)
439      ((gnus-article-text-type-exists-p 'cite)
440       (let ((buffer-read-only nil))
441         (gnus-article-hide-text-of-type 'cite)))
442      (t
443       (let ((buffer-read-only nil)
444             (marks (gnus-dissect-cited-text))
445             (inhibit-point-motion-hooks t)
446             (props (nconc (list 'article-type 'cite)
447                           gnus-hidden-properties))
448             beg end)
449         (while marks
450           (setq beg nil
451                 end nil)
452           (while (and marks (string= (cdar marks) ""))
453             (setq marks (cdr marks)))
454           (when marks 
455             (setq beg (caar marks)))
456           (while (and marks (not (string= (cdar marks) "")))
457             (setq marks (cdr marks)))
458           (when marks
459             (setq end (caar marks)))
460           ;; Skip past lines we want to leave visible.
461           (when (and beg end gnus-cited-lines-visible)
462             (goto-char beg)
463             (forward-line gnus-cited-lines-visible)
464             (if (>= (point) end)
465                 (setq beg nil)
466               (setq beg (point-marker))))
467           (when (and beg end)
468             (gnus-add-text-properties beg end props)
469             (goto-char beg)
470             (unless (save-excursion (search-backward "\n\n" nil t))
471               (insert "\n"))
472             (put-text-property
473              (point)
474              (progn
475                (gnus-article-add-button
476                 (point)
477                 (progn (eval gnus-cited-text-button-line-format-spec) (point))
478                 `gnus-article-toggle-cited-text (cons beg end))
479                (point))
480              'article-type 'annotation)
481             (set-marker beg (point)))))))))
482
483 (defun gnus-article-toggle-cited-text (region)
484   "Toggle hiding the text in REGION."
485   (let (buffer-read-only)
486     (funcall
487      (if (text-property-any
488           (car region) (1- (cdr region))
489           (car gnus-hidden-properties) (cadr gnus-hidden-properties))
490          'remove-text-properties 'gnus-add-text-properties)
491      (car region) (cdr region) gnus-hidden-properties)))
492
493 (defun gnus-article-hide-citation-maybe (&optional arg force)
494   "Toggle hiding of cited text that has an attribution line.
495 If given a negative prefix, always show; if given a positive prefix,
496 always hide.
497 This will do nothing unless at least `gnus-cite-hide-percentage'
498 percent and at least `gnus-cite-hide-absolute' lines of the body is
499 cited text with attributions.  When called interactively, these two
500 variables are ignored.
501 See also the documentation for `gnus-article-highlight-citation'."
502   (interactive (append (gnus-article-hidden-arg) (list 'force)))
503   (unless (gnus-article-check-hidden-text 'cite arg)
504     (save-excursion
505       (set-buffer gnus-article-buffer)
506       (gnus-cite-parse-maybe force)
507       (goto-char (point-min))
508       (search-forward "\n\n" nil t)
509       (let ((start (point))
510             (atts gnus-cite-attribution-alist)
511             (buffer-read-only nil)
512             (inhibit-point-motion-hooks t)
513             (hiden 0)
514             total)
515         (goto-char (point-max))
516         (gnus-article-search-signature)
517         (setq total (count-lines start (point)))
518         (while atts
519           (setq hiden (+ hiden (length (cdr (assoc (cdar atts)
520                                                    gnus-cite-prefix-alist))))
521                 atts (cdr atts)))
522         (when (or force
523                   (and (> (* 100 hiden) (* gnus-cite-hide-percentage total))
524                        (> hiden gnus-cite-hide-absolute)))
525           (setq atts gnus-cite-attribution-alist)
526           (while atts
527             (setq total (cdr (assoc (cdar atts) gnus-cite-prefix-alist))
528                   atts (cdr atts))
529             (while total
530               (setq hiden (car total)
531                     total (cdr total))
532               (goto-line hiden)
533               (unless (assq hiden gnus-cite-attribution-alist)
534                 (gnus-add-text-properties 
535                  (point) (progn (forward-line 1) (point))
536                  (nconc (list 'article-type 'cite)
537                         gnus-hidden-properties))))))))))
538
539 (defun gnus-article-hide-citation-in-followups ()
540   "Hide cited text in non-root articles."
541   (interactive)
542   (save-excursion
543     (set-buffer gnus-article-buffer)
544     (let ((article (cdr gnus-article-current)))
545       (unless (save-excursion
546                 (set-buffer gnus-summary-buffer)
547                 (gnus-article-displayed-root-p article))
548         (gnus-article-hide-citation)))))
549
550 ;;; Internal functions:
551
552 (defun gnus-cite-parse-maybe (&optional force)
553   ;; Parse if the buffer has changes since last time.
554   (if (equal gnus-cite-article gnus-article-current)
555       ()
556     ;;Reset parser information.
557     (setq gnus-cite-prefix-alist nil
558           gnus-cite-attribution-alist nil
559           gnus-cite-loose-prefix-alist nil
560           gnus-cite-loose-attribution-alist nil)
561     ;; Parse if not too large.
562     (if (and (not force)
563              gnus-cite-parse-max-size
564              (> (buffer-size) gnus-cite-parse-max-size))
565         ()
566       (setq gnus-cite-article (cons (car gnus-article-current)
567                                     (cdr gnus-article-current)))
568       (gnus-cite-parse))))
569
570 (defun gnus-cite-parse ()
571   ;; Parse and connect citation prefixes and attribution lines.
572   
573   ;; Parse current buffer searching for citation prefixes.
574   (goto-char (point-min))
575   (unless (search-forward "\n\n" nil t)
576     (goto-char (point-max)))
577   (let ((line (1+ (count-lines (point-min) (point))))
578         (case-fold-search t)
579         (max (save-excursion
580                (goto-char (point-max))
581                (gnus-article-search-signature)
582                (point)))
583         alist entry start begin end numbers prefix)
584     ;; Get all potential prefixes in `alist'.
585     (while (< (point) max)
586       ;; Each line.
587       (setq begin (point)
588             end (progn (beginning-of-line 2) (point))
589             start end)
590       (goto-char begin)
591       ;; Ignore standard Supercite attribution prefix.
592       (when (looking-at gnus-supercite-regexp)
593         (if (match-end 1)
594             (setq end (1+ (match-end 1)))
595           (setq end (1+ begin))))
596       ;; Ignore very long prefixes.
597       (when (> end (+ (point) gnus-cite-max-prefix))
598         (setq end (+ (point) gnus-cite-max-prefix)))
599       (while (re-search-forward gnus-cite-prefix-regexp (1- end) t)
600         ;; Each prefix.
601         (setq end (match-end 0)
602               prefix (buffer-substring begin end))
603         (gnus-set-text-properties 0 (length prefix) nil prefix)
604         (setq entry (assoc prefix alist))
605         (if entry 
606             (setcdr entry (cons line (cdr entry)))
607           (push (list prefix line) alist))
608         (goto-char begin))
609       (goto-char start)
610       (setq line (1+ line)))
611     ;; We got all the potential prefixes.  Now create
612     ;; `gnus-cite-prefix-alist' containing the oldest prefix for each
613     ;; line that appears at least gnus-cite-minimum-match-count
614     ;; times.  First sort them by length.  Longer is older.
615     (setq alist (sort alist (lambda (a b)
616                               (> (length (car a)) (length (car b))))))
617     (while alist
618       (setq entry (car alist)
619             prefix (car entry)
620             numbers (cdr entry)
621             alist (cdr alist))
622       (cond ((null numbers)
623              ;; No lines with this prefix that wasn't also part of
624              ;; a longer prefix.
625              )
626             ((< (length numbers) gnus-cite-minimum-match-count)
627              ;; Too few lines with this prefix.  We keep it a bit
628              ;; longer in case it is an exact match for an attribution
629              ;; line, but we don't remove the line from other
630              ;; prefixes. 
631              (push entry gnus-cite-prefix-alist))
632             (t
633              (push entry
634                    gnus-cite-prefix-alist)
635              ;; Remove articles from other prefixes.
636              (let ((loop alist)
637                    current)
638                (while loop
639                  (setq current (car loop)
640                        loop (cdr loop))
641                  (setcdr current 
642                          (gnus-set-difference (cdr current) numbers))))))))
643   ;; No citations have been connected to attribution lines yet.
644   (setq gnus-cite-loose-prefix-alist (append gnus-cite-prefix-alist nil))
645
646   ;; Parse current buffer searching for attribution lines.
647   (goto-char (point-min))
648   (search-forward "\n\n" nil t)
649   (while (re-search-forward gnus-cite-attribution-suffix (point-max) t)
650     (let* ((start (match-beginning 0))
651            (end (match-end 0))
652            (wrote (count-lines (point-min) end))
653            (prefix (gnus-cite-find-prefix wrote))
654            ;; Check previous line for an attribution leader.
655            (tag (progn
656                   (beginning-of-line 1)
657                   (when (looking-at gnus-supercite-secondary-regexp)
658                     (buffer-substring (match-beginning 1)
659                                       (match-end 1)))))
660            (in (progn
661                  (goto-char start)
662                  (and (re-search-backward gnus-cite-attribution-prefix
663                                           (save-excursion
664                                             (beginning-of-line 0)
665                                             (point))
666                                           t)
667                       (not (re-search-forward gnus-cite-attribution-suffix
668                                               start t))
669                       (count-lines (point-min) (1+ (point)))))))
670       (when (eq wrote in)
671         (setq in nil))
672       (goto-char end)
673       (push (list wrote in prefix tag)
674             gnus-cite-loose-attribution-alist)))
675   ;; Find exact supercite citations.
676   (gnus-cite-match-attributions 'small nil
677                                 (lambda (prefix tag)
678                                   (when tag
679                                     (concat "\\`" 
680                                             (regexp-quote prefix) "[ \t]*" 
681                                             (regexp-quote tag) ">"))))
682   ;; Find loose supercite citations after attributions.
683   (gnus-cite-match-attributions 'small t
684                                 (lambda (prefix tag)
685                                   (when tag
686                                     (concat "\\<"
687                                             (regexp-quote tag)
688                                             "\\>"))))
689   ;; Find loose supercite citations anywhere.
690   (gnus-cite-match-attributions 'small nil
691                                 (lambda (prefix tag)
692                                   (when tag
693                                     (concat "\\<"
694                                             (regexp-quote tag)
695                                             "\\>"))))
696   ;; Find nested citations after attributions.
697   (gnus-cite-match-attributions 'small-if-unique t
698                                 (lambda (prefix tag)
699                                   (concat "\\`" (regexp-quote prefix) ".+")))
700   ;; Find nested citations anywhere.
701   (gnus-cite-match-attributions 'small nil
702                                 (lambda (prefix tag)
703                                   (concat "\\`" (regexp-quote prefix) ".+")))
704   ;; Remove loose prefixes with too few lines.
705   (let ((alist gnus-cite-loose-prefix-alist)
706         entry)
707     (while alist
708       (setq entry (car alist)
709             alist (cdr alist))
710       (when (< (length (cdr entry)) gnus-cite-minimum-match-count)
711         (setq gnus-cite-prefix-alist
712               (delq entry gnus-cite-prefix-alist)
713               gnus-cite-loose-prefix-alist
714               (delq entry gnus-cite-loose-prefix-alist)))))
715   ;; Find flat attributions.
716   (gnus-cite-match-attributions 'first t nil)
717   ;; Find any attributions (are we getting desperate yet?).
718   (gnus-cite-match-attributions 'first nil nil))
719
720 (defun gnus-cite-match-attributions (sort after fun)
721   ;; Match all loose attributions and citations (SORT AFTER FUN) .
722   ;;
723   ;; If SORT is `small', the citation with the shortest prefix will be
724   ;; used, if it is `first' the first prefix will be used, if it is
725   ;; `small-if-unique' the shortest prefix will be used if the
726   ;; attribution line does not share its own prefix with other
727   ;; loose attribution lines, otherwise the first prefix will be used.
728   ;;
729   ;; If AFTER is non-nil, only citations after the attribution line
730   ;; will be considered.
731   ;;
732   ;; If FUN is non-nil, it will be called with the arguments (WROTE
733   ;; PREFIX TAG) and expected to return a regular expression.  Only
734   ;; citations whose prefix matches the regular expression will be
735   ;; considered. 
736   ;; 
737   ;; WROTE is the attribution line number.
738   ;; PREFIX is the attribution line prefix.
739   ;; TAG is the Supercite tag on the attribution line.
740   (let ((atts gnus-cite-loose-attribution-alist)
741         (case-fold-search t)
742         att wrote in prefix tag regexp limit smallest best size)
743     (while atts
744       (setq att (car atts)
745             atts (cdr atts)
746             wrote (nth 0 att)
747             in (nth 1 att)
748             prefix (nth 2 att)
749             tag (nth 3 att)
750             regexp (if fun (funcall fun prefix tag) "")
751             size (cond ((eq sort 'small) t)
752                        ((eq sort 'first) nil)
753                        (t (< (length (gnus-cite-find-loose prefix)) 2)))
754             limit (if after wrote -1)
755             smallest 1000000                   
756             best nil)
757       (let ((cites gnus-cite-loose-prefix-alist)
758             cite candidate numbers first compare)
759         (while cites
760           (setq cite (car cites)
761                 cites (cdr cites)
762                 candidate (car cite)
763                 numbers (cdr cite)
764                 first (apply 'min numbers)
765                 compare (if size (length candidate) first))
766           (and (> first limit)
767                regexp
768                (string-match regexp candidate)
769                (< compare smallest)
770                (setq best cite
771                      smallest compare))))
772       (if (null best)
773           ()
774         (setq gnus-cite-loose-attribution-alist
775               (delq att gnus-cite-loose-attribution-alist))
776         (push (cons wrote (car best)) gnus-cite-attribution-alist)
777         (when in
778           (push (cons in (car best)) gnus-cite-attribution-alist))
779         (when (memq best gnus-cite-loose-prefix-alist)
780           (let ((loop gnus-cite-prefix-alist)
781                 (numbers (cdr best))
782                 current)
783             (setq gnus-cite-loose-prefix-alist
784                   (delq best gnus-cite-loose-prefix-alist))
785             (while loop
786               (setq current (car loop)
787                     loop (cdr loop))
788               (if (eq current best)
789                   ()
790                 (setcdr current (gnus-set-difference (cdr current) numbers))
791                 (when (null (cdr current))
792                   (setq gnus-cite-loose-prefix-alist
793                         (delq current gnus-cite-loose-prefix-alist)
794                         atts (delq current atts)))))))))))
795
796 (defun gnus-cite-find-loose (prefix)
797   ;; Return a list of loose attribution lines prefixed by PREFIX.
798   (let* ((atts gnus-cite-loose-attribution-alist)
799          att line lines)
800     (while atts
801       (setq att (car atts)
802             line (car att)
803             atts (cdr atts))
804       (when (string-equal (gnus-cite-find-prefix line) prefix)
805         (push line lines)))
806     lines))
807
808 (defun gnus-cite-add-face (number prefix face)
809   ;; At line NUMBER, ignore PREFIX and add FACE to the rest of the line.
810   (when face
811     (let ((inhibit-point-motion-hooks t)
812           from to)
813       (goto-line number)
814       (unless (eobp);; Sometimes things become confused.
815         (forward-char (length prefix))
816         (skip-chars-forward " \t")
817         (setq from (point))
818         (end-of-line 1)
819         (skip-chars-backward " \t")
820         (setq to (point))
821         (when (< from to)
822           (gnus-overlay-put (gnus-make-overlay from to) 'face face))))))
823
824 (defun gnus-cite-toggle (prefix)
825   (save-excursion
826     (set-buffer gnus-article-buffer)
827     (let ((buffer-read-only nil)
828           (numbers (cdr (assoc prefix gnus-cite-prefix-alist)))
829           (inhibit-point-motion-hooks t)
830           number)
831       (while numbers
832         (setq number (car numbers)
833               numbers (cdr numbers))
834         (goto-line number)
835         (cond ((get-text-property (point) 'invisible)
836                (remove-text-properties (point) (progn (forward-line 1) (point))
837                                        gnus-hidden-properties))
838               ((assq number gnus-cite-attribution-alist))
839               (t
840                (gnus-add-text-properties 
841                 (point) (progn (forward-line 1) (point))
842                 (nconc (list 'article-type 'cite)
843                        gnus-hidden-properties))))))))
844
845 (defun gnus-cite-find-prefix (line)
846   ;; Return citation prefix for LINE.
847   (let ((alist gnus-cite-prefix-alist)
848         (prefix "")
849         entry)
850     (while alist
851       (setq entry (car alist)
852             alist (cdr alist))
853       (when (memq line (cdr entry))
854         (setq prefix (car entry))))
855     prefix))
856
857 (gnus-add-shutdown 'gnus-cache-close 'gnus)
858
859 (defun gnus-cache-close ()
860   (setq gnus-cite-prefix-alist nil))
861
862 (gnus-ems-redefine)
863
864 (provide 'gnus-cite)
865
866 ;;; gnus-cite.el ends here