1 ;;; gnus-cite.el --- parse citations in articles for Gnus
2 ;; Copyright (C) 1995 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
21 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
31 (autoload 'gnus-article-add-button "gnus-vis")
36 (defvar gnus-cite-prefix-regexp
37 "^[]>|:}+ ]*[]>|:}+]\\(.*>\\)?\\|^.*>"
38 "Regexp matching the longest possible citation prefix on a line.")
40 (defvar gnus-cite-max-prefix 20
41 "Maximal possible length for a citation prefix.")
43 (defvar gnus-supercite-regexp
44 (concat "^\\(" gnus-cite-prefix-regexp "\\)? *"
45 ">>>>> +\"\\([^\"\n]+\\)\" +==")
46 "Regexp matching normal SuperCite attribution lines.
47 The first regexp group should match a prefix added by another package.")
49 (defvar gnus-supercite-secondary-regexp "^.*\"\\([^\"\n]+\\)\" +=="
50 "Regexp matching mangled SuperCite attribution lines.
51 The first regexp group should match the SuperCite attribution.")
53 (defvar gnus-cite-minimum-match-count 2
54 "Minimal number of identical prefix'es before we believe it is a citation.")
56 (defvar gnus-cite-face-list
57 (if (eq gnus-display-type 'color)
58 (if (eq gnus-background-mode 'dark) 'light 'dark)
60 "Faces used for displaying different citations.
61 It is either a list of face names, or one of the following special
64 dark: Create faces from `gnus-face-dark-name-list'.
65 light: Create faces from `gnus-face-light-name-list'.
67 The variable `gnus-make-foreground' determines whether the created
68 faces change the foreground or the background colors.")
70 (defvar gnus-cite-attribution-prefix "in article\\|in <"
71 "Regexp matching the beginning of an attribution line.")
73 (defvar gnus-cite-attribution-postfix
74 "\\(wrote\\|writes\\|said\\|says\\):[ \t]*$"
75 "Regexp matching the end of an attribution line.
76 The text matching the first grouping will be used as a button.")
78 (defvar gnus-cite-attribution-face 'underline
79 "Face used for attribution lines.
80 It is merged with the face for the cited text belonging to the attribution.")
82 (defvar gnus-cite-hide-percentage 50
83 "Only hide cited text if it is larger than this percent of the body.")
85 (defvar gnus-cite-hide-absolute 10
86 "Only hide cited text if there is at least this number of cited lines.")
88 (defvar gnus-face-light-name-list
89 '("light blue" "light cyan" "light yellow" "light pink"
90 "pale green" "beige" "orange" "magenta" "violet" "medium purple"
92 "Names of light colors.")
94 (defvar gnus-face-dark-name-list
95 '("dark salmon" "firebrick"
96 "dark green" "dark orange" "dark khaki" "dark violet"
98 "Names of dark colors.")
100 ;;; Internal Variables:
102 (defvar gnus-article-length nil)
103 ;; Length of article last time we parsed it.
105 (defvar gnus-cite-prefix-alist nil)
106 ;; Alist of citation prefixes.
107 ;; The cdr is a list of lines with that prefix.
109 (defvar gnus-cite-attribution-alist nil)
110 ;; Alist of attribution lines.
111 ;; The car is a line number.
112 ;; The cdr is the prefix for the citation started by that line.
114 (defvar gnus-cite-loose-prefix-alist nil)
115 ;; Alist of citation prefixes that have no matching attribution.
116 ;; The cdr is a list of lines with that prefix.
118 (defvar gnus-cite-loose-attribution-alist nil)
119 ;; Alist of attribution lines that have no matching citation.
120 ;; Each member has the form (WROTE IN PREFIX TAG), where
121 ;; WROTE: is the attribution line number
122 ;; IN: is the line number of the previous line if part of the same attribution,
123 ;; PREFIX: Is the citation prefix of the attribution line(s), and
124 ;; TAG: Is a SuperCite tag, if any.
128 (defun gnus-article-highlight-citation ()
129 "Highlight cited text.
130 Each citation in the article will be highlighted with a different face.
131 The faces are taken from `gnus-cite-face-list'.
132 Attribution lines are highlighted with the sameface as the
133 corresponding citation merged with `gnus-cite-attribution-face'.
135 Text is considered cited if at least `gnus-cite-minimum-match-count'
136 lines matches `gnus-cite-prefix-regexp' with the same prefix.
138 Lines matching `gnus-cite-attribution-postfix' and perhaps
139 `gnus-cite-attribution-prefix' are considered attribution lines."
141 ;; Create dark or light faces if necessary.
142 (cond ((eq gnus-cite-face-list 'light)
143 (setq gnus-cite-face-list
144 (mapcar 'gnus-make-face gnus-face-light-name-list)))
145 ((eq gnus-cite-face-list 'dark)
146 (setq gnus-cite-face-list
147 (mapcar 'gnus-make-face gnus-face-dark-name-list))))
149 (set-buffer gnus-article-buffer)
150 (gnus-cite-parse-maybe)
151 (let ((buffer-read-only nil)
152 (alist gnus-cite-prefix-alist)
153 (faces gnus-cite-face-list)
154 (inhibit-point-motion-hooks t)
155 face entry prefix skip numbers number face-alist)
156 ;; Loop through citation prefixes.
158 (setq entry (car alist)
163 faces (or (cdr faces) gnus-cite-face-list)
164 face-alist (cons (cons prefix face) face-alist))
166 (setq number (car numbers)
167 numbers (cdr numbers))
168 (and (not (assq number gnus-cite-attribution-alist))
169 (not (assq number gnus-cite-loose-attribution-alist))
170 (gnus-cite-add-face number prefix face))))
171 ;; Loop through attribution lines.
172 (setq alist gnus-cite-attribution-alist)
174 (setq entry (car alist)
178 skip (gnus-cite-find-prefix number)
179 face (cdr (assoc prefix face-alist)))
180 ;; Add attribution button.
182 (if (re-search-forward gnus-cite-attribution-postfix
183 (save-excursion (end-of-line 1) (point))
185 (gnus-article-add-button (match-beginning 1) (match-end 1)
186 'gnus-cite-toggle prefix))
187 ;; Highlight attribution line.
188 (gnus-cite-add-face number skip face)
189 (gnus-cite-add-face number skip gnus-cite-attribution-face))
190 ;; Loop through attribution lines.
191 (setq alist gnus-cite-loose-attribution-alist)
193 (setq entry (car alist)
196 skip (gnus-cite-find-prefix number))
197 (gnus-cite-add-face number skip gnus-cite-attribution-face)))))
199 (defun gnus-article-hide-citation ()
200 "Hide all cited text except attribution lines.
201 See the documentation for `gnus-article-highlight-citation'."
204 (set-buffer gnus-article-buffer)
205 (gnus-cite-parse-maybe)
206 (let ((buffer-read-only nil)
207 (alist gnus-cite-prefix-alist)
208 (inhibit-point-motion-hooks t)
211 (setq numbers (cdr (car alist))
214 (setq number (car numbers)
215 numbers (cdr numbers))
217 (or (assq number gnus-cite-attribution-alist)
218 (add-text-properties (point) (progn (forward-line 1) (point))
219 gnus-hidden-properties)))))))
221 (defun gnus-article-hide-citation-maybe (&optional force)
222 "Hide cited text that has an attribution line.
223 This will do nothing unless at least `gnus-cite-hide-percentage'
224 percent ans at least `gnus-cite-hide-absolute' lines of the body is
225 cited text with attributions. When called interactively, these two
226 variables are ignored.
227 See also the documentation for `gnus-article-highlight-citation'."
228 (interactive (list 'force))
230 (set-buffer gnus-article-buffer)
231 (gnus-cite-parse-maybe)
232 (goto-char (point-min))
233 (search-forward "\n\n")
234 (let ((start (point))
235 (atts gnus-cite-attribution-alist)
236 (buffer-read-only nil)
237 (inhibit-point-motion-hooks t)
240 (goto-char (point-max))
241 (re-search-backward gnus-signature-separator nil t)
242 (setq total (count-lines start (point)))
244 (setq hiden (+ hiden (length (cdr (assoc (cdr (car atts))
245 gnus-cite-prefix-alist))))
248 (and (> (* 100 hiden) (* gnus-cite-hide-percentage total))
249 (> hiden gnus-cite-hide-absolute)))
251 (setq atts gnus-cite-attribution-alist)
253 (setq total (cdr (assoc (cdr (car atts)) gnus-cite-prefix-alist))
256 (setq hiden (car total)
259 (or (assq hiden gnus-cite-attribution-alist)
260 (add-text-properties (point)
261 (progn (forward-line 1) (point))
262 gnus-hidden-properties)))))))))
264 ;;; Internal functions:
266 (defun gnus-cite-parse-maybe ()
267 ;; Parse if the buffer has changes since last time.
268 (if (eq gnus-article-length (- (point-max) (point-min)))
270 (setq gnus-article-length (- (point-max) (point-min)))
273 (defun gnus-cite-parse ()
274 ;; Parse and connect citation prefixes and attribution lines.
275 (setq gnus-cite-prefix-alist nil
276 gnus-cite-attribution-alist nil
277 gnus-cite-loose-prefix-alist nil
278 gnus-cite-loose-attribution-alist nil)
279 ;; Parse current buffer searching for citation prefixes.
280 (goto-char (point-min))
281 (search-forward "\n\n")
282 (let ((line (1+ (count-lines (point-min) (point))))
285 (goto-char (point-max))
286 (re-search-backward gnus-signature-separator nil t)
288 alist entry start begin end numbers prefix)
289 ;; Get all potential prefixes in `alist'.
290 (while (< (point) max)
293 end (progn (beginning-of-line 2) (point))
296 ;; Ignore standard SuperCite attribution prefix.
297 (if (looking-at gnus-supercite-regexp)
299 (setq end (1+ (match-end 1)))
300 (setq end (1+ begin))))
301 ;; Ignore very long prefixes.
302 (if (> end (+ (point) gnus-cite-max-prefix))
303 (setq end (+ (point) gnus-cite-max-prefix)))
304 (while (re-search-forward gnus-cite-prefix-regexp (1- end) t)
306 (setq end (match-end 0)
307 prefix (buffer-substring begin end))
308 (set-text-properties 0 (length prefix) nil prefix)
309 (setq entry (assoc prefix alist))
311 (setcdr entry (cons line (cdr entry)))
312 (setq alist (cons (list prefix line) alist)))
315 (setq line (1+ line)))
316 ;; We got all the potential prefixes. Now create
317 ;; `gnus-cite-prefix-alist' containing the oldest prefix for each
318 ;; line that appears at least gnus-cite-minimum-match-count
319 ;; times. First sort them by length. Longer is older.
320 (setq alist (sort alist (lambda (a b)
321 (> (length (car a)) (length (car b))))))
323 (setq entry (car alist)
327 (cond ((null numbers)
328 ;; No lines with this prefix that wasn't also part of
331 ((< (length numbers) gnus-cite-minimum-match-count)
332 ;; Too few lines with this prefix. We keep it a bit
333 ;; longer in case it is an exact match for an attribution
334 ;; line, but we don't remove the line from other
336 (setq gnus-cite-prefix-alist
337 (cons entry gnus-cite-prefix-alist)))
339 (setq gnus-cite-prefix-alist (cons entry gnus-cite-prefix-alist))
340 ;; Remove articles from other prefixes.
344 (setq current (car loop)
347 (gnus-set-difference (cdr current) numbers))))))))
348 ;; No citations have been connected to attribution lines yet.
349 (setq gnus-cite-loose-prefix-alist (append gnus-cite-prefix-alist nil))
351 ;; Parse current buffer searching for attribution lines.
352 (goto-char (point-min))
353 (search-forward "\n\n")
354 (while (re-search-forward gnus-cite-attribution-postfix (point-max) t)
355 (let* ((start (match-beginning 0))
357 (wrote (count-lines (point-min) end))
358 (prefix (gnus-cite-find-prefix wrote))
359 ;; Check previous line for an attribution leader.
361 (beginning-of-line 1)
362 (and (looking-at gnus-supercite-secondary-regexp)
363 (buffer-substring (match-beginning 1)
367 (and (re-search-backward gnus-cite-attribution-prefix
369 (beginning-of-line 0)
372 (not (re-search-forward gnus-cite-attribution-postfix
374 (count-lines (point-min) (1+ (point)))))))
378 (setq gnus-cite-loose-attribution-alist
379 (cons (list wrote in prefix tag)
380 gnus-cite-loose-attribution-alist))))
381 ;; Find exact supercite citations.
382 (gnus-cite-match-attributions 'small nil
385 (concat "\\`" (regexp-quote prefix) "[ \t]*"
386 (regexp-quote tag) ">"))))
387 ;; Find loose supercite citations after attributions.
388 (gnus-cite-match-attributions 'small t
390 (if tag (concat "\\<" (regexp-quote tag) "\\>"))))
391 ;; Find loose supercite citations anywhere.
392 (gnus-cite-match-attributions 'small nil
394 (if tag (concat "\\<" (regexp-quote tag) "\\>"))))
395 ;; Find nested citations after attributions.
396 (gnus-cite-match-attributions 'small-if-unique t
398 (concat "\\`" (regexp-quote prefix) ".+")))
399 ;; Find nested citations anywhere.
400 (gnus-cite-match-attributions 'small nil
402 (concat "\\`" (regexp-quote prefix) ".+")))
403 ;; Remove loose prefixes with too few lines.
404 (let ((alist gnus-cite-loose-prefix-alist)
407 (setq entry (car alist)
409 (if (< (length (cdr entry)) gnus-cite-minimum-match-count)
410 (setq gnus-cite-prefix-alist
411 (delq entry gnus-cite-prefix-alist)
412 gnus-cite-loose-prefix-alist
413 (delq entry gnus-cite-loose-prefix-alist)))))
414 ;; Find flat attributions.
415 (gnus-cite-match-attributions 'first t nil)
416 ;; Find any attributions (are we getting desperate yet?).
417 (gnus-cite-match-attributions 'first nil nil))
419 (defun gnus-cite-match-attributions (sort after fun)
420 ;; Match all loose attributions and citations (SORT AFTER FUN) .
422 ;; If SORT is `small', the citation with the shortest prefix will be
423 ;; used, if it is `first' the first prefix will be used, if it is
424 ;; `small-if-unique' the shortest prefix will be used if the
425 ;; attribution line does not share its own prefix with other
426 ;; loose attribution lines, otherwise the first prefix will be used.
428 ;; If AFTER is non-nil, only citations after the attribution line
429 ;; will be concidered.
431 ;; If FUN is non-nil, it will be called with the arguments (WROTE
432 ;; PREFIX TAG) and expected to return a regular expression. Only
433 ;; citations whose prefix matches the regular expression will be
436 ;; WROTE is the attribution line number.
437 ;; PREFIX is the attribution line prefix.
438 ;; TAG is the SuperCite tag on the attribution line.
439 (let ((atts gnus-cite-loose-attribution-alist)
441 att wrote in prefix tag regexp limit smallest best size)
449 regexp (if fun (funcall fun prefix tag) "")
450 size (cond ((eq sort 'small) t)
451 ((eq sort 'first) nil)
452 (t (< (length (gnus-cite-find-loose prefix)) 2)))
453 limit (if after wrote -1)
456 (let ((cites gnus-cite-loose-prefix-alist)
457 cite candidate numbers first compare)
459 (setq cite (car cites)
463 first (apply 'min numbers)
464 compare (if size (length candidate) first))
467 (string-match regexp candidate)
473 (setq gnus-cite-loose-attribution-alist
474 (delq att gnus-cite-loose-attribution-alist))
475 (setq gnus-cite-attribution-alist
476 (cons (cons wrote (car best)) gnus-cite-attribution-alist))
478 (setq gnus-cite-attribution-alist
479 (cons (cons in (car best)) gnus-cite-attribution-alist)))
480 (if (memq best gnus-cite-loose-prefix-alist)
481 (let ((loop gnus-cite-prefix-alist)
484 (setq gnus-cite-loose-prefix-alist
485 (delq best gnus-cite-loose-prefix-alist))
487 (setq current (car loop)
489 (if (eq current best)
491 (setcdr current (gnus-set-difference (cdr current) numbers))
492 (if (null (cdr current))
493 (setq gnus-cite-loose-prefix-alist
494 (delq current gnus-cite-loose-prefix-alist)
495 atts (delq current atts)))))))))))
497 (defun gnus-cite-find-loose (prefix)
498 ;; Return a list of loose attribution lines prefixed by PREFIX.
499 (let* ((atts gnus-cite-loose-attribution-alist)
505 (if (string-equal (gnus-cite-find-prefix line) prefix)
506 (setq lines (cons line lines))))
509 (defun gnus-cite-add-face (number prefix face)
510 ;; At line NUMBER, ignore PREFIX and add FACE to the rest of the line.
512 (let ((inhibit-point-motion-hooks t)
515 (forward-char (length prefix))
516 (skip-chars-forward " \t")
519 (skip-chars-backward " \t")
522 (overlay-put (make-overlay from to) 'face face)))))
524 (defun gnus-cite-toggle (prefix)
526 (set-buffer gnus-article-buffer)
527 (let ((buffer-read-only nil)
528 (numbers (cdr (assoc prefix gnus-cite-prefix-alist)))
529 (inhibit-point-motion-hooks t)
532 (setq number (car numbers)
533 numbers (cdr numbers))
535 (cond ((get-text-property (point) 'invisible)
536 (remove-text-properties (point) (progn (forward-line 1) (point))
537 gnus-hidden-properties))
538 ((assq number gnus-cite-attribution-alist))
540 (add-text-properties (point) (progn (forward-line 1) (point))
541 gnus-hidden-properties)))))))
543 (defun gnus-cite-find-prefix (line)
544 ;; Return citation prefix for LINE.
545 (let ((alist gnus-cite-prefix-alist)
549 (setq entry (car alist)
551 (if (memq line (cdr entry))
552 (setq prefix (car entry))))
557 ;;; gnus-cite.el ends here