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.")
65 (defvar gnus-cite-attribution-prefix "in article\\|in <"
66 "Regexp matching the beginning of an attribution line.")
68 (defvar gnus-cite-attribution-suffix
69 "\\(wrote\\|writes\\|said\\|says\\):[ \t]*$"
70 "Regexp matching the end of an attribution line.
71 The text matching the first grouping will be used as a button.")
73 (defvar gnus-cite-attribution-face 'underline
74 "Face used for attribution lines.
75 It is merged with the face for the cited text belonging to the attribution.")
77 (defvar gnus-cite-face-list
78 (cond ((not (eq gnus-display-type 'color))
80 ((eq gnus-background-mode 'dark)
81 (mapcar 'gnus-make-face
82 gnus-face-light-name-list))
84 (mapcar 'gnus-make-face
85 gnus-face-dark-name-list)))
86 "List of faces used for highlighting citations.
88 When there are citations from multiple articles in the same message,
89 Gnus will try to give each citation from each article its own face.
90 This should make it easier to see who wrote what.")
92 (defvar gnus-cite-hide-percentage 50
93 "Only hide excess citation if above this percentage of the body.")
95 (defvar gnus-cite-hide-absolute 10
96 "Only hide excess citation if above this number of lines in the body.")
98 ;;; Internal Variables:
100 (defvar gnus-cite-article nil)
102 (defvar gnus-cite-prefix-alist nil)
103 ;; Alist of citation prefixes.
104 ;; The cdr is a list of lines with that prefix.
106 (defvar gnus-cite-attribution-alist nil)
107 ;; Alist of attribution lines.
108 ;; The car is a line number.
109 ;; The cdr is the prefix for the citation started by that line.
111 (defvar gnus-cite-loose-prefix-alist nil)
112 ;; Alist of citation prefixes that have no matching attribution.
113 ;; The cdr is a list of lines with that prefix.
115 (defvar gnus-cite-loose-attribution-alist nil)
116 ;; Alist of attribution lines that have no matching citation.
117 ;; Each member has the form (WROTE IN PREFIX TAG), where
118 ;; WROTE: is the attribution line number
119 ;; IN: is the line number of the previous line if part of the same attribution,
120 ;; PREFIX: Is the citation prefix of the attribution line(s), and
121 ;; TAG: Is a Supercite tag, if any.
123 (defvar gnus-cited-text-button-line-format-alist
126 (?l (- end beg) ?d)))
127 (defvar gnus-cited-text-button-line-format-spec nil)
131 (defun gnus-article-highlight-citation (&optional force)
132 "Highlight cited text.
133 Each citation in the article will be highlighted with a different face.
134 The faces are taken from `gnus-cite-face-list'.
135 Attribution lines are highlighted with the same face as the
136 corresponding citation merged with `gnus-cite-attribution-face'.
138 Text is considered cited if at least `gnus-cite-minimum-match-count'
139 lines matches `gnus-cite-prefix-regexp' with the same prefix.
141 Lines matching `gnus-cite-attribution-suffix' and perhaps
142 `gnus-cite-attribution-prefix' are considered attribution lines."
143 (interactive (list 'force))
144 ;; Create dark or light faces if necessary.
145 (cond ((eq gnus-cite-face-list 'light)
146 (setq gnus-cite-face-list
147 (mapcar 'gnus-make-face gnus-face-light-name-list)))
148 ((eq gnus-cite-face-list 'dark)
149 (setq gnus-cite-face-list
150 (mapcar 'gnus-make-face gnus-face-dark-name-list))))
152 (set-buffer gnus-article-buffer)
153 (gnus-cite-parse-maybe force)
154 (let ((buffer-read-only nil)
155 (alist gnus-cite-prefix-alist)
156 (faces gnus-cite-face-list)
157 (inhibit-point-motion-hooks t)
158 face entry prefix skip numbers number face-alist)
159 ;; Loop through citation prefixes.
161 (setq entry (car alist)
166 faces (or (cdr faces) gnus-cite-face-list)
167 face-alist (cons (cons prefix face) face-alist))
169 (setq number (car numbers)
170 numbers (cdr numbers))
171 (and (not (assq number gnus-cite-attribution-alist))
172 (not (assq number gnus-cite-loose-attribution-alist))
173 (gnus-cite-add-face number prefix face))))
174 ;; Loop through attribution lines.
175 (setq alist gnus-cite-attribution-alist)
177 (setq entry (car alist)
181 skip (gnus-cite-find-prefix number)
182 face (cdr (assoc prefix face-alist)))
183 ;; Add attribution button.
185 (if (re-search-forward gnus-cite-attribution-suffix
186 (save-excursion (end-of-line 1) (point))
188 (gnus-article-add-button (match-beginning 1) (match-end 1)
189 'gnus-cite-toggle prefix))
190 ;; Highlight attribution line.
191 (gnus-cite-add-face number skip face)
192 (gnus-cite-add-face number skip gnus-cite-attribution-face))
193 ;; Loop through attribution lines.
194 (setq alist gnus-cite-loose-attribution-alist)
196 (setq entry (car alist)
199 skip (gnus-cite-find-prefix number))
200 (gnus-cite-add-face number skip gnus-cite-attribution-face)))))
202 (defun gnus-dissect-cited-text ()
203 "Dissect the article buffer looking for cited text."
205 (set-buffer gnus-article-buffer)
206 (gnus-cite-parse-maybe)
207 (let ((alist gnus-cite-prefix-alist)
208 prefix numbers number marks m)
209 ;; Loop through citation prefixes.
211 (setq numbers (pop alist)
212 prefix (pop numbers))
214 (setq number (pop numbers))
215 (goto-char (point-min))
216 (forward-line number)
217 (push (cons (point-marker) "") marks)
219 (= (1- number) (car numbers)))
220 (setq number (pop numbers)))
221 (goto-char (point-min))
222 (forward-line (1- number))
223 (push (cons (point-marker) prefix) marks)))
224 (goto-char (point-min))
225 (search-forward "\n\n" nil t)
226 (push (cons (point-marker) "") marks)
227 (goto-char (point-max))
228 (article-search-signature)
229 (push (cons (point-marker) "") marks)
230 (setq marks (sort marks (lambda (m1 m2) (< (car m1) (car m2)))))
231 (let* ((omarks marks))
234 (if (= (caar omarks) (caadr omarks))
236 (unless (equal (cdar omarks) "")
237 (push (car omarks) marks))
238 (unless (equal (cdadr omarks) "")
239 (push (cadr omarks) marks))
240 (setq omarks (cdr omarks)))
241 (push (car omarks) marks))
242 (setq omarks (cdr omarks)))
244 (push (car omarks) marks))
245 (setq marks (setq m (nreverse marks)))
247 (if (and (equal (cdadr m) "")
248 (equal (cdar m) (cdaddr m))
249 (goto-char (caadr m))
251 (= (point) (caaddr m)))
256 (defun gnus-article-fill-cited-article (&optional force)
257 "Do word wrapping in the current article."
258 (interactive (list t))
260 (set-buffer gnus-article-buffer)
261 (let ((buffer-read-only nil)
262 (inhibit-point-motion-hooks t)
263 (marks (gnus-dissect-cited-text))
264 (adaptive-fill-mode nil))
268 (narrow-to-region (caar marks) (caadr marks))
269 (let ((adaptive-fill-regexp
270 (concat "^" (regexp-quote (cdar marks)) " *"))
271 (fill-prefix (cdar marks)))
272 (fill-region (point-min) (point-max)))
273 (set-marker (caar marks) nil)
274 (setq marks (cdr marks)))
276 (set-marker (caar marks) nil))
277 ;; All this information is now incorrect.
278 (setq gnus-cite-prefix-alist nil
279 gnus-cite-attribution-alist nil
280 gnus-cite-loose-prefix-alist nil
281 gnus-cite-loose-attribution-alist nil)))))
283 (defun gnus-article-hide-citation (&optional arg force)
284 "Toggle hiding of all cited text except attribution lines.
285 See the documentation for `gnus-article-highlight-citation'.
286 If given a negative prefix, always show; if given a positive prefix,
288 (interactive (append (article-hidden-arg) (list 'force)))
289 (setq gnus-cited-text-button-line-format-spec
290 (gnus-parse-format gnus-cited-text-button-line-format
291 gnus-cited-text-button-line-format-alist t))
293 (set-buffer gnus-article-buffer)
295 ((article-check-hidden-text 'cite arg)
297 ((article-text-type-exists-p 'cite)
298 (let ((buffer-read-only nil))
299 (article-hide-text-of-type 'cite)))
301 (let ((buffer-read-only nil)
302 (marks (gnus-dissect-cited-text))
303 (inhibit-point-motion-hooks t)
304 (props (nconc (list 'article-type 'cite)
305 gnus-hidden-properties))
310 (while (and marks (string= (cdar marks) ""))
311 (setq marks (cdr marks)))
313 (setq beg (caar marks)))
314 (while (and marks (not (string= (cdar marks) "")))
315 (setq marks (cdr marks)))
317 (setq end (caar marks)))
318 ;; Skip past lines we want to leave visible.
319 (when (and beg end gnus-cited-lines-visible)
321 (forward-line gnus-cited-lines-visible)
324 (setq beg (point-marker))))
326 (gnus-add-text-properties beg end props)
328 (unless (save-excursion (search-backward "\n\n" nil t))
333 (gnus-article-add-button
335 (progn (eval gnus-cited-text-button-line-format-spec) (point))
336 `gnus-article-toggle-cited-text (cons beg end))
338 'article-type 'annotation)
339 (set-marker beg (point)))))))))
341 (defun gnus-article-toggle-cited-text (region)
342 "Toggle hiding the text in REGION."
343 (let (buffer-read-only)
345 (if (text-property-any
346 (car region) (1- (cdr region))
347 (car gnus-hidden-properties) (cadr gnus-hidden-properties))
348 'remove-text-properties 'gnus-add-text-properties)
349 (car region) (cdr region) gnus-hidden-properties)))
351 (defun gnus-article-hide-citation-maybe (&optional arg force)
352 "Toggle hiding of cited text that has an attribution line.
353 If given a negative prefix, always show; if given a positive prefix,
355 This will do nothing unless at least `gnus-cite-hide-percentage'
356 percent and at least `gnus-cite-hide-absolute' lines of the body is
357 cited text with attributions. When called interactively, these two
358 variables are ignored.
359 See also the documentation for `gnus-article-highlight-citation'."
360 (interactive (append (article-hidden-arg) (list 'force)))
361 (unless (article-check-hidden-text 'cite arg)
363 (set-buffer gnus-article-buffer)
364 (gnus-cite-parse-maybe force)
365 (goto-char (point-min))
366 (search-forward "\n\n" nil t)
367 (let ((start (point))
368 (atts gnus-cite-attribution-alist)
369 (buffer-read-only nil)
370 (inhibit-point-motion-hooks t)
373 (goto-char (point-max))
374 (article-search-signature)
375 (setq total (count-lines start (point)))
377 (setq hiden (+ hiden (length (cdr (assoc (cdar atts)
378 gnus-cite-prefix-alist))))
381 (and (> (* 100 hiden) (* gnus-cite-hide-percentage total))
382 (> hiden gnus-cite-hide-absolute)))
384 (setq atts gnus-cite-attribution-alist)
386 (setq total (cdr (assoc (cdar atts) gnus-cite-prefix-alist))
389 (setq hiden (car total)
392 (or (assq hiden gnus-cite-attribution-alist)
393 (gnus-add-text-properties
394 (point) (progn (forward-line 1) (point))
395 (nconc (list 'article-type 'cite)
396 gnus-hidden-properties)))))))))))
398 (defun gnus-article-hide-citation-in-followups ()
399 "Hide cited text in non-root articles."
402 (set-buffer gnus-article-buffer)
403 (let ((article (cdr gnus-article-current)))
404 (unless (save-excursion
405 (set-buffer gnus-summary-buffer)
406 (gnus-article-displayed-root-p article))
407 (gnus-article-hide-citation)))))
409 ;;; Internal functions:
411 (defun gnus-cite-parse-maybe (&optional force)
412 ;; Parse if the buffer has changes since last time.
413 (if (equal gnus-cite-article gnus-article-current)
415 ;;Reset parser information.
416 (setq gnus-cite-prefix-alist nil
417 gnus-cite-attribution-alist nil
418 gnus-cite-loose-prefix-alist nil
419 gnus-cite-loose-attribution-alist nil)
420 ;; Parse if not too large.
422 gnus-cite-parse-max-size
423 (> (buffer-size) gnus-cite-parse-max-size))
425 (setq gnus-cite-article (cons (car gnus-article-current)
426 (cdr gnus-article-current)))
429 (defun gnus-cite-parse ()
430 ;; Parse and connect citation prefixes and attribution lines.
432 ;; Parse current buffer searching for citation prefixes.
433 (goto-char (point-min))
434 (or (search-forward "\n\n" nil t)
435 (goto-char (point-max)))
436 (let ((line (1+ (count-lines (point-min) (point))))
439 (goto-char (point-max))
440 (article-search-signature)
442 alist entry start begin end numbers prefix)
443 ;; Get all potential prefixes in `alist'.
444 (while (< (point) max)
447 end (progn (beginning-of-line 2) (point))
450 ;; Ignore standard Supercite attribution prefix.
451 (if (looking-at gnus-supercite-regexp)
453 (setq end (1+ (match-end 1)))
454 (setq end (1+ begin))))
455 ;; Ignore very long prefixes.
456 (if (> end (+ (point) gnus-cite-max-prefix))
457 (setq end (+ (point) gnus-cite-max-prefix)))
458 (while (re-search-forward gnus-cite-prefix-regexp (1- end) t)
460 (setq end (match-end 0)
461 prefix (buffer-substring begin end))
462 (gnus-set-text-properties 0 (length prefix) nil prefix)
463 (setq entry (assoc prefix alist))
465 (setcdr entry (cons line (cdr entry)))
466 (setq alist (cons (list prefix line) alist)))
469 (setq line (1+ line)))
470 ;; We got all the potential prefixes. Now create
471 ;; `gnus-cite-prefix-alist' containing the oldest prefix for each
472 ;; line that appears at least gnus-cite-minimum-match-count
473 ;; times. First sort them by length. Longer is older.
474 (setq alist (sort alist (lambda (a b)
475 (> (length (car a)) (length (car b))))))
477 (setq entry (car alist)
481 (cond ((null numbers)
482 ;; No lines with this prefix that wasn't also part of
485 ((< (length numbers) gnus-cite-minimum-match-count)
486 ;; Too few lines with this prefix. We keep it a bit
487 ;; longer in case it is an exact match for an attribution
488 ;; line, but we don't remove the line from other
490 (setq gnus-cite-prefix-alist
491 (cons entry gnus-cite-prefix-alist)))
493 (setq gnus-cite-prefix-alist (cons entry
494 gnus-cite-prefix-alist))
495 ;; Remove articles from other prefixes.
499 (setq current (car loop)
502 (gnus-set-difference (cdr current) numbers))))))))
503 ;; No citations have been connected to attribution lines yet.
504 (setq gnus-cite-loose-prefix-alist (append gnus-cite-prefix-alist nil))
506 ;; Parse current buffer searching for attribution lines.
507 (goto-char (point-min))
508 (search-forward "\n\n" nil t)
509 (while (re-search-forward gnus-cite-attribution-suffix (point-max) t)
510 (let* ((start (match-beginning 0))
512 (wrote (count-lines (point-min) end))
513 (prefix (gnus-cite-find-prefix wrote))
514 ;; Check previous line for an attribution leader.
516 (beginning-of-line 1)
517 (and (looking-at gnus-supercite-secondary-regexp)
518 (buffer-substring (match-beginning 1)
522 (and (re-search-backward gnus-cite-attribution-prefix
524 (beginning-of-line 0)
527 (not (re-search-forward gnus-cite-attribution-suffix
529 (count-lines (point-min) (1+ (point)))))))
533 (setq gnus-cite-loose-attribution-alist
534 (cons (list wrote in prefix tag)
535 gnus-cite-loose-attribution-alist))))
536 ;; Find exact supercite citations.
537 (gnus-cite-match-attributions 'small nil
541 (regexp-quote prefix) "[ \t]*"
542 (regexp-quote tag) ">"))))
543 ;; Find loose supercite citations after attributions.
544 (gnus-cite-match-attributions 'small t
546 (if tag (concat "\\<"
549 ;; Find loose supercite citations anywhere.
550 (gnus-cite-match-attributions 'small nil
552 (if tag (concat "\\<"
555 ;; Find nested citations after attributions.
556 (gnus-cite-match-attributions 'small-if-unique t
558 (concat "\\`" (regexp-quote prefix) ".+")))
559 ;; Find nested citations anywhere.
560 (gnus-cite-match-attributions 'small nil
562 (concat "\\`" (regexp-quote prefix) ".+")))
563 ;; Remove loose prefixes with too few lines.
564 (let ((alist gnus-cite-loose-prefix-alist)
567 (setq entry (car alist)
569 (if (< (length (cdr entry)) gnus-cite-minimum-match-count)
570 (setq gnus-cite-prefix-alist
571 (delq entry gnus-cite-prefix-alist)
572 gnus-cite-loose-prefix-alist
573 (delq entry gnus-cite-loose-prefix-alist)))))
574 ;; Find flat attributions.
575 (gnus-cite-match-attributions 'first t nil)
576 ;; Find any attributions (are we getting desperate yet?).
577 (gnus-cite-match-attributions 'first nil nil))
579 (defun gnus-cite-match-attributions (sort after fun)
580 ;; Match all loose attributions and citations (SORT AFTER FUN) .
582 ;; If SORT is `small', the citation with the shortest prefix will be
583 ;; used, if it is `first' the first prefix will be used, if it is
584 ;; `small-if-unique' the shortest prefix will be used if the
585 ;; attribution line does not share its own prefix with other
586 ;; loose attribution lines, otherwise the first prefix will be used.
588 ;; If AFTER is non-nil, only citations after the attribution line
589 ;; will be considered.
591 ;; If FUN is non-nil, it will be called with the arguments (WROTE
592 ;; PREFIX TAG) and expected to return a regular expression. Only
593 ;; citations whose prefix matches the regular expression will be
596 ;; WROTE is the attribution line number.
597 ;; PREFIX is the attribution line prefix.
598 ;; TAG is the Supercite tag on the attribution line.
599 (let ((atts gnus-cite-loose-attribution-alist)
601 att wrote in prefix tag regexp limit smallest best size)
609 regexp (if fun (funcall fun prefix tag) "")
610 size (cond ((eq sort 'small) t)
611 ((eq sort 'first) nil)
612 (t (< (length (gnus-cite-find-loose prefix)) 2)))
613 limit (if after wrote -1)
616 (let ((cites gnus-cite-loose-prefix-alist)
617 cite candidate numbers first compare)
619 (setq cite (car cites)
623 first (apply 'min numbers)
624 compare (if size (length candidate) first))
627 (string-match regexp candidate)
633 (setq gnus-cite-loose-attribution-alist
634 (delq att gnus-cite-loose-attribution-alist))
635 (setq gnus-cite-attribution-alist
636 (cons (cons wrote (car best)) gnus-cite-attribution-alist))
638 (setq gnus-cite-attribution-alist
639 (cons (cons in (car best)) gnus-cite-attribution-alist)))
640 (if (memq best gnus-cite-loose-prefix-alist)
641 (let ((loop gnus-cite-prefix-alist)
644 (setq gnus-cite-loose-prefix-alist
645 (delq best gnus-cite-loose-prefix-alist))
647 (setq current (car loop)
649 (if (eq current best)
651 (setcdr current (gnus-set-difference (cdr current) numbers))
652 (if (null (cdr current))
653 (setq gnus-cite-loose-prefix-alist
654 (delq current gnus-cite-loose-prefix-alist)
655 atts (delq current atts)))))))))))
657 (defun gnus-cite-find-loose (prefix)
658 ;; Return a list of loose attribution lines prefixed by PREFIX.
659 (let* ((atts gnus-cite-loose-attribution-alist)
665 (if (string-equal (gnus-cite-find-prefix line) prefix)
666 (setq lines (cons line lines))))
669 (defun gnus-cite-add-face (number prefix face)
670 ;; At line NUMBER, ignore PREFIX and add FACE to the rest of the line.
672 (let ((inhibit-point-motion-hooks t)
675 (unless (eobp);; Sometimes things become confused.
676 (forward-char (length prefix))
677 (skip-chars-forward " \t")
680 (skip-chars-backward " \t")
683 (gnus-overlay-put (gnus-make-overlay from to) 'face face))))))
685 (defun gnus-cite-toggle (prefix)
687 (set-buffer gnus-article-buffer)
688 (let ((buffer-read-only nil)
689 (numbers (cdr (assoc prefix gnus-cite-prefix-alist)))
690 (inhibit-point-motion-hooks t)
693 (setq number (car numbers)
694 numbers (cdr numbers))
696 (cond ((get-text-property (point) 'invisible)
697 (remove-text-properties (point) (progn (forward-line 1) (point))
698 gnus-hidden-properties))
699 ((assq number gnus-cite-attribution-alist))
701 (gnus-add-text-properties
702 (point) (progn (forward-line 1) (point))
703 (nconc (list 'article-type 'cite)
704 gnus-hidden-properties))))))))
706 (defun gnus-cite-find-prefix (line)
707 ;; Return citation prefix for LINE.
708 (let ((alist gnus-cite-prefix-alist)
712 (setq entry (car alist)
714 (if (memq line (cdr entry))
715 (setq prefix (car entry))))
718 (gnus-add-shutdown 'gnus-cache-close 'gnus)
720 (defun gnus-cache-close ()
721 (setq gnus-cite-prefix-alist nil))
727 ;;; gnus-cite.el ends here