1 ;;; gnus-cite.el --- parse citations in articles for Gnus
3 ;; Copyright (C) 1995-2017 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
))
31 (require 'message
) ; for message-cite-prefix-regexp
35 (defgroup gnus-cite nil
38 :link
'(custom-manual "(gnus)Article Highlighting")
41 (defcustom gnus-cited-opened-text-button-line-format
"%(%{[-]%}%)\n"
42 "Format of opened cited text buttons."
46 (defcustom gnus-cited-closed-text-button-line-format
"%(%{[+]%}%)\n"
47 "Format of closed cited text buttons."
51 (defcustom gnus-cited-lines-visible nil
52 "The number of lines of hidden cited text to remain visible.
53 Or a pair (cons) of numbers which are the number of lines at the top
54 and bottom of the text, respectively, to remain visible."
56 :type
'(choice (const :tag
"none" nil
)
58 (cons :tag
"Top and Bottom" integer integer
)))
60 (defcustom gnus-cite-parse-max-size
25000
61 "Maximum article size (in bytes) where parsing citations is allowed.
62 Set it to nil to parse all articles."
64 :type
'(choice (const :tag
"all" nil
)
67 (defcustom gnus-cite-max-prefix
20
68 "Maximum possible length for a citation prefix."
72 (defcustom gnus-supercite-regexp
73 (concat "^\\(" message-cite-prefix-regexp
"\\)? *"
74 ">>>>> +\"\\([^\"\n]+\\)\" +==")
75 "Regexp matching normal Supercite attribution lines.
76 The first grouping must match prefixes added by other packages."
80 (defcustom gnus-supercite-secondary-regexp
"^.*\"\\([^\"\n]+\\)\" +=="
81 "Regexp matching mangled Supercite attribution lines.
82 The first regexp group should match the Supercite attribution."
86 (defcustom gnus-cite-minimum-match-count
2
87 "Minimum number of identical prefixes before we believe it's a citation."
91 ;; Some Microsoft products put in a citation that extends to the
92 ;; remainder of the message:
94 ;; -----Original Message-----
97 ;; Sent: ... [date, in non-RFC-2822 format]
100 ;; Cited message, with no prefixes
102 ;; The four headers are always the same. But note they are prone to
103 ;; folding without additional indentation.
105 ;; Others use "----- Original Message -----" instead, and properly quote
106 ;; the body using "> ". This style is handled without special cases.
108 (defcustom gnus-cite-attribution-prefix
109 "In article\\|in <\\|On \\(Mon\\|Tue\\|Wed\\|Thu\\|Fri\\|Sat\\|Sun\\),\\|----- ?Original Message ?-----"
110 "Regexp matching the beginning of an attribution line."
114 (defcustom gnus-cite-attribution-suffix
115 "\\(\\(wrote\\|writes\\|said\\|says\\|>\\)\\(:\\|\\.\\.\\.\\)\\|----- ?Original Message ?-----\\)[ \t]*$"
116 "Regexp matching the end of an attribution line.
117 The text matching the first grouping will be used as a button."
121 (defcustom gnus-cite-unsightly-citation-regexp
122 "^-----Original Message-----\nFrom: \\(.+\n\\)+\n"
123 "Regexp matching Microsoft-type rest-of-message citations."
128 (defcustom gnus-cite-ignore-quoted-from t
129 "Non-nil means don't regard lines beginning with \">From \" as cited text.
130 Those lines may have been quoted by MTAs in order not to mix up with
131 the envelope From line."
136 (defface gnus-cite-attribution
'((t (:italic t
)))
137 "Face used for attribution lines."
139 ;; backward-compatibility alias
140 (put 'gnus-cite-attribution-face
'face-alias
'gnus-cite-attribution
)
141 (put 'gnus-cite-attribution-face
'obsolete-face
"22.1")
143 (defcustom gnus-cite-attribution-face
'gnus-cite-attribution
144 "Face used for attribution lines.
145 It is merged with the face for the cited text belonging to the attribution."
150 (defface gnus-cite-1
'((((class color
)
152 (:foreground
"light blue"))
155 (:foreground
"MidnightBlue"))
160 ;; backward-compatibility alias
161 (put 'gnus-cite-face-1
'face-alias
'gnus-cite-1
)
162 (put 'gnus-cite-face-1
'obsolete-face
"22.1")
164 (defface gnus-cite-2
'((((class color
)
166 (:foreground
"light cyan"))
169 (:foreground
"firebrick"))
174 ;; backward-compatibility alias
175 (put 'gnus-cite-face-2
'face-alias
'gnus-cite-2
)
176 (put 'gnus-cite-face-2
'obsolete-face
"22.1")
178 (defface gnus-cite-3
'((((class color
)
180 (:foreground
"light yellow"))
183 (:foreground
"dark green"))
188 ;; backward-compatibility alias
189 (put 'gnus-cite-face-3
'face-alias
'gnus-cite-3
)
190 (put 'gnus-cite-face-3
'obsolete-face
"22.1")
192 (defface gnus-cite-4
'((((class color
)
194 (:foreground
"light pink"))
197 (:foreground
"OrangeRed"))
202 ;; backward-compatibility alias
203 (put 'gnus-cite-face-4
'face-alias
'gnus-cite-4
)
204 (put 'gnus-cite-face-4
'obsolete-face
"22.1")
206 (defface gnus-cite-5
'((((class color
)
208 (:foreground
"pale green"))
211 (:foreground
"dark khaki"))
216 ;; backward-compatibility alias
217 (put 'gnus-cite-face-5
'face-alias
'gnus-cite-5
)
218 (put 'gnus-cite-face-5
'obsolete-face
"22.1")
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
)
232 (put 'gnus-cite-face-6
'obsolete-face
"22.1")
234 (defface gnus-cite-7
'((((class color
)
236 (:foreground
"orange"))
239 (:foreground
"SteelBlue4"))
244 ;; backward-compatibility alias
245 (put 'gnus-cite-face-7
'face-alias
'gnus-cite-7
)
246 (put 'gnus-cite-face-7
'obsolete-face
"22.1")
248 (defface gnus-cite-8
'((((class color
)
250 (:foreground
"magenta"))
253 (:foreground
"magenta"))
258 ;; backward-compatibility alias
259 (put 'gnus-cite-face-8
'face-alias
'gnus-cite-8
)
260 (put 'gnus-cite-face-8
'obsolete-face
"22.1")
262 (defface gnus-cite-9
'((((class color
)
264 (:foreground
"violet"))
267 (:foreground
"violet"))
272 ;; backward-compatibility alias
273 (put 'gnus-cite-face-9
'face-alias
'gnus-cite-9
)
274 (put 'gnus-cite-face-9
'obsolete-face
"22.1")
276 (defface gnus-cite-10
'((((class color
)
278 (:foreground
"plum1"))
281 (:foreground
"medium purple"))
286 ;; backward-compatibility alias
287 (put 'gnus-cite-face-10
'face-alias
'gnus-cite-10
)
288 (put 'gnus-cite-face-10
'obsolete-face
"22.1")
290 (defface gnus-cite-11
'((((class color
)
292 (:foreground
"turquoise"))
295 (:foreground
"turquoise"))
300 ;; backward-compatibility alias
301 (put 'gnus-cite-face-11
'face-alias
'gnus-cite-11
)
302 (put 'gnus-cite-face-11
'obsolete-face
"22.1")
304 (defcustom gnus-cite-face-list
305 '(gnus-cite-1 gnus-cite-2 gnus-cite-3 gnus-cite-4 gnus-cite-5 gnus-cite-6
306 gnus-cite-7 gnus-cite-8 gnus-cite-9 gnus-cite-10 gnus-cite-11
)
307 "List of faces used for highlighting citations.
309 When there are citations from multiple articles in the same message,
310 Gnus will try to give each citation from each article its own face.
311 This should make it easier to see who wrote what."
314 :set
(lambda (symbol value
)
316 (custom-set-default symbol value
)
317 (if (boundp 'gnus-message-max-citation-depth
)
318 (setq gnus-message-max-citation-depth
(length value
)))
319 (if (boundp 'gnus-message-citation-keywords
)
320 (setq gnus-message-citation-keywords
321 `((gnus-message-search-citation-line
324 (dolist (face value
(nreverse list
))
325 (push (list count
(list 'quote face
) 'prepend t
)
327 (setq count
(1+ count
)))))))))))
329 (defcustom gnus-cite-hide-percentage
50
330 "Only hide excess citation if above this percentage of the body."
334 (defcustom gnus-cite-hide-absolute
10
335 "Only hide excess citation if above this number of lines in the body."
339 (defcustom gnus-cite-blank-line-after-header t
340 "If non-nil, put a blank line between the citation header and the button."
344 ;; This has to go here because its default value depends on
345 ;; gnus-cite-face-list.
346 (defcustom gnus-article-boring-faces
(cons 'gnus-signature gnus-cite-face-list
)
347 "List of faces that are not worth reading.
348 If an article has more pages below the one you are looking at, but
349 nothing on those pages is a word of at least three letters that is not
350 in a boring face, then the pages will be skipped."
352 :group
'gnus-article-hiding
)
354 ;;; Internal Variables:
356 (defvar gnus-cite-article nil
)
357 (defvar gnus-cite-overlay-list nil
)
359 (defvar gnus-cite-prefix-alist nil
)
360 ;; Alist of citation prefixes.
361 ;; The cdr is a list of lines with that prefix.
363 (defvar gnus-cite-attribution-alist nil
)
364 ;; Alist of attribution lines.
365 ;; The car is a line number.
366 ;; The cdr is the prefix for the citation started by that line.
368 (defvar gnus-cite-loose-prefix-alist nil
)
369 ;; Alist of citation prefixes that have no matching attribution.
370 ;; The cdr is a list of lines with that prefix.
372 (defvar gnus-cite-loose-attribution-alist nil
)
373 ;; Alist of attribution lines that have no matching citation.
374 ;; Each member has the form (WROTE IN PREFIX TAG), where
375 ;; WROTE: is the attribution line number
376 ;; IN: is the line number of the previous line if part of the same attribution,
377 ;; PREFIX: Is the citation prefix of the attribution line(s), and
378 ;; TAG: Is a Supercite tag, if any.
380 (defvar gnus-cited-opened-text-button-line-format-alist
381 `((?b
(marker-position beg
) ?d
)
382 (?e
(marker-position end
) ?d
)
383 (?n
(count-lines beg end
) ?d
)
384 (?l
(- end beg
) ?d
)))
385 (defvar gnus-cited-opened-text-button-line-format-spec nil
)
386 (defvar gnus-cited-closed-text-button-line-format-alist
387 gnus-cited-opened-text-button-line-format-alist
)
388 (defvar gnus-cited-closed-text-button-line-format-spec nil
)
393 (defun gnus-article-highlight-citation (&optional force same-buffer
)
394 "Highlight cited text.
395 Each citation in the article will be highlighted with a different face.
396 The faces are taken from `gnus-cite-face-list'.
397 Attribution lines are highlighted with the same face as the
398 corresponding citation merged with the face `gnus-cite-attribution'.
400 Text is considered cited if at least `gnus-cite-minimum-match-count'
401 lines matches `message-cite-prefix-regexp' with the same prefix.
403 Lines matching `gnus-cite-attribution-suffix' and perhaps
404 `gnus-cite-attribution-prefix' are considered attribution lines."
405 (interactive (list 'force
))
406 (with-current-buffer (if same-buffer
(current-buffer) gnus-article-buffer
)
407 (gnus-cite-parse-maybe force
)
408 (let ((buffer-read-only nil
)
409 (alist gnus-cite-prefix-alist
)
410 (faces gnus-cite-face-list
)
411 (inhibit-point-motion-hooks t
)
412 face entry prefix skip numbers number face-alist
)
413 ;; Loop through citation prefixes.
415 (setq entry
(car alist
)
420 faces
(or (cdr faces
) gnus-cite-face-list
)
421 face-alist
(cons (cons prefix face
) face-alist
))
423 (setq number
(car numbers
)
424 numbers
(cdr numbers
))
425 (and (not (assq number gnus-cite-attribution-alist
))
426 (not (assq number gnus-cite-loose-attribution-alist
))
427 (gnus-cite-add-face number prefix face
))))
428 ;; Loop through attribution lines.
429 (setq alist gnus-cite-attribution-alist
)
431 (setq entry
(car alist
)
435 skip
(gnus-cite-find-prefix number
)
436 face
(cdr (assoc prefix face-alist
)))
437 ;; Add attribution button.
438 (goto-char (point-min))
439 (forward-line (1- number
))
440 (when (re-search-forward gnus-cite-attribution-suffix
443 (gnus-article-add-button (match-beginning 1) (match-end 1)
444 'gnus-cite-toggle prefix
))
445 ;; Highlight attribution line.
446 (gnus-cite-add-face number skip face
)
447 (gnus-cite-add-face number skip gnus-cite-attribution-face
))
448 ;; Loop through attribution lines.
449 (setq alist gnus-cite-loose-attribution-alist
)
451 (setq entry
(car alist
)
454 skip
(gnus-cite-find-prefix number
))
455 (gnus-cite-add-face number skip gnus-cite-attribution-face
)))))
457 (defun gnus-dissect-cited-text ()
458 "Dissect the article buffer looking for cited text."
459 (with-current-buffer gnus-article-buffer
460 (gnus-cite-parse-maybe nil t
)
461 (let ((alist gnus-cite-prefix-alist
)
462 prefix numbers number marks m
)
463 ;; Loop through citation prefixes.
465 (setq numbers
(pop alist
)
466 prefix
(pop numbers
))
468 (setq number
(pop numbers
))
469 (goto-char (point-min))
470 (forward-line number
)
471 (push (cons (point-marker) "") marks
)
473 (= (1- number
) (car numbers
)))
474 (setq number
(pop numbers
)))
475 (goto-char (point-min))
476 (forward-line (1- number
))
477 (push (cons (point-marker) prefix
) marks
)))
478 ;; Skip to the beginning of the body.
480 (push (cons (point-marker) "") marks
)
481 ;; Find the end of the body.
482 (goto-char (point-max))
483 (gnus-article-search-signature)
484 (push (cons (point-marker) "") marks
)
486 (setq marks
(sort marks
'car-less-than-car
))
487 (let ((omarks marks
))
490 (if (= (caar omarks
) (caadr omarks
))
492 (unless (equal (cdar omarks
) "")
493 (push (car omarks
) marks
))
494 (unless (equal (cdadr omarks
) "")
495 (push (cadr omarks
) marks
))
496 (unless (and (equal (cdar omarks
) "")
497 (equal (cdadr omarks
) "")
499 (setq omarks
(cdr omarks
))))
500 (push (car omarks
) marks
))
501 (setq omarks
(cdr omarks
)))
503 (push (car omarks
) marks
))
504 (setq marks
(setq m
(nreverse marks
)))
506 (if (and (equal (cdadr m
) "")
507 (equal (cdar m
) (cdaddr m
))
508 (goto-char (caadr m
))
509 (looking-at "[ \t]*$")
511 (= (point) (caaddr m
)))
516 (defun gnus-article-fill-cited-long-lines ()
517 (gnus-article-fill-cited-article nil t
))
519 (defun gnus-article-fill-cited-article (&optional width long-lines
)
520 "Do word wrapping in the current article.
521 If WIDTH (the numerical prefix), use that text width when
522 filling. If LONG-LINES, only fill sections that have lines
523 longer than the frame width."
525 (with-current-buffer gnus-article-buffer
526 (let ((buffer-read-only nil
)
527 (inhibit-point-motion-hooks t
)
528 (marks (gnus-dissect-cited-text))
529 (adaptive-fill-mode nil
)
530 (fill-column (if width
(prefix-numeric-value width
) fill-column
)))
533 (narrow-to-region (caar marks
) (caadr marks
))
534 (let ((adaptive-fill-regexp
535 (concat "^" (regexp-quote (cdar marks
)) " *"))
537 (if (string= (cdar marks
) "") ""
538 (concat (cdar marks
) " ")))
539 (do-fill (not long-lines
))
542 (setq do-fill
(gnus-article-foldable-buffer (cdar marks
))))
543 ;; Note: the XEmacs version of `fill-region' inserts a newline
544 ;; unless the region ends with a newline.
547 (fill-region (point-min) (point-max))
548 (goto-char (point-min))
552 (> (current-column) (window-width))
555 (narrow-to-region (line-beginning-position 0) (point))
556 (fill-region (point-min) (point-max))))))))
557 (set-marker (caar marks
) nil
)
558 (setq marks
(cdr marks
)))
560 (set-marker (caar marks
) nil
))
561 ;; All this information is now incorrect.
562 (setq gnus-cite-prefix-alist nil
563 gnus-cite-attribution-alist nil
564 gnus-cite-loose-prefix-alist nil
565 gnus-cite-loose-attribution-alist nil
566 gnus-cite-article nil
)))))
568 (defun gnus-article-foldable-buffer (prefix)
571 (goto-char (point-min))
573 (unless (> (length prefix
) (- (point-max) (point)))
574 (forward-char (length prefix
)))
575 (skip-chars-forward " \t")
577 (let ((elem (assq (current-column) columns
)))
579 (setq elem
(cons (current-column) 0))
581 (setcdr elem
(1+ (cdr elem
)))))
583 (when (> (current-column) (window-width))
587 ;; We know know that there are long lines here, but does this look
588 ;; like code? Check for ragged edges on the left.
589 (< (length columns
) 3))))
591 (defun gnus-article-hide-citation (&optional arg force
)
592 "Toggle hiding of all cited text except attribution lines.
593 See the documentation for `gnus-article-highlight-citation'.
594 If given a negative prefix, always show; if given a positive prefix,
596 (interactive (append (gnus-article-hidden-arg) (list 'force
)))
597 (gnus-set-format 'cited-opened-text-button t
)
598 (gnus-set-format 'cited-closed-text-button t
)
599 (with-current-buffer gnus-article-buffer
600 (let ((buffer-read-only nil
)
602 (inhibit-point-motion-hooks t
)
603 (props (nconc (list 'article-type
'cite
)
604 gnus-hidden-properties
))
608 (text-property-any point
(point-max)
610 'gnus-article-toggle-cited-text
))
613 (gnus-article-toggle-cited-text
614 (get-text-property point
'gnus-data
) arg
)
616 (setq point
(point)))
618 (setq marks
(gnus-dissect-cited-text))
622 (while (and marks
(string= (cdar marks
) ""))
623 (setq marks
(cdr marks
)))
625 (setq beg
(caar marks
)))
626 (while (and marks
(not (string= (cdar marks
) "")))
627 (setq marks
(cdr marks
)))
629 (setq end
(caar marks
)))
630 ;; Skip past lines we want to leave visible.
631 (when (and beg end gnus-cited-lines-visible
)
633 (forward-line (if (consp gnus-cited-lines-visible
)
634 (car gnus-cited-lines-visible
)
635 gnus-cited-lines-visible
))
638 (setq beg
(point-marker))
639 (when (consp gnus-cited-lines-visible
)
641 (forward-line (- (cdr gnus-cited-lines-visible
)))
644 (setq end
(point-marker))))))
646 (gnus-add-wash-type 'cite
)
647 ;; We use markers for the end-points to facilitate later
648 ;; wrapping and mangling of text.
649 (setq beg
(set-marker (make-marker) beg
)
650 end
(set-marker (make-marker) end
))
651 (gnus-add-text-properties-when 'article-type nil beg end props
)
653 (when (and gnus-cite-blank-line-after-header
654 (not (save-excursion (search-backward "\n\n" nil t
))))
657 (setq start
(point-marker))
659 (gnus-article-add-button
661 (progn (eval gnus-cited-closed-text-button-line-format-spec
)
663 `gnus-article-toggle-cited-text
664 (list (cons beg end
) start
))
666 'article-type
'annotation
)
667 (set-marker beg
(point))))))))
669 (defun gnus-article-toggle-cited-text (args &optional arg
)
670 "Toggle hiding the text in REGION.
671 ARG can be nil or a number. Positive means hide, negative
672 means show, nil means toggle."
673 (let* ((region (car args
))
678 (text-property-any beg
(1- end
) 'article-type
'cite
))
679 (inhibit-point-motion-hooks t
)
683 (and (> arg
0) (not hidden
))
684 (and (< arg
0) hidden
))
687 ;; Can't remove 'cite from g-a-wash-types here because
688 ;; multiple citations may be hidden -jas
689 (gnus-remove-text-properties-when
690 'article-type
'cite beg end
691 (cons 'article-type
(cons 'cite
692 gnus-hidden-properties
))))
693 (gnus-add-wash-type 'cite
)
694 (gnus-add-text-properties-when
695 'article-type nil beg end
696 (cons 'article-type
(cons 'cite
697 gnus-hidden-properties
))))
698 (let ((gnus-article-mime-handle-alist-1 gnus-article-mime-handle-alist
))
699 (gnus-set-mode-line 'article
))
706 (gnus-article-add-button
710 gnus-cited-opened-text-button-line-format-spec
711 gnus-cited-closed-text-button-line-format-spec
))
713 `gnus-article-toggle-cited-text
716 'article-type
'annotation
)))))
718 (defun gnus-article-hide-citation-maybe (&optional arg force
)
719 "Toggle hiding of cited text that has an attribution line.
720 If given a negative prefix, always show; if given a positive prefix,
722 This will do nothing unless at least `gnus-cite-hide-percentage'
723 percent and at least `gnus-cite-hide-absolute' lines of the body is
724 cited text with attributions. When called interactively, these two
725 variables are ignored.
726 See also the documentation for `gnus-article-highlight-citation'."
727 (interactive (append (gnus-article-hidden-arg) '(force)))
728 (with-current-buffer gnus-article-buffer
729 (gnus-delete-wash-type 'cite
)
730 (unless (gnus-article-check-hidden-text 'cite arg
)
732 (gnus-cite-parse-maybe force
)
734 (let ((start (point))
735 (atts gnus-cite-attribution-alist
)
736 (buffer-read-only nil
)
737 (inhibit-point-motion-hooks t
)
740 (goto-char (point-max))
741 (gnus-article-search-signature)
742 (setq total
(count-lines start
(point)))
744 (setq hidden
(+ hidden
(length
745 (cdr (assoc (cdar atts
)
746 gnus-cite-prefix-alist
))))
749 (and (> (* 100 hidden
) (* gnus-cite-hide-percentage total
))
750 (> hidden gnus-cite-hide-absolute
)))
751 (gnus-article-hide-citation)))))))
753 (defun gnus-article-hide-citation-in-followups ()
754 "Hide cited text in non-root articles."
756 (with-current-buffer gnus-article-buffer
757 (let ((article (cdr gnus-article-current
)))
758 (unless (with-current-buffer gnus-summary-buffer
759 (gnus-article-displayed-root-p article
))
760 (gnus-article-hide-citation)))))
762 ;;; Internal functions:
764 (defun gnus-cite-parse-maybe (&optional force no-overlay
)
765 "Always parse the buffer."
767 ;;Reset parser information.
768 (setq gnus-cite-prefix-alist nil
769 gnus-cite-attribution-alist nil
770 gnus-cite-loose-prefix-alist nil
771 gnus-cite-loose-attribution-alist nil
)
773 (gnus-cite-delete-overlays))
774 ;; Parse if not too large.
775 (if (and gnus-cite-parse-max-size
776 (> (buffer-size) gnus-cite-parse-max-size
))
778 (setq gnus-cite-article
(cons (car gnus-article-current
)
779 (cdr gnus-article-current
)))
780 (gnus-cite-parse-wrapper)))
782 (defun gnus-cite-delete-overlays ()
783 (dolist (overlay gnus-cite-overlay-list
)
785 (when (or (not (overlay-end overlay
))
786 (and (>= (overlay-end overlay
) (point-min))
787 (<= (overlay-end overlay
) (point-max))))
788 (setq gnus-cite-overlay-list
(delete overlay gnus-cite-overlay-list
))
790 (delete-overlay overlay
))))))
792 (defun gnus-cite-parse-wrapper ()
793 ;; Wrap chopped gnus-cite-parse.
795 (let ((inhibit-point-motion-hooks t
))
797 (gnus-cite-parse-attributions))
801 (gnus-cite-connect-attributions))))
803 (defun gnus-cite-parse ()
804 ;; Parse and connect citation prefixes and attribution lines.
806 ;; Parse current buffer searching for citation prefixes.
807 (let ((line (1+ (count-lines (point-min) (point))))
810 (goto-char (point-max))
811 (gnus-article-search-signature)
813 (prefix-regexp (concat "^\\(" message-cite-prefix-regexp
"\\)"))
814 alist entry start begin end numbers prefix guess-limit
)
815 ;; Get all potential prefixes in `alist'.
816 (while (< (point) max
)
819 guess-limit
(progn (skip-chars-forward "^> \t\r\n") (point))
823 ;; Ignore standard Supercite attribution prefix.
824 (when (and (< guess-limit
(+ begin gnus-cite-max-prefix
))
825 (looking-at gnus-supercite-regexp
))
827 (setq end
(1+ (match-end 1)))
828 (setq end
(1+ begin
))))
829 ;; Ignore very long prefixes.
830 (when (> end
(+ begin gnus-cite-max-prefix
))
831 (setq end
(+ begin gnus-cite-max-prefix
)))
832 ;; Ignore quoted envelope From_.
833 (when (and gnus-cite-ignore-quoted-from
835 (setq case-fold-search nil
)
836 (looking-at ">From ")
837 (setq case-fold-search t
)))
838 (setq end
(1+ begin
)))
839 (while (re-search-forward prefix-regexp
(1- end
) t
)
841 (setq end
(match-end 0)
842 prefix
(buffer-substring begin end
))
843 (set-text-properties 0 (length prefix
) nil prefix
)
844 (setq entry
(assoc prefix alist
))
846 (setcdr entry
(cons line
(cdr entry
)))
847 (push (list prefix line
) alist
))
850 (setq line
(1+ line
)))
851 ;; Horrible special case for some Microsoft mailers.
852 (goto-char (point-min))
853 (setq start t begin nil entry nil
)
855 ;; Assume this search ends up at the beginning of a line.
856 (if (re-search-forward gnus-cite-unsightly-citation-regexp max t
)
858 (when (number-or-marker-p start
)
859 (setq begin
(count-lines (point-min) start
)
860 end
(count-lines (point-min) (match-beginning 0))))
861 (setq start
(match-end 0)))
862 (when (number-or-marker-p start
)
863 (setq begin
(count-lines (point-min) start
)
864 end
(count-lines (point-min) max
)))
868 ;; Need to do 1+ because we're in the bol.
869 (push (setq begin
(1+ begin
)) entry
))))
871 (push (cons "" entry
) alist
))
872 ;; We got all the potential prefixes. Now create
873 ;; `gnus-cite-prefix-alist' containing the oldest prefix for each
874 ;; line that appears at least `gnus-cite-minimum-match-count'
875 ;; times. First sort them by length. Longer is older.
876 (setq alist
(sort alist
(lambda (a b
)
877 (> (length (car a
)) (length (car b
))))))
879 (setq entry
(car alist
)
883 (cond ((null numbers
)
884 ;; No lines with this prefix that wasn't also part of
887 ((< (length numbers
) gnus-cite-minimum-match-count
)
888 ;; Too few lines with this prefix. We keep it a bit
889 ;; longer in case it is an exact match for an attribution
890 ;; line, but we don't remove the line from other
892 (push entry gnus-cite-prefix-alist
))
895 gnus-cite-prefix-alist
)
896 ;; Remove articles from other prefixes.
900 (setq current
(car loop
)
903 (gnus-set-difference (cdr current
) numbers
)))))))))
905 (defun gnus-cite-parse-attributions ()
907 ;; Parse attributions
908 (while (re-search-forward gnus-cite-attribution-suffix
(point-max) t
)
909 (let* ((start (match-beginning 0))
911 (wrote (count-lines (point-min) end
))
912 (prefix (gnus-cite-find-prefix wrote
))
913 ;; Check previous line for an attribution leader.
915 (beginning-of-line 1)
916 (when (looking-at gnus-supercite-secondary-regexp
)
917 (buffer-substring (match-beginning 1)
921 (and (re-search-backward gnus-cite-attribution-prefix
923 (beginning-of-line 0)
926 (not (re-search-forward gnus-cite-attribution-suffix
928 (count-lines (point-min) (1+ (point)))))))
932 ;; don't add duplicates
933 (let ((al (buffer-substring (save-excursion (beginning-of-line 0)
936 (when (not (assoc al al-alist
))
937 (push (list wrote in prefix tag
)
938 gnus-cite-loose-attribution-alist
)
939 (push (cons al t
) al-alist
)))))))
941 (defun gnus-cite-connect-attributions ()
942 ;; Connect attributions to citations
944 ;; No citations have been connected to attribution lines yet.
945 (setq gnus-cite-loose-prefix-alist
(append gnus-cite-prefix-alist nil
))
947 ;; Parse current buffer searching for attribution lines.
948 ;; Find exact supercite citations.
949 (gnus-cite-match-attributions 'small nil
953 (regexp-quote prefix
) "[ \t]*"
954 (regexp-quote tag
) ">"))))
955 ;; Find loose supercite citations after attributions.
956 (gnus-cite-match-attributions 'small t
962 ;; Find loose supercite citations anywhere.
963 (gnus-cite-match-attributions 'small nil
969 ;; Find nested citations after attributions.
970 (gnus-cite-match-attributions 'small-if-unique t
972 (concat "\\`" (regexp-quote prefix
) ".+")))
973 ;; Find nested citations anywhere.
974 (gnus-cite-match-attributions 'small nil
976 (concat "\\`" (regexp-quote prefix
) ".+")))
977 ;; Remove loose prefixes with too few lines.
978 (let ((alist gnus-cite-loose-prefix-alist
)
981 (setq entry
(car alist
)
983 (when (< (length (cdr entry
)) gnus-cite-minimum-match-count
)
984 (setq gnus-cite-prefix-alist
985 (delq entry gnus-cite-prefix-alist
)
986 gnus-cite-loose-prefix-alist
987 (delq entry gnus-cite-loose-prefix-alist
)))))
988 ;; Find flat attributions.
989 (gnus-cite-match-attributions 'first t nil
)
990 ;; Find any attributions (are we getting desperate yet?).
991 (gnus-cite-match-attributions 'first nil nil
))
993 (defun gnus-cite-match-attributions (sort after fun
)
994 ;; Match all loose attributions and citations (SORT AFTER FUN) .
996 ;; If SORT is `small', the citation with the shortest prefix will be
997 ;; used, if it is `first' the first prefix will be used, if it is
998 ;; `small-if-unique' the shortest prefix will be used if the
999 ;; attribution line does not share its own prefix with other
1000 ;; loose attribution lines, otherwise the first prefix will be used.
1002 ;; If AFTER is non-nil, only citations after the attribution line
1003 ;; will be considered.
1005 ;; If FUN is non-nil, it will be called with the arguments (WROTE
1006 ;; PREFIX TAG) and expected to return a regular expression. Only
1007 ;; citations whose prefix matches the regular expression will be
1010 ;; WROTE is the attribution line number.
1011 ;; PREFIX is the attribution line prefix.
1012 ;; TAG is the Supercite tag on the attribution line.
1013 (let ((atts gnus-cite-loose-attribution-alist
)
1014 (case-fold-search t
)
1015 att wrote in prefix tag regexp limit smallest best size
)
1017 (setq att
(car atts
)
1023 regexp
(if fun
(funcall fun prefix tag
) "")
1024 size
(cond ((eq sort
'small
) t
)
1025 ((eq sort
'first
) nil
)
1026 (t (< (length (gnus-cite-find-loose prefix
)) 2)))
1027 limit
(if after wrote -
1)
1030 (let ((cites gnus-cite-loose-prefix-alist
)
1031 cite candidate numbers first compare
)
1033 (setq cite
(car cites
)
1035 candidate
(car cite
)
1037 first
(apply 'min numbers
)
1038 compare
(if size
(length candidate
) first
))
1039 (and (> first limit
)
1041 (string-match regexp candidate
)
1042 (< compare smallest
)
1044 smallest compare
))))
1047 (setq gnus-cite-loose-attribution-alist
1048 (delq att gnus-cite-loose-attribution-alist
))
1049 (push (cons wrote
(car best
)) gnus-cite-attribution-alist
)
1051 (push (cons in
(car best
)) gnus-cite-attribution-alist
))
1052 (when (memq best gnus-cite-loose-prefix-alist
)
1053 (let ((loop gnus-cite-prefix-alist
)
1054 (numbers (cdr best
))
1056 (setq gnus-cite-loose-prefix-alist
1057 (delq best gnus-cite-loose-prefix-alist
))
1059 (setq current
(car loop
)
1061 (if (eq current best
)
1063 (setcdr current
(gnus-set-difference (cdr current
) numbers
))
1064 (when (null (cdr current
))
1065 (setq gnus-cite-loose-prefix-alist
1066 (delq current gnus-cite-loose-prefix-alist
)
1067 atts
(delq current atts
)))))))))))
1069 (defun gnus-cite-find-loose (prefix)
1070 ;; Return a list of loose attribution lines prefixed by PREFIX.
1071 (let* ((atts gnus-cite-loose-attribution-alist
)
1074 (setq att
(car atts
)
1077 (when (string-equal (gnus-cite-find-prefix line
) prefix
)
1081 (defun gnus-cite-add-face (number prefix face
)
1082 ;; At line NUMBER, ignore PREFIX and add FACE to the rest of the line.
1084 (let ((inhibit-point-motion-hooks t
)
1086 (goto-char (point-min))
1087 (when (zerop (forward-line (1- number
)))
1088 (forward-char (length prefix
))
1089 (skip-chars-forward " \t")
1092 (skip-chars-backward " \t")
1095 (push (setq overlay
(make-overlay from to nil t
))
1096 gnus-cite-overlay-list
)
1097 (overlay-put overlay
'evaporate t
)
1098 (overlay-put overlay
'face face
))))))
1100 (defun gnus-cite-toggle (prefix)
1101 (with-current-buffer gnus-article-buffer
1102 (gnus-cite-parse-maybe nil t
)
1103 (let ((buffer-read-only nil
)
1104 (numbers (cdr (assoc prefix gnus-cite-prefix-alist
)))
1105 (inhibit-point-motion-hooks t
)
1108 (setq number
(car numbers
)
1109 numbers
(cdr numbers
))
1110 (goto-char (point-min))
1111 (forward-line (1- number
))
1112 (cond ((get-text-property (point) 'invisible
)
1113 ;; Can't remove 'cite from g-a-wash-types here because
1114 ;; multiple citations may be hidden -jas
1115 (remove-text-properties (point) (progn (forward-line 1) (point))
1116 gnus-hidden-properties
))
1117 ((assq number gnus-cite-attribution-alist
))
1119 (gnus-add-wash-type 'cite
)
1120 (add-text-properties
1121 (point) (progn (forward-line 1) (point))
1122 (nconc (list 'article-type
'cite
)
1123 gnus-hidden-properties
))))
1124 (let ((gnus-article-mime-handle-alist-1
1125 gnus-article-mime-handle-alist
))
1126 (gnus-set-mode-line 'article
))))))
1128 (defun gnus-cite-find-prefix (line)
1129 ;; Return citation prefix for LINE.
1130 (let ((alist gnus-cite-prefix-alist
)
1134 (setq entry
(car alist
)
1136 (when (memq line
(cdr entry
))
1137 (setq prefix
(car entry
))))
1140 (defun gnus-cite-localize ()
1141 "Make the citation variables local to the article buffer."
1142 (let ((vars '(gnus-cite-article
1143 gnus-cite-overlay-list gnus-cite-prefix-alist
1144 gnus-cite-attribution-alist gnus-cite-loose-prefix-alist
1145 gnus-cite-loose-attribution-alist
)))
1147 (make-local-variable (pop vars
)))))
1149 ;; Highlighting of different citation levels in message-mode.
1150 ;; - message-cite-prefix will be overridden if this is enabled.
1152 (defvar gnus-message-max-citation-depth
1153 (length gnus-cite-face-list
)
1154 "Maximum supported level of citation.")
1156 (defvar gnus-message-cite-prefix-regexp
1157 (concat "^\\(?:" message-cite-prefix-regexp
"\\)"))
1159 (defun gnus-message-search-citation-line (limit)
1160 "Search for a cited line and set match data accordingly.
1161 Returns nil if there is no such line before LIMIT, t otherwise."
1162 (when (re-search-forward gnus-message-cite-prefix-regexp limit t
)
1163 (let ((cdepth (min (length (apply 'concat
1165 (match-string-no-properties 0)
1166 "[ \t [:alnum:]]+")))
1167 gnus-message-max-citation-depth
))
1168 (mlist (make-list (* (1+ gnus-message-max-citation-depth
) 2) nil
))
1169 (start (point-at-bol))
1170 (end (point-at-eol)))
1171 (setcar mlist start
)
1172 (setcar (cdr mlist
) end
)
1173 (setcar (nthcdr (* cdepth
2) mlist
) start
)
1174 (setcar (nthcdr (1+ (* cdepth
2)) mlist
) end
)
1175 (set-match-data mlist
))
1178 (defvar gnus-message-citation-keywords
1179 ;; eval-when-compile ;; This breaks in XEmacs
1180 `((gnus-message-search-citation-line
1183 ;; (require 'gnus-cite)
1184 (dolist (face gnus-cite-face-list
(nreverse list
))
1185 (push (list count
(list 'quote face
) 'prepend t
) list
)
1186 (setq count
(1+ count
)))))) ;;
1187 "Keywords for highlighting different levels of message citations.")
1189 (defvar font-lock-defaults-computed
)
1190 (defvar font-lock-keywords
)
1191 (defvar font-lock-set-defaults
)
1193 (autoload 'font-lock-set-defaults
"font-lock")
1195 (define-minor-mode gnus-message-citation-mode
1196 "Minor mode providing more font-lock support for nested citations.
1197 When enabled, it automatically turns on `font-lock-mode'."
1201 (when (eq major-mode
'message-mode
) ;FIXME: Use derived-mode-p.
1202 ;; FIXME: Use font-lock-add-keywords!
1203 (let ((defaults (car font-lock-defaults
))
1206 (setq default
(if (consp defaults
)
1210 (setq defaults nil
))))
1211 (if gnus-message-citation-mode
1212 ;; `gnus-message-citation-keywords' should be the last
1213 ;; elements of the keywords because the others are unlikely
1214 ;; to have the OVERRIDE flags -- XEmacs applies a keyword
1215 ;; having no OVERRIDE flag to matched text even if it has
1216 ;; already other faces, while Emacs doesn't.
1217 (set (make-local-variable default
)
1218 (append (default-value default
)
1219 gnus-message-citation-keywords
))
1220 (kill-local-variable default
))))
1221 ;; Force `font-lock-set-defaults' to update `font-lock-keywords'.
1222 (setq font-lock-set-defaults nil
)
1223 (font-lock-set-defaults)
1226 (gnus-message-citation-mode (font-lock-mode 1)))))
1228 (defun turn-on-gnus-message-citation-mode ()
1229 "Turn on `gnus-message-citation-mode'."
1230 (gnus-message-citation-mode 1))
1231 (defun turn-off-gnus-message-citation-mode ()
1232 "Turn off `gnus-message-citation-mode'."
1233 (gnus-message-citation-mode -
1))
1235 (provide 'gnus-cite
)
1241 ;;; gnus-cite.el ends here