*** 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 (defvar gnus-cited-text-button-line-format "%(%{[...]%}%)\n"
36   "Format of cited text buttons.")
37
38 (defvar gnus-cited-lines-visible nil
39   "The number of lines of hidden cited text to remain visible.")
40
41 (defvar gnus-cite-parse-max-size 25000
42   "Maximum article size (in bytes) where parsing citations is allowed.
43 Set it to nil to parse all articles.")
44
45 (defvar gnus-cite-prefix-regexp 
46     "^[]>|:}+ ]*[]>|:}+]\\(.*>\\)?\\|^.*>"
47   "Regexp matching the longest possible citation prefix on a line.")
48
49 (defvar gnus-cite-max-prefix 20
50   "Maximum possible length for a citation prefix.")
51
52 (defvar gnus-supercite-regexp 
53   (concat "^\\(" gnus-cite-prefix-regexp "\\)? *"
54           ">>>>> +\"\\([^\"\n]+\\)\" +==")
55   "Regexp matching normal Supercite attribution lines.
56 The first grouping must match prefixes added by other packages.")
57
58 (defvar gnus-supercite-secondary-regexp "^.*\"\\([^\"\n]+\\)\" +=="
59   "Regexp matching mangled Supercite attribution lines.
60 The first regexp group should match the Supercite attribution.")
61
62 (defvar gnus-cite-minimum-match-count 2
63   "Minimum number of identical prefixes before we believe it's a citation.")
64
65 (defvar gnus-cite-attribution-prefix "in article\\|in <"
66   "Regexp matching the beginning of an attribution line.")
67
68 (defvar gnus-cite-attribution-suffix
69   "\\(wrote\\|writes\\|said\\|says\\):[ \t]*$"
70   "Regexp matching the end of an attribution line.
71 The text matching the first grouping will be used as a button.")
72
73 (defvar gnus-cite-attribution-face 'underline
74   "Face used for attribution lines.
75 It is merged with the face for the cited text belonging to the attribution.")
76
77 (defvar gnus-cite-face-list 
78   (cond ((not (eq gnus-display-type 'color))
79          '(italic))
80         ((eq gnus-background-mode 'dark)
81          (mapcar 'gnus-make-face 
82                  gnus-face-light-name-list))
83         (t 
84          (mapcar 'gnus-make-face 
85                  gnus-face-dark-name-list)))
86   "List of faces used for highlighting citations. 
87
88 When there are citations from multiple articles in the same message,
89 Gnus will try to give each citation from each article its own face.
90 This should make it easier to see who wrote what.")
91
92 (defvar gnus-cite-hide-percentage 50
93   "Only hide excess citation if above this percentage of the body.")
94
95 (defvar gnus-cite-hide-absolute 10
96   "Only hide excess citation if above this number of lines in the body.")
97
98 ;;; Internal Variables:
99
100 (defvar gnus-cite-article nil)
101
102 (defvar gnus-cite-prefix-alist nil)
103 ;; Alist of citation prefixes.  
104 ;; The cdr is a list of lines with that prefix.
105
106 (defvar gnus-cite-attribution-alist nil)
107 ;; Alist of attribution lines.
108 ;; The car is a line number.
109 ;; The cdr is the prefix for the citation started by that line.
110
111 (defvar gnus-cite-loose-prefix-alist nil)
112 ;; Alist of citation prefixes that have no matching attribution.
113 ;; The cdr is a list of lines with that prefix.
114
115 (defvar gnus-cite-loose-attribution-alist nil)
116 ;; Alist of attribution lines that have no matching citation.
117 ;; Each member has the form (WROTE IN PREFIX TAG), where
118 ;; WROTE: is the attribution line number
119 ;; IN: is the line number of the previous line if part of the same attribution,
120 ;; PREFIX: Is the citation prefix of the attribution line(s), and
121 ;; TAG: Is a Supercite tag, if any.
122
123 (defvar gnus-cited-text-button-line-format-alist 
124   `((?b beg ?d)
125     (?e end ?d)
126     (?l (- end beg) ?d)))
127 (defvar gnus-cited-text-button-line-format-spec nil)
128
129 ;;; Commands:
130
131 (defun gnus-article-highlight-citation (&optional force)
132   "Highlight cited text.
133 Each citation in the article will be highlighted with a different face.
134 The faces are taken from `gnus-cite-face-list'.
135 Attribution lines are highlighted with the same face as the
136 corresponding citation merged with `gnus-cite-attribution-face'.
137
138 Text is considered cited if at least `gnus-cite-minimum-match-count'
139 lines matches `gnus-cite-prefix-regexp' with the same prefix.  
140
141 Lines matching `gnus-cite-attribution-suffix' and perhaps
142 `gnus-cite-attribution-prefix' are considered attribution lines."
143   (interactive (list 'force))
144   ;; Create dark or light faces if necessary.
145   (cond ((eq gnus-cite-face-list 'light)
146          (setq gnus-cite-face-list
147                (mapcar 'gnus-make-face gnus-face-light-name-list)))
148         ((eq gnus-cite-face-list 'dark)
149          (setq gnus-cite-face-list
150                (mapcar 'gnus-make-face gnus-face-dark-name-list))))
151   (save-excursion
152     (set-buffer gnus-article-buffer)
153     (gnus-cite-parse-maybe force)
154     (let ((buffer-read-only nil)
155           (alist gnus-cite-prefix-alist)
156           (faces gnus-cite-face-list)
157           (inhibit-point-motion-hooks t)
158           face entry prefix skip numbers number face-alist)
159       ;; Loop through citation prefixes.
160       (while alist
161         (setq entry (car alist)
162               alist (cdr alist)
163               prefix (car entry)
164               numbers (cdr entry)
165               face (car faces)
166               faces (or (cdr faces) gnus-cite-face-list)
167               face-alist (cons (cons prefix face) face-alist))
168         (while numbers
169           (setq number (car numbers)
170                 numbers (cdr numbers))
171           (and (not (assq number gnus-cite-attribution-alist))
172                (not (assq number gnus-cite-loose-attribution-alist))
173                (gnus-cite-add-face number prefix face))))
174       ;; Loop through attribution lines.
175       (setq alist gnus-cite-attribution-alist)
176       (while alist
177         (setq entry (car alist)
178               alist (cdr alist)
179               number (car entry)
180               prefix (cdr entry)
181               skip (gnus-cite-find-prefix number)
182               face (cdr (assoc prefix face-alist)))
183         ;; Add attribution button.
184         (goto-line number)
185         (if (re-search-forward gnus-cite-attribution-suffix 
186                                (save-excursion (end-of-line 1) (point))
187                                t)
188             (gnus-article-add-button (match-beginning 1) (match-end 1)
189                                      'gnus-cite-toggle prefix))
190         ;; Highlight attribution line.
191         (gnus-cite-add-face number skip face)
192         (gnus-cite-add-face number skip gnus-cite-attribution-face))
193       ;; Loop through attribution lines.
194       (setq alist gnus-cite-loose-attribution-alist)
195       (while alist
196         (setq entry (car alist)
197               alist (cdr alist)
198               number (car entry)
199               skip (gnus-cite-find-prefix number))
200         (gnus-cite-add-face number skip gnus-cite-attribution-face)))))
201
202 (defun gnus-dissect-cited-text ()
203   "Dissect the article buffer looking for cited text."
204   (save-excursion
205     (set-buffer gnus-article-buffer)
206     (gnus-cite-parse-maybe)
207     (let ((alist gnus-cite-prefix-alist)
208           prefix numbers number marks m)
209       ;; Loop through citation prefixes.
210       (while alist
211         (setq numbers (pop alist)
212               prefix (pop numbers))
213         (while numbers
214           (setq number (pop numbers))
215           (goto-char (point-min))
216           (forward-line number)
217           (push (cons (point-marker) "") marks)
218           (while (and numbers
219                       (= (1- number) (car numbers)))
220             (setq number (pop numbers)))
221           (goto-char (point-min))
222           (forward-line (1- number))
223           (push (cons (point-marker) prefix) marks)))
224       (goto-char (point-min))
225       (search-forward "\n\n" nil t)
226       (push (cons (point-marker) "") marks)
227       (goto-char (point-max))
228       (article-search-signature)
229       (push (cons (point-marker) "") marks)
230       (setq marks (sort marks (lambda (m1 m2) (< (car m1) (car m2)))))
231       (let* ((omarks marks))
232         (setq marks nil)
233         (while (cdr omarks)
234           (if (= (caar omarks) (caadr omarks))
235               (progn
236                 (unless (equal (cdar omarks) "")
237                   (push (car omarks) marks))
238                 (unless (equal (cdadr omarks) "")
239                   (push (cadr omarks) marks))
240                 (setq omarks (cdr omarks)))
241             (push (car omarks) marks))
242           (setq omarks (cdr omarks)))
243         (when (car omarks)
244           (push (car omarks) marks))
245         (setq marks (setq m (nreverse marks)))
246         (while (cddr m)
247           (if (and (equal (cdadr m) "")
248                    (equal (cdar m) (cdaddr m))
249                    (goto-char (caadr m))
250                    (forward-line 1)
251                    (= (point) (caaddr m)))
252               (setcdr m (cdddr m))
253             (setq m (cdr m))))
254         marks))))
255             
256 (defun gnus-article-fill-cited-article (&optional force)
257   "Do word wrapping in the current article."
258   (interactive (list t))
259   (save-excursion
260     (set-buffer gnus-article-buffer)
261     (let ((buffer-read-only nil)
262           (inhibit-point-motion-hooks t)
263           (marks (gnus-dissect-cited-text))
264           (adaptive-fill-mode nil))
265       (save-restriction
266         (while (cdr marks)
267           (widen)
268           (narrow-to-region (caar marks) (caadr marks))
269           (let ((adaptive-fill-regexp
270                  (concat "^" (regexp-quote (cdar marks)) " *"))
271                 (fill-prefix (cdar marks)))
272             (fill-region (point-min) (point-max)))
273           (set-marker (caar marks) nil)
274           (setq marks (cdr marks)))
275         (when marks
276           (set-marker (caar marks) nil))))))
277
278 (defun gnus-article-hide-citation (&optional arg force)
279   "Toggle hiding of all cited text except attribution lines.
280 See the documentation for `gnus-article-highlight-citation'.
281 If given a negative prefix, always show; if given a positive prefix,
282 always hide."
283   (interactive (append (article-hidden-arg) (list 'force)))
284   (setq gnus-cited-text-button-line-format-spec 
285         (gnus-parse-format gnus-cited-text-button-line-format 
286                            gnus-cited-text-button-line-format-alist t))
287   (save-excursion
288     (set-buffer gnus-article-buffer)
289     (cond
290      ((article-check-hidden-text 'cite arg)
291       t)
292      ((article-text-type-exists-p 'cite)
293       (let ((buffer-read-only nil))
294         (article-hide-text-of-type 'cite)))
295      (t
296       (let ((buffer-read-only nil)
297             (marks (gnus-dissect-cited-text))
298             (inhibit-point-motion-hooks t)
299             (props (nconc (list 'article-type 'cite)
300                           gnus-hidden-properties))
301             beg end)
302         (while marks
303           (setq beg nil
304                 end nil)
305           (while (and marks (string= (cdar marks) ""))
306             (setq marks (cdr marks)))
307           (when marks 
308             (setq beg (caar marks)))
309           (while (and marks (not (string= (cdar marks) "")))
310             (setq marks (cdr marks)))
311           (when marks
312             (setq end (caar marks)))
313           ;; Skip past lines we want to leave visible.
314           (when (and beg end gnus-cited-lines-visible)
315             (goto-char beg)
316             (forward-line gnus-cited-lines-visible)
317             (if (>= (point) end)
318                 (setq beg nil)
319               (setq beg (point-marker))))
320           (when (and beg end)
321             (gnus-add-text-properties beg end props)
322             (goto-char beg)
323             (unless (save-excursion (search-backward "\n\n" nil t))
324               (insert "\n"))
325             (put-text-property
326              (point)
327              (progn
328                (gnus-article-add-button
329                 (point)
330                 (progn (eval gnus-cited-text-button-line-format-spec) (point))
331                 `gnus-article-toggle-cited-text (cons beg end))
332                (point))
333              'article-type 'annotation)
334             (set-marker beg (point)))))))))
335
336 (defun gnus-article-toggle-cited-text (region)
337   "Toggle hiding the text in REGION."
338   (let (buffer-read-only)
339     (funcall
340      (if (text-property-any
341           (car region) (1- (cdr region))
342           (car gnus-hidden-properties) (cadr gnus-hidden-properties))
343          'remove-text-properties 'gnus-add-text-properties)
344      (car region) (cdr region) gnus-hidden-properties)))
345
346 (defun gnus-article-hide-citation-maybe (&optional arg force)
347   "Toggle hiding of cited text that has an attribution line.
348 If given a negative prefix, always show; if given a positive prefix,
349 always hide.
350 This will do nothing unless at least `gnus-cite-hide-percentage'
351 percent and at least `gnus-cite-hide-absolute' lines of the body is
352 cited text with attributions.  When called interactively, these two
353 variables are ignored.
354 See also the documentation for `gnus-article-highlight-citation'."
355   (interactive (append (article-hidden-arg) (list 'force)))
356   (unless (article-check-hidden-text 'cite arg)
357     (save-excursion
358       (set-buffer gnus-article-buffer)
359       (gnus-cite-parse-maybe force)
360       (goto-char (point-min))
361       (search-forward "\n\n" nil t)
362       (let ((start (point))
363             (atts gnus-cite-attribution-alist)
364             (buffer-read-only nil)
365             (inhibit-point-motion-hooks t)
366             (hiden 0)
367             total)
368         (goto-char (point-max))
369         (article-search-signature)
370         (setq total (count-lines start (point)))
371         (while atts
372           (setq hiden (+ hiden (length (cdr (assoc (cdar atts)
373                                                    gnus-cite-prefix-alist))))
374                 atts (cdr atts)))
375         (if (or force
376                 (and (> (* 100 hiden) (* gnus-cite-hide-percentage total))
377                      (> hiden gnus-cite-hide-absolute)))
378             (progn
379               (setq atts gnus-cite-attribution-alist)
380               (while atts
381                 (setq total (cdr (assoc (cdar atts) gnus-cite-prefix-alist))
382                       atts (cdr atts))
383                 (while total
384                   (setq hiden (car total)
385                         total (cdr total))
386                   (goto-line hiden)
387                   (or (assq hiden gnus-cite-attribution-alist)
388                       (gnus-add-text-properties 
389                        (point) (progn (forward-line 1) (point))
390                        (nconc (list 'article-type 'cite)
391                               gnus-hidden-properties)))))))))))
392
393 (defun gnus-article-hide-citation-in-followups ()
394   "Hide cited text in non-root articles."
395   (interactive)
396   (save-excursion
397     (set-buffer gnus-article-buffer)
398     (let ((article (cdr gnus-article-current)))
399       (unless (save-excursion
400                 (set-buffer gnus-summary-buffer)
401                 (gnus-article-displayed-root-p article))
402         (gnus-article-hide-citation)))))
403
404 ;;; Internal functions:
405
406 (defun gnus-cite-parse-maybe (&optional force)
407   ;; Parse if the buffer has changes since last time.
408   (if (equal gnus-cite-article gnus-article-current)
409       ()
410     ;;Reset parser information.
411     (setq gnus-cite-prefix-alist nil
412           gnus-cite-attribution-alist nil
413           gnus-cite-loose-prefix-alist nil
414           gnus-cite-loose-attribution-alist nil)
415     ;; Parse if not too large.
416     (if (and (not force) 
417              gnus-cite-parse-max-size
418              (> (buffer-size) gnus-cite-parse-max-size))
419         ()
420       (setq gnus-cite-article (cons (car gnus-article-current)
421                                     (cdr gnus-article-current)))
422       (gnus-cite-parse))))
423
424 (defun gnus-cite-parse ()
425   ;; Parse and connect citation prefixes and attribution lines.
426   
427   ;; Parse current buffer searching for citation prefixes.
428   (goto-char (point-min))
429   (or (search-forward "\n\n" nil t)
430       (goto-char (point-max)))
431   (let ((line (1+ (count-lines (point-min) (point))))
432         (case-fold-search t)
433         (max (save-excursion
434                (goto-char (point-max))
435                (article-search-signature)
436                (point)))
437         alist entry start begin end numbers prefix)
438     ;; Get all potential prefixes in `alist'.
439     (while (< (point) max)
440       ;; Each line.
441       (setq begin (point)
442             end (progn (beginning-of-line 2) (point))
443             start end)
444       (goto-char begin)
445       ;; Ignore standard Supercite attribution prefix.
446       (if (looking-at gnus-supercite-regexp)
447           (if (match-end 1)
448               (setq end (1+ (match-end 1)))
449             (setq end (1+ begin))))
450       ;; Ignore very long prefixes.
451       (if (> end (+ (point) gnus-cite-max-prefix))
452           (setq end (+ (point) gnus-cite-max-prefix)))
453       (while (re-search-forward gnus-cite-prefix-regexp (1- end) t)
454         ;; Each prefix.
455         (setq end (match-end 0)
456               prefix (buffer-substring begin end))
457         (gnus-set-text-properties 0 (length prefix) nil prefix)
458         (setq entry (assoc prefix alist))
459         (if entry 
460             (setcdr entry (cons line (cdr entry)))
461           (setq alist (cons (list prefix line) alist)))
462         (goto-char begin))
463       (goto-char start)
464       (setq line (1+ line)))
465     ;; We got all the potential prefixes.  Now create
466     ;; `gnus-cite-prefix-alist' containing the oldest prefix for each
467     ;; line that appears at least gnus-cite-minimum-match-count
468     ;; times.  First sort them by length.  Longer is older.
469     (setq alist (sort alist (lambda (a b)
470                               (> (length (car a)) (length (car b))))))
471     (while alist
472       (setq entry (car alist)
473             prefix (car entry)
474             numbers (cdr entry)
475             alist (cdr alist))
476       (cond ((null numbers)
477              ;; No lines with this prefix that wasn't also part of
478              ;; a longer prefix.
479              )
480             ((< (length numbers) gnus-cite-minimum-match-count)
481              ;; Too few lines with this prefix.  We keep it a bit
482              ;; longer in case it is an exact match for an attribution
483              ;; line, but we don't remove the line from other
484              ;; prefixes. 
485              (setq gnus-cite-prefix-alist
486                    (cons entry gnus-cite-prefix-alist)))
487             (t
488              (setq gnus-cite-prefix-alist (cons entry
489                                                 gnus-cite-prefix-alist))
490              ;; Remove articles from other prefixes.
491              (let ((loop alist)
492                    current)
493                (while loop
494                  (setq current (car loop)
495                        loop (cdr loop))
496                  (setcdr current 
497                          (gnus-set-difference (cdr current) numbers))))))))
498   ;; No citations have been connected to attribution lines yet.
499   (setq gnus-cite-loose-prefix-alist (append gnus-cite-prefix-alist nil))
500
501   ;; Parse current buffer searching for attribution lines.
502   (goto-char (point-min))
503   (search-forward "\n\n" nil t)
504   (while (re-search-forward gnus-cite-attribution-suffix (point-max) t)
505     (let* ((start (match-beginning 0))
506            (end (match-end 0))
507            (wrote (count-lines (point-min) end))
508            (prefix (gnus-cite-find-prefix wrote))
509            ;; Check previous line for an attribution leader.
510            (tag (progn
511                   (beginning-of-line 1)
512                   (and (looking-at gnus-supercite-secondary-regexp)
513                        (buffer-substring (match-beginning 1)
514                                          (match-end 1)))))
515            (in (progn
516                  (goto-char start)
517                  (and (re-search-backward gnus-cite-attribution-prefix
518                                           (save-excursion
519                                             (beginning-of-line 0)
520                                             (point))
521                                           t)
522                       (not (re-search-forward gnus-cite-attribution-suffix
523                                               start t))
524                       (count-lines (point-min) (1+ (point)))))))
525       (if (eq wrote in)
526           (setq in nil))
527       (goto-char end)
528       (setq gnus-cite-loose-attribution-alist
529             (cons (list wrote in prefix tag)
530                   gnus-cite-loose-attribution-alist))))
531   ;; Find exact supercite citations.
532   (gnus-cite-match-attributions 'small nil
533                                 (lambda (prefix tag)
534                                   (if tag
535                                       (concat "\\`" 
536                                               (regexp-quote prefix) "[ \t]*" 
537                                               (regexp-quote tag) ">"))))
538   ;; Find loose supercite citations after attributions.
539   (gnus-cite-match-attributions 'small t
540                                 (lambda (prefix tag)
541                                   (if tag (concat "\\<"
542                                                   (regexp-quote tag)
543                                                   "\\>"))))
544   ;; Find loose supercite citations anywhere.
545   (gnus-cite-match-attributions 'small nil
546                                 (lambda (prefix tag)
547                                   (if tag (concat "\\<"
548                                                   (regexp-quote tag)
549                                                   "\\>"))))
550   ;; Find nested citations after attributions.
551   (gnus-cite-match-attributions 'small-if-unique t
552                                 (lambda (prefix tag)
553                                   (concat "\\`" (regexp-quote prefix) ".+")))
554   ;; Find nested citations anywhere.
555   (gnus-cite-match-attributions 'small nil
556                                 (lambda (prefix tag)
557                                   (concat "\\`" (regexp-quote prefix) ".+")))
558   ;; Remove loose prefixes with too few lines.
559   (let ((alist gnus-cite-loose-prefix-alist)
560         entry)
561     (while alist
562       (setq entry (car alist)
563             alist (cdr alist))
564       (if (< (length (cdr entry)) gnus-cite-minimum-match-count)
565           (setq gnus-cite-prefix-alist
566                 (delq entry gnus-cite-prefix-alist)
567                 gnus-cite-loose-prefix-alist
568                 (delq entry gnus-cite-loose-prefix-alist)))))
569   ;; Find flat attributions.
570   (gnus-cite-match-attributions 'first t nil)
571   ;; Find any attributions (are we getting desperate yet?).
572   (gnus-cite-match-attributions 'first nil nil))
573
574 (defun gnus-cite-match-attributions (sort after fun)
575   ;; Match all loose attributions and citations (SORT AFTER FUN) .
576   ;;
577   ;; If SORT is `small', the citation with the shortest prefix will be
578   ;; used, if it is `first' the first prefix will be used, if it is
579   ;; `small-if-unique' the shortest prefix will be used if the
580   ;; attribution line does not share its own prefix with other
581   ;; loose attribution lines, otherwise the first prefix will be used.
582   ;;
583   ;; If AFTER is non-nil, only citations after the attribution line
584   ;; will be considered.
585   ;;
586   ;; If FUN is non-nil, it will be called with the arguments (WROTE
587   ;; PREFIX TAG) and expected to return a regular expression.  Only
588   ;; citations whose prefix matches the regular expression will be
589   ;; considered. 
590   ;; 
591   ;; WROTE is the attribution line number.
592   ;; PREFIX is the attribution line prefix.
593   ;; TAG is the Supercite tag on the attribution line.
594   (let ((atts gnus-cite-loose-attribution-alist)
595         (case-fold-search t)
596         att wrote in prefix tag regexp limit smallest best size)
597     (while atts
598       (setq att (car atts)
599             atts (cdr atts)
600             wrote (nth 0 att)
601             in (nth 1 att)
602             prefix (nth 2 att)
603             tag (nth 3 att)
604             regexp (if fun (funcall fun prefix tag) "")
605             size (cond ((eq sort 'small) t)
606                        ((eq sort 'first) nil)
607                        (t (< (length (gnus-cite-find-loose prefix)) 2)))
608             limit (if after wrote -1)
609             smallest 1000000                   
610             best nil)
611       (let ((cites gnus-cite-loose-prefix-alist)
612             cite candidate numbers first compare)
613         (while cites
614           (setq cite (car cites)
615                 cites (cdr cites)
616                 candidate (car cite)
617                 numbers (cdr cite)
618                 first (apply 'min numbers)
619                 compare (if size (length candidate) first))
620           (and (> first limit)
621                regexp
622                (string-match regexp candidate)
623                (< compare smallest)
624                (setq best cite
625                      smallest compare))))
626       (if (null best)
627           ()
628         (setq gnus-cite-loose-attribution-alist
629               (delq att gnus-cite-loose-attribution-alist))
630         (setq gnus-cite-attribution-alist 
631               (cons (cons wrote (car best)) gnus-cite-attribution-alist))
632         (if in
633             (setq gnus-cite-attribution-alist 
634                   (cons (cons in (car best)) gnus-cite-attribution-alist)))
635         (if (memq best gnus-cite-loose-prefix-alist)
636             (let ((loop gnus-cite-prefix-alist)
637                   (numbers (cdr best))
638                   current)
639               (setq gnus-cite-loose-prefix-alist
640                     (delq best gnus-cite-loose-prefix-alist))
641               (while loop
642                 (setq current (car loop)
643                       loop (cdr loop))
644                 (if (eq current best)
645                     ()
646                   (setcdr current (gnus-set-difference (cdr current) numbers))
647                   (if (null (cdr current))
648                       (setq gnus-cite-loose-prefix-alist
649                             (delq current gnus-cite-loose-prefix-alist)
650                             atts (delq current atts)))))))))))
651
652 (defun gnus-cite-find-loose (prefix)
653   ;; Return a list of loose attribution lines prefixed by PREFIX.
654   (let* ((atts gnus-cite-loose-attribution-alist)
655          att line lines)
656     (while atts
657       (setq att (car atts)
658             line (car att)
659             atts (cdr atts))
660       (if (string-equal (gnus-cite-find-prefix line) prefix)
661           (setq lines (cons line lines))))
662     lines))
663
664 (defun gnus-cite-add-face (number prefix face)
665   ;; At line NUMBER, ignore PREFIX and add FACE to the rest of the line.
666   (when face
667     (let ((inhibit-point-motion-hooks t)
668           from to)
669       (goto-line number)
670       (unless (eobp);; Sometimes things become confused.
671         (forward-char (length prefix))
672         (skip-chars-forward " \t")
673         (setq from (point))
674         (end-of-line 1)
675         (skip-chars-backward " \t")
676         (setq to (point))
677         (when (< from to)
678           (gnus-overlay-put (gnus-make-overlay from to) 'face face))))))
679
680 (defun gnus-cite-toggle (prefix)
681   (save-excursion
682     (set-buffer gnus-article-buffer)
683     (let ((buffer-read-only nil)
684           (numbers (cdr (assoc prefix gnus-cite-prefix-alist)))
685           (inhibit-point-motion-hooks t)
686           number)
687       (while numbers
688         (setq number (car numbers)
689               numbers (cdr numbers))
690         (goto-line number)
691         (cond ((get-text-property (point) 'invisible)
692                (remove-text-properties (point) (progn (forward-line 1) (point))
693                                        gnus-hidden-properties))
694               ((assq number gnus-cite-attribution-alist))
695               (t
696                (gnus-add-text-properties 
697                 (point) (progn (forward-line 1) (point))
698                 (nconc (list 'article-type 'cite)
699                        gnus-hidden-properties))))))))
700
701 (defun gnus-cite-find-prefix (line)
702   ;; Return citation prefix for LINE.
703   (let ((alist gnus-cite-prefix-alist)
704         (prefix "")
705         entry)
706     (while alist
707       (setq entry (car alist)
708             alist (cdr alist))
709       (if (memq line (cdr entry))
710           (setq prefix (car entry))))
711     prefix))
712
713 (gnus-add-shutdown 'gnus-cache-close 'gnus)
714
715 (defun gnus-cache-close ()
716   (setq gnus-cite-prefix-alist nil))
717
718 (gnus-ems-redefine)
719
720 (provide 'gnus-cite)
721
722 ;;; gnus-cite.el ends here