root/trunk/lisp/gnus/gnus-cite.el

Revision 4220, 35.5 kB (checked in by miyoshi, 9 months ago)

Sync up with Emacs22.2.

  • Property svn:eol-style set to LF
Line 
1 ;;; gnus-cite.el --- parse citations in articles for Gnus
2
3 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 ;;   2004, 2005, 2006, 2007, 2008 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 3, 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., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (eval-when-compile (require 'cl))
30
31 (require 'gnus)
32 (require 'gnus-range)
33 (require 'gnus-art)
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-cited-opened-text-button-line-format "%(%{[-]%}%)\n"
45   "Format of opened cited text buttons."
46   :group 'gnus-cite
47   :type 'string)
48
49 (defcustom gnus-cited-closed-text-button-line-format "%(%{[+]%}%)\n"
50   "Format of closed cited text buttons."
51   :group 'gnus-cite
52   :type 'string)
53
54 (defcustom gnus-cited-lines-visible nil
55   "The number of lines of hidden cited text to remain visible.
56 Or a pair (cons) of numbers which are the number of lines at the top
57 and bottom of the text, respectively, to remain visible."
58   :group 'gnus-cite
59   :type '(choice (const :tag "none" nil)
60                  integer
61                  (cons :tag "Top and Bottom" integer integer)))
62
63 (defcustom gnus-cite-parse-max-size 25000
64   "Maximum article size (in bytes) where parsing citations is allowed.
65 Set it to nil to parse all articles."
66   :group 'gnus-cite
67   :type '(choice (const :tag "all" nil)
68                  integer))
69
70 (defcustom gnus-cite-max-prefix 20
71   "Maximum possible length for a citation prefix."
72   :group 'gnus-cite
73   :type 'integer)
74
75 (defcustom gnus-supercite-regexp
76   (concat "^\\(" message-cite-prefix-regexp "\\)? *"
77           ">>>>> +\"\\([^\"\n]+\\)\" +==")
78   "*Regexp matching normal Supercite attribution lines.
79 The first grouping must match prefixes added by other packages."
80   :group 'gnus-cite
81   :type 'regexp)
82
83 (defcustom gnus-supercite-secondary-regexp "^.*\"\\([^\"\n]+\\)\" +=="
84   "Regexp matching mangled Supercite attribution lines.
85 The first regexp group should match the Supercite attribution."
86   :group 'gnus-cite
87   :type 'regexp)
88
89 (defcustom gnus-cite-minimum-match-count 2
90   "Minimum number of identical prefixes before we believe it's a citation."
91   :group 'gnus-cite
92   :type 'integer)
93
94 ;; Some Microsoft products put in a citation that extends to the
95 ;; remainder of the message:
96 ;;
97 ;;     -----Original Message-----
98 ;;     From: ...
99 ;;     To: ...
100 ;;     Sent: ...   [date, in non-RFC-2822 format]
101 ;;     Subject: ...
102 ;;
103 ;;     Cited message, with no prefixes
104 ;;
105 ;; The four headers are always the same.  But note they are prone to
106 ;; folding without additional indentation.
107 ;;
108 ;; Others use "----- Original Message -----" instead, and properly quote
109 ;; the body using "> ".  This style is handled without special cases.
110
111 (defcustom gnus-cite-attribution-prefix
112   "In article\\|in <\\|On \\(Mon\\|Tue\\|Wed\\|Thu\\|Fri\\|Sat\\|Sun\\),\\|----- ?Original Message ?-----"
113   "*Regexp matching the beginning of an attribution line."
114   :group 'gnus-cite
115   :type 'regexp)
116
117 (defcustom gnus-cite-attribution-suffix
118   "\\(\\(wrote\\|writes\\|said\\|says\\|>\\)\\(:\\|\\.\\.\\.\\)\\|----- ?Original Message ?-----\\)[ \t]*$"
119   "*Regexp matching the end of an attribution line.
120 The text matching the first grouping will be used as a button."
121   :group 'gnus-cite
122   :type 'regexp)
123
124 (defcustom gnus-cite-unsightly-citation-regexp
125   "^-----Original Message-----\nFrom: \\(.+\n\\)+\n"
126   "Regexp matching Microsoft-type rest-of-message citations."
127   :version "22.1"
128   :group 'gnus-cite
129   :type 'regexp)
130
131 (defcustom gnus-cite-ignore-quoted-from t
132   "Non-nil means don't regard lines beginning with \">From \" as cited text.
133 Those lines may have been quoted by MTAs in order not to mix up with
134 the envelope From line."
135   :version "22.1"
136   :group 'gnus-cite
137   :type 'boolean)
138
139 (defface gnus-cite-attribution '((t (:italic t)))
140   "Face used for attribution lines."
141   :group 'gnus-cite)
142 ;; backward-compatibility alias
143 (put 'gnus-cite-attribution-face 'face-alias 'gnus-cite-attribution)
144
145 (defcustom gnus-cite-attribution-face 'gnus-cite-attribution
146   "Face used for attribution lines.
147 It is merged with the face for the cited text belonging to the attribution."
148   :version "22.1"
149   :group 'gnus-cite
150   :type 'face)
151
152 (defface gnus-cite-1 '((((class color)
153                          (background dark))
154                         (:foreground "light blue"))
155                        (((class color)
156                          (background light))
157                         (:foreground "MidnightBlue"))
158                        (t
159                         (:italic t)))
160   "Citation face."
161   :group 'gnus-cite)
162 ;; backward-compatibility alias
163 (put 'gnus-cite-face-1 'face-alias 'gnus-cite-1)
164
165 (defface gnus-cite-2 '((((class color)
166                          (background dark))
167                         (:foreground "light cyan"))
168                        (((class color)
169                          (background light))
170                         (:foreground "firebrick"))
171                        (t
172                         (:italic t)))
173   "Citation face."
174   :group 'gnus-cite)
175 ;; backward-compatibility alias
176 (put 'gnus-cite-face-2 'face-alias 'gnus-cite-2)
177
178 (defface gnus-cite-3 '((((class color)
179                          (background dark))
180                         (:foreground "light yellow"))
181                        (((class color)
182                          (background light))
183                         (:foreground "dark green"))
184                        (t
185                         (:italic t)))
186   "Citation face."
187   :group 'gnus-cite)
188 ;; backward-compatibility alias
189 (put 'gnus-cite-face-3 'face-alias 'gnus-cite-3)
190
191 (defface gnus-cite-4 '((((class color)
192                          (background dark))
193                         (:foreground "light pink"))
194                        (((class color)
195                          (background light))
196                         (:foreground "OrangeRed"))
197                        (t
198                         (:italic t)))
199   "Citation face."
200   :group 'gnus-cite)
201 ;; backward-compatibility alias
202 (put 'gnus-cite-face-4 'face-alias 'gnus-cite-4)
203
204 (defface gnus-cite-5 '((((class color)
205                          (background dark))
206                         (:foreground "pale green"))
207                        (((class color)
208                          (background light))
209                         (:foreground "dark khaki"))
210                        (t
211                         (:italic t)))
212   "Citation face."
213   :group 'gnus-cite)
214 ;; backward-compatibility alias
215 (put 'gnus-cite-face-5 'face-alias 'gnus-cite-5)
216
217 (defface gnus-cite-6 '((((class color)
218                          (background dark))
219                         (:foreground "beige"))
220                        (((class color)
221                          (background light))
222                         (:foreground "dark violet"))
223                        (t
224                         (:italic t)))
225   "Citation face."
226   :group 'gnus-cite)
227 ;; backward-compatibility alias
228 (put 'gnus-cite-face-6 'face-alias 'gnus-cite-6)
229
230 (defface gnus-cite-7 '((((class color)
231                          (background dark))
232                         (:foreground "orange"))
233                        (((class color)
234                          (background light))
235                         (:foreground "SteelBlue4"))
236                        (t
237                         (:italic t)))
238   "Citation face."
239   :group 'gnus-cite)
240 ;; backward-compatibility alias
241 (put 'gnus-cite-face-7 'face-alias 'gnus-cite-7)
242
243 (defface gnus-cite-8 '((((class color)
244                          (background dark))
245                         (:foreground "magenta"))
246                        (((class color)
247                          (background light))
248                         (:foreground "magenta"))
249                        (t
250                         (:italic t)))
251   "Citation face."
252   :group 'gnus-cite)
253 ;; backward-compatibility alias
254 (put 'gnus-cite-face-8 'face-alias 'gnus-cite-8)
255
256 (defface gnus-cite-9 '((((class color)
257                          (background dark))
258                         (:foreground "violet"))
259                        (((class color)
260                          (background light))
261                         (:foreground "violet"))
262                        (t
263                         (:italic t)))
264   "Citation face."
265   :group 'gnus-cite)
266 ;; backward-compatibility alias
267 (put 'gnus-cite-face-9 'face-alias 'gnus-cite-9)
268
269 (defface gnus-cite-10 '((((class color)
270                           (background dark))
271                          (:foreground "medium purple"))
272                         (((class color)
273                           (background light))
274                          (:foreground "medium purple"))
275                         (t
276                          (:italic t)))
277   "Citation face."
278   :group 'gnus-cite)
279 ;; backward-compatibility alias
280 (put 'gnus-cite-face-10 'face-alias 'gnus-cite-10)
281
282 (defface gnus-cite-11 '((((class color)
283                           (background dark))
284                          (:foreground "turquoise"))
285                         (((class color)
286                           (background light))
287                          (:foreground "turquoise"))
288                         (t
289                          (:italic t)))
290   "Citation face."
291   :group 'gnus-cite)
292 ;; backward-compatibility alias
293 (put 'gnus-cite-face-11 'face-alias 'gnus-cite-11)
294
295 (defcustom gnus-cite-face-list
296   '(gnus-cite-1 gnus-cite-2 gnus-cite-3 gnus-cite-4 gnus-cite-5 gnus-cite-6
297     gnus-cite-7 gnus-cite-8 gnus-cite-9 gnus-cite-10 gnus-cite-11)
298   "*List of faces used for highlighting citations.
299
300 When there are citations from multiple articles in the same message,
301 Gnus will try to give each citation from each article its own face.
302 This should make it easier to see who wrote what."
303   :group 'gnus-cite
304   :type '(repeat face))
305
306 (defcustom gnus-cite-hide-percentage 50
307   "Only hide excess citation if above this percentage of the body."
308   :group 'gnus-cite
309   :type 'number)
310
311 (defcustom gnus-cite-hide-absolute 10
312   "Only hide excess citation if above this number of lines in the body."
313   :group 'gnus-cite
314   :type 'integer)
315
316 (defcustom gnus-cite-blank-line-after-header t
317   "If non-nil, put a blank line between the citation header and the button."
318   :group 'gnus-cite
319   :type 'boolean)
320
321 ;; This has to go here because its default value depends on
322 ;; gnus-cite-face-list.
323 (defcustom gnus-article-boring-faces (cons 'gnus-signature gnus-cite-face-list)
324   "List of faces that are not worth reading.
325 If an article has more pages below the one you are looking at, but
326 nothing on those pages is a word of at least three letters that is not
327 in a boring face, then the pages will be skipped."
328   :type '(repeat face)
329   :group 'gnus-article-hiding)
330
331 ;;; Internal Variables:
332
333 (defvar gnus-cite-article nil)
334 (defvar gnus-cite-overlay-list nil)
335
336 (defvar gnus-cite-prefix-alist nil)
337 ;; Alist of citation prefixes.
338 ;; The cdr is a list of lines with that prefix.
339
340 (defvar gnus-cite-attribution-alist nil)
341 ;; Alist of attribution lines.
342 ;; The car is a line number.
343 ;; The cdr is the prefix for the citation started by that line.
344
345 (defvar gnus-cite-loose-prefix-alist nil)
346 ;; Alist of citation prefixes that have no matching attribution.
347 ;; The cdr is a list of lines with that prefix.
348
349 (defvar gnus-cite-loose-attribution-alist nil)
350 ;; Alist of attribution lines that have no matching citation.
351 ;; Each member has the form (WROTE IN PREFIX TAG), where
352 ;; WROTE: is the attribution line number
353 ;; IN: is the line number of the previous line if part of the same attribution,
354 ;; PREFIX: Is the citation prefix of the attribution line(s), and
355 ;; TAG: Is a Supercite tag, if any.
356
357 (defvar gnus-cited-opened-text-button-line-format-alist
358   `((?b (marker-position beg) ?d)
359     (?e (marker-position end) ?d)
360     (?n (count-lines beg end) ?d)
361     (?l (- end beg) ?d)))
362 (defvar gnus-cited-opened-text-button-line-format-spec nil)
363 (defvar gnus-cited-closed-text-button-line-format-alist
364   gnus-cited-opened-text-button-line-format-alist)
365 (defvar gnus-cited-closed-text-button-line-format-spec nil)
366
367
368 ;;; Commands:
369
370 (defun gnus-article-highlight-citation (&optional force)
371   "Highlight cited text.
372 Each citation in the article will be highlighted with a different face.
373 The faces are taken from `gnus-cite-face-list'.
374 Attribution lines are highlighted with the same face as the
375 corresponding citation merged with the face `gnus-cite-attribution'.
376
377 Text is considered cited if at least `gnus-cite-minimum-match-count'
378 lines matches `message-cite-prefix-regexp' with the same prefix.
379
380 Lines matching `gnus-cite-attribution-suffix' and perhaps
381 `gnus-cite-attribution-prefix' are considered attribution lines."
382   (interactive (list 'force))
383   (save-excursion
384     (set-buffer gnus-article-buffer)
385     (gnus-cite-parse-maybe force)
386     (let ((buffer-read-only nil)
387           (alist gnus-cite-prefix-alist)
388           (faces gnus-cite-face-list)
389           (inhibit-point-motion-hooks t)
390           face entry prefix skip numbers number face-alist)
391       ;; Loop through citation prefixes.
392       (while alist
393         (setq entry (car alist)
394               alist (cdr alist)
395               prefix (car entry)
396               numbers (cdr entry)
397               face (car faces)
398               faces (or (cdr faces) gnus-cite-face-list)
399               face-alist (cons (cons prefix face) face-alist))
400         (while numbers
401           (setq number (car numbers)
402                 numbers (cdr numbers))
403           (and (not (assq number gnus-cite-attribution-alist))
404                (not (assq number gnus-cite-loose-attribution-alist))
405                (gnus-cite-add-face number prefix face))))
406       ;; Loop through attribution lines.
407       (setq alist gnus-cite-attribution-alist)
408       (while alist
409         (setq entry (car alist)
410               alist (cdr alist)
411               number (car entry)
412               prefix (cdr entry)
413               skip (gnus-cite-find-prefix number)
414               face (cdr (assoc prefix face-alist)))
415         ;; Add attribution button.
416         (goto-char (point-min))
417         (forward-line (1- number))
418         (when (re-search-forward gnus-cite-attribution-suffix
419                                  (gnus-point-at-eol)
420                                  t)
421           (gnus-article-add-button (match-beginning 1) (match-end 1)
422                                    'gnus-cite-toggle prefix))
423         ;; Highlight attribution line.
424         (gnus-cite-add-face number skip face)
425         (gnus-cite-add-face number skip gnus-cite-attribution-face))
426       ;; Loop through attribution lines.
427       (setq alist gnus-cite-loose-attribution-alist)
428       (while alist
429         (setq entry (car alist)
430               alist (cdr alist)
431               number (car entry)
432               skip (gnus-cite-find-prefix number))
433         (gnus-cite-add-face number skip gnus-cite-attribution-face)))))
434
435 (defun gnus-dissect-cited-text ()
436   "Dissect the article buffer looking for cited text."
437   (save-excursion
438     (set-buffer gnus-article-buffer)
439     (gnus-cite-parse-maybe nil t)
440     (let ((alist gnus-cite-prefix-alist)
441           prefix numbers number marks m)
442       ;; Loop through citation prefixes.
443       (while alist
444         (setq numbers (pop alist)
445               prefix (pop numbers))
446         (while numbers
447           (setq number (pop numbers))
448           (goto-char (point-min))
449           (forward-line number)
450           (push (cons (point-marker) "") marks)
451           (while (and numbers
452                       (= (1- number) (car numbers)))
453             (setq number (pop numbers)))
454           (goto-char (point-min))
455           (forward-line (1- number))
456           (push (cons (point-marker) prefix) marks)))
457       ;; Skip to the beginning of the body.
458       (article-goto-body)
459       (push (cons (point-marker) "") marks)
460       ;; Find the end of the body.
461       (goto-char (point-max))
462       (gnus-article-search-signature)
463       (push (cons (point-marker) "") marks)
464       ;; Sort the marks.
465       (setq marks (sort marks 'car-less-than-car))
466       (let ((omarks marks))
467         (setq marks nil)
468         (while (cdr omarks)
469           (if (= (caar omarks) (caadr omarks))
470               (progn
471                 (unless (equal (cdar omarks) "")
472                   (push (car omarks) marks))
473                 (unless (equal (cdadr omarks) "")
474                   (push (cadr omarks) marks))
475                 (unless (and (equal (cdar omarks) "")
476                              (equal (cdadr omarks) "")
477                              (not (cddr omarks)))
478                   (setq omarks (cdr omarks))))
479             (push (car omarks) marks))
480           (setq omarks (cdr omarks)))
481         (when (car omarks)
482           (push (car omarks) marks))
483         (setq marks (setq m (nreverse marks)))
484         (while (cddr m)
485           (if (and (equal (cdadr m) "")
486                    (equal (cdar m) (cdaddr m))
487                    (goto-char (caadr m))
488                    (forward-line 1)
489                    (= (point) (caaddr m)))
490               (setcdr m (cdddr m))
491             (setq m (cdr m))))
492         marks))))
493
494 (defun gnus-article-fill-cited-article (&optional force width)
495   "Do word wrapping in the current article.
496 If WIDTH (the numerical prefix), use that text width when filling."
497   (interactive (list t current-prefix-arg))
498   (save-excursion
499     (set-buffer gnus-article-buffer)
500     (let ((buffer-read-only nil)
501           (inhibit-point-motion-hooks t)
502           (marks (gnus-dissect-cited-text))
503           (adaptive-fill-mode nil)
504           (filladapt-mode nil)
505           (fill-column (if width (prefix-numeric-value width) fill-column)))
506       (save-restriction
507         (while (cdr marks)
508           (narrow-to-region (caar marks) (caadr marks))
509           (let ((adaptive-fill-regexp
510                  (concat "^" (regexp-quote (cdar marks)) " *"))
511                 (fill-prefix
512                  (if (string= (cdar marks) "") ""
513                    (concat (cdar marks) " ")))
514                 use-hard-newlines)
515             (fill-region (point-min) (point-max)))
516           (set-marker (caar marks) nil)
517           (setq marks (cdr marks)))
518         (when marks
519           (set-marker (caar marks) nil))
520         ;; All this information is now incorrect.
521         (setq gnus-cite-prefix-alist nil
522               gnus-cite-attribution-alist nil
523               gnus-cite-loose-prefix-alist nil
524               gnus-cite-loose-attribution-alist nil
525               gnus-cite-article nil)))))
526
527 (defun gnus-article-hide-citation (&optional arg force)
528   "Toggle hiding of all cited text except attribution lines.
529 See the documentation for `gnus-article-highlight-citation'.
530 If given a negative prefix, always show; if given a positive prefix,
531 always hide."
532   (interactive (append (gnus-article-hidden-arg) (list 'force)))
533   (gnus-set-format 'cited-opened-text-button t)
534   (gnus-set-format 'cited-closed-text-button t)
535   (save-excursion
536     (set-buffer gnus-article-buffer)
537       (let ((buffer-read-only nil)
538             marks
539             (inhibit-point-motion-hooks t)
540             (props (nconc (list 'article-type 'cite)
541                           gnus-hidden-properties))
542             (point (point-min))
543             found beg end start)
544         (while (setq point
545                      (text-property-any point (point-max)
546                                         'gnus-callback
547                                         'gnus-article-toggle-cited-text))
548           (setq found t)
549           (goto-char point)
550           (gnus-article-toggle-cited-text
551            (get-text-property point 'gnus-data) arg)
552           (forward-line 1)
553           (setq point (point)))
554         (unless found
555           (setq marks (gnus-dissect-cited-text))
556           (while marks
557             (setq beg nil
558                   end nil)
559             (while (and marks (string= (cdar marks) ""))
560               (setq marks (cdr marks)))
561             (when marks
562               (setq beg (caar marks)))
563             (while (and marks (not (string= (cdar marks) "")))
564               (setq marks (cdr marks)))
565             (when marks
566             (setq end (caar marks)))
567             ;; Skip past lines we want to leave visible.
568             (when (and beg end gnus-cited-lines-visible)
569               (goto-char beg)
570               (forward-line (if (consp gnus-cited-lines-visible)
571                                 (car gnus-cited-lines-visible)
572                               gnus-cited-lines-visible))
573               (if (>= (point) end)
574                   (setq beg nil)
575                 (setq beg (point-marker))
576                 (when (consp gnus-cited-lines-visible)
577                   (goto-char end)
578                   (forward-line (- (cdr gnus-cited-lines-visible)))
579                   (if (<= (point) beg)
580                       (setq beg nil)
581                   (setq end (point-marker))))))
582             (when (and beg end)
583               (gnus-add-wash-type 'cite)
584               ;; We use markers for the end-points to facilitate later
585               ;; wrapping and mangling of text.
586               (setq beg (set-marker (make-marker) beg)
587                     end (set-marker (make-marker) end))
588               (gnus-add-text-properties-when 'article-type nil beg end props)
589               (goto-char beg)
590               (when (and gnus-cite-blank-line-after-header
591                          (not (save-excursion (search-backward "\n\n" nil t))))
592                 (insert "\n"))
593               (put-text-property
594                (setq start (point-marker))
595                (progn
596                (gnus-article-add-button
597                 (point)
598                 (progn (eval gnus-cited-closed-text-button-line-format-spec)
599                        (point))
600                 `gnus-article-toggle-cited-text
601                 (list (cons beg end) start))
602                (point))
603                'article-type 'annotation)
604               (set-marker beg (point))))))))
605
606 (defun gnus-article-toggle-cited-text (args &optional arg)
607   "Toggle hiding the text in REGION.
608 ARG can be nil or a number.  Positive means hide, negative
609 means show, nil means toggle."
610   (let* ((region (car args))
611          (beg (car region))
612          (end (cdr region))
613          (start (cadr args))
614          (hidden
615           (text-property-any beg (1- end) 'article-type 'cite))
616          (inhibit-point-motion-hooks t)
617          buffer-read-only)
618     (when (or (null arg)
619               (zerop arg)
620               (and (> arg 0) (not hidden))
621               (and (< arg 0) hidden))
622       (if hidden
623           (progn
624             ;; Can't remove 'cite from g-a-wash-types here because
625             ;; multiple citations may be hidden -jas
626             (gnus-remove-text-properties-when
627              'article-type 'cite beg end
628              (cons 'article-type (cons 'cite
629                                        gnus-hidden-properties))))
630         (gnus-add-wash-type 'cite)
631         (gnus-add-text-properties-when
632          'article-type nil beg end
633          (cons 'article-type (cons 'cite
634                                    gnus-hidden-properties))))
635       (let ((gnus-article-mime-handle-alist-1 gnus-article-mime-handle-alist))
636         (gnus-set-mode-line 'article))
637       (save-excursion
638         (goto-char start)
639         (gnus-delete-line)
640         (put-text-property
641          (point)
642          (progn
643            (gnus-article-add-button
644             (point)
645             (progn (eval
646                     (if hidden
647                         gnus-cited-opened-text-button-line-format-spec
648                       gnus-cited-closed-text-button-line-format-spec))
649                    (point))
650             `gnus-article-toggle-cited-text
651             args)
652            (point))
653          'article-type 'annotation)))))
654
655 (defun gnus-article-hide-citation-maybe (&optional arg force)
656   "Toggle hiding of cited text that has an attribution line.
657 If given a negative prefix, always show; if given a positive prefix,
658 always hide.
659 This will do nothing unless at least `gnus-cite-hide-percentage'
660 percent and at least `gnus-cite-hide-absolute' lines of the body is
661 cited text with attributions.  When called interactively, these two
662 variables are ignored.
663 See also the documentation for `gnus-article-highlight-citation'."
664   (interactive (append (gnus-article-hidden-arg) '(force)))
665   (with-current-buffer gnus-article-buffer
666     (gnus-delete-wash-type 'cite)
667     (unless (gnus-article-check-hidden-text 'cite arg)
668       (save-excursion
669         (gnus-cite-parse-maybe force)
670         (article-goto-body)
671         (let ((start (point))
672               (atts gnus-cite-attribution-alist)
673               (buffer-read-only nil)
674               (inhibit-point-motion-hooks t)
675               (hidden 0)
676               total)
677           (goto-char (point-max))
678           (gnus-article-search-signature)
679           (setq total (count-lines start (point)))
680           (while atts
681             (setq hidden (+ hidden (length (cdr (assoc (cdar atts)
682                                                        gnus-cite-prefix-alist))))
683                   atts (cdr atts)))
684           (when (or force
685                     (and (> (* 100 hidden) (* gnus-cite-hide-percentage total))
686                          (> hidden gnus-cite-hide-absolute)))
687             (gnus-add-wash-type 'cite)
688             (setq atts gnus-cite-attribution-alist)
689             (while atts
690               (setq total (cdr (assoc (cdar atts) gnus-cite-prefix-alist))
691                     atts (cdr atts))
692               (while total
693                 (setq hidden (car total)
694                       total (cdr total))
695                 (goto-char (point-min))
696                 (forward-line (1- hidden))
697                 (unless (assq hidden gnus-cite-attribution-alist)
698                   (gnus-add-text-properties
699                    (point) (progn (forward-line 1) (point))
700                    (nconc (list 'article-type 'cite)
701                           gnus-hidden-properties)))))))))
702     (gnus-set-mode-line 'article)))
703
704 (defun gnus-article-hide-citation-in-followups ()
705   "Hide cited text in non-root articles."
706   (interactive)
707   (save-excursion
708     (set-buffer gnus-article-buffer)
709     (let ((article (cdr gnus-article-current)))
710       (unless (save-excursion
711                 (set-buffer gnus-summary-buffer)
712                 (gnus-article-displayed-root-p article))
713         (gnus-article-hide-citation)))))
714
715 ;;; Internal functions:
716
717 (defun gnus-cite-parse-maybe (&optional force no-overlay)
718   "Always parse the buffer."
719   (gnus-cite-localize)
720   ;;Reset parser information.
721   (setq gnus-cite-prefix-alist nil
722         gnus-cite-attribution-alist nil
723         gnus-cite-loose-prefix-alist nil
724         gnus-cite-loose-attribution-alist nil)
725   (unless no-overlay
726     (gnus-cite-delete-overlays))
727   ;; Parse if not too large.
728   (if (and gnus-cite-parse-max-size
729            (> (buffer-size) gnus-cite-parse-max-size))
730       ()
731     (setq gnus-cite-article (cons (car gnus-article-current)
732                                   (cdr gnus-article-current)))
733     (gnus-cite-parse-wrapper)))
734
735 (defun gnus-cite-delete-overlays ()
736   (dolist (overlay gnus-cite-overlay-list)
737     (ignore-errors
738       (when (or (not (gnus-overlay-end overlay))
739                 (and (>= (gnus-overlay-end overlay) (point-min))
740                      (<= (gnus-overlay-end overlay) (point-max))))
741         (setq gnus-cite-overlay-list (delete overlay gnus-cite-overlay-list))
742         (ignore-errors
743           (gnus-delete-overlay overlay))))))
744
745 (defun gnus-cite-parse-wrapper ()
746   ;; Wrap chopped gnus-cite-parse.
747   (article-goto-body)
748   (let ((inhibit-point-motion-hooks t))
749     (save-excursion
750       (gnus-cite-parse-attributions))
751     (save-excursion
752       (gnus-cite-parse))
753     (save-excursion
754       (gnus-cite-connect-attributions))))
755
756 (defun gnus-cite-parse ()
757   ;; Parse and connect citation prefixes and attribution lines.
758
759   ;; Parse current buffer searching for citation prefixes.
760   (let ((line (1+ (count-lines (point-min) (point))))
761         (case-fold-search t)
762         (max (save-excursion
763                (goto-char (point-max))
764                (gnus-article-search-signature)
765                (point)))
766         (prefix-regexp (concat "^\\(" message-cite-prefix-regexp "\\)"))
767         alist entry start begin end numbers prefix guess-limit)
768     ;; Get all potential prefixes in `alist'.
769     (while (< (point) max)
770       ;; Each line.
771       (setq begin (point)
772             guess-limit (progn (skip-chars-forward "^> \t\r\n") (point))
773             end (gnus-point-at-bol 2)
774             start end)
775       (goto-char begin)
776       ;; Ignore standard Supercite attribution prefix.
777       (when (and (< guess-limit (+ begin gnus-cite-max-prefix))
778                  (looking-at gnus-supercite-regexp))
779         (if (match-end 1)
780             (setq end (1+ (match-end 1)))
781           (setq end (1+ begin))))
782       ;; Ignore very long prefixes.
783       (when (> end (+ begin gnus-cite-max-prefix))
784         (setq end (+ begin gnus-cite-max-prefix)))
785       ;; Ignore quoted envelope From_.
786       (when (and gnus-cite-ignore-quoted-from
787                  (prog2
788                      (setq case-fold-search nil)
789                      (looking-at ">From ")
790                    (setq case-fold-search t)))
791         (setq end (1+ begin)))
792       (while (re-search-forward prefix-regexp (1- end) t)
793         ;; Each prefix.
794         (setq end (match-end 0)
795               prefix (buffer-substring begin end))
796         (gnus-set-text-properties 0 (length prefix) nil prefix)
797         (setq entry (assoc prefix alist))
798         (if entry
799             (setcdr entry (cons line (cdr entry)))
800           (push (list prefix line) alist))
801         (goto-char begin))
802       (goto-char start)
803       (setq line (1+ line)))
804     ;; Horrible special case for some Microsoft mailers.
805     (goto-char (point-min))
806     (when (re-search-forward gnus-cite-unsightly-citation-regexp max t)
807       (setq begin (count-lines (point-min) (point)))
808       (setq end (count-lines (point-min) max))
809       (setq entry nil)
810       (while (< begin end)
811         (push begin entry)
812         (setq begin (1+ begin)))
813       (push (cons "" entry) alist))
814     ;; We got all the potential prefixes.  Now create
815     ;; `gnus-cite-prefix-alist' containing the oldest prefix for each
816     ;; line that appears at least `gnus-cite-minimum-match-count'
817     ;; times.  First sort them by length.  Longer is older.
818     (setq alist (sort alist (lambda (a b)
819                               (> (length (car a)) (length (car b))))))
820     (while alist
821       (setq entry (car alist)
822             prefix (car entry)
823             numbers (cdr entry)
824             alist (cdr alist))
825       (cond ((null numbers)
826              ;; No lines with this prefix that wasn't also part of
827              ;; a longer prefix.
828              )
829             ((< (length numbers) gnus-cite-minimum-match-count)
830              ;; Too few lines with this prefix.  We keep it a bit
831              ;; longer in case it is an exact match for an attribution
832              ;; line, but we don't remove the line from other
833              ;; prefixes.
834              (push entry gnus-cite-prefix-alist))
835             (t
836              (push entry
837                    gnus-cite-prefix-alist)
838              ;; Remove articles from other prefixes.
839              (let ((loop alist)
840                    current)
841                (while loop
842                  (setq current (car loop)
843                        loop (cdr loop))
844                  (setcdr current
845                          (gnus-set-difference (