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