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.
30 (autoload 'gnus-article-add-button "gnus-vis")
35 (defvar gnus-cite-prefix-regexp "^[^\n]*[]>|:}+]"
36 "Regexp matching the longest possible citation prefix on a line.")
38 (defvar gnus-cite-max-prefix 20
39 "Maximal possible length for a citation prefix.")
41 (defvar gnus-supercite-regexp
42 (concat "^\\(" gnus-cite-prefix-regexp "\\)? *"
43 ">>>>> +\"\\([^\"\n]+\\)\" +==")
44 "Regexp matching normal SuperCite attribution lines.
45 The first regexp group should match a prefix added by another package.
46 The second regexp group should match the SuperCite attribution itself.")
48 (defvar gnus-supercite-secondary-regexp "^.*\"\\([^\"\n]+\\)\" +=="
49 "Regexp matching mangled SuperCite attribution lines.
50 The first regexp group should match the SuperCite attribution.")
52 (defvar gnus-cite-minimum-match-count 2
53 "Minimal number of identical prefix'es before we believe it is a citation.")
55 (defvar gnus-cite-face-list '(italic)
56 "Faces used for displaying different citations.
57 It is either a list of face names, or one of the following special
60 dark: Create faces from `gnus-face-dark-name-list'.
61 light: Create faces from `gnus-face-light-name-list'.
63 The variable `gnus-make-foreground' determines whether the created
64 faces change the foreground or the background colors.")
66 (defvar gnus-cite-attribution-prefix "in article\\|in <"
67 "Regexp matching the beginning of an attribution line.")
69 (defvar gnus-cite-attribution-postfix "\\(wrote\\|writes\\|said\\):[ \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-hide-percentage 50
78 "Only hide cited text if it is larger than this percent of the body.")
80 (defvar gnus-cite-hide-absolute 10
81 "Only hide cited text if there is at least this number of cited lines.")
83 (defvar gnus-face-light-name-list
84 '("light blue" "light cyan" "light yellow" "light pink"
85 "pale green" "beige" "orange" "magenta" "violet" "medium purple"
87 "Names of light colors.")
89 (defvar gnus-face-dark-name-list
90 '("blue" "dark salmon" "firebrick"
91 "dark green" "dark orange" "dark khaki" "dark violet"
93 "Names of dark colors.")
95 ;;; Internal Variables:
97 (defvar gnus-article-length nil)
98 ;; Length of article last time we parsed it.
100 (defvar gnus-cite-prefix-alist nil)
101 ;; Alist of citation prefixes.
102 ;; The cdr is a list of lines with that prefix.
104 (defvar gnus-cite-attribution-alist nil)
105 ;; Alist of attribution lines.
106 ;; The car is a line number.
107 ;; The cdr is the prefix for the citation started by that line.
109 (defvar gnus-cite-loose-prefix-alist nil)
110 ;; Alist of citation prefixes that have no matching attribution.
111 ;; The cdr is a list of lines with that prefix.
113 (defvar gnus-cite-loose-attribution-alist nil)
114 ;; Alist of attribution lines that have no matching citation.
115 ;; Each member has the form (WROTE IN PREFIX TAG), where
116 ;; WROTE: is the attribution line number
117 ;; IN: is the line number of the previous line if part of the same attribution,
118 ;; PREFIX: Is the citation prefix of the attribution line(s), and
119 ;; TAG: Is a SuperCite tag, if any.
123 (defun gnus-article-highlight-citation ()
124 "Highlight cited text.
125 Each citation in the article will be highlighted with a different face.
126 The faces are taken from `gnus-cite-face-list'.
127 Attribution lines are highlighted with the sameface as the
128 corresponding citation merged with `gnus-cite-attribution-face'.
130 Text is concidered cited if at least `gnus-cite-minimum-match-count'
131 lines matches `gnus-cite-prefix-regexp' with the same prefix.
133 Lines matching `gnus-cite-attribution-postfix' and perhaps
134 `gnus-cite-attribution-prefix' are concidered attribution lines."
136 ;; Create dark or light faces if necessary.
137 (cond ((eq gnus-cite-face-list 'light)
138 (setq gnus-cite-face-list
139 (mapcar 'gnus-make-face gnus-face-light-name-list)))
140 ((eq gnus-cite-face-list 'dark)
141 (setq gnus-cite-face-list
142 (mapcar 'gnus-make-face gnus-face-dark-name-list))))
144 (set-buffer gnus-article-buffer)
145 (gnus-cite-parse-maybe)
146 (let ((buffer-read-only nil)
147 (alist gnus-cite-prefix-alist)
148 (faces gnus-cite-face-list)
149 (inhibit-point-motion-hooks t)
150 face entry prefix skip numbers number face-alist end)
151 ;; Loop through citation prefixes.
153 (setq entry (car alist)
158 faces (or (cdr faces) gnus-cite-face-list)
159 face-alist (cons (cons prefix face) face-alist))
161 (setq number (car numbers)
162 numbers (cdr numbers))
163 (and (not (assq number gnus-cite-attribution-alist))
164 (not (assq number gnus-cite-loose-attribution-alist))
165 (gnus-cite-add-face number prefix face))))
166 ;; Loop through attribution lines.
167 (setq alist gnus-cite-attribution-alist)
169 (setq entry (car alist)
173 skip (gnus-cite-find-prefix number)
174 face (cdr (assoc prefix face-alist)))
175 ;; Add attribution button.
177 (if (re-search-forward gnus-cite-attribution-postfix
178 (save-excursion (end-of-line 1) (point))
180 (gnus-article-add-button (match-beginning 1) (match-end 1)
181 'gnus-cite-toggle prefix))
182 ;; Highlight attribution line.
183 (gnus-cite-add-face number skip face)
184 (gnus-cite-add-face number skip gnus-cite-attribution-face))
185 ;; Loop through attribution lines.
186 (setq alist gnus-cite-loose-attribution-alist)
188 (setq entry (car alist)
191 skip (gnus-cite-find-prefix number))
192 (gnus-cite-add-face number skip gnus-cite-attribution-face)))))
194 (defun gnus-article-hide-citation ()
195 "Hide all cited text except attribution lines.
196 See the documentation for `gnus-article-highlight-citation'."
199 (set-buffer gnus-article-buffer)
200 (gnus-cite-parse-maybe)
201 (let ((buffer-read-only nil)
202 (alist gnus-cite-prefix-alist)
203 (inhibit-point-motion-hooks t)
206 (setq numbers (cdr (car alist))
209 (setq number (car numbers)
210 numbers (cdr numbers))
212 (or (assq number gnus-cite-attribution-alist)
213 (add-text-properties (point) (progn (forward-line 1) (point))
214 gnus-hidden-properties)))))))
216 (defun gnus-article-hide-citation-maybe (&optional force)
217 "Hide cited text that has an attribution line.
218 This will do nothing unless at least `gnus-cite-hide-percentage'
219 percent ans at least `gnus-cite-hide-absolute' lines of the body is
220 cited text with attributions. When called interactively, these two
221 variables are ignored.
222 See also the documentation for `gnus-article-highlight-citation'."
223 (interactive (list 'force))
225 (set-buffer gnus-article-buffer)
226 (gnus-cite-parse-maybe)
227 (goto-char (point-min))
228 (search-forward "\n\n")
229 (let ((start (point))
230 (atts gnus-cite-attribution-alist)
231 (buffer-read-only nil)
232 (inhibit-point-motion-hooks t)
235 (goto-char (point-max))
236 (re-search-backward gnus-signature-separator nil t)
237 (setq total (count-lines start (point)))
239 (setq hiden (+ hiden (length (cdr (assoc (cdr (car atts))
240 gnus-cite-prefix-alist))))
243 (and (> (* 100 hiden) (* gnus-cite-hide-percentage total))
244 (> hiden gnus-cite-hide-absolute)))
246 (setq atts gnus-cite-attribution-alist)
248 (setq total (cdr (assoc (cdr (car atts)) gnus-cite-prefix-alist))
251 (setq hiden (car total)
254 (or (assq hiden gnus-cite-attribution-alist)
255 (add-text-properties (point)
256 (progn (forward-line 1) (point))
257 gnus-hidden-properties)))))))))
259 ;;; Internal functions:
261 (defun gnus-cite-parse-maybe ()
262 ;; Parse if the buffer has changes since last time.
263 (if (eq gnus-article-length (- (point-max) (point-min)))
265 (setq gnus-article-length (- (point-max) (point-min)))
268 (defun gnus-cite-parse ()
269 ;; Parse and connect citation prefixes and attribution lines.
270 (setq gnus-cite-prefix-alist nil
271 gnus-cite-attribution-alist nil
272 gnus-cite-loose-prefix-alist nil
273 gnus-cite-loose-attribution-alist nil)
274 ;; Parse current buffer searching for citation prefixes.
275 (goto-char (point-min))
276 (search-forward "\n\n")
277 (let ((line (1+ (count-lines (point-min) (point))))
280 (goto-char (point-max))
281 (re-search-backward gnus-signature-separator nil t)
283 alist entry prefix start begin end numbers)
284 ;; Get all potential prefixes in `alist'.
285 (while (< (point) max)
288 end (progn (beginning-of-line 2) (point))
291 ;; Ignore standard SuperCite attribution prefix.
292 (if (looking-at gnus-supercite-regexp)
294 (setq end (1+ (match-end 1)))
295 (setq end (1+ begin))))
296 ;; Ignore very long prefixes.
297 (if (> end (+ (point) gnus-cite-max-prefix))
298 (setq end (+ (point) gnus-cite-max-prefix)))
299 (while (re-search-forward gnus-cite-prefix-regexp (1- end) t)
301 (setq end (match-end 0)
302 prefix (buffer-substring begin end))
303 (set-text-properties 0 (length prefix) nil prefix)
304 (setq entry (assoc prefix alist))
306 (setcdr entry (cons line (cdr entry)))
307 (setq alist (cons (list prefix line) alist)))
310 (setq line (1+ line)))
311 ;; We got all the potential prefixes. Now create
312 ;; `gnus-cite-prefix-alist' containing the oldest prefix for each
313 ;; line that appears at least gnus-cite-minimum-match-count
314 ;; times. First sort them by length. Longer is older.
315 (setq alist (sort alist (lambda (a b)
316 (> (length (car a)) (length (car b))))))
318 (setq entry (car alist)
322 (cond ((null numbers)
323 ;; No lines with this prefix that wasn't also part of
326 ((< (length numbers) gnus-cite-minimum-match-count)
327 ;; Too few lines with this prefix. We keep it a bit
328 ;; longer in case it is an exact match for an attribution
329 ;; line, but we don't remove the line from other
331 (setq gnus-cite-prefix-alist
332 (cons entry gnus-cite-prefix-alist)))
334 (setq gnus-cite-prefix-alist (cons entry gnus-cite-prefix-alist))
335 ;; Remove articles from other prefixes.
339 (setq current (car loop)
342 (gnus-set-difference (cdr current) numbers))))))))
343 ;; No citations have been connected to attribution lines yet.
344 (setq gnus-cite-loose-prefix-alist (append gnus-cite-prefix-alist nil))
346 ;; Parse current buffer searching for attribution lines.
347 (goto-char (point-min))
348 (search-forward "\n\n")
349 (while (re-search-forward gnus-cite-attribution-postfix (point-max) t)
350 (let* ((start (match-beginning 0))
352 (wrote (count-lines (point-min) end))
353 (prefix (gnus-cite-find-prefix wrote))
354 ;; Check previous line for an attribution leader.
356 (beginning-of-line 1)
357 (and (looking-at gnus-supercite-secondary-regexp)
358 (buffer-substring (match-beginning 1)
362 (and (re-search-backward gnus-cite-attribution-prefix
364 (beginning-of-line 0)
367 (not (re-search-forward gnus-cite-attribution-postfix
369 (count-lines (point-min) (1+ (point)))))))
373 (setq gnus-cite-loose-attribution-alist
374 (cons (list wrote in prefix tag)
375 gnus-cite-loose-attribution-alist))))
376 ;; Find exact supercite citations.
377 (gnus-cite-match-attributions 'small nil
380 (concat "\\`" (regexp-quote prefix) "[ \t]*"
381 (regexp-quote tag) ">"))))
382 ;; Find loose supercite citations after attributions.
383 (gnus-cite-match-attributions 'small t
385 (if tag (concat "\\<" (regexp-quote tag) "\\>"))))
386 ;; Find loose supercite citations anywhere.
387 (gnus-cite-match-attributions 'small nil
389 (if tag (concat "\\<" (regexp-quote tag) "\\>"))))
390 ;; Find nested citations after attributions.
391 (gnus-cite-match-attributions 'small-if-unique t
393 (concat "\\`" (regexp-quote prefix) ".+")))
394 ;; Find nested citations anywhere.
395 (gnus-cite-match-attributions 'small nil
397 (concat "\\`" (regexp-quote prefix) ".+")))
398 ;; Remove loose prefixes with too few lines.
399 (let ((alist gnus-cite-loose-prefix-alist)
402 (setq entry (car alist)
404 (if (< (length (cdr entry)) gnus-cite-minimum-match-count)
405 (setq gnus-cite-prefix-alist
406 (delq entry gnus-cite-prefix-alist)
407 gnus-cite-loose-prefix-alist
408 (delq entry gnus-cite-loose-prefix-alist)))))
409 ;; Find flat attributions.
410 (gnus-cite-match-attributions 'first t nil)
411 ;; Find any attributions (are we getting desperate yet?).
412 (gnus-cite-match-attributions 'first nil nil))
414 (defun gnus-cite-match-attributions (sort after fun)
415 ;; Match all loose attributions and citations (SORT AFTER FUN) .
417 ;; If SORT is `small', the citation with the shortest prefix will be
418 ;; used, if it is `first' the first prefix will be used, if it is
419 ;; `small-if-unique' the shortest prefix will be used if the
420 ;; attribution line does not share its own prefix with other
421 ;; loose attribution lines, otherwise the first prefix will be used.
423 ;; If AFTER is non-nil, only citations after the attribution line
424 ;; will be concidered.
426 ;; If FUN is non-nil, it will be called with the arguments (WROTE
427 ;; PREFIX TAG) and expected to return a regular expression. Only
428 ;; citations whose prefix matches the regular expression will be
431 ;; WROTE is the attribution line number.
432 ;; PREFIX is the attribution line prefix.
433 ;; TAG is the SuperCite tag on the attribution line.
434 (let ((atts gnus-cite-loose-attribution-alist)
436 att wrote in prefix tag regexp limit smallest best size aprefix)
444 regexp (if fun (funcall fun prefix tag) "")
445 size (cond ((eq sort 'small) t)
446 ((eq sort 'first) nil)
447 (t (< (length (gnus-cite-find-loose prefix)) 2)))
448 limit (if after wrote -1)
451 (let ((cites gnus-cite-loose-prefix-alist)
452 cite candidate numbers first compare)
454 (setq cite (car cites)
458 first (apply 'min numbers)
459 compare (if size (length candidate) first))
462 (string-match regexp candidate)
468 (setq gnus-cite-loose-attribution-alist
469 (delq att gnus-cite-loose-attribution-alist))
470 (setq gnus-cite-attribution-alist
471 (cons (cons wrote (car best)) gnus-cite-attribution-alist))
473 (setq gnus-cite-attribution-alist
474 (cons (cons in (car best)) gnus-cite-attribution-alist)))
475 (if (memq best gnus-cite-loose-prefix-alist)
476 (let ((loop gnus-cite-prefix-alist)
479 (setq gnus-cite-loose-prefix-alist
480 (delq best gnus-cite-loose-prefix-alist))
482 (setq current (car loop)
484 (if (eq current best)
486 (setcdr current (gnus-set-difference (cdr current) numbers))
487 (if (null (cdr current))
488 (setq gnus-cite-loose-prefix-alist
489 (delq current gnus-cite-loose-prefix-alist)
490 atts (delq current atts)))))))))))
492 (defun gnus-cite-find-loose (prefix)
493 ;; Return a list of loose attribution lines prefixed by PREFIX.
494 (let* ((atts gnus-cite-loose-attribution-alist)
495 att line lines candidate)
500 (if (string-equal (gnus-cite-find-prefix line) prefix)
501 (setq lines (cons line lines))))
504 (defun gnus-cite-add-face (number prefix face)
505 ;; At line NUMBER, ignore PREFIX and add FACE to the rest of the line.
507 (let ((inhibit-point-motion-hooks t)
510 (forward-char (length prefix))
511 (skip-chars-forward " \t")
514 (skip-chars-backward " \t")
517 (overlay-put (make-overlay from to) 'face face)))))
519 (defun gnus-cite-toggle (prefix)
521 (set-buffer gnus-article-buffer)
522 (let ((buffer-read-only nil)
523 (numbers (cdr (assoc prefix gnus-cite-prefix-alist)))
524 (inhibit-point-motion-hooks t)
527 (setq number (car numbers)
528 numbers (cdr numbers))
530 (cond ((get-text-property (point) 'invisible)
531 (remove-text-properties (point) (progn (forward-line 1) (point))
532 gnus-hidden-properties))
533 ((assq number gnus-cite-attribution-alist))
535 (add-text-properties (point) (progn (forward-line 1) (point))
536 gnus-hidden-properties)))))))
538 (defun gnus-cite-find-prefix (line)
539 ;; Return citation prefix for LINE.
540 (let ((alist gnus-cite-prefix-alist)
544 (setq entry (car alist)
546 (if (memq line (cdr entry))
547 (setq prefix (car entry))))
552 ;;; gnus-cite.el ends here