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, 2009, 2010, 2011 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
)
145 (put 'gnus-cite-attribution-face
'obsolete-face
"22.1")
147 (defcustom gnus-cite-attribution-face
'gnus-cite-attribution
148 "Face used for attribution lines.
149 It is merged with the face for the cited text belonging to the attribution."
154 (defface gnus-cite-1
'((((class color
)
156 (:foreground
"light blue"))
159 (:foreground
"MidnightBlue"))
164 ;; backward-compatibility alias
165 (put 'gnus-cite-face-1
'face-alias
'gnus-cite-1
)
166 (put 'gnus-cite-face-1
'obsolete-face
"22.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
)
180 (put 'gnus-cite-face-2
'obsolete-face
"22.1")
182 (defface gnus-cite-3
'((((class color
)
184 (:foreground
"light yellow"))
187 (:foreground
"dark green"))
192 ;; backward-compatibility alias
193 (put 'gnus-cite-face-3
'face-alias
'gnus-cite-3
)
194 (put 'gnus-cite-face-3
'obsolete-face
"22.1")
196 (defface gnus-cite-4
'((((class color
)
198 (:foreground
"light pink"))
201 (:foreground
"OrangeRed"))
206 ;; backward-compatibility alias
207 (put 'gnus-cite-face-4
'face-alias
'gnus-cite-4
)
208 (put 'gnus-cite-face-4
'obsolete-face
"22.1")
210 (defface gnus-cite-5
'((((class color
)
212 (:foreground
"pale green"))
215 (:foreground
"dark khaki"))
220 ;; backward-compatibility alias
221 (put 'gnus-cite-face-5
'face-alias
'gnus-cite-5
)
222 (put 'gnus-cite-face-5
'obsolete-face
"22.1")
224 (defface gnus-cite-6
'((((class color
)
226 (:foreground
"beige"))
229 (:foreground
"dark violet"))
234 ;; backward-compatibility alias
235 (put 'gnus-cite-face-6
'face-alias
'gnus-cite-6
)
236 (put 'gnus-cite-face-6
'obsolete-face
"22.1")
238 (defface gnus-cite-7
'((((class color
)
240 (:foreground
"orange"))
243 (:foreground
"SteelBlue4"))
248 ;; backward-compatibility alias
249 (put 'gnus-cite-face-7
'face-alias
'gnus-cite-7
)
250 (put 'gnus-cite-face-7
'obsolete-face
"22.1")
252 (defface gnus-cite-8
'((((class color
)
254 (:foreground
"magenta"))
257 (:foreground
"magenta"))
262 ;; backward-compatibility alias
263 (put 'gnus-cite-face-8
'face-alias
'gnus-cite-8
)
264 (put 'gnus-cite-face-8
'obsolete-face
"22.1")
266 (defface gnus-cite-9
'((((class color
)
268 (:foreground
"violet"))
271 (:foreground
"violet"))
276 ;; backward-compatibility alias
277 (put 'gnus-cite-face-9
'face-alias
'gnus-cite-9
)
278 (put 'gnus-cite-face-9
'obsolete-face
"22.1")
280 (defface gnus-cite-10
'((((class color
)
282 (:foreground
"plum1"))
285 (:foreground
"medium purple"))
290 ;; backward-compatibility alias
291 (put 'gnus-cite-face-10
'face-alias
'gnus-cite-10
)
292 (put 'gnus-cite-face-10
'obsolete-face
"22.1")
294 (defface gnus-cite-11
'((((class color
)
296 (:foreground
"turquoise"))
299 (:foreground
"turquoise"))
304 ;; backward-compatibility alias
305 (put 'gnus-cite-face-11
'face-alias
'gnus-cite-11
)
306 (put 'gnus-cite-face-11
'obsolete-face
"22.1")
308 (defcustom gnus-cite-face-list
309 '(gnus-cite-1 gnus-cite-2 gnus-cite-3 gnus-cite-4 gnus-cite-5 gnus-cite-6
310 gnus-cite-7 gnus-cite-8 gnus-cite-9 gnus-cite-10 gnus-cite-11
)
311 "*List of faces used for highlighting citations.
313 When there are citations from multiple articles in the same message,
314 Gnus will try to give each citation from each article its own face.
315 This should make it easier to see who wrote what."
318 :set
(lambda (symbol value
)
320 (custom-set-default symbol value
)
321 (if (boundp 'gnus-message-max-citation-depth
)
322 (setq gnus-message-max-citation-depth
(length value
)))
323 (if (boundp 'gnus-message-citation-keywords
)
324 (setq gnus-message-citation-keywords
325 `((gnus-message-search-citation-line
328 (dolist (face value
(nreverse list
))
329 (push (list count
(list 'quote face
) 'prepend t
)
331 (setq count
(1+ count
)))))))))))
333 (defcustom gnus-cite-hide-percentage
50
334 "Only hide excess citation if above this percentage of the body."
338 (defcustom gnus-cite-hide-absolute
10
339 "Only hide excess citation if above this number of lines in the body."
343 (defcustom gnus-cite-blank-line-after-header t
344 "If non-nil, put a blank line between the citation header and the button."
348 ;; This has to go here because its default value depends on
349 ;; gnus-cite-face-list.
350 (defcustom gnus-article-boring-faces
(cons 'gnus-signature gnus-cite-face-list
)
351 "List of faces that are not worth reading.
352 If an article has more pages below the one you are looking at, but
353 nothing on those pages is a word of at least three letters that is not
354 in a boring face, then the pages will be skipped."
356 :group
'gnus-article-hiding
)
358 ;;; Internal Variables:
360 (defvar gnus-cite-article nil
)
361 (defvar gnus-cite-overlay-list nil
)
363 (defvar gnus-cite-prefix-alist nil
)
364 ;; Alist of citation prefixes.
365 ;; The cdr is a list of lines with that prefix.
367 (defvar gnus-cite-attribution-alist nil
)
368 ;; Alist of attribution lines.
369 ;; The car is a line number.
370 ;; The cdr is the prefix for the citation started by that line.
372 (defvar gnus-cite-loose-prefix-alist nil
)
373 ;; Alist of citation prefixes that have no matching attribution.
374 ;; The cdr is a list of lines with that prefix.
376 (defvar gnus-cite-loose-attribution-alist nil
)
377 ;; Alist of attribution lines that have no matching citation.
378 ;; Each member has the form (WROTE IN PREFIX TAG), where
379 ;; WROTE: is the attribution line number
380 ;; IN: is the line number of the previous line if part of the same attribution,
381 ;; PREFIX: Is the citation prefix of the attribution line(s), and
382 ;; TAG: Is a Supercite tag, if any.
384 (defvar gnus-cited-opened-text-button-line-format-alist
385 `((?b
(marker-position beg
) ?d
)
386 (?e
(marker-position end
) ?d
)
387 (?n
(count-lines beg end
) ?d
)
388 (?l
(- end beg
) ?d
)))
389 (defvar gnus-cited-opened-text-button-line-format-spec nil
)
390 (defvar gnus-cited-closed-text-button-line-format-alist
391 gnus-cited-opened-text-button-line-format-alist
)
392 (defvar gnus-cited-closed-text-button-line-format-spec nil
)
397 (defun gnus-article-highlight-citation (&optional force same-buffer
)
398 "Highlight cited text.
399 Each citation in the article will be highlighted with a different face.
400 The faces are taken from `gnus-cite-face-list'.
401 Attribution lines are highlighted with the same face as the
402 corresponding citation merged with the face `gnus-cite-attribution'.
404 Text is considered cited if at least `gnus-cite-minimum-match-count'
405 lines matches `message-cite-prefix-regexp' with the same prefix.
407 Lines matching `gnus-cite-attribution-suffix' and perhaps
408 `gnus-cite-attribution-prefix' are considered attribution lines."
409 (interactive (list 'force
))
412 (set-buffer gnus-article-buffer
))
413 (gnus-cite-parse-maybe force
)
414 (let ((buffer-read-only nil
)
415 (alist gnus-cite-prefix-alist
)
416 (faces gnus-cite-face-list
)
417 (inhibit-point-motion-hooks t
)
418 face entry prefix skip numbers number face-alist
)
419 ;; Loop through citation prefixes.
421 (setq entry
(car alist
)
426 faces
(or (cdr faces
) gnus-cite-face-list
)
427 face-alist
(cons (cons prefix face
) face-alist
))
429 (setq number
(car numbers
)
430 numbers
(cdr numbers
))
431 (and (not (assq number gnus-cite-attribution-alist
))
432 (not (assq number gnus-cite-loose-attribution-alist
))
433 (gnus-cite-add-face number prefix face
))))
434 ;; Loop through attribution lines.
435 (setq alist gnus-cite-attribution-alist
)
437 (setq entry
(car alist
)
441 skip
(gnus-cite-find-prefix number
)
442 face
(cdr (assoc prefix face-alist
)))
443 ;; Add attribution button.
444 (goto-char (point-min))
445 (forward-line (1- number
))
446 (when (re-search-forward gnus-cite-attribution-suffix
449 (gnus-article-add-button (match-beginning 1) (match-end 1)
450 'gnus-cite-toggle prefix
))
451 ;; Highlight attribution line.
452 (gnus-cite-add-face number skip face
)
453 (gnus-cite-add-face number skip gnus-cite-attribution-face
))
454 ;; Loop through attribution lines.
455 (setq alist gnus-cite-loose-attribution-alist
)
457 (setq entry
(car alist
)
460 skip
(gnus-cite-find-prefix number
))
461 (gnus-cite-add-face number skip gnus-cite-attribution-face
)))))
463 (defun gnus-dissect-cited-text ()
464 "Dissect the article buffer looking for cited text."
466 (set-buffer gnus-article-buffer
)
467 (gnus-cite-parse-maybe nil t
)
468 (let ((alist gnus-cite-prefix-alist
)
469 prefix numbers number marks m
)
470 ;; Loop through citation prefixes.
472 (setq numbers
(pop alist
)
473 prefix
(pop numbers
))
475 (setq number
(pop numbers
))
476 (goto-char (point-min))
477 (forward-line number
)
478 (push (cons (point-marker) "") marks
)
480 (= (1- number
) (car numbers
)))
481 (setq number
(pop numbers
)))
482 (goto-char (point-min))
483 (forward-line (1- number
))
484 (push (cons (point-marker) prefix
) marks
)))
485 ;; Skip to the beginning of the body.
487 (push (cons (point-marker) "") marks
)
488 ;; Find the end of the body.
489 (goto-char (point-max))
490 (gnus-article-search-signature)
491 (push (cons (point-marker) "") marks
)
493 (setq marks
(sort marks
'car-less-than-car
))
494 (let ((omarks marks
))
497 (if (= (caar omarks
) (caadr omarks
))
499 (unless (equal (cdar omarks
) "")
500 (push (car omarks
) marks
))
501 (unless (equal (cdadr omarks
) "")
502 (push (cadr omarks
) marks
))
503 (unless (and (equal (cdar omarks
) "")
504 (equal (cdadr omarks
) "")
506 (setq omarks
(cdr omarks
))))
507 (push (car omarks
) marks
))
508 (setq omarks
(cdr omarks
)))
510 (push (car omarks
) marks
))
511 (setq marks
(setq m
(nreverse marks
)))
513 (if (and (equal (cdadr m
) "")
514 (equal (cdar m
) (cdaddr m
))
515 (goto-char (caadr m
))
517 (= (point) (caaddr m
)))
522 (defun gnus-article-fill-cited-article (&optional force width
)
523 "Do word wrapping in the current article.
524 If WIDTH (the numerical prefix), use that text width when filling."
525 (interactive (list t current-prefix-arg
))
527 (set-buffer gnus-article-buffer
)
528 (let ((buffer-read-only nil
)
529 (inhibit-point-motion-hooks t
)
530 (marks (gnus-dissect-cited-text))
531 (adaptive-fill-mode nil
)
533 (fill-column (if width
(prefix-numeric-value width
) fill-column
)))
536 (narrow-to-region (caar marks
) (caadr marks
))
537 (let ((adaptive-fill-regexp
538 (concat "^" (regexp-quote (cdar marks
)) " *"))
540 (if (string= (cdar marks
) "") ""
541 (concat (cdar marks
) " ")))
543 (fill-region (point-min) (point-max)))
544 (set-marker (caar marks
) nil
)
545 (setq marks
(cdr marks
)))
547 (set-marker (caar marks
) nil
))
548 ;; All this information is now incorrect.
549 (setq gnus-cite-prefix-alist nil
550 gnus-cite-attribution-alist nil
551 gnus-cite-loose-prefix-alist nil
552 gnus-cite-loose-attribution-alist nil
553 gnus-cite-article nil
)))))
555 (defun gnus-article-hide-citation (&optional arg force
)
556 "Toggle hiding of all cited text except attribution lines.
557 See the documentation for `gnus-article-highlight-citation'.
558 If given a negative prefix, always show; if given a positive prefix,
560 (interactive (append (gnus-article-hidden-arg) (list 'force
)))
561 (gnus-set-format 'cited-opened-text-button t
)
562 (gnus-set-format 'cited-closed-text-button t
)
564 (set-buffer gnus-article-buffer
)
565 (let ((buffer-read-only nil
)
567 (inhibit-point-motion-hooks t
)
568 (props (nconc (list 'article-type
'cite
)
569 gnus-hidden-properties
))
573 (text-property-any point
(point-max)
575 'gnus-article-toggle-cited-text
))
578 (gnus-article-toggle-cited-text
579 (get-text-property point
'gnus-data
) arg
)
581 (setq point
(point)))
583 (setq marks
(gnus-dissect-cited-text))
587 (while (and marks
(string= (cdar marks
) ""))
588 (setq marks
(cdr marks
)))
590 (setq beg
(caar marks
)))
591 (while (and marks
(not (string= (cdar marks
) "")))
592 (setq marks
(cdr marks
)))
594 (setq end
(caar marks
)))
595 ;; Skip past lines we want to leave visible.
596 (when (and beg end gnus-cited-lines-visible
)
598 (forward-line (if (consp gnus-cited-lines-visible
)
599 (car gnus-cited-lines-visible
)
600 gnus-cited-lines-visible
))
603 (setq beg
(point-marker))
604 (when (consp gnus-cited-lines-visible
)
606 (forward-line (- (cdr gnus-cited-lines-visible
)))
609 (setq end
(point-marker))))))
611 (gnus-add-wash-type 'cite
)
612 ;; We use markers for the end-points to facilitate later
613 ;; wrapping and mangling of text.
614 (setq beg
(set-marker (make-marker) beg
)
615 end
(set-marker (make-marker) end
))
616 (gnus-add-text-properties-when 'article-type nil beg end props
)
618 (when (and gnus-cite-blank-line-after-header
619 (not (save-excursion (search-backward "\n\n" nil t
))))
622 (setq start
(point-marker))
624 (gnus-article-add-button
626 (progn (eval gnus-cited-closed-text-button-line-format-spec
)
628 `gnus-article-toggle-cited-text
629 (list (cons beg end
) start
))
631 'article-type
'annotation
)
632 (set-marker beg
(point))))))))
634 (defun gnus-article-toggle-cited-text (args &optional arg
)
635 "Toggle hiding the text in REGION.
636 ARG can be nil or a number. Positive means hide, negative
637 means show, nil means toggle."
638 (let* ((region (car args
))
643 (text-property-any beg
(1- end
) 'article-type
'cite
))
644 (inhibit-point-motion-hooks t
)
648 (and (> arg
0) (not hidden
))
649 (and (< arg
0) hidden
))
652 ;; Can't remove 'cite from g-a-wash-types here because
653 ;; multiple citations may be hidden -jas
654 (gnus-remove-text-properties-when
655 'article-type
'cite beg end
656 (cons 'article-type
(cons 'cite
657 gnus-hidden-properties
))))
658 (gnus-add-wash-type 'cite
)
659 (gnus-add-text-properties-when
660 'article-type nil beg end
661 (cons 'article-type
(cons 'cite
662 gnus-hidden-properties
))))
663 (let ((gnus-article-mime-handle-alist-1 gnus-article-mime-handle-alist
))
664 (gnus-set-mode-line 'article
))
671 (gnus-article-add-button
675 gnus-cited-opened-text-button-line-format-spec
676 gnus-cited-closed-text-button-line-format-spec
))
678 `gnus-article-toggle-cited-text
681 'article-type
'annotation
)))))
683 (defun gnus-article-hide-citation-maybe (&optional arg force
)
684 "Toggle hiding of cited text that has an attribution line.
685 If given a negative prefix, always show; if given a positive prefix,
687 This will do nothing unless at least `gnus-cite-hide-percentage'
688 percent and at least `gnus-cite-hide-absolute' lines of the body is
689 cited text with attributions. When called interactively, these two
690 variables are ignored.
691 See also the documentation for `gnus-article-highlight-citation'."
692 (interactive (append (gnus-article-hidden-arg) '(force)))
693 (with-current-buffer gnus-article-buffer
694 (gnus-delete-wash-type 'cite
)
695 (unless (gnus-article-check-hidden-text 'cite arg
)
697 (gnus-cite-parse-maybe force
)
699 (let ((start (point))
700 (atts gnus-cite-attribution-alist
)
701 (buffer-read-only nil
)
702 (inhibit-point-motion-hooks t
)
705 (goto-char (point-max))
706 (gnus-article-search-signature)
707 (setq total
(count-lines start
(point)))
709 (setq hidden
(+ hidden
(length (cdr (assoc (cdar atts
)
710 gnus-cite-prefix-alist
))))
713 (and (> (* 100 hidden
) (* gnus-cite-hide-percentage total
))
714 (> hidden gnus-cite-hide-absolute
)))
715 (gnus-add-wash-type 'cite
)
716 (setq atts gnus-cite-attribution-alist
)
718 (setq total
(cdr (assoc (cdar atts
) gnus-cite-prefix-alist
))
721 (setq hidden
(car total
)
723 (goto-char (point-min))
724 (forward-line (1- hidden
))
725 (unless (assq hidden gnus-cite-attribution-alist
)
726 (gnus-add-text-properties
727 (point) (progn (forward-line 1) (point))
728 (nconc (list 'article-type
'cite
)
729 gnus-hidden-properties
)))))))))
730 (gnus-set-mode-line 'article
)))
732 (defun gnus-article-hide-citation-in-followups ()
733 "Hide cited text in non-root articles."
736 (set-buffer gnus-article-buffer
)
737 (let ((article (cdr gnus-article-current
)))
738 (unless (save-excursion
739 (set-buffer gnus-summary-buffer
)
740 (gnus-article-displayed-root-p article
))
741 (gnus-article-hide-citation)))))
743 ;;; Internal functions:
745 (defun gnus-cite-parse-maybe (&optional force no-overlay
)
746 "Always parse the buffer."
748 ;;Reset parser information.
749 (setq gnus-cite-prefix-alist nil
750 gnus-cite-attribution-alist nil
751 gnus-cite-loose-prefix-alist nil
752 gnus-cite-loose-attribution-alist nil
)
754 (gnus-cite-delete-overlays))
755 ;; Parse if not too large.
756 (if (and gnus-cite-parse-max-size
757 (> (buffer-size) gnus-cite-parse-max-size
))
759 (setq gnus-cite-article
(cons (car gnus-article-current
)
760 (cdr gnus-article-current
)))
761 (gnus-cite-parse-wrapper)))
763 (defun gnus-cite-delete-overlays ()
764 (dolist (overlay gnus-cite-overlay-list
)
766 (when (or (not (gnus-overlay-end overlay
))
767 (and (>= (gnus-overlay-end overlay
) (point-min))
768 (<= (gnus-overlay-end overlay
) (point-max))))
769 (setq gnus-cite-overlay-list
(delete overlay gnus-cite-overlay-list
))
771 (gnus-delete-overlay overlay
))))))
773 (defun gnus-cite-parse-wrapper ()
774 ;; Wrap chopped gnus-cite-parse.
776 (let ((inhibit-point-motion-hooks t
))
778 (gnus-cite-parse-attributions))
782 (gnus-cite-connect-attributions))))
784 (defun gnus-cite-parse ()
785 ;; Parse and connect citation prefixes and attribution lines.
787 ;; Parse current buffer searching for citation prefixes.
788 (let ((line (1+ (count-lines (point-min) (point))))
791 (goto-char (point-max))
792 (gnus-article-search-signature)
794 (prefix-regexp (concat "^\\(" message-cite-prefix-regexp
"\\)"))
795 alist entry start begin end numbers prefix guess-limit
)
796 ;; Get all potential prefixes in `alist'.
797 (while (< (point) max
)
800 guess-limit
(progn (skip-chars-forward "^> \t\r\n") (point))
804 ;; Ignore standard Supercite attribution prefix.
805 (when (and (< guess-limit
(+ begin gnus-cite-max-prefix
))
806 (looking-at gnus-supercite-regexp
))
808 (setq end
(1+ (match-end 1)))
809 (setq end
(1+ begin
))))
810 ;; Ignore very long prefixes.
811 (when (> end
(+ begin gnus-cite-max-prefix
))
812 (setq end
(+ begin gnus-cite-max-prefix
)))
813 ;; Ignore quoted envelope From_.
814 (when (and gnus-cite-ignore-quoted-from
816 (setq case-fold-search nil
)
817 (looking-at ">From ")
818 (setq case-fold-search t
)))
819 (setq end
(1+ begin
)))
820 (while (re-search-forward prefix-regexp
(1- end
) t
)
822 (setq end
(match-end 0)
823 prefix
(buffer-substring begin end
))
824 (set-text-properties 0 (length prefix
) nil prefix
)
825 (setq entry
(assoc prefix alist
))
827 (setcdr entry
(cons line
(cdr entry
)))
828 (push (list prefix line
) alist
))
831 (setq line
(1+ line
)))
832 ;; Horrible special case for some Microsoft mailers.
833 (goto-char (point-min))
834 (setq start t begin nil entry nil
)
836 ;; Assume this search ends up at the beginning of a line.
837 (if (re-search-forward gnus-cite-unsightly-citation-regexp max t
)
839 (when (number-or-marker-p start
)
840 (setq begin
(count-lines (point-min) start
)
841 end
(count-lines (point-min) (match-beginning 0))))
842 (setq start
(match-end 0)))
843 (when (number-or-marker-p start
)
844 (setq begin
(count-lines (point-min) start
)
845 end
(count-lines (point-min) max
)))
849 ;; Need to do 1+ because we're in the bol.
850 (push (setq begin
(1+ begin
)) entry
))))
852 (push (cons "" entry
) alist
))
853 ;; We got all the potential prefixes. Now create
854 ;; `gnus-cite-prefix-alist' containing the oldest prefix for each
855 ;; line that appears at least `gnus-cite-minimum-match-count'
856 ;; times. First sort them by length. Longer is older.
857 (setq alist
(sort alist
(lambda (a b
)
858 (> (length (car a
)) (length (car b
))))))
860 (setq entry
(car alist
)
864 (cond ((null numbers
)
865 ;; No lines with this prefix that wasn't also part of
868 ((< (length numbers
) gnus-cite-minimum-match-count
)
869 ;; Too few lines with this prefix. We keep it a bit
870 ;; longer in case it is an exact match for an attribution
871 ;; line, but we don't remove the line from other
873 (push entry gnus-cite-prefix-alist
))
876 gnus-cite-prefix-alist
)
877 ;; Remove articles from other prefixes.
881 (setq current
(car loop
)
884 (gnus-set-difference (cdr current
) numbers
)))))))))
886 (defun gnus-cite-parse-attributions ()
888 ;; Parse attributions
889 (while (re-search-forward gnus-cite-attribution-suffix
(point-max) t
)
890 (let* ((start (match-beginning 0))
892 (wrote (count-lines (point-min) end
))
893 (prefix (gnus-cite-find-prefix wrote
))
894 ;; Check previous line for an attribution leader.
896 (beginning-of-line 1)
897 (when (looking-at gnus-supercite-secondary-regexp
)
898 (buffer-substring (match-beginning 1)
902 (and (re-search-backward gnus-cite-attribution-prefix
904 (beginning-of-line 0)
907 (not (re-search-forward gnus-cite-attribution-suffix
909 (count-lines (point-min) (1+ (point)))))))
913 ;; don't add duplicates
914 (let ((al (buffer-substring (save-excursion (beginning-of-line 0)
917 (when (not (assoc al al-alist
))
918 (push (list wrote in prefix tag
)
919 gnus-cite-loose-attribution-alist
)
920 (push (cons al t
) al-alist
)))))))
922 (defun gnus-cite-connect-attributions ()
923 ;; Connect attributions to citations
925 ;; No citations have been connected to attribution lines yet.
926 (setq gnus-cite-loose-prefix-alist
(append gnus-cite-prefix-alist nil
))
928 ;; Parse current buffer searching for attribution lines.
929 ;; Find exact supercite citations.
930 (gnus-cite-match-attributions 'small nil
934 (regexp-quote prefix
) "[ \t]*"
935 (regexp-quote tag
) ">"))))
936 ;; Find loose supercite citations after attributions.
937 (gnus-cite-match-attributions 'small t
943 ;; Find loose supercite citations anywhere.
944 (gnus-cite-match-attributions 'small nil
950 ;; Find nested citations after attributions.
951 (gnus-cite-match-attributions 'small-if-unique t
953 (concat "\\`" (regexp-quote prefix
) ".+")))
954 ;; Find nested citations anywhere.
955 (gnus-cite-match-attributions 'small nil
957 (concat "\\`" (regexp-quote prefix
) ".+")))
958 ;; Remove loose prefixes with too few lines.
959 (let ((alist gnus-cite-loose-prefix-alist
)
962 (setq entry
(car alist
)
964 (when (< (length (cdr entry
)) gnus-cite-minimum-match-count
)
965 (setq gnus-cite-prefix-alist
966 (delq entry gnus-cite-prefix-alist
)
967 gnus-cite-loose-prefix-alist
968 (delq entry gnus-cite-loose-prefix-alist
)))))
969 ;; Find flat attributions.
970 (gnus-cite-match-attributions 'first t nil
)
971 ;; Find any attributions (are we getting desperate yet?).
972 (gnus-cite-match-attributions 'first nil nil
))
974 (defun gnus-cite-match-attributions (sort after fun
)
975 ;; Match all loose attributions and citations (SORT AFTER FUN) .
977 ;; If SORT is `small', the citation with the shortest prefix will be
978 ;; used, if it is `first' the first prefix will be used, if it is
979 ;; `small-if-unique' the shortest prefix will be used if the
980 ;; attribution line does not share its own prefix with other
981 ;; loose attribution lines, otherwise the first prefix will be used.
983 ;; If AFTER is non-nil, only citations after the attribution line
984 ;; will be considered.
986 ;; If FUN is non-nil, it will be called with the arguments (WROTE
987 ;; PREFIX TAG) and expected to return a regular expression. Only
988 ;; citations whose prefix matches the regular expression will be
991 ;; WROTE is the attribution line number.
992 ;; PREFIX is the attribution line prefix.
993 ;; TAG is the Supercite tag on the attribution line.
994 (let ((atts gnus-cite-loose-attribution-alist
)
996 att wrote in prefix tag regexp limit smallest best size
)
1004 regexp
(if fun
(funcall fun prefix tag
) "")
1005 size
(cond ((eq sort
'small
) t
)
1006 ((eq sort
'first
) nil
)
1007 (t (< (length (gnus-cite-find-loose prefix
)) 2)))
1008 limit
(if after wrote -
1)
1011 (let ((cites gnus-cite-loose-prefix-alist
)
1012 cite candidate numbers first compare
)
1014 (setq cite
(car cites
)
1016 candidate
(car cite
)
1018 first
(apply 'min numbers
)
1019 compare
(if size
(length candidate
) first
))
1020 (and (> first limit
)
1022 (string-match regexp candidate
)
1023 (< compare smallest
)
1025 smallest compare
))))
1028 (setq gnus-cite-loose-attribution-alist
1029 (delq att gnus-cite-loose-attribution-alist
))
1030 (push (cons wrote
(car best
)) gnus-cite-attribution-alist
)
1032 (push (cons in
(car best
)) gnus-cite-attribution-alist
))
1033 (when (memq best gnus-cite-loose-prefix-alist
)
1034 (let ((loop gnus-cite-prefix-alist
)
1035 (numbers (cdr best
))
1037 (setq gnus-cite-loose-prefix-alist
1038 (delq best gnus-cite-loose-prefix-alist
))
1040 (setq current
(car loop
)
1042 (if (eq current best
)
1044 (setcdr current
(gnus-set-difference (cdr current
) numbers
))
1045 (when (null (cdr current
))
1046 (setq gnus-cite-loose-prefix-alist
1047 (delq current gnus-cite-loose-prefix-alist
)
1048 atts
(delq current atts
)))))))))))
1050 (defun gnus-cite-find-loose (prefix)
1051 ;; Return a list of loose attribution lines prefixed by PREFIX.
1052 (let* ((atts gnus-cite-loose-attribution-alist
)
1055 (setq att
(car atts
)
1058 (when (string-equal (gnus-cite-find-prefix line
) prefix
)
1062 (defun gnus-cite-add-face (number prefix face
)
1063 ;; At line NUMBER, ignore PREFIX and add FACE to the rest of the line.
1065 (let ((inhibit-point-motion-hooks t
)
1067 (goto-char (point-min))
1068 (when (zerop (forward-line (1- number
)))
1069 (forward-char (length prefix
))
1070 (skip-chars-forward " \t")
1073 (skip-chars-backward " \t")
1076 (push (setq overlay
(gnus-make-overlay from to
))
1077 gnus-cite-overlay-list
)
1078 (gnus-overlay-put overlay
'evaporate t
)
1079 (gnus-overlay-put overlay
'face face
))))))
1081 (defun gnus-cite-toggle (prefix)
1083 (set-buffer gnus-article-buffer
)
1084 (gnus-cite-parse-maybe nil t
)
1085 (let ((buffer-read-only nil
)
1086 (numbers (cdr (assoc prefix gnus-cite-prefix-alist
)))
1087 (inhibit-point-motion-hooks t
)
1090 (setq number
(car numbers
)
1091 numbers
(cdr numbers
))
1092 (goto-char (point-min))
1093 (forward-line (1- number
))
1094 (cond ((get-text-property (point) 'invisible
)
1095 ;; Can't remove 'cite from g-a-wash-types here because
1096 ;; multiple citations may be hidden -jas
1097 (remove-text-properties (point) (progn (forward-line 1) (point))
1098 gnus-hidden-properties
))
1099 ((assq number gnus-cite-attribution-alist
))
1101 (gnus-add-wash-type 'cite
)
1102 (gnus-add-text-properties
1103 (point) (progn (forward-line 1) (point))
1104 (nconc (list 'article-type
'cite
)
1105 gnus-hidden-properties
))))
1106 (let ((gnus-article-mime-handle-alist-1
1107 gnus-article-mime-handle-alist
))
1108 (gnus-set-mode-line 'article
))))))
1110 (defun gnus-cite-find-prefix (line)
1111 ;; Return citation prefix for LINE.
1112 (let ((alist gnus-cite-prefix-alist
)
1116 (setq entry
(car alist
)
1118 (when (memq line
(cdr entry
))
1119 (setq prefix
(car entry
))))
1122 (defun gnus-cite-localize ()
1123 "Make the citation variables local to the article buffer."
1124 (let ((vars '(gnus-cite-article
1125 gnus-cite-overlay-list gnus-cite-prefix-alist
1126 gnus-cite-attribution-alist gnus-cite-loose-prefix-alist
1127 gnus-cite-loose-attribution-alist
)))
1129 (make-local-variable (pop vars
)))))
1131 (defun gnus-cited-line-p ()
1132 "Say whether the current line is a cited line."
1136 (dolist (prefix (mapcar 'car gnus-cite-prefix-alist
))
1137 (when (string= (buffer-substring (point) (+ (length prefix
) (point)))
1143 ;; Highlighting of different citation levels in message-mode.
1144 ;; - message-cite-prefix will be overridden if this is enabled.
1146 (defvar gnus-message-max-citation-depth
1147 (length gnus-cite-face-list
)
1148 "Maximum supported level of citation.")
1150 (defvar gnus-message-cite-prefix-regexp
1151 (concat "^\\(?:" message-cite-prefix-regexp
"\\)"))
1153 (defun gnus-message-search-citation-line (limit)
1154 "Search for a cited line and set match data accordingly.
1155 Returns nil if there is no such line before LIMIT, t otherwise."
1156 (when (re-search-forward gnus-message-cite-prefix-regexp limit t
)
1157 (let ((cdepth (min (length (apply 'concat
1159 (match-string-no-properties 0)
1160 "[ \t [:alnum:]]+")))
1161 gnus-message-max-citation-depth
))
1162 (mlist (make-list (* (1+ gnus-message-max-citation-depth
) 2) nil
))
1163 (start (point-at-bol))
1164 (end (point-at-eol)))
1165 (setcar mlist start
)
1166 (setcar (cdr mlist
) end
)
1167 (setcar (nthcdr (* cdepth
2) mlist
) start
)
1168 (setcar (nthcdr (1+ (* cdepth
2)) mlist
) end
)
1169 (set-match-data mlist
))
1172 (defvar gnus-message-citation-keywords
1173 ;; eval-when-compile ;; This breaks in XEmacs
1174 `((gnus-message-search-citation-line
1177 ;; (require 'gnus-cite)
1178 (dolist (face gnus-cite-face-list
(nreverse list
))
1179 (push (list count
(list 'quote face
) 'prepend t
) list
)
1180 (setq count
(1+ count
)))))) ;;
1181 "Keywords for highlighting different levels of message citations.")
1183 (defvar font-lock-defaults-computed
)
1184 (defvar font-lock-keywords
)
1185 (defvar font-lock-set-defaults
)
1188 (unless (featurep 'xemacs
)
1189 (autoload 'font-lock-set-defaults
"font-lock")))
1191 (define-minor-mode gnus-message-citation-mode
1192 "Toggle `gnus-message-citation-mode' in current buffer.
1193 This buffer local minor mode provides additional font-lock support for
1195 With prefix ARG, turn `gnus-message-citation-mode' on if and only if ARG
1197 Automatically turn `font-lock-mode' on when `gnus-message-citation-mode'
1202 (when (eq major-mode
'message-mode
)
1203 (let ((defaults (car (if (featurep 'xemacs
)
1204 (get 'message-mode
'font-lock-defaults
)
1205 font-lock-defaults
)))
1208 (setq default
(if (consp defaults
)
1212 (setq defaults nil
))))
1213 (if gnus-message-citation-mode
1214 ;; `gnus-message-citation-keywords' should be the last
1215 ;; elements of the keywords because the others are unlikely
1216 ;; to have the OVERRIDE flags -- XEmacs applies a keyword
1217 ;; having no OVERRIDE flag to matched text even if it has
1218 ;; already other faces, while Emacs doesn't.
1219 (set (make-local-variable default
)
1220 (append (default-value default
)
1221 gnus-message-citation-keywords
))
1222 (kill-local-variable default
))))
1223 ;; Force `font-lock-set-defaults' to update `font-lock-keywords'.
1224 (if (featurep 'xemacs
)
1226 (require 'font-lock
)
1227 (setq font-lock-defaults-computed nil
1228 font-lock-keywords nil
))
1229 (setq font-lock-set-defaults nil
))
1230 (font-lock-set-defaults)
1231 (cond ((symbol-value 'font-lock-mode
)
1232 (font-lock-fontify-buffer))
1233 (gnus-message-citation-mode
1234 (font-lock-mode 1)))))
1236 (defun turn-on-gnus-message-citation-mode ()
1237 "Turn on `gnus-message-citation-mode'."
1238 (gnus-message-citation-mode 1))
1239 (defun turn-off-gnus-message-citation-mode ()
1240 "Turn off `gnus-message-citation-mode'."
1241 (gnus-message-citation-mode -
1))
1245 (provide 'gnus-cite
)
1248 ;; coding: iso-8859-1
1251 ;; arch-tag: 1997b044-6067-471e-8c8f-dc903093098a
1252 ;;; gnus-cite.el ends here