1 ;;; gnus-cite.el --- parse citations in articles for Gnus
3 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
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))
33 (require 'message) ; for message-cite-prefix-regexp
37 (defgroup gnus-cite nil
40 :link '(custom-manual "(gnus)Article Highlighting")
43 (defcustom gnus-cited-opened-text-button-line-format "%(%{[-]%}%)\n"
44 "Format of opened cited text buttons."
48 (defcustom gnus-cited-closed-text-button-line-format "%(%{[+]%}%)\n"
49 "Format of closed cited text buttons."
53 (defcustom gnus-cited-lines-visible nil
54 "The number of lines of hidden cited text to remain visible.
55 Or a pair (cons) of numbers which are the number of lines at the top
56 and bottom of the text, respectively, to remain visible."
58 :type '(choice (const :tag "none" nil)
60 (cons :tag "Top and Bottom" integer integer)))
62 (defcustom gnus-cite-parse-max-size 25000
63 "Maximum article size (in bytes) where parsing citations is allowed.
64 Set it to nil to parse all articles."
66 :type '(choice (const :tag "all" nil)
69 (defcustom gnus-cite-max-prefix 20
70 "Maximum possible length for a citation prefix."
74 (defcustom gnus-supercite-regexp
75 (concat "^\\(" message-cite-prefix-regexp "\\)? *"
76 ">>>>> +\"\\([^\"\n]+\\)\" +==")
77 "*Regexp matching normal Supercite attribution lines.
78 The first grouping must match prefixes added by other packages."
82 (defcustom gnus-supercite-secondary-regexp "^.*\"\\([^\"\n]+\\)\" +=="
83 "Regexp matching mangled Supercite attribution lines.
84 The first regexp group should match the Supercite attribution."
88 (defcustom gnus-cite-minimum-match-count 2
89 "Minimum number of identical prefixes before we believe it's a citation."
93 (defcustom gnus-cite-attribution-prefix
94 "In article\\|in <\\|On \\(Mon\\|Tue\\|Wed\\|Thu\\|Fri\\|Sat\\|Sun\\),\\|-----Original Message-----"
95 "*Regexp matching the beginning of an attribution line."
99 (defcustom gnus-cite-attribution-suffix
100 "\\(\\(wrote\\|writes\\|said\\|says\\|>\\)\\(:\\|\\.\\.\\.\\)\\|-----Original Message-----\\)[ \t]*$"
101 "*Regexp matching the end of an attribution line.
102 The text matching the first grouping will be used as a button."
106 (defface gnus-cite-attribution-face '((t
108 "Face used for attribution lines.")
110 (defcustom gnus-cite-attribution-face 'gnus-cite-attribution-face
111 "Face used for attribution lines.
112 It is merged with the face for the cited text belonging to the attribution."
116 (defface gnus-cite-face-1 '((((class color)
118 (:foreground "light blue"))
121 (:foreground "MidnightBlue"))
126 (defface gnus-cite-face-2 '((((class color)
128 (:foreground "light cyan"))
131 (:foreground "firebrick"))
136 (defface gnus-cite-face-3 '((((class color)
138 (:foreground "light yellow"))
141 (:foreground "dark green"))
146 (defface gnus-cite-face-4 '((((class color)
148 (:foreground "light pink"))
151 (:foreground "OrangeRed"))
156 (defface gnus-cite-face-5 '((((class color)
158 (:foreground "pale green"))
161 (:foreground "dark khaki"))
166 (defface gnus-cite-face-6 '((((class color)
168 (:foreground "beige"))
171 (:foreground "dark violet"))
176 (defface gnus-cite-face-7 '((((class color)
178 (:foreground "orange"))
181 (:foreground "SteelBlue4"))
186 (defface gnus-cite-face-8 '((((class color)
188 (:foreground "magenta"))
191 (:foreground "magenta"))
196 (defface gnus-cite-face-9 '((((class color)
198 (:foreground "violet"))
201 (:foreground "violet"))
206 (defface gnus-cite-face-10 '((((class color)
208 (:foreground "medium purple"))
211 (:foreground "medium purple"))
216 (defface gnus-cite-face-11 '((((class color)
218 (:foreground "turquoise"))
221 (:foreground "turquoise"))
226 (defcustom gnus-cite-face-list
227 '(gnus-cite-face-1 gnus-cite-face-2 gnus-cite-face-3 gnus-cite-face-4
228 gnus-cite-face-5 gnus-cite-face-6 gnus-cite-face-7 gnus-cite-face-8
229 gnus-cite-face-9 gnus-cite-face-10 gnus-cite-face-11)
230 "*List of faces used for highlighting citations.
232 When there are citations from multiple articles in the same message,
233 Gnus will try to give each citation from each article its own face.
234 This should make it easier to see who wrote what."
236 :type '(repeat face))
238 (defcustom gnus-cite-hide-percentage 50
239 "Only hide excess citation if above this percentage of the body."
243 (defcustom gnus-cite-hide-absolute 10
244 "Only hide excess citation if above this number of lines in the body."
248 (defcustom gnus-cite-blank-line-after-header t
249 "If non-nil, put a blank line between the citation header and the button."
253 ;; This has to go here because its default value depends on
254 ;; gnus-cite-face-list.
255 (defcustom gnus-article-boring-faces (cons 'gnus-signature-face
257 "List of faces that are not worth reading.
258 If an article has more pages below the one you are looking at, but
259 nothing on those pages is a word of at least three letters that is not
260 in a boring face, then the pages will be skipped."
262 :group 'gnus-article-hiding)
264 ;;; Internal Variables:
266 (defvar gnus-cite-article nil)
267 (defvar gnus-cite-overlay-list nil)
269 (defvar gnus-cite-prefix-alist nil)
270 ;; Alist of citation prefixes.
271 ;; The cdr is a list of lines with that prefix.
273 (defvar gnus-cite-attribution-alist nil)
274 ;; Alist of attribution lines.
275 ;; The car is a line number.
276 ;; The cdr is the prefix for the citation started by that line.
278 (defvar gnus-cite-loose-prefix-alist nil)
279 ;; Alist of citation prefixes that have no matching attribution.
280 ;; The cdr is a list of lines with that prefix.
282 (defvar gnus-cite-loose-attribution-alist nil)
283 ;; Alist of attribution lines that have no matching citation.
284 ;; Each member has the form (WROTE IN PREFIX TAG), where
285 ;; WROTE: is the attribution line number
286 ;; IN: is the line number of the previous line if part of the same attribution,
287 ;; PREFIX: Is the citation prefix of the attribution line(s), and
288 ;; TAG: Is a Supercite tag, if any.
290 (defvar gnus-cited-opened-text-button-line-format-alist
291 `((?b (marker-position beg) ?d)
292 (?e (marker-position end) ?d)
293 (?n (count-lines beg end) ?d)
294 (?l (- end beg) ?d)))
295 (defvar gnus-cited-opened-text-button-line-format-spec nil)
296 (defvar gnus-cited-closed-text-button-line-format-alist
297 gnus-cited-opened-text-button-line-format-alist)
298 (defvar gnus-cited-closed-text-button-line-format-spec nil)
303 (defun gnus-article-highlight-citation (&optional force)
304 "Highlight cited text.
305 Each citation in the article will be highlighted with a different face.
306 The faces are taken from `gnus-cite-face-list'.
307 Attribution lines are highlighted with the same face as the
308 corresponding citation merged with `gnus-cite-attribution-face'.
310 Text is considered cited if at least `gnus-cite-minimum-match-count'
311 lines matches `message-cite-prefix-regexp' with the same prefix.
313 Lines matching `gnus-cite-attribution-suffix' and perhaps
314 `gnus-cite-attribution-prefix' are considered attribution lines."
315 (interactive (list 'force))
317 (set-buffer gnus-article-buffer)
318 (gnus-cite-parse-maybe force)
319 (let ((buffer-read-only nil)
320 (alist gnus-cite-prefix-alist)
321 (faces gnus-cite-face-list)
322 (inhibit-point-motion-hooks t)
323 face entry prefix skip numbers number face-alist)
324 ;; Loop through citation prefixes.
326 (setq entry (car alist)
331 faces (or (cdr faces) gnus-cite-face-list)
332 face-alist (cons (cons prefix face) face-alist))
334 (setq number (car numbers)
335 numbers (cdr numbers))
336 (and (not (assq number gnus-cite-attribution-alist))
337 (not (assq number gnus-cite-loose-attribution-alist))
338 (gnus-cite-add-face number prefix face))))
339 ;; Loop through attribution lines.
340 (setq alist gnus-cite-attribution-alist)
342 (setq entry (car alist)
346 skip (gnus-cite-find-prefix number)
347 face (cdr (assoc prefix face-alist)))
348 ;; Add attribution button.
349 (goto-char (point-min))
350 (forward-line (1- number))
351 (when (re-search-forward gnus-cite-attribution-suffix
352 (save-excursion (end-of-line 1) (point))
354 (gnus-article-add-button (match-beginning 1) (match-end 1)
355 'gnus-cite-toggle prefix))
356 ;; Highlight attribution line.
357 (gnus-cite-add-face number skip face)
358 (gnus-cite-add-face number skip gnus-cite-attribution-face))
359 ;; Loop through attribution lines.
360 (setq alist gnus-cite-loose-attribution-alist)
362 (setq entry (car alist)
365 skip (gnus-cite-find-prefix number))
366 (gnus-cite-add-face number skip gnus-cite-attribution-face)))))
368 (defun gnus-dissect-cited-text ()
369 "Dissect the article buffer looking for cited text."
371 (set-buffer gnus-article-buffer)
372 (gnus-cite-parse-maybe nil t)
373 (let ((alist gnus-cite-prefix-alist)
374 prefix numbers number marks m)
375 ;; Loop through citation prefixes.
377 (setq numbers (pop alist)
378 prefix (pop numbers))
380 (setq number (pop numbers))
381 (goto-char (point-min))
382 (forward-line number)
383 (push (cons (point-marker) "") marks)
385 (= (1- number) (car numbers)))
386 (setq number (pop numbers)))
387 (goto-char (point-min))
388 (forward-line (1- number))
389 (push (cons (point-marker) prefix) marks)))
390 ;; Skip to the beginning of the body.
392 (push (cons (point-marker) "") marks)
393 ;; Find the end of the body.
394 (goto-char (point-max))
395 (gnus-article-search-signature)
396 (push (cons (point-marker) "") marks)
398 (setq marks (sort marks 'car-less-than-car))
399 (let ((omarks marks))
402 (if (= (caar omarks) (caadr omarks))
404 (unless (equal (cdar omarks) "")
405 (push (car omarks) marks))
406 (unless (equal (cdadr omarks) "")
407 (push (cadr omarks) marks))
408 (unless (and (equal (cdar omarks) "")
409 (equal (cdadr omarks) "")
411 (setq omarks (cdr omarks))))
412 (push (car omarks) marks))
413 (setq omarks (cdr omarks)))
415 (push (car omarks) marks))
416 (setq marks (setq m (nreverse marks)))
418 (if (and (equal (cdadr m) "")
419 (equal (cdar m) (cdaddr m))
420 (goto-char (caadr m))
422 (= (point) (caaddr m)))
427 (defun gnus-article-fill-cited-article (&optional force width)
428 "Do word wrapping in the current article.
429 If WIDTH (the numerical prefix), use that text width when filling."
430 (interactive (list t current-prefix-arg))
432 (set-buffer gnus-article-buffer)
433 (let ((buffer-read-only nil)
434 (inhibit-point-motion-hooks t)
435 (marks (gnus-dissect-cited-text))
436 (adaptive-fill-mode nil)
438 (fill-column (if width (prefix-numeric-value width) fill-column)))
441 (narrow-to-region (caar marks) (caadr marks))
442 (let ((adaptive-fill-regexp
443 (concat "^" (regexp-quote (cdar marks)) " *"))
445 (if (string= (cdar marks) "") ""
446 (concat (cdar marks) " ")))
448 (fill-region (point-min) (point-max)))
449 (set-marker (caar marks) nil)
450 (setq marks (cdr marks)))
452 (set-marker (caar marks) nil))
453 ;; All this information is now incorrect.
454 (setq gnus-cite-prefix-alist nil
455 gnus-cite-attribution-alist nil
456 gnus-cite-loose-prefix-alist nil
457 gnus-cite-loose-attribution-alist nil
458 gnus-cite-article nil)))))
460 (defun gnus-article-hide-citation (&optional arg force)
461 "Toggle hiding of all cited text except attribution lines.
462 See the documentation for `gnus-article-highlight-citation'.
463 If given a negative prefix, always show; if given a positive prefix,
465 (interactive (append (gnus-article-hidden-arg) (list 'force)))
466 (gnus-set-format 'cited-opened-text-button t)
467 (gnus-set-format 'cited-closed-text-button t)
469 (set-buffer gnus-article-buffer)
470 (let ((buffer-read-only nil)
472 (inhibit-point-motion-hooks t)
473 (props (nconc (list 'article-type 'cite)
474 gnus-hidden-properties))
478 (text-property-any point (point-max)
480 'gnus-article-toggle-cited-text))
483 (gnus-article-toggle-cited-text
484 (get-text-property point 'gnus-data) arg)
486 (setq point (point)))
488 (setq marks (gnus-dissect-cited-text))
492 (while (and marks (string= (cdar marks) ""))
493 (setq marks (cdr marks)))
495 (setq beg (caar marks)))
496 (while (and marks (not (string= (cdar marks) "")))
497 (setq marks (cdr marks)))
499 (setq end (caar marks)))
500 ;; Skip past lines we want to leave visible.
501 (when (and beg end gnus-cited-lines-visible)
503 (forward-line (if (consp gnus-cited-lines-visible)
504 (car gnus-cited-lines-visible)
505 gnus-cited-lines-visible))
508 (setq beg (point-marker))
509 (when (consp gnus-cited-lines-visible)
511 (forward-line (- (cdr gnus-cited-lines-visible)))
514 (setq end (point-marker))))))
516 (gnus-add-wash-type 'cite)
517 ;; We use markers for the end-points to facilitate later
518 ;; wrapping and mangling of text.
519 (setq beg (set-marker (make-marker) beg)
520 end (set-marker (make-marker) end))
521 (gnus-add-text-properties-when 'article-type nil beg end props)
523 (when (and gnus-cite-blank-line-after-header
524 (not (save-excursion (search-backward "\n\n" nil t))))
527 (setq start (point-marker))
529 (gnus-article-add-button
531 (progn (eval gnus-cited-closed-text-button-line-format-spec)
533 `gnus-article-toggle-cited-text
534 (list (cons beg end) start))
536 'article-type 'annotation)
537 (set-marker beg (point))))))))
539 (defun gnus-article-toggle-cited-text (args &optional arg)
540 "Toggle hiding the text in REGION.
541 ARG can be nil or a number. Positive means hide, negative
542 means show, nil means toggle."
543 (let* ((region (car args))
548 (text-property-any beg (1- end) 'article-type 'cite))
549 (inhibit-point-motion-hooks t)
553 (and (> arg 0) (not hidden))
554 (and (< arg 0) hidden))
557 ;; Can't remove 'cite from g-a-wash-types here because
558 ;; multiple citations may be hidden -jas
559 (gnus-remove-text-properties-when
560 'article-type 'cite beg end
561 (cons 'article-type (cons 'cite
562 gnus-hidden-properties))))
563 (gnus-add-wash-type 'cite)
564 (gnus-add-text-properties-when
565 'article-type nil beg end
566 (cons 'article-type (cons 'cite
567 gnus-hidden-properties))))
568 (let ((gnus-article-mime-handle-alist-1 gnus-article-mime-handle-alist))
569 (gnus-set-mode-line 'article))
576 (gnus-article-add-button
580 gnus-cited-opened-text-button-line-format-spec
581 gnus-cited-closed-text-button-line-format-spec))
583 `gnus-article-toggle-cited-text
586 'article-type 'annotation)))))
588 (defun gnus-article-hide-citation-maybe (&optional arg force)
589 "Toggle hiding of cited text that has an attribution line.
590 If given a negative prefix, always show; if given a positive prefix,
592 This will do nothing unless at least `gnus-cite-hide-percentage'
593 percent and at least `gnus-cite-hide-absolute' lines of the body is
594 cited text with attributions. When called interactively, these two
595 variables are ignored.
596 See also the documentation for `gnus-article-highlight-citation'."
597 (interactive (append (gnus-article-hidden-arg) '(force)))
598 (unless (gnus-article-check-hidden-text 'cite arg)
600 (set-buffer gnus-article-buffer)
601 (gnus-cite-parse-maybe force)
603 (let ((start (point))
604 (atts gnus-cite-attribution-alist)
605 (buffer-read-only nil)
606 (inhibit-point-motion-hooks t)
609 (goto-char (point-max))
610 (gnus-article-search-signature)
611 (setq total (count-lines start (point)))
613 (setq hidden (+ hidden (length (cdr (assoc (cdar atts)
614 gnus-cite-prefix-alist))))
617 (and (> (* 100 hidden) (* gnus-cite-hide-percentage total))
618 (> hidden gnus-cite-hide-absolute)))
619 (setq atts gnus-cite-attribution-alist)
621 (setq total (cdr (assoc (cdar atts) gnus-cite-prefix-alist))
624 (setq hidden (car total)
626 (goto-char (point-min))
627 (forward-line (1- hidden))
628 (unless (assq hidden gnus-cite-attribution-alist)
629 (gnus-add-text-properties
630 (point) (progn (forward-line 1) (point))
631 (nconc (list 'article-type 'cite)
632 gnus-hidden-properties))))))))))
634 (defun gnus-article-hide-citation-in-followups ()
635 "Hide cited text in non-root articles."
638 (set-buffer gnus-article-buffer)
639 (let ((article (cdr gnus-article-current)))
640 (unless (save-excursion
641 (set-buffer gnus-summary-buffer)
642 (gnus-article-displayed-root-p article))
643 (gnus-article-hide-citation)))))
645 ;;; Internal functions:
647 (defun gnus-cite-parse-maybe (&optional force no-overlay)
648 "Always parse the buffer."
650 ;;Reset parser information.
651 (setq gnus-cite-prefix-alist nil
652 gnus-cite-attribution-alist nil
653 gnus-cite-loose-prefix-alist nil
654 gnus-cite-loose-attribution-alist nil)
656 (gnus-cite-delete-overlays))
657 ;; Parse if not too large.
658 (if (and gnus-cite-parse-max-size
659 (> (buffer-size) gnus-cite-parse-max-size))
661 (setq gnus-cite-article (cons (car gnus-article-current)
662 (cdr gnus-article-current)))
663 (gnus-cite-parse-wrapper)))
665 (defun gnus-cite-delete-overlays ()
666 (dolist (overlay gnus-cite-overlay-list)
668 (when (or (not (gnus-overlay-end overlay))
669 (and (>= (gnus-overlay-end overlay) (point-min))
670 (<= (gnus-overlay-end overlay) (point-max))))
671 (setq gnus-cite-overlay-list (delete overlay gnus-cite-overlay-list))
673 (gnus-delete-overlay overlay))))))
675 (defun gnus-cite-parse-wrapper ()
676 ;; Wrap chopped gnus-cite-parse.
678 (let ((inhibit-point-motion-hooks t))
680 (gnus-cite-parse-attributions))
684 (gnus-cite-connect-attributions))))
686 (defun gnus-cite-parse ()
687 ;; Parse and connect citation prefixes and attribution lines.
689 ;; Parse current buffer searching for citation prefixes.
690 (let ((line (1+ (count-lines (point-min) (point))))
693 (goto-char (point-max))
694 (gnus-article-search-signature)
696 (prefix-regexp (concat "^\\(" message-cite-prefix-regexp "\\)"))
697 alist entry start begin end numbers prefix guess-limit)
698 ;; Get all potential prefixes in `alist'.
699 (while (< (point) max)
702 guess-limit (progn (skip-chars-forward "^> \t\r\n") (point))
703 end (progn (beginning-of-line 2) (point))
706 ;; Ignore standard Supercite attribution prefix.
707 (when (and (< guess-limit (+ begin gnus-cite-max-prefix))
708 (looking-at gnus-supercite-regexp))
710 (setq end (1+ (match-end 1)))
711 (setq end (1+ begin))))
712 ;; Ignore very long prefixes.
713 (when (> end (+ begin gnus-cite-max-prefix))
714 (setq end (+ begin gnus-cite-max-prefix)))
715 (while (re-search-forward prefix-regexp (1- end) t)
717 (setq end (match-end 0)
718 prefix (buffer-substring begin end))
719 (gnus-set-text-properties 0 (length prefix) nil prefix)
720 (setq entry (assoc prefix alist))
722 (setcdr entry (cons line (cdr entry)))
723 (push (list prefix line) alist))
726 (setq line (1+ line)))
727 ;; We got all the potential prefixes. Now create
728 ;; `gnus-cite-prefix-alist' containing the oldest prefix for each
729 ;; line that appears at least gnus-cite-minimum-match-count
730 ;; times. First sort them by length. Longer is older.
731 (setq alist (sort alist (lambda (a b)
732 (> (length (car a)) (length (car b))))))
734 (setq entry (car alist)
738 (cond ((null numbers)
739 ;; No lines with this prefix that wasn't also part of
742 ((< (length numbers) gnus-cite-minimum-match-count)
743 ;; Too few lines with this prefix. We keep it a bit
744 ;; longer in case it is an exact match for an attribution
745 ;; line, but we don't remove the line from other
747 (push entry gnus-cite-prefix-alist))
750 gnus-cite-prefix-alist)
751 ;; Remove articles from other prefixes.
755 (setq current (car loop)
758 (gnus-set-difference (cdr current) numbers)))))))))
760 (defun gnus-cite-parse-attributions ()
762 ;; Parse attributions
763 (while (re-search-forward gnus-cite-attribution-suffix (point-max) t)
764 (let* ((start (match-beginning 0))
766 (wrote (count-lines (point-min) end))
767 (prefix (gnus-cite-find-prefix wrote))
768 ;; Check previous line for an attribution leader.
770 (beginning-of-line 1)
771 (when (looking-at gnus-supercite-secondary-regexp)
772 (buffer-substring (match-beginning 1)
776 (and (re-search-backward gnus-cite-attribution-prefix
778 (beginning-of-line 0)
781 (not (re-search-forward gnus-cite-attribution-suffix
783 (count-lines (point-min) (1+ (point)))))))
787 ;; don't add duplicates
788 (let ((al (buffer-substring (save-excursion (beginning-of-line 0)
791 (if (not (assoc al al-alist))
793 (push (list wrote in prefix tag)
794 gnus-cite-loose-attribution-alist)
795 (push (cons al t) al-alist))))))))
797 (defun gnus-cite-connect-attributions ()
798 ;; Connect attributions to citations
800 ;; No citations have been connected to attribution lines yet.
801 (setq gnus-cite-loose-prefix-alist (append gnus-cite-prefix-alist nil))
803 ;; Parse current buffer searching for attribution lines.
804 ;; Find exact supercite citations.
805 (gnus-cite-match-attributions 'small nil
809 (regexp-quote prefix) "[ \t]*"
810 (regexp-quote tag) ">"))))
811 ;; Find loose supercite citations after attributions.
812 (gnus-cite-match-attributions 'small t
818 ;; Find loose supercite citations anywhere.
819 (gnus-cite-match-attributions 'small nil
825 ;; Find nested citations after attributions.
826 (gnus-cite-match-attributions 'small-if-unique t
828 (concat "\\`" (regexp-quote prefix) ".+")))
829 ;; Find nested citations anywhere.
830 (gnus-cite-match-attributions 'small nil
832 (concat "\\`" (regexp-quote prefix) ".+")))
833 ;; Remove loose prefixes with too few lines.
834 (let ((alist gnus-cite-loose-prefix-alist)
837 (setq entry (car alist)
839 (when (< (length (cdr entry)) gnus-cite-minimum-match-count)
840 (setq gnus-cite-prefix-alist
841 (delq entry gnus-cite-prefix-alist)
842 gnus-cite-loose-prefix-alist
843 (delq entry gnus-cite-loose-prefix-alist)))))
844 ;; Find flat attributions.
845 (gnus-cite-match-attributions 'first t nil)
846 ;; Find any attributions (are we getting desperate yet?).
847 (gnus-cite-match-attributions 'first nil nil))
849 (defun gnus-cite-match-attributions (sort after fun)
850 ;; Match all loose attributions and citations (SORT AFTER FUN) .
852 ;; If SORT is `small', the citation with the shortest prefix will be
853 ;; used, if it is `first' the first prefix will be used, if it is
854 ;; `small-if-unique' the shortest prefix will be used if the
855 ;; attribution line does not share its own prefix with other
856 ;; loose attribution lines, otherwise the first prefix will be used.
858 ;; If AFTER is non-nil, only citations after the attribution line
859 ;; will be considered.
861 ;; If FUN is non-nil, it will be called with the arguments (WROTE
862 ;; PREFIX TAG) and expected to return a regular expression. Only
863 ;; citations whose prefix matches the regular expression will be
866 ;; WROTE is the attribution line number.
867 ;; PREFIX is the attribution line prefix.
868 ;; TAG is the Supercite tag on the attribution line.
869 (let ((atts gnus-cite-loose-attribution-alist)
871 att wrote in prefix tag regexp limit smallest best size)
879 regexp (if fun (funcall fun prefix tag) "")
880 size (cond ((eq sort 'small) t)
881 ((eq sort 'first) nil)
882 (t (< (length (gnus-cite-find-loose prefix)) 2)))
883 limit (if after wrote -1)
886 (let ((cites gnus-cite-loose-prefix-alist)
887 cite candidate numbers first compare)
889 (setq cite (car cites)
893 first (apply 'min numbers)
894 compare (if size (length candidate) first))
897 (string-match regexp candidate)
903 (setq gnus-cite-loose-attribution-alist
904 (delq att gnus-cite-loose-attribution-alist))
905 (push (cons wrote (car best)) gnus-cite-attribution-alist)
907 (push (cons in (car best)) gnus-cite-attribution-alist))
908 (when (memq best gnus-cite-loose-prefix-alist)
909 (let ((loop gnus-cite-prefix-alist)
912 (setq gnus-cite-loose-prefix-alist
913 (delq best gnus-cite-loose-prefix-alist))
915 (setq current (car loop)
917 (if (eq current best)
919 (setcdr current (gnus-set-difference (cdr current) numbers))
920 (when (null (cdr current))
921 (setq gnus-cite-loose-prefix-alist
922 (delq current gnus-cite-loose-prefix-alist)
923 atts (delq current atts)))))))))))
925 (defun gnus-cite-find-loose (prefix)
926 ;; Return a list of loose attribution lines prefixed by PREFIX.
927 (let* ((atts gnus-cite-loose-attribution-alist)
933 (when (string-equal (gnus-cite-find-prefix line) prefix)
937 (defun gnus-cite-add-face (number prefix face)
938 ;; At line NUMBER, ignore PREFIX and add FACE to the rest of the line.
940 (let ((inhibit-point-motion-hooks t)
942 (goto-char (point-min))
943 (when (zerop (forward-line (1- number)))
944 (forward-char (length prefix))
945 (skip-chars-forward " \t")
948 (skip-chars-backward " \t")
951 (push (setq overlay (gnus-make-overlay from to))
952 gnus-cite-overlay-list)
953 (gnus-overlay-put overlay 'face face))))))
955 (defun gnus-cite-toggle (prefix)
957 (set-buffer gnus-article-buffer)
958 (gnus-cite-parse-maybe nil t)
959 (let ((buffer-read-only nil)
960 (numbers (cdr (assoc prefix gnus-cite-prefix-alist)))
961 (inhibit-point-motion-hooks t)
964 (setq number (car numbers)
965 numbers (cdr numbers))
966 (goto-char (point-min))
967 (forward-line (1- number))
968 (cond ((get-text-property (point) 'invisible)
969 ;; Can't remove 'cite from g-a-wash-types here because
970 ;; multiple citations may be hidden -jas
971 (remove-text-properties (point) (progn (forward-line 1) (point))
972 gnus-hidden-properties))
973 ((assq number gnus-cite-attribution-alist))
975 (gnus-add-wash-type 'cite)
976 (gnus-add-text-properties
977 (point) (progn (forward-line 1) (point))
978 (nconc (list 'article-type 'cite)
979 gnus-hidden-properties))))
980 (let ((gnus-article-mime-handle-alist-1
981 gnus-article-mime-handle-alist))
982 (gnus-set-mode-line 'article))))))
984 (defun gnus-cite-find-prefix (line)
985 ;; Return citation prefix for LINE.
986 (let ((alist gnus-cite-prefix-alist)
990 (setq entry (car alist)
992 (when (memq line (cdr entry))
993 (setq prefix (car entry))))
996 (defun gnus-cite-localize ()
997 "Make the citation variables local to the article buffer."
998 (let ((vars '(gnus-cite-article
999 gnus-cite-overlay-list gnus-cite-prefix-alist
1000 gnus-cite-attribution-alist gnus-cite-loose-prefix-alist
1001 gnus-cite-loose-attribution-alist)))
1003 (make-local-variable (pop vars)))))
1007 (provide 'gnus-cite)
1010 ;; coding: iso-8859-1
1013 ;;; gnus-cite.el ends here