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