Merge from emacs--rel--22
[emacs.git] / lisp / gnus / gnus-cite.el
blob64c0864db78d140bdfbf11377d7438b07cb49b48
1 ;;; gnus-cite.el --- parse citations in articles for Gnus
3 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 ;; 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6 ;; Author: Per Abhiddenware
8 ;; This file is part of GNU Emacs.
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 3, or (at your option)
13 ;; any later version.
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.
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.
25 ;;; Commentary:
27 ;;; Code:
29 (eval-when-compile (require 'cl))
30 (eval-when-compile
31 (when (featurep 'xemacs)
32 (require 'easy-mmode))) ; for `define-minor-mode'
34 (require 'gnus)
35 (require 'gnus-range)
36 (require 'gnus-art)
37 (require 'message) ; for message-cite-prefix-regexp
39 ;;; Customization:
41 (defgroup gnus-cite nil
42 "Citation."
43 :prefix "gnus-cite-"
44 :link '(custom-manual "(gnus)Article Highlighting")
45 :group 'gnus-article)
47 (defcustom gnus-cited-opened-text-button-line-format "%(%{[-]%}%)\n"
48 "Format of opened cited text buttons."
49 :group 'gnus-cite
50 :type 'string)
52 (defcustom gnus-cited-closed-text-button-line-format "%(%{[+]%}%)\n"
53 "Format of closed cited text buttons."
54 :group 'gnus-cite
55 :type 'string)
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)))
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))
73 (defcustom gnus-cite-max-prefix 20
74 "Maximum possible length for a citation prefix."
75 :group 'gnus-cite
76 :type 'integer)
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)
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)
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)
97 ;; Some Microsoft products put in a citation that extends to the
98 ;; remainder of the message:
100 ;; -----Original Message-----
101 ;; From: ...
102 ;; To: ...
103 ;; Sent: ... [date, in non-RFC-2822 format]
104 ;; Subject: ...
106 ;; Cited message, with no prefixes
108 ;; The four headers are always the same. But note they are prone to
109 ;; folding without additional indentation.
111 ;; Others use "----- Original Message -----" instead, and properly quote
112 ;; the body using "> ". This style is handled without special cases.
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)
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)
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)
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)
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)
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)
155 (defface gnus-cite-1 '((((class color)
156 (background dark))
157 (:foreground "light blue"))
158 (((class color)
159 (background light))
160 (:foreground "MidnightBlue"))
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)
168 (defface gnus-cite-2 '((((class color)
169 (background dark))
170 (:foreground "light cyan"))
171 (((class color)
172 (background light))
173 (:foreground "firebrick"))
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)
181 (defface gnus-cite-3 '((((class color)
182 (background dark))
183 (:foreground "light yellow"))
184 (((class color)
185 (background light))
186 (:foreground "dark green"))
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)
194 (defface gnus-cite-4 '((((class color)
195 (background dark))
196 (:foreground "light pink"))
197 (((class color)
198 (background light))
199 (:foreground "OrangeRed"))
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)
207 (defface gnus-cite-5 '((((class color)
208 (background dark))
209 (:foreground "pale green"))
210 (((class color)
211 (background light))
212 (:foreground "dark khaki"))
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)
220 (defface gnus-cite-6 '((((class color)
221 (background dark))
222 (:foreground "beige"))
223 (((class color)
224 (background light))
225 (:foreground "dark violet"))
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)
233 (defface gnus-cite-7 '((((class color)
234 (background dark))
235 (:foreground "orange"))
236 (((class color)
237 (background light))
238 (:foreground "SteelBlue4"))
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)
246 (defface gnus-cite-8 '((((class color)
247 (background dark))
248 (:foreground "magenta"))
249 (((class color)
250 (background light))
251 (:foreground "magenta"))
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)
259 (defface gnus-cite-9 '((((class color)
260 (background dark))
261 (:foreground "violet"))
262 (((class color)
263 (background light))
264 (:foreground "violet"))
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)
272 (defface gnus-cite-10 '((((class color)
273 (background dark))
274 (:foreground "plum1"))
275 (((class color)
276 (background light))
277 (:foreground "medium purple"))
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)
285 (defface gnus-cite-11 '((((class color)
286 (background dark))
287 (:foreground "turquoise"))
288 (((class color)
289 (background light))
290 (:foreground "turquoise"))
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)
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.
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 :set (lambda (symbol value)
309 (prog1
310 (custom-set-default symbol value)
311 (if (boundp 'gnus-message-max-citation-depth)
312 (setq gnus-message-max-citation-depth (length value)))
313 (if (boundp 'gnus-message-citation-keywords)
314 (setq gnus-message-citation-keywords
315 `((gnus-message-search-citation-line
316 ,@(let ((list nil)
317 (count 1))
318 (dolist (face value (nreverse list))
319 (push (list count (list 'quote face) 'prepend t)
320 list)
321 (setq count (1+ count)))))))))))
323 (defcustom gnus-cite-hide-percentage 50
324 "Only hide excess citation if above this percentage of the body."
325 :group 'gnus-cite
326 :type 'number)
328 (defcustom gnus-cite-hide-absolute 10
329 "Only hide excess citation if above this number of lines in the body."
330 :group 'gnus-cite
331 :type 'integer)
333 (defcustom gnus-cite-blank-line-after-header t
334 "If non-nil, put a blank line between the citation header and the button."
335 :group 'gnus-cite
336 :type 'boolean)
338 ;; This has to go here because its default value depends on
339 ;; gnus-cite-face-list.
340 (defcustom gnus-article-boring-faces (cons 'gnus-signature gnus-cite-face-list)
341 "List of faces that are not worth reading.
342 If an article has more pages below the one you are looking at, but
343 nothing on those pages is a word of at least three letters that is not
344 in a boring face, then the pages will be skipped."
345 :type '(repeat face)
346 :group 'gnus-article-hiding)
348 ;;; Internal Variables:
350 (defvar gnus-cite-article nil)
351 (defvar gnus-cite-overlay-list nil)
353 (defvar gnus-cite-prefix-alist nil)
354 ;; Alist of citation prefixes.
355 ;; The cdr is a list of lines with that prefix.
357 (defvar gnus-cite-attribution-alist nil)
358 ;; Alist of attribution lines.
359 ;; The car is a line number.
360 ;; The cdr is the prefix for the citation started by that line.
362 (defvar gnus-cite-loose-prefix-alist nil)
363 ;; Alist of citation prefixes that have no matching attribution.
364 ;; The cdr is a list of lines with that prefix.
366 (defvar gnus-cite-loose-attribution-alist nil)
367 ;; Alist of attribution lines that have no matching citation.
368 ;; Each member has the form (WROTE IN PREFIX TAG), where
369 ;; WROTE: is the attribution line number
370 ;; IN: is the line number of the previous line if part of the same attribution,
371 ;; PREFIX: Is the citation prefix of the attribution line(s), and
372 ;; TAG: Is a Supercite tag, if any.
374 (defvar gnus-cited-opened-text-button-line-format-alist
375 `((?b (marker-position beg) ?d)
376 (?e (marker-position end) ?d)
377 (?n (count-lines beg end) ?d)
378 (?l (- end beg) ?d)))
379 (defvar gnus-cited-opened-text-button-line-format-spec nil)
380 (defvar gnus-cited-closed-text-button-line-format-alist
381 gnus-cited-opened-text-button-line-format-alist)
382 (defvar gnus-cited-closed-text-button-line-format-spec nil)
385 ;;; Commands:
387 (defun gnus-article-highlight-citation (&optional force same-buffer)
388 "Highlight cited text.
389 Each citation in the article will be highlighted with a different face.
390 The faces are taken from `gnus-cite-face-list'.
391 Attribution lines are highlighted with the same face as the
392 corresponding citation merged with the face `gnus-cite-attribution'.
394 Text is considered cited if at least `gnus-cite-minimum-match-count'
395 lines matches `message-cite-prefix-regexp' with the same prefix.
397 Lines matching `gnus-cite-attribution-suffix' and perhaps
398 `gnus-cite-attribution-prefix' are considered attribution lines."
399 (interactive (list 'force))
400 (save-excursion
401 (unless same-buffer
402 (set-buffer gnus-article-buffer))
403 (gnus-cite-parse-maybe force)
404 (let ((buffer-read-only nil)
405 (alist gnus-cite-prefix-alist)
406 (faces gnus-cite-face-list)
407 (inhibit-point-motion-hooks t)
408 face entry prefix skip numbers number face-alist)
409 ;; Loop through citation prefixes.
410 (while alist
411 (setq entry (car alist)
412 alist (cdr alist)
413 prefix (car entry)
414 numbers (cdr entry)
415 face (car faces)
416 faces (or (cdr faces) gnus-cite-face-list)
417 face-alist (cons (cons prefix face) face-alist))
418 (while numbers
419 (setq number (car numbers)
420 numbers (cdr numbers))
421 (and (not (assq number gnus-cite-attribution-alist))
422 (not (assq number gnus-cite-loose-attribution-alist))
423 (gnus-cite-add-face number prefix face))))
424 ;; Loop through attribution lines.
425 (setq alist gnus-cite-attribution-alist)
426 (while alist
427 (setq entry (car alist)
428 alist (cdr alist)
429 number (car entry)
430 prefix (cdr entry)
431 skip (gnus-cite-find-prefix number)
432 face (cdr (assoc prefix face-alist)))
433 ;; Add attribution button.
434 (goto-char (point-min))
435 (forward-line (1- number))
436 (when (re-search-forward gnus-cite-attribution-suffix
437 (point-at-eol)
439 (gnus-article-add-button (match-beginning 1) (match-end 1)
440 'gnus-cite-toggle prefix))
441 ;; Highlight attribution line.
442 (gnus-cite-add-face number skip face)
443 (gnus-cite-add-face number skip gnus-cite-attribution-face))
444 ;; Loop through attribution lines.
445 (setq alist gnus-cite-loose-attribution-alist)
446 (while alist
447 (setq entry (car alist)
448 alist (cdr alist)
449 number (car entry)
450 skip (gnus-cite-find-prefix number))
451 (gnus-cite-add-face number skip gnus-cite-attribution-face)))))
453 (defun gnus-dissect-cited-text ()
454 "Dissect the article buffer looking for cited text."
455 (save-excursion
456 (set-buffer gnus-article-buffer)
457 (gnus-cite-parse-maybe nil t)
458 (let ((alist gnus-cite-prefix-alist)
459 prefix numbers number marks m)
460 ;; Loop through citation prefixes.
461 (while alist
462 (setq numbers (pop alist)
463 prefix (pop numbers))
464 (while numbers
465 (setq number (pop numbers))
466 (goto-char (point-min))
467 (forward-line number)
468 (push (cons (point-marker) "") marks)
469 (while (and numbers
470 (= (1- number) (car numbers)))
471 (setq number (pop numbers)))
472 (goto-char (point-min))
473 (forward-line (1- number))
474 (push (cons (point-marker) prefix) marks)))
475 ;; Skip to the beginning of the body.
476 (article-goto-body)
477 (push (cons (point-marker) "") marks)
478 ;; Find the end of the body.
479 (goto-char (point-max))
480 (gnus-article-search-signature)
481 (push (cons (point-marker) "") marks)
482 ;; Sort the marks.
483 (setq marks (sort marks 'car-less-than-car))
484 (let ((omarks marks))
485 (setq marks nil)
486 (while (cdr omarks)
487 (if (= (caar omarks) (caadr omarks))
488 (progn
489 (unless (equal (cdar omarks) "")
490 (push (car omarks) marks))
491 (unless (equal (cdadr omarks) "")
492 (push (cadr omarks) marks))
493 (unless (and (equal (cdar omarks) "")
494 (equal (cdadr omarks) "")
495 (not (cddr omarks)))
496 (setq omarks (cdr omarks))))
497 (push (car omarks) marks))
498 (setq omarks (cdr omarks)))
499 (when (car omarks)
500 (push (car omarks) marks))
501 (setq marks (setq m (nreverse marks)))
502 (while (cddr m)
503 (if (and (equal (cdadr m) "")
504 (equal (cdar m) (cdaddr m))
505 (goto-char (caadr m))
506 (forward-line 1)
507 (= (point) (caaddr m)))
508 (setcdr m (cdddr m))
509 (setq m (cdr m))))
510 marks))))
512 (defun gnus-article-fill-cited-article (&optional force width)
513 "Do word wrapping in the current article.
514 If WIDTH (the numerical prefix), use that text width when filling."
515 (interactive (list t current-prefix-arg))
516 (save-excursion
517 (set-buffer gnus-article-buffer)
518 (let ((buffer-read-only nil)
519 (inhibit-point-motion-hooks t)
520 (marks (gnus-dissect-cited-text))
521 (adaptive-fill-mode nil)
522 (filladapt-mode nil)
523 (fill-column (if width (prefix-numeric-value width) fill-column)))
524 (save-restriction
525 (while (cdr marks)
526 (narrow-to-region (caar marks) (caadr marks))
527 (let ((adaptive-fill-regexp
528 (concat "^" (regexp-quote (cdar marks)) " *"))
529 (fill-prefix
530 (if (string= (cdar marks) "") ""
531 (concat (cdar marks) " ")))
532 use-hard-newlines)
533 (fill-region (point-min) (point-max)))
534 (set-marker (caar marks) nil)
535 (setq marks (cdr marks)))
536 (when marks
537 (set-marker (caar marks) nil))
538 ;; All this information is now incorrect.
539 (setq gnus-cite-prefix-alist nil
540 gnus-cite-attribution-alist nil
541 gnus-cite-loose-prefix-alist nil
542 gnus-cite-loose-attribution-alist nil
543 gnus-cite-article nil)))))
545 (defun gnus-article-hide-citation (&optional arg force)
546 "Toggle hiding of all cited text except attribution lines.
547 See the documentation for `gnus-article-highlight-citation'.
548 If given a negative prefix, always show; if given a positive prefix,
549 always hide."
550 (interactive (append (gnus-article-hidden-arg) (list 'force)))
551 (gnus-set-format 'cited-opened-text-button t)
552 (gnus-set-format 'cited-closed-text-button t)
553 (save-excursion
554 (set-buffer gnus-article-buffer)
555 (let ((buffer-read-only nil)
556 marks
557 (inhibit-point-motion-hooks t)
558 (props (nconc (list 'article-type 'cite)
559 gnus-hidden-properties))
560 (point (point-min))
561 found beg end start)
562 (while (setq point
563 (text-property-any point (point-max)
564 'gnus-callback
565 'gnus-article-toggle-cited-text))
566 (setq found t)
567 (goto-char point)
568 (gnus-article-toggle-cited-text
569 (get-text-property point 'gnus-data) arg)
570 (forward-line 1)
571 (setq point (point)))
572 (unless found
573 (setq marks (gnus-dissect-cited-text))
574 (while marks
575 (setq beg nil
576 end nil)
577 (while (and marks (string= (cdar marks) ""))
578 (setq marks (cdr marks)))
579 (when marks
580 (setq beg (caar marks)))
581 (while (and marks (not (string= (cdar marks) "")))
582 (setq marks (cdr marks)))
583 (when marks
584 (setq end (caar marks)))
585 ;; Skip past lines we want to leave visible.
586 (when (and beg end gnus-cited-lines-visible)
587 (goto-char beg)
588 (forward-line (if (consp gnus-cited-lines-visible)
589 (car gnus-cited-lines-visible)
590 gnus-cited-lines-visible))
591 (if (>= (point) end)
592 (setq beg nil)
593 (setq beg (point-marker))
594 (when (consp gnus-cited-lines-visible)
595 (goto-char end)
596 (forward-line (- (cdr gnus-cited-lines-visible)))
597 (if (<= (point) beg)
598 (setq beg nil)
599 (setq end (point-marker))))))
600 (when (and beg end)
601 (gnus-add-wash-type 'cite)
602 ;; We use markers for the end-points to facilitate later
603 ;; wrapping and mangling of text.
604 (setq beg (set-marker (make-marker) beg)
605 end (set-marker (make-marker) end))
606 (gnus-add-text-properties-when 'article-type nil beg end props)
607 (goto-char beg)
608 (when (and gnus-cite-blank-line-after-header
609 (not (save-excursion (search-backward "\n\n" nil t))))
610 (insert "\n"))
611 (put-text-property
612 (setq start (point-marker))
613 (progn
614 (gnus-article-add-button
615 (point)
616 (progn (eval gnus-cited-closed-text-button-line-format-spec)
617 (point))
618 `gnus-article-toggle-cited-text
619 (list (cons beg end) start))
620 (point))
621 'article-type 'annotation)
622 (set-marker beg (point))))))))
624 (defun gnus-article-toggle-cited-text (args &optional arg)
625 "Toggle hiding the text in REGION.
626 ARG can be nil or a number. Positive means hide, negative
627 means show, nil means toggle."
628 (let* ((region (car args))
629 (beg (car region))
630 (end (cdr region))
631 (start (cadr args))
632 (hidden
633 (text-property-any beg (1- end) 'article-type 'cite))
634 (inhibit-point-motion-hooks t)
635 buffer-read-only)
636 (when (or (null arg)
637 (zerop arg)
638 (and (> arg 0) (not hidden))
639 (and (< arg 0) hidden))
640 (if hidden
641 (progn
642 ;; Can't remove 'cite from g-a-wash-types here because
643 ;; multiple citations may be hidden -jas
644 (gnus-remove-text-properties-when
645 'article-type 'cite beg end
646 (cons 'article-type (cons 'cite
647 gnus-hidden-properties))))
648 (gnus-add-wash-type 'cite)
649 (gnus-add-text-properties-when
650 'article-type nil beg end
651 (cons 'article-type (cons 'cite
652 gnus-hidden-properties))))
653 (let ((gnus-article-mime-handle-alist-1 gnus-article-mime-handle-alist))
654 (gnus-set-mode-line 'article))
655 (save-excursion
656 (goto-char start)
657 (gnus-delete-line)
658 (put-text-property
659 (point)
660 (progn
661 (gnus-article-add-button
662 (point)
663 (progn (eval
664 (if hidden
665 gnus-cited-opened-text-button-line-format-spec
666 gnus-cited-closed-text-button-line-format-spec))
667 (point))
668 `gnus-article-toggle-cited-text
669 args)
670 (point))
671 'article-type 'annotation)))))
673 (defun gnus-article-hide-citation-maybe (&optional arg force)
674 "Toggle hiding of cited text that has an attribution line.
675 If given a negative prefix, always show; if given a positive prefix,
676 always hide.
677 This will do nothing unless at least `gnus-cite-hide-percentage'
678 percent and at least `gnus-cite-hide-absolute' lines of the body is
679 cited text with attributions. When called interactively, these two
680 variables are ignored.
681 See also the documentation for `gnus-article-highlight-citation'."
682 (interactive (append (gnus-article-hidden-arg) '(force)))
683 (with-current-buffer gnus-article-buffer
684 (gnus-delete-wash-type 'cite)
685 (unless (gnus-article-check-hidden-text 'cite arg)
686 (save-excursion
687 (gnus-cite-parse-maybe force)
688 (article-goto-body)
689 (let ((start (point))
690 (atts gnus-cite-attribution-alist)
691 (buffer-read-only nil)
692 (inhibit-point-motion-hooks t)
693 (hidden 0)
694 total)
695 (goto-char (point-max))
696 (gnus-article-search-signature)
697 (setq total (count-lines start (point)))
698 (while atts
699 (setq hidden (+ hidden (length (cdr (assoc (cdar atts)
700 gnus-cite-prefix-alist))))
701 atts (cdr atts)))
702 (when (or force
703 (and (> (* 100 hidden) (* gnus-cite-hide-percentage total))
704 (> hidden gnus-cite-hide-absolute)))
705 (gnus-add-wash-type 'cite)
706 (setq atts gnus-cite-attribution-alist)
707 (while atts
708 (setq total (cdr (assoc (cdar atts) gnus-cite-prefix-alist))
709 atts (cdr atts))
710 (while total
711 (setq hidden (car total)
712 total (cdr total))
713 (goto-char (point-min))
714 (forward-line (1- hidden))
715 (unless (assq hidden gnus-cite-attribution-alist)
716 (gnus-add-text-properties
717 (point) (progn (forward-line 1) (point))
718 (nconc (list 'article-type 'cite)
719 gnus-hidden-properties)))))))))
720 (gnus-set-mode-line 'article)))
722 (defun gnus-article-hide-citation-in-followups ()
723 "Hide cited text in non-root articles."
724 (interactive)
725 (save-excursion
726 (set-buffer gnus-article-buffer)
727 (let ((article (cdr gnus-article-current)))
728 (unless (save-excursion
729 (set-buffer gnus-summary-buffer)
730 (gnus-article-displayed-root-p article))
731 (gnus-article-hide-citation)))))
733 ;;; Internal functions:
735 (defun gnus-cite-parse-maybe (&optional force no-overlay)
736 "Always parse the buffer."
737 (gnus-cite-localize)
738 ;;Reset parser information.
739 (setq gnus-cite-prefix-alist nil
740 gnus-cite-attribution-alist nil
741 gnus-cite-loose-prefix-alist nil
742 gnus-cite-loose-attribution-alist nil)
743 (unless no-overlay
744 (gnus-cite-delete-overlays))
745 ;; Parse if not too large.
746 (if (and gnus-cite-parse-max-size
747 (> (buffer-size) gnus-cite-parse-max-size))
749 (setq gnus-cite-article (cons (car gnus-article-current)
750 (cdr gnus-article-current)))
751 (gnus-cite-parse-wrapper)))
753 (defun gnus-cite-delete-overlays ()
754 (dolist (overlay gnus-cite-overlay-list)
755 (ignore-errors
756 (when (or (not (gnus-overlay-end overlay))
757 (and (>= (gnus-overlay-end overlay) (point-min))
758 (<= (gnus-overlay-end overlay) (point-max))))
759 (setq gnus-cite-overlay-list (delete overlay gnus-cite-overlay-list))
760 (ignore-errors
761 (gnus-delete-overlay overlay))))))
763 (defun gnus-cite-parse-wrapper ()
764 ;; Wrap chopped gnus-cite-parse.
765 (article-goto-body)
766 (let ((inhibit-point-motion-hooks t))
767 (save-excursion
768 (gnus-cite-parse-attributions))
769 (save-excursion
770 (gnus-cite-parse))
771 (save-excursion
772 (gnus-cite-connect-attributions))))
774 (defun gnus-cite-parse ()
775 ;; Parse and connect citation prefixes and attribution lines.
777 ;; Parse current buffer searching for citation prefixes.
778 (let ((line (1+ (count-lines (point-min) (point))))
779 (case-fold-search t)
780 (max (save-excursion
781 (goto-char (point-max))
782 (gnus-article-search-signature)
783 (point)))
784 (prefix-regexp (concat "^\\(" message-cite-prefix-regexp "\\)"))
785 alist entry start begin end numbers prefix guess-limit)
786 ;; Get all potential prefixes in `alist'.
787 (while (< (point) max)
788 ;; Each line.
789 (setq begin (point)
790 guess-limit (progn (skip-chars-forward "^> \t\r\n") (point))
791 end (point-at-bol 2)
792 start end)
793 (goto-char begin)
794 ;; Ignore standard Supercite attribution prefix.
795 (when (and (< guess-limit (+ begin gnus-cite-max-prefix))
796 (looking-at gnus-supercite-regexp))
797 (if (match-end 1)
798 (setq end (1+ (match-end 1)))
799 (setq end (1+ begin))))
800 ;; Ignore very long prefixes.
801 (when (> end (+ begin gnus-cite-max-prefix))
802 (setq end (+ begin gnus-cite-max-prefix)))
803 ;; Ignore quoted envelope From_.
804 (when (and gnus-cite-ignore-quoted-from
805 (prog2
806 (setq case-fold-search nil)
807 (looking-at ">From ")
808 (setq case-fold-search t)))
809 (setq end (1+ begin)))
810 (while (re-search-forward prefix-regexp (1- end) t)
811 ;; Each prefix.
812 (setq end (match-end 0)
813 prefix (buffer-substring begin end))
814 (set-text-properties 0 (length prefix) nil prefix)
815 (setq entry (assoc prefix alist))
816 (if entry
817 (setcdr entry (cons line (cdr entry)))
818 (push (list prefix line) alist))
819 (goto-char begin))
820 (goto-char start)
821 (setq line (1+ line)))
822 ;; Horrible special case for some Microsoft mailers.
823 (goto-char (point-min))
824 (setq start t begin nil entry nil)
825 (while start
826 ;; Assume this search ends up at the beginning of a line.
827 (if (re-search-forward gnus-cite-unsightly-citation-regexp max t)
828 (progn
829 (when (number-or-marker-p start)
830 (setq begin (count-lines (point-min) start)
831 end (count-lines (point-min) (match-beginning 0))))
832 (setq start (match-end 0)))
833 (when (number-or-marker-p start)
834 (setq begin (count-lines (point-min) start)
835 end (count-lines (point-min) max)))
836 (setq start nil))
837 (when begin
838 (while (< begin end)
839 ;; Need to do 1+ because we're in the bol.
840 (push (setq begin (1+ begin)) entry))))
841 (when entry
842 (push (cons "" entry) alist))
843 ;; We got all the potential prefixes. Now create
844 ;; `gnus-cite-prefix-alist' containing the oldest prefix for each
845 ;; line that appears at least `gnus-cite-minimum-match-count'
846 ;; times. First sort them by length. Longer is older.
847 (setq alist (sort alist (lambda (a b)
848 (> (length (car a)) (length (car b))))))
849 (while alist
850 (setq entry (car alist)
851 prefix (car entry)
852 numbers (cdr entry)
853 alist (cdr alist))
854 (cond ((null numbers)
855 ;; No lines with this prefix that wasn't also part of
856 ;; a longer prefix.
858 ((< (length numbers) gnus-cite-minimum-match-count)
859 ;; Too few lines with this prefix. We keep it a bit
860 ;; longer in case it is an exact match for an attribution
861 ;; line, but we don't remove the line from other
862 ;; prefixes.
863 (push entry gnus-cite-prefix-alist))
865 (push entry
866 gnus-cite-prefix-alist)
867 ;; Remove articles from other prefixes.
868 (let ((loop alist)
869 current)
870 (while loop
871 (setq current (car loop)
872 loop (cdr loop))
873 (setcdr current
874 (gnus-set-difference (cdr current) numbers)))))))))
876 (defun gnus-cite-parse-attributions ()
877 (let (al-alist)
878 ;; Parse attributions
879 (while (re-search-forward gnus-cite-attribution-suffix (point-max) t)
880 (let* ((start (match-beginning 0))
881 (end (match-end 0))
882 (wrote (count-lines (point-min) end))
883 (prefix (gnus-cite-find-prefix wrote))
884 ;; Check previous line for an attribution leader.
885 (tag (progn
886 (beginning-of-line 1)
887 (when (looking-at gnus-supercite-secondary-regexp)
888 (buffer-substring (match-beginning 1)
889 (match-end 1)))))
890 (in (progn
891 (goto-char start)
892 (and (re-search-backward gnus-cite-attribution-prefix
893 (save-excursion
894 (beginning-of-line 0)
895 (point))
897 (not (re-search-forward gnus-cite-attribution-suffix
898 start t))
899 (count-lines (point-min) (1+ (point)))))))
900 (when (eq wrote in)
901 (setq in nil))
902 (goto-char end)
903 ;; don't add duplicates
904 (let ((al (buffer-substring (save-excursion (beginning-of-line 0)
905 (1+ (point)))
906 end)))
907 (when (not (assoc al al-alist))
908 (push (list wrote in prefix tag)
909 gnus-cite-loose-attribution-alist)
910 (push (cons al t) al-alist)))))))
912 (defun gnus-cite-connect-attributions ()
913 ;; Connect attributions to citations
915 ;; No citations have been connected to attribution lines yet.
916 (setq gnus-cite-loose-prefix-alist (append gnus-cite-prefix-alist nil))
918 ;; Parse current buffer searching for attribution lines.
919 ;; Find exact supercite citations.
920 (gnus-cite-match-attributions 'small nil
921 (lambda (prefix tag)
922 (when tag
923 (concat "\\`"
924 (regexp-quote prefix) "[ \t]*"
925 (regexp-quote tag) ">"))))
926 ;; Find loose supercite citations after attributions.
927 (gnus-cite-match-attributions 'small t
928 (lambda (prefix tag)
929 (when tag
930 (concat "\\<"
931 (regexp-quote tag)
932 "\\>"))))
933 ;; Find loose supercite citations anywhere.
934 (gnus-cite-match-attributions 'small nil
935 (lambda (prefix tag)
936 (when tag
937 (concat "\\<"
938 (regexp-quote tag)
939 "\\>"))))
940 ;; Find nested citations after attributions.
941 (gnus-cite-match-attributions 'small-if-unique t
942 (lambda (prefix tag)
943 (concat "\\`" (regexp-quote prefix) ".+")))
944 ;; Find nested citations anywhere.
945 (gnus-cite-match-attributions 'small nil
946 (lambda (prefix tag)
947 (concat "\\`" (regexp-quote prefix) ".+")))
948 ;; Remove loose prefixes with too few lines.
949 (let ((alist gnus-cite-loose-prefix-alist)
950 entry)
951 (while alist
952 (setq entry (car alist)
953 alist (cdr alist))
954 (when (< (length (cdr entry)) gnus-cite-minimum-match-count)
955 (setq gnus-cite-prefix-alist
956 (delq entry gnus-cite-prefix-alist)
957 gnus-cite-loose-prefix-alist
958 (delq entry gnus-cite-loose-prefix-alist)))))
959 ;; Find flat attributions.
960 (gnus-cite-match-attributions 'first t nil)
961 ;; Find any attributions (are we getting desperate yet?).
962 (gnus-cite-match-attributions 'first nil nil))
964 (defun gnus-cite-match-attributions (sort after fun)
965 ;; Match all loose attributions and citations (SORT AFTER FUN) .
967 ;; If SORT is `small', the citation with the shortest prefix will be
968 ;; used, if it is `first' the first prefix will be used, if it is
969 ;; `small-if-unique' the shortest prefix will be used if the
970 ;; attribution line does not share its own prefix with other
971 ;; loose attribution lines, otherwise the first prefix will be used.
973 ;; If AFTER is non-nil, only citations after the attribution line
974 ;; will be considered.
976 ;; If FUN is non-nil, it will be called with the arguments (WROTE
977 ;; PREFIX TAG) and expected to return a regular expression. Only
978 ;; citations whose prefix matches the regular expression will be
979 ;; considered.
981 ;; WROTE is the attribution line number.
982 ;; PREFIX is the attribution line prefix.
983 ;; TAG is the Supercite tag on the attribution line.
984 (let ((atts gnus-cite-loose-attribution-alist)
985 (case-fold-search t)
986 att wrote in prefix tag regexp limit smallest best size)
987 (while atts
988 (setq att (car atts)
989 atts (cdr atts)
990 wrote (nth 0 att)
991 in (nth 1 att)
992 prefix (nth 2 att)
993 tag (nth 3 att)
994 regexp (if fun (funcall fun prefix tag) "")
995 size (cond ((eq sort 'small) t)
996 ((eq sort 'first) nil)
997 (t (< (length (gnus-cite-find-loose prefix)) 2)))
998 limit (if after wrote -1)
999 smallest 1000000
1000 best nil)
1001 (let ((cites gnus-cite-loose-prefix-alist)
1002 cite candidate numbers first compare)
1003 (while cites
1004 (setq cite (car cites)
1005 cites (cdr cites)
1006 candidate (car cite)
1007 numbers (cdr cite)
1008 first (apply 'min numbers)
1009 compare (if size (length candidate) first))
1010 (and (> first limit)
1011 regexp
1012 (string-match regexp candidate)
1013 (< compare smallest)
1014 (setq best cite
1015 smallest compare))))
1016 (if (null best)
1018 (setq gnus-cite-loose-attribution-alist
1019 (delq att gnus-cite-loose-attribution-alist))
1020 (push (cons wrote (car best)) gnus-cite-attribution-alist)
1021 (when in
1022 (push (cons in (car best)) gnus-cite-attribution-alist))
1023 (when (memq best gnus-cite-loose-prefix-alist)
1024 (let ((loop gnus-cite-prefix-alist)
1025 (numbers (cdr best))
1026 current)
1027 (setq gnus-cite-loose-prefix-alist
1028 (delq best gnus-cite-loose-prefix-alist))
1029 (while loop
1030 (setq current (car loop)
1031 loop (cdr loop))
1032 (if (eq current best)
1034 (setcdr current (gnus-set-difference (cdr current) numbers))
1035 (when (null (cdr current))
1036 (setq gnus-cite-loose-prefix-alist
1037 (delq current gnus-cite-loose-prefix-alist)
1038 atts (delq current atts)))))))))))
1040 (defun gnus-cite-find-loose (prefix)
1041 ;; Return a list of loose attribution lines prefixed by PREFIX.
1042 (let* ((atts gnus-cite-loose-attribution-alist)
1043 att line lines)
1044 (while atts
1045 (setq att (car atts)
1046 line (car att)
1047 atts (cdr atts))
1048 (when (string-equal (gnus-cite-find-prefix line) prefix)
1049 (push line lines)))
1050 lines))
1052 (defun gnus-cite-add-face (number prefix face)
1053 ;; At line NUMBER, ignore PREFIX and add FACE to the rest of the line.
1054 (when face
1055 (let ((inhibit-point-motion-hooks t)
1056 from to overlay)
1057 (goto-char (point-min))
1058 (when (zerop (forward-line (1- number)))
1059 (forward-char (length prefix))
1060 (skip-chars-forward " \t")
1061 (setq from (point))
1062 (end-of-line 1)
1063 (skip-chars-backward " \t")
1064 (setq to (point))
1065 (when (< from to)
1066 (push (setq overlay (gnus-make-overlay from to))
1067 gnus-cite-overlay-list)
1068 (gnus-overlay-put overlay 'evaporate t)
1069 (gnus-overlay-put overlay 'face face))))))
1071 (defun gnus-cite-toggle (prefix)
1072 (save-excursion
1073 (set-buffer gnus-article-buffer)
1074 (gnus-cite-parse-maybe nil t)
1075 (let ((buffer-read-only nil)
1076 (numbers (cdr (assoc prefix gnus-cite-prefix-alist)))
1077 (inhibit-point-motion-hooks t)
1078 number)
1079 (while numbers
1080 (setq number (car numbers)
1081 numbers (cdr numbers))
1082 (goto-char (point-min))
1083 (forward-line (1- number))
1084 (cond ((get-text-property (point) 'invisible)
1085 ;; Can't remove 'cite from g-a-wash-types here because
1086 ;; multiple citations may be hidden -jas
1087 (remove-text-properties (point) (progn (forward-line 1) (point))
1088 gnus-hidden-properties))
1089 ((assq number gnus-cite-attribution-alist))
1091 (gnus-add-wash-type 'cite)
1092 (gnus-add-text-properties
1093 (point) (progn (forward-line 1) (point))
1094 (nconc (list 'article-type 'cite)
1095 gnus-hidden-properties))))
1096 (let ((gnus-article-mime-handle-alist-1
1097 gnus-article-mime-handle-alist))
1098 (gnus-set-mode-line 'article))))))
1100 (defun gnus-cite-find-prefix (line)
1101 ;; Return citation prefix for LINE.
1102 (let ((alist gnus-cite-prefix-alist)
1103 (prefix "")
1104 entry)
1105 (while alist
1106 (setq entry (car alist)
1107 alist (cdr alist))
1108 (when (memq line (cdr entry))
1109 (setq prefix (car entry))))
1110 prefix))
1112 (defun gnus-cite-localize ()
1113 "Make the citation variables local to the article buffer."
1114 (let ((vars '(gnus-cite-article
1115 gnus-cite-overlay-list gnus-cite-prefix-alist
1116 gnus-cite-attribution-alist gnus-cite-loose-prefix-alist
1117 gnus-cite-loose-attribution-alist)))
1118 (while vars
1119 (make-local-variable (pop vars)))))
1121 (defun gnus-cited-line-p ()
1122 "Say whether the current line is a cited line."
1123 (save-excursion
1124 (beginning-of-line)
1125 (let ((found nil))
1126 (dolist (prefix (mapcar 'car gnus-cite-prefix-alist))
1127 (when (string= (buffer-substring (point) (+ (length prefix) (point)))
1128 prefix)
1129 (setq found t)))
1130 found)))
1133 ;; Highlighting of different citation levels in message-mode.
1134 ;; - message-cite-prefix will be overridden if this is enabled.
1136 (defvar gnus-message-max-citation-depth
1137 (length gnus-cite-face-list)
1138 "Maximum supported level of citation.")
1140 (defvar gnus-message-cite-prefix-regexp
1141 (concat "^\\(?:" message-cite-prefix-regexp "\\)"))
1143 (defun gnus-message-search-citation-line (limit)
1144 "Search for a cited line and set match data accordingly.
1145 Returns nil if there is no such line before LIMIT, t otherwise."
1146 (when (re-search-forward gnus-message-cite-prefix-regexp limit t)
1147 (let ((cdepth (min (length (apply 'concat
1148 (split-string
1149 (match-string-no-properties 0)
1150 "[ \t [:alnum:]]+")))
1151 gnus-message-max-citation-depth))
1152 (mlist (make-list (* (1+ gnus-message-max-citation-depth) 2) nil))
1153 (start (point-at-bol))
1154 (end (point-at-eol)))
1155 (setcar mlist start)
1156 (setcar (cdr mlist) end)
1157 (setcar (nthcdr (* cdepth 2) mlist) start)
1158 (setcar (nthcdr (1+ (* cdepth 2)) mlist) end)
1159 (set-match-data mlist))
1162 (defvar gnus-message-citation-keywords
1163 ;; eval-when-compile ;; This breaks in XEmacs
1164 `((gnus-message-search-citation-line
1165 ,@(let ((list nil)
1166 (count 1))
1167 ;; (require 'gnus-cite)
1168 (dolist (face gnus-cite-face-list (nreverse list))
1169 (push (list count (list 'quote face) 'prepend t) list)
1170 (setq count (1+ count)))))) ;;
1171 "Keywords for highlighting different levels of message citations.")
1173 (defvar font-lock-defaults-computed)
1174 (defvar font-lock-keywords)
1175 (defvar font-lock-set-defaults)
1177 (eval-and-compile
1178 (unless (featurep 'xemacs)
1179 (autoload 'font-lock-set-defaults "font-lock")))
1181 (define-minor-mode gnus-message-citation-mode
1182 "Toggle `gnus-message-citation-mode' in current buffer.
1183 This buffer local minor mode provides additional font-lock support for
1184 nested citations.
1185 With prefix ARG, turn `gnus-message-citation-mode' on if and only if ARG
1186 is positive.
1187 Automatically turn `font-lock-mode' on when `gnus-message-citation-mode'
1188 is turned on."
1189 nil ;; init-value
1190 "" ;; lighter
1191 nil ;; keymap
1192 (when (eq major-mode 'message-mode)
1193 (let ((defaults (car (if (featurep 'xemacs)
1194 (get 'message-mode 'font-lock-defaults)
1195 font-lock-defaults)))
1196 default keywords)
1197 (while defaults
1198 (setq default (if (consp defaults)
1199 (pop defaults)
1200 (prog1
1201 defaults
1202 (setq defaults nil))))
1203 (if gnus-message-citation-mode
1204 ;; `gnus-message-citation-keywords' should be the last
1205 ;; elements of the keywords because the others are unlikely
1206 ;; to have the OVERRIDE flags -- XEmacs applies a keyword
1207 ;; having no OVERRIDE flag to matched text even if it has
1208 ;; already other faces, while Emacs doesn't.
1209 (set (make-local-variable default)
1210 (append (default-value default)
1211 gnus-message-citation-keywords))
1212 (kill-local-variable default))))
1213 ;; Force `font-lock-set-defaults' to update `font-lock-keywords'.
1214 (if (featurep 'xemacs)
1215 (progn
1216 (require 'font-lock)
1217 (setq font-lock-defaults-computed nil
1218 font-lock-keywords nil))
1219 (setq font-lock-set-defaults nil))
1220 (font-lock-set-defaults)
1221 (cond ((symbol-value 'font-lock-mode)
1222 (font-lock-fontify-buffer))
1223 (gnus-message-citation-mode
1224 (font-lock-mode 1)))))
1226 (defun turn-on-gnus-message-citation-mode ()
1227 "Turn on `gnus-message-citation-mode'."
1228 (gnus-message-citation-mode 1))
1229 (defun turn-off-gnus-message-citation-mode ()
1230 "Turn off `gnus-message-citation-mode'."
1231 (gnus-message-citation-mode -1))
1233 (gnus-ems-redefine)
1235 (provide 'gnus-cite)
1237 ;; Local Variables:
1238 ;; coding: iso-8859-1
1239 ;; End:
1241 ;;; arch-tag: 1997b044-6067-471e-8c8f-dc903093098a
1242 ;;; gnus-cite.el ends here