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