1 ;;; gnus-cite.el --- parse citations in articles for Gnus
3 ;; Copyright (C) 1995-2015 Free Software Foundation, Inc.
5 ;; Author: Per Abhiddenware
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 (eval-when-compile (require 'cl
))
28 (when (featurep 'xemacs
)
29 (require 'easy-mmode
))) ; for `define-minor-mode'
34 (require 'message
) ; for message-cite-prefix-regexp
38 (defgroup gnus-cite nil
41 :link
'(custom-manual "(gnus)Article Highlighting")
44 (defcustom gnus-cited-opened-text-button-line-format
"%(%{[-]%}%)\n"
45 "Format of opened cited text buttons."
49 (defcustom gnus-cited-closed-text-button-line-format
"%(%{[+]%}%)\n"
50 "Format of closed cited text buttons."
54 (defcustom gnus-cited-lines-visible nil
55 "The number of lines of hidden cited text to remain visible.
56 Or a pair (cons) of numbers which are the number of lines at the top
57 and bottom of the text, respectively, to remain visible."
59 :type
'(choice (const :tag
"none" nil
)
61 (cons :tag
"Top and Bottom" integer integer
)))
63 (defcustom gnus-cite-parse-max-size
25000
64 "Maximum article size (in bytes) where parsing citations is allowed.
65 Set it to nil to parse all articles."
67 :type
'(choice (const :tag
"all" nil
)
70 (defcustom gnus-cite-max-prefix
20
71 "Maximum possible length for a citation prefix."
75 (defcustom gnus-supercite-regexp
76 (concat "^\\(" message-cite-prefix-regexp
"\\)? *"
77 ">>>>> +\"\\([^\"\n]+\\)\" +==")
78 "*Regexp matching normal Supercite attribution lines.
79 The first grouping must match prefixes added by other packages."
83 (defcustom gnus-supercite-secondary-regexp
"^.*\"\\([^\"\n]+\\)\" +=="
84 "Regexp matching mangled Supercite attribution lines.
85 The first regexp group should match the Supercite attribution."
89 (defcustom gnus-cite-minimum-match-count
2
90 "Minimum number of identical prefixes before we believe it's a citation."
94 ;; Some Microsoft products put in a citation that extends to the
95 ;; remainder of the message:
97 ;; -----Original Message-----
100 ;; Sent: ... [date, in non-RFC-2822 format]
103 ;; Cited message, with no prefixes
105 ;; The four headers are always the same. But note they are prone to
106 ;; folding without additional indentation.
108 ;; Others use "----- Original Message -----" instead, and properly quote
109 ;; the body using "> ". This style is handled without special cases.
111 (defcustom gnus-cite-attribution-prefix
112 "In article\\|in <\\|On \\(Mon\\|Tue\\|Wed\\|Thu\\|Fri\\|Sat\\|Sun\\),\\|----- ?Original Message ?-----"
113 "*Regexp matching the beginning of an attribution line."
117 (defcustom gnus-cite-attribution-suffix
118 "\\(\\(wrote\\|writes\\|said\\|says\\|>\\)\\(:\\|\\.\\.\\.\\)\\|----- ?Original Message ?-----\\)[ \t]*$"
119 "*Regexp matching the end of an attribution line.
120 The text matching the first grouping will be used as a button."
124 (defcustom gnus-cite-unsightly-citation-regexp
125 "^-----Original Message-----\nFrom: \\(.+\n\\)+\n"
126 "Regexp matching Microsoft-type rest-of-message citations."
131 (defcustom gnus-cite-ignore-quoted-from t
132 "Non-nil means don't regard lines beginning with \">From \" as cited text.
133 Those lines may have been quoted by MTAs in order not to mix up with
134 the envelope From line."
139 (defface gnus-cite-attribution
'((t (:italic t
)))
140 "Face used for attribution lines."
142 ;; backward-compatibility alias
143 (put 'gnus-cite-attribution-face
'face-alias
'gnus-cite-attribution
)
144 (put 'gnus-cite-attribution-face
'obsolete-face
"22.1")
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
)
165 (put 'gnus-cite-face-1
'obsolete-face
"22.1")
167 (defface gnus-cite-2
'((((class color
)
169 (:foreground
"light cyan"))
172 (:foreground
"firebrick"))
177 ;; backward-compatibility alias
178 (put 'gnus-cite-face-2
'face-alias
'gnus-cite-2
)
179 (put 'gnus-cite-face-2
'obsolete-face
"22.1")
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
)
193 (put 'gnus-cite-face-3
'obsolete-face
"22.1")
195 (defface gnus-cite-4
'((((class color
)
197 (:foreground
"light pink"))
200 (:foreground
"OrangeRed"))
205 ;; backward-compatibility alias
206 (put 'gnus-cite-face-4
'face-alias
'gnus-cite-4
)
207 (put 'gnus-cite-face-4
'obsolete-face
"22.1")
209 (defface gnus-cite-5
'((((class color
)
211 (:foreground
"pale green"))
214 (:foreground
"dark khaki"))
219 ;; backward-compatibility alias
220 (put 'gnus-cite-face-5
'face-alias
'gnus-cite-5
)
221 (put 'gnus-cite-face-5
'obsolete-face
"22.1")
223 (defface gnus-cite-6
'((((class color
)
225 (:foreground
"beige"))
228 (:foreground
"dark violet"))
233 ;; backward-compatibility alias
234 (put 'gnus-cite-face-6
'face-alias
'gnus-cite-6
)
235 (put 'gnus-cite-face-6
'obsolete-face
"22.1")
237 (defface gnus-cite-7
'((((class color
)
239 (:foreground
"orange"))
242 (:foreground
"SteelBlue4"))
247 ;; backward-compatibility alias
248 (put 'gnus-cite-face-7
'face-alias
'gnus-cite-7
)
249 (put 'gnus-cite-face-7
'obsolete-face
"22.1")
251 (defface gnus-cite-8
'((((class color
)
253 (:foreground
"magenta"))
256 (:foreground
"magenta"))
261 ;; backward-compatibility alias
262 (put 'gnus-cite-face-8
'face-alias
'gnus-cite-8
)
263 (put 'gnus-cite-face-8
'obsolete-face
"22.1")
265 (defface gnus-cite-9
'((((class color
)
267 (:foreground
"violet"))
270 (:foreground
"violet"))
275 ;; backward-compatibility alias
276 (put 'gnus-cite-face-9
'face-alias
'gnus-cite-9
)
277 (put 'gnus-cite-face-9
'obsolete-face
"22.1")
279 (defface gnus-cite-10
'((((class color
)
281 (:foreground
"plum1"))
284 (:foreground
"medium purple"))
289 ;; backward-compatibility alias
290 (put 'gnus-cite-face-10
'face-alias
'gnus-cite-10
)
291 (put 'gnus-cite-face-10
'obsolete-face
"22.1")
293 (defface gnus-cite-11
'((((class color
)
295 (:foreground
"turquoise"))
298 (:foreground
"turquoise"))
303 ;; backward-compatibility alias
304 (put 'gnus-cite-face-11
'face-alias
'gnus-cite-11
)
305 (put 'gnus-cite-face-11
'obsolete-face
"22.1")
307 (defcustom gnus-cite-face-list
308 '(gnus-cite-1 gnus-cite-2 gnus-cite-3 gnus-cite-4 gnus-cite-5 gnus-cite-6
309 gnus-cite-7 gnus-cite-8 gnus-cite-9 gnus-cite-10 gnus-cite-11
)
310 "*List of faces used for highlighting citations.
312 When there are citations from multiple articles in the same message,
313 Gnus will try to give each citation from each article its own face.
314 This should make it easier to see who wrote what."
317 :set
(lambda (symbol value
)
319 (custom-set-default symbol value
)
320 (if (boundp 'gnus-message-max-citation-depth
)
321 (setq gnus-message-max-citation-depth
(length value
)))
322 (if (boundp 'gnus-message-citation-keywords
)
323 (setq gnus-message-citation-keywords
324 `((gnus-message-search-citation-line
327 (dolist (face value
(nreverse list
))
328 (push (list count
(list 'quote face
) 'prepend t
)
330 (setq count
(1+ count
)))))))))))
332 (defcustom gnus-cite-hide-percentage
50
333 "Only hide excess citation if above this percentage of the body."
337 (defcustom gnus-cite-hide-absolute
10
338 "Only hide excess citation if above this number of lines in the body."
342 (defcustom gnus-cite-blank-line-after-header t
343 "If non-nil, put a blank line between the citation header and the button."
347 ;; This has to go here because its default value depends on
348 ;; gnus-cite-face-list.
349 (defcustom gnus-article-boring-faces
(cons 'gnus-signature gnus-cite-face-list
)
350 "List of faces that are not worth reading.
351 If an article has more pages below the one you are looking at, but
352 nothing on those pages is a word of at least three letters that is not
353 in a boring face, then the pages will be skipped."
355 :group
'gnus-article-hiding
)
357 ;;; Internal Variables:
359 (defvar gnus-cite-article nil
)
360 (defvar gnus-cite-overlay-list nil
)
362 (defvar gnus-cite-prefix-alist nil
)
363 ;; Alist of citation prefixes.
364 ;; The cdr is a list of lines with that prefix.
366 (defvar gnus-cite-attribution-alist nil
)
367 ;; Alist of attribution lines.
368 ;; The car is a line number.
369 ;; The cdr is the prefix for the citation started by that line.
371 (defvar gnus-cite-loose-prefix-alist nil
)
372 ;; Alist of citation prefixes that have no matching attribution.
373 ;; The cdr is a list of lines with that prefix.
375 (defvar gnus-cite-loose-attribution-alist nil
)
376 ;; Alist of attribution lines that have no matching citation.
377 ;; Each member has the form (WROTE IN PREFIX TAG), where
378 ;; WROTE: is the attribution line number
379 ;; IN: is the line number of the previous line if part of the same attribution,
380 ;; PREFIX: Is the citation prefix of the attribution line(s), and
381 ;; TAG: Is a Supercite tag, if any.
383 (defvar gnus-cited-opened-text-button-line-format-alist
384 `((?b
(marker-position beg
) ?d
)
385 (?e
(marker-position end
) ?d
)
386 (?n
(count-lines beg end
) ?d
)
387 (?l
(- end beg
) ?d
)))
388 (defvar gnus-cited-opened-text-button-line-format-spec nil
)
389 (defvar gnus-cited-closed-text-button-line-format-alist
390 gnus-cited-opened-text-button-line-format-alist
)
391 (defvar gnus-cited-closed-text-button-line-format-spec nil
)
396 (defun gnus-article-highlight-citation (&optional force same-buffer
)
397 "Highlight cited text.
398 Each citation in the article will be highlighted with a different face.
399 The faces are taken from `gnus-cite-face-list'.
400 Attribution lines are highlighted with the same face as the
401 corresponding citation merged with the face `gnus-cite-attribution'.
403 Text is considered cited if at least `gnus-cite-minimum-match-count'
404 lines matches `message-cite-prefix-regexp' with the same prefix.
406 Lines matching `gnus-cite-attribution-suffix' and perhaps
407 `gnus-cite-attribution-prefix' are considered attribution lines."
408 (interactive (list 'force
))
409 (with-current-buffer (if same-buffer
(current-buffer) gnus-article-buffer
)
410 (gnus-cite-parse-maybe force
)
411 (let ((buffer-read-only nil
)
412 (alist gnus-cite-prefix-alist
)
413 (faces gnus-cite-face-list
)
414 (inhibit-point-motion-hooks t
)
415 face entry prefix skip numbers number face-alist
)
416 ;; Loop through citation prefixes.
418 (setq entry
(car alist
)
423 faces
(or (cdr faces
) gnus-cite-face-list
)
424 face-alist
(cons (cons prefix face
) face-alist
))
426 (setq number
(car numbers
)
427 numbers
(cdr numbers
))
428 (and (not (assq number gnus-cite-attribution-alist
))
429 (not (assq number gnus-cite-loose-attribution-alist
))
430 (gnus-cite-add-face number prefix face
))))
431 ;; Loop through attribution lines.
432 (setq alist gnus-cite-attribution-alist
)
434 (setq entry
(car alist
)
438 skip
(gnus-cite-find-prefix number
)
439 face
(cdr (assoc prefix face-alist
)))
440 ;; Add attribution button.
441 (goto-char (point-min))
442 (forward-line (1- number
))
443 (when (re-search-forward gnus-cite-attribution-suffix
446 (gnus-article-add-button (match-beginning 1) (match-end 1)
447 'gnus-cite-toggle prefix
))
448 ;; Highlight attribution line.
449 (gnus-cite-add-face number skip face
)
450 (gnus-cite-add-face number skip gnus-cite-attribution-face
))
451 ;; Loop through attribution lines.
452 (setq alist gnus-cite-loose-attribution-alist
)
454 (setq entry
(car alist
)
457 skip
(gnus-cite-find-prefix number
))
458 (gnus-cite-add-face number skip gnus-cite-attribution-face
)))))
460 (defun gnus-dissect-cited-text ()
461 "Dissect the article buffer looking for cited text."
462 (with-current-buffer gnus-article-buffer
463 (gnus-cite-parse-maybe nil t
)
464 (let ((alist gnus-cite-prefix-alist
)
465 prefix numbers number marks m
)
466 ;; Loop through citation prefixes.
468 (setq numbers
(pop alist
)
469 prefix
(pop numbers
))
471 (setq number
(pop numbers
))
472 (goto-char (point-min))
473 (forward-line number
)
474 (push (cons (point-marker) "") marks
)
476 (= (1- number
) (car numbers
)))
477 (setq number
(pop numbers
)))
478 (goto-char (point-min))
479 (forward-line (1- number
))
480 (push (cons (point-marker) prefix
) marks
)))
481 ;; Skip to the beginning of the body.
483 (push (cons (point-marker) "") marks
)
484 ;; Find the end of the body.
485 (goto-char (point-max))
486 (gnus-article-search-signature)
487 (push (cons (point-marker) "") marks
)
489 (setq marks
(sort marks
'car-less-than-car
))
490 (let ((omarks marks
))
493 (if (= (caar omarks
) (caadr omarks
))
495 (unless (equal (cdar omarks
) "")
496 (push (car omarks
) marks
))
497 (unless (equal (cdadr omarks
) "")
498 (push (cadr omarks
) marks
))
499 (unless (and (equal (cdar omarks
) "")
500 (equal (cdadr omarks
) "")
502 (setq omarks
(cdr omarks
))))
503 (push (car omarks
) marks
))
504 (setq omarks
(cdr omarks
)))
506 (push (car omarks
) marks
))
507 (setq marks
(setq m
(nreverse marks
)))
509 (if (and (equal (cdadr m
) "")
510 (equal (cdar m
) (cdaddr m
))
511 (goto-char (caadr m
))
512 (looking-at "[ \t]*$")
514 (= (point) (caaddr m
)))
519 (defun gnus-article-fill-cited-long-lines ()
520 (gnus-article-fill-cited-article nil t
))
522 (defun gnus-article-fill-cited-article (&optional width long-lines
)
523 "Do word wrapping in the current article.
524 If WIDTH (the numerical prefix), use that text width when
525 filling. If LONG-LINES, only fill sections that have lines
526 longer than the frame width."
528 (with-current-buffer gnus-article-buffer
529 (let ((buffer-read-only nil
)
530 (inhibit-point-motion-hooks t
)
531 (marks (gnus-dissect-cited-text))
532 (adaptive-fill-mode nil
)
534 (fill-column (if width
(prefix-numeric-value width
) fill-column
)))
537 (narrow-to-region (caar marks
) (caadr marks
))
538 (let ((adaptive-fill-regexp
539 (concat "^" (regexp-quote (cdar marks
)) " *"))
541 (if (string= (cdar marks
) "") ""
542 (concat (cdar marks
) " ")))
543 (do-fill (not long-lines
))
546 (setq do-fill
(gnus-article-foldable-buffer (cdar marks
))))
547 ;; Note: the XEmacs version of `fill-region' inserts a newline
548 ;; unless the region ends with a newline.
551 (fill-region (point-min) (point-max))
552 (goto-char (point-min))
556 (> (current-column) (window-width))
559 (narrow-to-region (line-beginning-position 0) (point))
560 (fill-region (point-min) (point-max))))))))
561 (set-marker (caar marks
) nil
)
562 (setq marks
(cdr marks
)))
564 (set-marker (caar marks
) nil
))
565 ;; All this information is now incorrect.
566 (setq gnus-cite-prefix-alist nil
567 gnus-cite-attribution-alist nil
568 gnus-cite-loose-prefix-alist nil
569 gnus-cite-loose-attribution-alist nil
570 gnus-cite-article nil
)))))
572 (defun gnus-article-foldable-buffer (prefix)
575 (goto-char (point-min))
577 (unless (> (length prefix
) (- (point-max) (point)))
578 (forward-char (length prefix
)))
579 (skip-chars-forward " \t")
581 (let ((elem (assq (current-column) columns
)))
583 (setq elem
(cons (current-column) 0))
585 (setcdr elem
(1+ (cdr elem
)))))
587 (when (> (current-column) (window-width))
591 ;; We know know that there are long lines here, but does this look
592 ;; like code? Check for ragged edges on the left.
593 (< (length columns
) 3))))
595 (defun gnus-article-hide-citation (&optional arg force
)
596 "Toggle hiding of all cited text except attribution lines.
597 See the documentation for `gnus-article-highlight-citation'.
598 If given a negative prefix, always show; if given a positive prefix,
600 (interactive (append (gnus-article-hidden-arg) (list 'force
)))
601 (gnus-set-format 'cited-opened-text-button t
)
602 (gnus-set-format 'cited-closed-text-button t
)
603 (with-current-buffer gnus-article-buffer
604 (let ((buffer-read-only nil
)
606 (inhibit-point-motion-hooks t
)
607 (props (nconc (list 'article-type
'cite
)
608 gnus-hidden-properties
))
612 (text-property-any point
(point-max)
614 'gnus-article-toggle-cited-text
))
617 (gnus-article-toggle-cited-text
618 (get-text-property point
'gnus-data
) arg
)
620 (setq point
(point)))
622 (setq marks
(gnus-dissect-cited-text))
626 (while (and marks
(string= (cdar marks
) ""))
627 (setq marks
(cdr marks
)))
629 (setq beg
(caar marks
)))
630 (while (and marks
(not (string= (cdar marks
) "")))
631 (setq marks
(cdr marks
)))
633 (setq end
(caar marks
)))
634 ;; Skip past lines we want to leave visible.
635 (when (and beg end gnus-cited-lines-visible
)
637 (forward-line (if (consp gnus-cited-lines-visible
)
638 (car gnus-cited-lines-visible
)
639 gnus-cited-lines-visible
))
642 (setq beg
(point-marker))
643 (when (consp gnus-cited-lines-visible
)
645 (forward-line (- (cdr gnus-cited-lines-visible
)))
648 (setq end
(point-marker))))))
650 (gnus-add-wash-type 'cite
)
651 ;; We use markers for the end-points to facilitate later
652 ;; wrapping and mangling of text.
653 (setq beg
(set-marker (make-marker) beg
)
654 end
(set-marker (make-marker) end
))
655 (gnus-add-text-properties-when 'article-type nil beg end props
)
657 (when (and gnus-cite-blank-line-after-header
658 (not (save-excursion (search-backward "\n\n" nil t
))))
661 (setq start
(point-marker))
663 (gnus-article-add-button
665 (progn (eval gnus-cited-closed-text-button-line-format-spec
)
667 `gnus-article-toggle-cited-text
668 (list (cons beg end
) start
))
670 'article-type
'annotation
)
671 (set-marker beg
(point))))))))
673 (defun gnus-article-toggle-cited-text (args &optional arg
)
674 "Toggle hiding the text in REGION.
675 ARG can be nil or a number. Positive means hide, negative
676 means show, nil means toggle."
677 (let* ((region (car args
))
682 (text-property-any beg
(1- end
) 'article-type
'cite
))
683 (inhibit-point-motion-hooks t
)
687 (and (> arg
0) (not hidden
))
688 (and (< arg
0) hidden
))
691 ;; Can't remove 'cite from g-a-wash-types here because
692 ;; multiple citations may be hidden -jas
693 (gnus-remove-text-properties-when
694 'article-type
'cite beg end
695 (cons 'article-type
(cons 'cite
696 gnus-hidden-properties
))))
697 (gnus-add-wash-type 'cite
)
698 (gnus-add-text-properties-when
699 'article-type nil beg end
700 (cons 'article-type
(cons 'cite
701 gnus-hidden-properties
))))
702 (let ((gnus-article-mime-handle-alist-1 gnus-article-mime-handle-alist
))
703 (gnus-set-mode-line 'article
))
710 (gnus-article-add-button
714 gnus-cited-opened-text-button-line-format-spec
715 gnus-cited-closed-text-button-line-format-spec
))
717 `gnus-article-toggle-cited-text
720 'article-type
'annotation
)))))
722 (defun gnus-article-hide-citation-maybe (&optional arg force
)
723 "Toggle hiding of cited text that has an attribution line.
724 If given a negative prefix, always show; if given a positive prefix,
726 This will do nothing unless at least `gnus-cite-hide-percentage'
727 percent and at least `gnus-cite-hide-absolute' lines of the body is
728 cited text with attributions. When called interactively, these two
729 variables are ignored.
730 See also the documentation for `gnus-article-highlight-citation'."
731 (interactive (append (gnus-article-hidden-arg) '(force)))
732 (with-current-buffer gnus-article-buffer
733 (gnus-delete-wash-type 'cite
)
734 (unless (gnus-article-check-hidden-text 'cite arg
)
736 (gnus-cite-parse-maybe force
)
738 (let ((start (point))
739 (atts gnus-cite-attribution-alist
)
740 (buffer-read-only nil
)
741 (inhibit-point-motion-hooks t
)
744 (goto-char (point-max))
745 (gnus-article-search-signature)
746 (setq total
(count-lines start
(point)))
748 (setq hidden
(+ hidden
(length
749 (cdr (assoc (cdar atts
)
750 gnus-cite-prefix-alist
))))
753 (and (> (* 100 hidden
) (* gnus-cite-hide-percentage total
))
754 (> hidden gnus-cite-hide-absolute
)))
755 (gnus-article-hide-citation)))))))
757 (defun gnus-article-hide-citation-in-followups ()
758 "Hide cited text in non-root articles."
760 (with-current-buffer gnus-article-buffer
761 (let ((article (cdr gnus-article-current
)))
762 (unless (with-current-buffer gnus-summary-buffer
763 (gnus-article-displayed-root-p article
))
764 (gnus-article-hide-citation)))))
766 ;;; Internal functions:
768 (defun gnus-cite-parse-maybe (&optional force no-overlay
)
769 "Always parse the buffer."
771 ;;Reset parser information.
772 (setq gnus-cite-prefix-alist nil
773 gnus-cite-attribution-alist nil
774 gnus-cite-loose-prefix-alist nil
775 gnus-cite-loose-attribution-alist nil
)
777 (gnus-cite-delete-overlays))
778 ;; Parse if not too large.
779 (if (and gnus-cite-parse-max-size
780 (> (buffer-size) gnus-cite-parse-max-size
))
782 (setq gnus-cite-article
(cons (car gnus-article-current
)
783 (cdr gnus-article-current
)))
784 (gnus-cite-parse-wrapper)))
786 (defun gnus-cite-delete-overlays ()
787 (dolist (overlay gnus-cite-overlay-list
)
789 (when (or (not (gnus-overlay-end overlay
))
790 (and (>= (gnus-overlay-end overlay
) (point-min))
791 (<= (gnus-overlay-end overlay
) (point-max))))
792 (setq gnus-cite-overlay-list
(delete overlay gnus-cite-overlay-list
))
794 (gnus-delete-overlay overlay
))))))
796 (defun gnus-cite-parse-wrapper ()
797 ;; Wrap chopped gnus-cite-parse.
799 (let ((inhibit-point-motion-hooks t
))
801 (gnus-cite-parse-attributions))
805 (gnus-cite-connect-attributions))))
807 (defun gnus-cite-parse ()
808 ;; Parse and connect citation prefixes and attribution lines.
810 ;; Parse current buffer searching for citation prefixes.
811 (let ((line (1+ (count-lines (point-min) (point))))
814 (goto-char (point-max))
815 (gnus-article-search-signature)
817 (prefix-regexp (concat "^\\(" message-cite-prefix-regexp
"\\)"))
818 alist entry start begin end numbers prefix guess-limit
)
819 ;; Get all potential prefixes in `alist'.
820 (while (< (point) max
)
823 guess-limit
(progn (skip-chars-forward "^> \t\r\n") (point))
827 ;; Ignore standard Supercite attribution prefix.
828 (when (and (< guess-limit
(+ begin gnus-cite-max-prefix
))
829 (looking-at gnus-supercite-regexp
))
831 (setq end
(1+ (match-end 1)))
832 (setq end
(1+ begin
))))
833 ;; Ignore very long prefixes.
834 (when (> end
(+ begin gnus-cite-max-prefix
))
835 (setq end
(+ begin gnus-cite-max-prefix
)))
836 ;; Ignore quoted envelope From_.
837 (when (and gnus-cite-ignore-quoted-from
839 (setq case-fold-search nil
)
840 (looking-at ">From ")
841 (setq case-fold-search t
)))
842 (setq end
(1+ begin
)))
843 (while (re-search-forward prefix-regexp
(1- end
) t
)
845 (setq end
(match-end 0)
846 prefix
(buffer-substring begin end
))
847 (set-text-properties 0 (length prefix
) nil prefix
)
848 (setq entry
(assoc prefix alist
))
850 (setcdr entry
(cons line
(cdr entry
)))
851 (push (list prefix line
) alist
))
854 (setq line
(1+ line
)))
855 ;; Horrible special case for some Microsoft mailers.
856 (goto-char (point-min))
857 (setq start t begin nil entry nil
)
859 ;; Assume this search ends up at the beginning of a line.
860 (if (re-search-forward gnus-cite-unsightly-citation-regexp max t
)
862 (when (number-or-marker-p start
)
863 (setq begin
(count-lines (point-min) start
)
864 end
(count-lines (point-min) (match-beginning 0))))
865 (setq start
(match-end 0)))
866 (when (number-or-marker-p start
)
867 (setq begin
(count-lines (point-min) start
)
868 end
(count-lines (point-min) max
)))
872 ;; Need to do 1+ because we're in the bol.
873 (push (setq begin
(1+ begin
)) entry
))))
875 (push (cons "" entry
) alist
))
876 ;; We got all the potential prefixes. Now create
877 ;; `gnus-cite-prefix-alist' containing the oldest prefix for each
878 ;; line that appears at least `gnus-cite-minimum-match-count'
879 ;; times. First sort them by length. Longer is older.
880 (setq alist
(sort alist
(lambda (a b
)
881 (> (length (car a
)) (length (car b
))))))
883 (setq entry
(car alist
)
887 (cond ((null numbers
)
888 ;; No lines with this prefix that wasn't also part of
891 ((< (length numbers
) gnus-cite-minimum-match-count
)
892 ;; Too few lines with this prefix. We keep it a bit
893 ;; longer in case it is an exact match for an attribution
894 ;; line, but we don't remove the line from other
896 (push entry gnus-cite-prefix-alist
))
899 gnus-cite-prefix-alist
)
900 ;; Remove articles from other prefixes.
904 (setq current
(car loop
)
907 (gnus-set-difference (cdr current
) numbers
)))))))))
909 (defun gnus-cite-parse-attributions ()
911 ;; Parse attributions
912 (while (re-search-forward gnus-cite-attribution-suffix
(point-max) t
)
913 (let* ((start (match-beginning 0))
915 (wrote (count-lines (point-min) end
))
916 (prefix (gnus-cite-find-prefix wrote
))
917 ;; Check previous line for an attribution leader.
919 (beginning-of-line 1)
920 (when (looking-at gnus-supercite-secondary-regexp
)
921 (buffer-substring (match-beginning 1)
925 (and (re-search-backward gnus-cite-attribution-prefix
927 (beginning-of-line 0)
930 (not (re-search-forward gnus-cite-attribution-suffix
932 (count-lines (point-min) (1+ (point)))))))
936 ;; don't add duplicates
937 (let ((al (buffer-substring (save-excursion (beginning-of-line 0)
940 (when (not (assoc al al-alist
))
941 (push (list wrote in prefix tag
)
942 gnus-cite-loose-attribution-alist
)
943 (push (cons al t
) al-alist
)))))))
945 (defun gnus-cite-connect-attributions ()
946 ;; Connect attributions to citations
948 ;; No citations have been connected to attribution lines yet.
949 (setq gnus-cite-loose-prefix-alist
(append gnus-cite-prefix-alist nil
))
951 ;; Parse current buffer searching for attribution lines.
952 ;; Find exact supercite citations.
953 (gnus-cite-match-attributions 'small nil
957 (regexp-quote prefix
) "[ \t]*"
958 (regexp-quote tag
) ">"))))
959 ;; Find loose supercite citations after attributions.
960 (gnus-cite-match-attributions 'small t
966 ;; Find loose supercite citations anywhere.
967 (gnus-cite-match-attributions 'small nil
973 ;; Find nested citations after attributions.
974 (gnus-cite-match-attributions 'small-if-unique t
976 (concat "\\`" (regexp-quote prefix
) ".+")))
977 ;; Find nested citations anywhere.
978 (gnus-cite-match-attributions 'small nil
980 (concat "\\`" (regexp-quote prefix
) ".+")))
981 ;; Remove loose prefixes with too few lines.
982 (let ((alist gnus-cite-loose-prefix-alist
)
985 (setq entry
(car alist
)
987 (when (< (length (cdr entry
)) gnus-cite-minimum-match-count
)
988 (setq gnus-cite-prefix-alist
989 (delq entry gnus-cite-prefix-alist
)
990 gnus-cite-loose-prefix-alist
991 (delq entry gnus-cite-loose-prefix-alist
)))))
992 ;; Find flat attributions.
993 (gnus-cite-match-attributions 'first t nil
)
994 ;; Find any attributions (are we getting desperate yet?).
995 (gnus-cite-match-attributions 'first nil nil
))
997 (defun gnus-cite-match-attributions (sort after fun
)
998 ;; Match all loose attributions and citations (SORT AFTER FUN) .
1000 ;; If SORT is `small', the citation with the shortest prefix will be
1001 ;; used, if it is `first' the first prefix will be used, if it is
1002 ;; `small-if-unique' the shortest prefix will be used if the
1003 ;; attribution line does not share its own prefix with other
1004 ;; loose attribution lines, otherwise the first prefix will be used.
1006 ;; If AFTER is non-nil, only citations after the attribution line
1007 ;; will be considered.
1009 ;; If FUN is non-nil, it will be called with the arguments (WROTE
1010 ;; PREFIX TAG) and expected to return a regular expression. Only
1011 ;; citations whose prefix matches the regular expression will be
1014 ;; WROTE is the attribution line number.
1015 ;; PREFIX is the attribution line prefix.
1016 ;; TAG is the Supercite tag on the attribution line.
1017 (let ((atts gnus-cite-loose-attribution-alist
)
1018 (case-fold-search t
)
1019 att wrote in prefix tag regexp limit smallest best size
)
1021 (setq att
(car atts
)
1027 regexp
(if fun
(funcall fun prefix tag
) "")
1028 size
(cond ((eq sort
'small
) t
)
1029 ((eq sort
'first
) nil
)
1030 (t (< (length (gnus-cite-find-loose prefix
)) 2)))
1031 limit
(if after wrote -
1)
1034 (let ((cites gnus-cite-loose-prefix-alist
)
1035 cite candidate numbers first compare
)
1037 (setq cite
(car cites
)
1039 candidate
(car cite
)
1041 first
(apply 'min numbers
)
1042 compare
(if size
(length candidate
) first
))
1043 (and (> first limit
)
1045 (string-match regexp candidate
)
1046 (< compare smallest
)
1048 smallest compare
))))
1051 (setq gnus-cite-loose-attribution-alist
1052 (delq att gnus-cite-loose-attribution-alist
))
1053 (push (cons wrote
(car best
)) gnus-cite-attribution-alist
)
1055 (push (cons in
(car best
)) gnus-cite-attribution-alist
))
1056 (when (memq best gnus-cite-loose-prefix-alist
)
1057 (let ((loop gnus-cite-prefix-alist
)
1058 (numbers (cdr best
))
1060 (setq gnus-cite-loose-prefix-alist
1061 (delq best gnus-cite-loose-prefix-alist
))
1063 (setq current
(car loop
)
1065 (if (eq current best
)
1067 (setcdr current
(gnus-set-difference (cdr current
) numbers
))
1068 (when (null (cdr current
))
1069 (setq gnus-cite-loose-prefix-alist
1070 (delq current gnus-cite-loose-prefix-alist
)
1071 atts
(delq current atts
)))))))))))
1073 (defun gnus-cite-find-loose (prefix)
1074 ;; Return a list of loose attribution lines prefixed by PREFIX.
1075 (let* ((atts gnus-cite-loose-attribution-alist
)
1078 (setq att
(car atts
)
1081 (when (string-equal (gnus-cite-find-prefix line
) prefix
)
1085 (defun gnus-cite-add-face (number prefix face
)
1086 ;; At line NUMBER, ignore PREFIX and add FACE to the rest of the line.
1088 (let ((inhibit-point-motion-hooks t
)
1090 (goto-char (point-min))
1091 (when (zerop (forward-line (1- number
)))
1092 (forward-char (length prefix
))
1093 (skip-chars-forward " \t")
1096 (skip-chars-backward " \t")
1099 (push (setq overlay
(gnus-make-overlay from to nil t
))
1100 gnus-cite-overlay-list
)
1101 (gnus-overlay-put overlay
'evaporate t
)
1102 (gnus-overlay-put overlay
'face face
))))))
1104 (defun gnus-cite-toggle (prefix)
1105 (with-current-buffer gnus-article-buffer
1106 (gnus-cite-parse-maybe nil t
)
1107 (let ((buffer-read-only nil
)
1108 (numbers (cdr (assoc prefix gnus-cite-prefix-alist
)))
1109 (inhibit-point-motion-hooks t
)
1112 (setq number
(car numbers
)
1113 numbers
(cdr numbers
))
1114 (goto-char (point-min))
1115 (forward-line (1- number
))
1116 (cond ((get-text-property (point) 'invisible
)
1117 ;; Can't remove 'cite from g-a-wash-types here because
1118 ;; multiple citations may be hidden -jas
1119 (remove-text-properties (point) (progn (forward-line 1) (point))
1120 gnus-hidden-properties
))
1121 ((assq number gnus-cite-attribution-alist
))
1123 (gnus-add-wash-type 'cite
)
1124 (gnus-add-text-properties
1125 (point) (progn (forward-line 1) (point))
1126 (nconc (list 'article-type
'cite
)
1127 gnus-hidden-properties
))))
1128 (let ((gnus-article-mime-handle-alist-1
1129 gnus-article-mime-handle-alist
))
1130 (gnus-set-mode-line 'article
))))))
1132 (defun gnus-cite-find-prefix (line)
1133 ;; Return citation prefix for LINE.
1134 (let ((alist gnus-cite-prefix-alist
)
1138 (setq entry
(car alist
)
1140 (when (memq line
(cdr entry
))
1141 (setq prefix
(car entry
))))
1144 (defun gnus-cite-localize ()
1145 "Make the citation variables local to the article buffer."
1146 (let ((vars '(gnus-cite-article
1147 gnus-cite-overlay-list gnus-cite-prefix-alist
1148 gnus-cite-attribution-alist gnus-cite-loose-prefix-alist
1149 gnus-cite-loose-attribution-alist
)))
1151 (make-local-variable (pop vars
)))))
1153 ;; Highlighting of different citation levels in message-mode.
1154 ;; - message-cite-prefix will be overridden if this is enabled.
1156 (defvar gnus-message-max-citation-depth
1157 (length gnus-cite-face-list
)
1158 "Maximum supported level of citation.")
1160 (defvar gnus-message-cite-prefix-regexp
1161 (concat "^\\(?:" message-cite-prefix-regexp
"\\)"))
1163 (defun gnus-message-search-citation-line (limit)
1164 "Search for a cited line and set match data accordingly.
1165 Returns nil if there is no such line before LIMIT, t otherwise."
1166 (when (re-search-forward gnus-message-cite-prefix-regexp limit t
)
1167 (let ((cdepth (min (length (apply 'concat
1169 (match-string-no-properties 0)
1170 "[ \t [:alnum:]]+")))
1171 gnus-message-max-citation-depth
))
1172 (mlist (make-list (* (1+ gnus-message-max-citation-depth
) 2) nil
))
1173 (start (point-at-bol))
1174 (end (point-at-eol)))
1175 (setcar mlist start
)
1176 (setcar (cdr mlist
) end
)
1177 (setcar (nthcdr (* cdepth
2) mlist
) start
)
1178 (setcar (nthcdr (1+ (* cdepth
2)) mlist
) end
)
1179 (set-match-data mlist
))
1182 (defvar gnus-message-citation-keywords
1183 ;; eval-when-compile ;; This breaks in XEmacs
1184 `((gnus-message-search-citation-line
1187 ;; (require 'gnus-cite)
1188 (dolist (face gnus-cite-face-list
(nreverse list
))
1189 (push (list count
(list 'quote face
) 'prepend t
) list
)
1190 (setq count
(1+ count
)))))) ;;
1191 "Keywords for highlighting different levels of message citations.")
1193 (defvar font-lock-defaults-computed
)
1194 (defvar font-lock-keywords
)
1195 (defvar font-lock-set-defaults
)
1198 (unless (featurep 'xemacs
)
1199 (autoload 'font-lock-set-defaults
"font-lock")))
1201 (define-minor-mode gnus-message-citation-mode
1202 "Minor mode providing more font-lock support for nested citations.
1203 When enabled, it automatically turns on `font-lock-mode'."
1207 (when (eq major-mode
'message-mode
) ;FIXME: Use derived-mode-p.
1208 ;; FIXME: Use font-lock-add-keywords!
1209 (let ((defaults (car (if (featurep 'xemacs
)
1210 (get 'message-mode
'font-lock-defaults
)
1211 font-lock-defaults
)))
1214 (setq default
(if (consp defaults
)
1218 (setq defaults nil
))))
1219 (if gnus-message-citation-mode
1220 ;; `gnus-message-citation-keywords' should be the last
1221 ;; elements of the keywords because the others are unlikely
1222 ;; to have the OVERRIDE flags -- XEmacs applies a keyword
1223 ;; having no OVERRIDE flag to matched text even if it has
1224 ;; already other faces, while Emacs doesn't.
1225 (set (make-local-variable default
)
1226 (append (default-value default
)
1227 gnus-message-citation-keywords
))
1228 (kill-local-variable default
))))
1229 ;; Force `font-lock-set-defaults' to update `font-lock-keywords'.
1230 (if (featurep 'xemacs
)
1232 (require 'font-lock
)
1233 (setq font-lock-defaults-computed nil
1234 font-lock-keywords nil
))
1235 (setq font-lock-set-defaults nil
))
1236 (font-lock-set-defaults)
1237 (cond (font-lock-mode
1238 (if (fboundp 'font-lock-flush
)
1240 (font-lock-fontify-buffer)))
1241 (gnus-message-citation-mode
1242 (font-lock-mode 1)))))
1244 (defun turn-on-gnus-message-citation-mode ()
1245 "Turn on `gnus-message-citation-mode'."
1246 (gnus-message-citation-mode 1))
1247 (defun turn-off-gnus-message-citation-mode ()
1248 "Turn off `gnus-message-citation-mode'."
1249 (gnus-message-citation-mode -
1))
1253 (provide 'gnus-cite
)
1259 ;;; gnus-cite.el ends here