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