*** empty log message ***
[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
262 (defvar gnus-cite-prefix-alist nil)
263 ;; Alist of citation prefixes.
264 ;; The cdr is a list of lines with that prefix.
265
266 (defvar gnus-cite-attribution-alist nil)
267 ;; Alist of attribution lines.
268 ;; The car is a line number.
269 ;; The cdr is the prefix for the citation started by that line.
270
271 (defvar gnus-cite-loose-prefix-alist nil)
272 ;; Alist of citation prefixes that have no matching attribution.
273 ;; The cdr is a list of lines with that prefix.
274
275 (defvar gnus-cite-loose-attribution-alist nil)
276 ;; Alist of attribution lines that have no matching citation.
277 ;; Each member has the form (WROTE IN PREFIX TAG), where
278 ;; WROTE: is the attribution line number
279 ;; IN: is the line number of the previous line if part of the same attribution,
280 ;; PREFIX: Is the citation prefix of the attribution line(s), and
281 ;; TAG: Is a Supercite tag, if any.
282
283 (defvar gnus-cited-text-button-line-format-alist
284   `((?b (marker-position beg) ?d)
285     (?e (marker-position end) ?d)
286     (?l (- end beg) ?d)))
287 (defvar gnus-cited-text-button-line-format-spec nil)
288
289 ;;; Commands:
290
291 (defun gnus-article-highlight-citation (&optional force)
292   "Highlight cited text.
293 Each citation in the article will be highlighted with a different face.
294 The faces are taken from `gnus-cite-face-list'.
295 Attribution lines are highlighted with the same face as the
296 corresponding citation merged with `gnus-cite-attribution-face'.
297
298 Text is considered cited if at least `gnus-cite-minimum-match-count'
299 lines matches `gnus-cite-prefix-regexp' with the same prefix.
300
301 Lines matching `gnus-cite-attribution-suffix' and perhaps
302 `gnus-cite-attribution-prefix' are considered attribution lines."
303   (interactive (list 'force))
304   (save-excursion
305     (set-buffer gnus-article-buffer)
306     (gnus-cite-parse-maybe force)
307     (let ((buffer-read-only nil)
308           (alist gnus-cite-prefix-alist)
309           (faces gnus-cite-face-list)
310           (inhibit-point-motion-hooks t)
311           face entry prefix skip numbers number face-alist)
312       ;; Loop through citation prefixes.
313       (while alist
314         (setq entry (car alist)
315               alist (cdr alist)
316               prefix (car entry)
317               numbers (cdr entry)
318               face (car faces)
319               faces (or (cdr faces) gnus-cite-face-list)
320               face-alist (cons (cons prefix face) face-alist))
321         (while numbers
322           (setq number (car numbers)
323                 numbers (cdr numbers))
324           (and (not (assq number gnus-cite-attribution-alist))
325                (not (assq number gnus-cite-loose-attribution-alist))
326                (gnus-cite-add-face number prefix face))))
327       ;; Loop through attribution lines.
328       (setq alist gnus-cite-attribution-alist)
329       (while alist
330         (setq entry (car alist)
331               alist (cdr alist)
332               number (car entry)
333               prefix (cdr entry)
334               skip (gnus-cite-find-prefix number)
335               face (cdr (assoc prefix face-alist)))
336         ;; Add attribution button.
337         (goto-line number)
338         (when (re-search-forward gnus-cite-attribution-suffix
339                                  (save-excursion (end-of-line 1) (point))
340                                  t)
341           (gnus-article-add-button (match-beginning 1) (match-end 1)
342                                    'gnus-cite-toggle prefix))
343         ;; Highlight attribution line.
344         (gnus-cite-add-face number skip face)
345         (gnus-cite-add-face number skip gnus-cite-attribution-face))
346       ;; Loop through attribution lines.
347       (setq alist gnus-cite-loose-attribution-alist)
348       (while alist
349         (setq entry (car alist)
350               alist (cdr alist)
351               number (car entry)
352               skip (gnus-cite-find-prefix number))
353         (gnus-cite-add-face number skip gnus-cite-attribution-face)))))
354
355 (defun gnus-dissect-cited-text ()
356   "Dissect the article buffer looking for cited text."
357   (save-excursion
358     (set-buffer gnus-article-buffer)
359     (gnus-cite-parse-maybe)
360     (let ((alist gnus-cite-prefix-alist)
361           prefix numbers number marks m)
362       ;; Loop through citation prefixes.
363       (while alist
364         (setq numbers (pop alist)
365               prefix (pop numbers))
366         (while numbers
367           (setq number (pop numbers))
368           (goto-char (point-min))
369           (forward-line number)
370           (push (cons (point-marker) "") marks)
371           (while (and numbers
372                       (= (1- number) (car numbers)))
373             (setq number (pop numbers)))
374           (goto-char (point-min))
375           (forward-line (1- number))
376           (push (cons (point-marker) prefix) marks)))
377       ;; Skip to the beginning of the body.
378       (goto-char (point-min))
379       (search-forward "\n\n" nil t)
380       (push (cons (point-marker) "") marks)
381       ;; Find the end of the body.
382       (goto-char (point-max))
383       (gnus-article-search-signature)
384       (push (cons (point-marker) "") marks)
385       ;; Sort the marks.
386       (setq marks (sort marks 'car-less-than-car))
387       (let ((omarks marks))
388         (setq marks nil)
389         (while (cdr omarks)
390           (if (= (caar omarks) (caadr omarks))
391               (progn
392                 (unless (equal (cdar omarks) "")
393                   (push (car omarks) marks))
394                 (unless (equal (cdadr omarks) "")
395                   (push (cadr omarks) marks))
396                 (unless (and (equal (cdar omarks) "")
397                              (equal (cdadr omarks) "")
398                              (not (cddr omarks)))
399                   (setq omarks (cdr omarks))))
400             (push (car omarks) marks))
401           (setq omarks (cdr omarks)))
402         (when (car omarks)
403           (push (car omarks) marks))
404         (setq marks (setq m (nreverse marks)))
405         (while (cddr m)
406           (if (and (equal (cdadr m) "")
407                    (equal (cdar m) (cdaddr m))
408                    (goto-char (caadr m))
409                    (forward-line 1)
410                    (= (point) (caaddr m)))
411               (setcdr m (cdddr m))
412             (setq m (cdr m))))
413         marks))))
414
415 (defun gnus-article-fill-cited-article (&optional force width)
416   "Do word wrapping in the current article.
417 If WIDTH (the numerical prefix), use that text width when filling."
418   (interactive (list t current-prefix-arg))
419   (save-excursion
420     (set-buffer gnus-article-buffer)
421     (let ((buffer-read-only nil)
422           (inhibit-point-motion-hooks t)
423           (marks (gnus-dissect-cited-text))
424           (adaptive-fill-mode nil)
425           (filladapt-mode nil)
426           (fill-column (if width (prefix-numeric-value width) fill-column)))
427       (save-restriction
428         (while (cdr marks)
429           (widen)
430           (narrow-to-region (caar marks) (caadr marks))
431           (let ((adaptive-fill-regexp
432                  (concat "^" (regexp-quote (cdar marks)) " *"))
433                 (fill-prefix (cdar marks)))
434             (fill-region (point-min) (point-max)))
435           (set-marker (caar marks) nil)
436           (setq marks (cdr marks)))
437         (when marks
438           (set-marker (caar marks) nil))
439         ;; All this information is now incorrect.
440         (setq gnus-cite-prefix-alist nil
441               gnus-cite-attribution-alist nil
442               gnus-cite-loose-prefix-alist nil
443               gnus-cite-loose-attribution-alist nil
444               gnus-cite-article nil)))))
445
446 (defun gnus-article-hide-citation (&optional arg force)
447   "Toggle hiding of all cited text except attribution lines.
448 See the documentation for `gnus-article-highlight-citation'.
449 If given a negative prefix, always show; if given a positive prefix,
450 always hide."
451   (interactive (append (gnus-article-hidden-arg) (list 'force)))
452   (gnus-set-format 'cited-text-button t)
453   (save-excursion
454     (set-buffer gnus-article-buffer)
455     (cond
456      ((gnus-article-check-hidden-text 'cite arg)
457       t)
458      ((gnus-article-text-type-exists-p 'cite)
459       (let ((buffer-read-only nil))
460         (gnus-article-hide-text-of-type 'cite)))
461      (t
462       (let ((buffer-read-only nil)
463             (marks (gnus-dissect-cited-text))
464             (inhibit-point-motion-hooks t)
465             (props (nconc (list 'article-type 'cite)
466                           gnus-hidden-properties))
467             beg end)
468         (while marks
469           (setq beg nil
470                 end nil)
471           (while (and marks (string= (cdar marks) ""))
472             (setq marks (cdr marks)))
473           (when marks
474             (setq beg (caar marks)))
475           (while (and marks (not (string= (cdar marks) "")))
476             (setq marks (cdr marks)))
477           (when marks
478             (setq end (caar marks)))
479           ;; Skip past lines we want to leave visible.
480           (when (and beg end gnus-cited-lines-visible)
481             (goto-char beg)
482             (forward-line gnus-cited-lines-visible)
483             (if (>= (point) end)
484                 (setq beg nil)
485               (setq beg (point-marker))))
486           (when (and beg 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 (cons beg end))
498                (point))
499              'article-type 'annotation)
500             (set-marker beg (point)))))))))
501
502 (defun gnus-article-toggle-cited-text (region)
503   "Toggle hiding the text in REGION."
504   (let (buffer-read-only)
505     (funcall
506      (if (text-property-any
507           (car region) (1- (cdr region))
508           (car gnus-hidden-properties) (cadr gnus-hidden-properties))
509          'remove-text-properties 'gnus-add-text-properties)
510      (car region) (cdr region) gnus-hidden-properties)))
511
512 (defun gnus-article-hide-citation-maybe (&optional arg force)
513   "Toggle hiding of cited text that has an attribution line.
514 If given a negative prefix, always show; if given a positive prefix,
515 always hide.
516 This will do nothing unless at least `gnus-cite-hide-percentage'
517 percent and at least `gnus-cite-hide-absolute' lines of the body is
518 cited text with attributions.  When called interactively, these two
519 variables are ignored.
520 See also the documentation for `gnus-article-highlight-citation'."
521   (interactive (append (gnus-article-hidden-arg) (list 'force)))
522   (unless (gnus-article-check-hidden-text 'cite arg)
523     (save-excursion
524       (set-buffer gnus-article-buffer)
525       (gnus-cite-parse-maybe force)
526       (goto-char (point-min))
527       (search-forward "\n\n" nil t)
528       (let ((start (point))
529             (atts gnus-cite-attribution-alist)
530             (buffer-read-only nil)
531             (inhibit-point-motion-hooks t)
532             (hiden 0)
533             total)
534         (goto-char (point-max))
535         (gnus-article-search-signature)
536         (setq total (count-lines start (point)))
537         (while atts
538           (setq hiden (+ hiden (length (cdr (assoc (cdar atts)
539                                                    gnus-cite-prefix-alist))))
540                 atts (cdr atts)))
541         (when (or force
542                   (and (> (* 100 hiden) (* gnus-cite-hide-percentage total))
543                        (> hiden gnus-cite-hide-absolute)))
544           (setq atts gnus-cite-attribution-alist)
545           (while atts
546             (setq total (cdr (assoc (cdar atts) gnus-cite-prefix-alist))
547                   atts (cdr atts))
548             (while total
549               (setq hiden (car total)
550                     total (cdr total))
551               (goto-line hiden)
552               (unless (assq hiden gnus-cite-attribution-alist)
553                 (gnus-add-text-properties
554                  (point) (progn (forward-line 1) (point))
555                  (nconc (list 'article-type 'cite)
556                         gnus-hidden-properties))))))))))
557
558 (defun gnus-article-hide-citation-in-followups ()
559   "Hide cited text in non-root articles."
560   (interactive)
561   (save-excursion
562     (set-buffer gnus-article-buffer)
563     (let ((article (cdr gnus-article-current)))
564       (unless (save-excursion
565                 (set-buffer gnus-summary-buffer)
566                 (gnus-article-displayed-root-p article))
567         (gnus-article-hide-citation)))))
568
569 ;;; Internal functions:
570
571 (defun gnus-cite-parse-maybe (&optional force)
572   ;; Parse if the buffer has changes since last time.
573   (if (equal gnus-cite-article gnus-article-current)
574       ()
575     ;;Reset parser information.
576     (setq gnus-cite-prefix-alist nil
577           gnus-cite-attribution-alist nil
578           gnus-cite-loose-prefix-alist nil
579           gnus-cite-loose-attribution-alist nil)
580     ;; Parse if not too large.
581     (if (and (not force)
582              gnus-cite-parse-max-size
583              (> (buffer-size) gnus-cite-parse-max-size))
584         ()
585       (setq gnus-cite-article (cons (car gnus-article-current)
586                                     (cdr gnus-article-current)))
587       (gnus-cite-parse-wrapper))))
588
589 (defun gnus-cite-parse-wrapper ()
590   ;; Wrap chopped gnus-cite-parse
591   (goto-char (point-min))
592   (unless (search-forward "\n\n" nil t)
593     (goto-char (point-max)))
594   (save-excursion
595     (gnus-cite-parse-attributions))
596   ;; Try to avoid check citation if there is no reason to believe
597   ;; that article has citations
598   (if (or gnus-cite-always-check
599           (save-excursion
600             (re-search-backward gnus-cite-reply-regexp nil t))
601           gnus-cite-loose-attribution-alist)
602       (progn (save-excursion
603                (gnus-cite-parse))
604              (save-excursion
605                (gnus-cite-connect-attributions)))))
606
607 (defun gnus-cite-parse ()
608   ;; Parse and connect citation prefixes and attribution lines.
609
610   ;; Parse current buffer searching for citation prefixes.
611   (let ((line (1+ (count-lines (point-min) (point))))
612         (case-fold-search t)
613         (max (save-excursion
614                (goto-char (point-max))
615                (gnus-article-search-signature)
616                (point)))
617         alist entry start begin end numbers prefix)
618     ;; Get all potential prefixes in `alist'.
619     (while (< (point) max)
620       ;; Each line.
621       (setq begin (point)
622             end (progn (beginning-of-line 2) (point))
623             start end)
624       (goto-char begin)
625       ;; Ignore standard Supercite attribution prefix.
626       (when (looking-at gnus-supercite-regexp)
627         (if (match-end 1)
628             (setq end (1+ (match-end 1)))
629           (setq end (1+ begin))))
630       ;; Ignore very long prefixes.
631       (when (> end (+ (point) gnus-cite-max-prefix))
632         (setq end (+ (point) gnus-cite-max-prefix)))
633       (while (re-search-forward gnus-cite-prefix-regexp (1- end) t)
634         ;; Each prefix.
635         (setq end (match-end 0)
636               prefix (buffer-substring begin end))
637         (gnus-set-text-properties 0 (length prefix) nil prefix)
638         (setq entry (assoc prefix alist))
639         (if entry
640             (setcdr entry (cons line (cdr entry)))
641           (push (list prefix line) alist))
642         (goto-char begin))
643       (goto-char start)
644       (setq line (1+ line)))
645     ;; We got all the potential prefixes.  Now create
646     ;; `gnus-cite-prefix-alist' containing the oldest prefix for each
647     ;; line that appears at least gnus-cite-minimum-match-count
648     ;; times.  First sort them by length.  Longer is older.
649     (setq alist (sort alist (lambda (a b)
650                               (> (length (car a)) (length (car b))))))
651     (while alist
652       (setq entry (car alist)
653             prefix (car entry)
654             numbers (cdr entry)
655             alist (cdr alist))
656       (cond ((null numbers)
657              ;; No lines with this prefix that wasn't also part of
658              ;; a longer prefix.
659              )
660             ((< (length numbers) gnus-cite-minimum-match-count)
661              ;; Too few lines with this prefix.  We keep it a bit
662              ;; longer in case it is an exact match for an attribution
663              ;; line, but we don't remove the line from other
664              ;; prefixes.
665              (push entry gnus-cite-prefix-alist))
666             (t
667              (push entry
668                    gnus-cite-prefix-alist)
669              ;; Remove articles from other prefixes.
670              (let ((loop alist)
671                    current)
672                (while loop
673                  (setq current (car loop)
674                        loop (cdr loop))
675                  (setcdr current
676                          (gnus-set-difference (cdr current) numbers)))))))))
677
678 (defun gnus-cite-parse-attributions ()
679   (let (al-alist)
680     ;; Parse attributions
681     (while (re-search-forward gnus-cite-attribution-suffix (point-max) t)
682       (let* ((start (match-beginning 0))
683              (end (match-end 0))
684              (wrote (count-lines (point-min) end))
685              (prefix (gnus-cite-find-prefix wrote))
686              ;; Check previous line for an attribution leader.
687              (tag (progn
688                     (beginning-of-line 1)
689                     (when (looking-at gnus-supercite-secondary-regexp)
690                       (buffer-substring (match-beginning 1)
691                                         (match-end 1)))))
692              (in (progn
693                    (goto-char start)
694                    (and (re-search-backward gnus-cite-attribution-prefix
695                                             (save-excursion
696                                               (beginning-of-line 0)
697                                               (point))
698                                             t)
699                         (not (re-search-forward gnus-cite-attribution-suffix
700                                                 start t))
701                         (count-lines (point-min) (1+ (point)))))))
702         (when (eq wrote in)
703           (setq in nil))
704         (goto-char end)
705         ;; don't add duplicates
706         (let ((al (buffer-substring (save-excursion (beginning-of-line 0)
707                                                     (1+ (point)))
708                                     end)))
709           (if (not (assoc al al-alist))
710               (progn
711                 (push (list wrote in prefix tag)
712                       gnus-cite-loose-attribution-alist)
713                 (push (cons al t) al-alist))))))))
714
715 (defun gnus-cite-connect-attributions ()
716   ;; Connect attributions to citations
717
718   ;; No citations have been connected to attribution lines yet.
719   (setq gnus-cite-loose-prefix-alist (append gnus-cite-prefix-alist nil))
720
721   ;; Parse current buffer searching for attribution lines.
722   ;; Find exact supercite citations.
723   (gnus-cite-match-attributions 'small nil
724                                 (lambda (prefix tag)
725                                   (when tag
726                                     (concat "\\`"
727                                             (regexp-quote prefix) "[ \t]*"
728                                             (regexp-quote tag) ">"))))
729   ;; Find loose supercite citations after attributions.
730   (gnus-cite-match-attributions 'small t
731                                 (lambda (prefix tag)
732                                   (when tag
733                                     (concat "\\<"
734                                             (regexp-quote tag)
735                                             "\\>"))))
736   ;; Find loose supercite citations anywhere.
737   (gnus-cite-match-attributions 'small nil
738                                 (lambda (prefix tag)
739                                   (when tag
740                                     (concat "\\<"
741                                             (regexp-quote tag)
742                                             "\\>"))))
743   ;; Find nested citations after attributions.
744   (gnus-cite-match-attributions 'small-if-unique t
745                                 (lambda (prefix tag)
746                                   (concat "\\`" (regexp-quote prefix) ".+")))
747   ;; Find nested citations anywhere.
748   (gnus-cite-match-attributions 'small nil
749                                 (lambda (prefix tag)
750                                   (concat "\\`" (regexp-quote prefix) ".+")))
751   ;; Remove loose prefixes with too few lines.
752   (let ((alist gnus-cite-loose-prefix-alist)
753         entry)
754     (while alist
755       (setq entry (car alist)
756             alist (cdr alist))
757       (when (< (length (cdr entry)) gnus-cite-minimum-match-count)
758         (setq gnus-cite-prefix-alist
759               (delq entry gnus-cite-prefix-alist)
760               gnus-cite-loose-prefix-alist
761               (delq entry gnus-cite-loose-prefix-alist)))))
762   ;; Find flat attributions.
763   (gnus-cite-match-attributions 'first t nil)
764   ;; Find any attributions (are we getting desperate yet?).
765   (gnus-cite-match-attributions 'first nil nil))
766
767 (defun gnus-cite-match-attributions (sort after fun)
768   ;; Match all loose attributions and citations (SORT AFTER FUN) .
769   ;;
770   ;; If SORT is `small', the citation with the shortest prefix will be
771   ;; used, if it is `first' the first prefix will be used, if it is
772   ;; `small-if-unique' the shortest prefix will be used if the
773   ;; attribution line does not share its own prefix with other
774   ;; loose attribution lines, otherwise the first prefix will be used.
775   ;;
776   ;; If AFTER is non-nil, only citations after the attribution line
777   ;; will be considered.
778   ;;
779   ;; If FUN is non-nil, it will be called with the arguments (WROTE
780   ;; PREFIX TAG) and expected to return a regular expression.  Only
781   ;; citations whose prefix matches the regular expression will be
782   ;; considered.
783   ;;
784   ;; WROTE is the attribution line number.
785   ;; PREFIX is the attribution line prefix.
786   ;; TAG is the Supercite tag on the attribution line.
787   (let ((atts gnus-cite-loose-attribution-alist)
788         (case-fold-search t)
789         att wrote in prefix tag regexp limit smallest best size)
790     (while atts
791       (setq att (car atts)
792             atts (cdr atts)
793             wrote (nth 0 att)
794             in (nth 1 att)
795             prefix (nth 2 att)
796             tag (nth 3 att)
797             regexp (if fun (funcall fun prefix tag) "")
798             size (cond ((eq sort 'small) t)
799                        ((eq sort 'first) nil)
800                        (t (< (length (gnus-cite-find-loose prefix)) 2)))
801             limit (if after wrote -1)
802             smallest 1000000
803             best nil)
804       (let ((cites gnus-cite-loose-prefix-alist)
805             cite candidate numbers first compare)
806         (while cites
807           (setq cite (car cites)
808                 cites (cdr cites)
809                 candidate (car cite)
810                 numbers (cdr cite)
811                 first (apply 'min numbers)
812                 compare (if size (length candidate) first))
813           (and (> first limit)
814                regexp
815                (string-match regexp candidate)
816                (< compare smallest)
817                (setq best cite
818                      smallest compare))))
819       (if (null best)
820           ()
821         (setq gnus-cite-loose-attribution-alist
822               (delq att gnus-cite-loose-attribution-alist))
823         (push (cons wrote (car best)) gnus-cite-attribution-alist)
824         (when in
825           (push (cons in (car best)) gnus-cite-attribution-alist))
826         (when (memq best gnus-cite-loose-prefix-alist)
827           (let ((loop gnus-cite-prefix-alist)
828                 (numbers (cdr best))
829                 current)
830             (setq gnus-cite-loose-prefix-alist
831                   (delq best gnus-cite-loose-prefix-alist))
832             (while loop
833               (setq current (car loop)
834                     loop (cdr loop))
835               (if (eq current best)
836                   ()
837                 (setcdr current (gnus-set-difference (cdr current) numbers))
838                 (when (null (cdr current))
839                   (setq gnus-cite-loose-prefix-alist
840                         (delq current gnus-cite-loose-prefix-alist)
841                         atts (delq current atts)))))))))))
842
843 (defun gnus-cite-find-loose (prefix)
844   ;; Return a list of loose attribution lines prefixed by PREFIX.
845   (let* ((atts gnus-cite-loose-attribution-alist)
846          att line lines)
847     (while atts
848       (setq att (car atts)
849             line (car att)
850             atts (cdr atts))
851       (when (string-equal (gnus-cite-find-prefix line) prefix)
852         (push line lines)))
853     lines))
854
855 (defun gnus-cite-add-face (number prefix face)
856   ;; At line NUMBER, ignore PREFIX and add FACE to the rest of the line.
857   (when face
858     (let ((inhibit-point-motion-hooks t)
859           from to)
860       (goto-line number)
861       (unless (eobp);; Sometimes things become confused.
862         (forward-char (length prefix))
863         (skip-chars-forward " \t")
864         (setq from (point))
865         (end-of-line 1)
866         (skip-chars-backward " \t")
867         (setq to (point))
868         (when (< from to)
869           (gnus-overlay-put (gnus-make-overlay from to) 'face face))))))
870
871 (defun gnus-cite-toggle (prefix)
872   (save-excursion
873     (set-buffer gnus-article-buffer)
874     (let ((buffer-read-only nil)
875           (numbers (cdr (assoc prefix gnus-cite-prefix-alist)))
876           (inhibit-point-motion-hooks t)
877           number)
878       (while numbers
879         (setq number (car numbers)
880               numbers (cdr numbers))
881         (goto-line number)
882         (cond ((get-text-property (point) 'invisible)
883                (remove-text-properties (point) (progn (forward-line 1) (point))
884                                        gnus-hidden-properties))
885               ((assq number gnus-cite-attribution-alist))
886               (t
887                (gnus-add-text-properties
888                 (point) (progn (forward-line 1) (point))
889                 (nconc (list 'article-type 'cite)
890                        gnus-hidden-properties))))))))
891
892 (defun gnus-cite-find-prefix (line)
893   ;; Return citation prefix for LINE.
894   (let ((alist gnus-cite-prefix-alist)
895         (prefix "")
896         entry)
897     (while alist
898       (setq entry (car alist)
899             alist (cdr alist))
900       (when (memq line (cdr entry))
901         (setq prefix (car entry))))
902     prefix))
903
904 (gnus-add-shutdown 'gnus-cache-close 'gnus)
905
906 (defun gnus-cache-close ()
907   (setq gnus-cite-prefix-alist nil))
908
909 (gnus-ems-redefine)
910
911 (provide 'gnus-cite)
912
913 ;;; gnus-cite.el ends here