* gnus.el: Fix copyright statements.
[gnus] / lisp / gnus-cite.el
1 ;;; gnus-cite.el --- parse citations in articles for Gnus
2 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Per Abhiddenware; you can redistribute it and/or modify
6 ;; it under the terms of the GNU General Public License as published by
7 ;; the Free Software Foundation; either version 2, or (at your option)
8 ;; any later version.
9
10 ;; GNU Emacs is distributed in the hope that it will be useful,
11 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 ;; GNU General Public License for more details.
14
15 ;; You should have received a copy of the GNU General Public License
16 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
17 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 ;; Boston, MA 02111-1307, USA.
19
20 ;;; Commentary:
21
22 ;;; Code:
23
24 (eval-when-compile (require 'cl))
25
26 (require 'gnus)
27 (require 'gnus-art)
28 (require 'gnus-range)
29
30 ;;; Customization:
31
32 (defgroup gnus-cite nil
33   "Citation."
34   :prefix "gnus-cite-"
35   :link '(custom-manual "(gnus)Article Highlighting")
36   :group 'gnus-article)
37
38 (defcustom gnus-cite-reply-regexp
39   "^\\(Subject: Re\\|In-Reply-To\\|References\\):"
40   "*If headers match this regexp it is reasonable to believe that
41 article has citations."
42   :group 'gnus-cite
43   :type 'string)
44
45 (defcustom gnus-cite-always-check nil
46   "Check article always for citations.  Set it t to check all articles."
47   :group 'gnus-cite
48   :type '(choice (const :tag "no" nil)
49                  (const :tag "yes" t)))
50
51 (defcustom gnus-cited-opened-text-button-line-format "%(%{[-]%}%)\n"
52   "Format of opened cited text buttons."
53   :group 'gnus-cite
54   :type 'string)
55
56 (defcustom gnus-cited-closed-text-button-line-format "%(%{[+]%}%)\n"
57   "Format of closed cited text buttons."
58   :group 'gnus-cite
59   :type 'string)
60
61 (defcustom gnus-cited-lines-visible nil
62   "The number of lines of hidden cited text to remain visible.
63 Or a pair (cons) of numbers which are the number of lines at the top
64 and bottom of the text, respectively, to remain visible."
65   :group 'gnus-cite
66   :type '(choice (const :tag "none" nil)
67                  integer
68                  (cons :tag "Top and Bottom" integer integer)))
69
70 (defcustom gnus-cite-parse-max-size 25000
71   "Maximum article size (in bytes) where parsing citations is allowed.
72 Set it to nil to parse all articles."
73   :group 'gnus-cite
74   :type '(choice (const :tag "all" nil)
75                  integer))
76
77 (defcustom gnus-cite-prefix-regexp
78   "^[]>»|:}+ ]*[]>»|:}+]\\(.*>»\\)?\\|^.*>"
79   "*Regexp matching the longest possible citation prefix on a line."
80   :group 'gnus-cite
81   :type 'regexp)
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 "^\\(" gnus-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 `gnus-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     (cond
466      ((gnus-article-check-hidden-text 'cite arg)
467       t)
468      ((gnus-article-text-type-exists-p 'cite)
469       (let ((buffer-read-only nil))
470         (gnus-article-hide-text-of-type 'cite)))
471      (t
472       (let ((buffer-read-only nil)
473             (marks (gnus-dissect-cited-text))
474             (inhibit-point-motion-hooks t)
475             (props (nconc (list 'article-type 'cite)
476                           gnus-hidden-properties))
477             beg end start)
478         (while marks
479           (setq beg nil
480                 end nil)
481           (while (and marks (string= (cdar marks) ""))
482             (setq marks (cdr marks)))
483           (when marks
484             (setq beg (caar marks)))
485           (while (and marks (not (string= (cdar marks) "")))
486             (setq marks (cdr marks)))
487           (when marks
488             (setq end (caar marks)))
489           ;; Skip past lines we want to leave visible.
490           (when (and beg end gnus-cited-lines-visible)
491             (goto-char beg)
492             (forward-line (if (consp gnus-cited-lines-visible)
493                               (car gnus-cited-lines-visible)
494                             gnus-cited-lines-visible))
495             (if (>= (point) end)
496                 (setq beg nil)
497               (setq beg (point-marker))
498               (when (consp gnus-cited-lines-visible)
499                 (goto-char end)
500                 (forward-line (- (cdr gnus-cited-lines-visible)))
501                 (if (<= (point) beg)
502                     (setq beg nil)
503                   (setq end (point-marker))))))
504           (when (and beg end)
505             ;; We use markers for the end-points to facilitate later
506             ;; wrapping and mangling of text.
507             (setq beg (set-marker (make-marker) beg)
508                   end (set-marker (make-marker) end))
509             (gnus-add-text-properties beg end props)
510             (goto-char beg)
511             (unless (save-excursion (search-backward "\n\n" nil t))
512               (insert "\n"))
513             (put-text-property
514              (setq start (point-marker))
515              (progn
516                (gnus-article-add-button
517                 (point)
518                 (progn (eval gnus-cited-closed-text-button-line-format-spec)
519                        (point))
520                 `gnus-article-toggle-cited-text
521                 (list (cons beg end) start))
522                (point))
523              'article-type 'annotation)
524             (set-marker beg (point)))))))))
525
526 (defun gnus-article-toggle-cited-text (args)
527   "Toggle hiding the text in REGION."
528   (let* ((region (car args))
529          (beg (car region))
530          (end (cdr region))
531          (start (cadr args))
532          (hidden
533           (text-property-any
534            beg (1- end)
535            (car gnus-hidden-properties) (cadr gnus-hidden-properties)))
536          (inhibit-point-motion-hooks t)
537          buffer-read-only)
538     (funcall
539      (if hidden
540          'remove-text-properties 'gnus-add-text-properties)
541      beg end gnus-hidden-properties)
542     (save-excursion
543       (goto-char start)
544       (gnus-delete-line)
545       (put-text-property
546        (point)
547        (progn
548          (gnus-article-add-button
549           (point)
550           (progn (eval
551                   (if hidden
552                       gnus-cited-opened-text-button-line-format-spec
553                     gnus-cited-closed-text-button-line-format-spec))
554                  (point))
555           `gnus-article-toggle-cited-text
556           args)
557          (point))
558        'article-type 'annotation))))
559
560 (defun gnus-article-hide-citation-maybe (&optional arg force)
561   "Toggle hiding of cited text that has an attribution line.
562 If given a negative prefix, always show; if given a positive prefix,
563 always hide.
564 This will do nothing unless at least `gnus-cite-hide-percentage'
565 percent and at least `gnus-cite-hide-absolute' lines of the body is
566 cited text with attributions.  When called interactively, these two
567 variables are ignored.
568 See also the documentation for `gnus-article-highlight-citation'."
569   (interactive (append (gnus-article-hidden-arg) '(force)))
570   (unless (gnus-article-check-hidden-text 'cite arg)
571     (save-excursion
572       (set-buffer gnus-article-buffer)
573       (gnus-cite-parse-maybe force)
574       (article-goto-body)
575       (let ((start (point))
576             (atts gnus-cite-attribution-alist)
577             (buffer-read-only nil)
578             (inhibit-point-motion-hooks t)
579             (hidden 0)
580             total)
581         (goto-char (point-max))
582         (gnus-article-search-signature)
583         (setq total (count-lines start (point)))
584         (while atts
585           (setq hidden (+ hidden (length (cdr (assoc (cdar atts)
586                                                      gnus-cite-prefix-alist))))
587                 atts (cdr atts)))
588         (when (or force
589                   (and (> (* 100 hidden) (* gnus-cite-hide-percentage total))
590                        (> hidden gnus-cite-hide-absolute)))
591           (setq atts gnus-cite-attribution-alist)
592           (while atts
593             (setq total (cdr (assoc (cdar atts) gnus-cite-prefix-alist))
594                   atts (cdr atts))
595             (while total
596               (setq hidden (car total)
597                     total (cdr total))
598               (goto-char (point-min))
599               (forward-line (1- hidden))
600               (unless (assq hidden gnus-cite-attribution-alist)
601                 (gnus-add-text-properties
602                  (point) (progn (forward-line 1) (point))
603                  (nconc (list 'article-type 'cite)
604                         gnus-hidden-properties))))))))))
605
606 (defun gnus-article-hide-citation-in-followups ()
607   "Hide cited text in non-root articles."
608   (interactive)
609   (save-excursion
610     (set-buffer gnus-article-buffer)
611     (let ((article (cdr gnus-article-current)))
612       (unless (save-excursion
613                 (set-buffer gnus-summary-buffer)
614                 (gnus-article-displayed-root-p article))
615         (gnus-article-hide-citation)))))
616
617 ;;; Internal functions:
618
619 (defun gnus-cite-parse-maybe (&optional force no-overlay)
620   "Always parse the buffer."
621   (gnus-cite-localize)
622   ;;Reset parser information.
623   (setq gnus-cite-prefix-alist nil
624         gnus-cite-attribution-alist nil
625         gnus-cite-loose-prefix-alist nil
626         gnus-cite-loose-attribution-alist nil)
627   (unless no-overlay
628     (gnus-cite-delete-overlays))
629   ;; Parse if not too large.
630   (if (and gnus-cite-parse-max-size
631            (> (buffer-size) gnus-cite-parse-max-size))
632       ()
633     (setq gnus-cite-article (cons (car gnus-article-current)
634                                   (cdr gnus-article-current)))
635     (gnus-cite-parse-wrapper)))
636
637 (defun gnus-cite-delete-overlays ()
638   (dolist (overlay gnus-cite-overlay-list)
639     (when (or (not (gnus-overlay-end overlay))
640               (and (>= (gnus-overlay-end overlay) (point-min))
641                    (<= (gnus-overlay-end overlay) (point-max))))
642       (setq gnus-cite-overlay-list (delete overlay gnus-cite-overlay-list))
643       (gnus-delete-overlay overlay))))
644
645 (defun gnus-cite-parse-wrapper ()
646   ;; Wrap chopped gnus-cite-parse.
647   (article-goto-body)
648   (let ((inhibit-point-motion-hooks t))
649     (save-excursion
650       (gnus-cite-parse-attributions))
651     (save-excursion
652       (gnus-cite-parse))
653     (save-excursion
654       (gnus-cite-connect-attributions))))
655
656 (defun gnus-cite-parse ()
657   ;; Parse and connect citation prefixes and attribution lines.
658
659   ;; Parse current buffer searching for citation prefixes.
660   (let ((line (1+ (count-lines (point-min) (point))))
661         (case-fold-search t)
662         (max (save-excursion
663                (goto-char (point-max))
664                (gnus-article-search-signature)
665                (point)))
666         alist entry start begin end numbers prefix)
667     ;; Get all potential prefixes in `alist'.
668     (while (< (point) max)
669       ;; Each line.
670       (setq begin (point)
671             end (progn (beginning-of-line 2) (point))
672             start end)
673       (goto-char begin)
674       ;; Ignore standard Supercite attribution prefix.
675       (when (looking-at gnus-supercite-regexp)
676         (if (match-end 1)
677             (setq end (1+ (match-end 1)))
678           (setq end (1+ begin))))
679       ;; Ignore very long prefixes.
680       (when (> end (+ (point) gnus-cite-max-prefix))
681         (setq end (+ (point) gnus-cite-max-prefix)))
682       (while (re-search-forward gnus-cite-prefix-regexp (1- end) t)
683         ;; Each prefix.
684         (setq end (match-end 0)
685               prefix (buffer-substring begin end))
686         (gnus-set-text-properties 0 (length prefix) nil prefix)
687         (setq entry (assoc prefix alist))
688         (if entry
689             (setcdr entry (cons line (cdr entry)))
690           (push (list prefix line) alist))
691         (goto-char begin))
692       (goto-char start)
693       (setq line (1+ line)))
694     ;; We got all the potential prefixes.  Now create
695     ;; `gnus-cite-prefix-alist' containing the oldest prefix for each
696     ;; line that appears at least gnus-cite-minimum-match-count
697     ;; times.  First sort them by length.  Longer is older.
698     (setq alist (sort alist (lambda (a b)
699                               (> (length (car a)) (length (car b))))))
700     (while alist
701       (setq entry (car alist)
702             prefix (car entry)
703             numbers (cdr entry)
704             alist (cdr alist))
705       (cond ((null numbers)
706              ;; No lines with this prefix that wasn't also part of
707              ;; a longer prefix.
708              )
709             ((< (length numbers) gnus-cite-minimum-match-count)
710              ;; Too few lines with this prefix.  We keep it a bit
711              ;; longer in case it is an exact match for an attribution
712              ;; line, but we don't remove the line from other
713              ;; prefixes.
714              (push entry gnus-cite-prefix-alist))
715             (t
716              (push entry
717                    gnus-cite-prefix-alist)
718              ;; Remove articles from other prefixes.
719              (let ((loop alist)
720                    current)
721                (while loop
722                  (setq current (car loop)
723                        loop (cdr loop))
724                  (setcdr current
725                          (gnus-set-difference (cdr current) numbers)))))))))
726
727 (defun gnus-cite-parse-attributions ()
728   (let (al-alist)
729     ;; Parse attributions
730     (while (re-search-forward gnus-cite-attribution-suffix (point-max) t)
731       (let* ((start (match-beginning 0))
732              (end (match-end 0))
733              (wrote (count-lines (point-min) end))
734              (prefix (gnus-cite-find-prefix wrote))
735              ;; Check previous line for an attribution leader.
736              (tag (progn
737                     (beginning-of-line 1)
738                     (when (looking-at gnus-supercite-secondary-regexp)
739                       (buffer-substring (match-beginning 1)
740                                         (match-end 1)))))
741              (in (progn
742                    (goto-char start)
743                    (and (re-search-backward gnus-cite-attribution-prefix
744                                             (save-excursion
745                                               (beginning-of-line 0)
746                                               (point))
747                                             t)
748                         (not (re-search-forward gnus-cite-attribution-suffix
749                                                 start t))
750                         (count-lines (point-min) (1+ (point)))))))
751         (when (eq wrote in)
752           (setq in nil))
753         (goto-char end)
754         ;; don't add duplicates
755         (let ((al (buffer-substring (save-excursion (beginning-of-line 0)
756                                                     (1+ (point)))
757                                     end)))
758           (if (not (assoc al al-alist))
759               (progn
760                 (push (list wrote in prefix tag)
761                       gnus-cite-loose-attribution-alist)
762                 (push (cons al t) al-alist))))))))
763
764 (defun gnus-cite-connect-attributions ()
765   ;; Connect attributions to citations
766
767   ;; No citations have been connected to attribution lines yet.
768   (setq gnus-cite-loose-prefix-alist (append gnus-cite-prefix-alist nil))
769
770   ;; Parse current buffer searching for attribution lines.
771   ;; Find exact supercite citations.
772   (gnus-cite-match-attributions 'small nil
773                                 (lambda (prefix tag)
774                                   (when tag
775                                     (concat "\\`"
776                                             (regexp-quote prefix) "[ \t]*"
777                                             (regexp-quote tag) ">"))))
778   ;; Find loose supercite citations after attributions.
779   (gnus-cite-match-attributions 'small t
780                                 (lambda (prefix tag)
781                                   (when tag
782                                     (concat "\\<"
783                                             (regexp-quote tag)
784                                             "\\>"))))
785   ;; Find loose supercite citations anywhere.
786   (gnus-cite-match-attributions 'small nil
787                                 (lambda (prefix tag)
788                                   (when tag
789                                     (concat "\\<"
790                                             (regexp-quote tag)
791                                             "\\>"))))
792   ;; Find nested citations after attributions.
793   (gnus-cite-match-attributions 'small-if-unique t
794                                 (lambda (prefix tag)
795                                   (concat "\\`" (regexp-quote prefix) ".+")))
796   ;; Find nested citations anywhere.
797   (gnus-cite-match-attributions 'small nil
798                                 (lambda (prefix tag)
799                                   (concat "\\`" (regexp-quote prefix) ".+")))
800   ;; Remove loose prefixes with too few lines.
801   (let ((alist gnus-cite-loose-prefix-alist)
802         entry)
803     (while alist
804       (setq entry (car alist)
805             alist (cdr alist))
806       (when (< (length (cdr entry)) gnus-cite-minimum-match-count)
807         (setq gnus-cite-prefix-alist
808               (delq entry gnus-cite-prefix-alist)
809               gnus-cite-loose-prefix-alist
810               (delq entry gnus-cite-loose-prefix-alist)))))
811   ;; Find flat attributions.
812   (gnus-cite-match-attributions 'first t nil)
813   ;; Find any attributions (are we getting desperate yet?).
814   (gnus-cite-match-attributions 'first nil nil))
815
816 (defun gnus-cite-match-attributions (sort after fun)
817   ;; Match all loose attributions and citations (SORT AFTER FUN) .
818   ;;
819   ;; If SORT is `small', the citation with the shortest prefix will be
820   ;; used, if it is `first' the first prefix will be used, if it is
821   ;; `small-if-unique' the shortest prefix will be used if the
822   ;; attribution line does not share its own prefix with other
823   ;; loose attribution lines, otherwise the first prefix will be used.
824   ;;
825   ;; If AFTER is non-nil, only citations after the attribution line
826   ;; will be considered.
827   ;;
828   ;; If FUN is non-nil, it will be called with the arguments (WROTE
829   ;; PREFIX TAG) and expected to return a regular expression.  Only
830   ;; citations whose prefix matches the regular expression will be
831   ;; considered.
832   ;;
833   ;; WROTE is the attribution line number.
834   ;; PREFIX is the attribution line prefix.
835   ;; TAG is the Supercite tag on the attribution line.
836   (let ((atts gnus-cite-loose-attribution-alist)
837         (case-fold-search t)
838         att wrote in prefix tag regexp limit smallest best size)
839     (while atts
840       (setq att (car atts)
841             atts (cdr atts)
842             wrote (nth 0 att)
843             in (nth 1 att)
844             prefix (nth 2 att)
845             tag (nth 3 att)
846             regexp (if fun (funcall fun prefix tag) "")
847             size (cond ((eq sort 'small) t)
848                        ((eq sort 'first) nil)
849                        (t (< (length (gnus-cite-find-loose prefix)) 2)))
850             limit (if after wrote -1)
851             smallest 1000000
852             best nil)
853       (let ((cites gnus-cite-loose-prefix-alist)
854             cite candidate numbers first compare)
855         (while cites
856           (setq cite (car cites)
857                 cites (cdr cites)
858                 candidate (car cite)
859                 numbers (cdr cite)
860                 first (apply 'min numbers)
861                 compare (if size (length candidate) first))
862           (and (> first limit)
863                regexp
864                (string-match regexp candidate)
865                (< compare smallest)
866                (setq best cite
867                      smallest compare))))
868       (if (null best)
869           ()
870         (setq gnus-cite-loose-attribution-alist
871               (delq att gnus-cite-loose-attribution-alist))
872         (push (cons wrote (car best)) gnus-cite-attribution-alist)
873         (when in
874           (push (cons in (car best)) gnus-cite-attribution-alist))
875         (when (memq best gnus-cite-loose-prefix-alist)
876           (let ((loop gnus-cite-prefix-alist)
877                 (numbers (cdr best))
878                 current)
879             (setq gnus-cite-loose-prefix-alist
880                   (delq best gnus-cite-loose-prefix-alist))
881             (while loop
882               (setq current (car loop)
883                     loop (cdr loop))
884               (if (eq current best)
885                   ()
886                 (setcdr current (gnus-set-difference (cdr current) numbers))
887                 (when (null (cdr current))
888                   (setq gnus-cite-loose-prefix-alist
889                         (delq current gnus-cite-loose-prefix-alist)
890                         atts (delq current atts)))))))))))
891
892 (defun gnus-cite-find-loose (prefix)
893   ;; Return a list of loose attribution lines prefixed by PREFIX.
894   (let* ((atts gnus-cite-loose-attribution-alist)
895          att line lines)
896     (while atts
897       (setq att (car atts)
898             line (car att)
899             atts (cdr atts))
900       (when (string-equal (gnus-cite-find-prefix line) prefix)
901         (push line lines)))
902     lines))
903
904 (defun gnus-cite-add-face (number prefix face)
905   ;; At line NUMBER, ignore PREFIX and add FACE to the rest of the line.
906   (when face
907     (let ((inhibit-point-motion-hooks t)
908           from to overlay)
909       (goto-char (point-min))
910       (when (zerop (forward-line (1- number)))
911         (forward-char (length prefix))
912         (skip-chars-forward " \t")
913         (setq from (point))
914         (end-of-line 1)
915         (skip-chars-backward " \t")
916         (setq to (point))
917         (when (< from to)
918           (push (setq overlay (gnus-make-overlay from to))
919                 gnus-cite-overlay-list)
920           (gnus-overlay-put overlay 'face face))))))
921
922 (defun gnus-cite-toggle (prefix)
923   (save-excursion
924     (set-buffer gnus-article-buffer)
925     (gnus-cite-parse-maybe nil t)
926     (let ((buffer-read-only nil)
927           (numbers (cdr (assoc prefix gnus-cite-prefix-alist)))
928           (inhibit-point-motion-hooks t)
929           number)
930       (while numbers
931         (setq number (car numbers)
932               numbers (cdr numbers))
933         (goto-char (point-min))
934         (forward-line (1- number))
935         (cond ((get-text-property (point) 'invisible)
936                (remove-text-properties (point) (progn (forward-line 1) (point))
937                                        gnus-hidden-properties))
938               ((assq number gnus-cite-attribution-alist))
939               (t
940                (gnus-add-text-properties
941                 (point) (progn (forward-line 1) (point))
942                 (nconc (list 'article-type 'cite)
943                        gnus-hidden-properties))))))))
944
945 (defun gnus-cite-find-prefix (line)
946   ;; Return citation prefix for LINE.
947   (let ((alist gnus-cite-prefix-alist)
948         (prefix "")
949         entry)
950     (while alist
951       (setq entry (car alist)
952             alist (cdr alist))
953       (when (memq line (cdr entry))
954         (setq prefix (car entry))))
955     prefix))
956
957 (defun gnus-cite-localize ()
958   "Make the citation variables local to the article buffer."
959   (let ((vars '(gnus-cite-article
960                 gnus-cite-overlay-list gnus-cite-prefix-alist
961                 gnus-cite-attribution-alist gnus-cite-loose-prefix-alist
962                 gnus-cite-loose-attribution-alist)))
963     (while vars
964       (make-local-variable (pop vars)))))
965
966 (gnus-ems-redefine)
967
968 (provide 'gnus-cite)
969
970 ;; Local Variables:
971 ;; coding: iso-8859-1
972 ;; End:
973
974 ;;; gnus-cite.el ends here