1 ;;; gnus-cite.el --- parse citations in articles for Gnus
3 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
5 ;; Free Software Foundation, Inc.
7 ;; Author: Per Abhiddenware
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
30 (eval-when-compile (require 'cl))
31 (eval-when-compile (require 'static))
36 (require 'message) ; for message-cite-prefix-regexp
40 (defgroup gnus-cite nil
43 :link '(custom-manual "(gnus)Article Highlighting")
46 (defcustom gnus-cited-opened-text-button-line-format "%(%{[-]%}%)\n"
47 "Format of opened cited text buttons."
51 (defcustom gnus-cited-closed-text-button-line-format "%(%{[+]%}%)\n"
52 "Format of closed cited text buttons."
56 (defcustom gnus-cited-lines-visible nil
57 "The number of lines of hidden cited text to remain visible.
58 Or a pair (cons) of numbers which are the number of lines at the top
59 and bottom of the text, respectively, to remain visible."
61 :type '(choice (const :tag "none" nil)
63 (cons :tag "Top and Bottom" integer integer)))
65 (defcustom gnus-cite-parse-max-size 25000
66 "Maximum article size (in bytes) where parsing citations is allowed.
67 Set it to nil to parse all articles."
69 :type '(choice (const :tag "all" nil)
72 (defcustom gnus-cite-max-prefix 20
73 "Maximum possible length for a citation prefix."
77 (defcustom gnus-supercite-regexp
78 (concat "^\\(" message-cite-prefix-regexp "\\)? *"
79 ">>>>> +\"\\([^\"\n]+\\)\" +==")
80 "*Regexp matching normal Supercite attribution lines.
81 The first grouping must match prefixes added by other packages."
85 (defcustom gnus-supercite-secondary-regexp "^.*\"\\([^\"\n]+\\)\" +=="
86 "Regexp matching mangled Supercite attribution lines.
87 The first regexp group should match the Supercite attribution."
91 (defcustom gnus-cite-minimum-match-count 2
92 "Minimum number of identical prefixes before we believe it's a citation."
96 ;; Some Microsoft products put in a citation that extends to the
97 ;; remainder of the message:
99 ;; -----Original Message-----
102 ;; Sent: ... [date, in non-RFC-2822 format]
105 ;; Cited message, with no prefixes
107 ;; The four headers are always the same. But note they are prone to
108 ;; folding without additional indentation.
110 ;; Others use "----- Original Message -----" instead, and properly quote
111 ;; the body using "> ". This style is handled without special cases.
113 (defcustom gnus-cite-attribution-prefix
114 "In article\\|in <\\|On \\(Mon\\|Tue\\|Wed\\|Thu\\|Fri\\|Sat\\|Sun\\),\\|----- ?Original Message ?-----"
115 "*Regexp matching the beginning of an attribution line."
119 (defcustom gnus-cite-attribution-suffix
120 "\\(\\(wrote\\|writes\\|said\\|says\\|>\\)\\(:\\|\\.\\.\\.\\)\\|----- ?Original Message ?-----\\)[ \t]*$"
121 "*Regexp matching the end of an attribution line.
122 The text matching the first grouping will be used as a button."
126 (defcustom gnus-cite-unsightly-citation-regexp
127 "^-----Original Message-----\nFrom: \\(.+\n\\)+\n"
128 "Regexp matching Microsoft-type rest-of-message citations."
133 (defcustom gnus-cite-ignore-quoted-from t
134 "Non-nil means don't regard lines beginning with \">From \" as cited text.
135 Those lines may have been quoted by MTAs in order not to mix up with
136 the envelope From line."
141 (defface gnus-cite-attribution-face '((t
143 "Face used for attribution lines."
146 (defcustom gnus-cite-attribution-face 'gnus-cite-attribution-face
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-face-1 '((((class color)
155 (:foreground "light blue"))
158 (:foreground "MidnightBlue"))
164 (defface gnus-cite-face-2 '((((class color)
166 (:foreground "light cyan"))
169 (:foreground "firebrick"))
175 (defface gnus-cite-face-3 '((((class color)
177 (:foreground "light yellow"))
180 (:foreground "dark green"))
186 (defface gnus-cite-face-4 '((((class color)
188 (:foreground "light pink"))
191 (:foreground "OrangeRed"))
197 (defface gnus-cite-face-5 '((((class color)
199 (:foreground "pale green"))
202 (:foreground "dark khaki"))
208 (defface gnus-cite-face-6 '((((class color)
210 (:foreground "beige"))
213 (:foreground "dark violet"))
219 (defface gnus-cite-face-7 '((((class color)
221 (:foreground "orange"))
224 (:foreground "SteelBlue4"))
230 (defface gnus-cite-face-8 '((((class color)
232 (:foreground "magenta"))
235 (:foreground "magenta"))
241 (defface gnus-cite-face-9 '((((class color)
243 (:foreground "violet"))
246 (:foreground "violet"))
252 (defface gnus-cite-face-10 '((((class color)
254 (:foreground "medium purple"))
257 (:foreground "medium purple"))
263 (defface gnus-cite-face-11 '((((class color)
265 (:foreground "turquoise"))
268 (:foreground "turquoise"))
274 (defcustom gnus-cite-face-list
275 '(gnus-cite-face-1 gnus-cite-face-2 gnus-cite-face-3 gnus-cite-face-4
276 gnus-cite-face-5 gnus-cite-face-6 gnus-cite-face-7 gnus-cite-face-8
277 gnus-cite-face-9 gnus-cite-face-10 gnus-cite-face-11)
278 "*List of faces used for highlighting citations.
280 When there are citations from multiple articles in the same message,
281 Gnus will try to give each citation from each article its own face.
282 This should make it easier to see who wrote what."
284 :type '(repeat face))
286 (defcustom gnus-cite-hide-percentage 50
287 "Only hide excess citation if above this percentage of the body."
291 (defcustom gnus-cite-hide-absolute 10
292 "Only hide excess citation if above this number of lines in the body."
296 (defcustom gnus-cite-blank-line-after-header t
297 "If non-nil, put a blank line between the citation header and the button."
301 ;; This has to go here because its default value depends on
302 ;; gnus-cite-face-list.
303 (defcustom gnus-article-boring-faces (cons 'gnus-signature-face
305 "List of faces that are not worth reading.
306 If an article has more pages below the one you are looking at, but
307 nothing on those pages is a word of at least three letters that is not
308 in a boring face, then the pages will be skipped."
310 :group 'gnus-article-hiding)
312 ;;; Internal Variables:
314 (defvar gnus-cite-article nil)
315 (defvar gnus-cite-overlay-list nil)
317 (defvar gnus-cite-prefix-alist nil)
318 ;; Alist of citation prefixes.
319 ;; The cdr is a list of lines with that prefix.
321 (defvar gnus-cite-attribution-alist nil)
322 ;; Alist of attribution lines.
323 ;; The car is a line number.
324 ;; The cdr is the prefix for the citation started by that line.
326 (defvar gnus-cite-loose-prefix-alist nil)
327 ;; Alist of citation prefixes that have no matching attribution.
328 ;; The cdr is a list of lines with that prefix.
330 (defvar gnus-cite-loose-attribution-alist nil)
331 ;; Alist of attribution lines that have no matching citation.
332 ;; Each member has the form (WROTE IN PREFIX TAG), where
333 ;; WROTE: is the attribution line number
334 ;; IN: is the line number of the previous line if part of the same attribution,
335 ;; PREFIX: Is the citation prefix of the attribution line(s), and
336 ;; TAG: Is a Supercite tag, if any.
338 (defvar gnus-cited-opened-text-button-line-format-alist
339 `((?b (marker-position beg) ?d)
340 (?e (marker-position end) ?d)
341 (?n (count-lines beg end) ?d)
342 (?l (- end beg) ?d)))
343 (defvar gnus-cited-opened-text-button-line-format-spec nil)
344 (defvar gnus-cited-closed-text-button-line-format-alist
345 gnus-cited-opened-text-button-line-format-alist)
346 (defvar gnus-cited-closed-text-button-line-format-spec nil)
351 (defun gnus-article-highlight-citation (&optional force)
352 "Highlight cited text.
353 Each citation in the article will be highlighted with a different face.
354 The faces are taken from `gnus-cite-face-list'.
355 Attribution lines are highlighted with the same face as the
356 corresponding citation merged with `gnus-cite-attribution-face'.
358 Text is considered cited if at least `gnus-cite-minimum-match-count'
359 lines matches `message-cite-prefix-regexp' with the same prefix.
361 Lines matching `gnus-cite-attribution-suffix' and perhaps
362 `gnus-cite-attribution-prefix' are considered attribution lines."
363 (interactive (list 'force))
365 (set-buffer gnus-article-buffer)
366 (gnus-cite-parse-maybe force)
367 (let ((buffer-read-only nil)
368 (alist gnus-cite-prefix-alist)
369 (faces gnus-cite-face-list)
370 (inhibit-point-motion-hooks t)
371 face entry prefix skip numbers number face-alist)
372 ;; Loop through citation prefixes.
374 (setq entry (car alist)
379 faces (or (cdr faces) gnus-cite-face-list)
380 face-alist (cons (cons prefix face) face-alist))
382 (setq number (car numbers)
383 numbers (cdr numbers))
384 (and (not (assq number gnus-cite-attribution-alist))
385 (not (assq number gnus-cite-loose-attribution-alist))
386 (gnus-cite-add-face number prefix face))))
387 ;; Loop through attribution lines.
388 (setq alist gnus-cite-attribution-alist)
390 (setq entry (car alist)
394 skip (gnus-cite-find-prefix number)
395 face (cdr (assoc prefix face-alist)))
396 ;; Add attribution button.
397 (goto-char (point-min))
398 (forward-line (1- number))
399 (when (re-search-forward gnus-cite-attribution-suffix
402 (gnus-article-add-button (match-beginning 1) (match-end 1)
403 'gnus-cite-toggle prefix))
404 ;; Highlight attribution line.
405 (gnus-cite-add-face number skip face)
406 (gnus-cite-add-face number skip gnus-cite-attribution-face))
407 ;; Loop through attribution lines.
408 (setq alist gnus-cite-loose-attribution-alist)
410 (setq entry (car alist)
413 skip (gnus-cite-find-prefix number))
414 (gnus-cite-add-face number skip gnus-cite-attribution-face)))))
416 (defun gnus-dissect-cited-text ()
417 "Dissect the article buffer looking for cited text."
419 (set-buffer gnus-article-buffer)
420 (gnus-cite-parse-maybe nil t)
421 (let ((alist gnus-cite-prefix-alist)
422 prefix numbers number marks m)
423 ;; Loop through citation prefixes.
425 (setq numbers (pop alist)
426 prefix (pop numbers))
428 (setq number (pop numbers))
429 (goto-char (point-min))
430 (forward-line number)
431 (push (cons (point-marker) "") marks)
433 (= (1- number) (car numbers)))
434 (setq number (pop numbers)))
435 (goto-char (point-min))
436 (forward-line (1- number))
437 (push (cons (point-marker) prefix) marks)))
438 ;; Skip to the beginning of the body.
440 (push (cons (point-marker) "") marks)
441 ;; Find the end of the body.
442 (goto-char (point-max))
443 (gnus-article-search-signature)
444 (push (cons (point-marker) "") marks)
446 (setq marks (sort marks 'car-less-than-car))
447 (let ((omarks marks))
450 (if (= (caar omarks) (caadr omarks))
452 (unless (equal (cdar omarks) "")
453 (push (car omarks) marks))
454 (unless (equal (cdadr omarks) "")
455 (push (cadr omarks) marks))
456 (unless (and (equal (cdar omarks) "")
457 (equal (cdadr omarks) "")
459 (setq omarks (cdr omarks))))
460 (push (car omarks) marks))
461 (setq omarks (cdr omarks)))
463 (push (car omarks) marks))
464 (setq marks (setq m (nreverse marks)))
466 (if (and (equal (cdadr m) "")
467 (equal (cdar m) (cdaddr m))
468 (goto-char (caadr m))
470 (= (point) (caaddr m)))
475 (defun gnus-article-fill-cited-article (&optional force width)
476 "Do word wrapping in the current article.
477 If WIDTH (the numerical prefix), use that text width when filling."
478 (interactive (list t current-prefix-arg))
480 (set-buffer gnus-article-buffer)
481 (let ((buffer-read-only nil)
482 (inhibit-point-motion-hooks t)
483 (marks (gnus-dissect-cited-text))
484 (adaptive-fill-mode nil)
486 (fill-column (if width (prefix-numeric-value width) fill-column)))
489 (narrow-to-region (caar marks) (caadr marks))
490 (let ((adaptive-fill-regexp
491 (concat "^" (regexp-quote (cdar marks)) " *"))
493 (if (string= (cdar marks) "") ""
494 (concat (cdar marks) " ")))
496 (fill-region (point-min) (point-max)))
497 (set-marker (caar marks) nil)
498 (setq marks (cdr marks)))
500 (set-marker (caar marks) nil))
501 ;; All this information is now incorrect.
502 (setq gnus-cite-prefix-alist nil
503 gnus-cite-attribution-alist nil
504 gnus-cite-loose-prefix-alist nil
505 gnus-cite-loose-attribution-alist nil
506 gnus-cite-article nil)))))
508 (defun gnus-article-hide-citation (&optional arg force)
509 "Toggle hiding of all cited text except attribution lines.
510 See the documentation for `gnus-article-highlight-citation'.
511 If given a negative prefix, always show; if given a positive prefix,
513 (interactive (append (gnus-article-hidden-arg) (list 'force)))
514 (gnus-set-format 'cited-opened-text-button t)
515 (gnus-set-format 'cited-closed-text-button t)
517 (set-buffer gnus-article-buffer)
518 (let ((buffer-read-only nil)
520 (inhibit-point-motion-hooks t)
521 (props (nconc (list 'article-type 'cite)
522 gnus-hidden-properties))
526 (text-property-any point (point-max)
528 'gnus-article-toggle-cited-text))
531 (gnus-article-toggle-cited-text
532 (get-text-property point 'gnus-data) arg)
534 (setq point (point)))
536 (setq marks (gnus-dissect-cited-text))
540 (while (and marks (string= (cdar marks) ""))
541 (setq marks (cdr marks)))
543 (setq beg (caar marks)))
544 (while (and marks (not (string= (cdar marks) "")))
545 (setq marks (cdr marks)))
547 (setq end (caar marks)))
548 ;; Skip past lines we want to leave visible.
549 (when (and beg end gnus-cited-lines-visible)
551 (forward-line (if (consp gnus-cited-lines-visible)
552 (car gnus-cited-lines-visible)
553 gnus-cited-lines-visible))
556 (setq beg (point-marker))
557 (when (consp gnus-cited-lines-visible)
559 (forward-line (- (cdr gnus-cited-lines-visible)))
562 (setq end (point-marker))))))
564 (gnus-add-wash-type 'cite)
565 ;; We use markers for the end-points to facilitate later
566 ;; wrapping and mangling of text.
567 (setq beg (set-marker (make-marker) beg)
568 end (set-marker (make-marker) end))
569 (gnus-add-text-properties-when 'article-type nil beg end props)
571 (when (and gnus-cite-blank-line-after-header
572 (not (save-excursion (search-backward "\n\n" nil t))))
575 (setq start (point-marker))
577 (gnus-article-add-button
579 (progn (eval gnus-cited-closed-text-button-line-format-spec)
581 `gnus-article-toggle-cited-text
582 (list (cons beg end) start))
584 'article-type 'annotation)
585 (set-marker beg (point))))))))
587 (defun gnus-article-toggle-cited-text (args &optional arg)
588 "Toggle hiding the text in REGION.
589 ARG can be nil or a number. Positive means hide, negative
590 means show, nil means toggle."
591 (let* ((region (car args))
596 (text-property-any beg (1- end) 'article-type 'cite))
597 (inhibit-point-motion-hooks t)
601 (and (> arg 0) (not hidden))
602 (and (< arg 0) hidden))
605 ;; Can't remove 'cite from g-a-wash-types here because
606 ;; multiple citations may be hidden -jas
607 (gnus-remove-text-properties-when
608 'article-type 'cite beg end
609 (cons 'article-type (cons 'cite
610 gnus-hidden-properties))))
611 (gnus-add-wash-type 'cite)
612 (gnus-add-text-properties-when
613 'article-type nil beg end
614 (cons 'article-type (cons 'cite
615 gnus-hidden-properties))))
616 (let ((gnus-article-mime-handle-alist-1 gnus-article-mime-handle-alist))
617 (gnus-set-mode-line 'article))
624 (gnus-article-add-button
628 gnus-cited-opened-text-button-line-format-spec
629 gnus-cited-closed-text-button-line-format-spec))
631 `gnus-article-toggle-cited-text
634 'article-type 'annotation)))))
636 (defun gnus-article-hide-citation-maybe (&optional arg force)
637 "Toggle hiding of cited text that has an attribution line.
638 If given a negative prefix, always show; if given a positive prefix,
640 This will do nothing unless at least `gnus-cite-hide-percentage'
641 percent and at least `gnus-cite-hide-absolute' lines of the body is
642 cited text with attributions. When called interactively, these two
643 variables are ignored.
644 See also the documentation for `gnus-article-highlight-citation'."
645 (interactive (append (gnus-article-hidden-arg) '(force)))
646 (with-current-buffer gnus-article-buffer
647 (gnus-delete-wash-type 'cite)
648 (unless (gnus-article-check-hidden-text 'cite arg)
650 (gnus-cite-parse-maybe force)
652 (let ((start (point))
653 (atts gnus-cite-attribution-alist)
654 (buffer-read-only nil)
655 (inhibit-point-motion-hooks t)
658 (goto-char (point-max))
659 (gnus-article-search-signature)
660 (setq total (count-lines start (point)))
662 (setq hidden (+ hidden (length (cdr (assoc (cdar atts)
663 gnus-cite-prefix-alist))))
666 (and (> (* 100 hidden) (* gnus-cite-hide-percentage total))
667 (> hidden gnus-cite-hide-absolute)))
668 (gnus-add-wash-type 'cite)
669 (setq atts gnus-cite-attribution-alist)
671 (setq total (cdr (assoc (cdar atts) gnus-cite-prefix-alist))
674 (setq hidden (car total)
676 (goto-char (point-min))
677 (forward-line (1- hidden))
678 (unless (assq hidden gnus-cite-attribution-alist)
679 (gnus-add-text-properties
680 (point) (progn (forward-line 1) (point))
681 (nconc (list 'article-type 'cite)
682 gnus-hidden-properties)))))))))
683 (gnus-set-mode-line 'article)))
685 (defun gnus-article-hide-citation-in-followups ()
686 "Hide cited text in non-root articles."
689 (set-buffer gnus-article-buffer)
690 (let ((article (cdr gnus-article-current)))
691 (unless (save-excursion
692 (set-buffer gnus-summary-buffer)
693 (gnus-article-displayed-root-p article))
694 (gnus-article-hide-citation)))))
696 ;;; Internal functions:
698 (defun gnus-cite-parse-maybe (&optional force no-overlay)
699 "Always parse the buffer."
701 ;;Reset parser information.
702 (setq gnus-cite-prefix-alist nil
703 gnus-cite-attribution-alist nil
704 gnus-cite-loose-prefix-alist nil
705 gnus-cite-loose-attribution-alist nil)
707 (gnus-cite-delete-overlays))
708 ;; Parse if not too large.
709 (if (and gnus-cite-parse-max-size
710 (> (buffer-size) gnus-cite-parse-max-size))
712 (setq gnus-cite-article (cons (car gnus-article-current)
713 (cdr gnus-article-current)))
714 (gnus-cite-parse-wrapper)))
716 (defun gnus-cite-delete-overlays ()
717 (dolist (overlay gnus-cite-overlay-list)
719 (when (or (not (gnus-overlay-end overlay))
720 (and (>= (gnus-overlay-end overlay) (point-min))
721 (<= (gnus-overlay-end overlay) (point-max))))
722 (setq gnus-cite-overlay-list (delete overlay gnus-cite-overlay-list))
724 (gnus-delete-overlay overlay))))))
726 (defun gnus-cite-parse-wrapper ()
727 ;; Wrap chopped gnus-cite-parse.
729 (let ((inhibit-point-motion-hooks t))
731 (gnus-cite-parse-attributions))
735 (gnus-cite-connect-attributions))))
737 (defun gnus-cite-parse ()
738 ;; Parse and connect citation prefixes and attribution lines.
740 ;; Parse current buffer searching for citation prefixes.
741 (let ((line (1+ (count-lines (point-min) (point))))
744 (goto-char (point-max))
745 (gnus-article-search-signature)
747 (prefix-regexp (concat "^\\(" message-cite-prefix-regexp "\\)"))
748 alist entry start begin end numbers prefix guess-limit)
749 ;; Get all potential prefixes in `alist'.
750 (while (< (point) max)
753 guess-limit (progn (skip-chars-forward "^> \t\r\n") (point))
757 ;; Ignore standard Supercite attribution prefix.
758 (when (and (< guess-limit (+ begin gnus-cite-max-prefix))
759 (looking-at gnus-supercite-regexp))
761 (setq end (1+ (match-end 1)))
762 (setq end (1+ begin))))
763 ;; Ignore very long prefixes.
764 (when (> end (+ begin gnus-cite-max-prefix))
765 (setq end (+ begin gnus-cite-max-prefix)))
766 ;; Ignore quoted envelope From_.
767 (when (and gnus-cite-ignore-quoted-from
769 (setq case-fold-search nil)
770 (looking-at ">From ")
771 (setq case-fold-search t)))
772 (setq end (1+ begin)))
773 (while (re-search-forward prefix-regexp (1- end) t)
775 (setq end (match-end 0)
776 prefix (buffer-substring begin end))
777 (set-text-properties 0 (length prefix) nil prefix)
778 (setq entry (assoc prefix alist))
780 (setcdr entry (cons line (cdr entry)))
781 (push (list prefix line) alist))
784 (setq line (1+ line)))
785 ;; Horrible special case for some Microsoft mailers.
786 (goto-char (point-min))
787 (when (re-search-forward gnus-cite-unsightly-citation-regexp max t)
788 (setq begin (count-lines (point-min) (point)))
789 (setq end (count-lines (point-min) max))
793 (setq begin (1+ begin)))
794 (push (cons "" entry) alist))
795 ;; We got all the potential prefixes. Now create
796 ;; `gnus-cite-prefix-alist' containing the oldest prefix for each
797 ;; line that appears at least `gnus-cite-minimum-match-count'
798 ;; times. First sort them by length. Longer is older.
799 (setq alist (sort alist (lambda (a b)
800 (> (length (car a)) (length (car b))))))
802 (setq entry (car alist)
806 (cond ((null numbers)
807 ;; No lines with this prefix that wasn't also part of
810 ((< (length numbers) gnus-cite-minimum-match-count)
811 ;; Too few lines with this prefix. We keep it a bit
812 ;; longer in case it is an exact match for an attribution
813 ;; line, but we don't remove the line from other
815 (push entry gnus-cite-prefix-alist))
818 gnus-cite-prefix-alist)
819 ;; Remove articles from other prefixes.
823 (setq current (car loop)
826 (gnus-set-difference (cdr current) numbers)))))))))
828 (defun gnus-cite-parse-attributions ()
830 ;; Parse attributions
831 (while (re-search-forward gnus-cite-attribution-suffix (point-max) t)
832 (let* ((start (match-beginning 0))
834 (wrote (count-lines (point-min) end))
835 (prefix (gnus-cite-find-prefix wrote))
836 ;; Check previous line for an attribution leader.
838 (beginning-of-line 1)
839 (when (looking-at gnus-supercite-secondary-regexp)
840 (buffer-substring (match-beginning 1)
844 (and (re-search-backward gnus-cite-attribution-prefix
846 (beginning-of-line 0)
849 (not (re-search-forward gnus-cite-attribution-suffix
851 (count-lines (point-min) (1+ (point)))))))
855 ;; don't add duplicates
856 (let ((al (buffer-substring (save-excursion (beginning-of-line 0)
859 (when (not (assoc al al-alist))
860 (push (list wrote in prefix tag)
861 gnus-cite-loose-attribution-alist)
862 (push (cons al t) al-alist)))))))
864 (defun gnus-cite-connect-attributions ()
865 ;; Connect attributions to citations
867 ;; No citations have been connected to attribution lines yet.
868 (setq gnus-cite-loose-prefix-alist (append gnus-cite-prefix-alist nil))
870 ;; Parse current buffer searching for attribution lines.
871 ;; Find exact supercite citations.
872 (gnus-cite-match-attributions 'small nil
876 (regexp-quote prefix) "[ \t]*"
877 (regexp-quote tag) ">"))))
878 ;; Find loose supercite citations after attributions.
879 (gnus-cite-match-attributions 'small t
885 ;; Find loose supercite citations anywhere.
886 (gnus-cite-match-attributions 'small nil
892 ;; Find nested citations after attributions.
893 (gnus-cite-match-attributions 'small-if-unique t
895 (concat "\\`" (regexp-quote prefix) ".+")))
896 ;; Find nested citations anywhere.
897 (gnus-cite-match-attributions 'small nil
899 (concat "\\`" (regexp-quote prefix) ".+")))
900 ;; Remove loose prefixes with too few lines.
901 (let ((alist gnus-cite-loose-prefix-alist)
904 (setq entry (car alist)
906 (when (< (length (cdr entry)) gnus-cite-minimum-match-count)
907 (setq gnus-cite-prefix-alist
908 (delq entry gnus-cite-prefix-alist)
909 gnus-cite-loose-prefix-alist
910 (delq entry gnus-cite-loose-prefix-alist)))))
911 ;; Find flat attributions.
912 (gnus-cite-match-attributions 'first t nil)
913 ;; Find any attributions (are we getting desperate yet?).
914 (gnus-cite-match-attributions 'first nil nil))
916 (defun gnus-cite-match-attributions (sort after fun)
917 ;; Match all loose attributions and citations (SORT AFTER FUN) .
919 ;; If SORT is `small', the citation with the shortest prefix will be
920 ;; used, if it is `first' the first prefix will be used, if it is
921 ;; `small-if-unique' the shortest prefix will be used if the
922 ;; attribution line does not share its own prefix with other
923 ;; loose attribution lines, otherwise the first prefix will be used.
925 ;; If AFTER is non-nil, only citations after the attribution line
926 ;; will be considered.
928 ;; If FUN is non-nil, it will be called with the arguments (WROTE
929 ;; PREFIX TAG) and expected to return a regular expression. Only
930 ;; citations whose prefix matches the regular expression will be
933 ;; WROTE is the attribution line number.
934 ;; PREFIX is the attribution line prefix.
935 ;; TAG is the Supercite tag on the attribution line.
936 (let ((atts gnus-cite-loose-attribution-alist)
938 att wrote in prefix tag regexp limit smallest best size)
946 regexp (if fun (funcall fun prefix tag) "")
947 size (cond ((eq sort 'small) t)
948 ((eq sort 'first) nil)
949 (t (< (length (gnus-cite-find-loose prefix)) 2)))
950 limit (if after wrote -1)
953 (let ((cites gnus-cite-loose-prefix-alist)
954 cite candidate numbers first compare)
956 (setq cite (car cites)
960 first (apply 'min numbers)
961 compare (if size (length candidate) first))
964 (string-match regexp candidate)
970 (setq gnus-cite-loose-attribution-alist
971 (delq att gnus-cite-loose-attribution-alist))
972 (push (cons wrote (car best)) gnus-cite-attribution-alist)
974 (push (cons in (car best)) gnus-cite-attribution-alist))
975 (when (memq best gnus-cite-loose-prefix-alist)
976 (let ((loop gnus-cite-prefix-alist)
979 (setq gnus-cite-loose-prefix-alist
980 (delq best gnus-cite-loose-prefix-alist))
982 (setq current (car loop)
984 (if (eq current best)
986 (setcdr current (gnus-set-difference (cdr current) numbers))
987 (when (null (cdr current))
988 (setq gnus-cite-loose-prefix-alist
989 (delq current gnus-cite-loose-prefix-alist)
990 atts (delq current atts)))))))))))
992 (defun gnus-cite-find-loose (prefix)
993 ;; Return a list of loose attribution lines prefixed by PREFIX.
994 (let* ((atts gnus-cite-loose-attribution-alist)
1000 (when (string-equal (gnus-cite-find-prefix line) prefix)
1004 (defun gnus-cite-add-face (number prefix face)
1005 ;; At line NUMBER, ignore PREFIX and add FACE to the rest of the line.
1007 (let ((inhibit-point-motion-hooks t)
1009 (goto-char (point-min))
1010 (when (zerop (forward-line (1- number)))
1011 (forward-char (length prefix))
1012 (skip-chars-forward " \t")
1015 (skip-chars-backward " \t")
1018 (push (setq overlay (gnus-make-overlay from to))
1019 gnus-cite-overlay-list)
1020 (gnus-overlay-put overlay 'evaporate t)
1021 (gnus-overlay-put overlay 'face face))))))
1023 (defun gnus-cite-toggle (prefix)
1025 (set-buffer gnus-article-buffer)
1026 (gnus-cite-parse-maybe nil t)
1027 (let ((buffer-read-only nil)
1028 (numbers (cdr (assoc prefix gnus-cite-prefix-alist)))
1029 (inhibit-point-motion-hooks t)
1032 (setq number (car numbers)
1033 numbers (cdr numbers))
1034 (goto-char (point-min))
1035 (forward-line (1- number))
1036 (cond ((get-text-property (point) 'invisible)
1037 ;; Can't remove 'cite from g-a-wash-types here because
1038 ;; multiple citations may be hidden -jas
1039 (remove-text-properties (point) (progn (forward-line 1) (point))
1040 gnus-hidden-properties))
1041 ((assq number gnus-cite-attribution-alist))
1043 (gnus-add-wash-type 'cite)
1044 (gnus-add-text-properties
1045 (point) (progn (forward-line 1) (point))
1046 (nconc (list 'article-type 'cite)
1047 gnus-hidden-properties))))
1048 (let ((gnus-article-mime-handle-alist-1
1049 gnus-article-mime-handle-alist))
1050 (gnus-set-mode-line 'article))))))
1052 (defun gnus-cite-find-prefix (line)
1053 ;; Return citation prefix for LINE.
1054 (let ((alist gnus-cite-prefix-alist)
1058 (setq entry (car alist)
1060 (when (memq line (cdr entry))
1061 (setq prefix (car entry))))
1064 (defun gnus-cite-localize ()
1065 "Make the citation variables local to the article buffer."
1066 (let ((vars '(gnus-cite-article
1067 gnus-cite-overlay-list gnus-cite-prefix-alist
1068 gnus-cite-attribution-alist gnus-cite-loose-prefix-alist
1069 gnus-cite-loose-attribution-alist)))
1071 (make-local-variable (pop vars)))))
1073 (defun gnus-cited-line-p ()
1074 "Say whether the current line is a cited line."
1078 (dolist (prefix (mapcar 'car gnus-cite-prefix-alist))
1079 (when (string= (buffer-substring (point) (+ (length prefix) (point)))
1086 (provide 'gnus-cite)
1089 ;; coding: iso-8859-1
1092 ;;; gnus-cite.el ends here