7aa68ab1b2b0f1d52b2fa2739cfecd80613c5ff5
[gnus] / 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 (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     (let ((buffer-read-only nil)
882           (numbers (cdr (assoc prefix gnus-cite-prefix-alist)))
883           (inhibit-point-motion-hooks t)
884           number)
885       (while numbers
886         (setq number (car numbers)
887               numbers (cdr numbers))
888         (goto-line number)
889         (cond ((get-text-property (point) 'invisible)
890                (remove-text-properties (point) (progn (forward-line 1) (point))
891                                        gnus-hidden-properties))
892               ((assq number gnus-cite-attribution-alist))
893               (t
894                (gnus-add-text-properties
895                 (point) (progn (forward-line 1) (point))
896                 (nconc (list 'article-type 'cite)
897                        gnus-hidden-properties))))))))
898
899 (defun gnus-cite-find-prefix (line)
900   ;; Return citation prefix for LINE.
901   (let ((alist gnus-cite-prefix-alist)
902         (prefix "")
903         entry)
904     (while alist
905       (setq entry (car alist)
906             alist (cdr alist))
907       (when (memq line (cdr entry))
908         (setq prefix (car entry))))
909     prefix))
910
911 (defun gnus-cite-localize ()
912   "Make the citation variables local to the article buffer."
913   (let ((vars '(gnus-cite-article
914                 gnus-cite-overlay-list gnus-cite-prefix-alist
915                 gnus-cite-attribution-alist gnus-cite-loose-prefix-alist
916                 gnus-cite-loose-attribution-alist)))
917     (while vars
918       (make-local-variable (pop vars)))))
919
920 (gnus-ems-redefine)
921
922 (provide 'gnus-cite)
923
924 ;;; gnus-cite.el ends here