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