2001-11-06 10:00:00 ShengHuo ZHU <zsh@cs.rochester.edu>
[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, 2001
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
443                  (if (string= (cdar marks) "") ""
444                    (concat (cdar marks) " "))))
445             (fill-region (point-min) (point-max)))
446           (set-marker (caar marks) nil)
447           (setq marks (cdr marks)))
448         (when marks
449           (set-marker (caar marks) nil))
450         ;; All this information is now incorrect.
451         (setq gnus-cite-prefix-alist nil
452               gnus-cite-attribution-alist nil
453               gnus-cite-loose-prefix-alist nil
454               gnus-cite-loose-attribution-alist nil
455               gnus-cite-article nil)))))
456
457 (defun gnus-article-hide-citation (&optional arg force)
458   "Toggle hiding of all cited text except attribution lines.
459 See the documentation for `gnus-article-highlight-citation'.
460 If given a negative prefix, always show; if given a positive prefix,
461 always hide."
462   (interactive (append (gnus-article-hidden-arg) (list 'force)))
463   (gnus-set-format 'cited-opened-text-button t)
464   (gnus-set-format 'cited-closed-text-button t)
465   (save-excursion
466     (set-buffer gnus-article-buffer)
467       (let ((buffer-read-only nil)
468             marks
469             (inhibit-point-motion-hooks t)
470             (props (nconc (list 'article-type 'cite)
471                           gnus-hidden-properties))
472             (point (point-min))
473             found beg end start)
474         (while (setq point
475                      (text-property-any point (point-max)
476                                         'gnus-callback
477                                         'gnus-article-toggle-cited-text))
478           (setq found t)
479           (goto-char point)
480           (gnus-article-toggle-cited-text
481            (get-text-property point 'gnus-data) arg)
482           (forward-line 1)
483           (setq point (point)))
484         (unless found
485           (setq marks (gnus-dissect-cited-text))
486           (while marks
487             (setq beg nil
488                   end nil)
489             (while (and marks (string= (cdar marks) ""))
490               (setq marks (cdr marks)))
491             (when marks
492               (setq beg (caar marks)))
493             (while (and marks (not (string= (cdar marks) "")))
494               (setq marks (cdr marks)))
495             (when marks
496             (setq end (caar marks)))
497             ;; Skip past lines we want to leave visible.
498             (when (and beg end gnus-cited-lines-visible)
499               (goto-char beg)
500               (forward-line (if (consp gnus-cited-lines-visible)
501                                 (car gnus-cited-lines-visible)
502                               gnus-cited-lines-visible))
503               (if (>= (point) end)
504                   (setq beg nil)
505                 (setq beg (point-marker))
506                 (when (consp gnus-cited-lines-visible)
507                   (goto-char end)
508                   (forward-line (- (cdr gnus-cited-lines-visible)))
509                   (if (<= (point) beg)
510                       (setq beg nil)
511                   (setq end (point-marker))))))
512             (when (and beg end)
513               (or (memq 'cite gnus-article-wash-types)
514                   (push 'cite gnus-article-wash-types))
515               ;; We use markers for the end-points to facilitate later
516               ;; wrapping and mangling of text.
517               (setq beg (set-marker (make-marker) beg)
518                     end (set-marker (make-marker) end))
519               (gnus-add-text-properties-when 'article-type nil beg end props)
520               (goto-char beg)
521               (unless (save-excursion (search-backward "\n\n" nil t))
522                 (insert "\n"))
523               (put-text-property
524                (setq start (point-marker))
525                (progn
526                (gnus-article-add-button
527                 (point)
528                 (progn (eval gnus-cited-closed-text-button-line-format-spec)
529                        (point))
530                 `gnus-article-toggle-cited-text
531                 (list (cons beg end) start))
532                (point))
533                'article-type 'annotation)
534               (set-marker beg (point))))))))
535
536 (defun gnus-article-toggle-cited-text (args &optional arg)
537   "Toggle hiding the text in REGION.
538 ARG can be nil or a number.  Positive means hide, negative
539 means show, nil means toggle."
540   (let* ((region (car args))
541          (beg (car region))
542          (end (cdr region))
543          (start (cadr args))
544          (hidden
545           (text-property-any beg (1- end) 'article-type 'cite))
546          (inhibit-point-motion-hooks t)
547          buffer-read-only)
548     (when (or (null arg)
549               (zerop arg)
550               (and (> arg 0) (not hidden))
551               (and (< arg 0) hidden))
552       (if hidden
553           (progn
554             ;; Can't remove 'cite from g-a-wash-types here because
555             ;; multiple citations may be hidden -jas
556             (gnus-remove-text-properties-when
557              'article-type 'cite beg end
558              (cons 'article-type (cons 'cite
559                                        gnus-hidden-properties))))
560         (or (memq 'cite gnus-article-wash-types)
561             (push 'cite gnus-article-wash-types))
562         (gnus-add-text-properties-when
563          'article-type nil beg end
564          (cons 'article-type (cons 'cite
565                                    gnus-hidden-properties))))
566       (let ((gnus-article-mime-handle-alist-1 gnus-article-mime-handle-alist))
567         (gnus-set-mode-line 'article))
568       (save-excursion
569         (goto-char start)
570         (gnus-delete-line)
571         (put-text-property
572          (point)
573          (progn
574            (gnus-article-add-button
575             (point)
576             (progn (eval
577                     (if hidden
578                         gnus-cited-opened-text-button-line-format-spec
579                       gnus-cited-closed-text-button-line-format-spec))
580                    (point))
581             `gnus-article-toggle-cited-text
582             args)
583            (point))
584          'article-type 'annotation)))))
585
586 (defun gnus-article-hide-citation-maybe (&optional arg force)
587   "Toggle hiding of cited text that has an attribution line.
588 If given a negative prefix, always show; if given a positive prefix,
589 always hide.
590 This will do nothing unless at least `gnus-cite-hide-percentage'
591 percent and at least `gnus-cite-hide-absolute' lines of the body is
592 cited text with attributions.  When called interactively, these two
593 variables are ignored.
594 See also the documentation for `gnus-article-highlight-citation'."
595   (interactive (append (gnus-article-hidden-arg) '(force)))
596   (unless (gnus-article-check-hidden-text 'cite arg)
597     (save-excursion
598       (set-buffer gnus-article-buffer)
599       (gnus-cite-parse-maybe force)
600       (article-goto-body)
601       (let ((start (point))
602             (atts gnus-cite-attribution-alist)
603             (buffer-read-only nil)
604             (inhibit-point-motion-hooks t)
605             (hidden 0)
606             total)
607         (goto-char (point-max))
608         (gnus-article-search-signature)
609         (setq total (count-lines start (point)))
610         (while atts
611           (setq hidden (+ hidden (length (cdr (assoc (cdar atts)
612                                                      gnus-cite-prefix-alist))))
613                 atts (cdr atts)))
614         (when (or force
615                   (and (> (* 100 hidden) (* gnus-cite-hide-percentage total))
616                        (> hidden gnus-cite-hide-absolute)))
617           (setq atts gnus-cite-attribution-alist)
618           (while atts
619             (setq total (cdr (assoc (cdar atts) gnus-cite-prefix-alist))
620                   atts (cdr atts))
621             (while total
622               (setq hidden (car total)
623                     total (cdr total))
624               (goto-char (point-min))
625               (forward-line (1- hidden))
626               (unless (assq hidden gnus-cite-attribution-alist)
627                 (gnus-add-text-properties
628                  (point) (progn (forward-line 1) (point))
629                  (nconc (list 'article-type 'cite)
630                         gnus-hidden-properties))))))))))
631
632 (defun gnus-article-hide-citation-in-followups ()
633   "Hide cited text in non-root articles."
634   (interactive)
635   (save-excursion
636     (set-buffer gnus-article-buffer)
637     (let ((article (cdr gnus-article-current)))
638       (unless (save-excursion
639                 (set-buffer gnus-summary-buffer)
640                 (gnus-article-displayed-root-p article))
641         (gnus-article-hide-citation)))))
642
643 ;;; Internal functions:
644
645 (defun gnus-cite-parse-maybe (&optional force no-overlay)
646   "Always parse the buffer."
647   (gnus-cite-localize)
648   ;;Reset parser information.
649   (setq gnus-cite-prefix-alist nil
650         gnus-cite-attribution-alist nil
651         gnus-cite-loose-prefix-alist nil
652         gnus-cite-loose-attribution-alist nil)
653   (unless no-overlay
654     (gnus-cite-delete-overlays))
655   ;; Parse if not too large.
656   (if (and gnus-cite-parse-max-size
657            (> (buffer-size) gnus-cite-parse-max-size))
658       ()
659     (setq gnus-cite-article (cons (car gnus-article-current)
660                                   (cdr gnus-article-current)))
661     (gnus-cite-parse-wrapper)))
662
663 (defun gnus-cite-delete-overlays ()
664   (dolist (overlay gnus-cite-overlay-list)
665     (when (or (not (gnus-overlay-end overlay))
666               (and (>= (gnus-overlay-end overlay) (point-min))
667                    (<= (gnus-overlay-end overlay) (point-max))))
668       (setq gnus-cite-overlay-list (delete overlay gnus-cite-overlay-list))
669       (gnus-delete-overlay overlay))))
670
671 (defun gnus-cite-parse-wrapper ()
672   ;; Wrap chopped gnus-cite-parse.
673   (article-goto-body)
674   (let ((inhibit-point-motion-hooks t))
675     (save-excursion
676       (gnus-cite-parse-attributions))
677     (save-excursion
678       (gnus-cite-parse))
679     (save-excursion
680       (gnus-cite-connect-attributions))))
681
682 (defun gnus-cite-parse ()
683   ;; Parse and connect citation prefixes and attribution lines.
684
685   ;; Parse current buffer searching for citation prefixes.
686   (let ((line (1+ (count-lines (point-min) (point))))
687         (case-fold-search t)
688         (max (save-excursion
689                (goto-char (point-max))
690                (gnus-article-search-signature)
691                (point)))
692         (prefix-regexp (concat "^\\(" message-cite-prefix-regexp "\\)"))
693         alist entry start begin end numbers prefix guess-limit)
694     ;; Get all potential prefixes in `alist'.
695     (while (< (point) max)
696       ;; Each line.
697       (setq begin (point)
698             guess-limit (progn (skip-chars-forward "^> \t\r\n") (point))
699             end (progn (beginning-of-line 2) (point))
700             start end)
701       (goto-char begin)
702       ;; Ignore standard Supercite attribution prefix.
703       (when (and (< guess-limit (+ begin gnus-cite-max-prefix))
704                  (looking-at gnus-supercite-regexp))
705         (if (match-end 1)
706             (setq end (1+ (match-end 1)))
707           (setq end (1+ begin))))
708       ;; Ignore very long prefixes.
709       (when (> end (+ begin gnus-cite-max-prefix))
710         (setq end (+ begin gnus-cite-max-prefix)))
711       (while (re-search-forward prefix-regexp (1- end) t)
712         ;; Each prefix.
713         (setq end (match-end 0)
714               prefix (buffer-substring begin end))
715         (gnus-set-text-properties 0 (length prefix) nil prefix)
716         (setq entry (assoc prefix alist))
717         (if entry
718             (setcdr entry (cons line (cdr entry)))
719           (push (list prefix line) alist))
720         (goto-char begin))
721       (goto-char start)
722       (setq line (1+ line)))
723     ;; We got all the potential prefixes.  Now create
724     ;; `gnus-cite-prefix-alist' containing the oldest prefix for each
725     ;; line that appears at least gnus-cite-minimum-match-count
726     ;; times.  First sort them by length.  Longer is older.
727     (setq alist (sort alist (lambda (a b)
728                               (> (length (car a)) (length (car b))))))
729     (while alist
730       (setq entry (car alist)
731             prefix (car entry)
732             numbers (cdr entry)
733             alist (cdr alist))
734       (cond ((null numbers)
735              ;; No lines with this prefix that wasn't also part of
736              ;; a longer prefix.
737              )
738             ((< (length numbers) gnus-cite-minimum-match-count)
739              ;; Too few lines with this prefix.  We keep it a bit
740              ;; longer in case it is an exact match for an attribution
741              ;; line, but we don't remove the line from other
742              ;; prefixes.
743              (push entry gnus-cite-prefix-alist))
744             (t
745              (push entry
746                    gnus-cite-prefix-alist)
747              ;; Remove articles from other prefixes.
748              (let ((loop alist)
749                    current)
750                (while loop
751                  (setq current (car loop)
752                        loop (cdr loop))
753                  (setcdr current
754                          (gnus-set-difference (cdr current) numbers)))))))))
755
756 (defun gnus-cite-parse-attributions ()
757   (let (al-alist)
758     ;; Parse attributions
759     (while (re-search-forward gnus-cite-attribution-suffix (point-max) t)
760       (let* ((start (match-beginning 0))
761              (end (match-end 0))
762              (wrote (count-lines (point-min) end))
763              (prefix (gnus-cite-find-prefix wrote))
764              ;; Check previous line for an attribution leader.
765              (tag (progn
766                     (beginning-of-line 1)
767                     (when (looking-at gnus-supercite-secondary-regexp)
768                       (buffer-substring (match-beginning 1)
769                                         (match-end 1)))))
770              (in (progn
771                    (goto-char start)
772                    (and (re-search-backward gnus-cite-attribution-prefix
773                                             (save-excursion
774                                               (beginning-of-line 0)
775                                               (point))
776                                             t)
777                         (not (re-search-forward gnus-cite-attribution-suffix
778                                                 start t))
779                         (count-lines (point-min) (1+ (point)))))))
780         (when (eq wrote in)
781           (setq in nil))
782         (goto-char end)
783         ;; don't add duplicates
784         (let ((al (buffer-substring (save-excursion (beginning-of-line 0)
785                                                     (1+ (point)))
786                                     end)))
787           (if (not (assoc al al-alist))
788               (progn
789                 (push (list wrote in prefix tag)
790                       gnus-cite-loose-attribution-alist)
791                 (push (cons al t) al-alist))))))))
792
793 (defun gnus-cite-connect-attributions ()
794   ;; Connect attributions to citations
795
796   ;; No citations have been connected to attribution lines yet.
797   (setq gnus-cite-loose-prefix-alist (append gnus-cite-prefix-alist nil))
798
799   ;; Parse current buffer searching for attribution lines.
800   ;; Find exact supercite citations.
801   (gnus-cite-match-attributions 'small nil
802                                 (lambda (prefix tag)
803                                   (when tag
804                                     (concat "\\`"
805                                             (regexp-quote prefix) "[ \t]*"
806                                             (regexp-quote tag) ">"))))
807   ;; Find loose supercite citations after attributions.
808   (gnus-cite-match-attributions 'small t
809                                 (lambda (prefix tag)
810                                   (when tag
811                                     (concat "\\<"
812                                             (regexp-quote tag)
813                                             "\\>"))))
814   ;; Find loose supercite citations anywhere.
815   (gnus-cite-match-attributions 'small nil
816                                 (lambda (prefix tag)
817                                   (when tag
818                                     (concat "\\<"
819                                             (regexp-quote tag)
820                                             "\\>"))))
821   ;; Find nested citations after attributions.
822   (gnus-cite-match-attributions 'small-if-unique t
823                                 (lambda (prefix tag)
824                                   (concat "\\`" (regexp-quote prefix) ".+")))
825   ;; Find nested citations anywhere.
826   (gnus-cite-match-attributions 'small nil
827                                 (lambda (prefix tag)
828                                   (concat "\\`" (regexp-quote prefix) ".+")))
829   ;; Remove loose prefixes with too few lines.
830   (let ((alist gnus-cite-loose-prefix-alist)
831         entry)
832     (while alist
833       (setq entry (car alist)
834             alist (cdr alist))
835       (when (< (length (cdr entry)) gnus-cite-minimum-match-count)
836         (setq gnus-cite-prefix-alist
837               (delq entry gnus-cite-prefix-alist)
838               gnus-cite-loose-prefix-alist
839               (delq entry gnus-cite-loose-prefix-alist)))))
840   ;; Find flat attributions.
841   (gnus-cite-match-attributions 'first t nil)
842   ;; Find any attributions (are we getting desperate yet?).
843   (gnus-cite-match-attributions 'first nil nil))
844
845 (defun gnus-cite-match-attributions (sort after fun)
846   ;; Match all loose attributions and citations (SORT AFTER FUN) .
847   ;;
848   ;; If SORT is `small', the citation with the shortest prefix will be
849   ;; used, if it is `first' the first prefix will be used, if it is
850   ;; `small-if-unique' the shortest prefix will be used if the
851   ;; attribution line does not share its own prefix with other
852   ;; loose attribution lines, otherwise the first prefix will be used.
853   ;;
854   ;; If AFTER is non-nil, only citations after the attribution line
855   ;; will be considered.
856   ;;
857   ;; If FUN is non-nil, it will be called with the arguments (WROTE
858   ;; PREFIX TAG) and expected to return a regular expression.  Only
859   ;; citations whose prefix matches the regular expression will be
860   ;; considered.
861   ;;
862   ;; WROTE is the attribution line number.
863   ;; PREFIX is the attribution line prefix.
864   ;; TAG is the Supercite tag on the attribution line.
865   (let ((atts gnus-cite-loose-attribution-alist)
866         (case-fold-search t)
867         att wrote in prefix tag regexp limit smallest best size)
868     (while atts
869       (setq att (car atts)
870             atts (cdr atts)
871             wrote (nth 0 att)
872             in (nth 1 att)
873             prefix (nth 2 att)
874             tag (nth 3 att)
875             regexp (if fun (funcall fun prefix tag) "")
876             size (cond ((eq sort 'small) t)
877                        ((eq sort 'first) nil)
878                        (t (< (length (gnus-cite-find-loose prefix)) 2)))
879             limit (if after wrote -1)
880             smallest 1000000
881             best nil)
882       (let ((cites gnus-cite-loose-prefix-alist)
883             cite candidate numbers first compare)
884         (while cites
885           (setq cite (car cites)
886                 cites (cdr cites)
887                 candidate (car cite)
888                 numbers (cdr cite)
889                 first (apply 'min numbers)
890                 compare (if size (length candidate) first))
891           (and (> first limit)
892                regexp
893                (string-match regexp candidate)
894                (< compare smallest)
895                (setq best cite
896                      smallest compare))))
897       (if (null best)
898           ()
899         (setq gnus-cite-loose-attribution-alist
900               (delq att gnus-cite-loose-attribution-alist))
901         (push (cons wrote (car best)) gnus-cite-attribution-alist)
902         (when in
903           (push (cons in (car best)) gnus-cite-attribution-alist))
904         (when (memq best gnus-cite-loose-prefix-alist)
905           (let ((loop gnus-cite-prefix-alist)
906                 (numbers (cdr best))
907                 current)
908             (setq gnus-cite-loose-prefix-alist
909                   (delq best gnus-cite-loose-prefix-alist))
910             (while loop
911               (setq current (car loop)
912                     loop (cdr loop))
913               (if (eq current best)
914                   ()
915                 (setcdr current (gnus-set-difference (cdr current) numbers))
916                 (when (null (cdr current))
917                   (setq gnus-cite-loose-prefix-alist
918                         (delq current gnus-cite-loose-prefix-alist)
919                         atts (delq current atts)))))))))))
920
921 (defun gnus-cite-find-loose (prefix)
922   ;; Return a list of loose attribution lines prefixed by PREFIX.
923   (let* ((atts gnus-cite-loose-attribution-alist)
924          att line lines)
925     (while atts
926       (setq att (car atts)
927             line (car att)
928             atts (cdr atts))
929       (when (string-equal (gnus-cite-find-prefix line) prefix)
930         (push line lines)))
931     lines))
932
933 (defun gnus-cite-add-face (number prefix face)
934   ;; At line NUMBER, ignore PREFIX and add FACE to the rest of the line.
935   (when face
936     (let ((inhibit-point-motion-hooks t)
937           from to overlay)
938       (goto-char (point-min))
939       (when (zerop (forward-line (1- number)))
940         (forward-char (length prefix))
941         (skip-chars-forward " \t")
942         (setq from (point))
943         (end-of-line 1)
944         (skip-chars-backward " \t")
945         (setq to (point))
946         (when (< from to)
947           (push (setq overlay (gnus-make-overlay from to))
948                 gnus-cite-overlay-list)
949           (gnus-overlay-put overlay 'face face))))))
950
951 (defun gnus-cite-toggle (prefix)
952   (save-excursion
953     (set-buffer gnus-article-buffer)
954     (gnus-cite-parse-maybe nil t)
955     (let ((buffer-read-only nil)
956           (numbers (cdr (assoc prefix gnus-cite-prefix-alist)))
957           (inhibit-point-motion-hooks t)
958           number)
959       (while numbers
960         (setq number (car numbers)
961               numbers (cdr numbers))
962         (goto-char (point-min))
963         (forward-line (1- number))
964         (cond ((get-text-property (point) 'invisible)
965                ;; Can't remove 'cite from g-a-wash-types here because
966                ;; multiple citations may be hidden -jas
967                (remove-text-properties (point) (progn (forward-line 1) (point))
968                                        gnus-hidden-properties))
969               ((assq number gnus-cite-attribution-alist))
970               (t
971                (or (memq 'cite gnus-article-wash-types)
972                    (push 'cite gnus-article-wash-types))
973                (gnus-add-text-properties
974                 (point) (progn (forward-line 1) (point))
975                 (nconc (list 'article-type 'cite)
976                        gnus-hidden-properties))))
977         (let ((gnus-article-mime-handle-alist-1
978                gnus-article-mime-handle-alist))
979           (gnus-set-mode-line 'article))))))
980
981 (defun gnus-cite-find-prefix (line)
982   ;; Return citation prefix for LINE.
983   (let ((alist gnus-cite-prefix-alist)
984         (prefix "")
985         entry)
986     (while alist
987       (setq entry (car alist)
988             alist (cdr alist))
989       (when (memq line (cdr entry))
990         (setq prefix (car entry))))
991     prefix))
992
993 (defun gnus-cite-localize ()
994   "Make the citation variables local to the article buffer."
995   (let ((vars '(gnus-cite-article
996                 gnus-cite-overlay-list gnus-cite-prefix-alist
997                 gnus-cite-attribution-alist gnus-cite-loose-prefix-alist
998                 gnus-cite-loose-attribution-alist)))
999     (while vars
1000       (make-local-variable (pop vars)))))
1001
1002 (gnus-ems-redefine)
1003
1004 (provide 'gnus-cite)
1005
1006 ;; Local Variables:
1007 ;; coding: iso-8859-1
1008 ;; End:
1009
1010 ;;; gnus-cite.el ends here