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