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