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