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