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