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."
284 ;; This has to go here because its default value depends on
285 ;; gnus-cite-face-list.
286 (defcustom gnus-article-boring-faces (cons 'gnus-signature-face
288 "List of faces that are not worth reading.
289 If an article has more pages below the one you are looking at, but
290 nothing on those pages is a word of at least three letters that is not
291 in a boring face, then the pages will be skipped."
293 :group 'gnus-article-hiding)
295 ;;; Internal Variables:
297 (defvar gnus-cite-article nil)
298 (defvar gnus-cite-overlay-list nil)
300 (defvar gnus-cite-prefix-alist nil)
301 ;; Alist of citation prefixes.
302 ;; The cdr is a list of lines with that prefix.
304 (defvar gnus-cite-attribution-alist nil)
305 ;; Alist of attribution lines.
306 ;; The car is a line number.
307 ;; The cdr is the prefix for the citation started by that line.
309 (defvar gnus-cite-loose-prefix-alist nil)
310 ;; Alist of citation prefixes that have no matching attribution.
311 ;; The cdr is a list of lines with that prefix.
313 (defvar gnus-cite-loose-attribution-alist nil)
314 ;; Alist of attribution lines that have no matching citation.
315 ;; Each member has the form (WROTE IN PREFIX TAG), where
316 ;; WROTE: is the attribution line number
317 ;; IN: is the line number of the previous line if part of the same attribution,
318 ;; PREFIX: Is the citation prefix of the attribution line(s), and
319 ;; TAG: Is a Supercite tag, if any.
321 (defvar gnus-cited-opened-text-button-line-format-alist
322 `((?b (marker-position beg) ?d)
323 (?e (marker-position end) ?d)
324 (?n (count-lines beg end) ?d)
325 (?l (- end beg) ?d)))
326 (defvar gnus-cited-opened-text-button-line-format-spec nil)
327 (defvar gnus-cited-closed-text-button-line-format-alist
328 gnus-cited-opened-text-button-line-format-alist)
329 (defvar gnus-cited-closed-text-button-line-format-spec nil)
334 (defun gnus-article-highlight-citation (&optional force)
335 "Highlight cited text.
336 Each citation in the article will be highlighted with a different face.
337 The faces are taken from `gnus-cite-face-list'.
338 Attribution lines are highlighted with the same face as the
339 corresponding citation merged with `gnus-cite-attribution-face'.
341 Text is considered cited if at least `gnus-cite-minimum-match-count'
342 lines matches `message-cite-prefix-regexp' with the same prefix.
344 Lines matching `gnus-cite-attribution-suffix' and perhaps
345 `gnus-cite-attribution-prefix' are considered attribution lines."
346 (interactive (list 'force))
348 (set-buffer gnus-article-buffer)
349 (gnus-cite-parse-maybe force)
350 (let ((buffer-read-only nil)
351 (alist gnus-cite-prefix-alist)
352 (faces gnus-cite-face-list)
353 (inhibit-point-motion-hooks t)
354 face entry prefix skip numbers number face-alist)
355 ;; Loop through citation prefixes.
357 (setq entry (car alist)
362 faces (or (cdr faces) gnus-cite-face-list)
363 face-alist (cons (cons prefix face) face-alist))
365 (setq number (car numbers)
366 numbers (cdr numbers))
367 (and (not (assq number gnus-cite-attribution-alist))
368 (not (assq number gnus-cite-loose-attribution-alist))
369 (gnus-cite-add-face number prefix face))))
370 ;; Loop through attribution lines.
371 (setq alist gnus-cite-attribution-alist)
373 (setq entry (car alist)
377 skip (gnus-cite-find-prefix number)
378 face (cdr (assoc prefix face-alist)))
379 ;; Add attribution button.
380 (goto-char (point-min))
381 (forward-line (1- number))
382 (when (re-search-forward gnus-cite-attribution-suffix
385 (gnus-article-add-button (match-beginning 1) (match-end 1)
386 'gnus-cite-toggle prefix))
387 ;; Highlight attribution line.
388 (gnus-cite-add-face number skip face)
389 (gnus-cite-add-face number skip gnus-cite-attribution-face))
390 ;; Loop through attribution lines.
391 (setq alist gnus-cite-loose-attribution-alist)
393 (setq entry (car alist)
396 skip (gnus-cite-find-prefix number))
397 (gnus-cite-add-face number skip gnus-cite-attribution-face)))))
399 (defun gnus-dissect-cited-text ()
400 "Dissect the article buffer looking for cited text."
402 (set-buffer gnus-article-buffer)
403 (gnus-cite-parse-maybe nil t)
404 (let ((alist gnus-cite-prefix-alist)
405 prefix numbers number marks m)
406 ;; Loop through citation prefixes.
408 (setq numbers (pop alist)
409 prefix (pop numbers))
411 (setq number (pop numbers))
412 (goto-char (point-min))
413 (forward-line number)
414 (push (cons (point-marker) "") marks)
416 (= (1- number) (car numbers)))
417 (setq number (pop numbers)))
418 (goto-char (point-min))
419 (forward-line (1- number))
420 (push (cons (point-marker) prefix) marks)))
421 ;; Skip to the beginning of the body.
423 (push (cons (point-marker) "") marks)
424 ;; Find the end of the body.
425 (goto-char (point-max))
426 (gnus-article-search-signature)
427 (push (cons (point-marker) "") marks)
429 (setq marks (sort marks 'car-less-than-car))
430 (let ((omarks marks))
433 (if (= (caar omarks) (caadr omarks))
435 (unless (equal (cdar omarks) "")
436 (push (car omarks) marks))
437 (unless (equal (cdadr omarks) "")
438 (push (cadr omarks) marks))
439 (unless (and (equal (cdar omarks) "")
440 (equal (cdadr omarks) "")
442 (setq omarks (cdr omarks))))
443 (push (car omarks) marks))
444 (setq omarks (cdr omarks)))
446 (push (car omarks) marks))
447 (setq marks (setq m (nreverse marks)))
449 (if (and (equal (cdadr m) "")
450 (equal (cdar m) (cdaddr m))
451 (goto-char (caadr m))
453 (= (point) (caaddr m)))
458 (defun gnus-article-fill-cited-article (&optional force width)
459 "Do word wrapping in the current article.
460 If WIDTH (the numerical prefix), use that text width when filling."
461 (interactive (list t current-prefix-arg))
463 (set-buffer gnus-article-buffer)
464 (let ((buffer-read-only nil)
465 (inhibit-point-motion-hooks t)
466 (marks (gnus-dissect-cited-text))
467 (adaptive-fill-mode nil)
469 (fill-column (if width (prefix-numeric-value width) fill-column)))
472 (narrow-to-region (caar marks) (caadr marks))
473 (let ((adaptive-fill-regexp
474 (concat "^" (regexp-quote (cdar marks)) " *"))
476 (if (string= (cdar marks) "") ""
477 (concat (cdar marks) " ")))
479 (fill-region (point-min) (point-max)))
480 (set-marker (caar marks) nil)
481 (setq marks (cdr marks)))
483 (set-marker (caar marks) nil))
484 ;; All this information is now incorrect.
485 (setq gnus-cite-prefix-alist nil
486 gnus-cite-attribution-alist nil
487 gnus-cite-loose-prefix-alist nil
488 gnus-cite-loose-attribution-alist nil
489 gnus-cite-article nil)))))
491 (defun gnus-article-hide-citation (&optional arg force)
492 "Toggle hiding of all cited text except attribution lines.
493 See the documentation for `gnus-article-highlight-citation'.
494 If given a negative prefix, always show; if given a positive prefix,
496 (interactive (append (gnus-article-hidden-arg) (list 'force)))
497 (gnus-set-format 'cited-opened-text-button t)
498 (gnus-set-format 'cited-closed-text-button t)
500 (set-buffer gnus-article-buffer)
501 (let ((buffer-read-only nil)
503 (inhibit-point-motion-hooks t)
504 (props (nconc (list 'article-type 'cite)
505 gnus-hidden-properties))
509 (text-property-any point (point-max)
511 'gnus-article-toggle-cited-text))
514 (gnus-article-toggle-cited-text
515 (get-text-property point 'gnus-data) arg)
517 (setq point (point)))
519 (setq marks (gnus-dissect-cited-text))
523 (while (and marks (string= (cdar marks) ""))
524 (setq marks (cdr marks)))
526 (setq beg (caar marks)))
527 (while (and marks (not (string= (cdar marks) "")))
528 (setq marks (cdr marks)))
530 (setq end (caar marks)))
531 ;; Skip past lines we want to leave visible.
532 (when (and beg end gnus-cited-lines-visible)
534 (forward-line (if (consp gnus-cited-lines-visible)
535 (car gnus-cited-lines-visible)
536 gnus-cited-lines-visible))
539 (setq beg (point-marker))
540 (when (consp gnus-cited-lines-visible)
542 (forward-line (- (cdr gnus-cited-lines-visible)))
545 (setq end (point-marker))))))
547 (gnus-add-wash-type 'cite)
548 ;; We use markers for the end-points to facilitate later
549 ;; wrapping and mangling of text.
550 (setq beg (set-marker (make-marker) beg)
551 end (set-marker (make-marker) end))
552 (gnus-add-text-properties-when 'article-type nil beg end props)
554 (when (and gnus-cite-blank-line-after-header
555 (not (save-excursion (search-backward "\n\n" nil t))))
558 (setq start (point-marker))
560 (gnus-article-add-button
562 (progn (eval gnus-cited-closed-text-button-line-format-spec)
564 `gnus-article-toggle-cited-text
565 (list (cons beg end) start))
567 'article-type 'annotation)
568 (set-marker beg (point))))))))
570 (defun gnus-article-toggle-cited-text (args &optional arg)
571 "Toggle hiding the text in REGION.
572 ARG can be nil or a number. Positive means hide, negative
573 means show, nil means toggle."
574 (let* ((region (car args))
579 (text-property-any beg (1- end) 'article-type 'cite))
580 (inhibit-point-motion-hooks t)
584 (and (> arg 0) (not hidden))
585 (and (< arg 0) hidden))
588 ;; Can't remove 'cite from g-a-wash-types here because
589 ;; multiple citations may be hidden -jas
590 (gnus-remove-text-properties-when
591 'article-type 'cite beg end
592 (cons 'article-type (cons 'cite
593 gnus-hidden-properties))))
594 (gnus-add-wash-type 'cite)
595 (gnus-add-text-properties-when
596 'article-type nil beg end
597 (cons 'article-type (cons 'cite
598 gnus-hidden-properties))))
599 (let ((gnus-article-mime-handle-alist-1 gnus-article-mime-handle-alist))
600 (gnus-set-mode-line 'article))
607 (gnus-article-add-button
611 gnus-cited-opened-text-button-line-format-spec
612 gnus-cited-closed-text-button-line-format-spec))
614 `gnus-article-toggle-cited-text
617 'article-type 'annotation)))))
619 (defun gnus-article-hide-citation-maybe (&optional arg force)
620 "Toggle hiding of cited text that has an attribution line.
621 If given a negative prefix, always show; if given a positive prefix,
623 This will do nothing unless at least `gnus-cite-hide-percentage'
624 percent and at least `gnus-cite-hide-absolute' lines of the body is
625 cited text with attributions. When called interactively, these two
626 variables are ignored.
627 See also the documentation for `gnus-article-highlight-citation'."
628 (interactive (append (gnus-article-hidden-arg) '(force)))
629 (with-current-buffer gnus-article-buffer
630 (gnus-delete-wash-type 'cite)
631 (unless (gnus-article-check-hidden-text 'cite arg)
633 (gnus-cite-parse-maybe force)
635 (let ((start (point))
636 (atts gnus-cite-attribution-alist)
637 (buffer-read-only nil)
638 (inhibit-point-motion-hooks t)
641 (goto-char (point-max))
642 (gnus-article-search-signature)
643 (setq total (count-lines start (point)))
645 (setq hidden (+ hidden (length (cdr (assoc (cdar atts)
646 gnus-cite-prefix-alist))))
649 (and (> (* 100 hidden) (* gnus-cite-hide-percentage total))
650 (> hidden gnus-cite-hide-absolute)))
651 (gnus-add-wash-type 'cite)
652 (setq atts gnus-cite-attribution-alist)
654 (setq total (cdr (assoc (cdar atts) gnus-cite-prefix-alist))
657 (setq hidden (car total)
659 (goto-char (point-min))
660 (forward-line (1- hidden))
661 (unless (assq hidden gnus-cite-attribution-alist)
662 (gnus-add-text-properties
663 (point) (progn (forward-line 1) (point))
664 (nconc (list 'article-type 'cite)
665 gnus-hidden-properties)))))))))
666 (gnus-set-mode-line 'article)))
668 (defun gnus-article-hide-citation-in-followups ()
669 "Hide cited text in non-root articles."
672 (set-buffer gnus-article-buffer)
673 (let ((article (cdr gnus-article-current)))
674 (unless (save-excursion
675 (set-buffer gnus-summary-buffer)
676 (gnus-article-displayed-root-p article))
677 (gnus-article-hide-citation)))))
679 ;;; Internal functions:
681 (defun gnus-cite-parse-maybe (&optional force no-overlay)
682 "Always parse the buffer."
684 ;;Reset parser information.
685 (setq gnus-cite-prefix-alist nil
686 gnus-cite-attribution-alist nil
687 gnus-cite-loose-prefix-alist nil
688 gnus-cite-loose-attribution-alist nil)
690 (gnus-cite-delete-overlays))
691 ;; Parse if not too large.
692 (if (and gnus-cite-parse-max-size
693 (> (buffer-size) gnus-cite-parse-max-size))
695 (setq gnus-cite-article (cons (car gnus-article-current)
696 (cdr gnus-article-current)))
697 (gnus-cite-parse-wrapper)))
699 (defun gnus-cite-delete-overlays ()
700 (dolist (overlay gnus-cite-overlay-list)
702 (when (or (not (gnus-overlay-end overlay))
703 (and (>= (gnus-overlay-end overlay) (point-min))
704 (<= (gnus-overlay-end overlay) (point-max))))
705 (setq gnus-cite-overlay-list (delete overlay gnus-cite-overlay-list))
707 (gnus-delete-overlay overlay))))))
709 (defun gnus-cite-parse-wrapper ()
710 ;; Wrap chopped gnus-cite-parse.
712 (let ((inhibit-point-motion-hooks t))
714 (gnus-cite-parse-attributions))
718 (gnus-cite-connect-attributions))))
720 (defun gnus-cite-parse ()
721 ;; Parse and connect citation prefixes and attribution lines.
723 ;; Parse current buffer searching for citation prefixes.
724 (let ((line (1+ (count-lines (point-min) (point))))
727 (goto-char (point-max))
728 (gnus-article-search-signature)
730 (prefix-regexp (concat "^\\(" message-cite-prefix-regexp "\\)"))
731 alist entry start begin end numbers prefix guess-limit)
732 ;; Get all potential prefixes in `alist'.
733 (while (< (point) max)
736 guess-limit (progn (skip-chars-forward "^> \t\r\n") (point))
740 ;; Ignore standard Supercite attribution prefix.
741 (when (and (< guess-limit (+ begin gnus-cite-max-prefix))
742 (looking-at gnus-supercite-regexp))
744 (setq end (1+ (match-end 1)))
745 (setq end (1+ begin))))
746 ;; Ignore very long prefixes.
747 (when (> end (+ begin gnus-cite-max-prefix))
748 (setq end (+ begin gnus-cite-max-prefix)))
749 ;; Ignore quoted envelope From_.
750 (when (and gnus-cite-ignore-quoted-from
752 (setq case-fold-search nil)
753 (looking-at ">From ")
754 (setq case-fold-search t)))
755 (setq end (1+ begin)))
756 (while (re-search-forward prefix-regexp (1- end) t)
758 (setq end (match-end 0)
759 prefix (buffer-substring begin end))
760 (set-text-properties 0 (length prefix) nil prefix)
761 (setq entry (assoc prefix alist))
763 (setcdr entry (cons line (cdr entry)))
764 (push (list prefix line) alist))
767 (setq line (1+ line)))
768 ;; Horrible special case for some Microsoft mailers.
769 (goto-char (point-min))
770 (when (re-search-forward gnus-cite-unsightly-citation-regexp max t)
771 (setq begin (count-lines (point-min) (point)))
772 (setq end (count-lines (point-min) max))
776 (setq begin (1+ begin)))
777 (push (cons "" entry) alist))
778 ;; We got all the potential prefixes. Now create
779 ;; `gnus-cite-prefix-alist' containing the oldest prefix for each
780 ;; line that appears at least `gnus-cite-minimum-match-count'
781 ;; times. First sort them by length. Longer is older.
782 (setq alist (sort alist (lambda (a b)
783 (> (length (car a)) (length (car b))))))
785 (setq entry (car alist)
789 (cond ((null numbers)
790 ;; No lines with this prefix that wasn't also part of
793 ((< (length numbers) gnus-cite-minimum-match-count)
794 ;; Too few lines with this prefix. We keep it a bit
795 ;; longer in case it is an exact match for an attribution
796 ;; line, but we don't remove the line from other
798 (push entry gnus-cite-prefix-alist))
801 gnus-cite-prefix-alist)
802 ;; Remove articles from other prefixes.
806 (setq current (car loop)
809 (gnus-set-difference (cdr current) numbers)))))))))
811 (defun gnus-cite-parse-attributions ()
813 ;; Parse attributions
814 (while (re-search-forward gnus-cite-attribution-suffix (point-max) t)
815 (let* ((start (match-beginning 0))
817 (wrote (count-lines (point-min) end))
818 (prefix (gnus-cite-find-prefix wrote))
819 ;; Check previous line for an attribution leader.
821 (beginning-of-line 1)
822 (when (looking-at gnus-supercite-secondary-regexp)
823 (buffer-substring (match-beginning 1)
827 (and (re-search-backward gnus-cite-attribution-prefix
829 (beginning-of-line 0)
832 (not (re-search-forward gnus-cite-attribution-suffix
834 (count-lines (point-min) (1+ (point)))))))
838 ;; don't add duplicates
839 (let ((al (buffer-substring (save-excursion (beginning-of-line 0)
842 (when (not (assoc al al-alist))
843 (push (list wrote in prefix tag)
844 gnus-cite-loose-attribution-alist)
845 (push (cons al t) al-alist)))))))
847 (defun gnus-cite-connect-attributions ()
848 ;; Connect attributions to citations
850 ;; No citations have been connected to attribution lines yet.
851 (setq gnus-cite-loose-prefix-alist (append gnus-cite-prefix-alist nil))
853 ;; Parse current buffer searching for attribution lines.
854 ;; Find exact supercite citations.
855 (gnus-cite-match-attributions 'small nil
859 (regexp-quote prefix) "[ \t]*"
860 (regexp-quote tag) ">"))))
861 ;; Find loose supercite citations after attributions.
862 (gnus-cite-match-attributions 'small t
868 ;; Find loose supercite citations anywhere.
869 (gnus-cite-match-attributions 'small nil
875 ;; Find nested citations after attributions.
876 (gnus-cite-match-attributions 'small-if-unique t
878 (concat "\\`" (regexp-quote prefix) ".+")))
879 ;; Find nested citations anywhere.
880 (gnus-cite-match-attributions 'small nil
882 (concat "\\`" (regexp-quote prefix) ".+")))
883 ;; Remove loose prefixes with too few lines.
884 (let ((alist gnus-cite-loose-prefix-alist)
887 (setq entry (car alist)
889 (when (< (length (cdr entry)) gnus-cite-minimum-match-count)
890 (setq gnus-cite-prefix-alist
891 (delq entry gnus-cite-prefix-alist)
892 gnus-cite-loose-prefix-alist
893 (delq entry gnus-cite-loose-prefix-alist)))))
894 ;; Find flat attributions.
895 (gnus-cite-match-attributions 'first t nil)
896 ;; Find any attributions (are we getting desperate yet?).
897 (gnus-cite-match-attributions 'first nil nil))
899 (defun gnus-cite-match-attributions (sort after fun)
900 ;; Match all loose attributions and citations (SORT AFTER FUN) .
902 ;; If SORT is `small', the citation with the shortest prefix will be
903 ;; used, if it is `first' the first prefix will be used, if it is
904 ;; `small-if-unique' the shortest prefix will be used if the
905 ;; attribution line does not share its own prefix with other
906 ;; loose attribution lines, otherwise the first prefix will be used.
908 ;; If AFTER is non-nil, only citations after the attribution line
909 ;; will be considered.
911 ;; If FUN is non-nil, it will be called with the arguments (WROTE
912 ;; PREFIX TAG) and expected to return a regular expression. Only
913 ;; citations whose prefix matches the regular expression will be
916 ;; WROTE is the attribution line number.
917 ;; PREFIX is the attribution line prefix.
918 ;; TAG is the Supercite tag on the attribution line.
919 (let ((atts gnus-cite-loose-attribution-alist)
921 att wrote in prefix tag regexp limit smallest best size)
929 regexp (if fun (funcall fun prefix tag) "")
930 size (cond ((eq sort 'small) t)
931 ((eq sort 'first) nil)
932 (t (< (length (gnus-cite-find-loose prefix)) 2)))
933 limit (if after wrote -1)
936 (let ((cites gnus-cite-loose-prefix-alist)
937 cite candidate numbers first compare)
939 (setq cite (car cites)
943 first (apply 'min numbers)
944 compare (if size (length candidate) first))
947 (string-match regexp candidate)
953 (setq gnus-cite-loose-attribution-alist
954 (delq att gnus-cite-loose-attribution-alist))
955 (push (cons wrote (car best)) gnus-cite-attribution-alist)
957 (push (cons in (car best)) gnus-cite-attribution-alist))
958 (when (memq best gnus-cite-loose-prefix-alist)
959 (let ((loop gnus-cite-prefix-alist)
962 (setq gnus-cite-loose-prefix-alist
963 (delq best gnus-cite-loose-prefix-alist))
965 (setq current (car loop)
967 (if (eq current best)
969 (setcdr current (gnus-set-difference (cdr current) numbers))
970 (when (null (cdr current))
971 (setq gnus-cite-loose-prefix-alist
972 (delq current gnus-cite-loose-prefix-alist)
973 atts (delq current atts)))))))))))
975 (defun gnus-cite-find-loose (prefix)
976 ;; Return a list of loose attribution lines prefixed by PREFIX.
977 (let* ((atts gnus-cite-loose-attribution-alist)
983 (when (string-equal (gnus-cite-find-prefix line) prefix)
987 (defun gnus-cite-add-face (number prefix face)
988 ;; At line NUMBER, ignore PREFIX and add FACE to the rest of the line.
990 (let ((inhibit-point-motion-hooks t)
992 (goto-char (point-min))
993 (when (zerop (forward-line (1- number)))
994 (forward-char (length prefix))
995 (skip-chars-forward " \t")
998 (skip-chars-backward " \t")
1001 (push (setq overlay (gnus-make-overlay from to))
1002 gnus-cite-overlay-list)
1003 (gnus-overlay-put overlay 'face face))))))
1005 (defun gnus-cite-toggle (prefix)
1007 (set-buffer gnus-article-buffer)
1008 (gnus-cite-parse-maybe nil t)
1009 (let ((buffer-read-only nil)
1010 (numbers (cdr (assoc prefix gnus-cite-prefix-alist)))
1011 (inhibit-point-motion-hooks t)
1014 (setq number (car numbers)
1015 numbers (cdr numbers))
1016 (goto-char (point-min))
1017 (forward-line (1- number))
1018 (cond ((get-text-property (point) 'invisible)
1019 ;; Can't remove 'cite from g-a-wash-types here because
1020 ;; multiple citations may be hidden -jas
1021 (remove-text-properties (point) (progn (forward-line 1) (point))
1022 gnus-hidden-properties))
1023 ((assq number gnus-cite-attribution-alist))
1025 (gnus-add-wash-type 'cite)
1026 (gnus-add-text-properties
1027 (point) (progn (forward-line 1) (point))
1028 (nconc (list 'article-type 'cite)
1029 gnus-hidden-properties))))
1030 (let ((gnus-article-mime-handle-alist-1
1031 gnus-article-mime-handle-alist))
1032 (gnus-set-mode-line 'article))))))
1034 (defun gnus-cite-find-prefix (line)
1035 ;; Return citation prefix for LINE.
1036 (let ((alist gnus-cite-prefix-alist)
1040 (setq entry (car alist)
1042 (when (memq line (cdr entry))
1043 (setq prefix (car entry))))
1046 (defun gnus-cite-localize ()
1047 "Make the citation variables local to the article buffer."
1048 (let ((vars '(gnus-cite-article
1049 gnus-cite-overlay-list gnus-cite-prefix-alist
1050 gnus-cite-attribution-alist gnus-cite-loose-prefix-alist
1051 gnus-cite-loose-attribution-alist)))
1053 (make-local-variable (pop vars)))))
1055 (defun gnus-cited-line-p ()
1056 "Say whether the current line is a cited line."
1060 (dolist (prefix (mapcar 'car gnus-cite-prefix-alist))
1061 (when (string= (buffer-substring (point) (+ (length prefix) (point)))
1068 (provide 'gnus-cite)
1071 ;; coding: iso-8859-1
1074 ;;; arch-tag: 1997b044-6067-471e-8c8f-dc903093098a
1075 ;;; gnus-cite.el ends here