Indent.
[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     (ignore-errors
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         (ignore-errors
676           (gnus-delete-overlay overlay))))))
677
678 (defun gnus-cite-parse-wrapper ()
679   ;; Wrap chopped gnus-cite-parse.
680   (article-goto-body)
681   (let ((inhibit-point-motion-hooks t))
682     (save-excursion
683       (gnus-cite-parse-attributions))
684     (save-excursion
685       (gnus-cite-parse))
686     (save-excursion
687       (gnus-cite-connect-attributions))))
688
689 (defun gnus-cite-parse ()
690   ;; Parse and connect citation prefixes and attribution lines.
691
692   ;; Parse current buffer searching for citation prefixes.
693   (let ((line (1+ (count-lines (point-min) (point))))
694         (case-fold-search t)
695         (max (save-excursion
696                (goto-char (point-max))
697                (gnus-article-search-signature)
698                (point)))
699         (prefix-regexp (concat "^\\(" message-cite-prefix-regexp "\\)"))
700         alist entry start begin end numbers prefix guess-limit)
701     ;; Get all potential prefixes in `alist'.
702     (while (< (point) max)
703       ;; Each line.
704       (setq begin (point)
705             guess-limit (progn (skip-chars-forward "^> \t\r\n") (point))
706             end (progn (beginning-of-line 2) (point))
707             start end)
708       (goto-char begin)
709       ;; Ignore standard Supercite attribution prefix.
710       (when (and (< guess-limit (+ begin gnus-cite-max-prefix))
711                  (looking-at gnus-supercite-regexp))
712         (if (match-end 1)
713             (setq end (1+ (match-end 1)))
714           (setq end (1+ begin))))
715       ;; Ignore very long prefixes.
716       (when (> end (+ begin gnus-cite-max-prefix))
717         (setq end (+ begin gnus-cite-max-prefix)))
718       (while (re-search-forward prefix-regexp (1- end) t)
719         ;; Each prefix.
720         (setq end (match-end 0)
721               prefix (buffer-substring begin end))
722         (gnus-set-text-properties 0 (length prefix) nil prefix)
723         (setq entry (assoc prefix alist))
724         (if entry
725             (setcdr entry (cons line (cdr entry)))
726           (push (list prefix line) alist))
727         (goto-char begin))
728       (goto-char start)
729       (setq line (1+ line)))
730     ;; We got all the potential prefixes.  Now create
731     ;; `gnus-cite-prefix-alist' containing the oldest prefix for each
732     ;; line that appears at least gnus-cite-minimum-match-count
733     ;; times.  First sort them by length.  Longer is older.
734     (setq alist (sort alist (lambda (a b)
735                               (> (length (car a)) (length (car b))))))
736     (while alist
737       (setq entry (car alist)
738             prefix (car entry)
739             numbers (cdr entry)
740             alist (cdr alist))
741       (cond ((null numbers)
742              ;; No lines with this prefix that wasn't also part of
743              ;; a longer prefix.
744              )
745             ((< (length numbers) gnus-cite-minimum-match-count)
746              ;; Too few lines with this prefix.  We keep it a bit
747              ;; longer in case it is an exact match for an attribution
748              ;; line, but we don't remove the line from other
749              ;; prefixes.
750              (push entry gnus-cite-prefix-alist))
751             (t
752              (push entry
753                    gnus-cite-prefix-alist)
754              ;; Remove articles from other prefixes.
755              (let ((loop alist)
756                    current)
757                (while loop
758                  (setq current (car loop)
759                        loop (cdr loop))
760                  (setcdr current
761                          (gnus-set-difference (cdr current) numbers)))))))))
762
763 (defun gnus-cite-parse-attributions ()
764   (let (al-alist)
765     ;; Parse attributions
766     (while (re-search-forward gnus-cite-attribution-suffix (point-max) t)
767       (let* ((start (match-beginning 0))
768              (end (match-end 0))
769              (wrote (count-lines (point-min) end))
770              (prefix (gnus-cite-find-prefix wrote))
771              ;; Check previous line for an attribution leader.
772              (tag (progn
773                     (beginning-of-line 1)
774                     (when (looking-at gnus-supercite-secondary-regexp)
775                       (buffer-substring (match-beginning 1)
776                                         (match-end 1)))))
777              (in (progn
778                    (goto-char start)
779                    (and (re-search-backward gnus-cite-attribution-prefix
780                                             (save-excursion
781                                               (beginning-of-line 0)
782                                               (point))
783                                             t)
784                         (not (re-search-forward gnus-cite-attribution-suffix
785                                                 start t))
786                         (count-lines (point-min) (1+ (point)))))))
787         (when (eq wrote in)
788           (setq in nil))
789         (goto-char end)
790         ;; don't add duplicates
791         (let ((al (buffer-substring (save-excursion (beginning-of-line 0)
792                                                     (1+ (point)))
793                                     end)))
794           (if (not (assoc al al-alist))
795               (progn
796                 (push (list wrote in prefix tag)
797                       gnus-cite-loose-attribution-alist)
798                 (push (cons al t) al-alist))))))))
799
800 (defun gnus-cite-connect-attributions ()
801   ;; Connect attributions to citations
802
803   ;; No citations have been connected to attribution lines yet.
804   (setq gnus-cite-loose-prefix-alist (append gnus-cite-prefix-alist nil))
805
806   ;; Parse current buffer searching for attribution lines.
807   ;; Find exact supercite citations.
808   (gnus-cite-match-attributions 'small nil
809                                 (lambda (prefix tag)
810                                   (when tag
811                                     (concat "\\`"
812                                             (regexp-quote prefix) "[ \t]*"
813                                             (regexp-quote tag) ">"))))
814   ;; Find loose supercite citations after attributions.
815   (gnus-cite-match-attributions 'small t
816                                 (lambda (prefix tag)
817                                   (when tag
818                                     (concat "\\<"
819                                             (regexp-quote tag)
820                                             "\\>"))))
821   ;; Find loose supercite citations anywhere.
822   (gnus-cite-match-attributions 'small nil
823                                 (lambda (prefix tag)
824                                   (when tag
825                                     (concat "\\<"
826                                             (regexp-quote tag)
827                                             "\\>"))))
828   ;; Find nested citations after attributions.
829   (gnus-cite-match-attributions 'small-if-unique t
830                                 (lambda (prefix tag)
831                                   (concat "\\`" (regexp-quote prefix) ".+")))
832   ;; Find nested citations anywhere.
833   (gnus-cite-match-attributions 'small nil
834                                 (lambda (prefix tag)
835                                   (concat "\\`" (regexp-quote prefix) ".+")))
836   ;; Remove loose prefixes with too few lines.
837   (let ((alist gnus-cite-loose-prefix-alist)
838         entry)
839     (while alist
840       (setq entry (car alist)
841             alist (cdr alist))
842       (when (< (length (cdr entry)) gnus-cite-minimum-match-count)
843         (setq gnus-cite-prefix-alist
844               (delq entry gnus-cite-prefix-alist)
845               gnus-cite-loose-prefix-alist
846               (delq entry gnus-cite-loose-prefix-alist)))))
847   ;; Find flat attributions.
848   (gnus-cite-match-attributions 'first t nil)
849   ;; Find any attributions (are we getting desperate yet?).
850   (gnus-cite-match-attributions 'first nil nil))
851
852 (defun gnus-cite-match-attributions (sort after fun)
853   ;; Match all loose attributions and citations (SORT AFTER FUN) .
854   ;;
855   ;; If SORT is `small', the citation with the shortest prefix will be
856   ;; used, if it is `first' the first prefix will be used, if it is
857   ;; `small-if-unique' the shortest prefix will be used if the
858   ;; attribution line does not share its own prefix with other
859   ;; loose attribution lines, otherwise the first prefix will be used.
860   ;;
861   ;; If AFTER is non-nil, only citations after the attribution line
862   ;; will be considered.
863   ;;
864   ;; If FUN is non-nil, it will be called with the arguments (WROTE
865   ;; PREFIX TAG) and expected to return a regular expression.  Only
866   ;; citations whose prefix matches the regular expression will be
867   ;; considered.
868   ;;
869   ;; WROTE is the attribution line number.
870   ;; PREFIX is the attribution line prefix.
871   ;; TAG is the Supercite tag on the attribution line.
872   (let ((atts gnus-cite-loose-attribution-alist)
873         (case-fold-search t)
874         att wrote in prefix tag regexp limit smallest best size)
875     (while atts
876       (setq att (car atts)
877             atts (cdr atts)
878             wrote (nth 0 att)
879             in (nth 1 att)
880             prefix (nth 2 att)
881             tag (nth 3 att)
882             regexp (if fun (funcall fun prefix tag) "")
883             size (cond ((eq sort 'small) t)
884                        ((eq sort 'first) nil)
885                        (t (< (length (gnus-cite-find-loose prefix)) 2)))
886             limit (if after wrote -1)
887             smallest 1000000
888             best nil)
889       (let ((cites gnus-cite-loose-prefix-alist)
890             cite candidate numbers first compare)
891         (while cites
892           (setq cite (car cites)
893                 cites (cdr cites)
894                 candidate (car cite)
895                 numbers (cdr cite)
896                 first (apply 'min numbers)
897                 compare (if size (length candidate) first))
898           (and (> first limit)
899                regexp
900                (string-match regexp candidate)
901                (< compare smallest)
902                (setq best cite
903                      smallest compare))))
904       (if (null best)
905           ()
906         (setq gnus-cite-loose-attribution-alist
907               (delq att gnus-cite-loose-attribution-alist))
908         (push (cons wrote (car best)) gnus-cite-attribution-alist)
909         (when in
910           (push (cons in (car best)) gnus-cite-attribution-alist))
911         (when (memq best gnus-cite-loose-prefix-alist)
912           (let ((loop gnus-cite-prefix-alist)
913                 (numbers (cdr best))
914                 current)
915             (setq gnus-cite-loose-prefix-alist
916                   (delq best gnus-cite-loose-prefix-alist))
917             (while loop
918               (setq current (car loop)
919                     loop (cdr loop))
920               (if (eq current best)
921                   ()
922                 (setcdr current (gnus-set-difference (cdr current) numbers))
923                 (when (null (cdr current))
924                   (setq gnus-cite-loose-prefix-alist
925                         (delq current gnus-cite-loose-prefix-alist)
926                         atts (delq current atts)))))))))))
927
928 (defun gnus-cite-find-loose (prefix)
929   ;; Return a list of loose attribution lines prefixed by PREFIX.
930   (let* ((atts gnus-cite-loose-attribution-alist)
931          att line lines)
932     (while atts
933       (setq att (car atts)
934             line (car att)
935             atts (cdr atts))
936       (when (string-equal (gnus-cite-find-prefix line) prefix)
937         (push line lines)))
938     lines))
939
940 (defun gnus-cite-add-face (number prefix face)
941   ;; At line NUMBER, ignore PREFIX and add FACE to the rest of the line.
942   (when face
943     (let ((inhibit-point-motion-hooks t)
944           from to overlay)
945       (goto-char (point-min))
946       (when (zerop (forward-line (1- number)))
947         (forward-char (length prefix))
948         (skip-chars-forward " \t")
949         (setq from (point))
950         (end-of-line 1)
951         (skip-chars-backward " \t")
952         (setq to (point))
953         (when (< from to)
954           (push (setq overlay (gnus-make-overlay from to))
955                 gnus-cite-overlay-list)
956           (gnus-overlay-put overlay 'face face))))))
957
958 (defun gnus-cite-toggle (prefix)
959   (save-excursion
960     (set-buffer gnus-article-buffer)
961     (gnus-cite-parse-maybe nil t)
962     (let ((buffer-read-only nil)
963           (numbers (cdr (assoc prefix gnus-cite-prefix-alist)))
964           (inhibit-point-motion-hooks t)
965           number)
966       (while numbers
967         (setq number (car numbers)
968               numbers (cdr numbers))
969         (goto-char (point-min))
970         (forward-line (1- number))
971         (cond ((get-text-property (point) 'invisible)
972                ;; Can't remove 'cite from g-a-wash-types here because
973                ;; multiple citations may be hidden -jas
974                (remove-text-properties (point) (progn (forward-line 1) (point))
975                                        gnus-hidden-properties))
976               ((assq number gnus-cite-attribution-alist))
977               (t
978                (gnus-add-wash-type 'cite)
979                (gnus-add-text-properties
980                 (point) (progn (forward-line 1) (point))
981                 (nconc (list 'article-type 'cite)
982                        gnus-hidden-properties))))
983         (let ((gnus-article-mime-handle-alist-1
984                gnus-article-mime-handle-alist))
985           (gnus-set-mode-line 'article))))))
986
987 (defun gnus-cite-find-prefix (line)
988   ;; Return citation prefix for LINE.
989   (let ((alist gnus-cite-prefix-alist)
990         (prefix "")
991         entry)
992     (while alist
993       (setq entry (car alist)
994             alist (cdr alist))
995       (when (memq line (cdr entry))
996         (setq prefix (car entry))))
997     prefix))
998
999 (defun gnus-cite-localize ()
1000   "Make the citation variables local to the article buffer."
1001   (let ((vars '(gnus-cite-article
1002                 gnus-cite-overlay-list gnus-cite-prefix-alist
1003                 gnus-cite-attribution-alist gnus-cite-loose-prefix-alist
1004                 gnus-cite-loose-attribution-alist)))
1005     (while vars
1006       (make-local-variable (pop vars)))))
1007
1008 (gnus-ems-redefine)
1009
1010 (provide 'gnus-cite)
1011
1012 ;; Local Variables:
1013 ;; coding: iso-8859-1
1014 ;; End:
1015
1016 ;;; gnus-cite.el ends here