Sync up gnus-5.6.33
[elisp/gnus.git-] / lisp / gnus-cite.el
1 ;;; gnus-cite.el --- parse citations in articles for Gnus
2 ;; Copyright (C) 1995,96,97,98 Free Software Foundation, Inc.
3
4 ;; Author: Per Abhiddenware; you can redistribute it and/or modify
5 ;; it under the terms of the GNU General Public License as published by
6 ;; the Free Software Foundation; either version 2, or (at your option)
7 ;; any later version.
8
9 ;; GNU Emacs is distributed in the hope that it will be useful,
10 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 ;; GNU General Public License for more details.
13
14 ;; You should have received a copy of the GNU General Public License
15 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
16 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 ;; Boston, MA 02111-1307, USA.
18
19 ;;; Commentary:
20
21 ;;; Code:
22
23 (eval-when-compile (require 'cl))
24
25 (require 'gnus)
26 (require 'gnus-art)
27 (require 'gnus-range)
28
29 ;;; Customization:
30
31 (defgroup gnus-cite nil
32   "Citation."
33   :prefix "gnus-cite-"
34   :link '(custom-manual "(gnus)Article Highlighting")
35   :group 'gnus-article)
36
37 (defcustom gnus-cite-reply-regexp
38   "^\\(Subject: Re\\|In-Reply-To\\|References\\):"
39   "*If headers match this regexp it is reasonable to believe that
40 article has citations."
41   :group 'gnus-cite
42   :type 'string)
43
44 (defcustom gnus-cite-always-check nil
45   "Check article always for citations. Set it t to check all articles."
46   :group 'gnus-cite
47   :type '(choice (const :tag "no" nil)
48                   (const :tag "yes" t)))
49
50 (defcustom gnus-cited-opened-text-button-line-format "%(%{[-]%}%)\n"
51   "Format of opened cited text buttons."
52   :group 'gnus-cite
53   :type 'string)
54
55 (defcustom gnus-cited-closed-text-button-line-format "%(%{[+]%}%)\n"
56   "Format of closed cited text buttons."
57   :group 'gnus-cite
58   :type 'string)
59
60 (defcustom gnus-cited-lines-visible nil
61   "The number of lines of hidden cited text to remain visible."
62   :group 'gnus-cite
63   :type '(choice (const :tag "none" nil)
64                  integer))
65
66 (defcustom gnus-cite-parse-max-size 25000
67   "Maximum article size (in bytes) where parsing citations is allowed.
68 Set it to nil to parse all articles."
69   :group 'gnus-cite
70   :type '(choice (const :tag "all" nil)
71                  integer))
72
73 (defcustom gnus-cite-prefix-regexp
74   "^[]>|:}+ ]*[]>|:}+]\\(.*>\\)?\\|^.*>"
75   "*Regexp matching the longest possible citation prefix on a line."
76   :group 'gnus-cite
77   :type 'regexp)
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 "^\\(" gnus-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\\),"
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\\|>\\)\\(:\\|\\.\\.\\.\\)\\)[ \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     (?l (- end beg) ?d)))
288 (defvar gnus-cited-opened-text-button-line-format-spec nil)
289 (defvar gnus-cited-closed-text-button-line-format-alist
290   gnus-cited-opened-text-button-line-format-alist)
291 (defvar gnus-cited-closed-text-button-line-format-spec nil)
292
293
294 ;;; Commands:
295
296 (defun gnus-article-highlight-citation (&optional force)
297   "Highlight cited text.
298 Each citation in the article will be highlighted with a different face.
299 The faces are taken from `gnus-cite-face-list'.
300 Attribution lines are highlighted with the same face as the
301 corresponding citation merged with `gnus-cite-attribution-face'.
302
303 Text is considered cited if at least `gnus-cite-minimum-match-count'
304 lines matches `gnus-cite-prefix-regexp' with the same prefix.
305
306 Lines matching `gnus-cite-attribution-suffix' and perhaps
307 `gnus-cite-attribution-prefix' are considered attribution lines."
308   (interactive (list 'force))
309   (save-excursion
310     (set-buffer gnus-article-buffer)
311     (gnus-cite-parse-maybe force)
312     (let ((buffer-read-only nil)
313           (alist gnus-cite-prefix-alist)
314           (faces gnus-cite-face-list)
315           (inhibit-point-motion-hooks t)
316           face entry prefix skip numbers number face-alist)
317       ;; Loop through citation prefixes.
318       (while alist
319         (setq entry (car alist)
320               alist (cdr alist)
321               prefix (car entry)
322               numbers (cdr entry)
323               face (car faces)
324               faces (or (cdr faces) gnus-cite-face-list)
325               face-alist (cons (cons prefix face) face-alist))
326         (while numbers
327           (setq number (car numbers)
328                 numbers (cdr numbers))
329           (and (not (assq number gnus-cite-attribution-alist))
330                (not (assq number gnus-cite-loose-attribution-alist))
331                (gnus-cite-add-face number prefix face))))
332       ;; Loop through attribution lines.
333       (setq alist gnus-cite-attribution-alist)
334       (while alist
335         (setq entry (car alist)
336               alist (cdr alist)
337               number (car entry)
338               prefix (cdr entry)
339               skip (gnus-cite-find-prefix number)
340               face (cdr (assoc prefix face-alist)))
341         ;; Add attribution button.
342         (goto-line number)
343         (when (re-search-forward gnus-cite-attribution-suffix
344                                  (save-excursion (end-of-line 1) (point))
345                                  t)
346           (gnus-article-add-button (match-beginning 1) (match-end 1)
347                                    'gnus-cite-toggle prefix))
348         ;; Highlight attribution line.
349         (gnus-cite-add-face number skip face)
350         (gnus-cite-add-face number skip gnus-cite-attribution-face))
351       ;; Loop through attribution lines.
352       (setq alist gnus-cite-loose-attribution-alist)
353       (while alist
354         (setq entry (car alist)
355               alist (cdr alist)
356               number (car entry)
357               skip (gnus-cite-find-prefix number))
358         (gnus-cite-add-face number skip gnus-cite-attribution-face)))))
359
360 (defun gnus-dissect-cited-text ()
361   "Dissect the article buffer looking for cited text."
362   (save-excursion
363     (set-buffer gnus-article-buffer)
364     (gnus-cite-parse-maybe)
365     (let ((alist gnus-cite-prefix-alist)
366           prefix numbers number marks m)
367       ;; Loop through citation prefixes.
368       (while alist
369         (setq numbers (pop alist)
370               prefix (pop numbers))
371         (while numbers
372           (setq number (pop numbers))
373           (goto-char (point-min))
374           (forward-line number)
375           (push (cons (point-marker) "") marks)
376           (while (and numbers
377                       (= (1- number) (car numbers)))
378             (setq number (pop numbers)))
379           (goto-char (point-min))
380           (forward-line (1- number))
381           (push (cons (point-marker) prefix) marks)))
382       ;; Skip to the beginning of the body.
383       (goto-char (point-min))
384       (search-forward "\n\n" nil t)
385       (push (cons (point-marker) "") marks)
386       ;; Find the end of the body.
387       (goto-char (point-max))
388       (gnus-article-search-signature)
389       (push (cons (point-marker) "") marks)
390       ;; Sort the marks.
391       (setq marks (sort marks 'car-less-than-car))
392       (let ((omarks marks))
393         (setq marks nil)
394         (while (cdr omarks)
395           (if (= (caar omarks) (caadr omarks))
396               (progn
397                 (unless (equal (cdar omarks) "")
398                   (push (car omarks) marks))
399                 (unless (equal (cdadr omarks) "")
400                   (push (cadr omarks) marks))
401                 (unless (and (equal (cdar omarks) "")
402                              (equal (cdadr omarks) "")
403                              (not (cddr omarks)))
404                   (setq omarks (cdr omarks))))
405             (push (car omarks) marks))
406           (setq omarks (cdr omarks)))
407         (when (car omarks)
408           (push (car omarks) marks))
409         (setq marks (setq m (nreverse marks)))
410         (while (cddr m)
411           (if (and (equal (cdadr m) "")
412                    (equal (cdar m) (cdaddr m))
413                    (goto-char (caadr m))
414                    (forward-line 1)
415                    (= (point) (caaddr m)))
416               (setcdr m (cdddr m))
417             (setq m (cdr m))))
418         marks))))
419
420 (defun gnus-article-fill-cited-article (&optional force width)
421   "Do word wrapping in the current article.
422 If WIDTH (the numerical prefix), use that text width when filling."
423   (interactive (list t current-prefix-arg))
424   (save-excursion
425     (set-buffer gnus-article-buffer)
426     (let ((buffer-read-only nil)
427           (inhibit-point-motion-hooks t)
428           (marks (gnus-dissect-cited-text))
429           (adaptive-fill-mode nil)
430           (filladapt-mode nil)
431           (fill-column (if width (prefix-numeric-value width) fill-column)))
432       (save-restriction
433         (while (cdr marks)
434           (widen)
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 gnus-cited-lines-visible)
489             (if (>= (point) end)
490                 (setq beg nil)
491               (setq beg (point-marker))))
492           (when (and beg end)
493             ;; We use markers for the end-points to facilitate later
494             ;; wrapping and mangling of text.
495             (setq beg (set-marker (make-marker) beg)
496                   end (set-marker (make-marker) end))
497             (gnus-add-text-properties beg end props)
498             (goto-char beg)
499             (unless (save-excursion (search-backward "\n\n" nil t))
500               (insert "\n"))
501             (put-text-property
502              (setq start (point-marker))
503              (progn
504                (gnus-article-add-button
505                 (point)
506                 (progn (eval gnus-cited-closed-text-button-line-format-spec)
507                        (point))
508                 `gnus-article-toggle-cited-text
509                 (list (cons beg end) start))
510                (point))
511              'article-type 'annotation)
512             (set-marker beg (point)))))))))
513
514 (defun gnus-article-toggle-cited-text (args)
515   "Toggle hiding the text in REGION."
516   (let* ((region (car args))
517          (start (cadr args))
518          (hidden
519           (text-property-any
520            (car region) (1- (cdr region))
521            (car gnus-hidden-properties) (cadr gnus-hidden-properties)))
522          (inhibit-point-motion-hooks t)
523          buffer-read-only)
524     (funcall
525      (if hidden
526          'remove-text-properties 'gnus-add-text-properties)
527      (car region) (cdr region) gnus-hidden-properties)
528     (save-excursion
529       (goto-char start)
530       (gnus-delete-line)
531       (put-text-property
532        (point)
533        (progn
534          (gnus-article-add-button
535           (point)
536           (progn (eval
537                   (if hidden
538                       gnus-cited-opened-text-button-line-format-spec
539                     gnus-cited-closed-text-button-line-format-spec))
540                  (point))
541           `gnus-article-toggle-cited-text
542           args)
543          (point))
544        'article-type 'annotation))))
545
546 (defun gnus-article-hide-citation-maybe (&optional arg force)
547   "Toggle hiding of cited text that has an attribution line.
548 If given a negative prefix, always show; if given a positive prefix,
549 always hide.
550 This will do nothing unless at least `gnus-cite-hide-percentage'
551 percent and at least `gnus-cite-hide-absolute' lines of the body is
552 cited text with attributions.  When called interactively, these two
553 variables are ignored.
554 See also the documentation for `gnus-article-highlight-citation'."
555   (interactive (append (gnus-article-hidden-arg) '(force)))
556   (unless (gnus-article-check-hidden-text 'cite arg)
557     (save-excursion
558       (set-buffer gnus-article-buffer)
559       (gnus-cite-parse-maybe force)
560       (goto-char (point-min))
561       (search-forward "\n\n" nil t)
562       (let ((start (point))
563             (atts gnus-cite-attribution-alist)
564             (buffer-read-only nil)
565             (inhibit-point-motion-hooks t)
566             (hidden 0)
567             total)
568         (goto-char (point-max))
569         (gnus-article-search-signature)
570         (setq total (count-lines start (point)))
571         (while atts
572           (setq hidden (+ hidden (length (cdr (assoc (cdar atts)
573                                                      gnus-cite-prefix-alist))))
574                 atts (cdr atts)))
575         (when (or force
576                   (and (> (* 100 hidden) (* gnus-cite-hide-percentage total))
577                        (> hidden gnus-cite-hide-absolute)))
578           (setq atts gnus-cite-attribution-alist)
579           (while atts
580             (setq total (cdr (assoc (cdar atts) gnus-cite-prefix-alist))
581                   atts (cdr atts))
582             (while total
583               (setq hidden (car total)
584                     total (cdr total))
585               (goto-line hidden)
586               (unless (assq hidden gnus-cite-attribution-alist)
587                 (gnus-add-text-properties
588                  (point) (progn (forward-line 1) (point))
589                  (nconc (list 'article-type 'cite)
590                         gnus-hidden-properties))))))))))
591
592 (defun gnus-article-hide-citation-in-followups ()
593   "Hide cited text in non-root articles."
594   (interactive)
595   (save-excursion
596     (set-buffer gnus-article-buffer)
597     (let ((article (cdr gnus-article-current)))
598       (unless (save-excursion
599                 (set-buffer gnus-summary-buffer)
600                 (gnus-article-displayed-root-p article))
601         (gnus-article-hide-citation)))))
602
603 ;;; Internal functions:
604
605 (defun gnus-cite-parse-maybe (&optional force)
606   ;; Parse if the buffer has changes since last time.
607   (if (and (not force)
608            (equal gnus-cite-article gnus-article-current))
609       ()
610     (gnus-cite-localize)
611     ;;Reset parser information.
612     (setq gnus-cite-prefix-alist nil
613           gnus-cite-attribution-alist nil
614           gnus-cite-loose-prefix-alist nil
615           gnus-cite-loose-attribution-alist nil)
616     (while gnus-cite-overlay-list
617       (gnus-delete-overlay (pop gnus-cite-overlay-list)))
618     ;; Parse if not too large.
619     (if (and (not force)
620              gnus-cite-parse-max-size
621              (> (buffer-size) gnus-cite-parse-max-size))
622         ()
623       (setq gnus-cite-article (cons (car gnus-article-current)
624                                     (cdr gnus-article-current)))
625       (gnus-cite-parse-wrapper))))
626
627 (defun gnus-cite-parse-wrapper ()
628   ;; Wrap chopped gnus-cite-parse
629   (goto-char (point-min))
630   (unless (search-forward "\n\n" nil t)
631     (goto-char (point-max)))
632   (save-excursion
633     (gnus-cite-parse-attributions))
634   ;; Try to avoid check citation if there is no reason to believe
635   ;; that article has citations
636   (if (or gnus-cite-always-check
637           (save-excursion
638             (re-search-backward gnus-cite-reply-regexp nil t))
639           gnus-cite-loose-attribution-alist)
640       (progn (save-excursion
641                (gnus-cite-parse))
642              (save-excursion
643                (gnus-cite-connect-attributions)))))
644
645 (defun gnus-cite-parse ()
646   ;; Parse and connect citation prefixes and attribution lines.
647
648   ;; Parse current buffer searching for citation prefixes.
649   (let ((line (1+ (count-lines (point-min) (point))))
650         (case-fold-search t)
651         (max (save-excursion
652                (goto-char (point-max))
653                (gnus-article-search-signature)
654                (point)))
655         alist entry start begin end numbers prefix)
656     ;; Get all potential prefixes in `alist'.
657     (while (< (point) max)
658       ;; Each line.
659       (setq begin (point)
660             end (progn (beginning-of-line 2) (point))
661             start end)
662       (goto-char begin)
663       ;; Ignore standard Supercite attribution prefix.
664       (when (looking-at gnus-supercite-regexp)
665         (if (match-end 1)
666             (setq end (1+ (match-end 1)))
667           (setq end (1+ begin))))
668       ;; Ignore very long prefixes.
669       (when (> end (+ (point) gnus-cite-max-prefix))
670         (setq end (+ (point) gnus-cite-max-prefix)))
671       (while (re-search-forward gnus-cite-prefix-regexp (1- end) t)
672         ;; Each prefix.
673         (setq end (match-end 0)
674               prefix (buffer-substring begin end))
675         (gnus-set-text-properties 0 (length prefix) nil prefix)
676         (setq entry (assoc prefix alist))
677         (if entry
678             (setcdr entry (cons line (cdr entry)))
679           (push (list prefix line) alist))
680         (goto-char begin))
681       (goto-char start)
682       (setq line (1+ line)))
683     ;; We got all the potential prefixes.  Now create
684     ;; `gnus-cite-prefix-alist' containing the oldest prefix for each
685     ;; line that appears at least gnus-cite-minimum-match-count
686     ;; times.  First sort them by length.  Longer is older.
687     (setq alist (sort alist (lambda (a b)
688                               (> (length (car a)) (length (car b))))))
689     (while alist
690       (setq entry (car alist)
691             prefix (car entry)
692             numbers (cdr entry)
693             alist (cdr alist))
694       (cond ((null numbers)
695              ;; No lines with this prefix that wasn't also part of
696              ;; a longer prefix.
697              )
698             ((< (length numbers) gnus-cite-minimum-match-count)
699              ;; Too few lines with this prefix.  We keep it a bit
700              ;; longer in case it is an exact match for an attribution
701              ;; line, but we don't remove the line from other
702              ;; prefixes.
703              (push entry gnus-cite-prefix-alist))
704             (t
705              (push entry
706                    gnus-cite-prefix-alist)
707              ;; Remove articles from other prefixes.
708              (let ((loop alist)
709                    current)
710                (while loop
711                  (setq current (car loop)
712                        loop (cdr loop))
713                  (setcdr current
714                          (gnus-set-difference (cdr current) numbers)))))))))
715
716 (defun gnus-cite-parse-attributions ()
717   (let (al-alist)
718     ;; Parse attributions
719     (while (re-search-forward gnus-cite-attribution-suffix (point-max) t)
720       (let* ((start (match-beginning 0))
721              (end (match-end 0))
722              (wrote (count-lines (point-min) end))
723              (prefix (gnus-cite-find-prefix wrote))
724              ;; Check previous line for an attribution leader.
725              (tag (progn
726                     (beginning-of-line 1)
727                     (when (looking-at gnus-supercite-secondary-regexp)
728                       (buffer-substring (match-beginning 1)
729                                         (match-end 1)))))
730              (in (progn
731                    (goto-char start)
732                    (and (re-search-backward gnus-cite-attribution-prefix
733                                             (save-excursion
734                                               (beginning-of-line 0)
735                                               (point))
736                                             t)
737                         (not (re-search-forward gnus-cite-attribution-suffix
738                                                 start t))
739                         (count-lines (point-min) (1+ (point)))))))
740         (when (eq wrote in)
741           (setq in nil))
742         (goto-char end)
743         ;; don't add duplicates
744         (let ((al (buffer-substring (save-excursion (beginning-of-line 0)
745                                                     (1+ (point)))
746                                     end)))
747           (if (not (assoc al al-alist))
748               (progn
749                 (push (list wrote in prefix tag)
750                       gnus-cite-loose-attribution-alist)
751                 (push (cons al t) al-alist))))))))
752
753 (defun gnus-cite-connect-attributions ()
754   ;; Connect attributions to citations
755
756   ;; No citations have been connected to attribution lines yet.
757   (setq gnus-cite-loose-prefix-alist (append gnus-cite-prefix-alist nil))
758
759   ;; Parse current buffer searching for attribution lines.
760   ;; Find exact supercite citations.
761   (gnus-cite-match-attributions 'small nil
762                                 (lambda (prefix tag)
763                                   (when tag
764                                     (concat "\\`"
765                                             (regexp-quote prefix) "[ \t]*"
766                                             (regexp-quote tag) ">"))))
767   ;; Find loose supercite citations after attributions.
768   (gnus-cite-match-attributions 'small t
769                                 (lambda (prefix tag)
770                                   (when tag
771                                     (concat "\\<"
772                                             (regexp-quote tag)
773                                             "\\>"))))
774   ;; Find loose supercite citations anywhere.
775   (gnus-cite-match-attributions 'small nil
776                                 (lambda (prefix tag)
777                                   (when tag
778                                     (concat "\\<"
779                                             (regexp-quote tag)
780                                             "\\>"))))
781   ;; Find nested citations after attributions.
782   (gnus-cite-match-attributions 'small-if-unique t
783                                 (lambda (prefix tag)
784                                   (concat "\\`" (regexp-quote prefix) ".+")))
785   ;; Find nested citations anywhere.
786   (gnus-cite-match-attributions 'small nil
787                                 (lambda (prefix tag)
788                                   (concat "\\`" (regexp-quote prefix) ".+")))
789   ;; Remove loose prefixes with too few lines.
790   (let ((alist gnus-cite-loose-prefix-alist)
791         entry)
792     (while alist
793       (setq entry (car alist)
794             alist (cdr alist))
795       (when (< (length (cdr entry)) gnus-cite-minimum-match-count)
796         (setq gnus-cite-prefix-alist
797               (delq entry gnus-cite-prefix-alist)
798               gnus-cite-loose-prefix-alist
799               (delq entry gnus-cite-loose-prefix-alist)))))
800   ;; Find flat attributions.
801   (gnus-cite-match-attributions 'first t nil)
802   ;; Find any attributions (are we getting desperate yet?).
803   (gnus-cite-match-attributions 'first nil nil))
804
805 (defun gnus-cite-match-attributions (sort after fun)
806   ;; Match all loose attributions and citations (SORT AFTER FUN) .
807   ;;
808   ;; If SORT is `small', the citation with the shortest prefix will be
809   ;; used, if it is `first' the first prefix will be used, if it is
810   ;; `small-if-unique' the shortest prefix will be used if the
811   ;; attribution line does not share its own prefix with other
812   ;; loose attribution lines, otherwise the first prefix will be used.
813   ;;
814   ;; If AFTER is non-nil, only citations after the attribution line
815   ;; will be considered.
816   ;;
817   ;; If FUN is non-nil, it will be called with the arguments (WROTE
818   ;; PREFIX TAG) and expected to return a regular expression.  Only
819   ;; citations whose prefix matches the regular expression will be
820   ;; considered.
821   ;;
822   ;; WROTE is the attribution line number.
823   ;; PREFIX is the attribution line prefix.
824   ;; TAG is the Supercite tag on the attribution line.
825   (let ((atts gnus-cite-loose-attribution-alist)
826         (case-fold-search t)
827         att wrote in prefix tag regexp limit smallest best size)
828     (while atts
829       (setq att (car atts)
830             atts (cdr atts)
831             wrote (nth 0 att)
832             in (nth 1 att)
833             prefix (nth 2 att)
834             tag (nth 3 att)
835             regexp (if fun (funcall fun prefix tag) "")
836             size (cond ((eq sort 'small) t)
837                        ((eq sort 'first) nil)
838                        (t (< (length (gnus-cite-find-loose prefix)) 2)))
839             limit (if after wrote -1)
840             smallest 1000000
841             best nil)
842       (let ((cites gnus-cite-loose-prefix-alist)
843             cite candidate numbers first compare)
844         (while cites
845           (setq cite (car cites)
846                 cites (cdr cites)
847                 candidate (car cite)
848                 numbers (cdr cite)
849                 first (apply 'min numbers)
850                 compare (if size (length candidate) first))
851           (and (> first limit)
852                regexp
853                (string-match regexp candidate)
854                (< compare smallest)
855                (setq best cite
856                      smallest compare))))
857       (if (null best)
858           ()
859         (setq gnus-cite-loose-attribution-alist
860               (delq att gnus-cite-loose-attribution-alist))
861         (push (cons wrote (car best)) gnus-cite-attribution-alist)
862         (when in
863           (push (cons in (car best)) gnus-cite-attribution-alist))
864         (when (memq best gnus-cite-loose-prefix-alist)
865           (let ((loop gnus-cite-prefix-alist)
866                 (numbers (cdr best))
867                 current)
868             (setq gnus-cite-loose-prefix-alist
869                   (delq best gnus-cite-loose-prefix-alist))
870             (while loop
871               (setq current (car loop)
872                     loop (cdr loop))
873               (if (eq current best)
874                   ()
875                 (setcdr current (gnus-set-difference (cdr current) numbers))
876                 (when (null (cdr current))
877                   (setq gnus-cite-loose-prefix-alist
878                         (delq current gnus-cite-loose-prefix-alist)
879                         atts (delq current atts)))))))))))
880
881 (defun gnus-cite-find-loose (prefix)
882   ;; Return a list of loose attribution lines prefixed by PREFIX.
883   (let* ((atts gnus-cite-loose-attribution-alist)
884          att line lines)
885     (while atts
886       (setq att (car atts)
887             line (car att)
888             atts (cdr atts))
889       (when (string-equal (gnus-cite-find-prefix line) prefix)
890         (push line lines)))
891     lines))
892
893 (defun gnus-cite-add-face (number prefix face)
894   ;; At line NUMBER, ignore PREFIX and add FACE to the rest of the line.
895   (when face
896     (let ((inhibit-point-motion-hooks t)
897           from to overlay)
898       (goto-line number)
899       (unless (eobp)                    ; Sometimes things become confused.
900         (forward-char (length prefix))
901         (skip-chars-forward " \t")
902         (setq from (point))
903         (end-of-line 1)
904         (skip-chars-backward " \t")
905         (setq to (point))
906         (when (< from to)
907           (push (setq overlay (gnus-make-overlay from to))
908                 gnus-cite-overlay-list)
909           (gnus-overlay-put overlay 'face face))))))
910
911 (defun gnus-cite-toggle (prefix)
912   (save-excursion
913     (set-buffer gnus-article-buffer)
914     (gnus-cite-parse-maybe)
915     (let ((buffer-read-only nil)
916           (numbers (cdr (assoc prefix gnus-cite-prefix-alist)))
917           (inhibit-point-motion-hooks t)
918           number)
919       (while numbers
920         (setq number (car numbers)
921               numbers (cdr numbers))
922         (goto-line number)
923         (cond ((get-text-property (point) 'invisible)
924                (remove-text-properties (point) (progn (forward-line 1) (point))
925                                        gnus-hidden-properties))
926               ((assq number gnus-cite-attribution-alist))
927               (t
928                (gnus-add-text-properties
929                 (point) (progn (forward-line 1) (point))
930                 (nconc (list 'article-type 'cite)
931                        gnus-hidden-properties))))))))
932
933 (defun gnus-cite-find-prefix (line)
934   ;; Return citation prefix for LINE.
935   (let ((alist gnus-cite-prefix-alist)
936         (prefix "")
937         entry)
938     (while alist
939       (setq entry (car alist)
940             alist (cdr alist))
941       (when (memq line (cdr entry))
942         (setq prefix (car entry))))
943     prefix))
944
945 (defun gnus-cite-localize ()
946   "Make the citation variables local to the article buffer."
947   (let ((vars '(gnus-cite-article
948                 gnus-cite-overlay-list gnus-cite-prefix-alist
949                 gnus-cite-attribution-alist gnus-cite-loose-prefix-alist
950                 gnus-cite-loose-attribution-alist)))
951     (while vars
952       (make-local-variable (pop vars)))))
953
954 (gnus-ems-redefine)
955
956 (provide 'gnus-cite)
957
958 ;;; gnus-cite.el ends here