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