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