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