*** empty log message ***
[gnus] / lisp / gnus-cite.el
1 ;;; gnus-cite.el --- parse citations in articles for Gnus
2 ;; Copyright (C) 1995,96,97 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-art)
30 (require 'gnus-range)
31
32 ;;; Customization:
33
34 (defgroup gnus-cite nil
35   "Citation."
36   :group 'article)
37   
38
39 (defcustom gnus-cite-reply-regexp
40   "^\\(Subject: Re\\|In-Reply-To\\|References\\):"
41   "If headers match this regexp it is reasonable to believe that
42 article has citations."
43   :group 'gnus-cite
44   :type 'string)
45
46 (defcustom gnus-cite-always-check nil
47   "Check article always for citations. Set it t to check all articles."
48   :group 'gnus-cite
49   :type '(choice (const :tag "no" nil)
50                   (const :tag "yes" t)))
51
52 (defcustom gnus-cited-text-button-line-format "%(%{[...]%}%)\n"
53   "Format of cited text buttons."
54   :group 'gnus-cite
55   :type 'string)
56
57 (defcustom gnus-cited-lines-visible nil
58   "The number of lines of hidden cited text to remain visible."
59   :group 'gnus-cite
60   :type '(choice (const :tag "none" nil)
61                  integer))
62
63 (defcustom gnus-cite-parse-max-size 25000
64   "Maximum article size (in bytes) where parsing citations is allowed.
65 Set it to nil to parse all articles."
66   :group 'gnus-cite
67   :type '(choice (const :tag "all" nil)
68                  integer))
69
70 (defcustom gnus-cite-prefix-regexp 
71     "^[]>|:}+ ]*[]>|:}+]\\(.*>\\)?\\|^.*>"
72   "Regexp matching the longest possible citation prefix on a line."
73   :group 'gnus-cite
74   :type 'regexp)
75
76 (defcustom gnus-cite-max-prefix 20
77   "Maximum possible length for a citation prefix."
78   :group 'gnus-cite
79   :type 'integer)
80
81 (defcustom gnus-supercite-regexp 
82   (concat "^\\(" gnus-cite-prefix-regexp "\\)? *"
83           ">>>>> +\"\\([^\"\n]+\\)\" +==")
84   "Regexp matching normal Supercite attribution lines.
85 The first grouping must match prefixes added by other packages."
86   :group 'gnus-cite
87   :type 'regexp)
88
89 (defcustom gnus-supercite-secondary-regexp "^.*\"\\([^\"\n]+\\)\" +=="
90   "Regexp matching mangled Supercite attribution lines.
91 The first regexp group should match the Supercite attribution."
92   :group 'gnus-cite
93   :type 'regexp)
94
95 (defcustom gnus-cite-minimum-match-count 2
96   "Minimum number of identical prefixes before we believe it's a citation."
97   :group 'gnus-cite
98   :type 'integer)
99
100 (defcustom gnus-cite-attribution-prefix "in article\\|in <"
101   "Regexp matching the beginning of an attribution line."
102   :group 'gnus-cite
103   :type 'regexp)
104
105 (defcustom gnus-cite-attribution-suffix
106   "\\(wrote\\|writes\\|said\\|says\\):[ \t]*$"
107   "Regexp matching the end of an attribution line.
108 The text matching the first grouping will be used as a button."
109   :group 'gnus-cite
110   :type 'regexp)
111
112 (defface gnus-cite-attribution-face '((t 
113                                        (:underline t)))
114   "Face used for attribution lines.")
115
116 (defcustom gnus-cite-attribution-face 'gnus-cite-attribution-face
117   "Face used for attribution lines.
118 It is merged with the face for the cited text belonging to the attribution."
119   :group 'gnus-cite
120   :type 'face)
121
122 (defface gnus-cite-face-1 '((((class color)
123                               (background dark))
124                              (:foreground "light blue"))
125                             (((class color)
126                               (background light))
127                              (:foreground "MidnightBlue"))
128                             (t 
129                              (:italic t)))
130   "Citation face.")
131
132 (defface gnus-cite-face-2 '((((class color)
133                               (background dark))
134                              (:foreground "light cyan"))
135                             (((class color)
136                               (background light))
137                              (:foreground "firebrick"))
138                             (t 
139                              (:italic t)))
140   "Citation face.")
141
142 (defface gnus-cite-face-3 '((((class color)
143                               (background dark))
144                              (:foreground "light yellow"))
145                             (((class color)
146                               (background light))
147                              (:foreground "dark green"))
148                             (t 
149                              (:italic t)))
150   "Citation face.")
151
152 (defface gnus-cite-face-4 '((((class color)
153                               (background dark))
154                              (:foreground "light pink"))
155                             (((class color)
156                               (background light))
157                              (:foreground "OrangeRed"))
158                             (t 
159                              (:italic t)))
160   "Citation face.")
161
162 (defface gnus-cite-face-5 '((((class color)
163                               (background dark))
164                              (:foreground "pale green"))
165                             (((class color)
166                               (background light))
167                              (:foreground "dark khaki"))
168                             (t 
169                              (:italic t)))
170   "Citation face.")
171
172 (defface gnus-cite-face-6 '((((class color)
173                               (background dark))
174                              (:foreground "beige"))
175                             (((class color)
176                               (background light))
177                              (:foreground "dark violet"))
178                             (t 
179                              (:italic t)))
180   "Citation face.")
181
182 (defface gnus-cite-face-7 '((((class color)
183                               (background dark))
184                              (:foreground "orange"))
185                             (((class color)
186                               (background light))
187                              (:foreground "SteelBlue4"))
188                             (t 
189                              (:italic t)))
190   "Citation face.")
191
192 (defface gnus-cite-face-8 '((((class color)
193                               (background dark))
194                              (:foreground "magenta"))
195                             (((class color)
196                               (background light))
197                              (:foreground "magenta"))
198                             (t 
199                              (:italic t)))
200   "Citation face.")
201
202 (defface gnus-cite-face-9 '((((class color)
203                               (background dark))
204                              (:foreground "violet"))
205                             (((class color)
206                               (background light))
207                              (:foreground "violet"))
208                             (t 
209                              (:italic t)))
210   "Citation face.")
211
212 (defface gnus-cite-face-10 '((((class color)
213                                (background dark))
214                               (:foreground "medium purple"))
215                              (((class color)
216                                (background light))
217                               (:foreground "medium purple"))
218                              (t 
219                               (:italic t)))
220   "Citation face.")
221
222 (defface gnus-cite-face-11 '((((class color)
223                                (background dark))
224                               (:foreground "turquoise"))
225                              (((class color)
226                                (background light))
227                               (:foreground "turquoise"))
228                              (t 
229                               (:italic t)))
230   "Citation face.")
231
232 (defcustom gnus-cite-face-list 
233   '(gnus-cite-face-1 gnus-cite-face-2 gnus-cite-face-3 gnus-cite-face-4 
234     gnus-cite-face-5 gnus-cite-face-6 gnus-cite-face-7 gnus-cite-face-8 
235     gnus-cite-face-9 gnus-cite-face-10 gnus-cite-face-11)
236   "List of faces used for highlighting citations. 
237
238 When there are citations from multiple articles in the same message,
239 Gnus will try to give each citation from each article its own face.
240 This should make it easier to see who wrote what."
241   :group 'gnus-cite
242   :type '(repeat face))
243
244 (defcustom gnus-cite-hide-percentage 50
245   "Only hide excess citation if above this percentage of the body."
246   :group 'gnus-cite
247   :type 'number)
248
249 (defcustom gnus-cite-hide-absolute 10
250   "Only hide excess citation if above this number of lines in the body."
251   :group 'gnus-cite
252   :type 'integer)
253
254 ;;; Internal Variables:
255
256 (defvar gnus-cite-article nil)
257
258 (defvar gnus-cite-prefix-alist nil)
259 ;; Alist of citation prefixes.  
260 ;; The cdr is a list of lines with that prefix.
261
262 (defvar gnus-cite-attribution-alist nil)
263 ;; Alist of attribution lines.
264 ;; The car is a line number.
265 ;; The cdr is the prefix for the citation started by that line.
266
267 (defvar gnus-cite-loose-prefix-alist nil)
268 ;; Alist of citation prefixes that have no matching attribution.
269 ;; The cdr is a list of lines with that prefix.
270
271 (defvar gnus-cite-loose-attribution-alist nil)
272 ;; Alist of attribution lines that have no matching citation.
273 ;; Each member has the form (WROTE IN PREFIX TAG), where
274 ;; WROTE: is the attribution line number
275 ;; IN: is the line number of the previous line if part of the same attribution,
276 ;; PREFIX: Is the citation prefix of the attribution line(s), and
277 ;; TAG: Is a Supercite tag, if any.
278
279 (defvar gnus-cited-text-button-line-format-alist 
280   `((?b (marker-position beg) ?d)
281     (?e (marker-position end) ?d)
282     (?l (- end beg) ?d)))
283 (defvar gnus-cited-text-button-line-format-spec nil)
284
285 ;;; Commands:
286
287 (defun gnus-article-highlight-citation (&optional force)
288   "Highlight cited text.
289 Each citation in the article will be highlighted with a different face.
290 The faces are taken from `gnus-cite-face-list'.
291 Attribution lines are highlighted with the same face as the
292 corresponding citation merged with `gnus-cite-attribution-face'.
293
294 Text is considered cited if at least `gnus-cite-minimum-match-count'
295 lines matches `gnus-cite-prefix-regexp' with the same prefix.  
296
297 Lines matching `gnus-cite-attribution-suffix' and perhaps
298 `gnus-cite-attribution-prefix' are considered attribution lines."
299   (interactive (list 'force))
300   (save-excursion
301     (set-buffer gnus-article-buffer)
302     (gnus-cite-parse-maybe force)
303     (let ((buffer-read-only nil)
304           (alist gnus-cite-prefix-alist)
305           (faces gnus-cite-face-list)
306           (inhibit-point-motion-hooks t)
307           face entry prefix skip numbers number face-alist)
308       ;; Loop through citation prefixes.
309       (while alist
310         (setq entry (car alist)
311               alist (cdr alist)
312               prefix (car entry)
313               numbers (cdr entry)
314               face (car faces)
315               faces (or (cdr faces) gnus-cite-face-list)
316               face-alist (cons (cons prefix face) face-alist))
317         (while numbers
318           (setq number (car numbers)
319                 numbers (cdr numbers))
320           (and (not (assq number gnus-cite-attribution-alist))
321                (not (assq number gnus-cite-loose-attribution-alist))
322                (gnus-cite-add-face number prefix face))))
323       ;; Loop through attribution lines.
324       (setq alist gnus-cite-attribution-alist)
325       (while alist
326         (setq entry (car alist)
327               alist (cdr alist)
328               number (car entry)
329               prefix (cdr entry)
330               skip (gnus-cite-find-prefix number)
331               face (cdr (assoc prefix face-alist)))
332         ;; Add attribution button.
333         (goto-line number)
334         (when (re-search-forward gnus-cite-attribution-suffix 
335                                  (save-excursion (end-of-line 1) (point))
336                                  t)
337           (gnus-article-add-button (match-beginning 1) (match-end 1)
338                                    'gnus-cite-toggle prefix))
339         ;; Highlight attribution line.
340         (gnus-cite-add-face number skip face)
341         (gnus-cite-add-face number skip gnus-cite-attribution-face))
342       ;; Loop through attribution lines.
343       (setq alist gnus-cite-loose-attribution-alist)
344       (while alist
345         (setq entry (car alist)
346               alist (cdr alist)
347               number (car entry)
348               skip (gnus-cite-find-prefix number))
349         (gnus-cite-add-face number skip gnus-cite-attribution-face)))))
350
351 (defun gnus-dissect-cited-text ()
352   "Dissect the article buffer looking for cited text."
353   (save-excursion
354     (set-buffer gnus-article-buffer)
355     (gnus-cite-parse-maybe)
356     (let ((alist gnus-cite-prefix-alist)
357           prefix numbers number marks m)
358       ;; Loop through citation prefixes.
359       (while alist
360         (setq numbers (pop alist)
361               prefix (pop numbers))
362         (while numbers
363           (setq number (pop numbers))
364           (goto-char (point-min))
365           (forward-line number)
366           (push (cons (point-marker) "") marks)
367           (while (and numbers
368                       (= (1- number) (car numbers)))
369             (setq number (pop numbers)))
370           (goto-char (point-min))
371           (forward-line (1- number))
372           (push (cons (point-marker) prefix) marks)))
373       ;; Skip to the beginning of the body.
374       (goto-char (point-min))
375       (search-forward "\n\n" nil t)
376       (push (cons (point-marker) "") marks)
377       ;; Find the end of the body.
378       (goto-char (point-max))
379       (gnus-article-search-signature)
380       (push (cons (point-marker) "") marks)
381       ;; Sort the marks.
382       (setq marks (sort marks (lambda (m1 m2) (< (car m1) (car m2)))))
383       (let ((omarks marks))
384         (setq marks nil)
385         (while (cdr omarks)
386           (if (= (caar omarks) (caadr omarks))
387               (progn
388                 (unless (equal (cdar omarks) "")
389                   (push (car omarks) marks))
390                 (unless (equal (cdadr omarks) "")
391                   (push (cadr omarks) marks))
392                 (unless (and (equal (cdar omarks) "")
393                              (equal (cdadr omarks) "")
394                              (not (cddr omarks)))
395                   (setq omarks (cdr omarks))))
396             (push (car omarks) marks))
397           (setq omarks (cdr omarks)))
398         (when (car omarks)
399           (push (car omarks) marks))
400         (setq marks (setq m (nreverse marks)))
401         (while (cddr m)
402           (if (and (equal (cdadr m) "")
403                    (equal (cdar m) (cdaddr m))
404                    (goto-char (caadr m))
405                    (forward-line 1)
406                    (= (point) (caaddr m)))
407               (setcdr m (cdddr m))
408             (setq m (cdr m))))
409         marks))))
410
411 (defun gnus-article-fill-cited-article (&optional force)
412   "Do word wrapping in the current article."
413   (interactive (list t))
414   (save-excursion
415     (set-buffer gnus-article-buffer)
416     (let ((buffer-read-only nil)
417           (inhibit-point-motion-hooks t)
418           (marks (gnus-dissect-cited-text))
419           (adaptive-fill-mode nil))
420       (save-restriction
421         (while (cdr marks)
422           (widen)
423           (narrow-to-region (caar marks) (caadr marks))
424           (let ((adaptive-fill-regexp
425                  (concat "^" (regexp-quote (cdar marks)) " *"))
426                 (fill-prefix (cdar marks)))
427             (fill-region (point-min) (point-max)))
428           (set-marker (caar marks) nil)
429           (setq marks (cdr marks)))
430         (when marks
431           (set-marker (caar marks) nil))
432         ;; All this information is now incorrect.
433         (setq gnus-cite-prefix-alist nil
434               gnus-cite-attribution-alist nil
435               gnus-cite-loose-prefix-alist nil
436               gnus-cite-loose-attribution-alist nil)))))
437
438 (defun gnus-article-hide-citation (&optional arg force)
439   "Toggle hiding of all cited text except attribution lines.
440 See the documentation for `gnus-article-highlight-citation'.
441 If given a negative prefix, always show; if given a positive prefix,
442 always hide."
443   (interactive (append (gnus-article-hidden-arg) (list 'force)))
444   (setq gnus-cited-text-button-line-format-spec 
445         (gnus-parse-format gnus-cited-text-button-line-format 
446                            gnus-cited-text-button-line-format-alist t))
447   (save-excursion
448     (set-buffer gnus-article-buffer)
449     (cond
450      ((gnus-article-check-hidden-text 'cite arg)
451       t)
452      ((gnus-article-text-type-exists-p 'cite)
453       (let ((buffer-read-only nil))
454         (gnus-article-hide-text-of-type 'cite)))
455      (t
456       (let ((buffer-read-only nil)
457             (marks (gnus-dissect-cited-text))
458             (inhibit-point-motion-hooks t)
459             (props (nconc (list 'article-type 'cite)
460                           gnus-hidden-properties))
461             beg end)
462         (while marks
463           (setq beg nil
464                 end nil)
465           (while (and marks (string= (cdar marks) ""))
466             (setq marks (cdr marks)))
467           (when marks 
468             (setq beg (caar marks)))
469           (while (and marks (not (string= (cdar marks) "")))
470             (setq marks (cdr marks)))
471           (when marks
472             (setq end (caar marks)))
473           ;; Skip past lines we want to leave visible.
474           (when (and beg end gnus-cited-lines-visible)
475             (goto-char beg)
476             (forward-line gnus-cited-lines-visible)
477             (if (>= (point) end)
478                 (setq beg nil)
479               (setq beg (point-marker))))
480           (when (and beg end)
481             (gnus-add-text-properties beg end props)
482             (goto-char beg)
483             (unless (save-excursion (search-backward "\n\n" nil t))
484               (insert "\n"))
485             (put-text-property
486              (point)
487              (progn
488                (gnus-article-add-button
489                 (point)
490                 (progn (eval gnus-cited-text-button-line-format-spec) (point))
491                 `gnus-article-toggle-cited-text (cons beg end))
492                (point))
493              'article-type 'annotation)
494             (set-marker beg (point)))))))))
495
496 (defun gnus-article-toggle-cited-text (region)
497   "Toggle hiding the text in REGION."
498   (let (buffer-read-only)
499     (funcall
500      (if (text-property-any
501           (car region) (1- (cdr region))
502           (car gnus-hidden-properties) (cadr gnus-hidden-properties))
503          'remove-text-properties 'gnus-add-text-properties)
504      (car region) (cdr region) gnus-hidden-properties)))
505
506 (defun gnus-article-hide-citation-maybe (&optional arg force)
507   "Toggle hiding of cited text that has an attribution line.
508 If given a negative prefix, always show; if given a positive prefix,
509 always hide.
510 This will do nothing unless at least `gnus-cite-hide-percentage'
511 percent and at least `gnus-cite-hide-absolute' lines of the body is
512 cited text with attributions.  When called interactively, these two
513 variables are ignored.
514 See also the documentation for `gnus-article-highlight-citation'."
515   (interactive (append (gnus-article-hidden-arg) (list 'force)))
516   (unless (gnus-article-check-hidden-text 'cite arg)
517     (save-excursion
518       (set-buffer gnus-article-buffer)
519       (gnus-cite-parse-maybe force)
520       (goto-char (point-min))
521       (search-forward "\n\n" nil t)
522       (let ((start (point))
523             (atts gnus-cite-attribution-alist)
524             (buffer-read-only nil)
525             (inhibit-point-motion-hooks t)
526             (hiden 0)
527             total)
528         (goto-char (point-max))
529         (gnus-article-search-signature)
530         (setq total (count-lines start (point)))
531         (while atts
532           (setq hiden (+ hiden (length (cdr (assoc (cdar atts)
533                                                    gnus-cite-prefix-alist))))
534                 atts (cdr atts)))
535         (when (or force
536                   (and (> (* 100 hiden) (* gnus-cite-hide-percentage total))
537                        (> hiden gnus-cite-hide-absolute)))
538           (setq atts gnus-cite-attribution-alist)
539           (while atts
540             (setq total (cdr (assoc (cdar atts) gnus-cite-prefix-alist))
541                   atts (cdr atts))
542             (while total
543               (setq hiden (car total)
544                     total (cdr total))
545               (goto-line hiden)
546               (unless (assq hiden gnus-cite-attribution-alist)
547                 (gnus-add-text-properties 
548                  (point) (progn (forward-line 1) (point))
549                  (nconc (list 'article-type 'cite)
550                         gnus-hidden-properties))))))))))
551
552 (defun gnus-article-hide-citation-in-followups ()
553   "Hide cited text in non-root articles."
554   (interactive)
555   (save-excursion
556     (set-buffer gnus-article-buffer)
557     (let ((article (cdr gnus-article-current)))
558       (unless (save-excursion
559                 (set-buffer gnus-summary-buffer)
560                 (gnus-article-displayed-root-p article))
561         (gnus-article-hide-citation)))))
562
563 ;;; Internal functions:
564
565 (defun gnus-cite-parse-maybe (&optional force)
566   ;; Parse if the buffer has changes since last time.
567   (if (equal gnus-cite-article gnus-article-current)
568       ()
569     ;;Reset parser information.
570     (setq gnus-cite-prefix-alist nil
571           gnus-cite-attribution-alist nil
572           gnus-cite-loose-prefix-alist nil
573           gnus-cite-loose-attribution-alist nil)
574     ;; Parse if not too large.
575     (if (and (not force)
576              gnus-cite-parse-max-size
577              (> (buffer-size) gnus-cite-parse-max-size))
578         ()
579       (setq gnus-cite-article (cons (car gnus-article-current)
580                                     (cdr gnus-article-current)))
581       (gnus-cite-parse-wrapper))))
582
583 (defun gnus-cite-parse-wrapper ()
584   ;; Wrap chopped gnus-cite-parse
585   (goto-char (point-min))
586   (unless (search-forward "\n\n" nil t)
587     (goto-char (point-max)))
588   (save-excursion 
589     (gnus-cite-parse-attributions))
590   ;; Try to avoid check citation if there is no reason to believe
591   ;; that article has citations
592   (if (or gnus-cite-always-check
593           (save-excursion
594             (re-search-backward gnus-cite-reply-regexp nil t))
595           gnus-cite-loose-attribution-alist)
596       (progn (save-excursion
597                (gnus-cite-parse))
598              (save-excursion
599                (gnus-cite-connect-attributions)))))
600
601 (defun gnus-cite-parse ()
602   ;; Parse and connect citation prefixes and attribution lines.
603   
604   ;; Parse current buffer searching for citation prefixes.
605   (let ((line (1+ (count-lines (point-min) (point))))
606         (case-fold-search t)
607         (max (save-excursion
608                (goto-char (point-max))
609                (gnus-article-search-signature)
610                (point)))
611         alist entry start begin end numbers prefix)
612     ;; Get all potential prefixes in `alist'.
613     (while (< (point) max)
614       ;; Each line.
615       (setq begin (point)
616             end (progn (beginning-of-line 2) (point))
617             start end)
618       (goto-char begin)
619       ;; Ignore standard Supercite attribution prefix.
620       (when (looking-at gnus-supercite-regexp)
621         (if (match-end 1)
622             (setq end (1+ (match-end 1)))
623           (setq end (1+ begin))))
624       ;; Ignore very long prefixes.
625       (when (> end (+ (point) gnus-cite-max-prefix))
626         (setq end (+ (point) gnus-cite-max-prefix)))
627       (while (re-search-forward gnus-cite-prefix-regexp (1- end) t)
628         ;; Each prefix.
629         (setq end (match-end 0)
630               prefix (buffer-substring begin end))
631         (gnus-set-text-properties 0 (length prefix) nil prefix)
632         (setq entry (assoc prefix alist))
633         (if entry 
634             (setcdr entry (cons line (cdr entry)))
635           (push (list prefix line) alist))
636         (goto-char begin))
637       (goto-char start)
638       (setq line (1+ line)))
639     ;; We got all the potential prefixes.  Now create
640     ;; `gnus-cite-prefix-alist' containing the oldest prefix for each
641     ;; line that appears at least gnus-cite-minimum-match-count
642     ;; times.  First sort them by length.  Longer is older.
643     (setq alist (sort alist (lambda (a b)
644                               (> (length (car a)) (length (car b))))))
645     (while alist
646       (setq entry (car alist)
647             prefix (car entry)
648             numbers (cdr entry)
649             alist (cdr alist))
650       (cond ((null numbers)
651              ;; No lines with this prefix that wasn't also part of
652              ;; a longer prefix.
653              )
654             ((< (length numbers) gnus-cite-minimum-match-count)
655              ;; Too few lines with this prefix.  We keep it a bit
656              ;; longer in case it is an exact match for an attribution
657              ;; line, but we don't remove the line from other
658              ;; prefixes. 
659              (push entry gnus-cite-prefix-alist))
660             (t
661              (push entry
662                    gnus-cite-prefix-alist)
663              ;; Remove articles from other prefixes.
664              (let ((loop alist)
665                    current)
666                (while loop
667                  (setq current (car loop)
668                        loop (cdr loop))
669                  (setcdr current 
670                          (gnus-set-difference (cdr current) numbers)))))))))
671
672 (defun gnus-cite-parse-attributions ()
673   (let (al-alist)
674     ;; Parse attributions
675     (while (re-search-forward gnus-cite-attribution-suffix (point-max) t)
676       (let* ((start (match-beginning 0))
677              (end (match-end 0))
678              (wrote (count-lines (point-min) end))
679              (prefix (gnus-cite-find-prefix wrote))
680              ;; Check previous line for an attribution leader.
681              (tag (progn
682                     (beginning-of-line 1)
683                     (when (looking-at gnus-supercite-secondary-regexp)
684                       (buffer-substring (match-beginning 1)
685                                         (match-end 1)))))
686              (in (progn
687                    (goto-char start)
688                    (and (re-search-backward gnus-cite-attribution-prefix
689                                             (save-excursion
690                                               (beginning-of-line 0)
691                                               (point))
692                                             t)
693                         (not (re-search-forward gnus-cite-attribution-suffix
694                                                 start t))
695                         (count-lines (point-min) (1+ (point)))))))
696         (when (eq wrote in)
697           (setq in nil))
698         (goto-char end)
699         ;; don't add duplicates
700         (let ((al (buffer-substring (save-excursion (beginning-of-line 0)
701                                                     (1+ (point)))
702                                     end)))
703           (if (not (assoc al al-alist))
704               (progn
705                 (push (list wrote in prefix tag) 
706                       gnus-cite-loose-attribution-alist)
707                 (push (cons al t) al-alist))))))))
708
709 (defun gnus-cite-connect-attributions ()
710   ;; Connect attributions to citations
711
712   ;; No citations have been connected to attribution lines yet.
713   (setq gnus-cite-loose-prefix-alist (append gnus-cite-prefix-alist nil))
714
715   ;; Parse current buffer searching for attribution lines.
716   ;; Find exact supercite citations.
717   (gnus-cite-match-attributions 'small nil
718                                 (lambda (prefix tag)
719                                   (when tag
720                                     (concat "\\`" 
721                                             (regexp-quote prefix) "[ \t]*" 
722                                             (regexp-quote tag) ">"))))
723   ;; Find loose supercite citations after attributions.
724   (gnus-cite-match-attributions 'small t
725                                 (lambda (prefix tag)
726                                   (when tag
727                                     (concat "\\<"
728                                             (regexp-quote tag)
729                                             "\\>"))))
730   ;; Find loose supercite citations anywhere.
731   (gnus-cite-match-attributions 'small nil
732                                 (lambda (prefix tag)
733                                   (when tag
734                                     (concat "\\<"
735                                             (regexp-quote tag)
736                                             "\\>"))))
737   ;; Find nested citations after attributions.
738   (gnus-cite-match-attributions 'small-if-unique t
739                                 (lambda (prefix tag)
740                                   (concat "\\`" (regexp-quote prefix) ".+")))
741   ;; Find nested citations anywhere.
742   (gnus-cite-match-attributions 'small nil
743                                 (lambda (prefix tag)
744                                   (concat "\\`" (regexp-quote prefix) ".+")))
745   ;; Remove loose prefixes with too few lines.
746   (let ((alist gnus-cite-loose-prefix-alist)
747         entry)
748     (while alist
749       (setq entry (car alist)
750             alist (cdr alist))
751       (when (< (length (cdr entry)) gnus-cite-minimum-match-count)
752         (setq gnus-cite-prefix-alist
753               (delq entry gnus-cite-prefix-alist)
754               gnus-cite-loose-prefix-alist
755               (delq entry gnus-cite-loose-prefix-alist)))))
756   ;; Find flat attributions.
757   (gnus-cite-match-attributions 'first t nil)
758   ;; Find any attributions (are we getting desperate yet?).
759   (gnus-cite-match-attributions 'first nil nil))
760
761 (defun gnus-cite-match-attributions (sort after fun)
762   ;; Match all loose attributions and citations (SORT AFTER FUN) .
763   ;;
764   ;; If SORT is `small', the citation with the shortest prefix will be
765   ;; used, if it is `first' the first prefix will be used, if it is
766   ;; `small-if-unique' the shortest prefix will be used if the
767   ;; attribution line does not share its own prefix with other
768   ;; loose attribution lines, otherwise the first prefix will be used.
769   ;;
770   ;; If AFTER is non-nil, only citations after the attribution line
771   ;; will be considered.
772   ;;
773   ;; If FUN is non-nil, it will be called with the arguments (WROTE
774   ;; PREFIX TAG) and expected to return a regular expression.  Only
775   ;; citations whose prefix matches the regular expression will be
776   ;; considered. 
777   ;; 
778   ;; WROTE is the attribution line number.
779   ;; PREFIX is the attribution line prefix.
780   ;; TAG is the Supercite tag on the attribution line.
781   (let ((atts gnus-cite-loose-attribution-alist)
782         (case-fold-search t)
783         att wrote in prefix tag regexp limit smallest best size)
784     (while atts
785       (setq att (car atts)
786             atts (cdr atts)
787             wrote (nth 0 att)
788             in (nth 1 att)
789             prefix (nth 2 att)
790             tag (nth 3 att)
791             regexp (if fun (funcall fun prefix tag) "")
792             size (cond ((eq sort 'small) t)
793                        ((eq sort 'first) nil)
794                        (t (< (length (gnus-cite-find-loose prefix)) 2)))
795             limit (if after wrote -1)
796             smallest 1000000                   
797             best nil)
798       (let ((cites gnus-cite-loose-prefix-alist)
799             cite candidate numbers first compare)
800         (while cites
801           (setq cite (car cites)
802                 cites (cdr cites)
803                 candidate (car cite)
804                 numbers (cdr cite)
805                 first (apply 'min numbers)
806                 compare (if size (length candidate) first))
807           (and (> first limit)
808                regexp
809                (string-match regexp candidate)
810                (< compare smallest)
811                (setq best cite
812                      smallest compare))))
813       (if (null best)
814           ()
815         (setq gnus-cite-loose-attribution-alist
816               (delq att gnus-cite-loose-attribution-alist))
817         (push (cons wrote (car best)) gnus-cite-attribution-alist)
818         (when in
819           (push (cons in (car best)) gnus-cite-attribution-alist))
820         (when (memq best gnus-cite-loose-prefix-alist)
821           (let ((loop gnus-cite-prefix-alist)
822                 (numbers (cdr best))
823                 current)
824             (setq gnus-cite-loose-prefix-alist
825                   (delq best gnus-cite-loose-prefix-alist))
826             (while loop
827               (setq current (car loop)
828                     loop (cdr loop))
829               (if (eq current best)
830                   ()
831                 (setcdr current (gnus-set-difference (cdr current) numbers))
832                 (when (null (cdr current))
833                   (setq gnus-cite-loose-prefix-alist
834                         (delq current gnus-cite-loose-prefix-alist)
835                         atts (delq current atts)))))))))))
836
837 (defun gnus-cite-find-loose (prefix)
838   ;; Return a list of loose attribution lines prefixed by PREFIX.
839   (let* ((atts gnus-cite-loose-attribution-alist)
840          att line lines)
841     (while atts
842       (setq att (car atts)
843             line (car att)
844             atts (cdr atts))
845       (when (string-equal (gnus-cite-find-prefix line) prefix)
846         (push line lines)))
847     lines))
848
849 (defun gnus-cite-add-face (number prefix face)
850   ;; At line NUMBER, ignore PREFIX and add FACE to the rest of the line.
851   (when face
852     (let ((inhibit-point-motion-hooks t)
853           from to)
854       (goto-line number)
855       (unless (eobp);; Sometimes things become confused.
856         (forward-char (length prefix))
857         (skip-chars-forward " \t")
858         (setq from (point))
859         (end-of-line 1)
860         (skip-chars-backward " \t")
861         (setq to (point))
862         (when (< from to)
863           (gnus-overlay-put (gnus-make-overlay from to) 'face face))))))
864
865 (defun gnus-cite-toggle (prefix)
866   (save-excursion
867     (set-buffer gnus-article-buffer)
868     (let ((buffer-read-only nil)
869           (numbers (cdr (assoc prefix gnus-cite-prefix-alist)))
870           (inhibit-point-motion-hooks t)
871           number)
872       (while numbers
873         (setq number (car numbers)
874               numbers (cdr numbers))
875         (goto-line number)
876         (cond ((get-text-property (point) 'invisible)
877                (remove-text-properties (point) (progn (forward-line 1) (point))
878                                        gnus-hidden-properties))
879               ((assq number gnus-cite-attribution-alist))
880               (t
881                (gnus-add-text-properties 
882                 (point) (progn (forward-line 1) (point))
883                 (nconc (list 'article-type 'cite)
884                        gnus-hidden-properties))))))))
885
886 (defun gnus-cite-find-prefix (line)
887   ;; Return citation prefix for LINE.
888   (let ((alist gnus-cite-prefix-alist)
889         (prefix "")
890         entry)
891     (while alist
892       (setq entry (car alist)
893             alist (cdr alist))
894       (when (memq line (cdr entry))
895         (setq prefix (car entry))))
896     prefix))
897
898 (gnus-add-shutdown 'gnus-cache-close 'gnus)
899
900 (defun gnus-cache-close ()
901   (setq gnus-cite-prefix-alist nil))
902
903 (gnus-ems-redefine)
904
905 (provide 'gnus-cite)
906
907 ;;; gnus-cite.el ends here