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))
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 (defcustom gnus-cite-attribution-prefix
95 "In article\\|in <\\|On \\(Mon\\|Tue\\|Wed\\|Thu\\|Fri\\|Sat\\|Sun\\),\\|-----Original Message-----"
96 "*Regexp matching the beginning of an attribution line."
100 (defcustom gnus-cite-attribution-suffix
101 "\\(\\(wrote\\|writes\\|said\\|says\\|>\\)\\(:\\|\\.\\.\\.\\)\\|-----Original Message-----\\)[ \t]*$"
102 "*Regexp matching the end of an attribution line.
103 The text matching the first grouping will be used as a button."
107 (defface gnus-cite-attribution-face '((t
109 "Face used for attribution lines.")
111 (defcustom gnus-cite-attribution-face 'gnus-cite-attribution-face
112 "Face used for attribution lines.
113 It is merged with the face for the cited text belonging to the attribution."
117 (defface gnus-cite-face-1 '((((class color)
119 (:foreground "light blue"))
122 (:foreground "MidnightBlue"))
127 (defface gnus-cite-face-2 '((((class color)
129 (:foreground "light cyan"))
132 (:foreground "firebrick"))
137 (defface gnus-cite-face-3 '((((class color)
139 (:foreground "light yellow"))
142 (:foreground "dark green"))
147 (defface gnus-cite-face-4 '((((class color)
149 (:foreground "light pink"))
152 (:foreground "OrangeRed"))
157 (defface gnus-cite-face-5 '((((class color)
159 (:foreground "pale green"))
162 (:foreground "dark khaki"))
167 (defface gnus-cite-face-6 '((((class color)
169 (:foreground "beige"))
172 (:foreground "dark violet"))
177 (defface gnus-cite-face-7 '((((class color)
179 (:foreground "orange"))
182 (:foreground "SteelBlue4"))
187 (defface gnus-cite-face-8 '((((class color)
189 (:foreground "magenta"))
192 (:foreground "magenta"))
197 (defface gnus-cite-face-9 '((((class color)
199 (:foreground "violet"))
202 (:foreground "violet"))
207 (defface gnus-cite-face-10 '((((class color)
209 (:foreground "medium purple"))
212 (:foreground "medium purple"))
217 (defface gnus-cite-face-11 '((((class color)
219 (:foreground "turquoise"))
222 (:foreground "turquoise"))
227 (defcustom gnus-cite-face-list
228 '(gnus-cite-face-1 gnus-cite-face-2 gnus-cite-face-3 gnus-cite-face-4
229 gnus-cite-face-5 gnus-cite-face-6 gnus-cite-face-7 gnus-cite-face-8
230 gnus-cite-face-9 gnus-cite-face-10 gnus-cite-face-11)
231 "*List of faces used for highlighting citations.
233 When there are citations from multiple articles in the same message,
234 Gnus will try to give each citation from each article its own face.
235 This should make it easier to see who wrote what."
237 :type '(repeat face))
239 (defcustom gnus-cite-hide-percentage 50
240 "Only hide excess citation if above this percentage of the body."
244 (defcustom gnus-cite-hide-absolute 10
245 "Only hide excess citation if above this number of lines in the body."
249 (defcustom gnus-cite-blank-line-after-header t
250 "If non-nil, put a blank line between the citation header and the button."
254 ;;; Internal Variables:
256 (defvar gnus-cite-article nil)
257 (defvar gnus-cite-overlay-list nil)
259 (defvar gnus-cite-prefix-alist nil)
260 ;; Alist of citation prefixes.
261 ;; The cdr is a list of lines with that prefix.
263 (defvar gnus-cite-attribution-alist nil)
264 ;; Alist of attribution lines.
265 ;; The car is a line number.
266 ;; The cdr is the prefix for the citation started by that line.
268 (defvar gnus-cite-loose-prefix-alist nil)
269 ;; Alist of citation prefixes that have no matching attribution.
270 ;; The cdr is a list of lines with that prefix.
272 (defvar gnus-cite-loose-attribution-alist nil)
273 ;; Alist of attribution lines that have no matching citation.
274 ;; Each member has the form (WROTE IN PREFIX TAG), where
275 ;; WROTE: is the attribution line number
276 ;; IN: is the line number of the previous line if part of the same attribution,
277 ;; PREFIX: Is the citation prefix of the attribution line(s), and
278 ;; TAG: Is a Supercite tag, if any.
280 (defvar gnus-cited-opened-text-button-line-format-alist
281 `((?b (marker-position beg) ?d)
282 (?e (marker-position end) ?d)
283 (?n (count-lines beg end) ?d)
284 (?l (- end beg) ?d)))
285 (defvar gnus-cited-opened-text-button-line-format-spec nil)
286 (defvar gnus-cited-closed-text-button-line-format-alist
287 gnus-cited-opened-text-button-line-format-alist)
288 (defvar gnus-cited-closed-text-button-line-format-spec nil)
293 (defun gnus-article-highlight-citation (&optional force)
294 "Highlight cited text.
295 Each citation in the article will be highlighted with a different face.
296 The faces are taken from `gnus-cite-face-list'.
297 Attribution lines are highlighted with the same face as the
298 corresponding citation merged with `gnus-cite-attribution-face'.
300 Text is considered cited if at least `gnus-cite-minimum-match-count'
301 lines matches `message-cite-prefix-regexp' with the same prefix.
303 Lines matching `gnus-cite-attribution-suffix' and perhaps
304 `gnus-cite-attribution-prefix' are considered attribution lines."
305 (interactive (list 'force))
307 (set-buffer gnus-article-buffer)
308 (gnus-cite-parse-maybe force)
309 (let ((buffer-read-only nil)
310 (alist gnus-cite-prefix-alist)
311 (faces gnus-cite-face-list)
312 (inhibit-point-motion-hooks t)
313 face entry prefix skip numbers number face-alist)
314 ;; Loop through citation prefixes.
316 (setq entry (car alist)
321 faces (or (cdr faces) gnus-cite-face-list)
322 face-alist (cons (cons prefix face) face-alist))
324 (setq number (car numbers)
325 numbers (cdr numbers))
326 (and (not (assq number gnus-cite-attribution-alist))
327 (not (assq number gnus-cite-loose-attribution-alist))
328 (gnus-cite-add-face number prefix face))))
329 ;; Loop through attribution lines.
330 (setq alist gnus-cite-attribution-alist)
332 (setq entry (car alist)
336 skip (gnus-cite-find-prefix number)
337 face (cdr (assoc prefix face-alist)))
338 ;; Add attribution button.
339 (goto-char (point-min))
340 (forward-line (1- number))
341 (when (re-search-forward gnus-cite-attribution-suffix
342 (save-excursion (end-of-line 1) (point))
344 (gnus-article-add-button (match-beginning 1) (match-end 1)
345 'gnus-cite-toggle prefix))
346 ;; Highlight attribution line.
347 (gnus-cite-add-face number skip face)
348 (gnus-cite-add-face number skip gnus-cite-attribution-face))
349 ;; Loop through attribution lines.
350 (setq alist gnus-cite-loose-attribution-alist)
352 (setq entry (car alist)
355 skip (gnus-cite-find-prefix number))
356 (gnus-cite-add-face number skip gnus-cite-attribution-face)))))
358 (defun gnus-dissect-cited-text ()
359 "Dissect the article buffer looking for cited text."
361 (set-buffer gnus-article-buffer)
362 (gnus-cite-parse-maybe nil t)
363 (let ((alist gnus-cite-prefix-alist)
364 prefix numbers number marks m)
365 ;; Loop through citation prefixes.
367 (setq numbers (pop alist)
368 prefix (pop numbers))
370 (setq number (pop numbers))
371 (goto-char (point-min))
372 (forward-line number)
373 (push (cons (point-marker) "") marks)
375 (= (1- number) (car numbers)))
376 (setq number (pop numbers)))
377 (goto-char (point-min))
378 (forward-line (1- number))
379 (push (cons (point-marker) prefix) marks)))
380 ;; Skip to the beginning of the body.
382 (push (cons (point-marker) "") marks)
383 ;; Find the end of the body.
384 (goto-char (point-max))
385 (gnus-article-search-signature)
386 (push (cons (point-marker) "") marks)
388 (setq marks (sort marks 'car-less-than-car))
389 (let ((omarks marks))
392 (if (= (caar omarks) (caadr omarks))
394 (unless (equal (cdar omarks) "")
395 (push (car omarks) marks))
396 (unless (equal (cdadr omarks) "")
397 (push (cadr omarks) marks))
398 (unless (and (equal (cdar omarks) "")
399 (equal (cdadr omarks) "")
401 (setq omarks (cdr omarks))))
402 (push (car omarks) marks))
403 (setq omarks (cdr omarks)))
405 (push (car omarks) marks))
406 (setq marks (setq m (nreverse marks)))
408 (if (and (equal (cdadr m) "")
409 (equal (cdar m) (cdaddr m))
410 (goto-char (caadr m))
412 (= (point) (caaddr m)))
417 (defun gnus-article-fill-cited-article (&optional force width)
418 "Do word wrapping in the current article.
419 If WIDTH (the numerical prefix), use that text width when filling."
420 (interactive (list t current-prefix-arg))
422 (set-buffer gnus-article-buffer)
423 (let ((buffer-read-only nil)
424 (inhibit-point-motion-hooks t)
425 (marks (gnus-dissect-cited-text))
426 (adaptive-fill-mode nil)
428 (fill-column (if width (prefix-numeric-value width) fill-column)))
431 (narrow-to-region (caar marks) (caadr marks))
432 (let ((adaptive-fill-regexp
433 (concat "^" (regexp-quote (cdar marks)) " *"))
435 (if (string= (cdar marks) "") ""
436 (concat (cdar marks) " ")))
438 (fill-region (point-min) (point-max)))
439 (set-marker (caar marks) nil)
440 (setq marks (cdr marks)))
442 (set-marker (caar marks) nil))
443 ;; All this information is now incorrect.
444 (setq gnus-cite-prefix-alist nil
445 gnus-cite-attribution-alist nil
446 gnus-cite-loose-prefix-alist nil
447 gnus-cite-loose-attribution-alist nil
448 gnus-cite-article nil)))))
450 (defun gnus-article-hide-citation (&optional arg force)
451 "Toggle hiding of all cited text except attribution lines.
452 See the documentation for `gnus-article-highlight-citation'.
453 If given a negative prefix, always show; if given a positive prefix,
455 (interactive (append (gnus-article-hidden-arg) (list 'force)))
456 (gnus-set-format 'cited-opened-text-button t)
457 (gnus-set-format 'cited-closed-text-button t)
459 (set-buffer gnus-article-buffer)
460 (let ((buffer-read-only nil)
462 (inhibit-point-motion-hooks t)
463 (props (nconc (list 'article-type 'cite)
464 gnus-hidden-properties))
468 (text-property-any point (point-max)
470 'gnus-article-toggle-cited-text))
473 (gnus-article-toggle-cited-text
474 (get-text-property point 'gnus-data) arg)
476 (setq point (point)))
478 (setq marks (gnus-dissect-cited-text))
482 (while (and marks (string= (cdar marks) ""))
483 (setq marks (cdr marks)))
485 (setq beg (caar marks)))
486 (while (and marks (not (string= (cdar marks) "")))
487 (setq marks (cdr marks)))
489 (setq end (caar marks)))
490 ;; Skip past lines we want to leave visible.
491 (when (and beg end gnus-cited-lines-visible)
493 (forward-line (if (consp gnus-cited-lines-visible)
494 (car gnus-cited-lines-visible)
495 gnus-cited-lines-visible))
498 (setq beg (point-marker))
499 (when (consp gnus-cited-lines-visible)
501 (forward-line (- (cdr gnus-cited-lines-visible)))
504 (setq end (point-marker))))))
506 (gnus-add-wash-type 'cite)
507 ;; We use markers for the end-points to facilitate later
508 ;; wrapping and mangling of text.
509 (setq beg (set-marker (make-marker) beg)
510 end (set-marker (make-marker) end))
511 (gnus-add-text-properties-when 'article-type nil beg end props)
513 (when (and gnus-cite-blank-line-after-header
514 (not (save-excursion (search-backward "\n\n" nil t))))
517 (setq start (point-marker))
519 (gnus-article-add-button
521 (progn (eval gnus-cited-closed-text-button-line-format-spec)
523 `gnus-article-toggle-cited-text
524 (list (cons beg end) start))
526 'article-type 'annotation)
527 (set-marker beg (point))))))))
529 (defun gnus-article-toggle-cited-text (args &optional arg)
530 "Toggle hiding the text in REGION.
531 ARG can be nil or a number. Positive means hide, negative
532 means show, nil means toggle."
533 (let* ((region (car args))
538 (text-property-any beg (1- end) 'article-type 'cite))
539 (inhibit-point-motion-hooks t)
543 (and (> arg 0) (not hidden))
544 (and (< arg 0) hidden))
547 ;; Can't remove 'cite from g-a-wash-types here because
548 ;; multiple citations may be hidden -jas
549 (gnus-remove-text-properties-when
550 'article-type 'cite beg end
551 (cons 'article-type (cons 'cite
552 gnus-hidden-properties))))
553 (gnus-add-wash-type 'cite)
554 (gnus-add-text-properties-when
555 'article-type nil beg end
556 (cons 'article-type (cons 'cite
557 gnus-hidden-properties))))
558 (let ((gnus-article-mime-handle-alist-1 gnus-article-mime-handle-alist))
559 (gnus-set-mode-line 'article))
566 (gnus-article-add-button
570 gnus-cited-opened-text-button-line-format-spec
571 gnus-cited-closed-text-button-line-format-spec))
573 `gnus-article-toggle-cited-text
576 'article-type 'annotation)))))
578 (defun gnus-article-hide-citation-maybe (&optional arg force)
579 "Toggle hiding of cited text that has an attribution line.
580 If given a negative prefix, always show; if given a positive prefix,
582 This will do nothing unless at least `gnus-cite-hide-percentage'
583 percent and at least `gnus-cite-hide-absolute' lines of the body is
584 cited text with attributions. When called interactively, these two
585 variables are ignored.
586 See also the documentation for `gnus-article-highlight-citation'."
587 (interactive (append (gnus-article-hidden-arg) '(force)))
588 (unless (gnus-article-check-hidden-text 'cite arg)
590 (set-buffer gnus-article-buffer)
591 (gnus-cite-parse-maybe force)
593 (let ((start (point))
594 (atts gnus-cite-attribution-alist)
595 (buffer-read-only nil)
596 (inhibit-point-motion-hooks t)
599 (goto-char (point-max))
600 (gnus-article-search-signature)
601 (setq total (count-lines start (point)))
603 (setq hidden (+ hidden (length (cdr (assoc (cdar atts)
604 gnus-cite-prefix-alist))))
607 (and (> (* 100 hidden) (* gnus-cite-hide-percentage total))
608 (> hidden gnus-cite-hide-absolute)))
609 (setq atts gnus-cite-attribution-alist)
611 (setq total (cdr (assoc (cdar atts) gnus-cite-prefix-alist))
614 (setq hidden (car total)
616 (goto-char (point-min))
617 (forward-line (1- hidden))
618 (unless (assq hidden gnus-cite-attribution-alist)
619 (gnus-add-text-properties
620 (point) (progn (forward-line 1) (point))
621 (nconc (list 'article-type 'cite)
622 gnus-hidden-properties))))))))))
624 (defun gnus-article-hide-citation-in-followups ()
625 "Hide cited text in non-root articles."
628 (set-buffer gnus-article-buffer)
629 (let ((article (cdr gnus-article-current)))
630 (unless (save-excursion
631 (set-buffer gnus-summary-buffer)
632 (gnus-article-displayed-root-p article))
633 (gnus-article-hide-citation)))))
635 ;;; Internal functions:
637 (defun gnus-cite-parse-maybe (&optional force no-overlay)
638 "Always parse the buffer."
640 ;;Reset parser information.
641 (setq gnus-cite-prefix-alist nil
642 gnus-cite-attribution-alist nil
643 gnus-cite-loose-prefix-alist nil
644 gnus-cite-loose-attribution-alist nil)
646 (gnus-cite-delete-overlays))
647 ;; Parse if not too large.
648 (if (and gnus-cite-parse-max-size
649 (> (buffer-size) gnus-cite-parse-max-size))
651 (setq gnus-cite-article (cons (car gnus-article-current)
652 (cdr gnus-article-current)))
653 (gnus-cite-parse-wrapper)))
655 (defun gnus-cite-delete-overlays ()
656 (dolist (overlay gnus-cite-overlay-list)
658 (when (or (not (gnus-overlay-end overlay))
659 (and (>= (gnus-overlay-end overlay) (point-min))
660 (<= (gnus-overlay-end overlay) (point-max))))
661 (setq gnus-cite-overlay-list (delete overlay gnus-cite-overlay-list))
663 (gnus-delete-overlay overlay))))))
665 (defun gnus-cite-parse-wrapper ()
666 ;; Wrap chopped gnus-cite-parse.
668 (let ((inhibit-point-motion-hooks t))
670 (gnus-cite-parse-attributions))
674 (gnus-cite-connect-attributions))))
676 (defun gnus-cite-parse ()
677 ;; Parse and connect citation prefixes and attribution lines.
679 ;; Parse current buffer searching for citation prefixes.
680 (let ((line (1+ (count-lines (point-min) (point))))
683 (goto-char (point-max))
684 (gnus-article-search-signature)
686 (prefix-regexp (concat "^\\(" message-cite-prefix-regexp "\\)"))
687 alist entry start begin end numbers prefix guess-limit)
688 ;; Get all potential prefixes in `alist'.
689 (while (< (point) max)
692 guess-limit (progn (skip-chars-forward "^> \t\r\n") (point))
693 end (progn (beginning-of-line 2) (point))
696 ;; Ignore standard Supercite attribution prefix.
697 (when (and (< guess-limit (+ begin gnus-cite-max-prefix))
698 (looking-at gnus-supercite-regexp))
700 (setq end (1+ (match-end 1)))
701 (setq end (1+ begin))))
702 ;; Ignore very long prefixes.
703 (when (> end (+ begin gnus-cite-max-prefix))
704 (setq end (+ begin gnus-cite-max-prefix)))
705 (while (re-search-forward prefix-regexp (1- end) t)
707 (setq end (match-end 0)
708 prefix (buffer-substring begin end))
709 (gnus-set-text-properties 0 (length prefix) nil prefix)
710 (setq entry (assoc prefix alist))
712 (setcdr entry (cons line (cdr entry)))
713 (push (list prefix line) alist))
716 (setq line (1+ line)))
717 ;; We got all the potential prefixes. Now create
718 ;; `gnus-cite-prefix-alist' containing the oldest prefix for each
719 ;; line that appears at least gnus-cite-minimum-match-count
720 ;; times. First sort them by length. Longer is older.
721 (setq alist (sort alist (lambda (a b)
722 (> (length (car a)) (length (car b))))))
724 (setq entry (car alist)
728 (cond ((null numbers)
729 ;; No lines with this prefix that wasn't also part of
732 ((< (length numbers) gnus-cite-minimum-match-count)
733 ;; Too few lines with this prefix. We keep it a bit
734 ;; longer in case it is an exact match for an attribution
735 ;; line, but we don't remove the line from other
737 (push entry gnus-cite-prefix-alist))
740 gnus-cite-prefix-alist)
741 ;; Remove articles from other prefixes.
745 (setq current (car loop)
748 (gnus-set-difference (cdr current) numbers)))))))))
750 (defun gnus-cite-parse-attributions ()
752 ;; Parse attributions
753 (while (re-search-forward gnus-cite-attribution-suffix (point-max) t)
754 (let* ((start (match-beginning 0))
756 (wrote (count-lines (point-min) end))
757 (prefix (gnus-cite-find-prefix wrote))
758 ;; Check previous line for an attribution leader.
760 (beginning-of-line 1)
761 (when (looking-at gnus-supercite-secondary-regexp)
762 (buffer-substring (match-beginning 1)
766 (and (re-search-backward gnus-cite-attribution-prefix
768 (beginning-of-line 0)
771 (not (re-search-forward gnus-cite-attribution-suffix
773 (count-lines (point-min) (1+ (point)))))))
777 ;; don't add duplicates
778 (let ((al (buffer-substring (save-excursion (beginning-of-line 0)
781 (if (not (assoc al al-alist))
783 (push (list wrote in prefix tag)
784 gnus-cite-loose-attribution-alist)
785 (push (cons al t) al-alist))))))))
787 (defun gnus-cite-connect-attributions ()
788 ;; Connect attributions to citations
790 ;; No citations have been connected to attribution lines yet.
791 (setq gnus-cite-loose-prefix-alist (append gnus-cite-prefix-alist nil))
793 ;; Parse current buffer searching for attribution lines.
794 ;; Find exact supercite citations.
795 (gnus-cite-match-attributions 'small nil
799 (regexp-quote prefix) "[ \t]*"
800 (regexp-quote tag) ">"))))
801 ;; Find loose supercite citations after attributions.
802 (gnus-cite-match-attributions 'small t
808 ;; Find loose supercite citations anywhere.
809 (gnus-cite-match-attributions 'small nil
815 ;; Find nested citations after attributions.
816 (gnus-cite-match-attributions 'small-if-unique t
818 (concat "\\`" (regexp-quote prefix) ".+")))
819 ;; Find nested citations anywhere.
820 (gnus-cite-match-attributions 'small nil
822 (concat "\\`" (regexp-quote prefix) ".+")))
823 ;; Remove loose prefixes with too few lines.
824 (let ((alist gnus-cite-loose-prefix-alist)
827 (setq entry (car alist)
829 (when (< (length (cdr entry)) gnus-cite-minimum-match-count)
830 (setq gnus-cite-prefix-alist
831 (delq entry gnus-cite-prefix-alist)
832 gnus-cite-loose-prefix-alist
833 (delq entry gnus-cite-loose-prefix-alist)))))
834 ;; Find flat attributions.
835 (gnus-cite-match-attributions 'first t nil)
836 ;; Find any attributions (are we getting desperate yet?).
837 (gnus-cite-match-attributions 'first nil nil))
839 (defun gnus-cite-match-attributions (sort after fun)
840 ;; Match all loose attributions and citations (SORT AFTER FUN) .
842 ;; If SORT is `small', the citation with the shortest prefix will be
843 ;; used, if it is `first' the first prefix will be used, if it is
844 ;; `small-if-unique' the shortest prefix will be used if the
845 ;; attribution line does not share its own prefix with other
846 ;; loose attribution lines, otherwise the first prefix will be used.
848 ;; If AFTER is non-nil, only citations after the attribution line
849 ;; will be considered.
851 ;; If FUN is non-nil, it will be called with the arguments (WROTE
852 ;; PREFIX TAG) and expected to return a regular expression. Only
853 ;; citations whose prefix matches the regular expression will be
856 ;; WROTE is the attribution line number.
857 ;; PREFIX is the attribution line prefix.
858 ;; TAG is the Supercite tag on the attribution line.
859 (let ((atts gnus-cite-loose-attribution-alist)
861 att wrote in prefix tag regexp limit smallest best size)
869 regexp (if fun (funcall fun prefix tag) "")
870 size (cond ((eq sort 'small) t)
871 ((eq sort 'first) nil)
872 (t (< (length (gnus-cite-find-loose prefix)) 2)))
873 limit (if after wrote -1)
876 (let ((cites gnus-cite-loose-prefix-alist)
877 cite candidate numbers first compare)
879 (setq cite (car cites)
883 first (apply 'min numbers)
884 compare (if size (length candidate) first))
887 (string-match regexp candidate)
893 (setq gnus-cite-loose-attribution-alist
894 (delq att gnus-cite-loose-attribution-alist))
895 (push (cons wrote (car best)) gnus-cite-attribution-alist)
897 (push (cons in (car best)) gnus-cite-attribution-alist))
898 (when (memq best gnus-cite-loose-prefix-alist)
899 (let ((loop gnus-cite-prefix-alist)
902 (setq gnus-cite-loose-prefix-alist
903 (delq best gnus-cite-loose-prefix-alist))
905 (setq current (car loop)
907 (if (eq current best)
909 (setcdr current (gnus-set-difference (cdr current) numbers))
910 (when (null (cdr current))
911 (setq gnus-cite-loose-prefix-alist
912 (delq current gnus-cite-loose-prefix-alist)
913 atts (delq current atts)))))))))))
915 (defun gnus-cite-find-loose (prefix)
916 ;; Return a list of loose attribution lines prefixed by PREFIX.
917 (let* ((atts gnus-cite-loose-attribution-alist)
923 (when (string-equal (gnus-cite-find-prefix line) prefix)
927 (defun gnus-cite-add-face (number prefix face)
928 ;; At line NUMBER, ignore PREFIX and add FACE to the rest of the line.
930 (let ((inhibit-point-motion-hooks t)
932 (goto-char (point-min))
933 (when (zerop (forward-line (1- number)))
934 (forward-char (length prefix))
935 (skip-chars-forward " \t")
938 (skip-chars-backward " \t")
941 (push (setq overlay (gnus-make-overlay from to))
942 gnus-cite-overlay-list)
943 (gnus-overlay-put overlay 'face face))))))
945 (defun gnus-cite-toggle (prefix)
947 (set-buffer gnus-article-buffer)
948 (gnus-cite-parse-maybe nil t)
949 (let ((buffer-read-only nil)
950 (numbers (cdr (assoc prefix gnus-cite-prefix-alist)))
951 (inhibit-point-motion-hooks t)
954 (setq number (car numbers)
955 numbers (cdr numbers))
956 (goto-char (point-min))
957 (forward-line (1- number))
958 (cond ((get-text-property (point) 'invisible)
959 ;; Can't remove 'cite from g-a-wash-types here because
960 ;; multiple citations may be hidden -jas
961 (remove-text-properties (point) (progn (forward-line 1) (point))
962 gnus-hidden-properties))
963 ((assq number gnus-cite-attribution-alist))
965 (gnus-add-wash-type 'cite)
966 (gnus-add-text-properties
967 (point) (progn (forward-line 1) (point))
968 (nconc (list 'article-type 'cite)
969 gnus-hidden-properties))))
970 (let ((gnus-article-mime-handle-alist-1
971 gnus-article-mime-handle-alist))
972 (gnus-set-mode-line 'article))))))
974 (defun gnus-cite-find-prefix (line)
975 ;; Return citation prefix for LINE.
976 (let ((alist gnus-cite-prefix-alist)
980 (setq entry (car alist)
982 (when (memq line (cdr entry))
983 (setq prefix (car entry))))
986 (defun gnus-cite-localize ()
987 "Make the citation variables local to the article buffer."
988 (let ((vars '(gnus-cite-article
989 gnus-cite-overlay-list gnus-cite-prefix-alist
990 gnus-cite-attribution-alist gnus-cite-loose-prefix-alist
991 gnus-cite-loose-attribution-alist)))
993 (make-local-variable (pop vars)))))
1000 ;; coding: iso-8859-1
1003 ;;; gnus-cite.el ends here