927abba1b41d0d65e371c6308fab7d850fecb484
[elisp/gnus.git-] / 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 (eval-when-compile (require 'static))
31
32 (require 'gnus)
33 (require 'gnus-art)
34 (require 'gnus-range)
35 (require 'message)      ; for message-cite-prefix-regexp
36
37 ;;; Customization:
38
39 (defgroup gnus-cite nil
40   "Citation."
41   :prefix "gnus-cite-"
42   :link '(custom-manual "(gnus)Article Highlighting")
43   :group 'gnus-article)
44
45 (defcustom gnus-cite-reply-regexp
46   "^\\(Subject: Re\\|In-Reply-To\\|References\\):"
47   "*If headers match this regexp it is reasonable to believe that
48 article has citations."
49   :group 'gnus-cite
50   :type 'string)
51
52 (defcustom gnus-cite-always-check nil
53   "Check article always for citations.  Set it t to check all articles."
54   :group 'gnus-cite
55   :type '(choice (const :tag "no" nil)
56                  (const :tag "yes" t)))
57
58 (defcustom gnus-cited-opened-text-button-line-format "%(%{[-]%}%)\n"
59   "Format of opened cited text buttons."
60   :group 'gnus-cite
61   :type 'string)
62
63 (defcustom gnus-cited-closed-text-button-line-format "%(%{[+]%}%)\n"
64   "Format of closed cited text buttons."
65   :group 'gnus-cite
66   :type 'string)
67
68 (defcustom gnus-cited-lines-visible nil
69   "The number of lines of hidden cited text to remain visible.
70 Or a pair (cons) of numbers which are the number of lines at the top
71 and bottom of the text, respectively, to remain visible."
72   :group 'gnus-cite
73   :type '(choice (const :tag "none" nil)
74                  integer
75                  (cons :tag "Top and Bottom" integer integer)))
76
77 (defcustom gnus-cite-parse-max-size 25000
78   "Maximum article size (in bytes) where parsing citations is allowed.
79 Set it to nil to parse all articles."
80   :group 'gnus-cite
81   :type '(choice (const :tag "all" nil)
82                  integer))
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 "^\\(" message-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 (defcustom gnus-cite-blank-line-after-header t
264   "If non-nil, put a blank line between the citation header and the button."
265   :group 'gnus-cite
266   :type 'boolean)
267
268 ;;; Internal Variables:
269
270 (defvar gnus-cite-article nil)
271 (defvar gnus-cite-overlay-list nil)
272
273 (defvar gnus-cite-prefix-alist nil)
274 ;; Alist of citation prefixes.
275 ;; The cdr is a list of lines with that prefix.
276
277 (defvar gnus-cite-attribution-alist nil)
278 ;; Alist of attribution lines.
279 ;; The car is a line number.
280 ;; The cdr is the prefix for the citation started by that line.
281
282 (defvar gnus-cite-loose-prefix-alist nil)
283 ;; Alist of citation prefixes that have no matching attribution.
284 ;; The cdr is a list of lines with that prefix.
285
286 (defvar gnus-cite-loose-attribution-alist nil)
287 ;; Alist of attribution lines that have no matching citation.
288 ;; Each member has the form (WROTE IN PREFIX TAG), where
289 ;; WROTE: is the attribution line number
290 ;; IN: is the line number of the previous line if part of the same attribution,
291 ;; PREFIX: Is the citation prefix of the attribution line(s), and
292 ;; TAG: Is a Supercite tag, if any.
293
294 (defvar gnus-cited-opened-text-button-line-format-alist
295   `((?b (marker-position beg) ?d)
296     (?e (marker-position end) ?d)
297     (?n (count-lines beg end) ?d)
298     (?l (- end beg) ?d)))
299 (defvar gnus-cited-opened-text-button-line-format-spec nil)
300 (defvar gnus-cited-closed-text-button-line-format-alist
301   gnus-cited-opened-text-button-line-format-alist)
302 (defvar gnus-cited-closed-text-button-line-format-spec nil)
303
304
305 ;;; Commands:
306
307 (defun gnus-article-highlight-citation (&optional force)
308   "Highlight cited text.
309 Each citation in the article will be highlighted with a different face.
310 The faces are taken from `gnus-cite-face-list'.
311 Attribution lines are highlighted with the same face as the
312 corresponding citation merged with `gnus-cite-attribution-face'.
313
314 Text is considered cited if at least `gnus-cite-minimum-match-count'
315 lines matches `message-cite-prefix-regexp' with the same prefix.
316
317 Lines matching `gnus-cite-attribution-suffix' and perhaps
318 `gnus-cite-attribution-prefix' are considered attribution lines."
319   (interactive (list 'force))
320   (save-excursion
321     (set-buffer gnus-article-buffer)
322     (gnus-cite-parse-maybe force)
323     (let ((buffer-read-only nil)
324           (alist gnus-cite-prefix-alist)
325           (faces gnus-cite-face-list)
326           (inhibit-point-motion-hooks t)
327           face entry prefix skip numbers number face-alist)
328       ;; Loop through citation prefixes.
329       (while alist
330         (setq entry (car alist)
331               alist (cdr alist)
332               prefix (car entry)
333               numbers (cdr entry)
334               face (car faces)
335               faces (or (cdr faces) gnus-cite-face-list)
336               face-alist (cons (cons prefix face) face-alist))
337         (while numbers
338           (setq number (car numbers)
339                 numbers (cdr numbers))
340           (and (not (assq number gnus-cite-attribution-alist))
341                (not (assq number gnus-cite-loose-attribution-alist))
342                (gnus-cite-add-face number prefix face))))
343       ;; Loop through attribution lines.
344       (setq alist gnus-cite-attribution-alist)
345       (while alist
346         (setq entry (car alist)
347               alist (cdr alist)
348               number (car entry)
349               prefix (cdr entry)
350               skip (gnus-cite-find-prefix number)
351               face (cdr (assoc prefix face-alist)))
352         ;; Add attribution button.
353         (goto-char (point-min))
354         (forward-line (1- number))
355         (when (re-search-forward gnus-cite-attribution-suffix
356                                  (save-excursion (end-of-line 1) (point))
357                                  t)
358           (gnus-article-add-button (match-beginning 1) (match-end 1)
359                                    'gnus-cite-toggle prefix))
360         ;; Highlight attribution line.
361         (gnus-cite-add-face number skip face)
362         (gnus-cite-add-face number skip gnus-cite-attribution-face))
363       ;; Loop through attribution lines.
364       (setq alist gnus-cite-loose-attribution-alist)
365       (while alist
366         (setq entry (car alist)
367               alist (cdr alist)
368               number (car entry)
369               skip (gnus-cite-find-prefix number))
370         (gnus-cite-add-face number skip gnus-cite-attribution-face)))))
371
372 (defun gnus-dissect-cited-text ()
373   "Dissect the article buffer looking for cited text."
374   (save-excursion
375     (set-buffer gnus-article-buffer)
376     (gnus-cite-parse-maybe nil t)
377     (let ((alist gnus-cite-prefix-alist)
378           prefix numbers number marks m)
379       ;; Loop through citation prefixes.
380       (while alist
381         (setq numbers (pop alist)
382               prefix (pop numbers))
383         (while numbers
384           (setq number (pop numbers))
385           (goto-char (point-min))
386           (forward-line number)
387           (push (cons (point-marker) "") marks)
388           (while (and numbers
389                       (= (1- number) (car numbers)))
390             (setq number (pop numbers)))
391           (goto-char (point-min))
392           (forward-line (1- number))
393           (push (cons (point-marker) prefix) marks)))
394       ;; Skip to the beginning of the body.
395       (article-goto-body)
396       (push (cons (point-marker) "") marks)
397       ;; Find the end of the body.
398       (goto-char (point-max))
399       (gnus-article-search-signature)
400       (push (cons (point-marker) "") marks)
401       ;; Sort the marks.
402       (setq marks (sort marks 'car-less-than-car))
403       (let ((omarks marks))
404         (setq marks nil)
405         (while (cdr omarks)
406           (if (= (caar omarks) (caadr omarks))
407               (progn
408                 (unless (equal (cdar omarks) "")
409                   (push (car omarks) marks))
410                 (unless (equal (cdadr omarks) "")
411                   (push (cadr omarks) marks))
412                 (unless (and (equal (cdar omarks) "")
413                              (equal (cdadr omarks) "")
414                              (not (cddr omarks)))
415                   (setq omarks (cdr omarks))))
416             (push (car omarks) marks))
417           (setq omarks (cdr omarks)))
418         (when (car omarks)
419           (push (car omarks) marks))
420         (setq marks (setq m (nreverse marks)))
421         (while (cddr m)
422           (if (and (equal (cdadr m) "")
423                    (equal (cdar m) (cdaddr m))
424                    (goto-char (caadr m))
425                    (forward-line 1)
426                    (= (point) (caaddr m)))
427               (setcdr m (cdddr m))
428             (setq m (cdr m))))
429         marks))))
430
431 (defun gnus-article-fill-cited-article (&optional force width)
432   "Do word wrapping in the current article.
433 If WIDTH (the numerical prefix), use that text width when filling."
434   (interactive (list t current-prefix-arg))
435   (save-excursion
436     (set-buffer gnus-article-buffer)
437     (let ((buffer-read-only nil)
438           (inhibit-point-motion-hooks t)
439           (marks (gnus-dissect-cited-text))
440           (adaptive-fill-mode nil)
441           (filladapt-mode nil)
442           (fill-column (if width (prefix-numeric-value width) fill-column)))
443       (save-restriction
444         (while (cdr marks)
445           (narrow-to-region (caar marks) (caadr marks))
446           (let ((adaptive-fill-regexp
447                  (concat "^" (regexp-quote (cdar marks)) " *"))
448                 (fill-prefix
449                  (if (string= (cdar marks) "") ""
450                    (concat (cdar marks) " ")))
451                 use-hard-newlines)
452             (fill-region (point-min) (point-max)))
453           (set-marker (caar marks) nil)
454           (setq marks (cdr marks)))
455         (when marks
456           (set-marker (caar marks) nil))
457         ;; All this information is now incorrect.
458         (setq gnus-cite-prefix-alist nil
459               gnus-cite-attribution-alist nil
460               gnus-cite-loose-prefix-alist nil
461               gnus-cite-loose-attribution-alist nil
462               gnus-cite-article nil)))))
463
464 (defun gnus-article-hide-citation (&optional arg force)
465   "Toggle hiding of all cited text except attribution lines.
466 See the documentation for `gnus-article-highlight-citation'.
467 If given a negative prefix, always show; if given a positive prefix,
468 always hide."
469   (interactive (append (gnus-article-hidden-arg) (list 'force)))
470   (gnus-set-format 'cited-opened-text-button t)
471   (gnus-set-format 'cited-closed-text-button t)
472   (save-excursion
473     (set-buffer gnus-article-buffer)
474     (let ((buffer-read-only nil)
475           marks
476           (inhibit-point-motion-hooks t)
477           (props (nconc (list 'article-type 'cite)
478                         gnus-hidden-properties))
479           (point (point-min))
480           found beg end start)
481       (while (setq point
482                    (text-property-any point (point-max)
483                                       'gnus-callback
484                                       'gnus-article-toggle-cited-text))
485         (setq found t)
486         (goto-char point)
487         (gnus-article-toggle-cited-text
488          (get-text-property point 'gnus-data) arg)
489         (forward-line 1)
490         (setq point (point)))
491       (unless found
492         (setq marks (gnus-dissect-cited-text))
493         (while marks
494           (setq beg nil
495                 end nil)
496           (while (and marks (string= (cdar marks) ""))
497             (setq marks (cdr marks)))
498           (when marks
499             (setq beg (caar marks)))
500           (while (and marks (not (string= (cdar marks) "")))
501             (setq marks (cdr marks)))
502           (when marks
503             (setq end (caar marks)))
504           ;; Skip past lines we want to leave visible.
505           (when (and beg end gnus-cited-lines-visible)
506             (goto-char beg)
507             (forward-line (if (consp gnus-cited-lines-visible)
508                               (car gnus-cited-lines-visible)
509                             gnus-cited-lines-visible))
510             (if (>= (point) end)
511                 (setq beg nil)
512               (setq beg (point-marker))
513               (when (consp gnus-cited-lines-visible)
514                 (goto-char end)
515                 (forward-line (- (cdr gnus-cited-lines-visible)))
516                 (if (<= (point) beg)
517                     (setq beg nil)
518                   (setq end (point-marker))))))
519           (when (and beg end)
520             (gnus-add-wash-type 'cite)
521             ;; We use markers for the end-points to facilitate later
522             ;; wrapping and mangling of text.
523             (setq beg (set-marker (make-marker) beg)
524                   end (set-marker (make-marker) end))
525             (gnus-add-text-properties-when 'article-type nil beg end props)
526             (goto-char beg)
527             (when (and gnus-cite-blank-line-after-header
528                        (not (save-excursion (search-backward "\n\n" nil t))))
529               (insert "\n"))
530             (put-text-property
531              (setq start (point-marker))
532              (progn
533                (gnus-article-add-button
534                 (point)
535                 (progn (eval gnus-cited-closed-text-button-line-format-spec)
536                        (point))
537                 `gnus-article-toggle-cited-text
538                 (list (cons beg end) start))
539                (point))
540              'article-type 'annotation)
541             (set-marker beg (point))))))))
542
543 (defun gnus-article-toggle-cited-text (args &optional arg)
544   "Toggle hiding the text in REGION.
545 ARG can be nil or a number.  Positive means hide, negative
546 means show, nil means toggle."
547   (let* ((region (car args))
548          (beg (car region))
549          (end (cdr region))
550          (start (cadr args))
551          (hidden
552           (text-property-any beg (1- end) 'article-type 'cite))
553          (inhibit-point-motion-hooks t)
554          buffer-read-only)
555     (when (or (null arg)
556               (zerop arg)
557               (and (> arg 0) (not hidden))
558               (and (< arg 0) hidden))
559       (if hidden
560           (progn
561             ;; Can't remove 'cite from g-a-wash-types here because
562             ;; multiple citations may be hidden -jas
563             (gnus-remove-text-properties-when
564              'article-type 'cite beg end
565              (cons 'article-type (cons 'cite
566                                        gnus-hidden-properties))))
567         (gnus-add-wash-type 'cite)
568         (gnus-add-text-properties-when
569          'article-type nil beg end
570          (cons 'article-type (cons 'cite
571                                    gnus-hidden-properties))))
572       (let ((gnus-article-mime-handle-alist-1 gnus-article-mime-handle-alist))
573         (gnus-set-mode-line 'article))
574       (save-excursion
575         (goto-char start)
576         (gnus-delete-line)
577         (put-text-property
578          (point)
579          (progn
580            (gnus-article-add-button
581             (point)
582             (progn (eval
583                     (if hidden
584                         gnus-cited-opened-text-button-line-format-spec
585                       gnus-cited-closed-text-button-line-format-spec))
586                    (point))
587             `gnus-article-toggle-cited-text
588             args)
589            (point))
590          'article-type 'annotation)))))
591
592 (defun gnus-article-hide-citation-maybe (&optional arg force)
593   "Toggle hiding of cited text that has an attribution line.
594 If given a negative prefix, always show; if given a positive prefix,
595 always hide.
596 This will do nothing unless at least `gnus-cite-hide-percentage'
597 percent and at least `gnus-cite-hide-absolute' lines of the body is
598 cited text with attributions.  When called interactively, these two
599 variables are ignored.
600 See also the documentation for `gnus-article-highlight-citation'."
601   (interactive (append (gnus-article-hidden-arg) '(force)))
602   (unless (gnus-article-check-hidden-text 'cite arg)
603     (save-excursion
604       (set-buffer gnus-article-buffer)
605       (gnus-cite-parse-maybe force)
606       (article-goto-body)
607       (let ((start (point))
608             (atts gnus-cite-attribution-alist)
609             (buffer-read-only nil)
610             (inhibit-point-motion-hooks t)
611             (hidden 0)
612             total)
613         (goto-char (point-max))
614         (gnus-article-search-signature)
615         (setq total (count-lines start (point)))
616         (while atts
617           (setq hidden (+ hidden (length (cdr (assoc (cdar atts)
618                                                      gnus-cite-prefix-alist))))
619                 atts (cdr atts)))
620         (when (or force
621                   (and (> (* 100 hidden) (* gnus-cite-hide-percentage total))
622                        (> hidden gnus-cite-hide-absolute)))
623           (setq atts gnus-cite-attribution-alist)
624           (while atts
625             (setq total (cdr (assoc (cdar atts) gnus-cite-prefix-alist))
626                   atts (cdr atts))
627             (while total
628               (setq hidden (car total)
629                     total (cdr total))
630               (goto-char (point-min))
631               (forward-line (1- hidden))
632               (unless (assq hidden gnus-cite-attribution-alist)
633                 (gnus-add-text-properties
634                  (point) (progn (forward-line 1) (point))
635                  (nconc (list 'article-type 'cite)
636                         gnus-hidden-properties))))))))))
637
638 (defun gnus-article-hide-citation-in-followups ()
639   "Hide cited text in non-root articles."
640   (interactive)
641   (save-excursion
642     (set-buffer gnus-article-buffer)
643     (let ((article (cdr gnus-article-current)))
644       (unless (save-excursion
645                 (set-buffer gnus-summary-buffer)
646                 (gnus-article-displayed-root-p article))
647         (gnus-article-hide-citation)))))
648
649 ;;; Internal functions:
650
651 (defun gnus-cite-parse-maybe (&optional force no-overlay)
652   "Always parse the buffer."
653   (gnus-cite-localize)
654   ;;Reset parser information.
655   (setq gnus-cite-prefix-alist nil
656         gnus-cite-attribution-alist nil
657         gnus-cite-loose-prefix-alist nil
658         gnus-cite-loose-attribution-alist nil)
659   (unless no-overlay
660     (gnus-cite-delete-overlays))
661   ;; Parse if not too large.
662   (if (and gnus-cite-parse-max-size
663            (> (buffer-size) gnus-cite-parse-max-size))
664       ()
665     (setq gnus-cite-article (cons (car gnus-article-current)
666                                   (cdr gnus-article-current)))
667     (gnus-cite-parse-wrapper)))
668
669 (defun gnus-cite-delete-overlays ()
670   (dolist (overlay gnus-cite-overlay-list)
671     (when (or (not (gnus-overlay-end overlay))
672               (and (>= (gnus-overlay-end overlay) (point-min))
673                    (<= (gnus-overlay-end overlay) (point-max))))
674       (setq gnus-cite-overlay-list (delete overlay gnus-cite-overlay-list))
675       (gnus-delete-overlay overlay))))
676
677 (defun gnus-cite-parse-wrapper ()
678   ;; Wrap chopped gnus-cite-parse.
679   (article-goto-body)
680   (let ((inhibit-point-motion-hooks t))
681     (save-excursion
682       (gnus-cite-parse-attributions))
683     (save-excursion
684       (gnus-cite-parse))
685     (save-excursion
686       (gnus-cite-connect-attributions))))
687
688 (defun gnus-cite-parse ()
689   ;; Parse and connect citation prefixes and attribution lines.
690
691   ;; Parse current buffer searching for citation prefixes.
692   (let ((line (1+ (count-lines (point-min) (point))))
693         (case-fold-search t)
694         (max (save-excursion
695                (goto-char (point-max))
696                (gnus-article-search-signature)
697                (point)))
698         (prefix-regexp (concat "^\\(" message-cite-prefix-regexp "\\)"))
699         alist entry start begin end numbers prefix guess-limit mc-flag)
700     ;; Get all potential prefixes in `alist'.
701     (while (< (point) max)
702       ;; Each line.
703       (setq begin (point)
704             guess-limit (progn (skip-chars-forward "^> \t\r\n") (point))
705             end (progn (beginning-of-line 2) (point))
706             start end)
707       (goto-char begin)
708       ;; Ignore standard Supercite attribution prefix.
709       (when (and (< guess-limit (+ begin gnus-cite-max-prefix))
710                  (looking-at gnus-supercite-regexp))
711         (if (match-end 1)
712             (setq end (1+ (match-end 1)))
713           (setq end (1+ begin))))
714       ;; Ignore very long prefixes.
715       (when (> end (+ begin gnus-cite-max-prefix))
716         (setq end (+ begin gnus-cite-max-prefix)))
717       (while (re-search-forward prefix-regexp (1- end) t)
718         ;; Each prefix.
719         (setq end (match-end 0)
720               prefix (buffer-substring begin end))
721         (gnus-set-text-properties 0 (length prefix) nil prefix)
722         (setq entry (assoc prefix alist))
723         (if entry
724             (setcdr entry (cons line (cdr entry)))
725           (push (list prefix line) alist))
726         (goto-char begin))
727       (goto-char start)
728       (setq line (1+ line)))
729     ;; We got all the potential prefixes.  Now create
730     ;; `gnus-cite-prefix-alist' containing the oldest prefix for each
731     ;; line that appears at least gnus-cite-minimum-match-count
732     ;; times.  First sort them by length.  Longer is older.
733     (setq alist (sort alist (lambda (a b)
734                               (> (length (car a)) (length (car b))))))
735     (while alist
736       (setq entry (car alist)
737             prefix (car entry)
738             numbers (cdr entry)
739             alist (cdr alist))
740       (cond ((null numbers)
741              ;; No lines with this prefix that wasn't also part of
742              ;; a longer prefix.
743              )
744             ((< (length numbers) gnus-cite-minimum-match-count)
745              ;; Too few lines with this prefix.  We keep it a bit
746              ;; longer in case it is an exact match for an attribution
747              ;; line, but we don't remove the line from other
748              ;; prefixes.
749              (push entry gnus-cite-prefix-alist))
750             (t
751              (push entry
752                    gnus-cite-prefix-alist)
753              ;; Remove articles from other prefixes.
754              (let ((loop alist)
755                    current)
756                (while loop
757                  (setq current (car loop)
758                        loop (cdr loop))
759                  (setcdr current
760                          (gnus-set-difference (cdr current) numbers)))))))))
761
762 (defun gnus-cite-parse-attributions ()
763   (let (al-alist)
764     ;; Parse attributions
765     (while (re-search-forward gnus-cite-attribution-suffix (point-max) t)
766       (let* ((start (match-beginning 0))
767              (end (match-end 0))
768              (wrote (count-lines (point-min) end))
769              (prefix (gnus-cite-find-prefix wrote))
770              ;; Check previous line for an attribution leader.
771              (tag (progn
772                     (beginning-of-line 1)
773                     (when (looking-at gnus-supercite-secondary-regexp)
774                       (buffer-substring (match-beginning 1)
775                                         (match-end 1)))))
776              (in (progn
777                    (goto-char start)
778                    (and (re-search-backward gnus-cite-attribution-prefix
779                                             (save-excursion
780                                               (beginning-of-line 0)
781                                               (point))
782                                             t)
783                         (not (re-search-forward gnus-cite-attribution-suffix
784                                                 start t))
785                         (count-lines (point-min) (1+ (point)))))))
786         (when (eq wrote in)
787           (setq in nil))
788         (goto-char end)
789         ;; don't add duplicates
790         (let ((al (buffer-substring (save-excursion (beginning-of-line 0)
791                                                     (1+ (point)))
792                                     end)))
793           (if (not (assoc al al-alist))
794               (progn
795                 (push (list wrote in prefix tag)
796                       gnus-cite-loose-attribution-alist)
797                 (push (cons al t) al-alist))))))))
798
799 (defun gnus-cite-connect-attributions ()
800   ;; Connect attributions to citations
801
802   ;; No citations have been connected to attribution lines yet.
803   (setq gnus-cite-loose-prefix-alist (append gnus-cite-prefix-alist nil))
804
805   ;; Parse current buffer searching for attribution lines.
806   ;; Find exact supercite citations.
807   (gnus-cite-match-attributions 'small nil
808                                 (lambda (prefix tag)
809                                   (when tag
810                                     (concat "\\`"
811                                             (regexp-quote prefix) "[ \t]*"
812                                             (regexp-quote tag) ">"))))
813   ;; Find loose supercite citations after attributions.
814   (gnus-cite-match-attributions 'small t
815                                 (lambda (prefix tag)
816                                   (when tag
817                                     (concat "\\<"
818                                             (regexp-quote tag)
819                                             "\\>"))))
820   ;; Find loose supercite citations anywhere.
821   (gnus-cite-match-attributions 'small nil
822                                 (lambda (prefix tag)
823                                   (when tag
824                                     (concat "\\<"
825                                             (regexp-quote tag)
826                                             "\\>"))))
827   ;; Find nested citations after attributions.
828   (gnus-cite-match-attributions 'small-if-unique t
829                                 (lambda (prefix tag)
830                                   (concat "\\`" (regexp-quote prefix) ".+")))
831   ;; Find nested citations anywhere.
832   (gnus-cite-match-attributions 'small nil
833                                 (lambda (prefix tag)
834                                   (concat "\\`" (regexp-quote prefix) ".+")))
835   ;; Remove loose prefixes with too few lines.
836   (let ((alist gnus-cite-loose-prefix-alist)
837         entry)
838     (while alist
839       (setq entry (car alist)
840             alist (cdr alist))
841       (when (< (length (cdr entry)) gnus-cite-minimum-match-count)
842         (setq gnus-cite-prefix-alist
843               (delq entry gnus-cite-prefix-alist)
844               gnus-cite-loose-prefix-alist
845               (delq entry gnus-cite-loose-prefix-alist)))))
846   ;; Find flat attributions.
847   (gnus-cite-match-attributions 'first t nil)
848   ;; Find any attributions (are we getting desperate yet?).
849   (gnus-cite-match-attributions 'first nil nil))
850
851 (defun gnus-cite-match-attributions (sort after fun)
852   ;; Match all loose attributions and citations (SORT AFTER FUN) .
853   ;;
854   ;; If SORT is `small', the citation with the shortest prefix will be
855   ;; used, if it is `first' the first prefix will be used, if it is
856   ;; `small-if-unique' the shortest prefix will be used if the
857   ;; attribution line does not share its own prefix with other
858   ;; loose attribution lines, otherwise the first prefix will be used.
859   ;;
860   ;; If AFTER is non-nil, only citations after the attribution line
861   ;; will be considered.
862   ;;
863   ;; If FUN is non-nil, it will be called with the arguments (WROTE
864   ;; PREFIX TAG) and expected to return a regular expression.  Only
865   ;; citations whose prefix matches the regular expression will be
866   ;; considered.
867   ;;
868   ;; WROTE is the attribution line number.
869   ;; PREFIX is the attribution line prefix.
870   ;; TAG is the Supercite tag on the attribution line.
871   (let ((atts gnus-cite-loose-attribution-alist)
872         (case-fold-search t)
873         att wrote in prefix tag regexp limit smallest best size)
874     (while atts
875       (setq att (car atts)
876             atts (cdr atts)
877             wrote (nth 0 att)
878             in (nth 1 att)
879             prefix (nth 2 att)
880             tag (nth 3 att)
881             regexp (if fun (funcall fun prefix tag) "")
882             size (cond ((eq sort 'small) t)
883                        ((eq sort 'first) nil)
884                        (t (< (length (gnus-cite-find-loose prefix)) 2)))
885             limit (if after wrote -1)
886             smallest 1000000
887             best nil)
888       (let ((cites gnus-cite-loose-prefix-alist)
889             cite candidate numbers first compare)
890         (while cites
891           (setq cite (car cites)
892                 cites (cdr cites)
893                 candidate (car cite)
894                 numbers (cdr cite)
895                 first (apply 'min numbers)
896                 compare (if size (length candidate) first))
897           (and (> first limit)
898                regexp
899                (string-match regexp candidate)
900                (< compare smallest)
901                (setq best cite
902                      smallest compare))))
903       (if (null best)
904           ()
905         (setq gnus-cite-loose-attribution-alist
906               (delq att gnus-cite-loose-attribution-alist))
907         (push (cons wrote (car best)) gnus-cite-attribution-alist)
908         (when in
909           (push (cons in (car best)) gnus-cite-attribution-alist))
910         (when (memq best gnus-cite-loose-prefix-alist)
911           (let ((loop gnus-cite-prefix-alist)
912                 (numbers (cdr best))
913                 current)
914             (setq gnus-cite-loose-prefix-alist
915                   (delq best gnus-cite-loose-prefix-alist))
916             (while loop
917               (setq current (car loop)
918                     loop (cdr loop))
919               (if (eq current best)
920                   ()
921                 (setcdr current (gnus-set-difference (cdr current) numbers))
922                 (when (null (cdr current))
923                   (setq gnus-cite-loose-prefix-alist
924                         (delq current gnus-cite-loose-prefix-alist)
925                         atts (delq current atts)))))))))))
926
927 (defun gnus-cite-find-loose (prefix)
928   ;; Return a list of loose attribution lines prefixed by PREFIX.
929   (let* ((atts gnus-cite-loose-attribution-alist)
930          att line lines)
931     (while atts
932       (setq att (car atts)
933             line (car att)
934             atts (cdr atts))
935       (when (string-equal (gnus-cite-find-prefix line) prefix)
936         (push line lines)))
937     lines))
938
939 (defun gnus-cite-add-face (number prefix face)
940   ;; At line NUMBER, ignore PREFIX and add FACE to the rest of the line.
941   (when face
942     (let ((inhibit-point-motion-hooks t)
943           from to overlay)
944       (goto-char (point-min))
945       (when (zerop (forward-line (1- number)))
946         (static-if (or (featurep 'xemacs)
947                        (and (eq emacs-major-version 20)
948                             (>= emacs-minor-version 3))
949                        (>= emacs-major-version 21))
950             (forward-char (length prefix))
951           (move-to-column (string-width prefix)))
952         (skip-chars-forward " \t")
953         (setq from (point))
954         (end-of-line 1)
955         (skip-chars-backward " \t")
956         (setq to (point))
957         (when (< from to)
958           (push (setq overlay (gnus-make-overlay from to))
959                 gnus-cite-overlay-list)
960           (gnus-overlay-put overlay 'face face))))))
961
962 (defun gnus-cite-toggle (prefix)
963   (save-excursion
964     (set-buffer gnus-article-buffer)
965     (gnus-cite-parse-maybe nil t)
966     (let ((buffer-read-only nil)
967           (numbers (cdr (assoc prefix gnus-cite-prefix-alist)))
968           (inhibit-point-motion-hooks t)
969           number)
970       (while numbers
971         (setq number (car numbers)
972               numbers (cdr numbers))
973         (goto-char (point-min))
974         (forward-line (1- number))
975         (cond ((get-text-property (point) 'invisible)
976                ;; Can't remove 'cite from g-a-wash-types here because
977                ;; multiple citations may be hidden -jas
978                (remove-text-properties (point) (progn (forward-line 1) (point))
979                                        gnus-hidden-properties))
980               ((assq number gnus-cite-attribution-alist))
981               (t
982                (gnus-add-wash-type 'cite)
983                (gnus-add-text-properties
984                 (point) (progn (forward-line 1) (point))
985                 (nconc (list 'article-type 'cite)
986                        gnus-hidden-properties))))
987         (let ((gnus-article-mime-handle-alist-1
988                gnus-article-mime-handle-alist))
989           (gnus-set-mode-line 'article))))))
990
991 (defun gnus-cite-find-prefix (line)
992   ;; Return citation prefix for LINE.
993   (let ((alist gnus-cite-prefix-alist)
994         (prefix "")
995         entry)
996     (while alist
997       (setq entry (car alist)
998             alist (cdr alist))
999       (when (memq line (cdr entry))
1000         (setq prefix (car entry))))
1001     prefix))
1002
1003 (defun gnus-cite-localize ()
1004   "Make the citation variables local to the article buffer."
1005   (let ((vars '(gnus-cite-article
1006                 gnus-cite-overlay-list gnus-cite-prefix-alist
1007                 gnus-cite-attribution-alist gnus-cite-loose-prefix-alist
1008                 gnus-cite-loose-attribution-alist)))
1009     (while vars
1010       (make-local-variable (pop vars)))))
1011
1012 (gnus-ems-redefine)
1013
1014 (provide 'gnus-cite)
1015
1016 ;; Local Variables:
1017 ;; coding: iso-8859-1
1018 ;; End:
1019
1020 ;;; gnus-cite.el ends here