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