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