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.
31 (eval-when-compile (require 'cl))
34 (autoload 'gnus-article-add-button "gnus-vis"))
38 (defvar gnus-cited-text-button-line-format "%(%{[...]%}%)\n"
39 "Format of cited text buttons.")
41 (defvar gnus-cited-lines-visible nil
42 "The number of lines of hidden cited text to remain visible.")
44 (defvar gnus-cite-parse-max-size 25000
45 "Maximum article size (in bytes) where parsing citations is allowed.
46 Set it to nil to parse all articles.")
48 (defvar gnus-cite-prefix-regexp
49 "^[]>|:}+ ]*[]>|:}+]\\(.*>\\)?\\|^.*>"
50 "Regexp matching the longest possible citation prefix on a line.")
52 (defvar gnus-cite-max-prefix 20
53 "Maximum possible length for a citation prefix.")
55 (defvar gnus-supercite-regexp
56 (concat "^\\(" gnus-cite-prefix-regexp "\\)? *"
57 ">>>>> +\"\\([^\"\n]+\\)\" +==")
58 "Regexp matching normal SuperCite attribution lines.
59 The first grouping must match prefixes added by other packages.")
61 (defvar gnus-supercite-secondary-regexp "^.*\"\\([^\"\n]+\\)\" +=="
62 "Regexp matching mangled SuperCite attribution lines.
63 The first regexp group should match the SuperCite attribution.")
65 (defvar gnus-cite-minimum-match-count 2
66 "Minimum number of identical prefixes before we believe it's a citation.")
69 ;(defvar gnus-cite-face-list
70 ; (if (eq gnus-display-type 'color)
71 ; (if (eq gnus-background-mode 'dark) 'light 'dark)
73 ; "Faces used for displaying different citations.
74 ;It is either a list of face names, or one of the following special
77 ;dark: Create faces from `gnus-face-dark-name-list'.
78 ;light: Create faces from `gnus-face-light-name-list'.
80 ;The variable `gnus-make-foreground' determines whether the created
81 ;faces change the foreground or the background colors.")
83 (defvar gnus-cite-attribution-prefix "in article\\|in <"
84 "Regexp matching the beginning of an attribution line.")
86 (defvar gnus-cite-attribution-suffix
87 "\\(wrote\\|writes\\|said\\|says\\):[ \t]*$"
88 "Regexp matching the end of an attribution line.
89 The text matching the first grouping will be used as a button.")
92 ;(defvar gnus-cite-attribution-face 'underline
93 ; "Face used for attribution lines.
94 ;It is merged with the face for the cited text belonging to the attribution.")
97 ;(defvar gnus-cite-hide-percentage 50
98 ; "Only hide cited text if it is larger than this percent of the body.")
101 ;(defvar gnus-cite-hide-absolute 10
102 ; "Only hide cited text if there is at least this number of cited lines.")
105 ;(defvar gnus-face-light-name-list
106 ; '("light blue" "light cyan" "light yellow" "light pink"
107 ; "pale green" "beige" "orange" "magenta" "violet" "medium purple"
109 ; "Names of light colors.")
112 ;(defvar gnus-face-dark-name-list
113 ; '("dark salmon" "firebrick"
114 ; "dark green" "dark orange" "dark khaki" "dark violet"
116 ; "Names of dark colors.")
118 ;;; Internal Variables:
120 (defvar gnus-article-length nil)
121 ;; Length of article last time we parsed it.
122 ;; BUG! KLUDGE! UGLY! FIX ME!
124 (defvar gnus-cite-prefix-alist nil)
125 ;; Alist of citation prefixes.
126 ;; The cdr is a list of lines with that prefix.
128 (defvar gnus-cite-attribution-alist nil)
129 ;; Alist of attribution lines.
130 ;; The car is a line number.
131 ;; The cdr is the prefix for the citation started by that line.
133 (defvar gnus-cite-loose-prefix-alist nil)
134 ;; Alist of citation prefixes that have no matching attribution.
135 ;; The cdr is a list of lines with that prefix.
137 (defvar gnus-cite-loose-attribution-alist nil)
138 ;; Alist of attribution lines that have no matching citation.
139 ;; Each member has the form (WROTE IN PREFIX TAG), where
140 ;; WROTE: is the attribution line number
141 ;; IN: is the line number of the previous line if part of the same attribution,
142 ;; PREFIX: Is the citation prefix of the attribution line(s), and
143 ;; TAG: Is a SuperCite tag, if any.
145 (defvar gnus-cited-text-button-line-format-alist
148 (?l (- end beg) ?d)))
149 (defvar gnus-cited-text-button-line-format-spec nil)
153 (defun gnus-article-highlight-citation (&optional force)
154 "Highlight cited text.
155 Each citation in the article will be highlighted with a different face.
156 The faces are taken from `gnus-cite-face-list'.
157 Attribution lines are highlighted with the same face as the
158 corresponding citation merged with `gnus-cite-attribution-face'.
160 Text is considered cited if at least `gnus-cite-minimum-match-count'
161 lines matches `gnus-cite-prefix-regexp' with the same prefix.
163 Lines matching `gnus-cite-attribution-suffix' and perhaps
164 `gnus-cite-attribution-prefix' are considered attribution lines."
165 (interactive (list 'force))
166 ;; Create dark or light faces if necessary.
167 (cond ((eq gnus-cite-face-list 'light)
168 (setq gnus-cite-face-list
169 (mapcar 'gnus-make-face gnus-face-light-name-list)))
170 ((eq gnus-cite-face-list 'dark)
171 (setq gnus-cite-face-list
172 (mapcar 'gnus-make-face gnus-face-dark-name-list))))
174 (set-buffer gnus-article-buffer)
175 (gnus-cite-parse-maybe force)
176 (let ((buffer-read-only nil)
177 (alist gnus-cite-prefix-alist)
178 (faces gnus-cite-face-list)
179 (inhibit-point-motion-hooks t)
180 face entry prefix skip numbers number face-alist)
181 ;; Loop through citation prefixes.
183 (setq entry (car alist)
188 faces (or (cdr faces) gnus-cite-face-list)
189 face-alist (cons (cons prefix face) face-alist))
191 (setq number (car numbers)
192 numbers (cdr numbers))
193 (and (not (assq number gnus-cite-attribution-alist))
194 (not (assq number gnus-cite-loose-attribution-alist))
195 (gnus-cite-add-face number prefix face))))
196 ;; Loop through attribution lines.
197 (setq alist gnus-cite-attribution-alist)
199 (setq entry (car alist)
203 skip (gnus-cite-find-prefix number)
204 face (cdr (assoc prefix face-alist)))
205 ;; Add attribution button.
207 (if (re-search-forward gnus-cite-attribution-suffix
208 (save-excursion (end-of-line 1) (point))
210 (gnus-article-add-button (match-beginning 1) (match-end 1)
211 'gnus-cite-toggle prefix))
212 ;; Highlight attribution line.
213 (gnus-cite-add-face number skip face)
214 (gnus-cite-add-face number skip gnus-cite-attribution-face))
215 ;; Loop through attribution lines.
216 (setq alist gnus-cite-loose-attribution-alist)
218 (setq entry (car alist)
221 skip (gnus-cite-find-prefix number))
222 (gnus-cite-add-face number skip gnus-cite-attribution-face)))))
224 (defun gnus-dissect-cited-text ()
225 "Dissect the article buffer looking for cited text."
227 (set-buffer gnus-article-buffer)
228 (gnus-cite-parse-maybe)
229 (let ((alist gnus-cite-prefix-alist)
230 prefix numbers number marks m)
231 ;; Loop through citation prefixes.
233 (setq numbers (pop alist)
234 prefix (pop numbers))
236 (setq number (pop numbers))
237 (goto-char (point-min))
238 (forward-line number)
239 (push (cons (point-marker) "") marks)
241 (= (1- number) (car numbers)))
242 (setq number (pop numbers)))
243 (goto-char (point-min))
244 (forward-line (1- number))
245 (push (cons (point-marker) prefix) marks)))
246 (goto-char (point-min))
247 (search-forward "\n\n" nil t)
248 (push (cons (point-marker) "") marks)
249 (goto-char (point-max))
250 (re-search-backward gnus-signature-separator nil t)
251 (push (cons (point-marker) "") marks)
252 (setq marks (sort marks (lambda (m1 m2) (< (car m1) (car m2)))))
253 (let* ((omarks marks))
256 (if (= (caar omarks) (caadr omarks))
258 (unless (equal (cdar omarks) "")
259 (push (car omarks) marks))
260 (unless (equal (cdadr omarks) "")
261 (push (cadr omarks) marks))
262 (setq omarks (cdr omarks)))
263 (push (car omarks) marks))
264 (setq omarks (cdr omarks)))
266 (push (car omarks) marks))
267 (setq marks (setq m (nreverse marks)))
269 (if (and (equal (cdadr m) "")
270 (equal (cdar m) (cdaddr m))
271 (goto-char (caadr m))
273 (= (point) (caaddr m)))
279 (defun gnus-article-fill-cited-article (&optional force)
280 "Do word wrapping in the current article."
281 (interactive (list t))
283 (set-buffer gnus-article-buffer)
284 (let ((buffer-read-only nil)
285 (inhibit-point-motion-hooks t)
286 (marks (gnus-dissect-cited-text))
287 (adaptive-fill-mode nil))
291 (narrow-to-region (caar marks) (caadr marks))
292 (let ((adaptive-fill-regexp
293 (concat "^" (regexp-quote (cdar marks)) " *"))
294 (fill-prefix (cdar marks)))
295 (fill-region (point-min) (point-max)))
296 (set-marker (caar marks) nil)
297 (setq marks (cdr marks)))
298 (set-marker (caar marks) nil)))))
300 (defun gnus-article-hide-citation (&optional arg force)
301 "Toggle hiding of all cited text except attribution lines.
302 See the documentation for `gnus-article-highlight-citation'.
303 If given a negative prefix, always show; if given a positive prefix,
305 (interactive (list current-prefix-arg 'force))
306 (setq gnus-cited-text-button-line-format-spec
307 (gnus-parse-format gnus-cited-text-button-line-format
308 gnus-cited-text-button-line-format-alist t))
309 (unless (gnus-article-check-hidden-text 'cite arg)
311 (set-buffer gnus-article-buffer)
312 (let ((buffer-read-only nil)
313 (marks (gnus-dissect-cited-text))
314 (inhibit-point-motion-hooks t)
315 (props (nconc (list 'gnus-type 'cite)
316 gnus-hidden-properties))
321 (while (and marks (string= (cdar marks) ""))
322 (setq marks (cdr marks)))
324 (setq beg (caar marks)))
325 (while (and marks (not (string= (cdar marks) "")))
326 (setq marks (cdr marks)))
328 (setq end (caar marks)))
329 ;; Skip past lines we want to leave visible.
330 (when (and beg gnus-cited-lines-visible)
332 (forward-line gnus-cited-lines-visible)
335 (setq beg (point-marker))))
337 (gnus-add-text-properties beg end props)
339 (unless (save-excursion (search-backward "\n\n" nil t))
341 (gnus-article-add-button
343 (progn (eval gnus-cited-text-button-line-format-spec) (point))
344 `gnus-article-toggle-cited-text (cons beg end))
345 (set-marker beg (point))))))))
347 (defun gnus-article-toggle-cited-text (region)
348 "Toggle hiding the text in REGION."
349 (let (buffer-read-only)
351 (if (text-property-any
352 (car region) (1- (cdr region))
353 (car gnus-hidden-properties) (cadr gnus-hidden-properties))
354 'remove-text-properties 'gnus-add-text-properties)
355 (car region) (cdr region) gnus-hidden-properties)))
357 (defun gnus-article-hide-citation-maybe (&optional arg force)
358 "Toggle hiding of cited text that has an attribution line.
359 If given a negative prefix, always show; if given a positive prefix,
361 This will do nothing unless at least `gnus-cite-hide-percentage'
362 percent and at least `gnus-cite-hide-absolute' lines of the body is
363 cited text with attributions. When called interactively, these two
364 variables are ignored.
365 See also the documentation for `gnus-article-highlight-citation'."
366 (interactive (list current-prefix-arg 'force))
367 (unless (gnus-article-check-hidden-text 'cite arg)
369 (set-buffer gnus-article-buffer)
370 (gnus-cite-parse-maybe force)
371 (goto-char (point-min))
372 (search-forward "\n\n" nil t)
373 (let ((start (point))
374 (atts gnus-cite-attribution-alist)
375 (buffer-read-only nil)
376 (inhibit-point-motion-hooks t)
379 (goto-char (point-max))
380 (re-search-backward gnus-signature-separator nil t)
381 (setq total (count-lines start (point)))
383 (setq hiden (+ hiden (length (cdr (assoc (cdar atts)
384 gnus-cite-prefix-alist))))
387 (and (> (* 100 hiden) (* gnus-cite-hide-percentage total))
388 (> hiden gnus-cite-hide-absolute)))
390 (setq atts gnus-cite-attribution-alist)
392 (setq total (cdr (assoc (cdar atts) gnus-cite-prefix-alist))
395 (setq hiden (car total)
398 (or (assq hiden gnus-cite-attribution-alist)
399 (gnus-add-text-properties
400 (point) (progn (forward-line 1) (point))
401 (nconc (list 'gnus-type 'cite)
402 gnus-hidden-properties)))))))))))
404 (defun gnus-article-hide-citation-in-followups ()
405 "Hide cited text in non-root articles."
408 (set-buffer gnus-article-buffer)
409 (let ((article (cdr gnus-article-current)))
410 (unless (save-excursion
411 (set-buffer gnus-summary-buffer)
412 (gnus-article-displayed-root-p article))
413 (gnus-article-hide-citation)))))
415 ;;; Internal functions:
417 (defun gnus-cite-parse-maybe (&optional force)
418 ;; Parse if the buffer has changes since last time.
419 (if (eq gnus-article-length (- (point-max) (point-min)))
421 ;;Reset parser information.
422 (setq gnus-cite-prefix-alist nil
423 gnus-cite-attribution-alist nil
424 gnus-cite-loose-prefix-alist nil
425 gnus-cite-loose-attribution-alist nil)
426 ;; Parse if not too large.
428 gnus-cite-parse-max-size
429 (> (buffer-size) gnus-cite-parse-max-size))
431 (setq gnus-article-length (- (point-max) (point-min)))
434 (defun gnus-cite-parse ()
435 ;; Parse and connect citation prefixes and attribution lines.
437 ;; Parse current buffer searching for citation prefixes.
438 (goto-char (point-min))
439 (or (search-forward "\n\n" nil t)
440 (goto-char (point-max)))
441 (let ((line (1+ (count-lines (point-min) (point))))
444 (goto-char (point-max))
445 (re-search-backward gnus-signature-separator nil t)
447 alist entry start begin end numbers prefix)
448 ;; Get all potential prefixes in `alist'.
449 (while (< (point) max)
452 end (progn (beginning-of-line 2) (point))
455 ;; Ignore standard SuperCite attribution prefix.
456 (if (looking-at gnus-supercite-regexp)
458 (setq end (1+ (match-end 1)))
459 (setq end (1+ begin))))
460 ;; Ignore very long prefixes.
461 (if (> end (+ (point) gnus-cite-max-prefix))
462 (setq end (+ (point) gnus-cite-max-prefix)))
463 (while (re-search-forward gnus-cite-prefix-regexp (1- end) t)
465 (setq end (match-end 0)
466 prefix (buffer-substring begin end))
467 (gnus-set-text-properties 0 (length prefix) nil prefix)
468 (setq entry (assoc prefix alist))
470 (setcdr entry (cons line (cdr entry)))
471 (setq alist (cons (list prefix line) alist)))
474 (setq line (1+ line)))
475 ;; We got all the potential prefixes. Now create
476 ;; `gnus-cite-prefix-alist' containing the oldest prefix for each
477 ;; line that appears at least gnus-cite-minimum-match-count
478 ;; times. First sort them by length. Longer is older.
479 (setq alist (sort alist (lambda (a b)
480 (> (length (car a)) (length (car b))))))
482 (setq entry (car alist)
486 (cond ((null numbers)
487 ;; No lines with this prefix that wasn't also part of
490 ((< (length numbers) gnus-cite-minimum-match-count)
491 ;; Too few lines with this prefix. We keep it a bit
492 ;; longer in case it is an exact match for an attribution
493 ;; line, but we don't remove the line from other
495 (setq gnus-cite-prefix-alist
496 (cons entry gnus-cite-prefix-alist)))
498 (setq gnus-cite-prefix-alist (cons entry
499 gnus-cite-prefix-alist))
500 ;; Remove articles from other prefixes.
504 (setq current (car loop)
507 (gnus-set-difference (cdr current) numbers))))))))
508 ;; No citations have been connected to attribution lines yet.
509 (setq gnus-cite-loose-prefix-alist (append gnus-cite-prefix-alist nil))
511 ;; Parse current buffer searching for attribution lines.
512 (goto-char (point-min))
513 (search-forward "\n\n" nil t)
514 (while (re-search-forward gnus-cite-attribution-suffix (point-max) t)
515 (let* ((start (match-beginning 0))
517 (wrote (count-lines (point-min) end))
518 (prefix (gnus-cite-find-prefix wrote))
519 ;; Check previous line for an attribution leader.
521 (beginning-of-line 1)
522 (and (looking-at gnus-supercite-secondary-regexp)
523 (buffer-substring (match-beginning 1)
527 (and (re-search-backward gnus-cite-attribution-prefix
529 (beginning-of-line 0)
532 (not (re-search-forward gnus-cite-attribution-suffix
534 (count-lines (point-min) (1+ (point)))))))
538 (setq gnus-cite-loose-attribution-alist
539 (cons (list wrote in prefix tag)
540 gnus-cite-loose-attribution-alist))))
541 ;; Find exact supercite citations.
542 (gnus-cite-match-attributions 'small nil
546 (regexp-quote prefix) "[ \t]*"
547 (regexp-quote tag) ">"))))
548 ;; Find loose supercite citations after attributions.
549 (gnus-cite-match-attributions 'small t
551 (if tag (concat "\\<"
554 ;; Find loose supercite citations anywhere.
555 (gnus-cite-match-attributions 'small nil
557 (if tag (concat "\\<"
560 ;; Find nested citations after attributions.
561 (gnus-cite-match-attributions 'small-if-unique t
563 (concat "\\`" (regexp-quote prefix) ".+")))
564 ;; Find nested citations anywhere.
565 (gnus-cite-match-attributions 'small nil
567 (concat "\\`" (regexp-quote prefix) ".+")))
568 ;; Remove loose prefixes with too few lines.
569 (let ((alist gnus-cite-loose-prefix-alist)
572 (setq entry (car alist)
574 (if (< (length (cdr entry)) gnus-cite-minimum-match-count)
575 (setq gnus-cite-prefix-alist
576 (delq entry gnus-cite-prefix-alist)
577 gnus-cite-loose-prefix-alist
578 (delq entry gnus-cite-loose-prefix-alist)))))
579 ;; Find flat attributions.
580 (gnus-cite-match-attributions 'first t nil)
581 ;; Find any attributions (are we getting desperate yet?).
582 (gnus-cite-match-attributions 'first nil nil))
584 (defun gnus-cite-match-attributions (sort after fun)
585 ;; Match all loose attributions and citations (SORT AFTER FUN) .
587 ;; If SORT is `small', the citation with the shortest prefix will be
588 ;; used, if it is `first' the first prefix will be used, if it is
589 ;; `small-if-unique' the shortest prefix will be used if the
590 ;; attribution line does not share its own prefix with other
591 ;; loose attribution lines, otherwise the first prefix will be used.
593 ;; If AFTER is non-nil, only citations after the attribution line
594 ;; will be considered.
596 ;; If FUN is non-nil, it will be called with the arguments (WROTE
597 ;; PREFIX TAG) and expected to return a regular expression. Only
598 ;; citations whose prefix matches the regular expression will be
601 ;; WROTE is the attribution line number.
602 ;; PREFIX is the attribution line prefix.
603 ;; TAG is the SuperCite tag on the attribution line.
604 (let ((atts gnus-cite-loose-attribution-alist)
606 att wrote in prefix tag regexp limit smallest best size)
614 regexp (if fun (funcall fun prefix tag) "")
615 size (cond ((eq sort 'small) t)
616 ((eq sort 'first) nil)
617 (t (< (length (gnus-cite-find-loose prefix)) 2)))
618 limit (if after wrote -1)
621 (let ((cites gnus-cite-loose-prefix-alist)
622 cite candidate numbers first compare)
624 (setq cite (car cites)
628 first (apply 'min numbers)
629 compare (if size (length candidate) first))
632 (string-match regexp candidate)
638 (setq gnus-cite-loose-attribution-alist
639 (delq att gnus-cite-loose-attribution-alist))
640 (setq gnus-cite-attribution-alist
641 (cons (cons wrote (car best)) gnus-cite-attribution-alist))
643 (setq gnus-cite-attribution-alist
644 (cons (cons in (car best)) gnus-cite-attribution-alist)))
645 (if (memq best gnus-cite-loose-prefix-alist)
646 (let ((loop gnus-cite-prefix-alist)
649 (setq gnus-cite-loose-prefix-alist
650 (delq best gnus-cite-loose-prefix-alist))
652 (setq current (car loop)
654 (if (eq current best)
656 (setcdr current (gnus-set-difference (cdr current) numbers))
657 (if (null (cdr current))
658 (setq gnus-cite-loose-prefix-alist
659 (delq current gnus-cite-loose-prefix-alist)
660 atts (delq current atts)))))))))))
662 (defun gnus-cite-find-loose (prefix)
663 ;; Return a list of loose attribution lines prefixed by PREFIX.
664 (let* ((atts gnus-cite-loose-attribution-alist)
670 (if (string-equal (gnus-cite-find-prefix line) prefix)
671 (setq lines (cons line lines))))
674 (defun gnus-cite-add-face (number prefix face)
675 ;; At line NUMBER, ignore PREFIX and add FACE to the rest of the line.
677 (let ((inhibit-point-motion-hooks t)
680 (unless (eobp) ;; Sometimes things become confused.
681 (forward-char (length prefix))
682 (skip-chars-forward " \t")
685 (skip-chars-backward " \t")
688 (gnus-overlay-put (gnus-make-overlay from to) 'face face))))))
690 (defun gnus-cite-toggle (prefix)
692 (set-buffer gnus-article-buffer)
693 (let ((buffer-read-only nil)
694 (numbers (cdr (assoc prefix gnus-cite-prefix-alist)))
695 (inhibit-point-motion-hooks t)
698 (setq number (car numbers)
699 numbers (cdr numbers))
701 (cond ((get-text-property (point) 'invisible)
702 (remove-text-properties (point) (progn (forward-line 1) (point))
703 gnus-hidden-properties))
704 ((assq number gnus-cite-attribution-alist))
706 (gnus-add-text-properties
707 (point) (progn (forward-line 1) (point))
708 (nconc (list 'gnus-type 'cite)
709 gnus-hidden-properties))))))))
711 (defun gnus-cite-find-prefix (line)
712 ;; Return citation prefix for LINE.
713 (let ((alist gnus-cite-prefix-alist)
717 (setq entry (car alist)
719 (if (memq line (cdr entry))
720 (setq prefix (car entry))))
727 ;;; gnus-cite.el ends here