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