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