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