1 ;;; gnus-cite.el --- parse citations in articles for Gnus
2 ;; Copyright (C) 1995,96 Free Software Foundation, Inc.
4 ;; Author: Per Abrahamsen <abraham@iesd.auc.dk>
5 ;; Keywords: news, mail
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
35 (defvar gnus-cited-text-button-line-format "%(%{[...]%}%)\n"
36 "Format of cited text buttons.")
38 (defvar gnus-cited-lines-visible nil
39 "The number of lines of hidden cited text to remain visible.")
41 (defvar gnus-cite-parse-max-size 25000
42 "Maximum article size (in bytes) where parsing citations is allowed.
43 Set it to nil to parse all articles.")
45 (defvar gnus-cite-prefix-regexp
46 "^[]>|:}+ ]*[]>|:}+]\\(.*>\\)?\\|^.*>"
47 "Regexp matching the longest possible citation prefix on a line.")
49 (defvar gnus-cite-max-prefix 20
50 "Maximum possible length for a citation prefix.")
52 (defvar gnus-supercite-regexp
53 (concat "^\\(" gnus-cite-prefix-regexp "\\)? *"
54 ">>>>> +\"\\([^\"\n]+\\)\" +==")
55 "Regexp matching normal Supercite attribution lines.
56 The first grouping must match prefixes added by other packages.")
58 (defvar gnus-supercite-secondary-regexp "^.*\"\\([^\"\n]+\\)\" +=="
59 "Regexp matching mangled Supercite attribution lines.
60 The first regexp group should match the Supercite attribution.")
62 (defvar gnus-cite-minimum-match-count 2
63 "Minimum number of identical prefixes before we believe it's a citation.")
66 ;(defvar gnus-cite-face-list
67 ; (if (eq gnus-display-type 'color)
68 ; (if (eq gnus-background-mode 'dark) 'light 'dark)
70 ; "Faces used for displaying different citations.
71 ;It is either a list of face names, or one of the following special
74 ;dark: Create faces from `gnus-face-dark-name-list'.
75 ;light: Create faces from `gnus-face-light-name-list'.
77 ;The variable `gnus-make-foreground' determines whether the created
78 ;faces change the foreground or the background colors.")
80 (defvar gnus-cite-attribution-prefix "in article\\|in <"
81 "Regexp matching the beginning of an attribution line.")
83 (defvar gnus-cite-attribution-suffix
84 "\\(wrote\\|writes\\|said\\|says\\):[ \t]*$"
85 "Regexp matching the end of an attribution line.
86 The text matching the first grouping will be used as a button.")
89 ;(defvar gnus-cite-attribution-face 'underline
90 ; "Face used for attribution lines.
91 ;It is merged with the face for the cited text belonging to the attribution.")
94 ;(defvar gnus-cite-hide-percentage 50
95 ; "Only hide cited text if it is larger than this percent of the body.")
98 ;(defvar gnus-cite-hide-absolute 10
99 ; "Only hide cited text if there is at least this number of cited lines.")
102 ;(defvar gnus-face-light-name-list
103 ; '("light blue" "light cyan" "light yellow" "light pink"
104 ; "pale green" "beige" "orange" "magenta" "violet" "medium purple"
106 ; "Names of light colors.")
109 ;(defvar gnus-face-dark-name-list
110 ; '("dark salmon" "firebrick"
111 ; "dark green" "dark orange" "dark khaki" "dark violet"
113 ; "Names of dark colors.")
115 ;;; Internal Variables:
117 (defvar gnus-cite-article nil)
119 (defvar gnus-cite-prefix-alist nil)
120 ;; Alist of citation prefixes.
121 ;; The cdr is a list of lines with that prefix.
123 (defvar gnus-cite-attribution-alist nil)
124 ;; Alist of attribution lines.
125 ;; The car is a line number.
126 ;; The cdr is the prefix for the citation started by that line.
128 (defvar gnus-cite-loose-prefix-alist nil)
129 ;; Alist of citation prefixes that have no matching attribution.
130 ;; The cdr is a list of lines with that prefix.
132 (defvar gnus-cite-loose-attribution-alist nil)
133 ;; Alist of attribution lines that have no matching citation.
134 ;; Each member has the form (WROTE IN PREFIX TAG), where
135 ;; WROTE: is the attribution line number
136 ;; IN: is the line number of the previous line if part of the same attribution,
137 ;; PREFIX: Is the citation prefix of the attribution line(s), and
138 ;; TAG: Is a Supercite tag, if any.
140 (defvar gnus-cited-text-button-line-format-alist
143 (?l (- end beg) ?d)))
144 (defvar gnus-cited-text-button-line-format-spec nil)
148 (defun gnus-article-highlight-citation (&optional force)
149 "Highlight cited text.
150 Each citation in the article will be highlighted with a different face.
151 The faces are taken from `gnus-cite-face-list'.
152 Attribution lines are highlighted with the same face as the
153 corresponding citation merged with `gnus-cite-attribution-face'.
155 Text is considered cited if at least `gnus-cite-minimum-match-count'
156 lines matches `gnus-cite-prefix-regexp' with the same prefix.
158 Lines matching `gnus-cite-attribution-suffix' and perhaps
159 `gnus-cite-attribution-prefix' are considered attribution lines."
160 (interactive (list 'force))
161 ;; Create dark or light faces if necessary.
162 (cond ((eq gnus-cite-face-list 'light)
163 (setq gnus-cite-face-list
164 (mapcar 'gnus-make-face gnus-face-light-name-list)))
165 ((eq gnus-cite-face-list 'dark)
166 (setq gnus-cite-face-list
167 (mapcar 'gnus-make-face gnus-face-dark-name-list))))
169 (set-buffer gnus-article-buffer)
170 (gnus-cite-parse-maybe force)
171 (let ((buffer-read-only nil)
172 (alist gnus-cite-prefix-alist)
173 (faces gnus-cite-face-list)
174 (inhibit-point-motion-hooks t)
175 face entry prefix skip numbers number face-alist)
176 ;; Loop through citation prefixes.
178 (setq entry (car alist)
183 faces (or (cdr faces) gnus-cite-face-list)
184 face-alist (cons (cons prefix face) face-alist))
186 (setq number (car numbers)
187 numbers (cdr numbers))
188 (and (not (assq number gnus-cite-attribution-alist))
189 (not (assq number gnus-cite-loose-attribution-alist))
190 (gnus-cite-add-face number prefix face))))
191 ;; Loop through attribution lines.
192 (setq alist gnus-cite-attribution-alist)
194 (setq entry (car alist)
198 skip (gnus-cite-find-prefix number)
199 face (cdr (assoc prefix face-alist)))
200 ;; Add attribution button.
202 (if (re-search-forward gnus-cite-attribution-suffix
203 (save-excursion (end-of-line 1) (point))
205 (gnus-article-add-button (match-beginning 1) (match-end 1)
206 'gnus-cite-toggle prefix))
207 ;; Highlight attribution line.
208 (gnus-cite-add-face number skip face)
209 (gnus-cite-add-face number skip gnus-cite-attribution-face))
210 ;; Loop through attribution lines.
211 (setq alist gnus-cite-loose-attribution-alist)
213 (setq entry (car alist)
216 skip (gnus-cite-find-prefix number))
217 (gnus-cite-add-face number skip gnus-cite-attribution-face)))))
219 (defun gnus-dissect-cited-text ()
220 "Dissect the article buffer looking for cited text."
222 (set-buffer gnus-article-buffer)
223 (gnus-cite-parse-maybe)
224 (let ((alist gnus-cite-prefix-alist)
225 prefix numbers number marks m)
226 ;; Loop through citation prefixes.
228 (setq numbers (pop alist)
229 prefix (pop numbers))
231 (setq number (pop numbers))
232 (goto-char (point-min))
233 (forward-line number)
234 (push (cons (point-marker) "") marks)
236 (= (1- number) (car numbers)))
237 (setq number (pop numbers)))
238 (goto-char (point-min))
239 (forward-line (1- number))
240 (push (cons (point-marker) prefix) marks)))
241 (goto-char (point-min))
242 (search-forward "\n\n" nil t)
243 (push (cons (point-marker) "") marks)
244 (goto-char (point-max))
245 (article-search-signature)
246 (push (cons (point-marker) "") marks)
247 (setq marks (sort marks (lambda (m1 m2) (< (car m1) (car m2)))))
248 (let* ((omarks marks))
251 (if (= (caar omarks) (caadr omarks))
253 (unless (equal (cdar omarks) "")
254 (push (car omarks) marks))
255 (unless (equal (cdadr omarks) "")
256 (push (cadr omarks) marks))
257 (setq omarks (cdr omarks)))
258 (push (car omarks) marks))
259 (setq omarks (cdr omarks)))
261 (push (car omarks) marks))
262 (setq marks (setq m (nreverse marks)))
264 (if (and (equal (cdadr m) "")
265 (equal (cdar m) (cdaddr m))
266 (goto-char (caadr m))
268 (= (point) (caaddr m)))
273 (defun gnus-article-fill-cited-article (&optional force)
274 "Do word wrapping in the current article."
275 (interactive (list t))
277 (set-buffer gnus-article-buffer)
278 (let ((buffer-read-only nil)
279 (inhibit-point-motion-hooks t)
280 (marks (gnus-dissect-cited-text))
281 (adaptive-fill-mode nil))
285 (narrow-to-region (caar marks) (caadr marks))
286 (let ((adaptive-fill-regexp
287 (concat "^" (regexp-quote (cdar marks)) " *"))
288 (fill-prefix (cdar marks)))
289 (fill-region (point-min) (point-max)))
290 (set-marker (caar marks) nil)
291 (setq marks (cdr marks)))
293 (set-marker (caar marks) nil))))))
295 (defun gnus-article-hide-citation (&optional arg force)
296 "Toggle hiding of all cited text except attribution lines.
297 See the documentation for `gnus-article-highlight-citation'.
298 If given a negative prefix, always show; if given a positive prefix,
300 (interactive (append (article-hidden-arg) (list 'force)))
301 (setq gnus-cited-text-button-line-format-spec
302 (gnus-parse-format gnus-cited-text-button-line-format
303 gnus-cited-text-button-line-format-alist t))
305 (set-buffer gnus-article-buffer)
307 ((article-check-hidden-text 'cite arg)
309 ((article-text-type-exists-p 'cite)
310 (let ((buffer-read-only nil))
311 (article-hide-text-of-type 'cite)))
313 (let ((buffer-read-only nil)
314 (marks (gnus-dissect-cited-text))
315 (inhibit-point-motion-hooks t)
316 (props (nconc (list 'article-type 'cite)
317 gnus-hidden-properties))
322 (while (and marks (string= (cdar marks) ""))
323 (setq marks (cdr marks)))
325 (setq beg (caar marks)))
326 (while (and marks (not (string= (cdar marks) "")))
327 (setq marks (cdr marks)))
329 (setq end (caar marks)))
330 ;; Skip past lines we want to leave visible.
331 (when (and beg end gnus-cited-lines-visible)
333 (forward-line gnus-cited-lines-visible)
336 (setq beg (point-marker))))
338 (gnus-add-text-properties beg end props)
340 (unless (save-excursion (search-backward "\n\n" nil t))
345 (gnus-article-add-button
347 (progn (eval gnus-cited-text-button-line-format-spec) (point))
348 `gnus-article-toggle-cited-text (cons beg end))
350 'article-type 'annotation)
351 (set-marker beg (point)))))))))
353 (defun gnus-article-toggle-cited-text (region)
354 "Toggle hiding the text in REGION."
355 (let (buffer-read-only)
357 (if (text-property-any
358 (car region) (1- (cdr region))
359 (car gnus-hidden-properties) (cadr gnus-hidden-properties))
360 'remove-text-properties 'gnus-add-text-properties)
361 (car region) (cdr region) gnus-hidden-properties)))
363 (defun gnus-article-hide-citation-maybe (&optional arg force)
364 "Toggle hiding of cited text that has an attribution line.
365 If given a negative prefix, always show; if given a positive prefix,
367 This will do nothing unless at least `gnus-cite-hide-percentage'
368 percent and at least `gnus-cite-hide-absolute' lines of the body is
369 cited text with attributions. When called interactively, these two
370 variables are ignored.
371 See also the documentation for `gnus-article-highlight-citation'."
372 (interactive (append (article-hidden-arg) (list 'force)))
373 (unless (article-check-hidden-text 'cite arg)
375 (set-buffer gnus-article-buffer)
376 (gnus-cite-parse-maybe force)
377 (goto-char (point-min))
378 (search-forward "\n\n" nil t)
379 (let ((start (point))
380 (atts gnus-cite-attribution-alist)
381 (buffer-read-only nil)
382 (inhibit-point-motion-hooks t)
385 (goto-char (point-max))
386 (article-search-signature)
387 (setq total (count-lines start (point)))
389 (setq hiden (+ hiden (length (cdr (assoc (cdar atts)
390 gnus-cite-prefix-alist))))
393 (and (> (* 100 hiden) (* gnus-cite-hide-percentage total))
394 (> hiden gnus-cite-hide-absolute)))
396 (setq atts gnus-cite-attribution-alist)
398 (setq total (cdr (assoc (cdar atts) gnus-cite-prefix-alist))
401 (setq hiden (car total)
404 (or (assq hiden gnus-cite-attribution-alist)
405 (gnus-add-text-properties
406 (point) (progn (forward-line 1) (point))
407 (nconc (list 'article-type 'cite)
408 gnus-hidden-properties)))))))))))
410 (defun gnus-article-hide-citation-in-followups ()
411 "Hide cited text in non-root articles."
414 (set-buffer gnus-article-buffer)
415 (let ((article (cdr gnus-article-current)))
416 (unless (save-excursion
417 (set-buffer gnus-summary-buffer)
418 (gnus-article-displayed-root-p article))
419 (gnus-article-hide-citation)))))
421 ;;; Internal functions:
423 (defun gnus-cite-parse-maybe (&optional force)
424 ;; Parse if the buffer has changes since last time.
425 (if (equal gnus-cite-article gnus-article-current)
427 ;;Reset parser information.
428 (setq gnus-cite-prefix-alist nil
429 gnus-cite-attribution-alist nil
430 gnus-cite-loose-prefix-alist nil
431 gnus-cite-loose-attribution-alist nil)
432 ;; Parse if not too large.
434 gnus-cite-parse-max-size
435 (> (buffer-size) gnus-cite-parse-max-size))
437 (setq gnus-cite-article (cons (car gnus-article-current)
438 (cdr gnus-article-current)))
441 (defun gnus-cite-parse ()
442 ;; Parse and connect citation prefixes and attribution lines.
444 ;; Parse current buffer searching for citation prefixes.
445 (goto-char (point-min))
446 (or (search-forward "\n\n" nil t)
447 (goto-char (point-max)))
448 (let ((line (1+ (count-lines (point-min) (point))))
451 (goto-char (point-max))
452 (article-search-signature)
454 alist entry start begin end numbers prefix)
455 ;; Get all potential prefixes in `alist'.
456 (while (< (point) max)
459 end (progn (beginning-of-line 2) (point))
462 ;; Ignore standard Supercite attribution prefix.
463 (if (looking-at gnus-supercite-regexp)
465 (setq end (1+ (match-end 1)))
466 (setq end (1+ begin))))
467 ;; Ignore very long prefixes.
468 (if (> end (+ (point) gnus-cite-max-prefix))
469 (setq end (+ (point) gnus-cite-max-prefix)))
470 (while (re-search-forward gnus-cite-prefix-regexp (1- end) t)
472 (setq end (match-end 0)
473 prefix (buffer-substring begin end))
474 (gnus-set-text-properties 0 (length prefix) nil prefix)
475 (setq entry (assoc prefix alist))
477 (setcdr entry (cons line (cdr entry)))
478 (setq alist (cons (list prefix line) alist)))
481 (setq line (1+ line)))
482 ;; We got all the potential prefixes. Now create
483 ;; `gnus-cite-prefix-alist' containing the oldest prefix for each
484 ;; line that appears at least gnus-cite-minimum-match-count
485 ;; times. First sort them by length. Longer is older.
486 (setq alist (sort alist (lambda (a b)
487 (> (length (car a)) (length (car b))))))
489 (setq entry (car alist)
493 (cond ((null numbers)
494 ;; No lines with this prefix that wasn't also part of
497 ((< (length numbers) gnus-cite-minimum-match-count)
498 ;; Too few lines with this prefix. We keep it a bit
499 ;; longer in case it is an exact match for an attribution
500 ;; line, but we don't remove the line from other
502 (setq gnus-cite-prefix-alist
503 (cons entry gnus-cite-prefix-alist)))
505 (setq gnus-cite-prefix-alist (cons entry
506 gnus-cite-prefix-alist))
507 ;; Remove articles from other prefixes.
511 (setq current (car loop)
514 (gnus-set-difference (cdr current) numbers))))))))
515 ;; No citations have been connected to attribution lines yet.
516 (setq gnus-cite-loose-prefix-alist (append gnus-cite-prefix-alist nil))
518 ;; Parse current buffer searching for attribution lines.
519 (goto-char (point-min))
520 (search-forward "\n\n" nil t)
521 (while (re-search-forward gnus-cite-attribution-suffix (point-max) t)
522 (let* ((start (match-beginning 0))
524 (wrote (count-lines (point-min) end))
525 (prefix (gnus-cite-find-prefix wrote))
526 ;; Check previous line for an attribution leader.
528 (beginning-of-line 1)
529 (and (looking-at gnus-supercite-secondary-regexp)
530 (buffer-substring (match-beginning 1)
534 (and (re-search-backward gnus-cite-attribution-prefix
536 (beginning-of-line 0)
539 (not (re-search-forward gnus-cite-attribution-suffix
541 (count-lines (point-min) (1+ (point)))))))
545 (setq gnus-cite-loose-attribution-alist
546 (cons (list wrote in prefix tag)
547 gnus-cite-loose-attribution-alist))))
548 ;; Find exact supercite citations.
549 (gnus-cite-match-attributions 'small nil
553 (regexp-quote prefix) "[ \t]*"
554 (regexp-quote tag) ">"))))
555 ;; Find loose supercite citations after attributions.
556 (gnus-cite-match-attributions 'small t
558 (if tag (concat "\\<"
561 ;; Find loose supercite citations anywhere.
562 (gnus-cite-match-attributions 'small nil
564 (if tag (concat "\\<"
567 ;; Find nested citations after attributions.
568 (gnus-cite-match-attributions 'small-if-unique t
570 (concat "\\`" (regexp-quote prefix) ".+")))
571 ;; Find nested citations anywhere.
572 (gnus-cite-match-attributions 'small nil
574 (concat "\\`" (regexp-quote prefix) ".+")))
575 ;; Remove loose prefixes with too few lines.
576 (let ((alist gnus-cite-loose-prefix-alist)
579 (setq entry (car alist)
581 (if (< (length (cdr entry)) gnus-cite-minimum-match-count)
582 (setq gnus-cite-prefix-alist
583 (delq entry gnus-cite-prefix-alist)
584 gnus-cite-loose-prefix-alist
585 (delq entry gnus-cite-loose-prefix-alist)))))
586 ;; Find flat attributions.
587 (gnus-cite-match-attributions 'first t nil)
588 ;; Find any attributions (are we getting desperate yet?).
589 (gnus-cite-match-attributions 'first nil nil))
591 (defun gnus-cite-match-attributions (sort after fun)
592 ;; Match all loose attributions and citations (SORT AFTER FUN) .
594 ;; If SORT is `small', the citation with the shortest prefix will be
595 ;; used, if it is `first' the first prefix will be used, if it is
596 ;; `small-if-unique' the shortest prefix will be used if the
597 ;; attribution line does not share its own prefix with other
598 ;; loose attribution lines, otherwise the first prefix will be used.
600 ;; If AFTER is non-nil, only citations after the attribution line
601 ;; will be considered.
603 ;; If FUN is non-nil, it will be called with the arguments (WROTE
604 ;; PREFIX TAG) and expected to return a regular expression. Only
605 ;; citations whose prefix matches the regular expression will be
608 ;; WROTE is the attribution line number.
609 ;; PREFIX is the attribution line prefix.
610 ;; TAG is the Supercite tag on the attribution line.
611 (let ((atts gnus-cite-loose-attribution-alist)
613 att wrote in prefix tag regexp limit smallest best size)
621 regexp (if fun (funcall fun prefix tag) "")
622 size (cond ((eq sort 'small) t)
623 ((eq sort 'first) nil)
624 (t (< (length (gnus-cite-find-loose prefix)) 2)))
625 limit (if after wrote -1)
628 (let ((cites gnus-cite-loose-prefix-alist)
629 cite candidate numbers first compare)
631 (setq cite (car cites)
635 first (apply 'min numbers)
636 compare (if size (length candidate) first))
639 (string-match regexp candidate)
645 (setq gnus-cite-loose-attribution-alist
646 (delq att gnus-cite-loose-attribution-alist))
647 (setq gnus-cite-attribution-alist
648 (cons (cons wrote (car best)) gnus-cite-attribution-alist))
650 (setq gnus-cite-attribution-alist
651 (cons (cons in (car best)) gnus-cite-attribution-alist)))
652 (if (memq best gnus-cite-loose-prefix-alist)
653 (let ((loop gnus-cite-prefix-alist)
656 (setq gnus-cite-loose-prefix-alist
657 (delq best gnus-cite-loose-prefix-alist))
659 (setq current (car loop)
661 (if (eq current best)
663 (setcdr current (gnus-set-difference (cdr current) numbers))
664 (if (null (cdr current))
665 (setq gnus-cite-loose-prefix-alist
666 (delq current gnus-cite-loose-prefix-alist)
667 atts (delq current atts)))))))))))
669 (defun gnus-cite-find-loose (prefix)
670 ;; Return a list of loose attribution lines prefixed by PREFIX.
671 (let* ((atts gnus-cite-loose-attribution-alist)
677 (if (string-equal (gnus-cite-find-prefix line) prefix)
678 (setq lines (cons line lines))))
681 (defun gnus-cite-add-face (number prefix face)
682 ;; At line NUMBER, ignore PREFIX and add FACE to the rest of the line.
684 (let ((inhibit-point-motion-hooks t)
687 (unless (eobp);; Sometimes things become confused.
688 (forward-char (length prefix))
689 (skip-chars-forward " \t")
692 (skip-chars-backward " \t")
695 (gnus-overlay-put (gnus-make-overlay from to) 'face face))))))
697 (defun gnus-cite-toggle (prefix)
699 (set-buffer gnus-article-buffer)
700 (let ((buffer-read-only nil)
701 (numbers (cdr (assoc prefix gnus-cite-prefix-alist)))
702 (inhibit-point-motion-hooks t)
705 (setq number (car numbers)
706 numbers (cdr numbers))
708 (cond ((get-text-property (point) 'invisible)
709 (remove-text-properties (point) (progn (forward-line 1) (point))
710 gnus-hidden-properties))
711 ((assq number gnus-cite-attribution-alist))
713 (gnus-add-text-properties
714 (point) (progn (forward-line 1) (point))
715 (nconc (list 'article-type 'cite)
716 gnus-hidden-properties))))))))
718 (defun gnus-cite-find-prefix (line)
719 ;; Return citation prefix for LINE.
720 (let ((alist gnus-cite-prefix-alist)
724 (setq entry (car alist)
726 (if (memq line (cdr entry))
727 (setq prefix (car entry))))
730 (gnus-add-shutdown 'gnus-cache-close 'gnus)
732 (defun gnus-cache-close ()
733 (setq gnus-cite-prefix-alist nil))
739 ;;; gnus-cite.el ends here