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 3, 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
"plum1"))
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 (setq start t begin nil entry nil
)
826 ;; Assume this search ends up at the beginning of a line.
827 (if (re-search-forward gnus-cite-unsightly-citation-regexp max t
)
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
)))
839 ;; Need to do 1+ because we're in the bol.
840 (push (setq begin
(1+ begin
)) 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
))))))
850 (setq entry
(car alist
)
854 (cond ((null numbers
)
855 ;; No lines with this prefix that wasn't also part of
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
863 (push entry gnus-cite-prefix-alist
))
866 gnus-cite-prefix-alist
)
867 ;; Remove articles from other prefixes.
871 (setq current
(car loop
)
874 (gnus-set-difference (cdr current
) numbers
)))))))))
876 (defun gnus-cite-parse-attributions ()
878 ;; Parse attributions
879 (while (re-search-forward gnus-cite-attribution-suffix
(point-max) t
)
880 (let* ((start (match-beginning 0))
882 (wrote (count-lines (point-min) end
))
883 (prefix (gnus-cite-find-prefix wrote
))
884 ;; Check previous line for an attribution leader.
886 (beginning-of-line 1)
887 (when (looking-at gnus-supercite-secondary-regexp
)
888 (buffer-substring (match-beginning 1)
892 (and (re-search-backward gnus-cite-attribution-prefix
894 (beginning-of-line 0)
897 (not (re-search-forward gnus-cite-attribution-suffix
899 (count-lines (point-min) (1+ (point)))))))
903 ;; don't add duplicates
904 (let ((al (buffer-substring (save-excursion (beginning-of-line 0)
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
924 (regexp-quote prefix
) "[ \t]*"
925 (regexp-quote tag
) ">"))))
926 ;; Find loose supercite citations after attributions.
927 (gnus-cite-match-attributions 'small t
933 ;; Find loose supercite citations anywhere.
934 (gnus-cite-match-attributions 'small nil
940 ;; Find nested citations after attributions.
941 (gnus-cite-match-attributions 'small-if-unique t
943 (concat "\\`" (regexp-quote prefix
) ".+")))
944 ;; Find nested citations anywhere.
945 (gnus-cite-match-attributions 'small nil
947 (concat "\\`" (regexp-quote prefix
) ".+")))
948 ;; Remove loose prefixes with too few lines.
949 (let ((alist gnus-cite-loose-prefix-alist
)
952 (setq entry
(car 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
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
)
986 att wrote in prefix tag regexp limit smallest best size
)
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)
1001 (let ((cites gnus-cite-loose-prefix-alist
)
1002 cite candidate numbers first compare
)
1004 (setq cite
(car cites
)
1006 candidate
(car cite
)
1008 first
(apply 'min numbers
)
1009 compare
(if size
(length candidate
) first
))
1010 (and (> first limit
)
1012 (string-match regexp candidate
)
1013 (< compare smallest
)
1015 smallest compare
))))
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
)
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
))
1027 (setq gnus-cite-loose-prefix-alist
1028 (delq best gnus-cite-loose-prefix-alist
))
1030 (setq current
(car 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
)
1045 (setq att
(car atts
)
1048 (when (string-equal (gnus-cite-find-prefix line
) prefix
)
1052 (defun gnus-cite-add-face (number prefix face
)
1053 ;; At line NUMBER, ignore PREFIX and add FACE to the rest of the line.
1055 (let ((inhibit-point-motion-hooks t
)
1057 (goto-char (point-min))
1058 (when (zerop (forward-line (1- number
)))
1059 (forward-char (length prefix
))
1060 (skip-chars-forward " \t")
1063 (skip-chars-backward " \t")
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)
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
)
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
)
1106 (setq entry
(car alist
)
1108 (when (memq line
(cdr entry
))
1109 (setq prefix
(car entry
))))
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
)))
1119 (make-local-variable (pop vars
)))))
1121 (defun gnus-cited-line-p ()
1122 "Say whether the current line is a cited line."
1126 (dolist (prefix (mapcar 'car gnus-cite-prefix-alist
))
1127 (when (string= (buffer-substring (point) (+ (length prefix
) (point)))
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
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
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
)
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
1185 With prefix ARG, turn `gnus-message-citation-mode' on if and only if ARG
1187 Automatically turn `font-lock-mode' on when `gnus-message-citation-mode'
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
)))
1198 (setq default
(if (consp 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
)
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))
1235 (provide 'gnus-cite
)
1238 ;; coding: iso-8859-1
1241 ;;; arch-tag: 1997b044-6067-471e-8c8f-dc903093098a
1242 ;;; gnus-cite.el ends here