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 '(italic)
57 "Faces used for displaying different citations.
58 It is either a list of face names, or one of the following special
61 dark: Create faces from `gnus-face-dark-name-list'.
62 light: Create faces from `gnus-face-light-name-list'.
64 The variable `gnus-make-foreground' determines whether the created
65 faces change the foreground or the background colors.")
67 (defvar gnus-cite-attribution-prefix "in article\\|in <"
68 "Regexp matching the beginning of an attribution line.")
70 (defvar gnus-cite-attribution-postfix
71 "\\(wrote\\|writes\\|said\\|says\\):[ \t]*$"
72 "Regexp matching the end of an attribution line.
73 The text matching the first grouping will be used as a button.")
75 (defvar gnus-cite-attribution-face 'underline
76 "Face used for attribution lines.
77 It is merged with the face for the cited text belonging to the attribution.")
79 (defvar gnus-cite-hide-percentage 50
80 "Only hide cited text if it is larger than this percent of the body.")
82 (defvar gnus-cite-hide-absolute 10
83 "Only hide cited text if there is at least this number of cited lines.")
85 (defvar gnus-face-light-name-list
86 '("light blue" "light cyan" "light yellow" "light pink"
87 "pale green" "beige" "orange" "magenta" "violet" "medium purple"
89 "Names of light colors.")
91 (defvar gnus-face-dark-name-list
92 '("blue" "dark salmon" "firebrick"
93 "dark green" "dark orange" "dark khaki" "dark violet"
95 "Names of dark colors.")
97 ;;; Internal Variables:
99 (defvar gnus-article-length nil)
100 ;; Length of article last time we parsed it.
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.
125 (defun gnus-article-highlight-citation ()
126 "Highlight cited text.
127 Each citation in the article will be highlighted with a different face.
128 The faces are taken from `gnus-cite-face-list'.
129 Attribution lines are highlighted with the sameface as the
130 corresponding citation merged with `gnus-cite-attribution-face'.
132 Text is considered cited if at least `gnus-cite-minimum-match-count'
133 lines matches `gnus-cite-prefix-regexp' with the same prefix.
135 Lines matching `gnus-cite-attribution-postfix' and perhaps
136 `gnus-cite-attribution-prefix' are considered attribution lines."
138 ;; Create dark or light faces if necessary.
139 (cond ((eq gnus-cite-face-list 'light)
140 (setq gnus-cite-face-list
141 (mapcar 'gnus-make-face gnus-face-light-name-list)))
142 ((eq gnus-cite-face-list 'dark)
143 (setq gnus-cite-face-list
144 (mapcar 'gnus-make-face gnus-face-dark-name-list))))
146 (set-buffer gnus-article-buffer)
147 (gnus-cite-parse-maybe)
148 (let ((buffer-read-only nil)
149 (alist gnus-cite-prefix-alist)
150 (faces gnus-cite-face-list)
151 (inhibit-point-motion-hooks t)
152 face entry prefix skip numbers number face-alist end)
153 ;; Loop through citation prefixes.
155 (setq entry (car alist)
160 faces (or (cdr faces) gnus-cite-face-list)
161 face-alist (cons (cons prefix face) face-alist))
163 (setq number (car numbers)
164 numbers (cdr numbers))
165 (and (not (assq number gnus-cite-attribution-alist))
166 (not (assq number gnus-cite-loose-attribution-alist))
167 (gnus-cite-add-face number prefix face))))
168 ;; Loop through attribution lines.
169 (setq alist gnus-cite-attribution-alist)
171 (setq entry (car alist)
175 skip (gnus-cite-find-prefix number)
176 face (cdr (assoc prefix face-alist)))
177 ;; Add attribution button.
179 (if (re-search-forward gnus-cite-attribution-postfix
180 (save-excursion (end-of-line 1) (point))
182 (gnus-article-add-button (match-beginning 1) (match-end 1)
183 'gnus-cite-toggle prefix))
184 ;; Highlight attribution line.
185 (gnus-cite-add-face number skip face)
186 (gnus-cite-add-face number skip gnus-cite-attribution-face))
187 ;; Loop through attribution lines.
188 (setq alist gnus-cite-loose-attribution-alist)
190 (setq entry (car alist)
193 skip (gnus-cite-find-prefix number))
194 (gnus-cite-add-face number skip gnus-cite-attribution-face)))))
196 (defun gnus-article-hide-citation ()
197 "Hide all cited text except attribution lines.
198 See the documentation for `gnus-article-highlight-citation'."
201 (set-buffer gnus-article-buffer)
202 (gnus-cite-parse-maybe)
203 (let ((buffer-read-only nil)
204 (alist gnus-cite-prefix-alist)
205 (inhibit-point-motion-hooks t)
208 (setq numbers (cdr (car alist))
211 (setq number (car numbers)
212 numbers (cdr numbers))
214 (or (assq number gnus-cite-attribution-alist)
215 (add-text-properties (point) (progn (forward-line 1) (point))
216 gnus-hidden-properties)))))))
218 (defun gnus-article-hide-citation-maybe (&optional force)
219 "Hide cited text that has an attribution line.
220 This will do nothing unless at least `gnus-cite-hide-percentage'
221 percent ans at least `gnus-cite-hide-absolute' lines of the body is
222 cited text with attributions. When called interactively, these two
223 variables are ignored.
224 See also the documentation for `gnus-article-highlight-citation'."
225 (interactive (list 'force))
227 (set-buffer gnus-article-buffer)
228 (gnus-cite-parse-maybe)
229 (goto-char (point-min))
230 (search-forward "\n\n")
231 (let ((start (point))
232 (atts gnus-cite-attribution-alist)
233 (buffer-read-only nil)
234 (inhibit-point-motion-hooks t)
237 (goto-char (point-max))
238 (re-search-backward gnus-signature-separator nil t)
239 (setq total (count-lines start (point)))
241 (setq hiden (+ hiden (length (cdr (assoc (cdr (car atts))
242 gnus-cite-prefix-alist))))
245 (and (> (* 100 hiden) (* gnus-cite-hide-percentage total))
246 (> hiden gnus-cite-hide-absolute)))
248 (setq atts gnus-cite-attribution-alist)
250 (setq total (cdr (assoc (cdr (car atts)) gnus-cite-prefix-alist))
253 (setq hiden (car total)
256 (or (assq hiden gnus-cite-attribution-alist)
257 (add-text-properties (point)
258 (progn (forward-line 1) (point))
259 gnus-hidden-properties)))))))))
261 ;;; Internal functions:
263 (defun gnus-cite-parse-maybe ()
264 ;; Parse if the buffer has changes since last time.
265 (if (eq gnus-article-length (- (point-max) (point-min)))
267 (setq gnus-article-length (- (point-max) (point-min)))
270 (defun gnus-cite-parse ()
271 ;; Parse and connect citation prefixes and attribution lines.
272 (setq gnus-cite-prefix-alist nil
273 gnus-cite-attribution-alist nil
274 gnus-cite-loose-prefix-alist nil
275 gnus-cite-loose-attribution-alist nil)
276 ;; Parse current buffer searching for citation prefixes.
277 (goto-char (point-min))
278 (search-forward "\n\n")
279 (let ((line (1+ (count-lines (point-min) (point))))
282 (goto-char (point-max))
283 (re-search-backward gnus-signature-separator nil t)
285 alist entry prefix start begin end numbers)
286 ;; Get all potential prefixes in `alist'.
287 (while (< (point) max)
290 end (progn (beginning-of-line 2) (point))
293 ;; Ignore standard SuperCite attribution prefix.
294 (if (looking-at gnus-supercite-regexp)
296 (setq end (1+ (match-end 1)))
297 (setq end (1+ begin))))
298 ;; Ignore very long prefixes.
299 (if (> end (+ (point) gnus-cite-max-prefix))
300 (setq end (+ (point) gnus-cite-max-prefix)))
301 (while (re-search-forward gnus-cite-prefix-regexp (1- end) t)
303 (setq end (match-end 0)
304 prefix (buffer-substring begin end))
305 (set-text-properties 0 (length prefix) nil prefix)
306 (setq entry (assoc prefix alist))
308 (setcdr entry (cons line (cdr entry)))
309 (setq alist (cons (list prefix line) alist)))
312 (setq line (1+ line)))
313 ;; We got all the potential prefixes. Now create
314 ;; `gnus-cite-prefix-alist' containing the oldest prefix for each
315 ;; line that appears at least gnus-cite-minimum-match-count
316 ;; times. First sort them by length. Longer is older.
317 (setq alist (sort alist (lambda (a b)
318 (> (length (car a)) (length (car b))))))
320 (setq entry (car alist)
324 (cond ((null numbers)
325 ;; No lines with this prefix that wasn't also part of
328 ((< (length numbers) gnus-cite-minimum-match-count)
329 ;; Too few lines with this prefix. We keep it a bit
330 ;; longer in case it is an exact match for an attribution
331 ;; line, but we don't remove the line from other
333 (setq gnus-cite-prefix-alist
334 (cons entry gnus-cite-prefix-alist)))
336 (setq gnus-cite-prefix-alist (cons entry gnus-cite-prefix-alist))
337 ;; Remove articles from other prefixes.
341 (setq current (car loop)
344 (gnus-set-difference (cdr current) numbers))))))))
345 ;; No citations have been connected to attribution lines yet.
346 (setq gnus-cite-loose-prefix-alist (append gnus-cite-prefix-alist nil))
348 ;; Parse current buffer searching for attribution lines.
349 (goto-char (point-min))
350 (search-forward "\n\n")
351 (while (re-search-forward gnus-cite-attribution-postfix (point-max) t)
352 (let* ((start (match-beginning 0))
354 (wrote (count-lines (point-min) end))
355 (prefix (gnus-cite-find-prefix wrote))
356 ;; Check previous line for an attribution leader.
358 (beginning-of-line 1)
359 (and (looking-at gnus-supercite-secondary-regexp)
360 (buffer-substring (match-beginning 1)
364 (and (re-search-backward gnus-cite-attribution-prefix
366 (beginning-of-line 0)
369 (not (re-search-forward gnus-cite-attribution-postfix
371 (count-lines (point-min) (1+ (point)))))))
375 (setq gnus-cite-loose-attribution-alist
376 (cons (list wrote in prefix tag)
377 gnus-cite-loose-attribution-alist))))
378 ;; Find exact supercite citations.
379 (gnus-cite-match-attributions 'small nil
382 (concat "\\`" (regexp-quote prefix) "[ \t]*"
383 (regexp-quote tag) ">"))))
384 ;; Find loose supercite citations after attributions.
385 (gnus-cite-match-attributions 'small t
387 (if tag (concat "\\<" (regexp-quote tag) "\\>"))))
388 ;; Find loose supercite citations anywhere.
389 (gnus-cite-match-attributions 'small nil
391 (if tag (concat "\\<" (regexp-quote tag) "\\>"))))
392 ;; Find nested citations after attributions.
393 (gnus-cite-match-attributions 'small-if-unique t
395 (concat "\\`" (regexp-quote prefix) ".+")))
396 ;; Find nested citations anywhere.
397 (gnus-cite-match-attributions 'small nil
399 (concat "\\`" (regexp-quote prefix) ".+")))
400 ;; Remove loose prefixes with too few lines.
401 (let ((alist gnus-cite-loose-prefix-alist)
404 (setq entry (car alist)
406 (if (< (length (cdr entry)) gnus-cite-minimum-match-count)
407 (setq gnus-cite-prefix-alist
408 (delq entry gnus-cite-prefix-alist)
409 gnus-cite-loose-prefix-alist
410 (delq entry gnus-cite-loose-prefix-alist)))))
411 ;; Find flat attributions.
412 (gnus-cite-match-attributions 'first t nil)
413 ;; Find any attributions (are we getting desperate yet?).
414 (gnus-cite-match-attributions 'first nil nil))
416 (defun gnus-cite-match-attributions (sort after fun)
417 ;; Match all loose attributions and citations (SORT AFTER FUN) .
419 ;; If SORT is `small', the citation with the shortest prefix will be
420 ;; used, if it is `first' the first prefix will be used, if it is
421 ;; `small-if-unique' the shortest prefix will be used if the
422 ;; attribution line does not share its own prefix with other
423 ;; loose attribution lines, otherwise the first prefix will be used.
425 ;; If AFTER is non-nil, only citations after the attribution line
426 ;; will be concidered.
428 ;; If FUN is non-nil, it will be called with the arguments (WROTE
429 ;; PREFIX TAG) and expected to return a regular expression. Only
430 ;; citations whose prefix matches the regular expression will be
433 ;; WROTE is the attribution line number.
434 ;; PREFIX is the attribution line prefix.
435 ;; TAG is the SuperCite tag on the attribution line.
436 (let ((atts gnus-cite-loose-attribution-alist)
438 att wrote in prefix tag regexp limit smallest best size aprefix)
446 regexp (if fun (funcall fun prefix tag) "")
447 size (cond ((eq sort 'small) t)
448 ((eq sort 'first) nil)
449 (t (< (length (gnus-cite-find-loose prefix)) 2)))
450 limit (if after wrote -1)
453 (let ((cites gnus-cite-loose-prefix-alist)
454 cite candidate numbers first compare)
456 (setq cite (car cites)
460 first (apply 'min numbers)
461 compare (if size (length candidate) first))
464 (string-match regexp candidate)
470 (setq gnus-cite-loose-attribution-alist
471 (delq att gnus-cite-loose-attribution-alist))
472 (setq gnus-cite-attribution-alist
473 (cons (cons wrote (car best)) gnus-cite-attribution-alist))
475 (setq gnus-cite-attribution-alist
476 (cons (cons in (car best)) gnus-cite-attribution-alist)))
477 (if (memq best gnus-cite-loose-prefix-alist)
478 (let ((loop gnus-cite-prefix-alist)
481 (setq gnus-cite-loose-prefix-alist
482 (delq best gnus-cite-loose-prefix-alist))
484 (setq current (car loop)
486 (if (eq current best)
488 (setcdr current (gnus-set-difference (cdr current) numbers))
489 (if (null (cdr current))
490 (setq gnus-cite-loose-prefix-alist
491 (delq current gnus-cite-loose-prefix-alist)
492 atts (delq current atts)))))))))))
494 (defun gnus-cite-find-loose (prefix)
495 ;; Return a list of loose attribution lines prefixed by PREFIX.
496 (let* ((atts gnus-cite-loose-attribution-alist)
497 att line lines candidate)
502 (if (string-equal (gnus-cite-find-prefix line) prefix)
503 (setq lines (cons line lines))))
506 (defun gnus-cite-add-face (number prefix face)
507 ;; At line NUMBER, ignore PREFIX and add FACE to the rest of the line.
509 (let ((inhibit-point-motion-hooks t)
512 (forward-char (length prefix))
513 (skip-chars-forward " \t")
516 (skip-chars-backward " \t")
519 (overlay-put (make-overlay from to) 'face face)))))
521 (defun gnus-cite-toggle (prefix)
523 (set-buffer gnus-article-buffer)
524 (let ((buffer-read-only nil)
525 (numbers (cdr (assoc prefix gnus-cite-prefix-alist)))
526 (inhibit-point-motion-hooks t)
529 (setq number (car numbers)
530 numbers (cdr numbers))
532 (cond ((get-text-property (point) 'invisible)
533 (remove-text-properties (point) (progn (forward-line 1) (point))
534 gnus-hidden-properties))
535 ((assq number gnus-cite-attribution-alist))
537 (add-text-properties (point) (progn (forward-line 1) (point))
538 gnus-hidden-properties)))))))
540 (defun gnus-cite-find-prefix (line)
541 ;; Return citation prefix for LINE.
542 (let ((alist gnus-cite-prefix-alist)
546 (setq entry (car alist)
548 (if (memq line (cdr entry))
549 (setq prefix (car entry))))
554 ;;; gnus-cite.el ends here