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