1 ;;; gnus-cite.el --- parse citations in articles for Gnus
3 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 ;; 2004, 2005, 2006, 2007, 2008 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 3, 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., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
29 (eval-when-compile (require 'cl
))
34 (require 'message
) ; for message-cite-prefix-regexp
38 (defgroup gnus-cite nil
41 :link
'(custom-manual "(gnus)Article Highlighting")
44 (defcustom gnus-cited-opened-text-button-line-format
"%(%{[-]%}%)\n"
45 "Format of opened cited text buttons."
49 (defcustom gnus-cited-closed-text-button-line-format
"%(%{[+]%}%)\n"
50 "Format of closed cited text buttons."
54 (defcustom gnus-cited-lines-visible nil
55 "The number of lines of hidden cited text to remain visible.
56 Or a pair (cons) of numbers which are the number of lines at the top
57 and bottom of the text, respectively, to remain visible."
59 :type
'(choice (const :tag
"none" nil
)
61 (cons :tag
"Top and Bottom" integer integer
)))
63 (defcustom gnus-cite-parse-max-size
25000
64 "Maximum article size (in bytes) where parsing citations is allowed.
65 Set it to nil to parse all articles."
67 :type
'(choice (const :tag
"all" nil
)
70 (defcustom gnus-cite-max-prefix
20
71 "Maximum possible length for a citation prefix."
75 (defcustom gnus-supercite-regexp
76 (concat "^\\(" message-cite-prefix-regexp
"\\)? *"
77 ">>>>> +\"\\([^\"\n]+\\)\" +==")
78 "*Regexp matching normal Supercite attribution lines.
79 The first grouping must match prefixes added by other packages."
83 (defcustom gnus-supercite-secondary-regexp
"^.*\"\\([^\"\n]+\\)\" +=="
84 "Regexp matching mangled Supercite attribution lines.
85 The first regexp group should match the Supercite attribution."
89 (defcustom gnus-cite-minimum-match-count
2
90 "Minimum number of identical prefixes before we believe it's a citation."
94 ;; Some Microsoft products put in a citation that extends to the
95 ;; remainder of the message:
97 ;; -----Original Message-----
100 ;; Sent: ... [date, in non-RFC-2822 format]
103 ;; Cited message, with no prefixes
105 ;; The four headers are always the same. But note they are prone to
106 ;; folding without additional indentation.
108 ;; Others use "----- Original Message -----" instead, and properly quote
109 ;; the body using "> ". This style is handled without special cases.
111 (defcustom gnus-cite-attribution-prefix
112 "In article\\|in <\\|On \\(Mon\\|Tue\\|Wed\\|Thu\\|Fri\\|Sat\\|Sun\\),\\|----- ?Original Message ?-----"
113 "*Regexp matching the beginning of an attribution line."
117 (defcustom gnus-cite-attribution-suffix
118 "\\(\\(wrote\\|writes\\|said\\|says\\|>\\)\\(:\\|\\.\\.\\.\\)\\|----- ?Original Message ?-----\\)[ \t]*$"
119 "*Regexp matching the end of an attribution line.
120 The text matching the first grouping will be used as a button."
124 (defcustom gnus-cite-unsightly-citation-regexp
125 "^-----Original Message-----\nFrom: \\(.+\n\\)+\n"
126 "Regexp matching Microsoft-type rest-of-message citations."
131 (defcustom gnus-cite-ignore-quoted-from t
132 "Non-nil means don't regard lines beginning with \">From \" as cited text.
133 Those lines may have been quoted by MTAs in order not to mix up with
134 the envelope From line."
139 (defface gnus-cite-attribution
'((t (:italic t
)))
140 "Face used for attribution lines."
142 ;; backward-compatibility alias
143 (put 'gnus-cite-attribution-face
'face-alias
'gnus-cite-attribution
)
145 (defcustom gnus-cite-attribution-face
'gnus-cite-attribution
146 "Face used for attribution lines.
147 It is merged with the face for the cited text belonging to the attribution."
152 (defface gnus-cite-1
'((((class color
)
154 (:foreground
"light blue"))
157 (:foreground
"MidnightBlue"))
162 ;; backward-compatibility alias
163 (put 'gnus-cite-face-1
'face-alias
'gnus-cite-1
)
165 (defface gnus-cite-2
'((((class color
)
167 (:foreground
"light cyan"))
170 (:foreground
"firebrick"))
175 ;; backward-compatibility alias
176 (put 'gnus-cite-face-2
'face-alias
'gnus-cite-2
)
178 (defface gnus-cite-3
'((((class color
)
180 (:foreground
"light yellow"))
183 (:foreground
"dark green"))
188 ;; backward-compatibility alias
189 (put 'gnus-cite-face-3
'face-alias
'gnus-cite-3
)
191 (defface gnus-cite-4
'((((class color
)
193 (:foreground
"light pink"))
196 (:foreground
"OrangeRed"))
201 ;; backward-compatibility alias
202 (put 'gnus-cite-face-4
'face-alias
'gnus-cite-4
)
204 (defface gnus-cite-5
'((((class color
)
206 (:foreground
"pale green"))
209 (:foreground
"dark khaki"))
214 ;; backward-compatibility alias
215 (put 'gnus-cite-face-5
'face-alias
'gnus-cite-5
)
217 (defface gnus-cite-6
'((((class color
)
219 (:foreground
"beige"))
222 (:foreground
"dark violet"))
227 ;; backward-compatibility alias
228 (put 'gnus-cite-face-6
'face-alias
'gnus-cite-6
)
230 (defface gnus-cite-7
'((((class color
)
232 (:foreground
"orange"))
235 (:foreground
"SteelBlue4"))
240 ;; backward-compatibility alias
241 (put 'gnus-cite-face-7
'face-alias
'gnus-cite-7
)
243 (defface gnus-cite-8
'((((class color
)
245 (:foreground
"magenta"))
248 (:foreground
"magenta"))
253 ;; backward-compatibility alias
254 (put 'gnus-cite-face-8
'face-alias
'gnus-cite-8
)
256 (defface gnus-cite-9
'((((class color
)
258 (:foreground
"violet"))
261 (:foreground
"violet"))
266 ;; backward-compatibility alias
267 (put 'gnus-cite-face-9
'face-alias
'gnus-cite-9
)
269 (defface gnus-cite-10
'((((class color
)
271 (:foreground
"medium purple"))
274 (:foreground
"medium purple"))
279 ;; backward-compatibility alias
280 (put 'gnus-cite-face-10
'face-alias
'gnus-cite-10
)
282 (defface gnus-cite-11
'((((class color
)
284 (:foreground
"turquoise"))
287 (:foreground
"turquoise"))
292 ;; backward-compatibility alias
293 (put 'gnus-cite-face-11
'face-alias
'gnus-cite-11
)
295 (defcustom gnus-cite-face-list
296 '(gnus-cite-1 gnus-cite-2 gnus-cite-3 gnus-cite-4 gnus-cite-5 gnus-cite-6
297 gnus-cite-7 gnus-cite-8 gnus-cite-9 gnus-cite-10 gnus-cite-11
)
298 "*List of faces used for highlighting citations.
300 When there are citations from multiple articles in the same message,
301 Gnus will try to give each citation from each article its own face.
302 This should make it easier to see who wrote what."
304 :type
'(repeat face
))
306 (defcustom gnus-cite-hide-percentage
50
307 "Only hide excess citation if above this percentage of the body."
311 (defcustom gnus-cite-hide-absolute
10
312 "Only hide excess citation if above this number of lines in the body."
316 (defcustom gnus-cite-blank-line-after-header t
317 "If non-nil, put a blank line between the citation header and the button."
321 ;; This has to go here because its default value depends on
322 ;; gnus-cite-face-list.
323 (defcustom gnus-article-boring-faces
(cons 'gnus-signature gnus-cite-face-list
)
324 "List of faces that are not worth reading.
325 If an article has more pages below the one you are looking at, but
326 nothing on those pages is a word of at least three letters that is not
327 in a boring face, then the pages will be skipped."
329 :group
'gnus-article-hiding
)
331 ;;; Internal Variables:
333 (defvar gnus-cite-article nil
)
334 (defvar gnus-cite-overlay-list nil
)
336 (defvar gnus-cite-prefix-alist nil
)
337 ;; Alist of citation prefixes.
338 ;; The cdr is a list of lines with that prefix.
340 (defvar gnus-cite-attribution-alist nil
)
341 ;; Alist of attribution lines.
342 ;; The car is a line number.
343 ;; The cdr is the prefix for the citation started by that line.
345 (defvar gnus-cite-loose-prefix-alist nil
)
346 ;; Alist of citation prefixes that have no matching attribution.
347 ;; The cdr is a list of lines with that prefix.
349 (defvar gnus-cite-loose-attribution-alist nil
)
350 ;; Alist of attribution lines that have no matching citation.
351 ;; Each member has the form (WROTE IN PREFIX TAG), where
352 ;; WROTE: is the attribution line number
353 ;; IN: is the line number of the previous line if part of the same attribution,
354 ;; PREFIX: Is the citation prefix of the attribution line(s), and
355 ;; TAG: Is a Supercite tag, if any.
357 (defvar gnus-cited-opened-text-button-line-format-alist
358 `((?b
(marker-position beg
) ?d
)
359 (?e
(marker-position end
) ?d
)
360 (?n
(count-lines beg end
) ?d
)
361 (?l
(- end beg
) ?d
)))
362 (defvar gnus-cited-opened-text-button-line-format-spec nil
)
363 (defvar gnus-cited-closed-text-button-line-format-alist
364 gnus-cited-opened-text-button-line-format-alist
)
365 (defvar gnus-cited-closed-text-button-line-format-spec nil
)
370 (defun gnus-article-highlight-citation (&optional force
)
371 "Highlight cited text.
372 Each citation in the article will be highlighted with a different face.
373 The faces are taken from `gnus-cite-face-list'.
374 Attribution lines are highlighted with the same face as the
375 corresponding citation merged with the face `gnus-cite-attribution'.
377 Text is considered cited if at least `gnus-cite-minimum-match-count'
378 lines matches `message-cite-prefix-regexp' with the same prefix.
380 Lines matching `gnus-cite-attribution-suffix' and perhaps
381 `gnus-cite-attribution-prefix' are considered attribution lines."
382 (interactive (list 'force
))
384 (set-buffer gnus-article-buffer
)
385 (gnus-cite-parse-maybe force
)
386 (let ((buffer-read-only nil
)
387 (alist gnus-cite-prefix-alist
)
388 (faces gnus-cite-face-list
)
389 (inhibit-point-motion-hooks t
)
390 face entry prefix skip numbers number face-alist
)
391 ;; Loop through citation prefixes.
393 (setq entry
(car alist
)
398 faces
(or (cdr faces
) gnus-cite-face-list
)
399 face-alist
(cons (cons prefix face
) face-alist
))
401 (setq number
(car numbers
)
402 numbers
(cdr numbers
))
403 (and (not (assq number gnus-cite-attribution-alist
))
404 (not (assq number gnus-cite-loose-attribution-alist
))
405 (gnus-cite-add-face number prefix face
))))
406 ;; Loop through attribution lines.
407 (setq alist gnus-cite-attribution-alist
)
409 (setq entry
(car alist
)
413 skip
(gnus-cite-find-prefix number
)
414 face
(cdr (assoc prefix face-alist
)))
415 ;; Add attribution button.
416 (goto-char (point-min))
417 (forward-line (1- number
))
418 (when (re-search-forward gnus-cite-attribution-suffix
421 (gnus-article-add-button (match-beginning 1) (match-end 1)
422 'gnus-cite-toggle prefix
))
423 ;; Highlight attribution line.
424 (gnus-cite-add-face number skip face
)
425 (gnus-cite-add-face number skip gnus-cite-attribution-face
))
426 ;; Loop through attribution lines.
427 (setq alist gnus-cite-loose-attribution-alist
)
429 (setq entry
(car alist
)
432 skip
(gnus-cite-find-prefix number
))
433 (gnus-cite-add-face number skip gnus-cite-attribution-face
)))))
435 (defun gnus-dissect-cited-text ()
436 "Dissect the article buffer looking for cited text."
438 (set-buffer gnus-article-buffer
)
439 (gnus-cite-parse-maybe nil t
)
440 (let ((alist gnus-cite-prefix-alist
)
441 prefix numbers number marks m
)
442 ;; Loop through citation prefixes.
444 (setq numbers
(pop alist
)
445 prefix
(pop numbers
))
447 (setq number
(pop numbers
))
448 (goto-char (point-min))
449 (forward-line number
)
450 (push (cons (point-marker) "") marks
)
452 (= (1- number
) (car numbers
)))
453 (setq number
(pop numbers
)))
454 (goto-char (point-min))
455 (forward-line (1- number
))
456 (push (cons (point-marker) prefix
) marks
)))
457 ;; Skip to the beginning of the body.
459 (push (cons (point-marker) "") marks
)
460 ;; Find the end of the body.
461 (goto-char (point-max))
462 (gnus-article-search-signature)
463 (push (cons (point-marker) "") marks
)
465 (setq marks
(sort marks
'car-less-than-car
))
466 (let ((omarks marks
))
469 (if (= (caar omarks
) (caadr omarks
))
471 (unless (equal (cdar omarks
) "")
472 (push (car omarks
) marks
))
473 (unless (equal (cdadr omarks
) "")
474 (push (cadr omarks
) marks
))
475 (unless (and (equal (cdar omarks
) "")
476 (equal (cdadr omarks
) "")
478 (setq omarks
(cdr omarks
))))
479 (push (car omarks
) marks
))
480 (setq omarks
(cdr omarks
)))
482 (push (car omarks
) marks
))
483 (setq marks
(setq m
(nreverse marks
)))
485 (if (and (equal (cdadr m
) "")
486 (equal (cdar m
) (cdaddr m
))
487 (goto-char (caadr m
))
489 (= (point) (caaddr m
)))
494 (defun gnus-article-fill-cited-article (&optional force width
)
495 "Do word wrapping in the current article.
496 If WIDTH (the numerical prefix), use that text width when filling."
497 (interactive (list t current-prefix-arg
))
499 (set-buffer gnus-article-buffer
)
500 (let ((buffer-read-only nil
)
501 (inhibit-point-motion-hooks t
)
502 (marks (gnus-dissect-cited-text))
503 (adaptive-fill-mode nil
)
505 (fill-column (if width
(prefix-numeric-value width
) fill-column
)))
508 (narrow-to-region (caar marks
) (caadr marks
))
509 (let ((adaptive-fill-regexp
510 (concat "^" (regexp-quote (cdar marks
)) " *"))
512 (if (string= (cdar marks
) "") ""
513 (concat (cdar marks
) " ")))
515 (fill-region (point-min) (point-max)))
516 (set-marker (caar marks
) nil
)
517 (setq marks
(cdr marks
)))
519 (set-marker (caar marks
) nil
))
520 ;; All this information is now incorrect.
521 (setq gnus-cite-prefix-alist nil
522 gnus-cite-attribution-alist nil
523 gnus-cite-loose-prefix-alist nil
524 gnus-cite-loose-attribution-alist nil
525 gnus-cite-article nil
)))))
527 (defun gnus-article-hide-citation (&optional arg force
)
528 "Toggle hiding of all cited text except attribution lines.
529 See the documentation for `gnus-article-highlight-citation'.
530 If given a negative prefix, always show; if given a positive prefix,
532 (interactive (append (gnus-article-hidden-arg) (list 'force
)))
533 (gnus-set-format 'cited-opened-text-button t
)
534 (gnus-set-format 'cited-closed-text-button t
)
536 (set-buffer gnus-article-buffer
)
537 (let ((buffer-read-only nil
)
539 (inhibit-point-motion-hooks t
)
540 (props (nconc (list 'article-type
'cite
)
541 gnus-hidden-properties
))
545 (text-property-any point
(point-max)
547 'gnus-article-toggle-cited-text
))
550 (gnus-article-toggle-cited-text
551 (get-text-property point
'gnus-data
) arg
)
553 (setq point
(point)))
555 (setq marks
(gnus-dissect-cited-text))
559 (while (and marks
(string= (cdar marks
) ""))
560 (setq marks
(cdr marks
)))
562 (setq beg
(caar marks
)))
563 (while (and marks
(not (string= (cdar marks
) "")))
564 (setq marks
(cdr marks
)))
566 (setq end
(caar marks
)))
567 ;; Skip past lines we want to leave visible.
568 (when (and beg end gnus-cited-lines-visible
)
570 (forward-line (if (consp gnus-cited-lines-visible
)
571 (car gnus-cited-lines-visible
)
572 gnus-cited-lines-visible
))
575 (setq beg
(point-marker))
576 (when (consp gnus-cited-lines-visible
)
578 (forward-line (- (cdr gnus-cited-lines-visible
)))
581 (setq end
(point-marker))))))
583 (gnus-add-wash-type 'cite
)
584 ;; We use markers for the end-points to facilitate later
585 ;; wrapping and mangling of text.
586 (setq beg
(set-marker (make-marker) beg
)
587 end
(set-marker (make-marker) end
))
588 (gnus-add-text-properties-when 'article-type nil beg end props
)
590 (when (and gnus-cite-blank-line-after-header
591 (not (save-excursion (search-backward "\n\n" nil t
))))
594 (setq start
(point-marker))
596 (gnus-article-add-button
598 (progn (eval gnus-cited-closed-text-button-line-format-spec
)
600 `gnus-article-toggle-cited-text
601 (list (cons beg end
) start
))
603 'article-type
'annotation
)
604 (set-marker beg
(point))))))))
606 (defun gnus-article-toggle-cited-text (args &optional arg
)
607 "Toggle hiding the text in REGION.
608 ARG can be nil or a number. Positive means hide, negative
609 means show, nil means toggle."
610 (let* ((region (car args
))
615 (text-property-any beg
(1- end
) 'article-type
'cite
))
616 (inhibit-point-motion-hooks t
)
620 (and (> arg
0) (not hidden
))
621 (and (< arg
0) hidden
))
624 ;; Can't remove 'cite from g-a-wash-types here because
625 ;; multiple citations may be hidden -jas
626 (gnus-remove-text-properties-when
627 'article-type
'cite beg end
628 (cons 'article-type
(cons 'cite
629 gnus-hidden-properties
))))
630 (gnus-add-wash-type 'cite
)
631 (gnus-add-text-properties-when
632 'article-type nil beg end
633 (cons 'article-type
(cons 'cite
634 gnus-hidden-properties
))))
635 (let ((gnus-article-mime-handle-alist-1 gnus-article-mime-handle-alist
))
636 (gnus-set-mode-line 'article
))
643 (gnus-article-add-button
647 gnus-cited-opened-text-button-line-format-spec
648 gnus-cited-closed-text-button-line-format-spec
))
650 `gnus-article-toggle-cited-text
653 'article-type
'annotation
)))))
655 (defun gnus-article-hide-citation-maybe (&optional arg force
)
656 "Toggle hiding of cited text that has an attribution line.
657 If given a negative prefix, always show; if given a positive prefix,
659 This will do nothing unless at least `gnus-cite-hide-percentage'
660 percent and at least `gnus-cite-hide-absolute' lines of the body is
661 cited text with attributions. When called interactively, these two
662 variables are ignored.
663 See also the documentation for `gnus-article-highlight-citation'."
664 (interactive (append (gnus-article-hidden-arg) '(force)))
665 (with-current-buffer gnus-article-buffer
666 (gnus-delete-wash-type 'cite
)
667 (unless (gnus-article-check-hidden-text 'cite arg
)
669 (gnus-cite-parse-maybe force
)
671 (let ((start (point))
672 (atts gnus-cite-attribution-alist
)
673 (buffer-read-only nil
)
674 (inhibit-point-motion-hooks t
)
677 (goto-char (point-max))
678 (gnus-article-search-signature)
679 (setq total
(count-lines start
(point)))
681 (setq hidden
(+ hidden
(length (cdr (assoc (cdar atts
)
682 gnus-cite-prefix-alist
))))
685 (and (> (* 100 hidden
) (* gnus-cite-hide-percentage total
))
686 (> hidden gnus-cite-hide-absolute
)))
687 (gnus-add-wash-type 'cite
)
688 (setq atts gnus-cite-attribution-alist
)
690 (setq total
(cdr (assoc (cdar atts
) gnus-cite-prefix-alist
))
693 (setq hidden
(car total
)
695 (goto-char (point-min))
696 (forward-line (1- hidden
))
697 (unless (assq hidden gnus-cite-attribution-alist
)
698 (gnus-add-text-properties
699 (point) (progn (forward-line 1) (point))
700 (nconc (list 'article-type
'cite
)
701 gnus-hidden-properties
)))))))))
702 (gnus-set-mode-line 'article
)))
704 (defun gnus-article-hide-citation-in-followups ()
705 "Hide cited text in non-root articles."
708 (set-buffer gnus-article-buffer
)
709 (let ((article (cdr gnus-article-current
)))
710 (unless (save-excursion
711 (set-buffer gnus-summary-buffer
)
712 (gnus-article-displayed-root-p article
))
713 (gnus-article-hide-citation)))))
715 ;;; Internal functions:
717 (defun gnus-cite-parse-maybe (&optional force no-overlay
)
718 "Always parse the buffer."
720 ;;Reset parser information.
721 (setq gnus-cite-prefix-alist nil
722 gnus-cite-attribution-alist nil
723 gnus-cite-loose-prefix-alist nil
724 gnus-cite-loose-attribution-alist nil
)
726 (gnus-cite-delete-overlays))
727 ;; Parse if not too large.
728 (if (and gnus-cite-parse-max-size
729 (> (buffer-size) gnus-cite-parse-max-size
))
731 (setq gnus-cite-article
(cons (car gnus-article-current
)
732 (cdr gnus-article-current
)))
733 (gnus-cite-parse-wrapper)))
735 (defun gnus-cite-delete-overlays ()
736 (dolist (overlay gnus-cite-overlay-list
)
738 (when (or (not (gnus-overlay-end overlay
))
739 (and (>= (gnus-overlay-end overlay
) (point-min))
740 (<= (gnus-overlay-end overlay
) (point-max))))
741 (setq gnus-cite-overlay-list
(delete overlay gnus-cite-overlay-list
))
743 (gnus-delete-overlay overlay
))))))
745 (defun gnus-cite-parse-wrapper ()
746 ;; Wrap chopped gnus-cite-parse.
748 (let ((inhibit-point-motion-hooks t
))
750 (gnus-cite-parse-attributions))
754 (gnus-cite-connect-attributions))))
756 (defun gnus-cite-parse ()
757 ;; Parse and connect citation prefixes and attribution lines.
759 ;; Parse current buffer searching for citation prefixes.
760 (let ((line (1+ (count-lines (point-min) (point))))
763 (goto-char (point-max))
764 (gnus-article-search-signature)
766 (prefix-regexp (concat "^\\(" message-cite-prefix-regexp
"\\)"))
767 alist entry start begin end numbers prefix guess-limit
)
768 ;; Get all potential prefixes in `alist'.
769 (while (< (point) max
)
772 guess-limit
(progn (skip-chars-forward "^> \t\r\n") (point))
773 end
(gnus-point-at-bol 2)
776 ;; Ignore standard Supercite attribution prefix.
777 (when (and (< guess-limit
(+ begin gnus-cite-max-prefix
))
778 (looking-at gnus-supercite-regexp
))
780 (setq end
(1+ (match-end 1)))
781 (setq end
(1+ begin
))))
782 ;; Ignore very long prefixes.
783 (when (> end
(+ begin gnus-cite-max-prefix
))
784 (setq end
(+ begin gnus-cite-max-prefix
)))
785 ;; Ignore quoted envelope From_.
786 (when (and gnus-cite-ignore-quoted-from
788 (setq case-fold-search nil
)
789 (looking-at ">From ")
790 (setq case-fold-search t
)))
791 (setq end
(1+ begin
)))
792 (while (re-search-forward prefix-regexp
(1- end
) t
)
794 (setq end
(match-end 0)
795 prefix
(buffer-substring begin end
))
796 (gnus-set-text-properties 0 (length prefix
) nil prefix
)
797 (setq entry
(assoc prefix alist
))
799 (setcdr entry
(cons line
(cdr entry
)))
800 (push (list prefix line
) alist
))
803 (setq line
(1+ line
)))
804 ;; Horrible special case for some Microsoft mailers.
805 (goto-char (point-min))
806 (when (re-search-forward gnus-cite-unsightly-citation-regexp max t
)
807 (setq begin
(count-lines (point-min) (point)))
808 (setq end
(count-lines (point-min) max
))
812 (setq begin
(1+ begin
)))
813 (push (cons "" entry
) alist
))
814 ;; We got all the potential prefixes. Now create
815 ;; `gnus-cite-prefix-alist' containing the oldest prefix for each
816 ;; line that appears at least `gnus-cite-minimum-match-count'
817 ;; times. First sort them by length. Longer is older.
818 (setq alist
(sort alist
(lambda (a b
)
819 (> (length (car a
)) (length (car b
))))))
821 (setq entry
(car alist
)
825 (cond ((null numbers
)
826 ;; No lines with this prefix that wasn't also part of
829 ((< (length numbers
) gnus-cite-minimum-match-count
)
830 ;; Too few lines with this prefix. We keep it a bit
831 ;; longer in case it is an exact match for an attribution
832 ;; line, but we don't remove the line from other
834 (push entry gnus-cite-prefix-alist
))
837 gnus-cite-prefix-alist
)
838 ;; Remove articles from other prefixes.
842 (setq current
(car loop
)
845 (gnus-set-difference (cdr current
) numbers
)))))))))
847 (defun gnus-cite-parse-attributions ()
849 ;; Parse attributions
850 (while (re-search-forward gnus-cite-attribution-suffix
(point-max) t
)
851 (let* ((start (match-beginning 0))
853 (wrote (count-lines (point-min) end
))
854 (prefix (gnus-cite-find-prefix wrote
))
855 ;; Check previous line for an attribution leader.
857 (beginning-of-line 1)
858 (when (looking-at gnus-supercite-secondary-regexp
)
859 (buffer-substring (match-beginning 1)
863 (and (re-search-backward gnus-cite-attribution-prefix
865 (beginning-of-line 0)
868 (not (re-search-forward gnus-cite-attribution-suffix
870 (count-lines (point-min) (1+ (point)))))))
874 ;; don't add duplicates
875 (let ((al (buffer-substring (save-excursion (beginning-of-line 0)
878 (if (not (assoc al al-alist
))
880 (push (list wrote in prefix tag
)
881 gnus-cite-loose-attribution-alist
)
882 (push (cons al t
) al-alist
))))))))
884 (defun gnus-cite-connect-attributions ()
885 ;; Connect attributions to citations
887 ;; No citations have been connected to attribution lines yet.
888 (setq gnus-cite-loose-prefix-alist
(append gnus-cite-prefix-alist nil
))
890 ;; Parse current buffer searching for attribution lines.
891 ;; Find exact supercite citations.
892 (gnus-cite-match-attributions 'small nil
896 (regexp-quote prefix
) "[ \t]*"
897 (regexp-quote tag
) ">"))))
898 ;; Find loose supercite citations after attributions.
899 (gnus-cite-match-attributions 'small t
905 ;; Find loose supercite citations anywhere.
906 (gnus-cite-match-attributions 'small nil
912 ;; Find nested citations after attributions.
913 (gnus-cite-match-attributions 'small-if-unique t
915 (concat "\\`" (regexp-quote prefix
) ".+")))
916 ;; Find nested citations anywhere.
917 (gnus-cite-match-attributions 'small nil
919 (concat "\\`" (regexp-quote prefix
) ".+")))
920 ;; Remove loose prefixes with too few lines.
921 (let ((alist gnus-cite-loose-prefix-alist
)
924 (setq entry
(car alist
)
926 (when (< (length (cdr entry
)) gnus-cite-minimum-match-count
)
927 (setq gnus-cite-prefix-alist
928 (delq entry gnus-cite-prefix-alist
)
929 gnus-cite-loose-prefix-alist
930 (delq entry gnus-cite-loose-prefix-alist
)))))
931 ;; Find flat attributions.
932 (gnus-cite-match-attributions 'first t nil
)
933 ;; Find any attributions (are we getting desperate yet?).
934 (gnus-cite-match-attributions 'first nil nil
))
936 (defun gnus-cite-match-attributions (sort after fun
)
937 ;; Match all loose attributions and citations (SORT AFTER FUN) .
939 ;; If SORT is `small', the citation with the shortest prefix will be
940 ;; used, if it is `first' the first prefix will be used, if it is
941 ;; `small-if-unique' the shortest prefix will be used if the
942 ;; attribution line does not share its own prefix with other
943 ;; loose attribution lines, otherwise the first prefix will be used.
945 ;; If AFTER is non-nil, only citations after the attribution line
946 ;; will be considered.
948 ;; If FUN is non-nil, it will be called with the arguments (WROTE
949 ;; PREFIX TAG) and expected to return a regular expression. Only
950 ;; citations whose prefix matches the regular expression will be
953 ;; WROTE is the attribution line number.
954 ;; PREFIX is the attribution line prefix.
955 ;; TAG is the Supercite tag on the attribution line.
956 (let ((atts gnus-cite-loose-attribution-alist
)
958 att wrote in prefix tag regexp limit smallest best size
)
966 regexp
(if fun
(funcall fun prefix tag
) "")
967 size
(cond ((eq sort
'small
) t
)
968 ((eq sort
'first
) nil
)
969 (t (< (length (gnus-cite-find-loose prefix
)) 2)))
970 limit
(if after wrote -
1)
973 (let ((cites gnus-cite-loose-prefix-alist
)
974 cite candidate numbers first compare
)
976 (setq cite
(car cites
)
980 first
(apply 'min numbers
)
981 compare
(if size
(length candidate
) first
))
984 (string-match regexp candidate
)
990 (setq gnus-cite-loose-attribution-alist
991 (delq att gnus-cite-loose-attribution-alist
))
992 (push (cons wrote
(car best
)) gnus-cite-attribution-alist
)
994 (push (cons in
(car best
)) gnus-cite-attribution-alist
))
995 (when (memq best gnus-cite-loose-prefix-alist
)
996 (let ((loop gnus-cite-prefix-alist
)
999 (setq gnus-cite-loose-prefix-alist
1000 (delq best gnus-cite-loose-prefix-alist
))
1002 (setq current
(car loop
)
1004 (if (eq current best
)
1006 (setcdr current
(gnus-set-difference (cdr current
) numbers
))
1007 (when (null (cdr current
))
1008 (setq gnus-cite-loose-prefix-alist
1009 (delq current gnus-cite-loose-prefix-alist
)
1010 atts
(delq current atts
)))))))))))
1012 (defun gnus-cite-find-loose (prefix)
1013 ;; Return a list of loose attribution lines prefixed by PREFIX.
1014 (let* ((atts gnus-cite-loose-attribution-alist
)
1017 (setq att
(car atts
)
1020 (when (string-equal (gnus-cite-find-prefix line
) prefix
)
1024 (defun gnus-cite-add-face (number prefix face
)
1025 ;; At line NUMBER, ignore PREFIX and add FACE to the rest of the line.
1027 (let ((inhibit-point-motion-hooks t
)
1029 (goto-char (point-min))
1030 (when (zerop (forward-line (1- number
)))
1031 (forward-char (length prefix
))
1032 (skip-chars-forward " \t")
1035 (skip-chars-backward " \t")
1038 (push (setq overlay
(gnus-make-overlay from to
))
1039 gnus-cite-overlay-list
)
1040 (gnus-overlay-put overlay
'evaporate t
)
1041 (gnus-overlay-put overlay
'face face
))))))
1043 (defun gnus-cite-toggle (prefix)
1045 (set-buffer gnus-article-buffer
)
1046 (gnus-cite-parse-maybe nil t
)
1047 (let ((buffer-read-only nil
)
1048 (numbers (cdr (assoc prefix gnus-cite-prefix-alist
)))
1049 (inhibit-point-motion-hooks t
)
1052 (setq number
(car numbers
)
1053 numbers
(cdr numbers
))
1054 (goto-char (point-min))
1055 (forward-line (1- number
))
1056 (cond ((get-text-property (point) 'invisible
)
1057 ;; Can't remove 'cite from g-a-wash-types here because
1058 ;; multiple citations may be hidden -jas
1059 (remove-text-properties (point) (progn (forward-line 1) (point))
1060 gnus-hidden-properties
))
1061 ((assq number gnus-cite-attribution-alist
))
1063 (gnus-add-wash-type 'cite
)
1064 (gnus-add-text-properties
1065 (point) (progn (forward-line 1) (point))
1066 (nconc (list 'article-type
'cite
)
1067 gnus-hidden-properties
))))
1068 (let ((gnus-article-mime-handle-alist-1
1069 gnus-article-mime-handle-alist
))
1070 (gnus-set-mode-line 'article
))))))
1072 (defun gnus-cite-find-prefix (line)
1073 ;; Return citation prefix for LINE.
1074 (let ((alist gnus-cite-prefix-alist
)
1078 (setq entry
(car alist
)
1080 (when (memq line
(cdr entry
))
1081 (setq prefix
(car entry
))))
1084 (defun gnus-cite-localize ()
1085 "Make the citation variables local to the article buffer."
1086 (let ((vars '(gnus-cite-article
1087 gnus-cite-overlay-list gnus-cite-prefix-alist
1088 gnus-cite-attribution-alist gnus-cite-loose-prefix-alist
1089 gnus-cite-loose-attribution-alist
)))
1091 (make-local-variable (pop vars
)))))
1093 (defun gnus-cited-line-p ()
1094 "Say whether the current line is a cited line."
1098 (dolist (prefix (mapcar 'car gnus-cite-prefix-alist
))
1099 (when (string= (buffer-substring (point) (+ (length prefix
) (point)))
1106 (provide 'gnus-cite
)
1109 ;; coding: iso-8859-1
1112 ;;; arch-tag: 1997b044-6067-471e-8c8f-dc903093098a
1113 ;;; gnus-cite.el ends here