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