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.
32 (autoload 'gnus-article-add-button "gnus-vis"))
36 (defvar gnus-cite-parse-max-size 25000
37 "Maximum article size (in bytes) where parsing citations is allowed.
38 Set it to nil to parse all articles.")
40 (defvar gnus-cite-prefix-regexp
41 "^[]>|:}+ ]*[]>|:}+]\\(.*>\\)?\\|^.*>"
42 "Regexp matching the longest possible citation prefix on a line.")
44 (defvar gnus-cite-max-prefix 20
45 "Maximum possible length for a citation prefix.")
47 (defvar gnus-supercite-regexp
48 (concat "^\\(" gnus-cite-prefix-regexp "\\)? *"
49 ">>>>> +\"\\([^\"\n]+\\)\" +==")
50 "Regexp matching normal SuperCite attribution lines.
51 The first grouping must match prefixes added by other packages.")
53 (defvar gnus-supercite-secondary-regexp "^.*\"\\([^\"\n]+\\)\" +=="
54 "Regexp matching mangled SuperCite attribution lines.
55 The first regexp group should match the SuperCite attribution.")
57 (defvar gnus-cite-minimum-match-count 2
58 "Minimum number of identical prefixes before we believe it's a citation.")
61 ;(defvar gnus-cite-face-list
62 ; (if (eq gnus-display-type 'color)
63 ; (if (eq gnus-background-mode 'dark) 'light 'dark)
65 ; "Faces used for displaying different citations.
66 ;It is either a list of face names, or one of the following special
69 ;dark: Create faces from `gnus-face-dark-name-list'.
70 ;light: Create faces from `gnus-face-light-name-list'.
72 ;The variable `gnus-make-foreground' determines whether the created
73 ;faces change the foreground or the background colors.")
75 (defvar gnus-cite-attribution-prefix "in article\\|in <"
76 "Regexp matching the beginning of an attribution line.")
78 (defvar gnus-cite-attribution-suffix
79 "\\(wrote\\|writes\\|said\\|says\\):[ \t]*$"
80 "Regexp matching the end of an attribution line.
81 The text matching the first grouping will be used as a button.")
84 ;(defvar gnus-cite-attribution-face 'underline
85 ; "Face used for attribution lines.
86 ;It is merged with the face for the cited text belonging to the attribution.")
89 ;(defvar gnus-cite-hide-percentage 50
90 ; "Only hide cited text if it is larger than this percent of the body.")
93 ;(defvar gnus-cite-hide-absolute 10
94 ; "Only hide cited text if there is at least this number of cited lines.")
97 ;(defvar gnus-face-light-name-list
98 ; '("light blue" "light cyan" "light yellow" "light pink"
99 ; "pale green" "beige" "orange" "magenta" "violet" "medium purple"
101 ; "Names of light colors.")
104 ;(defvar gnus-face-dark-name-list
105 ; '("dark salmon" "firebrick"
106 ; "dark green" "dark orange" "dark khaki" "dark violet"
108 ; "Names of dark colors.")
110 ;;; Internal Variables:
112 (defvar gnus-article-length nil)
113 ;; Length of article last time we parsed it.
114 ;; BUG! KLUDGE! UGLY! FIX ME!
116 (defvar gnus-cite-prefix-alist nil)
117 ;; Alist of citation prefixes.
118 ;; The cdr is a list of lines with that prefix.
120 (defvar gnus-cite-attribution-alist nil)
121 ;; Alist of attribution lines.
122 ;; The car is a line number.
123 ;; The cdr is the prefix for the citation started by that line.
125 (defvar gnus-cite-loose-prefix-alist nil)
126 ;; Alist of citation prefixes that have no matching attribution.
127 ;; The cdr is a list of lines with that prefix.
129 (defvar gnus-cite-loose-attribution-alist nil)
130 ;; Alist of attribution lines that have no matching citation.
131 ;; Each member has the form (WROTE IN PREFIX TAG), where
132 ;; WROTE: is the attribution line number
133 ;; IN: is the line number of the previous line if part of the same attribution,
134 ;; PREFIX: Is the citation prefix of the attribution line(s), and
135 ;; TAG: Is a SuperCite tag, if any.
139 (defun gnus-article-highlight-citation (&optional force)
140 "Highlight cited text.
141 Each citation in the article will be highlighted with a different face.
142 The faces are taken from `gnus-cite-face-list'.
143 Attribution lines are highlighted with the same face as the
144 corresponding citation merged with `gnus-cite-attribution-face'.
146 Text is considered cited if at least `gnus-cite-minimum-match-count'
147 lines matches `gnus-cite-prefix-regexp' with the same prefix.
149 Lines matching `gnus-cite-attribution-suffix' and perhaps
150 `gnus-cite-attribution-prefix' are considered attribution lines."
151 (interactive (list 'force))
152 ;; Create dark or light faces if necessary.
153 (cond ((eq gnus-cite-face-list 'light)
154 (setq gnus-cite-face-list
155 (mapcar 'gnus-make-face gnus-face-light-name-list)))
156 ((eq gnus-cite-face-list 'dark)
157 (setq gnus-cite-face-list
158 (mapcar 'gnus-make-face gnus-face-dark-name-list))))
160 (set-buffer gnus-article-buffer)
161 (gnus-cite-parse-maybe force)
162 (let ((buffer-read-only nil)
163 (alist gnus-cite-prefix-alist)
164 (faces gnus-cite-face-list)
165 (inhibit-point-motion-hooks t)
166 face entry prefix skip numbers number face-alist)
167 ;; Loop through citation prefixes.
169 (setq entry (car alist)
174 faces (or (cdr faces) gnus-cite-face-list)
175 face-alist (cons (cons prefix face) face-alist))
177 (setq number (car numbers)
178 numbers (cdr numbers))
179 (and (not (assq number gnus-cite-attribution-alist))
180 (not (assq number gnus-cite-loose-attribution-alist))
181 (gnus-cite-add-face number prefix face))))
182 ;; Loop through attribution lines.
183 (setq alist gnus-cite-attribution-alist)
185 (setq entry (car alist)
189 skip (gnus-cite-find-prefix number)
190 face (cdr (assoc prefix face-alist)))
191 ;; Add attribution button.
193 (if (re-search-forward gnus-cite-attribution-suffix
194 (save-excursion (end-of-line 1) (point))
196 (gnus-article-add-button (match-beginning 1) (match-end 1)
197 'gnus-cite-toggle prefix))
198 ;; Highlight attribution line.
199 (gnus-cite-add-face number skip face)
200 (gnus-cite-add-face number skip gnus-cite-attribution-face))
201 ;; Loop through attribution lines.
202 (setq alist gnus-cite-loose-attribution-alist)
204 (setq entry (car alist)
207 skip (gnus-cite-find-prefix number))
208 (gnus-cite-add-face number skip gnus-cite-attribution-face)))))
210 (defun gnus-article-hide-citation (&optional force)
211 "Hide all cited text except attribution lines.
212 See the documentation for `gnus-article-highlight-citation'."
213 (interactive (list 'force))
215 (set-buffer gnus-article-buffer)
216 (gnus-cite-parse-maybe force)
217 (let ((buffer-read-only nil)
218 (alist gnus-cite-prefix-alist)
219 (inhibit-point-motion-hooks t)
222 (setq numbers (cdr (car alist))
225 (setq number (car numbers)
226 numbers (cdr numbers))
228 (or (assq number gnus-cite-attribution-alist)
229 (add-text-properties (point) (progn (forward-line 1) (point))
230 gnus-hidden-properties)))))))
232 (defun gnus-article-hide-citation-maybe (&optional force)
233 "Hide cited text that has an attribution line.
234 This will do nothing unless at least `gnus-cite-hide-percentage'
235 percent and at least `gnus-cite-hide-absolute' lines of the body is
236 cited text with attributions. When called interactively, these two
237 variables are ignored.
238 See also the documentation for `gnus-article-highlight-citation'."
239 (interactive (list 'force))
241 (set-buffer gnus-article-buffer)
242 (gnus-cite-parse-maybe force)
243 (goto-char (point-min))
244 (search-forward "\n\n" nil t)
245 (let ((start (point))
246 (atts gnus-cite-attribution-alist)
247 (buffer-read-only nil)
248 (inhibit-point-motion-hooks t)
251 (goto-char (point-max))
252 (re-search-backward gnus-signature-separator nil t)
253 (setq total (count-lines start (point)))
255 (setq hiden (+ hiden (length (cdr (assoc (cdr (car atts))
256 gnus-cite-prefix-alist))))
259 (and (> (* 100 hiden) (* gnus-cite-hide-percentage total))
260 (> hiden gnus-cite-hide-absolute)))
262 (setq atts gnus-cite-attribution-alist)
264 (setq total (cdr (assoc (cdr (car atts)) gnus-cite-prefix-alist))
267 (setq hiden (car total)
270 (or (assq hiden gnus-cite-attribution-alist)
271 (add-text-properties (point)
272 (progn (forward-line 1) (point))
273 gnus-hidden-properties)))))))))
275 ;;; Internal functions:
277 (defun gnus-cite-parse-maybe (&optional force)
278 ;; Parse if the buffer has changes since last time.
279 (if (eq gnus-article-length (- (point-max) (point-min)))
281 ;;Reset parser information.
282 (setq gnus-cite-prefix-alist nil
283 gnus-cite-attribution-alist nil
284 gnus-cite-loose-prefix-alist nil
285 gnus-cite-loose-attribution-alist nil)
286 ;; Parse if not too large.
288 gnus-cite-parse-max-size
289 (> (buffer-size) gnus-cite-parse-max-size))
291 (setq gnus-article-length (- (point-max) (point-min)))
294 (defun gnus-cite-parse ()
295 ;; Parse and connect citation prefixes and attribution lines.
297 ;; Parse current buffer searching for citation prefixes.
298 (goto-char (point-min))
299 (or (search-forward "\n\n" nil t)
300 (goto-char (point-max)))
301 (let ((line (1+ (count-lines (point-min) (point))))
304 (goto-char (point-max))
305 (re-search-backward gnus-signature-separator nil t)
307 alist entry start begin end numbers prefix)
308 ;; Get all potential prefixes in `alist'.
309 (while (< (point) max)
312 end (progn (beginning-of-line 2) (point))
315 ;; Ignore standard SuperCite attribution prefix.
316 (if (looking-at gnus-supercite-regexp)
318 (setq end (1+ (match-end 1)))
319 (setq end (1+ begin))))
320 ;; Ignore very long prefixes.
321 (if (> end (+ (point) gnus-cite-max-prefix))
322 (setq end (+ (point) gnus-cite-max-prefix)))
323 (while (re-search-forward gnus-cite-prefix-regexp (1- end) t)
325 (setq end (match-end 0)
326 prefix (buffer-substring begin end))
327 (set-text-properties 0 (length prefix) nil prefix)
328 (setq entry (assoc prefix alist))
330 (setcdr entry (cons line (cdr entry)))
331 (setq alist (cons (list prefix line) alist)))
334 (setq line (1+ line)))
335 ;; We got all the potential prefixes. Now create
336 ;; `gnus-cite-prefix-alist' containing the oldest prefix for each
337 ;; line that appears at least gnus-cite-minimum-match-count
338 ;; times. First sort them by length. Longer is older.
339 (setq alist (sort alist (lambda (a b)
340 (> (length (car a)) (length (car b))))))
342 (setq entry (car alist)
346 (cond ((null numbers)
347 ;; No lines with this prefix that wasn't also part of
350 ((< (length numbers) gnus-cite-minimum-match-count)
351 ;; Too few lines with this prefix. We keep it a bit
352 ;; longer in case it is an exact match for an attribution
353 ;; line, but we don't remove the line from other
355 (setq gnus-cite-prefix-alist
356 (cons entry gnus-cite-prefix-alist)))
358 (setq gnus-cite-prefix-alist (cons entry
359 gnus-cite-prefix-alist))
360 ;; Remove articles from other prefixes.
364 (setq current (car loop)
367 (gnus-set-difference (cdr current) numbers))))))))
368 ;; No citations have been connected to attribution lines yet.
369 (setq gnus-cite-loose-prefix-alist (append gnus-cite-prefix-alist nil))
371 ;; Parse current buffer searching for attribution lines.
372 (goto-char (point-min))
373 (search-forward "\n\n" nil t)
374 (while (re-search-forward gnus-cite-attribution-suffix (point-max) t)
375 (let* ((start (match-beginning 0))
377 (wrote (count-lines (point-min) end))
378 (prefix (gnus-cite-find-prefix wrote))
379 ;; Check previous line for an attribution leader.
381 (beginning-of-line 1)
382 (and (looking-at gnus-supercite-secondary-regexp)
383 (buffer-substring (match-beginning 1)
387 (and (re-search-backward gnus-cite-attribution-prefix
389 (beginning-of-line 0)
392 (not (re-search-forward gnus-cite-attribution-suffix
394 (count-lines (point-min) (1+ (point)))))))
398 (setq gnus-cite-loose-attribution-alist
399 (cons (list wrote in prefix tag)
400 gnus-cite-loose-attribution-alist))))
401 ;; Find exact supercite citations.
402 (gnus-cite-match-attributions 'small nil
406 (regexp-quote prefix) "[ \t]*"
407 (regexp-quote tag) ">"))))
408 ;; Find loose supercite citations after attributions.
409 (gnus-cite-match-attributions 'small t
411 (if tag (concat "\\<"
414 ;; Find loose supercite citations anywhere.
415 (gnus-cite-match-attributions 'small nil
417 (if tag (concat "\\<"
420 ;; Find nested citations after attributions.
421 (gnus-cite-match-attributions 'small-if-unique t
423 (concat "\\`" (regexp-quote prefix) ".+")))
424 ;; Find nested citations anywhere.
425 (gnus-cite-match-attributions 'small nil
427 (concat "\\`" (regexp-quote prefix) ".+")))
428 ;; Remove loose prefixes with too few lines.
429 (let ((alist gnus-cite-loose-prefix-alist)
432 (setq entry (car alist)
434 (if (< (length (cdr entry)) gnus-cite-minimum-match-count)
435 (setq gnus-cite-prefix-alist
436 (delq entry gnus-cite-prefix-alist)
437 gnus-cite-loose-prefix-alist
438 (delq entry gnus-cite-loose-prefix-alist)))))
439 ;; Find flat attributions.
440 (gnus-cite-match-attributions 'first t nil)
441 ;; Find any attributions (are we getting desperate yet?).
442 (gnus-cite-match-attributions 'first nil nil))
444 (defun gnus-cite-match-attributions (sort after fun)
445 ;; Match all loose attributions and citations (SORT AFTER FUN) .
447 ;; If SORT is `small', the citation with the shortest prefix will be
448 ;; used, if it is `first' the first prefix will be used, if it is
449 ;; `small-if-unique' the shortest prefix will be used if the
450 ;; attribution line does not share its own prefix with other
451 ;; loose attribution lines, otherwise the first prefix will be used.
453 ;; If AFTER is non-nil, only citations after the attribution line
454 ;; will be concidered.
456 ;; If FUN is non-nil, it will be called with the arguments (WROTE
457 ;; PREFIX TAG) and expected to return a regular expression. Only
458 ;; citations whose prefix matches the regular expression will be
461 ;; WROTE is the attribution line number.
462 ;; PREFIX is the attribution line prefix.
463 ;; TAG is the SuperCite tag on the attribution line.
464 (let ((atts gnus-cite-loose-attribution-alist)
466 att wrote in prefix tag regexp limit smallest best size)
474 regexp (if fun (funcall fun prefix tag) "")
475 size (cond ((eq sort 'small) t)
476 ((eq sort 'first) nil)
477 (t (< (length (gnus-cite-find-loose prefix)) 2)))
478 limit (if after wrote -1)
481 (let ((cites gnus-cite-loose-prefix-alist)
482 cite candidate numbers first compare)
484 (setq cite (car cites)
488 first (apply 'min numbers)
489 compare (if size (length candidate) first))
492 (string-match regexp candidate)
498 (setq gnus-cite-loose-attribution-alist
499 (delq att gnus-cite-loose-attribution-alist))
500 (setq gnus-cite-attribution-alist
501 (cons (cons wrote (car best)) gnus-cite-attribution-alist))
503 (setq gnus-cite-attribution-alist
504 (cons (cons in (car best)) gnus-cite-attribution-alist)))
505 (if (memq best gnus-cite-loose-prefix-alist)
506 (let ((loop gnus-cite-prefix-alist)
509 (setq gnus-cite-loose-prefix-alist
510 (delq best gnus-cite-loose-prefix-alist))
512 (setq current (car loop)
514 (if (eq current best)
516 (setcdr current (gnus-set-difference (cdr current) numbers))
517 (if (null (cdr current))
518 (setq gnus-cite-loose-prefix-alist
519 (delq current gnus-cite-loose-prefix-alist)
520 atts (delq current atts)))))))))))
522 (defun gnus-cite-find-loose (prefix)
523 ;; Return a list of loose attribution lines prefixed by PREFIX.
524 (let* ((atts gnus-cite-loose-attribution-alist)
530 (if (string-equal (gnus-cite-find-prefix line) prefix)
531 (setq lines (cons line lines))))
534 (defun gnus-cite-add-face (number prefix face)
535 ;; At line NUMBER, ignore PREFIX and add FACE to the rest of the line.
537 (let ((inhibit-point-motion-hooks t)
540 (forward-char (length prefix))
541 (skip-chars-forward " \t")
544 (skip-chars-backward " \t")
547 (gnus-overlay-put (gnus-make-overlay from to) 'face face)))))
549 (defun gnus-cite-toggle (prefix)
551 (set-buffer gnus-article-buffer)
552 (let ((buffer-read-only nil)
553 (numbers (cdr (assoc prefix gnus-cite-prefix-alist)))
554 (inhibit-point-motion-hooks t)
557 (setq number (car numbers)
558 numbers (cdr numbers))
560 (cond ((get-text-property (point) 'invisible)
561 (remove-text-properties (point) (progn (forward-line 1) (point))
562 gnus-hidden-properties))
563 ((assq number gnus-cite-attribution-alist))
565 (add-text-properties (point) (progn (forward-line 1) (point))
566 gnus-hidden-properties)))))))
568 (defun gnus-cite-find-prefix (line)
569 ;; Return citation prefix for LINE.
570 (let ((alist gnus-cite-prefix-alist)
574 (setq entry (car alist)
576 (if (memq line (cdr entry))
577 (setq prefix (car entry))))
584 ;;; gnus-cite.el ends here