*** 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         (when (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         ;; All this information is now incorrect.
278         (setq gnus-cite-prefix-alist nil
279               gnus-cite-attribution-alist nil
280               gnus-cite-loose-prefix-alist nil
281               gnus-cite-loose-attribution-alist nil)))))
282
283 (defun gnus-article-hide-citation (&optional arg force)
284   "Toggle hiding of all cited text except attribution lines.
285 See the documentation for `gnus-article-highlight-citation'.
286 If given a negative prefix, always show; if given a positive prefix,
287 always hide."
288   (interactive (append (article-hidden-arg) (list 'force)))
289   (setq gnus-cited-text-button-line-format-spec 
290         (gnus-parse-format gnus-cited-text-button-line-format 
291                            gnus-cited-text-button-line-format-alist t))
292   (save-excursion
293     (set-buffer gnus-article-buffer)
294     (cond
295      ((article-check-hidden-text 'cite arg)
296       t)
297      ((article-text-type-exists-p 'cite)
298       (let ((buffer-read-only nil))
299         (article-hide-text-of-type 'cite)))
300      (t
301       (let ((buffer-read-only nil)
302             (marks (gnus-dissect-cited-text))
303             (inhibit-point-motion-hooks t)
304             (props (nconc (list 'article-type 'cite)
305                           gnus-hidden-properties))
306             beg end)
307         (while marks
308           (setq beg nil
309                 end nil)
310           (while (and marks (string= (cdar marks) ""))
311             (setq marks (cdr marks)))
312           (when marks 
313             (setq beg (caar marks)))
314           (while (and marks (not (string= (cdar marks) "")))
315             (setq marks (cdr marks)))
316           (when marks
317             (setq end (caar marks)))
318           ;; Skip past lines we want to leave visible.
319           (when (and beg end gnus-cited-lines-visible)
320             (goto-char beg)
321             (forward-line gnus-cited-lines-visible)
322             (if (>= (point) end)
323                 (setq beg nil)
324               (setq beg (point-marker))))
325           (when (and beg end)
326             (gnus-add-text-properties beg end props)
327             (goto-char beg)
328             (unless (save-excursion (search-backward "\n\n" nil t))
329               (insert "\n"))
330             (put-text-property
331              (point)
332              (progn
333                (gnus-article-add-button
334                 (point)
335                 (progn (eval gnus-cited-text-button-line-format-spec) (point))
336                 `gnus-article-toggle-cited-text (cons beg end))
337                (point))
338              'article-type 'annotation)
339             (set-marker beg (point)))))))))
340
341 (defun gnus-article-toggle-cited-text (region)
342   "Toggle hiding the text in REGION."
343   (let (buffer-read-only)
344     (funcall
345      (if (text-property-any
346           (car region) (1- (cdr region))
347           (car gnus-hidden-properties) (cadr gnus-hidden-properties))
348          'remove-text-properties 'gnus-add-text-properties)
349      (car region) (cdr region) gnus-hidden-properties)))
350
351 (defun gnus-article-hide-citation-maybe (&optional arg force)
352   "Toggle hiding of cited text that has an attribution line.
353 If given a negative prefix, always show; if given a positive prefix,
354 always hide.
355 This will do nothing unless at least `gnus-cite-hide-percentage'
356 percent and at least `gnus-cite-hide-absolute' lines of the body is
357 cited text with attributions.  When called interactively, these two
358 variables are ignored.
359 See also the documentation for `gnus-article-highlight-citation'."
360   (interactive (append (article-hidden-arg) (list 'force)))
361   (unless (article-check-hidden-text 'cite arg)
362     (save-excursion
363       (set-buffer gnus-article-buffer)
364       (gnus-cite-parse-maybe force)
365       (goto-char (point-min))
366       (search-forward "\n\n" nil t)
367       (let ((start (point))
368             (atts gnus-cite-attribution-alist)
369             (buffer-read-only nil)
370             (inhibit-point-motion-hooks t)
371             (hiden 0)
372             total)
373         (goto-char (point-max))
374         (article-search-signature)
375         (setq total (count-lines start (point)))
376         (while atts
377           (setq hiden (+ hiden (length (cdr (assoc (cdar atts)
378                                                    gnus-cite-prefix-alist))))
379                 atts (cdr atts)))
380         (when (or force
381                   (and (> (* 100 hiden) (* gnus-cite-hide-percentage total))
382                        (> hiden gnus-cite-hide-absolute)))
383           (setq atts gnus-cite-attribution-alist)
384           (while atts
385             (setq total (cdr (assoc (cdar atts) gnus-cite-prefix-alist))
386                   atts (cdr atts))
387             (while total
388               (setq hiden (car total)
389                     total (cdr total))
390               (goto-line hiden)
391               (unless (assq hiden gnus-cite-attribution-alist)
392                 (gnus-add-text-properties 
393                  (point) (progn (forward-line 1) (point))
394                  (nconc (list 'article-type 'cite)
395                         gnus-hidden-properties))))))))))
396
397 (defun gnus-article-hide-citation-in-followups ()
398   "Hide cited text in non-root articles."
399   (interactive)
400   (save-excursion
401     (set-buffer gnus-article-buffer)
402     (let ((article (cdr gnus-article-current)))
403       (unless (save-excursion
404                 (set-buffer gnus-summary-buffer)
405                 (gnus-article-displayed-root-p article))
406         (gnus-article-hide-citation)))))
407
408 ;;; Internal functions:
409
410 (defun gnus-cite-parse-maybe (&optional force)
411   ;; Parse if the buffer has changes since last time.
412   (if (equal gnus-cite-article gnus-article-current)
413       ()
414     ;;Reset parser information.
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     ;; Parse if not too large.
420     (if (and (not force)
421              gnus-cite-parse-max-size
422              (> (buffer-size) gnus-cite-parse-max-size))
423         ()
424       (setq gnus-cite-article (cons (car gnus-article-current)
425                                     (cdr gnus-article-current)))
426       (gnus-cite-parse))))
427
428 (defun gnus-cite-parse ()
429   ;; Parse and connect citation prefixes and attribution lines.
430   
431   ;; Parse current buffer searching for citation prefixes.
432   (goto-char (point-min))
433   (unless (search-forward "\n\n" nil t)
434     (goto-char (point-max)))
435   (let ((line (1+ (count-lines (point-min) (point))))
436         (case-fold-search t)
437         (max (save-excursion
438                (goto-char (point-max))
439                (article-search-signature)
440                (point)))
441         alist entry start begin end numbers prefix)
442     ;; Get all potential prefixes in `alist'.
443     (while (< (point) max)
444       ;; Each line.
445       (setq begin (point)
446             end (progn (beginning-of-line 2) (point))
447             start end)
448       (goto-char begin)
449       ;; Ignore standard Supercite attribution prefix.
450       (when (looking-at gnus-supercite-regexp)
451         (if (match-end 1)
452             (setq end (1+ (match-end 1)))
453           (setq end (1+ begin))))
454       ;; Ignore very long prefixes.
455       (when (> end (+ (point) gnus-cite-max-prefix))
456         (setq end (+ (point) gnus-cite-max-prefix)))
457       (while (re-search-forward gnus-cite-prefix-regexp (1- end) t)
458         ;; Each prefix.
459         (setq end (match-end 0)
460               prefix (buffer-substring begin end))
461         (gnus-set-text-properties 0 (length prefix) nil prefix)
462         (setq entry (assoc prefix alist))
463         (if entry 
464             (setcdr entry (cons line (cdr entry)))
465           (push (list prefix line) alist))
466         (goto-char begin))
467       (goto-char start)
468       (setq line (1+ line)))
469     ;; We got all the potential prefixes.  Now create
470     ;; `gnus-cite-prefix-alist' containing the oldest prefix for each
471     ;; line that appears at least gnus-cite-minimum-match-count
472     ;; times.  First sort them by length.  Longer is older.
473     (setq alist (sort alist (lambda (a b)
474                               (> (length (car a)) (length (car b))))))
475     (while alist
476       (setq entry (car alist)
477             prefix (car entry)
478             numbers (cdr entry)
479             alist (cdr alist))
480       (cond ((null numbers)
481              ;; No lines with this prefix that wasn't also part of
482              ;; a longer prefix.
483              )
484             ((< (length numbers) gnus-cite-minimum-match-count)
485              ;; Too few lines with this prefix.  We keep it a bit
486              ;; longer in case it is an exact match for an attribution
487              ;; line, but we don't remove the line from other
488              ;; prefixes. 
489              (push entry gnus-cite-prefix-alist))
490             (t
491              (push entry
492                    gnus-cite-prefix-alist)
493              ;; Remove articles from other prefixes.
494              (let ((loop alist)
495                    current)
496                (while loop
497                  (setq current (car loop)
498                        loop (cdr loop))
499                  (setcdr current 
500                          (gnus-set-difference (cdr current) numbers))))))))
501   ;; No citations have been connected to attribution lines yet.
502   (setq gnus-cite-loose-prefix-alist (append gnus-cite-prefix-alist nil))
503
504   ;; Parse current buffer searching for attribution lines.
505   (goto-char (point-min))
506   (search-forward "\n\n" nil t)
507   (while (re-search-forward gnus-cite-attribution-suffix (point-max) t)
508     (let* ((start (match-beginning 0))
509            (end (match-end 0))
510            (wrote (count-lines (point-min) end))
511            (prefix (gnus-cite-find-prefix wrote))
512            ;; Check previous line for an attribution leader.
513            (tag (progn
514                   (beginning-of-line 1)
515                   (when (looking-at gnus-supercite-secondary-regexp)
516                     (buffer-substring (match-beginning 1)
517                                       (match-end 1)))))
518            (in (progn
519                  (goto-char start)
520                  (and (re-search-backward gnus-cite-attribution-prefix
521                                           (save-excursion
522                                             (beginning-of-line 0)
523                                             (point))
524                                           t)
525                       (not (re-search-forward gnus-cite-attribution-suffix
526                                               start t))
527                       (count-lines (point-min) (1+ (point)))))))
528       (when (eq wrote in)
529         (setq in nil))
530       (goto-char end)
531       (push (list wrote in prefix tag)
532             gnus-cite-loose-attribution-alist)))
533   ;; Find exact supercite citations.
534   (gnus-cite-match-attributions 'small nil
535                                 (lambda (prefix tag)
536                                   (when tag
537                                     (concat "\\`" 
538                                             (regexp-quote prefix) "[ \t]*" 
539                                             (regexp-quote tag) ">"))))
540   ;; Find loose supercite citations after attributions.
541   (gnus-cite-match-attributions 'small t
542                                 (lambda (prefix tag)
543                                   (when tag
544                                     (concat "\\<"
545                                             (regexp-quote tag)
546                                             "\\>"))))
547   ;; Find loose supercite citations anywhere.
548   (gnus-cite-match-attributions 'small nil
549                                 (lambda (prefix tag)
550                                   (when tag
551                                     (concat "\\<"
552                                             (regexp-quote tag)
553                                             "\\>"))))
554   ;; Find nested citations after attributions.
555   (gnus-cite-match-attributions 'small-if-unique t
556                                 (lambda (prefix tag)
557                                   (concat "\\`" (regexp-quote prefix) ".+")))
558   ;; Find nested citations anywhere.
559   (gnus-cite-match-attributions 'small nil
560                                 (lambda (prefix tag)
561                                   (concat "\\`" (regexp-quote prefix) ".+")))
562   ;; Remove loose prefixes with too few lines.
563   (let ((alist gnus-cite-loose-prefix-alist)
564         entry)
565     (while alist
566       (setq entry (car alist)
567             alist (cdr alist))
568       (when (< (length (cdr entry)) gnus-cite-minimum-match-count)
569         (setq gnus-cite-prefix-alist
570               (delq entry gnus-cite-prefix-alist)
571               gnus-cite-loose-prefix-alist
572               (delq entry gnus-cite-loose-prefix-alist)))))
573   ;; Find flat attributions.
574   (gnus-cite-match-attributions 'first t nil)
575   ;; Find any attributions (are we getting desperate yet?).
576   (gnus-cite-match-attributions 'first nil nil))
577
578 (defun gnus-cite-match-attributions (sort after fun)
579   ;; Match all loose attributions and citations (SORT AFTER FUN) .
580   ;;
581   ;; If SORT is `small', the citation with the shortest prefix will be
582   ;; used, if it is `first' the first prefix will be used, if it is
583   ;; `small-if-unique' the shortest prefix will be used if the
584   ;; attribution line does not share its own prefix with other
585   ;; loose attribution lines, otherwise the first prefix will be used.
586   ;;
587   ;; If AFTER is non-nil, only citations after the attribution line
588   ;; will be considered.
589   ;;
590   ;; If FUN is non-nil, it will be called with the arguments (WROTE
591   ;; PREFIX TAG) and expected to return a regular expression.  Only
592   ;; citations whose prefix matches the regular expression will be
593   ;; considered. 
594   ;; 
595   ;; WROTE is the attribution line number.
596   ;; PREFIX is the attribution line prefix.
597   ;; TAG is the Supercite tag on the attribution line.
598   (let ((atts gnus-cite-loose-attribution-alist)
599         (case-fold-search t)
600         att wrote in prefix tag regexp limit smallest best size)
601     (while atts
602       (setq att (car atts)
603             atts (cdr atts)
604             wrote (nth 0 att)
605             in (nth 1 att)
606             prefix (nth 2 att)
607             tag (nth 3 att)
608             regexp (if fun (funcall fun prefix tag) "")
609             size (cond ((eq sort 'small) t)
610                        ((eq sort 'first) nil)
611                        (t (< (length (gnus-cite-find-loose prefix)) 2)))
612             limit (if after wrote -1)
613             smallest 1000000                   
614             best nil)
615       (let ((cites gnus-cite-loose-prefix-alist)
616             cite candidate numbers first compare)
617         (while cites
618           (setq cite (car cites)
619                 cites (cdr cites)
620                 candidate (car cite)
621                 numbers (cdr cite)
622                 first (apply 'min numbers)
623                 compare (if size (length candidate) first))
624           (and (> first limit)
625                regexp
626                (string-match regexp candidate)
627                (< compare smallest)
628                (setq best cite
629                      smallest compare))))
630       (if (null best)
631           ()
632         (setq gnus-cite-loose-attribution-alist
633               (delq att gnus-cite-loose-attribution-alist))
634         (push (cons wrote (car best)) gnus-cite-attribution-alist)
635         (when in
636           (push (cons in (car best)) gnus-cite-attribution-alist))
637         (when (memq best gnus-cite-loose-prefix-alist)
638           (let ((loop gnus-cite-prefix-alist)
639                 (numbers (cdr best))
640                 current)
641             (setq gnus-cite-loose-prefix-alist
642                   (delq best gnus-cite-loose-prefix-alist))
643             (while loop
644               (setq current (car loop)
645                     loop (cdr loop))
646               (if (eq current best)
647                   ()
648                 (setcdr current (gnus-set-difference (cdr current) numbers))
649                 (when (null (cdr current))
650                   (setq gnus-cite-loose-prefix-alist
651                         (delq current gnus-cite-loose-prefix-alist)
652                         atts (delq current atts)))))))))))
653
654 (defun gnus-cite-find-loose (prefix)
655   ;; Return a list of loose attribution lines prefixed by PREFIX.
656   (let* ((atts gnus-cite-loose-attribution-alist)
657          att line lines)
658     (while atts
659       (setq att (car atts)
660             line (car att)
661             atts (cdr atts))
662       (when (string-equal (gnus-cite-find-prefix line) prefix)
663         (push line lines)))
664     lines))
665
666 (defun gnus-cite-add-face (number prefix face)
667   ;; At line NUMBER, ignore PREFIX and add FACE to the rest of the line.
668   (when face
669     (let ((inhibit-point-motion-hooks t)
670           from to)
671       (goto-line number)
672       (unless (eobp);; Sometimes things become confused.
673         (forward-char (length prefix))
674         (skip-chars-forward " \t")
675         (setq from (point))
676         (end-of-line 1)
677         (skip-chars-backward " \t")
678         (setq to (point))
679         (when (< from to)
680           (gnus-overlay-put (gnus-make-overlay from to) 'face face))))))
681
682 (defun gnus-cite-toggle (prefix)
683   (save-excursion
684     (set-buffer gnus-article-buffer)
685     (let ((buffer-read-only nil)
686           (numbers (cdr (assoc prefix gnus-cite-prefix-alist)))
687           (inhibit-point-motion-hooks t)
688           number)
689       (while numbers
690         (setq number (car numbers)
691               numbers (cdr numbers))
692         (goto-line number)
693         (cond ((get-text-property (point) 'invisible)
694                (remove-text-properties (point) (progn (forward-line 1) (point))
695                                        gnus-hidden-properties))
696               ((assq number gnus-cite-attribution-alist))
697               (t
698                (gnus-add-text-properties 
699                 (point) (progn (forward-line 1) (point))
700                 (nconc (list 'article-type 'cite)
701                        gnus-hidden-properties))))))))
702
703 (defun gnus-cite-find-prefix (line)
704   ;; Return citation prefix for LINE.
705   (let ((alist gnus-cite-prefix-alist)
706         (prefix "")
707         entry)
708     (while alist
709       (setq entry (car alist)
710             alist (cdr alist))
711       (when (memq line (cdr entry))
712         (setq prefix (car entry))))
713     prefix))
714
715 (gnus-add-shutdown 'gnus-cache-close 'gnus)
716
717 (defun gnus-cache-close ()
718   (setq gnus-cite-prefix-alist nil))
719
720 (gnus-ems-redefine)
721
722 (provide 'gnus-cite)
723
724 ;;; gnus-cite.el ends here