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