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