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