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