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 of the License, or
13 ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
27 (eval-when-compile (require 'cl
))
29 (when (featurep 'xemacs
)
30 (require 'easy-mmode
))) ; for `define-minor-mode'
35 (require 'message
) ; for message-cite-prefix-regexp
39 (defgroup gnus-cite nil
42 :link
'(custom-manual "(gnus)Article Highlighting")
45 (defcustom gnus-cited-opened-text-button-line-format
"%(%{[-]%}%)\n"
46 "Format of opened cited text buttons."
50 (defcustom gnus-cited-closed-text-button-line-format
"%(%{[+]%}%)\n"
51 "Format of closed cited text buttons."
55 (defcustom gnus-cited-lines-visible nil
56 "The number of lines of hidden cited text to remain visible.
57 Or a pair (cons) of numbers which are the number of lines at the top
58 and bottom of the text, respectively, to remain visible."
60 :type
'(choice (const :tag
"none" nil
)
62 (cons :tag
"Top and Bottom" integer integer
)))
64 (defcustom gnus-cite-parse-max-size
25000
65 "Maximum article size (in bytes) where parsing citations is allowed.
66 Set it to nil to parse all articles."
68 :type
'(choice (const :tag
"all" nil
)
71 (defcustom gnus-cite-max-prefix
20
72 "Maximum possible length for a citation prefix."
76 (defcustom gnus-supercite-regexp
77 (concat "^\\(" message-cite-prefix-regexp
"\\)? *"
78 ">>>>> +\"\\([^\"\n]+\\)\" +==")
79 "*Regexp matching normal Supercite attribution lines.
80 The first grouping must match prefixes added by other packages."
84 (defcustom gnus-supercite-secondary-regexp
"^.*\"\\([^\"\n]+\\)\" +=="
85 "Regexp matching mangled Supercite attribution lines.
86 The first regexp group should match the Supercite attribution."
90 (defcustom gnus-cite-minimum-match-count
2
91 "Minimum number of identical prefixes before we believe it's a citation."
95 ;; Some Microsoft products put in a citation that extends to the
96 ;; remainder of the message:
98 ;; -----Original Message-----
101 ;; Sent: ... [date, in non-RFC-2822 format]
104 ;; Cited message, with no prefixes
106 ;; The four headers are always the same. But note they are prone to
107 ;; folding without additional indentation.
109 ;; Others use "----- Original Message -----" instead, and properly quote
110 ;; the body using "> ". This style is handled without special cases.
112 (defcustom gnus-cite-attribution-prefix
113 "In article\\|in <\\|On \\(Mon\\|Tue\\|Wed\\|Thu\\|Fri\\|Sat\\|Sun\\),\\|----- ?Original Message ?-----"
114 "*Regexp matching the beginning of an attribution line."
118 (defcustom gnus-cite-attribution-suffix
119 "\\(\\(wrote\\|writes\\|said\\|says\\|>\\)\\(:\\|\\.\\.\\.\\)\\|----- ?Original Message ?-----\\)[ \t]*$"
120 "*Regexp matching the end of an attribution line.
121 The text matching the first grouping will be used as a button."
125 (defcustom gnus-cite-unsightly-citation-regexp
126 "^-----Original Message-----\nFrom: \\(.+\n\\)+\n"
127 "Regexp matching Microsoft-type rest-of-message citations."
132 (defcustom gnus-cite-ignore-quoted-from t
133 "Non-nil means don't regard lines beginning with \">From \" as cited text.
134 Those lines may have been quoted by MTAs in order not to mix up with
135 the envelope From line."
140 (defface gnus-cite-attribution
'((t (:italic t
)))
141 "Face used for attribution lines."
143 ;; backward-compatibility alias
144 (put 'gnus-cite-attribution-face
'face-alias
'gnus-cite-attribution
)
146 (defcustom gnus-cite-attribution-face
'gnus-cite-attribution
147 "Face used for attribution lines.
148 It is merged with the face for the cited text belonging to the attribution."
153 (defface gnus-cite-1
'((((class color
)
155 (:foreground
"light blue"))
158 (:foreground
"MidnightBlue"))
163 ;; backward-compatibility alias
164 (put 'gnus-cite-face-1
'face-alias
'gnus-cite-1
)
166 (defface gnus-cite-2
'((((class color
)
168 (:foreground
"light cyan"))
171 (:foreground
"firebrick"))
176 ;; backward-compatibility alias
177 (put 'gnus-cite-face-2
'face-alias
'gnus-cite-2
)
179 (defface gnus-cite-3
'((((class color
)
181 (:foreground
"light yellow"))
184 (:foreground
"dark green"))
189 ;; backward-compatibility alias
190 (put 'gnus-cite-face-3
'face-alias
'gnus-cite-3
)
192 (defface gnus-cite-4
'((((class color
)
194 (:foreground
"light pink"))
197 (:foreground
"OrangeRed"))
202 ;; backward-compatibility alias
203 (put 'gnus-cite-face-4
'face-alias
'gnus-cite-4
)
205 (defface gnus-cite-5
'((((class color
)
207 (:foreground
"pale green"))
210 (:foreground
"dark khaki"))
215 ;; backward-compatibility alias
216 (put 'gnus-cite-face-5
'face-alias
'gnus-cite-5
)
218 (defface gnus-cite-6
'((((class color
)
220 (:foreground
"beige"))
223 (:foreground
"dark violet"))
228 ;; backward-compatibility alias
229 (put 'gnus-cite-face-6
'face-alias
'gnus-cite-6
)
231 (defface gnus-cite-7
'((((class color
)
233 (:foreground
"orange"))
236 (:foreground
"SteelBlue4"))
241 ;; backward-compatibility alias
242 (put 'gnus-cite-face-7
'face-alias
'gnus-cite-7
)
244 (defface gnus-cite-8
'((((class color
)
246 (:foreground
"magenta"))
249 (:foreground
"magenta"))
254 ;; backward-compatibility alias
255 (put 'gnus-cite-face-8
'face-alias
'gnus-cite-8
)
257 (defface gnus-cite-9
'((((class color
)
259 (:foreground
"violet"))
262 (:foreground
"violet"))
267 ;; backward-compatibility alias
268 (put 'gnus-cite-face-9
'face-alias
'gnus-cite-9
)
270 (defface gnus-cite-10
'((((class color
)
272 (:foreground
"plum1"))
275 (:foreground
"medium purple"))
280 ;; backward-compatibility alias
281 (put 'gnus-cite-face-10
'face-alias
'gnus-cite-10
)
283 (defface gnus-cite-11
'((((class color
)
285 (:foreground
"turquoise"))
288 (:foreground
"turquoise"))
293 ;; backward-compatibility alias
294 (put 'gnus-cite-face-11
'face-alias
'gnus-cite-11
)
296 (defcustom gnus-cite-face-list
297 '(gnus-cite-1 gnus-cite-2 gnus-cite-3 gnus-cite-4 gnus-cite-5 gnus-cite-6
298 gnus-cite-7 gnus-cite-8 gnus-cite-9 gnus-cite-10 gnus-cite-11
)
299 "*List of faces used for highlighting citations.
301 When there are citations from multiple articles in the same message,
302 Gnus will try to give each citation from each article its own face.
303 This should make it easier to see who wrote what."
306 :set
(lambda (symbol value
)
308 (custom-set-default symbol value
)
309 (if (boundp 'gnus-message-max-citation-depth
)
310 (setq gnus-message-max-citation-depth
(length value
)))
311 (if (boundp 'gnus-message-citation-keywords
)
312 (setq gnus-message-citation-keywords
313 `((gnus-message-search-citation-line
316 (dolist (face value
(nreverse list
))
317 (push (list count
(list 'quote face
) 'prepend t
)
319 (setq count
(1+ count
)))))))))))
321 (defcustom gnus-cite-hide-percentage
50
322 "Only hide excess citation if above this percentage of the body."
326 (defcustom gnus-cite-hide-absolute
10
327 "Only hide excess citation if above this number of lines in the body."
331 (defcustom gnus-cite-blank-line-after-header t
332 "If non-nil, put a blank line between the citation header and the button."
336 ;; This has to go here because its default value depends on
337 ;; gnus-cite-face-list.
338 (defcustom gnus-article-boring-faces
(cons 'gnus-signature gnus-cite-face-list
)
339 "List of faces that are not worth reading.
340 If an article has more pages below the one you are looking at, but
341 nothing on those pages is a word of at least three letters that is not
342 in a boring face, then the pages will be skipped."
344 :group
'gnus-article-hiding
)
346 ;;; Internal Variables:
348 (defvar gnus-cite-article nil
)
349 (defvar gnus-cite-overlay-list nil
)
351 (defvar gnus-cite-prefix-alist nil
)
352 ;; Alist of citation prefixes.
353 ;; The cdr is a list of lines with that prefix.
355 (defvar gnus-cite-attribution-alist nil
)
356 ;; Alist of attribution lines.
357 ;; The car is a line number.
358 ;; The cdr is the prefix for the citation started by that line.
360 (defvar gnus-cite-loose-prefix-alist nil
)
361 ;; Alist of citation prefixes that have no matching attribution.
362 ;; The cdr is a list of lines with that prefix.
364 (defvar gnus-cite-loose-attribution-alist nil
)
365 ;; Alist of attribution lines that have no matching citation.
366 ;; Each member has the form (WROTE IN PREFIX TAG), where
367 ;; WROTE: is the attribution line number
368 ;; IN: is the line number of the previous line if part of the same attribution,
369 ;; PREFIX: Is the citation prefix of the attribution line(s), and
370 ;; TAG: Is a Supercite tag, if any.
372 (defvar gnus-cited-opened-text-button-line-format-alist
373 `((?b
(marker-position beg
) ?d
)
374 (?e
(marker-position end
) ?d
)
375 (?n
(count-lines beg end
) ?d
)
376 (?l
(- end beg
) ?d
)))
377 (defvar gnus-cited-opened-text-button-line-format-spec nil
)
378 (defvar gnus-cited-closed-text-button-line-format-alist
379 gnus-cited-opened-text-button-line-format-alist
)
380 (defvar gnus-cited-closed-text-button-line-format-spec nil
)
385 (defun gnus-article-highlight-citation (&optional force same-buffer
)
386 "Highlight cited text.
387 Each citation in the article will be highlighted with a different face.
388 The faces are taken from `gnus-cite-face-list'.
389 Attribution lines are highlighted with the same face as the
390 corresponding citation merged with the face `gnus-cite-attribution'.
392 Text is considered cited if at least `gnus-cite-minimum-match-count'
393 lines matches `message-cite-prefix-regexp' with the same prefix.
395 Lines matching `gnus-cite-attribution-suffix' and perhaps
396 `gnus-cite-attribution-prefix' are considered attribution lines."
397 (interactive (list 'force
))
400 (set-buffer gnus-article-buffer
))
401 (gnus-cite-parse-maybe force
)
402 (let ((buffer-read-only nil
)
403 (alist gnus-cite-prefix-alist
)
404 (faces gnus-cite-face-list
)
405 (inhibit-point-motion-hooks t
)
406 face entry prefix skip numbers number face-alist
)
407 ;; Loop through citation prefixes.
409 (setq entry
(car alist
)
414 faces
(or (cdr faces
) gnus-cite-face-list
)
415 face-alist
(cons (cons prefix face
) face-alist
))
417 (setq number
(car numbers
)
418 numbers
(cdr numbers
))
419 (and (not (assq number gnus-cite-attribution-alist
))
420 (not (assq number gnus-cite-loose-attribution-alist
))
421 (gnus-cite-add-face number prefix face
))))
422 ;; Loop through attribution lines.
423 (setq alist gnus-cite-attribution-alist
)
425 (setq entry
(car alist
)
429 skip
(gnus-cite-find-prefix number
)
430 face
(cdr (assoc prefix face-alist
)))
431 ;; Add attribution button.
432 (goto-char (point-min))
433 (forward-line (1- number
))
434 (when (re-search-forward gnus-cite-attribution-suffix
437 (gnus-article-add-button (match-beginning 1) (match-end 1)
438 'gnus-cite-toggle prefix
))
439 ;; Highlight attribution line.
440 (gnus-cite-add-face number skip face
)
441 (gnus-cite-add-face number skip gnus-cite-attribution-face
))
442 ;; Loop through attribution lines.
443 (setq alist gnus-cite-loose-attribution-alist
)
445 (setq entry
(car alist
)
448 skip
(gnus-cite-find-prefix number
))
449 (gnus-cite-add-face number skip gnus-cite-attribution-face
)))))
451 (defun gnus-dissect-cited-text ()
452 "Dissect the article buffer looking for cited text."
454 (set-buffer gnus-article-buffer
)
455 (gnus-cite-parse-maybe nil t
)
456 (let ((alist gnus-cite-prefix-alist
)
457 prefix numbers number marks m
)
458 ;; Loop through citation prefixes.
460 (setq numbers
(pop alist
)
461 prefix
(pop numbers
))
463 (setq number
(pop numbers
))
464 (goto-char (point-min))
465 (forward-line number
)
466 (push (cons (point-marker) "") marks
)
468 (= (1- number
) (car numbers
)))
469 (setq number
(pop numbers
)))
470 (goto-char (point-min))
471 (forward-line (1- number
))
472 (push (cons (point-marker) prefix
) marks
)))
473 ;; Skip to the beginning of the body.
475 (push (cons (point-marker) "") marks
)
476 ;; Find the end of the body.
477 (goto-char (point-max))
478 (gnus-article-search-signature)
479 (push (cons (point-marker) "") marks
)
481 (setq marks
(sort marks
'car-less-than-car
))
482 (let ((omarks marks
))
485 (if (= (caar omarks
) (caadr omarks
))
487 (unless (equal (cdar omarks
) "")
488 (push (car omarks
) marks
))
489 (unless (equal (cdadr omarks
) "")
490 (push (cadr omarks
) marks
))
491 (unless (and (equal (cdar omarks
) "")
492 (equal (cdadr omarks
) "")
494 (setq omarks
(cdr omarks
))))
495 (push (car omarks
) marks
))
496 (setq omarks
(cdr omarks
)))
498 (push (car omarks
) marks
))
499 (setq marks
(setq m
(nreverse marks
)))
501 (if (and (equal (cdadr m
) "")
502 (equal (cdar m
) (cdaddr m
))
503 (goto-char (caadr m
))
505 (= (point) (caaddr m
)))
510 (defun gnus-article-fill-cited-article (&optional force width
)
511 "Do word wrapping in the current article.
512 If WIDTH (the numerical prefix), use that text width when filling."
513 (interactive (list t current-prefix-arg
))
515 (set-buffer gnus-article-buffer
)
516 (let ((buffer-read-only nil
)
517 (inhibit-point-motion-hooks t
)
518 (marks (gnus-dissect-cited-text))
519 (adaptive-fill-mode nil
)
521 (fill-column (if width
(prefix-numeric-value width
) fill-column
)))
524 (narrow-to-region (caar marks
) (caadr marks
))
525 (let ((adaptive-fill-regexp
526 (concat "^" (regexp-quote (cdar marks
)) " *"))
528 (if (string= (cdar marks
) "") ""
529 (concat (cdar marks
) " ")))
531 (fill-region (point-min) (point-max)))
532 (set-marker (caar marks
) nil
)
533 (setq marks
(cdr marks
)))
535 (set-marker (caar marks
) nil
))
536 ;; All this information is now incorrect.
537 (setq gnus-cite-prefix-alist nil
538 gnus-cite-attribution-alist nil
539 gnus-cite-loose-prefix-alist nil
540 gnus-cite-loose-attribution-alist nil
541 gnus-cite-article nil
)))))
543 (defun gnus-article-hide-citation (&optional arg force
)
544 "Toggle hiding of all cited text except attribution lines.
545 See the documentation for `gnus-article-highlight-citation'.
546 If given a negative prefix, always show; if given a positive prefix,
548 (interactive (append (gnus-article-hidden-arg) (list 'force
)))
549 (gnus-set-format 'cited-opened-text-button t
)
550 (gnus-set-format 'cited-closed-text-button t
)
552 (set-buffer gnus-article-buffer
)
553 (let ((buffer-read-only nil
)
555 (inhibit-point-motion-hooks t
)
556 (props (nconc (list 'article-type
'cite
)
557 gnus-hidden-properties
))
561 (text-property-any point
(point-max)
563 'gnus-article-toggle-cited-text
))
566 (gnus-article-toggle-cited-text
567 (get-text-property point
'gnus-data
) arg
)
569 (setq point
(point)))
571 (setq marks
(gnus-dissect-cited-text))
575 (while (and marks
(string= (cdar marks
) ""))
576 (setq marks
(cdr marks
)))
578 (setq beg
(caar marks
)))
579 (while (and marks
(not (string= (cdar marks
) "")))
580 (setq marks
(cdr marks
)))
582 (setq end
(caar marks
)))
583 ;; Skip past lines we want to leave visible.
584 (when (and beg end gnus-cited-lines-visible
)
586 (forward-line (if (consp gnus-cited-lines-visible
)
587 (car gnus-cited-lines-visible
)
588 gnus-cited-lines-visible
))
591 (setq beg
(point-marker))
592 (when (consp gnus-cited-lines-visible
)
594 (forward-line (- (cdr gnus-cited-lines-visible
)))
597 (setq end
(point-marker))))))
599 (gnus-add-wash-type 'cite
)
600 ;; We use markers for the end-points to facilitate later
601 ;; wrapping and mangling of text.
602 (setq beg
(set-marker (make-marker) beg
)
603 end
(set-marker (make-marker) end
))
604 (gnus-add-text-properties-when 'article-type nil beg end props
)
606 (when (and gnus-cite-blank-line-after-header
607 (not (save-excursion (search-backward "\n\n" nil t
))))
610 (setq start
(point-marker))
612 (gnus-article-add-button
614 (progn (eval gnus-cited-closed-text-button-line-format-spec
)
616 `gnus-article-toggle-cited-text
617 (list (cons beg end
) start
))
619 'article-type
'annotation
)
620 (set-marker beg
(point))))))))
622 (defun gnus-article-toggle-cited-text (args &optional arg
)
623 "Toggle hiding the text in REGION.
624 ARG can be nil or a number. Positive means hide, negative
625 means show, nil means toggle."
626 (let* ((region (car args
))
631 (text-property-any beg
(1- end
) 'article-type
'cite
))
632 (inhibit-point-motion-hooks t
)
636 (and (> arg
0) (not hidden
))
637 (and (< arg
0) hidden
))
640 ;; Can't remove 'cite from g-a-wash-types here because
641 ;; multiple citations may be hidden -jas
642 (gnus-remove-text-properties-when
643 'article-type
'cite beg end
644 (cons 'article-type
(cons 'cite
645 gnus-hidden-properties
))))
646 (gnus-add-wash-type 'cite
)
647 (gnus-add-text-properties-when
648 'article-type nil beg end
649 (cons 'article-type
(cons 'cite
650 gnus-hidden-properties
))))
651 (let ((gnus-article-mime-handle-alist-1 gnus-article-mime-handle-alist
))
652 (gnus-set-mode-line 'article
))
659 (gnus-article-add-button
663 gnus-cited-opened-text-button-line-format-spec
664 gnus-cited-closed-text-button-line-format-spec
))
666 `gnus-article-toggle-cited-text
669 'article-type
'annotation
)))))
671 (defun gnus-article-hide-citation-maybe (&optional arg force
)
672 "Toggle hiding of cited text that has an attribution line.
673 If given a negative prefix, always show; if given a positive prefix,
675 This will do nothing unless at least `gnus-cite-hide-percentage'
676 percent and at least `gnus-cite-hide-absolute' lines of the body is
677 cited text with attributions. When called interactively, these two
678 variables are ignored.
679 See also the documentation for `gnus-article-highlight-citation'."
680 (interactive (append (gnus-article-hidden-arg) '(force)))
681 (with-current-buffer gnus-article-buffer
682 (gnus-delete-wash-type 'cite
)
683 (unless (gnus-article-check-hidden-text 'cite arg
)
685 (gnus-cite-parse-maybe force
)
687 (let ((start (point))
688 (atts gnus-cite-attribution-alist
)
689 (buffer-read-only nil
)
690 (inhibit-point-motion-hooks t
)
693 (goto-char (point-max))
694 (gnus-article-search-signature)
695 (setq total
(count-lines start
(point)))
697 (setq hidden
(+ hidden
(length (cdr (assoc (cdar atts
)
698 gnus-cite-prefix-alist
))))
701 (and (> (* 100 hidden
) (* gnus-cite-hide-percentage total
))
702 (> hidden gnus-cite-hide-absolute
)))
703 (gnus-add-wash-type 'cite
)
704 (setq atts gnus-cite-attribution-alist
)
706 (setq total
(cdr (assoc (cdar atts
) gnus-cite-prefix-alist
))
709 (setq hidden
(car total
)
711 (goto-char (point-min))
712 (forward-line (1- hidden
))
713 (unless (assq hidden gnus-cite-attribution-alist
)
714 (gnus-add-text-properties
715 (point) (progn (forward-line 1) (point))
716 (nconc (list 'article-type
'cite
)
717 gnus-hidden-properties
)))))))))
718 (gnus-set-mode-line 'article
)))
720 (defun gnus-article-hide-citation-in-followups ()
721 "Hide cited text in non-root articles."
724 (set-buffer gnus-article-buffer
)
725 (let ((article (cdr gnus-article-current
)))
726 (unless (save-excursion
727 (set-buffer gnus-summary-buffer
)
728 (gnus-article-displayed-root-p article
))
729 (gnus-article-hide-citation)))))
731 ;;; Internal functions:
733 (defun gnus-cite-parse-maybe (&optional force no-overlay
)
734 "Always parse the buffer."
736 ;;Reset parser information.
737 (setq gnus-cite-prefix-alist nil
738 gnus-cite-attribution-alist nil
739 gnus-cite-loose-prefix-alist nil
740 gnus-cite-loose-attribution-alist nil
)
742 (gnus-cite-delete-overlays))
743 ;; Parse if not too large.
744 (if (and gnus-cite-parse-max-size
745 (> (buffer-size) gnus-cite-parse-max-size
))
747 (setq gnus-cite-article
(cons (car gnus-article-current
)
748 (cdr gnus-article-current
)))
749 (gnus-cite-parse-wrapper)))
751 (defun gnus-cite-delete-overlays ()
752 (dolist (overlay gnus-cite-overlay-list
)
754 (when (or (not (gnus-overlay-end overlay
))
755 (and (>= (gnus-overlay-end overlay
) (point-min))
756 (<= (gnus-overlay-end overlay
) (point-max))))
757 (setq gnus-cite-overlay-list
(delete overlay gnus-cite-overlay-list
))
759 (gnus-delete-overlay overlay
))))))
761 (defun gnus-cite-parse-wrapper ()
762 ;; Wrap chopped gnus-cite-parse.
764 (let ((inhibit-point-motion-hooks t
))
766 (gnus-cite-parse-attributions))
770 (gnus-cite-connect-attributions))))
772 (defun gnus-cite-parse ()
773 ;; Parse and connect citation prefixes and attribution lines.
775 ;; Parse current buffer searching for citation prefixes.
776 (let ((line (1+ (count-lines (point-min) (point))))
779 (goto-char (point-max))
780 (gnus-article-search-signature)
782 (prefix-regexp (concat "^\\(" message-cite-prefix-regexp
"\\)"))
783 alist entry start begin end numbers prefix guess-limit
)
784 ;; Get all potential prefixes in `alist'.
785 (while (< (point) max
)
788 guess-limit
(progn (skip-chars-forward "^> \t\r\n") (point))
792 ;; Ignore standard Supercite attribution prefix.
793 (when (and (< guess-limit
(+ begin gnus-cite-max-prefix
))
794 (looking-at gnus-supercite-regexp
))
796 (setq end
(1+ (match-end 1)))
797 (setq end
(1+ begin
))))
798 ;; Ignore very long prefixes.
799 (when (> end
(+ begin gnus-cite-max-prefix
))
800 (setq end
(+ begin gnus-cite-max-prefix
)))
801 ;; Ignore quoted envelope From_.
802 (when (and gnus-cite-ignore-quoted-from
804 (setq case-fold-search nil
)
805 (looking-at ">From ")
806 (setq case-fold-search t
)))
807 (setq end
(1+ begin
)))
808 (while (re-search-forward prefix-regexp
(1- end
) t
)
810 (setq end
(match-end 0)
811 prefix
(buffer-substring begin end
))
812 (set-text-properties 0 (length prefix
) nil prefix
)
813 (setq entry
(assoc prefix alist
))
815 (setcdr entry
(cons line
(cdr entry
)))
816 (push (list prefix line
) alist
))
819 (setq line
(1+ line
)))
820 ;; Horrible special case for some Microsoft mailers.
821 (goto-char (point-min))
822 (setq start t begin nil entry nil
)
824 ;; Assume this search ends up at the beginning of a line.
825 (if (re-search-forward gnus-cite-unsightly-citation-regexp max t
)
827 (when (number-or-marker-p start
)
828 (setq begin
(count-lines (point-min) start
)
829 end
(count-lines (point-min) (match-beginning 0))))
830 (setq start
(match-end 0)))
831 (when (number-or-marker-p start
)
832 (setq begin
(count-lines (point-min) start
)
833 end
(count-lines (point-min) max
)))
837 ;; Need to do 1+ because we're in the bol.
838 (push (setq begin
(1+ begin
)) entry
))))
840 (push (cons "" entry
) alist
))
841 ;; We got all the potential prefixes. Now create
842 ;; `gnus-cite-prefix-alist' containing the oldest prefix for each
843 ;; line that appears at least `gnus-cite-minimum-match-count'
844 ;; times. First sort them by length. Longer is older.
845 (setq alist
(sort alist
(lambda (a b
)
846 (> (length (car a
)) (length (car b
))))))
848 (setq entry
(car alist
)
852 (cond ((null numbers
)
853 ;; No lines with this prefix that wasn't also part of
856 ((< (length numbers
) gnus-cite-minimum-match-count
)
857 ;; Too few lines with this prefix. We keep it a bit
858 ;; longer in case it is an exact match for an attribution
859 ;; line, but we don't remove the line from other
861 (push entry gnus-cite-prefix-alist
))
864 gnus-cite-prefix-alist
)
865 ;; Remove articles from other prefixes.
869 (setq current
(car loop
)
872 (gnus-set-difference (cdr current
) numbers
)))))))))
874 (defun gnus-cite-parse-attributions ()
876 ;; Parse attributions
877 (while (re-search-forward gnus-cite-attribution-suffix
(point-max) t
)
878 (let* ((start (match-beginning 0))
880 (wrote (count-lines (point-min) end
))
881 (prefix (gnus-cite-find-prefix wrote
))
882 ;; Check previous line for an attribution leader.
884 (beginning-of-line 1)
885 (when (looking-at gnus-supercite-secondary-regexp
)
886 (buffer-substring (match-beginning 1)
890 (and (re-search-backward gnus-cite-attribution-prefix
892 (beginning-of-line 0)
895 (not (re-search-forward gnus-cite-attribution-suffix
897 (count-lines (point-min) (1+ (point)))))))
901 ;; don't add duplicates
902 (let ((al (buffer-substring (save-excursion (beginning-of-line 0)
905 (when (not (assoc al al-alist
))
906 (push (list wrote in prefix tag
)
907 gnus-cite-loose-attribution-alist
)
908 (push (cons al t
) al-alist
)))))))
910 (defun gnus-cite-connect-attributions ()
911 ;; Connect attributions to citations
913 ;; No citations have been connected to attribution lines yet.
914 (setq gnus-cite-loose-prefix-alist
(append gnus-cite-prefix-alist nil
))
916 ;; Parse current buffer searching for attribution lines.
917 ;; Find exact supercite citations.
918 (gnus-cite-match-attributions 'small nil
922 (regexp-quote prefix
) "[ \t]*"
923 (regexp-quote tag
) ">"))))
924 ;; Find loose supercite citations after attributions.
925 (gnus-cite-match-attributions 'small t
931 ;; Find loose supercite citations anywhere.
932 (gnus-cite-match-attributions 'small nil
938 ;; Find nested citations after attributions.
939 (gnus-cite-match-attributions 'small-if-unique t
941 (concat "\\`" (regexp-quote prefix
) ".+")))
942 ;; Find nested citations anywhere.
943 (gnus-cite-match-attributions 'small nil
945 (concat "\\`" (regexp-quote prefix
) ".+")))
946 ;; Remove loose prefixes with too few lines.
947 (let ((alist gnus-cite-loose-prefix-alist
)
950 (setq entry
(car alist
)
952 (when (< (length (cdr entry
)) gnus-cite-minimum-match-count
)
953 (setq gnus-cite-prefix-alist
954 (delq entry gnus-cite-prefix-alist
)
955 gnus-cite-loose-prefix-alist
956 (delq entry gnus-cite-loose-prefix-alist
)))))
957 ;; Find flat attributions.
958 (gnus-cite-match-attributions 'first t nil
)
959 ;; Find any attributions (are we getting desperate yet?).
960 (gnus-cite-match-attributions 'first nil nil
))
962 (defun gnus-cite-match-attributions (sort after fun
)
963 ;; Match all loose attributions and citations (SORT AFTER FUN) .
965 ;; If SORT is `small', the citation with the shortest prefix will be
966 ;; used, if it is `first' the first prefix will be used, if it is
967 ;; `small-if-unique' the shortest prefix will be used if the
968 ;; attribution line does not share its own prefix with other
969 ;; loose attribution lines, otherwise the first prefix will be used.
971 ;; If AFTER is non-nil, only citations after the attribution line
972 ;; will be considered.
974 ;; If FUN is non-nil, it will be called with the arguments (WROTE
975 ;; PREFIX TAG) and expected to return a regular expression. Only
976 ;; citations whose prefix matches the regular expression will be
979 ;; WROTE is the attribution line number.
980 ;; PREFIX is the attribution line prefix.
981 ;; TAG is the Supercite tag on the attribution line.
982 (let ((atts gnus-cite-loose-attribution-alist
)
984 att wrote in prefix tag regexp limit smallest best size
)
992 regexp
(if fun
(funcall fun prefix tag
) "")
993 size
(cond ((eq sort
'small
) t
)
994 ((eq sort
'first
) nil
)
995 (t (< (length (gnus-cite-find-loose prefix
)) 2)))
996 limit
(if after wrote -
1)
999 (let ((cites gnus-cite-loose-prefix-alist
)
1000 cite candidate numbers first compare
)
1002 (setq cite
(car cites
)
1004 candidate
(car cite
)
1006 first
(apply 'min numbers
)
1007 compare
(if size
(length candidate
) first
))
1008 (and (> first limit
)
1010 (string-match regexp candidate
)
1011 (< compare smallest
)
1013 smallest compare
))))
1016 (setq gnus-cite-loose-attribution-alist
1017 (delq att gnus-cite-loose-attribution-alist
))
1018 (push (cons wrote
(car best
)) gnus-cite-attribution-alist
)
1020 (push (cons in
(car best
)) gnus-cite-attribution-alist
))
1021 (when (memq best gnus-cite-loose-prefix-alist
)
1022 (let ((loop gnus-cite-prefix-alist
)
1023 (numbers (cdr best
))
1025 (setq gnus-cite-loose-prefix-alist
1026 (delq best gnus-cite-loose-prefix-alist
))
1028 (setq current
(car loop
)
1030 (if (eq current best
)
1032 (setcdr current
(gnus-set-difference (cdr current
) numbers
))
1033 (when (null (cdr current
))
1034 (setq gnus-cite-loose-prefix-alist
1035 (delq current gnus-cite-loose-prefix-alist
)
1036 atts
(delq current atts
)))))))))))
1038 (defun gnus-cite-find-loose (prefix)
1039 ;; Return a list of loose attribution lines prefixed by PREFIX.
1040 (let* ((atts gnus-cite-loose-attribution-alist
)
1043 (setq att
(car atts
)
1046 (when (string-equal (gnus-cite-find-prefix line
) prefix
)
1050 (defun gnus-cite-add-face (number prefix face
)
1051 ;; At line NUMBER, ignore PREFIX and add FACE to the rest of the line.
1053 (let ((inhibit-point-motion-hooks t
)
1055 (goto-char (point-min))
1056 (when (zerop (forward-line (1- number
)))
1057 (forward-char (length prefix
))
1058 (skip-chars-forward " \t")
1061 (skip-chars-backward " \t")
1064 (push (setq overlay
(gnus-make-overlay from to
))
1065 gnus-cite-overlay-list
)
1066 (gnus-overlay-put overlay
'evaporate t
)
1067 (gnus-overlay-put overlay
'face face
))))))
1069 (defun gnus-cite-toggle (prefix)
1071 (set-buffer gnus-article-buffer
)
1072 (gnus-cite-parse-maybe nil t
)
1073 (let ((buffer-read-only nil
)
1074 (numbers (cdr (assoc prefix gnus-cite-prefix-alist
)))
1075 (inhibit-point-motion-hooks t
)
1078 (setq number
(car numbers
)
1079 numbers
(cdr numbers
))
1080 (goto-char (point-min))
1081 (forward-line (1- number
))
1082 (cond ((get-text-property (point) 'invisible
)
1083 ;; Can't remove 'cite from g-a-wash-types here because
1084 ;; multiple citations may be hidden -jas
1085 (remove-text-properties (point) (progn (forward-line 1) (point))
1086 gnus-hidden-properties
))
1087 ((assq number gnus-cite-attribution-alist
))
1089 (gnus-add-wash-type 'cite
)
1090 (gnus-add-text-properties
1091 (point) (progn (forward-line 1) (point))
1092 (nconc (list 'article-type
'cite
)
1093 gnus-hidden-properties
))))
1094 (let ((gnus-article-mime-handle-alist-1
1095 gnus-article-mime-handle-alist
))
1096 (gnus-set-mode-line 'article
))))))
1098 (defun gnus-cite-find-prefix (line)
1099 ;; Return citation prefix for LINE.
1100 (let ((alist gnus-cite-prefix-alist
)
1104 (setq entry
(car alist
)
1106 (when (memq line
(cdr entry
))
1107 (setq prefix
(car entry
))))
1110 (defun gnus-cite-localize ()
1111 "Make the citation variables local to the article buffer."
1112 (let ((vars '(gnus-cite-article
1113 gnus-cite-overlay-list gnus-cite-prefix-alist
1114 gnus-cite-attribution-alist gnus-cite-loose-prefix-alist
1115 gnus-cite-loose-attribution-alist
)))
1117 (make-local-variable (pop vars
)))))
1119 (defun gnus-cited-line-p ()
1120 "Say whether the current line is a cited line."
1124 (dolist (prefix (mapcar 'car gnus-cite-prefix-alist
))
1125 (when (string= (buffer-substring (point) (+ (length prefix
) (point)))
1131 ;; Highlighting of different citation levels in message-mode.
1132 ;; - message-cite-prefix will be overridden if this is enabled.
1134 (defvar gnus-message-max-citation-depth
1135 (length gnus-cite-face-list
)
1136 "Maximum supported level of citation.")
1138 (defvar gnus-message-cite-prefix-regexp
1139 (concat "^\\(?:" message-cite-prefix-regexp
"\\)"))
1141 (defun gnus-message-search-citation-line (limit)
1142 "Search for a cited line and set match data accordingly.
1143 Returns nil if there is no such line before LIMIT, t otherwise."
1144 (when (re-search-forward gnus-message-cite-prefix-regexp limit t
)
1145 (let ((cdepth (min (length (apply 'concat
1147 (match-string-no-properties 0)
1148 "[ \t [:alnum:]]+")))
1149 gnus-message-max-citation-depth
))
1150 (mlist (make-list (* (1+ gnus-message-max-citation-depth
) 2) nil
))
1151 (start (point-at-bol))
1152 (end (point-at-eol)))
1153 (setcar mlist start
)
1154 (setcar (cdr mlist
) end
)
1155 (setcar (nthcdr (* cdepth
2) mlist
) start
)
1156 (setcar (nthcdr (1+ (* cdepth
2)) mlist
) end
)
1157 (set-match-data mlist
))
1160 (defvar gnus-message-citation-keywords
1161 ;; eval-when-compile ;; This breaks in XEmacs
1162 `((gnus-message-search-citation-line
1165 ;; (require 'gnus-cite)
1166 (dolist (face gnus-cite-face-list
(nreverse list
))
1167 (push (list count
(list 'quote face
) 'prepend t
) list
)
1168 (setq count
(1+ count
)))))) ;;
1169 "Keywords for highlighting different levels of message citations.")
1171 (defvar font-lock-defaults-computed
)
1172 (defvar font-lock-keywords
)
1173 (defvar font-lock-set-defaults
)
1176 (unless (featurep 'xemacs
)
1177 (autoload 'font-lock-set-defaults
"font-lock")))
1179 (define-minor-mode gnus-message-citation-mode
1180 "Toggle `gnus-message-citation-mode' in current buffer.
1181 This buffer local minor mode provides additional font-lock support for
1183 With prefix ARG, turn `gnus-message-citation-mode' on if and only if ARG
1185 Automatically turn `font-lock-mode' on when `gnus-message-citation-mode'
1190 (when (eq major-mode
'message-mode
)
1191 (let ((defaults (car (if (featurep 'xemacs
)
1192 (get 'message-mode
'font-lock-defaults
)
1193 font-lock-defaults
)))
1196 (setq default
(if (consp defaults
)
1200 (setq defaults nil
))))
1201 (if gnus-message-citation-mode
1202 ;; `gnus-message-citation-keywords' should be the last
1203 ;; elements of the keywords because the others are unlikely
1204 ;; to have the OVERRIDE flags -- XEmacs applies a keyword
1205 ;; having no OVERRIDE flag to matched text even if it has
1206 ;; already other faces, while Emacs doesn't.
1207 (set (make-local-variable default
)
1208 (append (default-value default
)
1209 gnus-message-citation-keywords
))
1210 (kill-local-variable default
))))
1211 ;; Force `font-lock-set-defaults' to update `font-lock-keywords'.
1212 (if (featurep 'xemacs
)
1214 (require 'font-lock
)
1215 (setq font-lock-defaults-computed nil
1216 font-lock-keywords nil
))
1217 (setq font-lock-set-defaults nil
))
1218 (font-lock-set-defaults)
1219 (cond ((symbol-value 'font-lock-mode
)
1220 (font-lock-fontify-buffer))
1221 (gnus-message-citation-mode
1222 (font-lock-mode 1)))))
1224 (defun turn-on-gnus-message-citation-mode ()
1225 "Turn on `gnus-message-citation-mode'."
1226 (gnus-message-citation-mode 1))
1227 (defun turn-off-gnus-message-citation-mode ()
1228 "Turn off `gnus-message-citation-mode'."
1229 (gnus-message-citation-mode -
1))
1233 (provide 'gnus-cite
)
1236 ;; coding: iso-8859-1
1239 ;; arch-tag: 1997b044-6067-471e-8c8f-dc903093098a
1240 ;;; gnus-cite.el ends here