1 ;;; gnus-cite.el --- parse citations in articles for Gnus
3 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
4 ;; 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 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
29 (eval-when-compile (require 'cl))
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."
130 (defcustom gnus-cite-ignore-quoted-from t
131 "Non-nil means don't regard lines beginning with \">From \" as cited text.
132 Those lines may have been quoted by MTAs in order not to mix up with
133 the envelope From line."
137 (defface gnus-cite-attribution-face '((t
139 "Face used for attribution lines.")
141 (defcustom gnus-cite-attribution-face 'gnus-cite-attribution-face
142 "Face used for attribution lines.
143 It is merged with the face for the cited text belonging to the attribution."
147 (defface gnus-cite-face-1 '((((class color)
149 (:foreground "light blue"))
152 (:foreground "MidnightBlue"))
157 (defface gnus-cite-face-2 '((((class color)
159 (:foreground "light cyan"))
162 (:foreground "firebrick"))
167 (defface gnus-cite-face-3 '((((class color)
169 (:foreground "light yellow"))
172 (:foreground "dark green"))
177 (defface gnus-cite-face-4 '((((class color)
179 (:foreground "light pink"))
182 (:foreground "OrangeRed"))
187 (defface gnus-cite-face-5 '((((class color)
189 (:foreground "pale green"))
192 (:foreground "dark khaki"))
197 (defface gnus-cite-face-6 '((((class color)
199 (:foreground "beige"))
202 (:foreground "dark violet"))
207 (defface gnus-cite-face-7 '((((class color)
209 (:foreground "orange"))
212 (:foreground "SteelBlue4"))
217 (defface gnus-cite-face-8 '((((class color)
219 (:foreground "magenta"))
222 (:foreground "magenta"))
227 (defface gnus-cite-face-9 '((((class color)
229 (:foreground "violet"))
232 (:foreground "violet"))
237 (defface gnus-cite-face-10 '((((class color)
239 (:foreground "medium purple"))
242 (:foreground "medium purple"))
247 (defface gnus-cite-face-11 '((((class color)
249 (:foreground "turquoise"))
252 (:foreground "turquoise"))
257 (defcustom gnus-cite-face-list
258 '(gnus-cite-face-1 gnus-cite-face-2 gnus-cite-face-3 gnus-cite-face-4
259 gnus-cite-face-5 gnus-cite-face-6 gnus-cite-face-7 gnus-cite-face-8
260 gnus-cite-face-9 gnus-cite-face-10 gnus-cite-face-11)
261 "*List of faces used for highlighting citations.
263 When there are citations from multiple articles in the same message,
264 Gnus will try to give each citation from each article its own face.
265 This should make it easier to see who wrote what."
267 :type '(repeat face))
269 (defcustom gnus-cite-hide-percentage 50
270 "Only hide excess citation if above this percentage of the body."
274 (defcustom gnus-cite-hide-absolute 10
275 "Only hide excess citation if above this number of lines in the body."
279 (defcustom gnus-cite-blank-line-after-header t
280 "If non-nil, put a blank line between the citation header and the button."
285 ;; This has to go here because its default value depends on
286 ;; gnus-cite-face-list.
287 (defcustom gnus-article-boring-faces (cons 'gnus-signature-face
289 "List of faces that are not worth reading.
290 If an article has more pages below the one you are looking at, but
291 nothing on those pages is a word of at least three letters that is not
292 in a boring face, then the pages will be skipped."
295 :group 'gnus-article-hiding)
297 ;;; Internal Variables:
299 (defvar gnus-cite-article nil)
300 (defvar gnus-cite-overlay-list nil)
302 (defvar gnus-cite-prefix-alist nil)
303 ;; Alist of citation prefixes.
304 ;; The cdr is a list of lines with that prefix.
306 (defvar gnus-cite-attribution-alist nil)
307 ;; Alist of attribution lines.
308 ;; The car is a line number.
309 ;; The cdr is the prefix for the citation started by that line.
311 (defvar gnus-cite-loose-prefix-alist nil)
312 ;; Alist of citation prefixes that have no matching attribution.
313 ;; The cdr is a list of lines with that prefix.
315 (defvar gnus-cite-loose-attribution-alist nil)
316 ;; Alist of attribution lines that have no matching citation.
317 ;; Each member has the form (WROTE IN PREFIX TAG), where
318 ;; WROTE: is the attribution line number
319 ;; IN: is the line number of the previous line if part of the same attribution,
320 ;; PREFIX: Is the citation prefix of the attribution line(s), and
321 ;; TAG: Is a Supercite tag, if any.
323 (defvar gnus-cited-opened-text-button-line-format-alist
324 `((?b (marker-position beg) ?d)
325 (?e (marker-position end) ?d)
326 (?n (count-lines beg end) ?d)
327 (?l (- end beg) ?d)))
328 (defvar gnus-cited-opened-text-button-line-format-spec nil)
329 (defvar gnus-cited-closed-text-button-line-format-alist
330 gnus-cited-opened-text-button-line-format-alist)
331 (defvar gnus-cited-closed-text-button-line-format-spec nil)
336 (defun gnus-article-highlight-citation (&optional force)
337 "Highlight cited text.
338 Each citation in the article will be highlighted with a different face.
339 The faces are taken from `gnus-cite-face-list'.
340 Attribution lines are highlighted with the same face as the
341 corresponding citation merged with `gnus-cite-attribution-face'.
343 Text is considered cited if at least `gnus-cite-minimum-match-count'
344 lines matches `message-cite-prefix-regexp' with the same prefix.
346 Lines matching `gnus-cite-attribution-suffix' and perhaps
347 `gnus-cite-attribution-prefix' are considered attribution lines."
348 (interactive (list 'force))
350 (set-buffer gnus-article-buffer)
351 (gnus-cite-parse-maybe force)
352 (let ((buffer-read-only nil)
353 (alist gnus-cite-prefix-alist)
354 (faces gnus-cite-face-list)
355 (inhibit-point-motion-hooks t)
356 face entry prefix skip numbers number face-alist)
357 ;; Loop through citation prefixes.
359 (setq entry (car alist)
364 faces (or (cdr faces) gnus-cite-face-list)
365 face-alist (cons (cons prefix face) face-alist))
367 (setq number (car numbers)
368 numbers (cdr numbers))
369 (and (not (assq number gnus-cite-attribution-alist))
370 (not (assq number gnus-cite-loose-attribution-alist))
371 (gnus-cite-add-face number prefix face))))
372 ;; Loop through attribution lines.
373 (setq alist gnus-cite-attribution-alist)
375 (setq entry (car alist)
379 skip (gnus-cite-find-prefix number)
380 face (cdr (assoc prefix face-alist)))
381 ;; Add attribution button.
382 (goto-char (point-min))
383 (forward-line (1- number))
384 (when (re-search-forward gnus-cite-attribution-suffix
387 (gnus-article-add-button (match-beginning 1) (match-end 1)
388 'gnus-cite-toggle prefix))
389 ;; Highlight attribution line.
390 (gnus-cite-add-face number skip face)
391 (gnus-cite-add-face number skip gnus-cite-attribution-face))
392 ;; Loop through attribution lines.
393 (setq alist gnus-cite-loose-attribution-alist)
395 (setq entry (car alist)
398 skip (gnus-cite-find-prefix number))
399 (gnus-cite-add-face number skip gnus-cite-attribution-face)))))
401 (defun gnus-dissect-cited-text ()
402 "Dissect the article buffer looking for cited text."
404 (set-buffer gnus-article-buffer)
405 (gnus-cite-parse-maybe nil t)
406 (let ((alist gnus-cite-prefix-alist)
407 prefix numbers number marks m)
408 ;; Loop through citation prefixes.
410 (setq numbers (pop alist)
411 prefix (pop numbers))
413 (setq number (pop numbers))
414 (goto-char (point-min))
415 (forward-line number)
416 (push (cons (point-marker) "") marks)
418 (= (1- number) (car numbers)))
419 (setq number (pop numbers)))
420 (goto-char (point-min))
421 (forward-line (1- number))
422 (push (cons (point-marker) prefix) marks)))
423 ;; Skip to the beginning of the body.
425 (push (cons (point-marker) "") marks)
426 ;; Find the end of the body.
427 (goto-char (point-max))
428 (gnus-article-search-signature)
429 (push (cons (point-marker) "") marks)
431 (setq marks (sort marks 'car-less-than-car))
432 (let ((omarks marks))
435 (if (= (caar omarks) (caadr omarks))
437 (unless (equal (cdar omarks) "")
438 (push (car omarks) marks))
439 (unless (equal (cdadr omarks) "")
440 (push (cadr omarks) marks))
441 (unless (and (equal (cdar omarks) "")
442 (equal (cdadr omarks) "")
444 (setq omarks (cdr omarks))))
445 (push (car omarks) marks))
446 (setq omarks (cdr omarks)))
448 (push (car omarks) marks))
449 (setq marks (setq m (nreverse marks)))
451 (if (and (equal (cdadr m) "")
452 (equal (cdar m) (cdaddr m))
453 (goto-char (caadr m))
455 (= (point) (caaddr m)))
460 (defun gnus-article-fill-cited-article (&optional force width)
461 "Do word wrapping in the current article.
462 If WIDTH (the numerical prefix), use that text width when filling."
463 (interactive (list t current-prefix-arg))
465 (set-buffer gnus-article-buffer)
466 (let ((buffer-read-only nil)
467 (inhibit-point-motion-hooks t)
468 (marks (gnus-dissect-cited-text))
469 (adaptive-fill-mode nil)
471 (fill-column (if width (prefix-numeric-value width) fill-column)))
474 (narrow-to-region (caar marks) (caadr marks))
475 (let ((adaptive-fill-regexp
476 (concat "^" (regexp-quote (cdar marks)) " *"))
478 (if (string= (cdar marks) "") ""
479 (concat (cdar marks) " ")))
481 (fill-region (point-min) (point-max)))
482 (set-marker (caar marks) nil)
483 (setq marks (cdr marks)))
485 (set-marker (caar marks) nil))
486 ;; All this information is now incorrect.
487 (setq gnus-cite-prefix-alist nil
488 gnus-cite-attribution-alist nil
489 gnus-cite-loose-prefix-alist nil
490 gnus-cite-loose-attribution-alist nil
491 gnus-cite-article nil)))))
493 (defun gnus-article-hide-citation (&optional arg force)
494 "Toggle hiding of all cited text except attribution lines.
495 See the documentation for `gnus-article-highlight-citation'.
496 If given a negative prefix, always show; if given a positive prefix,
498 (interactive (append (gnus-article-hidden-arg) (list 'force)))
499 (gnus-set-format 'cited-opened-text-button t)
500 (gnus-set-format 'cited-closed-text-button t)
502 (set-buffer gnus-article-buffer)
503 (let ((buffer-read-only nil)
505 (inhibit-point-motion-hooks t)
506 (props (nconc (list 'article-type 'cite)
507 gnus-hidden-properties))
511 (text-property-any point (point-max)
513 'gnus-article-toggle-cited-text))
516 (gnus-article-toggle-cited-text
517 (get-text-property point 'gnus-data) arg)
519 (setq point (point)))
521 (setq marks (gnus-dissect-cited-text))
525 (while (and marks (string= (cdar marks) ""))
526 (setq marks (cdr marks)))
528 (setq beg (caar marks)))
529 (while (and marks (not (string= (cdar marks) "")))
530 (setq marks (cdr marks)))
532 (setq end (caar marks)))
533 ;; Skip past lines we want to leave visible.
534 (when (and beg end gnus-cited-lines-visible)
536 (forward-line (if (consp gnus-cited-lines-visible)
537 (car gnus-cited-lines-visible)
538 gnus-cited-lines-visible))
541 (setq beg (point-marker))
542 (when (consp gnus-cited-lines-visible)
544 (forward-line (- (cdr gnus-cited-lines-visible)))
547 (setq end (point-marker))))))
549 (gnus-add-wash-type 'cite)
550 ;; We use markers for the end-points to facilitate later
551 ;; wrapping and mangling of text.
552 (setq beg (set-marker (make-marker) beg)
553 end (set-marker (make-marker) end))
554 (gnus-add-text-properties-when 'article-type nil beg end props)
556 (when (and gnus-cite-blank-line-after-header
557 (not (save-excursion (search-backward "\n\n" nil t))))
560 (setq start (point-marker))
562 (gnus-article-add-button
564 (progn (eval gnus-cited-closed-text-button-line-format-spec)
566 `gnus-article-toggle-cited-text
567 (list (cons beg end) start))
569 'article-type 'annotation)
570 (set-marker beg (point))))))))
572 (defun gnus-article-toggle-cited-text (args &optional arg)
573 "Toggle hiding the text in REGION.
574 ARG can be nil or a number. Positive means hide, negative
575 means show, nil means toggle."
576 (let* ((region (car args))
581 (text-property-any beg (1- end) 'article-type 'cite))
582 (inhibit-point-motion-hooks t)
586 (and (> arg 0) (not hidden))
587 (and (< arg 0) hidden))
590 ;; Can't remove 'cite from g-a-wash-types here because
591 ;; multiple citations may be hidden -jas
592 (gnus-remove-text-properties-when
593 'article-type 'cite beg end
594 (cons 'article-type (cons 'cite
595 gnus-hidden-properties))))
596 (gnus-add-wash-type 'cite)
597 (gnus-add-text-properties-when
598 'article-type nil beg end
599 (cons 'article-type (cons 'cite
600 gnus-hidden-properties))))
601 (let ((gnus-article-mime-handle-alist-1 gnus-article-mime-handle-alist))
602 (gnus-set-mode-line 'article))
609 (gnus-article-add-button
613 gnus-cited-opened-text-button-line-format-spec
614 gnus-cited-closed-text-button-line-format-spec))
616 `gnus-article-toggle-cited-text
619 'article-type 'annotation)))))
621 (defun gnus-article-hide-citation-maybe (&optional arg force)
622 "Toggle hiding of cited text that has an attribution line.
623 If given a negative prefix, always show; if given a positive prefix,
625 This will do nothing unless at least `gnus-cite-hide-percentage'
626 percent and at least `gnus-cite-hide-absolute' lines of the body is
627 cited text with attributions. When called interactively, these two
628 variables are ignored.
629 See also the documentation for `gnus-article-highlight-citation'."
630 (interactive (append (gnus-article-hidden-arg) '(force)))
631 (with-current-buffer gnus-article-buffer
632 (gnus-delete-wash-type 'cite)
633 (unless (gnus-article-check-hidden-text 'cite arg)
635 (gnus-cite-parse-maybe force)
637 (let ((start (point))
638 (atts gnus-cite-attribution-alist)
639 (buffer-read-only nil)
640 (inhibit-point-motion-hooks t)
643 (goto-char (point-max))
644 (gnus-article-search-signature)
645 (setq total (count-lines start (point)))
647 (setq hidden (+ hidden (length (cdr (assoc (cdar atts)
648 gnus-cite-prefix-alist))))
651 (and (> (* 100 hidden) (* gnus-cite-hide-percentage total))
652 (> hidden gnus-cite-hide-absolute)))
653 (gnus-add-wash-type 'cite)
654 (setq atts gnus-cite-attribution-alist)
656 (setq total (cdr (assoc (cdar atts) gnus-cite-prefix-alist))
659 (setq hidden (car total)
661 (goto-char (point-min))
662 (forward-line (1- hidden))
663 (unless (assq hidden gnus-cite-attribution-alist)
664 (gnus-add-text-properties
665 (point) (progn (forward-line 1) (point))
666 (nconc (list 'article-type 'cite)
667 gnus-hidden-properties)))))))))
668 (gnus-set-mode-line 'article)))
670 (defun gnus-article-hide-citation-in-followups ()
671 "Hide cited text in non-root articles."
674 (set-buffer gnus-article-buffer)
675 (let ((article (cdr gnus-article-current)))
676 (unless (save-excursion
677 (set-buffer gnus-summary-buffer)
678 (gnus-article-displayed-root-p article))
679 (gnus-article-hide-citation)))))
681 ;;; Internal functions:
683 (defun gnus-cite-parse-maybe (&optional force no-overlay)
684 "Always parse the buffer."
686 ;;Reset parser information.
687 (setq gnus-cite-prefix-alist nil
688 gnus-cite-attribution-alist nil
689 gnus-cite-loose-prefix-alist nil
690 gnus-cite-loose-attribution-alist nil)
692 (gnus-cite-delete-overlays))
693 ;; Parse if not too large.
694 (if (and gnus-cite-parse-max-size
695 (> (buffer-size) gnus-cite-parse-max-size))
697 (setq gnus-cite-article (cons (car gnus-article-current)
698 (cdr gnus-article-current)))
699 (gnus-cite-parse-wrapper)))
701 (defun gnus-cite-delete-overlays ()
702 (dolist (overlay gnus-cite-overlay-list)
704 (when (or (not (gnus-overlay-end overlay))
705 (and (>= (gnus-overlay-end overlay) (point-min))
706 (<= (gnus-overlay-end overlay) (point-max))))
707 (setq gnus-cite-overlay-list (delete overlay gnus-cite-overlay-list))
709 (gnus-delete-overlay overlay))))))
711 (defun gnus-cite-parse-wrapper ()
712 ;; Wrap chopped gnus-cite-parse.
714 (let ((inhibit-point-motion-hooks t))
716 (gnus-cite-parse-attributions))
720 (gnus-cite-connect-attributions))))
722 (defun gnus-cite-parse ()
723 ;; Parse and connect citation prefixes and attribution lines.
725 ;; Parse current buffer searching for citation prefixes.
726 (let ((line (1+ (count-lines (point-min) (point))))
729 (goto-char (point-max))
730 (gnus-article-search-signature)
732 (prefix-regexp (concat "^\\(" message-cite-prefix-regexp "\\)"))
733 alist entry start begin end numbers prefix guess-limit)
734 ;; Get all potential prefixes in `alist'.
735 (while (< (point) max)
738 guess-limit (progn (skip-chars-forward "^> \t\r\n") (point))
742 ;; Ignore standard Supercite attribution prefix.
743 (when (and (< guess-limit (+ begin gnus-cite-max-prefix))
744 (looking-at gnus-supercite-regexp))
746 (setq end (1+ (match-end 1)))
747 (setq end (1+ begin))))
748 ;; Ignore very long prefixes.
749 (when (> end (+ begin gnus-cite-max-prefix))
750 (setq end (+ begin gnus-cite-max-prefix)))
751 ;; Ignore quoted envelope From_.
752 (when (and gnus-cite-ignore-quoted-from
754 (setq case-fold-search nil)
755 (looking-at ">From ")
756 (setq case-fold-search t)))
757 (setq end (1+ begin)))
758 (while (re-search-forward prefix-regexp (1- end) t)
760 (setq end (match-end 0)
761 prefix (buffer-substring begin end))
762 (set-text-properties 0 (length prefix) nil prefix)
763 (setq entry (assoc prefix alist))
765 (setcdr entry (cons line (cdr entry)))
766 (push (list prefix line) alist))
769 (setq line (1+ line)))
770 ;; Horrible special case for some Microsoft mailers.
771 (goto-char (point-min))
772 (when (re-search-forward gnus-cite-unsightly-citation-regexp max t)
773 (setq begin (count-lines (point-min) (point)))
774 (setq end (count-lines (point-min) max))
778 (setq begin (1+ begin)))
779 (push (cons "" entry) alist))
780 ;; We got all the potential prefixes. Now create
781 ;; `gnus-cite-prefix-alist' containing the oldest prefix for each
782 ;; line that appears at least `gnus-cite-minimum-match-count'
783 ;; times. First sort them by length. Longer is older.
784 (setq alist (sort alist (lambda (a b)
785 (> (length (car a)) (length (car b))))))
787 (setq entry (car alist)
791 (cond ((null numbers)
792 ;; No lines with this prefix that wasn't also part of
795 ((< (length numbers) gnus-cite-minimum-match-count)
796 ;; Too few lines with this prefix. We keep it a bit
797 ;; longer in case it is an exact match for an attribution
798 ;; line, but we don't remove the line from other
800 (push entry gnus-cite-prefix-alist))
803 gnus-cite-prefix-alist)
804 ;; Remove articles from other prefixes.
808 (setq current (car loop)
811 (gnus-set-difference (cdr current) numbers)))))))))
813 (defun gnus-cite-parse-attributions ()
815 ;; Parse attributions
816 (while (re-search-forward gnus-cite-attribution-suffix (point-max) t)
817 (let* ((start (match-beginning 0))
819 (wrote (count-lines (point-min) end))
820 (prefix (gnus-cite-find-prefix wrote))
821 ;; Check previous line for an attribution leader.
823 (beginning-of-line 1)
824 (when (looking-at gnus-supercite-secondary-regexp)
825 (buffer-substring (match-beginning 1)
829 (and (re-search-backward gnus-cite-attribution-prefix
831 (beginning-of-line 0)
834 (not (re-search-forward gnus-cite-attribution-suffix
836 (count-lines (point-min) (1+ (point)))))))
840 ;; don't add duplicates
841 (let ((al (buffer-substring (save-excursion (beginning-of-line 0)
844 (when (not (assoc al al-alist))
845 (push (list wrote in prefix tag)
846 gnus-cite-loose-attribution-alist)
847 (push (cons al t) al-alist)))))))
849 (defun gnus-cite-connect-attributions ()
850 ;; Connect attributions to citations
852 ;; No citations have been connected to attribution lines yet.
853 (setq gnus-cite-loose-prefix-alist (append gnus-cite-prefix-alist nil))
855 ;; Parse current buffer searching for attribution lines.
856 ;; Find exact supercite citations.
857 (gnus-cite-match-attributions 'small nil
861 (regexp-quote prefix) "[ \t]*"
862 (regexp-quote tag) ">"))))
863 ;; Find loose supercite citations after attributions.
864 (gnus-cite-match-attributions 'small t
870 ;; Find loose supercite citations anywhere.
871 (gnus-cite-match-attributions 'small nil
877 ;; Find nested citations after attributions.
878 (gnus-cite-match-attributions 'small-if-unique t
880 (concat "\\`" (regexp-quote prefix) ".+")))
881 ;; Find nested citations anywhere.
882 (gnus-cite-match-attributions 'small nil
884 (concat "\\`" (regexp-quote prefix) ".+")))
885 ;; Remove loose prefixes with too few lines.
886 (let ((alist gnus-cite-loose-prefix-alist)
889 (setq entry (car alist)
891 (when (< (length (cdr entry)) gnus-cite-minimum-match-count)
892 (setq gnus-cite-prefix-alist
893 (delq entry gnus-cite-prefix-alist)
894 gnus-cite-loose-prefix-alist
895 (delq entry gnus-cite-loose-prefix-alist)))))
896 ;; Find flat attributions.
897 (gnus-cite-match-attributions 'first t nil)
898 ;; Find any attributions (are we getting desperate yet?).
899 (gnus-cite-match-attributions 'first nil nil))
901 (defun gnus-cite-match-attributions (sort after fun)
902 ;; Match all loose attributions and citations (SORT AFTER FUN) .
904 ;; If SORT is `small', the citation with the shortest prefix will be
905 ;; used, if it is `first' the first prefix will be used, if it is
906 ;; `small-if-unique' the shortest prefix will be used if the
907 ;; attribution line does not share its own prefix with other
908 ;; loose attribution lines, otherwise the first prefix will be used.
910 ;; If AFTER is non-nil, only citations after the attribution line
911 ;; will be considered.
913 ;; If FUN is non-nil, it will be called with the arguments (WROTE
914 ;; PREFIX TAG) and expected to return a regular expression. Only
915 ;; citations whose prefix matches the regular expression will be
918 ;; WROTE is the attribution line number.
919 ;; PREFIX is the attribution line prefix.
920 ;; TAG is the Supercite tag on the attribution line.
921 (let ((atts gnus-cite-loose-attribution-alist)
923 att wrote in prefix tag regexp limit smallest best size)
931 regexp (if fun (funcall fun prefix tag) "")
932 size (cond ((eq sort 'small) t)
933 ((eq sort 'first) nil)
934 (t (< (length (gnus-cite-find-loose prefix)) 2)))
935 limit (if after wrote -1)
938 (let ((cites gnus-cite-loose-prefix-alist)
939 cite candidate numbers first compare)
941 (setq cite (car cites)
945 first (apply 'min numbers)
946 compare (if size (length candidate) first))
949 (string-match regexp candidate)
955 (setq gnus-cite-loose-attribution-alist
956 (delq att gnus-cite-loose-attribution-alist))
957 (push (cons wrote (car best)) gnus-cite-attribution-alist)
959 (push (cons in (car best)) gnus-cite-attribution-alist))
960 (when (memq best gnus-cite-loose-prefix-alist)
961 (let ((loop gnus-cite-prefix-alist)
964 (setq gnus-cite-loose-prefix-alist
965 (delq best gnus-cite-loose-prefix-alist))
967 (setq current (car loop)
969 (if (eq current best)
971 (setcdr current (gnus-set-difference (cdr current) numbers))
972 (when (null (cdr current))
973 (setq gnus-cite-loose-prefix-alist
974 (delq current gnus-cite-loose-prefix-alist)
975 atts (delq current atts)))))))))))
977 (defun gnus-cite-find-loose (prefix)
978 ;; Return a list of loose attribution lines prefixed by PREFIX.
979 (let* ((atts gnus-cite-loose-attribution-alist)
985 (when (string-equal (gnus-cite-find-prefix line) prefix)
989 (defun gnus-cite-add-face (number prefix face)
990 ;; At line NUMBER, ignore PREFIX and add FACE to the rest of the line.
992 (let ((inhibit-point-motion-hooks t)
994 (goto-char (point-min))
995 (when (zerop (forward-line (1- number)))
996 (forward-char (length prefix))
997 (skip-chars-forward " \t")
1000 (skip-chars-backward " \t")
1003 (push (setq overlay (gnus-make-overlay from to))
1004 gnus-cite-overlay-list)
1005 (gnus-overlay-put overlay 'face face))))))
1007 (defun gnus-cite-toggle (prefix)
1009 (set-buffer gnus-article-buffer)
1010 (gnus-cite-parse-maybe nil t)
1011 (let ((buffer-read-only nil)
1012 (numbers (cdr (assoc prefix gnus-cite-prefix-alist)))
1013 (inhibit-point-motion-hooks t)
1016 (setq number (car numbers)
1017 numbers (cdr numbers))
1018 (goto-char (point-min))
1019 (forward-line (1- number))
1020 (cond ((get-text-property (point) 'invisible)
1021 ;; Can't remove 'cite from g-a-wash-types here because
1022 ;; multiple citations may be hidden -jas
1023 (remove-text-properties (point) (progn (forward-line 1) (point))
1024 gnus-hidden-properties))
1025 ((assq number gnus-cite-attribution-alist))
1027 (gnus-add-wash-type 'cite)
1028 (gnus-add-text-properties
1029 (point) (progn (forward-line 1) (point))
1030 (nconc (list 'article-type 'cite)
1031 gnus-hidden-properties))))
1032 (let ((gnus-article-mime-handle-alist-1
1033 gnus-article-mime-handle-alist))
1034 (gnus-set-mode-line 'article))))))
1036 (defun gnus-cite-find-prefix (line)
1037 ;; Return citation prefix for LINE.
1038 (let ((alist gnus-cite-prefix-alist)
1042 (setq entry (car alist)
1044 (when (memq line (cdr entry))
1045 (setq prefix (car entry))))
1048 (defun gnus-cite-localize ()
1049 "Make the citation variables local to the article buffer."
1050 (let ((vars '(gnus-cite-article
1051 gnus-cite-overlay-list gnus-cite-prefix-alist
1052 gnus-cite-attribution-alist gnus-cite-loose-prefix-alist
1053 gnus-cite-loose-attribution-alist)))
1055 (make-local-variable (pop vars)))))
1057 (defun gnus-cited-line-p ()
1058 "Say whether the current line is a cited line."
1062 (dolist (prefix (mapcar 'car gnus-cite-prefix-alist))
1063 (when (string= (buffer-substring (point) (+ (length prefix) (point)))
1070 (provide 'gnus-cite)
1073 ;; coding: iso-8859-1
1076 ;;; arch-tag: 1997b044-6067-471e-8c8f-dc903093098a
1077 ;;; gnus-cite.el ends here