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