1 ;;; gnus-cite.el --- parse citations in articles for Gnus -*- coding: iso-latin-1 -*-
3 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000
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-cite-reply-regexp
45 "^\\(Subject: Re\\|In-Reply-To\\|References\\):"
46 "*If headers match this regexp it is reasonable to believe that
47 article has citations."
51 (defcustom gnus-cite-always-check nil
52 "Check article always for citations. Set it t to check all articles."
54 :type '(choice (const :tag "no" nil)
55 (const :tag "yes" t)))
57 (defcustom gnus-cited-opened-text-button-line-format "%(%{[-]%}%)\n"
58 "Format of opened cited text buttons."
62 (defcustom gnus-cited-closed-text-button-line-format "%(%{[+]%}%)\n"
63 "Format of closed cited text buttons."
67 (defcustom gnus-cited-lines-visible nil
68 "The number of lines of hidden cited text to remain visible.
69 Or a pair (cons) of numbers which are the number of lines at the top
70 and bottom of the text, respectively, to remain visible."
72 :type '(choice (const :tag "none" nil)
74 (cons :tag "Top and Bottom" integer integer)))
76 (defcustom gnus-cite-parse-max-size 25000
77 "Maximum article size (in bytes) where parsing citations is allowed.
78 Set it to nil to parse all articles."
80 :type '(choice (const :tag "all" nil)
83 (defcustom gnus-cite-max-prefix 20
84 "Maximum possible length for a citation prefix."
88 (defcustom gnus-supercite-regexp
89 (concat "^\\(" message-cite-prefix-regexp "\\)? *"
90 ">>>>> +\"\\([^\"\n]+\\)\" +==")
91 "*Regexp matching normal Supercite attribution lines.
92 The first grouping must match prefixes added by other packages."
96 (defcustom gnus-supercite-secondary-regexp "^.*\"\\([^\"\n]+\\)\" +=="
97 "Regexp matching mangled Supercite attribution lines.
98 The first regexp group should match the Supercite attribution."
102 (defcustom gnus-cite-minimum-match-count 2
103 "Minimum number of identical prefixes before we believe it's a citation."
107 (defcustom gnus-cite-attribution-prefix
108 "In article\\|in <\\|On \\(Mon\\|Tue\\|Wed\\|Thu\\|Fri\\|Sat\\|Sun\\),\\|-----Original Message-----"
109 "*Regexp matching the beginning of an attribution line."
113 (defcustom gnus-cite-attribution-suffix
114 "\\(\\(wrote\\|writes\\|said\\|says\\|>\\)\\(:\\|\\.\\.\\.\\)\\|-----Original Message-----\\)[ \t]*$"
115 "*Regexp matching the end of an attribution line.
116 The text matching the first grouping will be used as a button."
120 (defface gnus-cite-attribution-face '((t
122 "Face used for attribution lines.")
124 (defcustom gnus-cite-attribution-face 'gnus-cite-attribution-face
125 "Face used for attribution lines.
126 It is merged with the face for the cited text belonging to the attribution."
130 (defface gnus-cite-face-1 '((((class color)
132 (:foreground "light blue"))
135 (:foreground "MidnightBlue"))
140 (defface gnus-cite-face-2 '((((class color)
142 (:foreground "light cyan"))
145 (:foreground "firebrick"))
150 (defface gnus-cite-face-3 '((((class color)
152 (:foreground "light yellow"))
155 (:foreground "dark green"))
160 (defface gnus-cite-face-4 '((((class color)
162 (:foreground "light pink"))
165 (:foreground "OrangeRed"))
170 (defface gnus-cite-face-5 '((((class color)
172 (:foreground "pale green"))
175 (:foreground "dark khaki"))
180 (defface gnus-cite-face-6 '((((class color)
182 (:foreground "beige"))
185 (:foreground "dark violet"))
190 (defface gnus-cite-face-7 '((((class color)
192 (:foreground "orange"))
195 (:foreground "SteelBlue4"))
200 (defface gnus-cite-face-8 '((((class color)
202 (:foreground "magenta"))
205 (:foreground "magenta"))
210 (defface gnus-cite-face-9 '((((class color)
212 (:foreground "violet"))
215 (:foreground "violet"))
220 (defface gnus-cite-face-10 '((((class color)
222 (:foreground "medium purple"))
225 (:foreground "medium purple"))
230 (defface gnus-cite-face-11 '((((class color)
232 (:foreground "turquoise"))
235 (:foreground "turquoise"))
240 (defcustom gnus-cite-face-list
241 '(gnus-cite-face-1 gnus-cite-face-2 gnus-cite-face-3 gnus-cite-face-4
242 gnus-cite-face-5 gnus-cite-face-6 gnus-cite-face-7 gnus-cite-face-8
243 gnus-cite-face-9 gnus-cite-face-10 gnus-cite-face-11)
244 "*List of faces used for highlighting citations.
246 When there are citations from multiple articles in the same message,
247 Gnus will try to give each citation from each article its own face.
248 This should make it easier to see who wrote what."
250 :type '(repeat face))
252 (defcustom gnus-cite-hide-percentage 50
253 "Only hide excess citation if above this percentage of the body."
257 (defcustom gnus-cite-hide-absolute 10
258 "Only hide excess citation if above this number of lines in the body."
262 ;;; Internal Variables:
264 (defvar gnus-cite-article nil)
265 (defvar gnus-cite-overlay-list nil)
267 (defvar gnus-cite-prefix-alist nil)
268 ;; Alist of citation prefixes.
269 ;; The cdr is a list of lines with that prefix.
271 (defvar gnus-cite-attribution-alist nil)
272 ;; Alist of attribution lines.
273 ;; The car is a line number.
274 ;; The cdr is the prefix for the citation started by that line.
276 (defvar gnus-cite-loose-prefix-alist nil)
277 ;; Alist of citation prefixes that have no matching attribution.
278 ;; The cdr is a list of lines with that prefix.
280 (defvar gnus-cite-loose-attribution-alist nil)
281 ;; Alist of attribution lines that have no matching citation.
282 ;; Each member has the form (WROTE IN PREFIX TAG), where
283 ;; WROTE: is the attribution line number
284 ;; IN: is the line number of the previous line if part of the same attribution,
285 ;; PREFIX: Is the citation prefix of the attribution line(s), and
286 ;; TAG: Is a Supercite tag, if any.
288 (defvar gnus-cited-opened-text-button-line-format-alist
289 `((?b (marker-position beg) ?d)
290 (?e (marker-position end) ?d)
291 (?n (count-lines beg end) ?d)
292 (?l (- end beg) ?d)))
293 (defvar gnus-cited-opened-text-button-line-format-spec nil)
294 (defvar gnus-cited-closed-text-button-line-format-alist
295 gnus-cited-opened-text-button-line-format-alist)
296 (defvar gnus-cited-closed-text-button-line-format-spec nil)
301 (defun gnus-article-highlight-citation (&optional force)
302 "Highlight cited text.
303 Each citation in the article will be highlighted with a different face.
304 The faces are taken from `gnus-cite-face-list'.
305 Attribution lines are highlighted with the same face as the
306 corresponding citation merged with `gnus-cite-attribution-face'.
308 Text is considered cited if at least `gnus-cite-minimum-match-count'
309 lines matches `message-cite-prefix-regexp' with the same prefix.
311 Lines matching `gnus-cite-attribution-suffix' and perhaps
312 `gnus-cite-attribution-prefix' are considered attribution lines."
313 (interactive (list 'force))
315 (set-buffer gnus-article-buffer)
316 (gnus-cite-parse-maybe force)
317 (let ((buffer-read-only nil)
318 (alist gnus-cite-prefix-alist)
319 (faces gnus-cite-face-list)
320 (inhibit-point-motion-hooks t)
321 face entry prefix skip numbers number face-alist)
322 ;; Loop through citation prefixes.
324 (setq entry (car alist)
329 faces (or (cdr faces) gnus-cite-face-list)
330 face-alist (cons (cons prefix face) face-alist))
332 (setq number (car numbers)
333 numbers (cdr numbers))
334 (and (not (assq number gnus-cite-attribution-alist))
335 (not (assq number gnus-cite-loose-attribution-alist))
336 (gnus-cite-add-face number prefix face))))
337 ;; Loop through attribution lines.
338 (setq alist gnus-cite-attribution-alist)
340 (setq entry (car alist)
344 skip (gnus-cite-find-prefix number)
345 face (cdr (assoc prefix face-alist)))
346 ;; Add attribution button.
347 (goto-char (point-min))
348 (forward-line (1- number))
349 (when (re-search-forward gnus-cite-attribution-suffix
350 (save-excursion (end-of-line 1) (point))
352 (gnus-article-add-button (match-beginning 1) (match-end 1)
353 'gnus-cite-toggle prefix))
354 ;; Highlight attribution line.
355 (gnus-cite-add-face number skip face)
356 (gnus-cite-add-face number skip gnus-cite-attribution-face))
357 ;; Loop through attribution lines.
358 (setq alist gnus-cite-loose-attribution-alist)
360 (setq entry (car alist)
363 skip (gnus-cite-find-prefix number))
364 (gnus-cite-add-face number skip gnus-cite-attribution-face)))))
366 (defun gnus-dissect-cited-text ()
367 "Dissect the article buffer looking for cited text."
369 (set-buffer gnus-article-buffer)
370 (gnus-cite-parse-maybe nil t)
371 (let ((alist gnus-cite-prefix-alist)
372 prefix numbers number marks m)
373 ;; Loop through citation prefixes.
375 (setq numbers (pop alist)
376 prefix (pop numbers))
378 (setq number (pop numbers))
379 (goto-char (point-min))
380 (forward-line number)
381 (push (cons (point-marker) "") marks)
383 (= (1- number) (car numbers)))
384 (setq number (pop numbers)))
385 (goto-char (point-min))
386 (forward-line (1- number))
387 (push (cons (point-marker) prefix) marks)))
388 ;; Skip to the beginning of the body.
390 (push (cons (point-marker) "") marks)
391 ;; Find the end of the body.
392 (goto-char (point-max))
393 (gnus-article-search-signature)
394 (push (cons (point-marker) "") marks)
396 (setq marks (sort marks 'car-less-than-car))
397 (let ((omarks marks))
400 (if (= (caar omarks) (caadr omarks))
402 (unless (equal (cdar omarks) "")
403 (push (car omarks) marks))
404 (unless (equal (cdadr omarks) "")
405 (push (cadr omarks) marks))
406 (unless (and (equal (cdar omarks) "")
407 (equal (cdadr omarks) "")
409 (setq omarks (cdr omarks))))
410 (push (car omarks) marks))
411 (setq omarks (cdr omarks)))
413 (push (car omarks) marks))
414 (setq marks (setq m (nreverse marks)))
416 (if (and (equal (cdadr m) "")
417 (equal (cdar m) (cdaddr m))
418 (goto-char (caadr m))
420 (= (point) (caaddr m)))
425 (defun gnus-article-fill-cited-article (&optional force width)
426 "Do word wrapping in the current article.
427 If WIDTH (the numerical prefix), use that text width when filling."
428 (interactive (list t current-prefix-arg))
430 (set-buffer gnus-article-buffer)
431 (let ((buffer-read-only nil)
432 (inhibit-point-motion-hooks t)
433 (marks (gnus-dissect-cited-text))
434 (adaptive-fill-mode nil)
436 (fill-column (if width (prefix-numeric-value width) fill-column)))
439 (narrow-to-region (caar marks) (caadr marks))
440 (let ((adaptive-fill-regexp
441 (concat "^" (regexp-quote (cdar marks)) " *"))
443 (if (string= (cdar marks) "") ""
444 (concat (cdar marks) " "))))
445 (fill-region (point-min) (point-max)))
446 (set-marker (caar marks) nil)
447 (setq marks (cdr marks)))
449 (set-marker (caar marks) nil))
450 ;; All this information is now incorrect.
451 (setq gnus-cite-prefix-alist nil
452 gnus-cite-attribution-alist nil
453 gnus-cite-loose-prefix-alist nil
454 gnus-cite-loose-attribution-alist nil
455 gnus-cite-article nil)))))
457 (defun gnus-article-hide-citation (&optional arg force)
458 "Toggle hiding of all cited text except attribution lines.
459 See the documentation for `gnus-article-highlight-citation'.
460 If given a negative prefix, always show; if given a positive prefix,
462 (interactive (append (gnus-article-hidden-arg) (list 'force)))
463 (gnus-set-format 'cited-opened-text-button t)
464 (gnus-set-format 'cited-closed-text-button t)
466 (set-buffer gnus-article-buffer)
467 (let ((buffer-read-only nil)
469 (inhibit-point-motion-hooks t)
470 (props (nconc (list 'article-type 'cite)
471 gnus-hidden-properties))
475 (text-property-any point (point-max)
477 'gnus-article-toggle-cited-text))
480 (gnus-article-toggle-cited-text
481 (get-text-property point 'gnus-data) arg)
483 (setq point (point)))
485 (setq marks (gnus-dissect-cited-text))
489 (while (and marks (string= (cdar marks) ""))
490 (setq marks (cdr marks)))
492 (setq beg (caar marks)))
493 (while (and marks (not (string= (cdar marks) "")))
494 (setq marks (cdr marks)))
496 (setq end (caar marks)))
497 ;; Skip past lines we want to leave visible.
498 (when (and beg end gnus-cited-lines-visible)
500 (forward-line (if (consp gnus-cited-lines-visible)
501 (car gnus-cited-lines-visible)
502 gnus-cited-lines-visible))
505 (setq beg (point-marker))
506 (when (consp gnus-cited-lines-visible)
508 (forward-line (- (cdr gnus-cited-lines-visible)))
511 (setq end (point-marker))))))
513 ;; We use markers for the end-points to facilitate later
514 ;; wrapping and mangling of text.
515 (setq beg (set-marker (make-marker) beg)
516 end (set-marker (make-marker) end))
517 (gnus-add-text-properties-when 'article-type nil beg end props)
519 (unless (save-excursion (search-backward "\n\n" nil t))
522 (setq start (point-marker))
524 (gnus-article-add-button
526 (progn (eval gnus-cited-closed-text-button-line-format-spec)
528 `gnus-article-toggle-cited-text
529 (list (cons beg end) start))
531 'article-type 'annotation)
532 (set-marker beg (point))))))))
534 (defun gnus-article-toggle-cited-text (args &optional arg)
535 "Toggle hiding the text in REGION.
536 ARG can be nil or a number. Positive means hide, negative
537 means show, nil means toggle."
538 (let* ((region (car args))
543 (text-property-any beg (1- end) 'article-type 'cite))
544 (inhibit-point-motion-hooks t)
548 (and (> arg 0) (not hidden))
549 (and (< arg 0) hidden))
551 (gnus-remove-text-properties-when
552 'article-type 'cite beg end
553 (cons 'article-type (cons 'cite
554 gnus-hidden-properties)))
555 (gnus-add-text-properties-when
556 'article-type nil beg end
557 (cons 'article-type (cons 'cite
558 gnus-hidden-properties))))
565 (gnus-article-add-button
569 gnus-cited-opened-text-button-line-format-spec
570 gnus-cited-closed-text-button-line-format-spec))
572 `gnus-article-toggle-cited-text
575 'article-type 'annotation)))))
577 (defun gnus-article-hide-citation-maybe (&optional arg force)
578 "Toggle hiding of cited text that has an attribution line.
579 If given a negative prefix, always show; if given a positive prefix,
581 This will do nothing unless at least `gnus-cite-hide-percentage'
582 percent and at least `gnus-cite-hide-absolute' lines of the body is
583 cited text with attributions. When called interactively, these two
584 variables are ignored.
585 See also the documentation for `gnus-article-highlight-citation'."
586 (interactive (append (gnus-article-hidden-arg) '(force)))
587 (unless (gnus-article-check-hidden-text 'cite arg)
589 (set-buffer gnus-article-buffer)
590 (gnus-cite-parse-maybe force)
592 (let ((start (point))
593 (atts gnus-cite-attribution-alist)
594 (buffer-read-only nil)
595 (inhibit-point-motion-hooks t)
598 (goto-char (point-max))
599 (gnus-article-search-signature)
600 (setq total (count-lines start (point)))
602 (setq hidden (+ hidden (length (cdr (assoc (cdar atts)
603 gnus-cite-prefix-alist))))
606 (and (> (* 100 hidden) (* gnus-cite-hide-percentage total))
607 (> hidden gnus-cite-hide-absolute)))
608 (setq atts gnus-cite-attribution-alist)
610 (setq total (cdr (assoc (cdar atts) gnus-cite-prefix-alist))
613 (setq hidden (car total)
615 (goto-char (point-min))
616 (forward-line (1- hidden))
617 (unless (assq hidden gnus-cite-attribution-alist)
618 (gnus-add-text-properties
619 (point) (progn (forward-line 1) (point))
620 (nconc (list 'article-type 'cite)
621 gnus-hidden-properties))))))))))
623 (defun gnus-article-hide-citation-in-followups ()
624 "Hide cited text in non-root articles."
627 (set-buffer gnus-article-buffer)
628 (let ((article (cdr gnus-article-current)))
629 (unless (save-excursion
630 (set-buffer gnus-summary-buffer)
631 (gnus-article-displayed-root-p article))
632 (gnus-article-hide-citation)))))
634 ;;; Internal functions:
636 (defun gnus-cite-parse-maybe (&optional force no-overlay)
637 "Always parse the buffer."
639 ;;Reset parser information.
640 (setq gnus-cite-prefix-alist nil
641 gnus-cite-attribution-alist nil
642 gnus-cite-loose-prefix-alist nil
643 gnus-cite-loose-attribution-alist nil)
645 (gnus-cite-delete-overlays))
646 ;; Parse if not too large.
647 (if (and gnus-cite-parse-max-size
648 (> (buffer-size) gnus-cite-parse-max-size))
650 (setq gnus-cite-article (cons (car gnus-article-current)
651 (cdr gnus-article-current)))
652 (gnus-cite-parse-wrapper)))
654 (defun gnus-cite-delete-overlays ()
655 (dolist (overlay gnus-cite-overlay-list)
656 (when (or (not (gnus-overlay-end overlay))
657 (and (>= (gnus-overlay-end overlay) (point-min))
658 (<= (gnus-overlay-end overlay) (point-max))))
659 (setq gnus-cite-overlay-list (delete overlay gnus-cite-overlay-list))
660 (gnus-delete-overlay overlay))))
662 (defun gnus-cite-parse-wrapper ()
663 ;; Wrap chopped gnus-cite-parse.
665 (let ((inhibit-point-motion-hooks t))
667 (gnus-cite-parse-attributions))
671 (gnus-cite-connect-attributions))))
673 (defun gnus-cite-parse ()
674 ;; Parse and connect citation prefixes and attribution lines.
676 ;; Parse current buffer searching for citation prefixes.
677 (let ((line (1+ (count-lines (point-min) (point))))
680 (goto-char (point-max))
681 (gnus-article-search-signature)
683 (prefix-regexp (concat "^\\(" message-cite-prefix-regexp "\\)"))
684 alist entry start begin end numbers prefix guess-limit)
685 ;; Get all potential prefixes in `alist'.
686 (while (< (point) max)
689 guess-limit (progn (skip-chars-forward "^> \t\r\n") (point))
690 end (progn (beginning-of-line 2) (point))
693 ;; Ignore standard Supercite attribution prefix.
694 (when (and (< guess-limit (+ begin gnus-cite-max-prefix))
695 (looking-at gnus-supercite-regexp))
697 (setq end (1+ (match-end 1)))
698 (setq end (1+ begin))))
699 ;; Ignore very long prefixes.
700 (when (> end (+ begin gnus-cite-max-prefix))
701 (setq end (+ begin gnus-cite-max-prefix)))
702 (while (re-search-forward prefix-regexp (1- end) t)
704 (setq end (match-end 0)
705 prefix (buffer-substring begin end))
706 (gnus-set-text-properties 0 (length prefix) nil prefix)
707 (setq entry (assoc prefix alist))
709 (setcdr entry (cons line (cdr entry)))
710 (push (list prefix line) alist))
713 (setq line (1+ line)))
714 ;; We got all the potential prefixes. Now create
715 ;; `gnus-cite-prefix-alist' containing the oldest prefix for each
716 ;; line that appears at least gnus-cite-minimum-match-count
717 ;; times. First sort them by length. Longer is older.
718 (setq alist (sort alist (lambda (a b)
719 (> (length (car a)) (length (car b))))))
721 (setq entry (car alist)
725 (cond ((null numbers)
726 ;; No lines with this prefix that wasn't also part of
729 ((< (length numbers) gnus-cite-minimum-match-count)
730 ;; Too few lines with this prefix. We keep it a bit
731 ;; longer in case it is an exact match for an attribution
732 ;; line, but we don't remove the line from other
734 (push entry gnus-cite-prefix-alist))
737 gnus-cite-prefix-alist)
738 ;; Remove articles from other prefixes.
742 (setq current (car loop)
745 (gnus-set-difference (cdr current) numbers)))))))))
747 (defun gnus-cite-parse-attributions ()
749 ;; Parse attributions
750 (while (re-search-forward gnus-cite-attribution-suffix (point-max) t)
751 (let* ((start (match-beginning 0))
753 (wrote (count-lines (point-min) end))
754 (prefix (gnus-cite-find-prefix wrote))
755 ;; Check previous line for an attribution leader.
757 (beginning-of-line 1)
758 (when (looking-at gnus-supercite-secondary-regexp)
759 (buffer-substring (match-beginning 1)
763 (and (re-search-backward gnus-cite-attribution-prefix
765 (beginning-of-line 0)
768 (not (re-search-forward gnus-cite-attribution-suffix
770 (count-lines (point-min) (1+ (point)))))))
774 ;; don't add duplicates
775 (let ((al (buffer-substring (save-excursion (beginning-of-line 0)
778 (if (not (assoc al al-alist))
780 (push (list wrote in prefix tag)
781 gnus-cite-loose-attribution-alist)
782 (push (cons al t) al-alist))))))))
784 (defun gnus-cite-connect-attributions ()
785 ;; Connect attributions to citations
787 ;; No citations have been connected to attribution lines yet.
788 (setq gnus-cite-loose-prefix-alist (append gnus-cite-prefix-alist nil))
790 ;; Parse current buffer searching for attribution lines.
791 ;; Find exact supercite citations.
792 (gnus-cite-match-attributions 'small nil
796 (regexp-quote prefix) "[ \t]*"
797 (regexp-quote tag) ">"))))
798 ;; Find loose supercite citations after attributions.
799 (gnus-cite-match-attributions 'small t
805 ;; Find loose supercite citations anywhere.
806 (gnus-cite-match-attributions 'small nil
812 ;; Find nested citations after attributions.
813 (gnus-cite-match-attributions 'small-if-unique t
815 (concat "\\`" (regexp-quote prefix) ".+")))
816 ;; Find nested citations anywhere.
817 (gnus-cite-match-attributions 'small nil
819 (concat "\\`" (regexp-quote prefix) ".+")))
820 ;; Remove loose prefixes with too few lines.
821 (let ((alist gnus-cite-loose-prefix-alist)
824 (setq entry (car alist)
826 (when (< (length (cdr entry)) gnus-cite-minimum-match-count)
827 (setq gnus-cite-prefix-alist
828 (delq entry gnus-cite-prefix-alist)
829 gnus-cite-loose-prefix-alist
830 (delq entry gnus-cite-loose-prefix-alist)))))
831 ;; Find flat attributions.
832 (gnus-cite-match-attributions 'first t nil)
833 ;; Find any attributions (are we getting desperate yet?).
834 (gnus-cite-match-attributions 'first nil nil))
836 (defun gnus-cite-match-attributions (sort after fun)
837 ;; Match all loose attributions and citations (SORT AFTER FUN) .
839 ;; If SORT is `small', the citation with the shortest prefix will be
840 ;; used, if it is `first' the first prefix will be used, if it is
841 ;; `small-if-unique' the shortest prefix will be used if the
842 ;; attribution line does not share its own prefix with other
843 ;; loose attribution lines, otherwise the first prefix will be used.
845 ;; If AFTER is non-nil, only citations after the attribution line
846 ;; will be considered.
848 ;; If FUN is non-nil, it will be called with the arguments (WROTE
849 ;; PREFIX TAG) and expected to return a regular expression. Only
850 ;; citations whose prefix matches the regular expression will be
853 ;; WROTE is the attribution line number.
854 ;; PREFIX is the attribution line prefix.
855 ;; TAG is the Supercite tag on the attribution line.
856 (let ((atts gnus-cite-loose-attribution-alist)
858 att wrote in prefix tag regexp limit smallest best size)
866 regexp (if fun (funcall fun prefix tag) "")
867 size (cond ((eq sort 'small) t)
868 ((eq sort 'first) nil)
869 (t (< (length (gnus-cite-find-loose prefix)) 2)))
870 limit (if after wrote -1)
873 (let ((cites gnus-cite-loose-prefix-alist)
874 cite candidate numbers first compare)
876 (setq cite (car cites)
880 first (apply 'min numbers)
881 compare (if size (length candidate) first))
884 (string-match regexp candidate)
890 (setq gnus-cite-loose-attribution-alist
891 (delq att gnus-cite-loose-attribution-alist))
892 (push (cons wrote (car best)) gnus-cite-attribution-alist)
894 (push (cons in (car best)) gnus-cite-attribution-alist))
895 (when (memq best gnus-cite-loose-prefix-alist)
896 (let ((loop gnus-cite-prefix-alist)
899 (setq gnus-cite-loose-prefix-alist
900 (delq best gnus-cite-loose-prefix-alist))
902 (setq current (car loop)
904 (if (eq current best)
906 (setcdr current (gnus-set-difference (cdr current) numbers))
907 (when (null (cdr current))
908 (setq gnus-cite-loose-prefix-alist
909 (delq current gnus-cite-loose-prefix-alist)
910 atts (delq current atts)))))))))))
912 (defun gnus-cite-find-loose (prefix)
913 ;; Return a list of loose attribution lines prefixed by PREFIX.
914 (let* ((atts gnus-cite-loose-attribution-alist)
920 (when (string-equal (gnus-cite-find-prefix line) prefix)
924 (defun gnus-cite-add-face (number prefix face)
925 ;; At line NUMBER, ignore PREFIX and add FACE to the rest of the line.
927 (let ((inhibit-point-motion-hooks t)
929 (goto-char (point-min))
930 (when (zerop (forward-line (1- number)))
931 (forward-char (length prefix))
932 (skip-chars-forward " \t")
935 (skip-chars-backward " \t")
938 (push (setq overlay (gnus-make-overlay from to))
939 gnus-cite-overlay-list)
940 (gnus-overlay-put overlay 'face face))))))
942 (defun gnus-cite-toggle (prefix)
944 (set-buffer gnus-article-buffer)
945 (gnus-cite-parse-maybe nil t)
946 (let ((buffer-read-only nil)
947 (numbers (cdr (assoc prefix gnus-cite-prefix-alist)))
948 (inhibit-point-motion-hooks t)
951 (setq number (car numbers)
952 numbers (cdr numbers))
953 (goto-char (point-min))
954 (forward-line (1- number))
955 (cond ((get-text-property (point) 'invisible)
956 (remove-text-properties (point) (progn (forward-line 1) (point))
957 gnus-hidden-properties))
958 ((assq number gnus-cite-attribution-alist))
960 (gnus-add-text-properties
961 (point) (progn (forward-line 1) (point))
962 (nconc (list 'article-type 'cite)
963 gnus-hidden-properties))))))))
965 (defun gnus-cite-find-prefix (line)
966 ;; Return citation prefix for LINE.
967 (let ((alist gnus-cite-prefix-alist)
971 (setq entry (car alist)
973 (when (memq line (cdr entry))
974 (setq prefix (car entry))))
977 (defun gnus-cite-localize ()
978 "Make the citation variables local to the article buffer."
979 (let ((vars '(gnus-cite-article
980 gnus-cite-overlay-list gnus-cite-prefix-alist
981 gnus-cite-attribution-alist gnus-cite-loose-prefix-alist
982 gnus-cite-loose-attribution-alist)))
984 (make-local-variable (pop vars)))))
991 ;; coding: iso-8859-1
994 ;;; gnus-cite.el ends here