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