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 "^[^\n]*[]>|:}+]"
37 "Regexp matching the longest possible citation prefix on a line.")
39 (defvar gnus-cite-max-prefix 20
40 "Maximal possible length for a citation prefix.")
42 (defvar gnus-supercite-regexp
43 (concat "^\\(" gnus-cite-prefix-regexp "\\)? *"
44 ">>>>> +\"\\([^\"\n]+\\)\" +==")
45 "Regexp matching normal SuperCite attribution lines.
46 The first regexp group should match a prefix added by another package.
47 The second regexp group should match the SuperCite attribution itself.")
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 "\\(wrote\\|writes\\|said\\):[ \t]*$"
71 "Regexp matching the end of an attribution line.
72 The text matching the first grouping will be used as a button.")
74 (defvar gnus-cite-attribution-face 'underline
75 "Face used for attribution lines.
76 It is merged with the face for the cited text belonging to the attribution.")
78 (defvar gnus-cite-hide-percentage 50
79 "Only hide cited text if it is larger than this percent of the body.")
81 (defvar gnus-cite-hide-absolute 10
82 "Only hide cited text if there is at least this number of cited lines.")
84 (defvar gnus-face-light-name-list
85 '("light blue" "light cyan" "light yellow" "light pink"
86 "pale green" "beige" "orange" "magenta" "violet" "medium purple"
88 "Names of light colors.")
90 (defvar gnus-face-dark-name-list
91 '("blue" "dark salmon" "firebrick"
92 "dark green" "dark orange" "dark khaki" "dark violet"
94 "Names of dark colors.")
96 ;;; Internal Variables:
98 (defvar gnus-article-length nil)
99 ;; Length of article last time we parsed it.
101 (defvar gnus-cite-prefix-alist nil)
102 ;; Alist of citation prefixes.
103 ;; The cdr is a list of lines with that prefix.
105 (defvar gnus-cite-attribution-alist nil)
106 ;; Alist of attribution lines.
107 ;; The car is a line number.
108 ;; The cdr is the prefix for the citation started by that line.
110 (defvar gnus-cite-loose-prefix-alist nil)
111 ;; Alist of citation prefixes that have no matching attribution.
112 ;; The cdr is a list of lines with that prefix.
114 (defvar gnus-cite-loose-attribution-alist nil)
115 ;; Alist of attribution lines that have no matching citation.
116 ;; Each member has the form (WROTE IN PREFIX TAG), where
117 ;; WROTE: is the attribution line number
118 ;; IN: is the line number of the previous line if part of the same attribution,
119 ;; PREFIX: Is the citation prefix of the attribution line(s), and
120 ;; TAG: Is a SuperCite tag, if any.
124 (defun gnus-article-highlight-citation ()
125 "Highlight cited text.
126 Each citation in the article will be highlighted with a different face.
127 The faces are taken from `gnus-cite-face-list'.
128 Attribution lines are highlighted with the sameface as the
129 corresponding citation merged with `gnus-cite-attribution-face'.
131 Text is concidered cited if at least `gnus-cite-minimum-match-count'
132 lines matches `gnus-cite-prefix-regexp' with the same prefix.
134 Lines matching `gnus-cite-attribution-postfix' and perhaps
135 `gnus-cite-attribution-prefix' are concidered attribution lines."
137 ;; Create dark or light faces if necessary.
138 (cond ((eq gnus-cite-face-list 'light)
139 (setq gnus-cite-face-list
140 (mapcar 'gnus-make-face gnus-face-light-name-list)))
141 ((eq gnus-cite-face-list 'dark)
142 (setq gnus-cite-face-list
143 (mapcar 'gnus-make-face gnus-face-dark-name-list))))
145 (set-buffer gnus-article-buffer)
146 (gnus-cite-parse-maybe)
147 (let ((buffer-read-only nil)
148 (alist gnus-cite-prefix-alist)
149 (faces gnus-cite-face-list)
150 (inhibit-point-motion-hooks t)
151 face entry prefix skip numbers number face-alist end)
152 ;; Loop through citation prefixes.
154 (setq entry (car alist)
159 faces (or (cdr faces) gnus-cite-face-list)
160 face-alist (cons (cons prefix face) face-alist))
162 (setq number (car numbers)
163 numbers (cdr numbers))
164 (and (not (assq number gnus-cite-attribution-alist))
165 (not (assq number gnus-cite-loose-attribution-alist))
166 (gnus-cite-add-face number prefix face))))
167 ;; Loop through attribution lines.
168 (setq alist gnus-cite-attribution-alist)
170 (setq entry (car alist)
174 skip (gnus-cite-find-prefix number)
175 face (cdr (assoc prefix face-alist)))
176 ;; Add attribution button.
178 (if (re-search-forward gnus-cite-attribution-postfix
179 (save-excursion (end-of-line 1) (point))
181 (gnus-article-add-button (match-beginning 1) (match-end 1)
182 'gnus-cite-toggle prefix))
183 ;; Highlight attribution line.
184 (gnus-cite-add-face number skip face)
185 (gnus-cite-add-face number skip gnus-cite-attribution-face))
186 ;; Loop through attribution lines.
187 (setq alist gnus-cite-loose-attribution-alist)
189 (setq entry (car alist)
192 skip (gnus-cite-find-prefix number))
193 (gnus-cite-add-face number skip gnus-cite-attribution-face)))))
195 (defun gnus-article-hide-citation ()
196 "Hide all cited text except attribution lines.
197 See the documentation for `gnus-article-highlight-citation'."
200 (set-buffer gnus-article-buffer)
201 (gnus-cite-parse-maybe)
202 (let ((buffer-read-only nil)
203 (alist gnus-cite-prefix-alist)
204 (inhibit-point-motion-hooks t)
207 (setq numbers (cdr (car alist))
210 (setq number (car numbers)
211 numbers (cdr numbers))
213 (or (assq number gnus-cite-attribution-alist)
214 (add-text-properties (point) (progn (forward-line 1) (point))
215 gnus-hidden-properties)))))))
217 (defun gnus-article-hide-citation-maybe (&optional force)
218 "Hide cited text that has an attribution line.
219 This will do nothing unless at least `gnus-cite-hide-percentage'
220 percent ans at least `gnus-cite-hide-absolute' lines of the body is
221 cited text with attributions. When called interactively, these two
222 variables are ignored.
223 See also the documentation for `gnus-article-highlight-citation'."
224 (interactive (list 'force))
226 (set-buffer gnus-article-buffer)
227 (gnus-cite-parse-maybe)
228 (goto-char (point-min))
229 (search-forward "\n\n")
230 (let ((start (point))
231 (atts gnus-cite-attribution-alist)
232 (buffer-read-only nil)
233 (inhibit-point-motion-hooks t)
236 (goto-char (point-max))
237 (re-search-backward gnus-signature-separator nil t)
238 (setq total (count-lines start (point)))
240 (setq hiden (+ hiden (length (cdr (assoc (cdr (car atts))
241 gnus-cite-prefix-alist))))
244 (and (> (* 100 hiden) (* gnus-cite-hide-percentage total))
245 (> hiden gnus-cite-hide-absolute)))
247 (setq atts gnus-cite-attribution-alist)
249 (setq total (cdr (assoc (cdr (car atts)) gnus-cite-prefix-alist))
252 (setq hiden (car total)
255 (or (assq hiden gnus-cite-attribution-alist)
256 (add-text-properties (point)
257 (progn (forward-line 1) (point))
258 gnus-hidden-properties)))))))))
260 ;;; Internal functions:
262 (defun gnus-cite-parse-maybe ()
263 ;; Parse if the buffer has changes since last time.
264 (if (eq gnus-article-length (- (point-max) (point-min)))
266 (setq gnus-article-length (- (point-max) (point-min)))
269 (defun gnus-cite-parse ()
270 ;; Parse and connect citation prefixes and attribution lines.
271 (setq gnus-cite-prefix-alist nil
272 gnus-cite-attribution-alist nil
273 gnus-cite-loose-prefix-alist nil
274 gnus-cite-loose-attribution-alist nil)
275 ;; Parse current buffer searching for citation prefixes.
276 (goto-char (point-min))
277 (search-forward "\n\n")
278 (let ((line (1+ (count-lines (point-min) (point))))
281 (goto-char (point-max))
282 (re-search-backward gnus-signature-separator nil t)
284 alist entry prefix start begin end numbers)
285 ;; Get all potential prefixes in `alist'.
286 (while (< (point) max)
289 end (progn (beginning-of-line 2) (point))
292 ;; Ignore standard SuperCite attribution prefix.
293 (if (looking-at gnus-supercite-regexp)
295 (setq end (1+ (match-end 1)))
296 (setq end (1+ begin))))
297 ;; Ignore very long prefixes.
298 (if (> end (+ (point) gnus-cite-max-prefix))
299 (setq end (+ (point) gnus-cite-max-prefix)))
300 (while (re-search-forward gnus-cite-prefix-regexp (1- end) t)
302 (setq end (match-end 0)
303 prefix (buffer-substring begin end))
304 (set-text-properties 0 (length prefix) nil prefix)
305 (setq entry (assoc prefix alist))
307 (setcdr entry (cons line (cdr entry)))
308 (setq alist (cons (list prefix line) alist)))
311 (setq line (1+ line)))
312 ;; We got all the potential prefixes. Now create
313 ;; `gnus-cite-prefix-alist' containing the oldest prefix for each
314 ;; line that appears at least gnus-cite-minimum-match-count
315 ;; times. First sort them by length. Longer is older.
316 (setq alist (sort alist (lambda (a b)
317 (> (length (car a)) (length (car b))))))
319 (setq entry (car alist)
323 (cond ((null numbers)
324 ;; No lines with this prefix that wasn't also part of
327 ((< (length numbers) gnus-cite-minimum-match-count)
328 ;; Too few lines with this prefix. We keep it a bit
329 ;; longer in case it is an exact match for an attribution
330 ;; line, but we don't remove the line from other
332 (setq gnus-cite-prefix-alist
333 (cons entry gnus-cite-prefix-alist)))
335 (setq gnus-cite-prefix-alist (cons entry gnus-cite-prefix-alist))
336 ;; Remove articles from other prefixes.
340 (setq current (car loop)
343 (gnus-set-difference (cdr current) numbers))))))))
344 ;; No citations have been connected to attribution lines yet.
345 (setq gnus-cite-loose-prefix-alist (append gnus-cite-prefix-alist nil))
347 ;; Parse current buffer searching for attribution lines.
348 (goto-char (point-min))
349 (search-forward "\n\n")
350 (while (re-search-forward gnus-cite-attribution-postfix (point-max) t)
351 (let* ((start (match-beginning 0))
353 (wrote (count-lines (point-min) end))
354 (prefix (gnus-cite-find-prefix wrote))
355 ;; Check previous line for an attribution leader.
357 (beginning-of-line 1)
358 (and (looking-at gnus-supercite-secondary-regexp)
359 (buffer-substring (match-beginning 1)
363 (and (re-search-backward gnus-cite-attribution-prefix
365 (beginning-of-line 0)
368 (not (re-search-forward gnus-cite-attribution-postfix
370 (count-lines (point-min) (1+ (point)))))))
374 (setq gnus-cite-loose-attribution-alist
375 (cons (list wrote in prefix tag)
376 gnus-cite-loose-attribution-alist))))
377 ;; Find exact supercite citations.
378 (gnus-cite-match-attributions 'small nil
381 (concat "\\`" (regexp-quote prefix) "[ \t]*"
382 (regexp-quote tag) ">"))))
383 ;; Find loose supercite citations after attributions.
384 (gnus-cite-match-attributions 'small t
386 (if tag (concat "\\<" (regexp-quote tag) "\\>"))))
387 ;; Find loose supercite citations anywhere.
388 (gnus-cite-match-attributions 'small nil
390 (if tag (concat "\\<" (regexp-quote tag) "\\>"))))
391 ;; Find nested citations after attributions.
392 (gnus-cite-match-attributions 'small-if-unique t
394 (concat "\\`" (regexp-quote prefix) ".+")))
395 ;; Find nested citations anywhere.
396 (gnus-cite-match-attributions 'small nil
398 (concat "\\`" (regexp-quote prefix) ".+")))
399 ;; Remove loose prefixes with too few lines.
400 (let ((alist gnus-cite-loose-prefix-alist)
403 (setq entry (car alist)
405 (if (< (length (cdr entry)) gnus-cite-minimum-match-count)
406 (setq gnus-cite-prefix-alist
407 (delq entry gnus-cite-prefix-alist)
408 gnus-cite-loose-prefix-alist
409 (delq entry gnus-cite-loose-prefix-alist)))))
410 ;; Find flat attributions.
411 (gnus-cite-match-attributions 'first t nil)
412 ;; Find any attributions (are we getting desperate yet?).
413 (gnus-cite-match-attributions 'first nil nil))
415 (defun gnus-cite-match-attributions (sort after fun)
416 ;; Match all loose attributions and citations (SORT AFTER FUN) .
418 ;; If SORT is `small', the citation with the shortest prefix will be
419 ;; used, if it is `first' the first prefix will be used, if it is
420 ;; `small-if-unique' the shortest prefix will be used if the
421 ;; attribution line does not share its own prefix with other
422 ;; loose attribution lines, otherwise the first prefix will be used.
424 ;; If AFTER is non-nil, only citations after the attribution line
425 ;; will be concidered.
427 ;; If FUN is non-nil, it will be called with the arguments (WROTE
428 ;; PREFIX TAG) and expected to return a regular expression. Only
429 ;; citations whose prefix matches the regular expression will be
432 ;; WROTE is the attribution line number.
433 ;; PREFIX is the attribution line prefix.
434 ;; TAG is the SuperCite tag on the attribution line.
435 (let ((atts gnus-cite-loose-attribution-alist)
437 att wrote in prefix tag regexp limit smallest best size aprefix)
445 regexp (if fun (funcall fun prefix tag) "")
446 size (cond ((eq sort 'small) t)
447 ((eq sort 'first) nil)
448 (t (< (length (gnus-cite-find-loose prefix)) 2)))
449 limit (if after wrote -1)
452 (let ((cites gnus-cite-loose-prefix-alist)
453 cite candidate numbers first compare)
455 (setq cite (car cites)
459 first (apply 'min numbers)
460 compare (if size (length candidate) first))
463 (string-match regexp candidate)
469 (setq gnus-cite-loose-attribution-alist
470 (delq att gnus-cite-loose-attribution-alist))
471 (setq gnus-cite-attribution-alist
472 (cons (cons wrote (car best)) gnus-cite-attribution-alist))
474 (setq gnus-cite-attribution-alist
475 (cons (cons in (car best)) gnus-cite-attribution-alist)))
476 (if (memq best gnus-cite-loose-prefix-alist)
477 (let ((loop gnus-cite-prefix-alist)
480 (setq gnus-cite-loose-prefix-alist
481 (delq best gnus-cite-loose-prefix-alist))
483 (setq current (car loop)
485 (if (eq current best)
487 (setcdr current (gnus-set-difference (cdr current) numbers))
488 (if (null (cdr current))
489 (setq gnus-cite-loose-prefix-alist
490 (delq current gnus-cite-loose-prefix-alist)
491 atts (delq current atts)))))))))))
493 (defun gnus-cite-find-loose (prefix)
494 ;; Return a list of loose attribution lines prefixed by PREFIX.
495 (let* ((atts gnus-cite-loose-attribution-alist)
496 att line lines candidate)
501 (if (string-equal (gnus-cite-find-prefix line) prefix)
502 (setq lines (cons line lines))))
505 (defun gnus-cite-add-face (number prefix face)
506 ;; At line NUMBER, ignore PREFIX and add FACE to the rest of the line.
508 (let ((inhibit-point-motion-hooks t)
511 (forward-char (length prefix))
512 (skip-chars-forward " \t")
515 (skip-chars-backward " \t")
518 (overlay-put (make-overlay from to) 'face face)))))
520 (defun gnus-cite-toggle (prefix)
522 (set-buffer gnus-article-buffer)
523 (let ((buffer-read-only nil)
524 (numbers (cdr (assoc prefix gnus-cite-prefix-alist)))
525 (inhibit-point-motion-hooks t)
528 (setq number (car numbers)
529 numbers (cdr numbers))
531 (cond ((get-text-property (point) 'invisible)
532 (remove-text-properties (point) (progn (forward-line 1) (point))
533 gnus-hidden-properties))
534 ((assq number gnus-cite-attribution-alist))
536 (add-text-properties (point) (progn (forward-line 1) (point))
537 gnus-hidden-properties)))))))
539 (defun gnus-cite-find-prefix (line)
540 ;; Return citation prefix for LINE.
541 (let ((alist gnus-cite-prefix-alist)
545 (setq entry (car alist)
547 (if (memq line (cdr entry))
548 (setq prefix (car entry))))
553 ;;; gnus-cite.el ends here