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