1 ;;; gnus-cite.el --- parse citations in articles for Gnus
3 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
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))
30 (eval-when-compile (require 'static))
35 (require 'message) ; for message-cite-prefix-regexp
39 (defgroup gnus-cite nil
42 :link '(custom-manual "(gnus)Article Highlighting")
45 (defcustom gnus-cite-reply-regexp
46 "^\\(Subject: Re\\|In-Reply-To\\|References\\):"
47 "*If headers match this regexp it is reasonable to believe that
48 article has citations."
52 (defcustom gnus-cite-always-check nil
53 "Check article always for citations. Set it t to check all articles."
55 :type '(choice (const :tag "no" nil)
56 (const :tag "yes" t)))
58 (defcustom gnus-cited-opened-text-button-line-format "%(%{[-]%}%)\n"
59 "Format of opened cited text buttons."
63 (defcustom gnus-cited-closed-text-button-line-format "%(%{[+]%}%)\n"
64 "Format of closed cited text buttons."
68 (defcustom gnus-cited-lines-visible nil
69 "The number of lines of hidden cited text to remain visible.
70 Or a pair (cons) of numbers which are the number of lines at the top
71 and bottom of the text, respectively, to remain visible."
73 :type '(choice (const :tag "none" nil)
75 (cons :tag "Top and Bottom" integer integer)))
77 (defcustom gnus-cite-parse-max-size 25000
78 "Maximum article size (in bytes) where parsing citations is allowed.
79 Set it to nil to parse all articles."
81 :type '(choice (const :tag "all" nil)
84 (defcustom gnus-cite-max-prefix 20
85 "Maximum possible length for a citation prefix."
89 (defcustom gnus-supercite-regexp
90 (concat "^\\(" message-cite-prefix-regexp "\\)? *"
91 ">>>>> +\"\\([^\"\n]+\\)\" +==")
92 "*Regexp matching normal Supercite attribution lines.
93 The first grouping must match prefixes added by other packages."
97 (defcustom gnus-supercite-secondary-regexp "^.*\"\\([^\"\n]+\\)\" +=="
98 "Regexp matching mangled Supercite attribution lines.
99 The first regexp group should match the Supercite attribution."
103 (defcustom gnus-cite-minimum-match-count 2
104 "Minimum number of identical prefixes before we believe it's a citation."
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 (defface gnus-cite-attribution-face '((t
123 "Face used for attribution lines.")
125 (defcustom gnus-cite-attribution-face 'gnus-cite-attribution-face
126 "Face used for attribution lines.
127 It is merged with the face for the cited text belonging to the attribution."
131 (defface gnus-cite-face-1 '((((class color)
133 (:foreground "light blue"))
136 (:foreground "MidnightBlue"))
141 (defface gnus-cite-face-2 '((((class color)
143 (:foreground "light cyan"))
146 (:foreground "firebrick"))
151 (defface gnus-cite-face-3 '((((class color)
153 (:foreground "light yellow"))
156 (:foreground "dark green"))
161 (defface gnus-cite-face-4 '((((class color)
163 (:foreground "light pink"))
166 (:foreground "OrangeRed"))
171 (defface gnus-cite-face-5 '((((class color)
173 (:foreground "pale green"))
176 (:foreground "dark khaki"))
181 (defface gnus-cite-face-6 '((((class color)
183 (:foreground "beige"))
186 (:foreground "dark violet"))
191 (defface gnus-cite-face-7 '((((class color)
193 (:foreground "orange"))
196 (:foreground "SteelBlue4"))
201 (defface gnus-cite-face-8 '((((class color)
203 (:foreground "magenta"))
206 (:foreground "magenta"))
211 (defface gnus-cite-face-9 '((((class color)
213 (:foreground "violet"))
216 (:foreground "violet"))
221 (defface gnus-cite-face-10 '((((class color)
223 (:foreground "medium purple"))
226 (:foreground "medium purple"))
231 (defface gnus-cite-face-11 '((((class color)
233 (:foreground "turquoise"))
236 (:foreground "turquoise"))
241 (defcustom gnus-cite-face-list
242 '(gnus-cite-face-1 gnus-cite-face-2 gnus-cite-face-3 gnus-cite-face-4
243 gnus-cite-face-5 gnus-cite-face-6 gnus-cite-face-7 gnus-cite-face-8
244 gnus-cite-face-9 gnus-cite-face-10 gnus-cite-face-11)
245 "*List of faces used for highlighting citations.
247 When there are citations from multiple articles in the same message,
248 Gnus will try to give each citation from each article its own face.
249 This should make it easier to see who wrote what."
251 :type '(repeat face))
253 (defcustom gnus-cite-hide-percentage 50
254 "Only hide excess citation if above this percentage of the body."
258 (defcustom gnus-cite-hide-absolute 10
259 "Only hide excess citation if above this number of lines in the body."
263 (defcustom gnus-cite-blank-line-after-header t
264 "If non-nil, put a blank line between the citation header and the button."
268 ;;; Internal Variables:
270 (defvar gnus-cite-article nil)
271 (defvar gnus-cite-overlay-list nil)
273 (defvar gnus-cite-prefix-alist nil)
274 ;; Alist of citation prefixes.
275 ;; The cdr is a list of lines with that prefix.
277 (defvar gnus-cite-attribution-alist nil)
278 ;; Alist of attribution lines.
279 ;; The car is a line number.
280 ;; The cdr is the prefix for the citation started by that line.
282 (defvar gnus-cite-loose-prefix-alist nil)
283 ;; Alist of citation prefixes that have no matching attribution.
284 ;; The cdr is a list of lines with that prefix.
286 (defvar gnus-cite-loose-attribution-alist nil)
287 ;; Alist of attribution lines that have no matching citation.
288 ;; Each member has the form (WROTE IN PREFIX TAG), where
289 ;; WROTE: is the attribution line number
290 ;; IN: is the line number of the previous line if part of the same attribution,
291 ;; PREFIX: Is the citation prefix of the attribution line(s), and
292 ;; TAG: Is a Supercite tag, if any.
294 (defvar gnus-cited-opened-text-button-line-format-alist
295 `((?b (marker-position beg) ?d)
296 (?e (marker-position end) ?d)
297 (?n (count-lines beg end) ?d)
298 (?l (- end beg) ?d)))
299 (defvar gnus-cited-opened-text-button-line-format-spec nil)
300 (defvar gnus-cited-closed-text-button-line-format-alist
301 gnus-cited-opened-text-button-line-format-alist)
302 (defvar gnus-cited-closed-text-button-line-format-spec nil)
307 (defun gnus-article-highlight-citation (&optional force)
308 "Highlight cited text.
309 Each citation in the article will be highlighted with a different face.
310 The faces are taken from `gnus-cite-face-list'.
311 Attribution lines are highlighted with the same face as the
312 corresponding citation merged with `gnus-cite-attribution-face'.
314 Text is considered cited if at least `gnus-cite-minimum-match-count'
315 lines matches `message-cite-prefix-regexp' with the same prefix.
317 Lines matching `gnus-cite-attribution-suffix' and perhaps
318 `gnus-cite-attribution-prefix' are considered attribution lines."
319 (interactive (list 'force))
321 (set-buffer gnus-article-buffer)
322 (gnus-cite-parse-maybe force)
323 (let ((buffer-read-only nil)
324 (alist gnus-cite-prefix-alist)
325 (faces gnus-cite-face-list)
326 (inhibit-point-motion-hooks t)
327 face entry prefix skip numbers number face-alist)
328 ;; Loop through citation prefixes.
330 (setq entry (car alist)
335 faces (or (cdr faces) gnus-cite-face-list)
336 face-alist (cons (cons prefix face) face-alist))
338 (setq number (car numbers)
339 numbers (cdr numbers))
340 (and (not (assq number gnus-cite-attribution-alist))
341 (not (assq number gnus-cite-loose-attribution-alist))
342 (gnus-cite-add-face number prefix face))))
343 ;; Loop through attribution lines.
344 (setq alist gnus-cite-attribution-alist)
346 (setq entry (car alist)
350 skip (gnus-cite-find-prefix number)
351 face (cdr (assoc prefix face-alist)))
352 ;; Add attribution button.
353 (goto-char (point-min))
354 (forward-line (1- number))
355 (when (re-search-forward gnus-cite-attribution-suffix
356 (save-excursion (end-of-line 1) (point))
358 (gnus-article-add-button (match-beginning 1) (match-end 1)
359 'gnus-cite-toggle prefix))
360 ;; Highlight attribution line.
361 (gnus-cite-add-face number skip face)
362 (gnus-cite-add-face number skip gnus-cite-attribution-face))
363 ;; Loop through attribution lines.
364 (setq alist gnus-cite-loose-attribution-alist)
366 (setq entry (car alist)
369 skip (gnus-cite-find-prefix number))
370 (gnus-cite-add-face number skip gnus-cite-attribution-face)))))
372 (defun gnus-dissect-cited-text ()
373 "Dissect the article buffer looking for cited text."
375 (set-buffer gnus-article-buffer)
376 (gnus-cite-parse-maybe nil t)
377 (let ((alist gnus-cite-prefix-alist)
378 prefix numbers number marks m)
379 ;; Loop through citation prefixes.
381 (setq numbers (pop alist)
382 prefix (pop numbers))
384 (setq number (pop numbers))
385 (goto-char (point-min))
386 (forward-line number)
387 (push (cons (point-marker) "") marks)
389 (= (1- number) (car numbers)))
390 (setq number (pop numbers)))
391 (goto-char (point-min))
392 (forward-line (1- number))
393 (push (cons (point-marker) prefix) marks)))
394 ;; Skip to the beginning of the body.
396 (push (cons (point-marker) "") marks)
397 ;; Find the end of the body.
398 (goto-char (point-max))
399 (gnus-article-search-signature)
400 (push (cons (point-marker) "") marks)
402 (setq marks (sort marks 'car-less-than-car))
403 (let ((omarks marks))
406 (if (= (caar omarks) (caadr omarks))
408 (unless (equal (cdar omarks) "")
409 (push (car omarks) marks))
410 (unless (equal (cdadr omarks) "")
411 (push (cadr omarks) marks))
412 (unless (and (equal (cdar omarks) "")
413 (equal (cdadr omarks) "")
415 (setq omarks (cdr omarks))))
416 (push (car omarks) marks))
417 (setq omarks (cdr omarks)))
419 (push (car omarks) marks))
420 (setq marks (setq m (nreverse marks)))
422 (if (and (equal (cdadr m) "")
423 (equal (cdar m) (cdaddr m))
424 (goto-char (caadr m))
426 (= (point) (caaddr m)))
431 (defun gnus-article-fill-cited-article (&optional force width)
432 "Do word wrapping in the current article.
433 If WIDTH (the numerical prefix), use that text width when filling."
434 (interactive (list t current-prefix-arg))
436 (set-buffer gnus-article-buffer)
437 (let ((buffer-read-only nil)
438 (inhibit-point-motion-hooks t)
439 (marks (gnus-dissect-cited-text))
440 (adaptive-fill-mode nil)
442 (fill-column (if width (prefix-numeric-value width) fill-column)))
445 (narrow-to-region (caar marks) (caadr marks))
446 (let ((adaptive-fill-regexp
447 (concat "^" (regexp-quote (cdar marks)) " *"))
449 (if (string= (cdar marks) "") ""
450 (concat (cdar marks) " ")))
452 (fill-region (point-min) (point-max)))
453 (set-marker (caar marks) nil)
454 (setq marks (cdr marks)))
456 (set-marker (caar marks) nil))
457 ;; All this information is now incorrect.
458 (setq gnus-cite-prefix-alist nil
459 gnus-cite-attribution-alist nil
460 gnus-cite-loose-prefix-alist nil
461 gnus-cite-loose-attribution-alist nil
462 gnus-cite-article nil)))))
464 (defun gnus-article-hide-citation (&optional arg force)
465 "Toggle hiding of all cited text except attribution lines.
466 See the documentation for `gnus-article-highlight-citation'.
467 If given a negative prefix, always show; if given a positive prefix,
469 (interactive (append (gnus-article-hidden-arg) (list 'force)))
470 (gnus-set-format 'cited-opened-text-button t)
471 (gnus-set-format 'cited-closed-text-button t)
473 (set-buffer gnus-article-buffer)
474 (let ((buffer-read-only nil)
476 (inhibit-point-motion-hooks t)
477 (props (nconc (list 'article-type 'cite)
478 gnus-hidden-properties))
482 (text-property-any point (point-max)
484 'gnus-article-toggle-cited-text))
487 (gnus-article-toggle-cited-text
488 (get-text-property point 'gnus-data) arg)
490 (setq point (point)))
492 (setq marks (gnus-dissect-cited-text))
496 (while (and marks (string= (cdar marks) ""))
497 (setq marks (cdr marks)))
499 (setq beg (caar marks)))
500 (while (and marks (not (string= (cdar marks) "")))
501 (setq marks (cdr marks)))
503 (setq end (caar marks)))
504 ;; Skip past lines we want to leave visible.
505 (when (and beg end gnus-cited-lines-visible)
507 (forward-line (if (consp gnus-cited-lines-visible)
508 (car gnus-cited-lines-visible)
509 gnus-cited-lines-visible))
512 (setq beg (point-marker))
513 (when (consp gnus-cited-lines-visible)
515 (forward-line (- (cdr gnus-cited-lines-visible)))
518 (setq end (point-marker))))))
520 (gnus-add-wash-type 'cite)
521 ;; We use markers for the end-points to facilitate later
522 ;; wrapping and mangling of text.
523 (setq beg (set-marker (make-marker) beg)
524 end (set-marker (make-marker) end))
525 (gnus-add-text-properties-when 'article-type nil beg end props)
527 (when (and gnus-cite-blank-line-after-header
528 (not (save-excursion (search-backward "\n\n" nil t))))
531 (setq start (point-marker))
533 (gnus-article-add-button
535 (progn (eval gnus-cited-closed-text-button-line-format-spec)
537 `gnus-article-toggle-cited-text
538 (list (cons beg end) start))
540 'article-type 'annotation)
541 (set-marker beg (point))))))))
543 (defun gnus-article-toggle-cited-text (args &optional arg)
544 "Toggle hiding the text in REGION.
545 ARG can be nil or a number. Positive means hide, negative
546 means show, nil means toggle."
547 (let* ((region (car args))
552 (text-property-any beg (1- end) 'article-type 'cite))
553 (inhibit-point-motion-hooks t)
557 (and (> arg 0) (not hidden))
558 (and (< arg 0) hidden))
561 ;; Can't remove 'cite from g-a-wash-types here because
562 ;; multiple citations may be hidden -jas
563 (gnus-remove-text-properties-when
564 'article-type 'cite beg end
565 (cons 'article-type (cons 'cite
566 gnus-hidden-properties))))
567 (gnus-add-wash-type 'cite)
568 (gnus-add-text-properties-when
569 'article-type nil beg end
570 (cons 'article-type (cons 'cite
571 gnus-hidden-properties))))
572 (let ((gnus-article-mime-handle-alist-1 gnus-article-mime-handle-alist))
573 (gnus-set-mode-line 'article))
580 (gnus-article-add-button
584 gnus-cited-opened-text-button-line-format-spec
585 gnus-cited-closed-text-button-line-format-spec))
587 `gnus-article-toggle-cited-text
590 'article-type 'annotation)))))
592 (defun gnus-article-hide-citation-maybe (&optional arg force)
593 "Toggle hiding of cited text that has an attribution line.
594 If given a negative prefix, always show; if given a positive prefix,
596 This will do nothing unless at least `gnus-cite-hide-percentage'
597 percent and at least `gnus-cite-hide-absolute' lines of the body is
598 cited text with attributions. When called interactively, these two
599 variables are ignored.
600 See also the documentation for `gnus-article-highlight-citation'."
601 (interactive (append (gnus-article-hidden-arg) '(force)))
602 (unless (gnus-article-check-hidden-text 'cite arg)
604 (set-buffer gnus-article-buffer)
605 (gnus-cite-parse-maybe force)
607 (let ((start (point))
608 (atts gnus-cite-attribution-alist)
609 (buffer-read-only nil)
610 (inhibit-point-motion-hooks t)
613 (goto-char (point-max))
614 (gnus-article-search-signature)
615 (setq total (count-lines start (point)))
617 (setq hidden (+ hidden (length (cdr (assoc (cdar atts)
618 gnus-cite-prefix-alist))))
621 (and (> (* 100 hidden) (* gnus-cite-hide-percentage total))
622 (> hidden gnus-cite-hide-absolute)))
623 (setq atts gnus-cite-attribution-alist)
625 (setq total (cdr (assoc (cdar atts) gnus-cite-prefix-alist))
628 (setq hidden (car total)
630 (goto-char (point-min))
631 (forward-line (1- hidden))
632 (unless (assq hidden gnus-cite-attribution-alist)
633 (gnus-add-text-properties
634 (point) (progn (forward-line 1) (point))
635 (nconc (list 'article-type 'cite)
636 gnus-hidden-properties))))))))))
638 (defun gnus-article-hide-citation-in-followups ()
639 "Hide cited text in non-root articles."
642 (set-buffer gnus-article-buffer)
643 (let ((article (cdr gnus-article-current)))
644 (unless (save-excursion
645 (set-buffer gnus-summary-buffer)
646 (gnus-article-displayed-root-p article))
647 (gnus-article-hide-citation)))))
649 ;;; Internal functions:
651 (defun gnus-cite-parse-maybe (&optional force no-overlay)
652 "Always parse the buffer."
654 ;;Reset parser information.
655 (setq gnus-cite-prefix-alist nil
656 gnus-cite-attribution-alist nil
657 gnus-cite-loose-prefix-alist nil
658 gnus-cite-loose-attribution-alist nil)
660 (gnus-cite-delete-overlays))
661 ;; Parse if not too large.
662 (if (and gnus-cite-parse-max-size
663 (> (buffer-size) gnus-cite-parse-max-size))
665 (setq gnus-cite-article (cons (car gnus-article-current)
666 (cdr gnus-article-current)))
667 (gnus-cite-parse-wrapper)))
669 (defun gnus-cite-delete-overlays ()
670 (dolist (overlay gnus-cite-overlay-list)
671 (when (or (not (gnus-overlay-end overlay))
672 (and (>= (gnus-overlay-end overlay) (point-min))
673 (<= (gnus-overlay-end overlay) (point-max))))
674 (setq gnus-cite-overlay-list (delete overlay gnus-cite-overlay-list))
675 (gnus-delete-overlay overlay))))
677 (defun gnus-cite-parse-wrapper ()
678 ;; Wrap chopped gnus-cite-parse.
680 (let ((inhibit-point-motion-hooks t))
682 (gnus-cite-parse-attributions))
686 (gnus-cite-connect-attributions))))
688 (defun gnus-cite-parse ()
689 ;; Parse and connect citation prefixes and attribution lines.
691 ;; Parse current buffer searching for citation prefixes.
692 (let ((line (1+ (count-lines (point-min) (point))))
695 (goto-char (point-max))
696 (gnus-article-search-signature)
698 (prefix-regexp (concat "^\\(" message-cite-prefix-regexp "\\)"))
699 alist entry start begin end numbers prefix guess-limit mc-flag)
700 ;; Get all potential prefixes in `alist'.
701 (while (< (point) max)
704 guess-limit (progn (skip-chars-forward "^> \t\r\n") (point))
705 end (progn (beginning-of-line 2) (point))
708 ;; Ignore standard Supercite attribution prefix.
709 (when (and (< guess-limit (+ begin gnus-cite-max-prefix))
710 (looking-at gnus-supercite-regexp))
712 (setq end (1+ (match-end 1)))
713 (setq end (1+ begin))))
714 ;; Ignore very long prefixes.
715 (when (> end (+ begin gnus-cite-max-prefix))
716 (setq end (+ begin gnus-cite-max-prefix)))
717 (while (re-search-forward prefix-regexp (1- end) t)
719 (setq end (match-end 0)
720 prefix (buffer-substring begin end))
721 (gnus-set-text-properties 0 (length prefix) nil prefix)
722 (setq entry (assoc prefix alist))
724 (setcdr entry (cons line (cdr entry)))
725 (push (list prefix line) alist))
728 (setq line (1+ line)))
729 ;; We got all the potential prefixes. Now create
730 ;; `gnus-cite-prefix-alist' containing the oldest prefix for each
731 ;; line that appears at least gnus-cite-minimum-match-count
732 ;; times. First sort them by length. Longer is older.
733 (setq alist (sort alist (lambda (a b)
734 (> (length (car a)) (length (car b))))))
736 (setq entry (car alist)
740 (cond ((null numbers)
741 ;; No lines with this prefix that wasn't also part of
744 ((< (length numbers) gnus-cite-minimum-match-count)
745 ;; Too few lines with this prefix. We keep it a bit
746 ;; longer in case it is an exact match for an attribution
747 ;; line, but we don't remove the line from other
749 (push entry gnus-cite-prefix-alist))
752 gnus-cite-prefix-alist)
753 ;; Remove articles from other prefixes.
757 (setq current (car loop)
760 (gnus-set-difference (cdr current) numbers)))))))))
762 (defun gnus-cite-parse-attributions ()
764 ;; Parse attributions
765 (while (re-search-forward gnus-cite-attribution-suffix (point-max) t)
766 (let* ((start (match-beginning 0))
768 (wrote (count-lines (point-min) end))
769 (prefix (gnus-cite-find-prefix wrote))
770 ;; Check previous line for an attribution leader.
772 (beginning-of-line 1)
773 (when (looking-at gnus-supercite-secondary-regexp)
774 (buffer-substring (match-beginning 1)
778 (and (re-search-backward gnus-cite-attribution-prefix
780 (beginning-of-line 0)
783 (not (re-search-forward gnus-cite-attribution-suffix
785 (count-lines (point-min) (1+ (point)))))))
789 ;; don't add duplicates
790 (let ((al (buffer-substring (save-excursion (beginning-of-line 0)
793 (if (not (assoc al al-alist))
795 (push (list wrote in prefix tag)
796 gnus-cite-loose-attribution-alist)
797 (push (cons al t) al-alist))))))))
799 (defun gnus-cite-connect-attributions ()
800 ;; Connect attributions to citations
802 ;; No citations have been connected to attribution lines yet.
803 (setq gnus-cite-loose-prefix-alist (append gnus-cite-prefix-alist nil))
805 ;; Parse current buffer searching for attribution lines.
806 ;; Find exact supercite citations.
807 (gnus-cite-match-attributions 'small nil
811 (regexp-quote prefix) "[ \t]*"
812 (regexp-quote tag) ">"))))
813 ;; Find loose supercite citations after attributions.
814 (gnus-cite-match-attributions 'small t
820 ;; Find loose supercite citations anywhere.
821 (gnus-cite-match-attributions 'small nil
827 ;; Find nested citations after attributions.
828 (gnus-cite-match-attributions 'small-if-unique t
830 (concat "\\`" (regexp-quote prefix) ".+")))
831 ;; Find nested citations anywhere.
832 (gnus-cite-match-attributions 'small nil
834 (concat "\\`" (regexp-quote prefix) ".+")))
835 ;; Remove loose prefixes with too few lines.
836 (let ((alist gnus-cite-loose-prefix-alist)
839 (setq entry (car alist)
841 (when (< (length (cdr entry)) gnus-cite-minimum-match-count)
842 (setq gnus-cite-prefix-alist
843 (delq entry gnus-cite-prefix-alist)
844 gnus-cite-loose-prefix-alist
845 (delq entry gnus-cite-loose-prefix-alist)))))
846 ;; Find flat attributions.
847 (gnus-cite-match-attributions 'first t nil)
848 ;; Find any attributions (are we getting desperate yet?).
849 (gnus-cite-match-attributions 'first nil nil))
851 (defun gnus-cite-match-attributions (sort after fun)
852 ;; Match all loose attributions and citations (SORT AFTER FUN) .
854 ;; If SORT is `small', the citation with the shortest prefix will be
855 ;; used, if it is `first' the first prefix will be used, if it is
856 ;; `small-if-unique' the shortest prefix will be used if the
857 ;; attribution line does not share its own prefix with other
858 ;; loose attribution lines, otherwise the first prefix will be used.
860 ;; If AFTER is non-nil, only citations after the attribution line
861 ;; will be considered.
863 ;; If FUN is non-nil, it will be called with the arguments (WROTE
864 ;; PREFIX TAG) and expected to return a regular expression. Only
865 ;; citations whose prefix matches the regular expression will be
868 ;; WROTE is the attribution line number.
869 ;; PREFIX is the attribution line prefix.
870 ;; TAG is the Supercite tag on the attribution line.
871 (let ((atts gnus-cite-loose-attribution-alist)
873 att wrote in prefix tag regexp limit smallest best size)
881 regexp (if fun (funcall fun prefix tag) "")
882 size (cond ((eq sort 'small) t)
883 ((eq sort 'first) nil)
884 (t (< (length (gnus-cite-find-loose prefix)) 2)))
885 limit (if after wrote -1)
888 (let ((cites gnus-cite-loose-prefix-alist)
889 cite candidate numbers first compare)
891 (setq cite (car cites)
895 first (apply 'min numbers)
896 compare (if size (length candidate) first))
899 (string-match regexp candidate)
905 (setq gnus-cite-loose-attribution-alist
906 (delq att gnus-cite-loose-attribution-alist))
907 (push (cons wrote (car best)) gnus-cite-attribution-alist)
909 (push (cons in (car best)) gnus-cite-attribution-alist))
910 (when (memq best gnus-cite-loose-prefix-alist)
911 (let ((loop gnus-cite-prefix-alist)
914 (setq gnus-cite-loose-prefix-alist
915 (delq best gnus-cite-loose-prefix-alist))
917 (setq current (car loop)
919 (if (eq current best)
921 (setcdr current (gnus-set-difference (cdr current) numbers))
922 (when (null (cdr current))
923 (setq gnus-cite-loose-prefix-alist
924 (delq current gnus-cite-loose-prefix-alist)
925 atts (delq current atts)))))))))))
927 (defun gnus-cite-find-loose (prefix)
928 ;; Return a list of loose attribution lines prefixed by PREFIX.
929 (let* ((atts gnus-cite-loose-attribution-alist)
935 (when (string-equal (gnus-cite-find-prefix line) prefix)
939 (defun gnus-cite-add-face (number prefix face)
940 ;; At line NUMBER, ignore PREFIX and add FACE to the rest of the line.
942 (let ((inhibit-point-motion-hooks t)
944 (goto-char (point-min))
945 (when (zerop (forward-line (1- number)))
946 (static-if (or (featurep 'xemacs)
947 (and (eq emacs-major-version 20)
948 (>= emacs-minor-version 3))
949 (>= emacs-major-version 21))
950 (forward-char (length prefix))
951 (move-to-column (string-width prefix)))
952 (skip-chars-forward " \t")
955 (skip-chars-backward " \t")
958 (push (setq overlay (gnus-make-overlay from to))
959 gnus-cite-overlay-list)
960 (gnus-overlay-put overlay 'face face))))))
962 (defun gnus-cite-toggle (prefix)
964 (set-buffer gnus-article-buffer)
965 (gnus-cite-parse-maybe nil t)
966 (let ((buffer-read-only nil)
967 (numbers (cdr (assoc prefix gnus-cite-prefix-alist)))
968 (inhibit-point-motion-hooks t)
971 (setq number (car numbers)
972 numbers (cdr numbers))
973 (goto-char (point-min))
974 (forward-line (1- number))
975 (cond ((get-text-property (point) 'invisible)
976 ;; Can't remove 'cite from g-a-wash-types here because
977 ;; multiple citations may be hidden -jas
978 (remove-text-properties (point) (progn (forward-line 1) (point))
979 gnus-hidden-properties))
980 ((assq number gnus-cite-attribution-alist))
982 (gnus-add-wash-type 'cite)
983 (gnus-add-text-properties
984 (point) (progn (forward-line 1) (point))
985 (nconc (list 'article-type 'cite)
986 gnus-hidden-properties))))
987 (let ((gnus-article-mime-handle-alist-1
988 gnus-article-mime-handle-alist))
989 (gnus-set-mode-line 'article))))))
991 (defun gnus-cite-find-prefix (line)
992 ;; Return citation prefix for LINE.
993 (let ((alist gnus-cite-prefix-alist)
997 (setq entry (car alist)
999 (when (memq line (cdr entry))
1000 (setq prefix (car entry))))
1003 (defun gnus-cite-localize ()
1004 "Make the citation variables local to the article buffer."
1005 (let ((vars '(gnus-cite-article
1006 gnus-cite-overlay-list gnus-cite-prefix-alist
1007 gnus-cite-attribution-alist gnus-cite-loose-prefix-alist
1008 gnus-cite-loose-attribution-alist)))
1010 (make-local-variable (pop vars)))))
1014 (provide 'gnus-cite)
1017 ;; coding: iso-8859-1
1020 ;;; gnus-cite.el ends here