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 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 2, or (at your option)
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.
29 (eval-when-compile (require 'cl))
31 (when (featurep 'xemacs)
32 (require 'easy-mmode))) ; for `define-minor-mode'
37 (require 'message) ; for message-cite-prefix-regexp
41 (defgroup gnus-cite nil
44 :link '(custom-manual "(gnus)Article Highlighting")
47 (defcustom gnus-cited-opened-text-button-line-format "%(%{[-]%}%)\n"
48 "Format of opened cited text buttons."
52 (defcustom gnus-cited-closed-text-button-line-format "%(%{[+]%}%)\n"
53 "Format of closed cited text buttons."
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."
62 :type '(choice (const :tag "none" nil)
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."
70 :type '(choice (const :tag "all" nil)
73 (defcustom gnus-cite-max-prefix 20
74 "Maximum possible length for a citation prefix."
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."
86 (defcustom gnus-supercite-secondary-regexp "^.*\"\\([^\"\n]+\\)\" +=="
87 "Regexp matching mangled Supercite attribution lines.
88 The first regexp group should match the Supercite attribution."
92 (defcustom gnus-cite-minimum-match-count 2
93 "Minimum number of identical prefixes before we believe it's a citation."
97 ;; Some Microsoft products put in a citation that extends to the
98 ;; remainder of the message:
100 ;; -----Original Message-----
103 ;; Sent: ... [date, in non-RFC-2822 format]
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."
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."
127 (defcustom gnus-cite-unsightly-citation-regexp
128 "^-----Original Message-----\nFrom: \\(.+\n\\)+\n"
129 "Regexp matching Microsoft-type rest-of-message citations."
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."
142 (defface gnus-cite-attribution '((t (:italic t)))
143 "Face used for attribution lines."
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."
155 (defface gnus-cite-1 '((((class color)
157 (:foreground "light blue"))
160 (:foreground "MidnightBlue"))
165 ;; backward-compatibility alias
166 (put 'gnus-cite-face-1 'face-alias 'gnus-cite-1)
168 (defface gnus-cite-2 '((((class color)
170 (:foreground "light cyan"))
173 (:foreground "firebrick"))
178 ;; backward-compatibility alias
179 (put 'gnus-cite-face-2 'face-alias 'gnus-cite-2)
181 (defface gnus-cite-3 '((((class color)
183 (:foreground "light yellow"))
186 (:foreground "dark green"))
191 ;; backward-compatibility alias
192 (put 'gnus-cite-face-3 'face-alias 'gnus-cite-3)
194 (defface gnus-cite-4 '((((class color)
196 (:foreground "light pink"))
199 (:foreground "OrangeRed"))
204 ;; backward-compatibility alias
205 (put 'gnus-cite-face-4 'face-alias 'gnus-cite-4)
207 (defface gnus-cite-5 '((((class color)
209 (:foreground "pale green"))
212 (:foreground "dark khaki"))
217 ;; backward-compatibility alias
218 (put 'gnus-cite-face-5 'face-alias 'gnus-cite-5)
220 (defface gnus-cite-6 '((((class color)
222 (:foreground "beige"))
225 (:foreground "dark violet"))
230 ;; backward-compatibility alias
231 (put 'gnus-cite-face-6 'face-alias 'gnus-cite-6)
233 (defface gnus-cite-7 '((((class color)
235 (:foreground "orange"))
238 (:foreground "SteelBlue4"))
243 ;; backward-compatibility alias
244 (put 'gnus-cite-face-7 'face-alias 'gnus-cite-7)
246 (defface gnus-cite-8 '((((class color)
248 (:foreground "magenta"))
251 (:foreground "magenta"))
256 ;; backward-compatibility alias
257 (put 'gnus-cite-face-8 'face-alias 'gnus-cite-8)
259 (defface gnus-cite-9 '((((class color)
261 (:foreground "violet"))
264 (:foreground "violet"))
269 ;; backward-compatibility alias
270 (put 'gnus-cite-face-9 'face-alias 'gnus-cite-9)
272 (defface gnus-cite-10 '((((class color)
274 (:foreground "medium purple"))
277 (:foreground "medium purple"))
282 ;; backward-compatibility alias
283 (put 'gnus-cite-face-10 'face-alias 'gnus-cite-10)
285 (defface gnus-cite-11 '((((class color)
287 (:foreground "turquoise"))
290 (:foreground "turquoise"))
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."
308 :set (lambda (symbol value)
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
318 (dolist (face value (nreverse list))
319 (push (list count (list 'quote face) 'prepend t)
321 (setq count (1+ count)))))))))))
323 (defcustom gnus-cite-hide-percentage 50
324 "Only hide excess citation if above this percentage of the body."
328 (defcustom gnus-cite-hide-absolute 10
329 "Only hide excess citation if above this number of lines in the body."
333 (defcustom gnus-cite-blank-line-after-header t
334 "If non-nil, put a blank line between the citation header and the button."
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."
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)
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))
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.
411 (setq entry (car alist)
416 faces (or (cdr faces) gnus-cite-face-list)
417 face-alist (cons (cons prefix face) face-alist))
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)
427 (setq entry (car alist)
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
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)
447 (setq entry (car alist)
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."
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.
462 (setq numbers (pop alist)
463 prefix (pop numbers))
465 (setq number (pop numbers))
466 (goto-char (point-min))
467 (forward-line number)
468 (push (cons (point-marker) "") marks)
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.
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)
483 (setq marks (sort marks 'car-less-than-car))
484 (let ((omarks marks))
487 (if (= (caar omarks) (caadr omarks))
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) "")
496 (setq omarks (cdr omarks))))
497 (push (car omarks) marks))
498 (setq omarks (cdr omarks)))
500 (push (car omarks) marks))
501 (setq marks (setq m (nreverse marks)))
503 (if (and (equal (cdadr m) "")
504 (equal (cdar m) (cdaddr m))
505 (goto-char (caadr m))
507 (= (point) (caaddr m)))
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))
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)
523 (fill-column (if width (prefix-numeric-value width) fill-column)))
526 (narrow-to-region (caar marks) (caadr marks))
527 (let ((adaptive-fill-regexp
528 (concat "^" (regexp-quote (cdar marks)) " *"))
530 (if (string= (cdar marks) "") ""
531 (concat (cdar marks) " ")))
533 (fill-region (point-min) (point-max)))
534 (set-marker (caar marks) nil)
535 (setq marks (cdr 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,
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)
554 (set-buffer gnus-article-buffer)
555 (let ((buffer-read-only nil)
557 (inhibit-point-motion-hooks t)
558 (props (nconc (list 'article-type 'cite)
559 gnus-hidden-properties))
563 (text-property-any point (point-max)
565 'gnus-article-toggle-cited-text))
568 (gnus-article-toggle-cited-text
569 (get-text-property point 'gnus-data) arg)
571 (setq point (point)))
573 (setq marks (gnus-dissect-cited-text))
577 (while (and marks (string= (cdar marks) ""))
578 (setq marks (cdr marks)))
580 (setq beg (caar marks)))
581 (while (and marks (not (string= (cdar marks) "")))
582 (setq marks (cdr marks)))
584 (setq end (caar marks)))
585 ;; Skip past lines we want to leave visible.
586 (when (and beg end gnus-cited-lines-visible)
588 (forward-line (if (consp gnus-cited-lines-visible)
589 (car gnus-cited-lines-visible)
590 gnus-cited-lines-visible))
593 (setq beg (point-marker))
594 (when (consp gnus-cited-lines-visible)
596 (forward-line (- (cdr gnus-cited-lines-visible)))
599 (setq end (point-marker))))))
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)
608 (when (and gnus-cite-blank-line-after-header
609 (not (save-excursion (search-backward "\n\n" nil t))))
612 (setq start (point-marker))
614 (gnus-article-add-button
616 (progn (eval gnus-cited-closed-text-button-line-format-spec)
618 `gnus-article-toggle-cited-text
619 (list (cons beg end) start))
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))
633 (text-property-any beg (1- end) 'article-type 'cite))
634 (inhibit-point-motion-hooks t)
638 (and (> arg 0) (not hidden))
639 (and (< arg 0) hidden))
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))
661 (gnus-article-add-button
665 gnus-cited-opened-text-button-line-format-spec
666 gnus-cited-closed-text-button-line-format-spec))
668 `gnus-article-toggle-cited-text
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,
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)
687 (gnus-cite-parse-maybe force)
689 (let ((start (point))
690 (atts gnus-cite-attribution-alist)
691 (buffer-read-only nil)
692 (inhibit-point-motion-hooks t)
695 (goto-char (point-max))
696 (gnus-article-search-signature)
697 (setq total (count-lines start (point)))
699 (setq hidden (+ hidden (length (cdr (assoc (cdar atts)
700 gnus-cite-prefix-alist))))
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)
708 (setq total (cdr (assoc (cdar atts) gnus-cite-prefix-alist))
711 (setq hidden (car 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."
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."
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)
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)
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))
761 (gnus-delete-overlay overlay))))))
763 (defun gnus-cite-parse-wrapper ()
764 ;; Wrap chopped gnus-cite-parse.
766 (let ((inhibit-point-motion-hooks t))
768 (gnus-cite-parse-attributions))
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))))
781 (goto-char (point-max))
782 (gnus-article-search-signature)
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)
790 guess-limit (progn (skip-chars-forward "^> \t\r\n") (point))
794 ;; Ignore standard Supercite attribution prefix.
795 (when (and (< guess-limit (+ begin gnus-cite-max-prefix))
796 (looking-at gnus-supercite-regexp))
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
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)
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))
817 (setcdr entry (cons line (cdr entry)))
818 (push (list prefix line) alist))
821 (setq line (1+ line)))
822 ;; Horrible special case for some Microsoft mailers.
823 (goto-char (point-min))
824 (when (re-search-forward gnus-cite-unsightly-citation-regexp max t)
825 (setq begin (count-lines (point-min) (point)))
826 (setq end (count-lines (point-min) max))
830 (setq begin (1+ begin)))
831 (push (cons "" entry) alist))
832 ;; We got all the potential prefixes. Now create
833 ;; `gnus-cite-prefix-alist' containing the oldest prefix for each
834 ;; line that appears at least `gnus-cite-minimum-match-count'
835 ;; times. First sort them by length. Longer is older.
836 (setq alist (sort alist (lambda (a b)
837 (> (length (car a)) (length (car b))))))
839 (setq entry (car alist)
843 (cond ((null numbers)
844 ;; No lines with this prefix that wasn't also part of
847 ((< (length numbers) gnus-cite-minimum-match-count)
848 ;; Too few lines with this prefix. We keep it a bit
849 ;; longer in case it is an exact match for an attribution
850 ;; line, but we don't remove the line from other
852 (push entry gnus-cite-prefix-alist))
855 gnus-cite-prefix-alist)
856 ;; Remove articles from other prefixes.
860 (setq current (car loop)
863 (gnus-set-difference (cdr current) numbers)))))))))
865 (defun gnus-cite-parse-attributions ()
867 ;; Parse attributions
868 (while (re-search-forward gnus-cite-attribution-suffix (point-max) t)
869 (let* ((start (match-beginning 0))
871 (wrote (count-lines (point-min) end))
872 (prefix (gnus-cite-find-prefix wrote))
873 ;; Check previous line for an attribution leader.
875 (beginning-of-line 1)
876 (when (looking-at gnus-supercite-secondary-regexp)
877 (buffer-substring (match-beginning 1)
881 (and (re-search-backward gnus-cite-attribution-prefix
883 (beginning-of-line 0)
886 (not (re-search-forward gnus-cite-attribution-suffix
888 (count-lines (point-min) (1+ (point)))))))
892 ;; don't add duplicates
893 (let ((al (buffer-substring (save-excursion (beginning-of-line 0)
896 (when (not (assoc al al-alist))
897 (push (list wrote in prefix tag)
898 gnus-cite-loose-attribution-alist)
899 (push (cons al t) al-alist)))))))
901 (defun gnus-cite-connect-attributions ()
902 ;; Connect attributions to citations
904 ;; No citations have been connected to attribution lines yet.
905 (setq gnus-cite-loose-prefix-alist (append gnus-cite-prefix-alist nil))
907 ;; Parse current buffer searching for attribution lines.
908 ;; Find exact supercite citations.
909 (gnus-cite-match-attributions 'small nil
913 (regexp-quote prefix) "[ \t]*"
914 (regexp-quote tag) ">"))))
915 ;; Find loose supercite citations after attributions.
916 (gnus-cite-match-attributions 'small t
922 ;; Find loose supercite citations anywhere.
923 (gnus-cite-match-attributions 'small nil
929 ;; Find nested citations after attributions.
930 (gnus-cite-match-attributions 'small-if-unique t
932 (concat "\\`" (regexp-quote prefix) ".+")))
933 ;; Find nested citations anywhere.
934 (gnus-cite-match-attributions 'small nil
936 (concat "\\`" (regexp-quote prefix) ".+")))
937 ;; Remove loose prefixes with too few lines.
938 (let ((alist gnus-cite-loose-prefix-alist)
941 (setq entry (car alist)
943 (when (< (length (cdr entry)) gnus-cite-minimum-match-count)
944 (setq gnus-cite-prefix-alist
945 (delq entry gnus-cite-prefix-alist)
946 gnus-cite-loose-prefix-alist
947 (delq entry gnus-cite-loose-prefix-alist)))))
948 ;; Find flat attributions.
949 (gnus-cite-match-attributions 'first t nil)
950 ;; Find any attributions (are we getting desperate yet?).
951 (gnus-cite-match-attributions 'first nil nil))
953 (defun gnus-cite-match-attributions (sort after fun)
954 ;; Match all loose attributions and citations (SORT AFTER FUN) .
956 ;; If SORT is `small', the citation with the shortest prefix will be
957 ;; used, if it is `first' the first prefix will be used, if it is
958 ;; `small-if-unique' the shortest prefix will be used if the
959 ;; attribution line does not share its own prefix with other
960 ;; loose attribution lines, otherwise the first prefix will be used.
962 ;; If AFTER is non-nil, only citations after the attribution line
963 ;; will be considered.
965 ;; If FUN is non-nil, it will be called with the arguments (WROTE
966 ;; PREFIX TAG) and expected to return a regular expression. Only
967 ;; citations whose prefix matches the regular expression will be
970 ;; WROTE is the attribution line number.
971 ;; PREFIX is the attribution line prefix.
972 ;; TAG is the Supercite tag on the attribution line.
973 (let ((atts gnus-cite-loose-attribution-alist)
975 att wrote in prefix tag regexp limit smallest best size)
983 regexp (if fun (funcall fun prefix tag) "")
984 size (cond ((eq sort 'small) t)
985 ((eq sort 'first) nil)
986 (t (< (length (gnus-cite-find-loose prefix)) 2)))
987 limit (if after wrote -1)
990 (let ((cites gnus-cite-loose-prefix-alist)
991 cite candidate numbers first compare)
993 (setq cite (car cites)
997 first (apply 'min numbers)
998 compare (if size (length candidate) first))
1001 (string-match regexp candidate)
1002 (< compare smallest)
1004 smallest compare))))
1007 (setq gnus-cite-loose-attribution-alist
1008 (delq att gnus-cite-loose-attribution-alist))
1009 (push (cons wrote (car best)) gnus-cite-attribution-alist)
1011 (push (cons in (car best)) gnus-cite-attribution-alist))
1012 (when (memq best gnus-cite-loose-prefix-alist)
1013 (let ((loop gnus-cite-prefix-alist)
1014 (numbers (cdr best))
1016 (setq gnus-cite-loose-prefix-alist
1017 (delq best gnus-cite-loose-prefix-alist))
1019 (setq current (car loop)
1021 (if (eq current best)
1023 (setcdr current (gnus-set-difference (cdr current) numbers))
1024 (when (null (cdr current))
1025 (setq gnus-cite-loose-prefix-alist
1026 (delq current gnus-cite-loose-prefix-alist)
1027 atts (delq current atts)))))))))))
1029 (defun gnus-cite-find-loose (prefix)
1030 ;; Return a list of loose attribution lines prefixed by PREFIX.
1031 (let* ((atts gnus-cite-loose-attribution-alist)
1034 (setq att (car atts)
1037 (when (string-equal (gnus-cite-find-prefix line) prefix)
1041 (defun gnus-cite-add-face (number prefix face)
1042 ;; At line NUMBER, ignore PREFIX and add FACE to the rest of the line.
1044 (let ((inhibit-point-motion-hooks t)
1046 (goto-char (point-min))
1047 (when (zerop (forward-line (1- number)))
1048 (forward-char (length prefix))
1049 (skip-chars-forward " \t")
1052 (skip-chars-backward " \t")
1055 (push (setq overlay (gnus-make-overlay from to))
1056 gnus-cite-overlay-list)
1057 (gnus-overlay-put overlay 'evaporate t)
1058 (gnus-overlay-put overlay 'face face))))))
1060 (defun gnus-cite-toggle (prefix)
1062 (set-buffer gnus-article-buffer)
1063 (gnus-cite-parse-maybe nil t)
1064 (let ((buffer-read-only nil)
1065 (numbers (cdr (assoc prefix gnus-cite-prefix-alist)))
1066 (inhibit-point-motion-hooks t)
1069 (setq number (car numbers)
1070 numbers (cdr numbers))
1071 (goto-char (point-min))
1072 (forward-line (1- number))
1073 (cond ((get-text-property (point) 'invisible)
1074 ;; Can't remove 'cite from g-a-wash-types here because
1075 ;; multiple citations may be hidden -jas
1076 (remove-text-properties (point) (progn (forward-line 1) (point))
1077 gnus-hidden-properties))
1078 ((assq number gnus-cite-attribution-alist))
1080 (gnus-add-wash-type 'cite)
1081 (gnus-add-text-properties
1082 (point) (progn (forward-line 1) (point))
1083 (nconc (list 'article-type 'cite)
1084 gnus-hidden-properties))))
1085 (let ((gnus-article-mime-handle-alist-1
1086 gnus-article-mime-handle-alist))
1087 (gnus-set-mode-line 'article))))))
1089 (defun gnus-cite-find-prefix (line)
1090 ;; Return citation prefix for LINE.
1091 (let ((alist gnus-cite-prefix-alist)
1095 (setq entry (car alist)
1097 (when (memq line (cdr entry))
1098 (setq prefix (car entry))))
1101 (defun gnus-cite-localize ()
1102 "Make the citation variables local to the article buffer."
1103 (let ((vars '(gnus-cite-article
1104 gnus-cite-overlay-list gnus-cite-prefix-alist
1105 gnus-cite-attribution-alist gnus-cite-loose-prefix-alist
1106 gnus-cite-loose-attribution-alist)))
1108 (make-local-variable (pop vars)))))
1110 (defun gnus-cited-line-p ()
1111 "Say whether the current line is a cited line."
1115 (dolist (prefix (mapcar 'car gnus-cite-prefix-alist))
1116 (when (string= (buffer-substring (point) (+ (length prefix) (point)))
1122 ;; Highlighting of different citation levels in message-mode.
1123 ;; - message-cite-prefix will be overridden if this is enabled.
1125 (defvar gnus-message-max-citation-depth
1126 (length gnus-cite-face-list)
1127 "Maximum supported level of citation.")
1129 (defvar gnus-message-cite-prefix-regexp
1130 (concat "^\\(?:" message-cite-prefix-regexp "\\)"))
1132 (defun gnus-message-search-citation-line (limit)
1133 "Search for a cited line and set match data accordingly.
1134 Returns nil if there is no such line before LIMIT, t otherwise."
1135 (when (re-search-forward gnus-message-cite-prefix-regexp limit t)
1136 (let ((cdepth (min (length (apply 'concat
1138 (match-string-no-properties 0)
1139 "[ \t [:alnum:]]+")))
1140 gnus-message-max-citation-depth))
1141 (mlist (make-list (* (1+ gnus-message-max-citation-depth) 2) nil))
1142 (start (line-beginning-position))
1143 (end (line-end-position)))
1144 (setcar mlist start)
1145 (setcar (cdr mlist) end)
1146 (setcar (nthcdr (* cdepth 2) mlist) start)
1147 (setcar (nthcdr (1+ (* cdepth 2)) mlist) end)
1148 (set-match-data mlist))
1151 (defvar gnus-message-citation-keywords
1152 ;; eval-when-compile ;; This breaks in XEmacs
1153 `((gnus-message-search-citation-line
1156 ;; (require 'gnus-cite)
1157 (dolist (face gnus-cite-face-list (nreverse list))
1158 (push (list count (list 'quote face) 'prepend t) list)
1159 (setq count (1+ count)))))) ;;
1160 "Keywords for highlighting different levels of message citations.")
1163 (autoload 'font-lock-compile-keywords "font-lock")
1164 (defvar font-lock-keywords)
1165 (unless (fboundp 'font-lock-add-keywords)
1166 (autoload 'font-lock-add-keywords "font-lock"))
1167 (unless (fboundp 'font-lock-remove-keywords)
1168 (autoload 'font-lock-remove-keywords "font-lock")))
1170 (defun gnus-message-add-citation-keywords ()
1171 "Add font-lock for nested citations to current buffer."
1172 (if (fboundp 'font-lock-add-keywords)
1173 (font-lock-add-keywords nil gnus-message-citation-keywords 'append)
1174 (font-lock-set-defaults)
1175 (let ((was-compiled (eq (car font-lock-keywords) t)))
1176 (setq font-lock-keywords (copy-sequence (if was-compiled
1177 (cadr font-lock-keywords)
1178 font-lock-keywords)))
1179 (dolist (keyword gnus-message-citation-keywords)
1180 (setq font-lock-keywords (delete keyword font-lock-keywords)))
1181 (let ((old (if (eq (car-safe font-lock-keywords) t)
1182 (cdr font-lock-keywords)
1183 font-lock-keywords)))
1184 (setq font-lock-keywords (append old gnus-message-citation-keywords)))
1186 (setq font-lock-keywords
1187 (font-lock-compile-keywords font-lock-keywords))))))
1189 (defun gnus-message-remove-citation-keywords ()
1190 "Remove font-lock for nested citations from current buffer."
1191 (if (fboundp 'font-lock-remove-keywords)
1192 (font-lock-remove-keywords nil gnus-message-citation-keywords)
1193 (font-lock-set-defaults)
1194 (let ((was-compiled (eq (car font-lock-keywords) t)))
1196 (setq font-lock-keywords (cadr font-lock-keywords)))
1197 (setq font-lock-keywords (copy-sequence font-lock-keywords))
1198 (dolist (keyword gnus-message-citation-keywords)
1199 (setq font-lock-keywords (delete keyword font-lock-keywords)))
1201 (setq font-lock-keywords
1202 (font-lock-compile-keywords font-lock-keywords))))))
1204 (define-minor-mode gnus-message-citation-mode
1205 "Toggle `gnus-message-citation-mode' in current buffer.
1206 This buffer local minor mode provides additional font-lock support for
1208 With prefix ARG, turn `gnus-message-citation-mode' on if and only if ARG is
1213 (if gnus-message-citation-mode
1214 (gnus-message-add-citation-keywords)
1215 (gnus-message-remove-citation-keywords))
1216 (font-lock-fontify-buffer))
1218 (defun turn-on-gnus-message-citation-mode ()
1219 "Turn on `gnus-message-citation-mode'."
1220 (gnus-message-citation-mode 1))
1221 (defun turn-off-gnus-message-citation-mode ()
1222 "Turn off `gnus-message-citation-mode'."
1223 (gnus-message-citation-mode -1))
1227 (provide 'gnus-cite)
1230 ;; coding: iso-8859-1
1233 ;;; arch-tag: 1997b044-6067-471e-8c8f-dc903093098a
1234 ;;; gnus-cite.el ends here