1 ;;; gnus-cite.el --- Highlight GNUS article.
2 ;; Copyright (C) 1995 Free Software Foundation, Inc.
4 ;; Author: Per Abrahamsen <abraham@iesd.auc.dk>
5 ;; Keywords: news, mail
16 (defvar gnus-cite-prefix-regexp "^[^\n]*[]>|:}+]"
17 "Regexp matching the longest possible citation prefix on a line.")
19 (defvar gnus-cite-max-prefix 20
20 "Maximal possible length for a citation prefix.")
22 (defvar gnus-supercite-regexp
23 (concat "^\\(" gnus-cite-prefix-regexp "\\)? *"
24 ">>>>> +\"\\([^\"\n]+\\)\" +==")
25 "Regexp matching normal SuperCite attribution lines.
26 The first regexp group should match a prefix added by another package.
27 The second regexp group should match the SuperCite attribution itself.")
29 (defvar gnus-supercite-secondary-regexp "^.*\"\\([^\"\n]+\\)\" +=="
30 "Regexp matching mangled SuperCite attribution lines.
31 The first regexp group should match the SuperCite attribution.")
33 (defvar gnus-cite-minimum-match-count 2
34 "Minimal number of identical prefix'es before we believe it is a citation.")
36 (defvar gnus-cite-face-list '(italic)
37 "Faces used for displaying different citations.
38 It is either a list of face names, or one of the following special
41 dark: Create faces from `gnus-face-dark-name-list'.
42 light: Create faces from `gnus-face-light-name-list'.
44 The variable `gnus-make-foreground' determines whether the created
45 faces change the foreground or the background colors.")
47 (defvar gnus-cite-attribution-prefix "in article\\|in <"
48 "Regexp matching the beginning of an attribution line.")
50 (defvar gnus-cite-attribution-postfix "\\(wrote\\|writes\\|said\\):[ \t]*$"
51 "Regexp matching the end of an attribution line.
52 The text matching the first grouping will be used as a button.")
54 (defvar gnus-cite-attribution-face 'underline
55 "Face used for attribution lines.
56 It is merged with the face for the cited text belonging to the attribution.")
58 (defvar gnus-cite-hide-percentage 30
59 "Only hide cited text if it is larger than this percent of the body.")
61 (defvar gnus-cite-hide-absolute 5
62 "Only hide cited text if there is at least this number of cited lines.")
65 ;;; Internal Variables:
67 (defvar gnus-cite-prefix-alist nil)
68 ;; Alist of citation prefixes.
69 ;; The cdr is a list of lines with that prefix.
71 (defvar gnus-cite-attribution-alist nil)
72 ;; Alist of attribution lines.
73 ;; The car is a line number.
74 ;; The cdr is the prefix for the citation started by that line.
76 (defvar gnus-cite-loose-prefix-alist nil)
77 ;; Alist of citation prefixes that have no matching attribution.
78 ;; The cdr is a list of lines with that prefix.
80 (defvar gnus-cite-loose-attribution-alist nil)
81 ;; Alist of attribution lines that have no matching citation.
82 ;; Each member has the form (WROTE IN PREFIX TAG), where
83 ;; WROTE: is the attribution line number
84 ;; IN: is the line number of the previous line if part of the same attribution,
85 ;; PREFIX: Is the citation prefix of the attribution line(s), and
86 ;; TAG: Is a SuperCite tag, if any.
90 (defun gnus-article-highlight-citation ()
91 "Highlight cited text.
92 Each citation in the article will be highlighted with a different face.
93 The faces are taken from `gnus-cite-face-list'.
94 Attribution lines are highlighted with the sameface as the
95 corresponding citation merged with `gnus-cite-attribution-face'.
97 Text is concidered cited if at least `gnus-cite-minimum-match-count'
98 lines matches `gnus-cite-prefix-regexp' with the same prefix.
100 Lines matching `gnus-cite-attribution-postfix' and perhaps
101 `gnus-cite-attribution-prefix' are concidered attribution lines."
103 ;; Create dark or light faces if necessary.
104 (cond ((eq gnus-cite-face-list 'light)
105 (setq gnus-cite-face-list
106 (mapcar 'gnus-make-face gnus-face-light-name-list)))
107 ((eq gnus-cite-face-list 'dark)
108 (setq gnus-cite-face-list
109 (mapcar 'gnus-make-face gnus-face-dark-name-list))))
111 (set-buffer gnus-article-buffer)
112 (gnus-cite-parse-maybe)
113 (let ((buffer-read-only nil)
114 (alist gnus-cite-prefix-alist)
115 (faces gnus-cite-face-list)
116 face entry prefix skip numbers number face-alist end)
117 ;; Loop through citation prefixes.
119 (setq entry (car alist)
124 faces (or (cdr faces) gnus-cite-face-list)
125 face-alist (cons (cons prefix face) face-alist))
127 (setq number (car numbers)
128 numbers (cdr numbers))
129 (and (not (assq number gnus-cite-attribution-alist))
130 (not (assq number gnus-cite-loose-attribution-alist))
131 (gnus-cite-add-face number prefix face))))
132 ;; Loop through attribution lines.
133 (setq alist gnus-cite-attribution-alist)
135 (setq entry (car alist)
139 skip (gnus-cite-find-prefix number)
140 face (cdr (assoc prefix face-alist)))
141 ;; Add attribution button.
143 (if (re-search-forward gnus-cite-attribution-postfix
144 (save-excursion (end-of-line 1) (point))
146 (gnus-article-add-button (match-beginning 1) (match-end 1)
147 'gnus-cite-toggle prefix))
148 ;; Highlight attribution line.
149 (gnus-cite-add-face number skip face)
150 (gnus-cite-add-face number skip gnus-cite-attribution-face))
151 ;; Loop through attribution lines.
152 (setq alist gnus-cite-loose-attribution-alist)
154 (setq entry (car alist)
157 skip (gnus-cite-find-prefix number))
158 (gnus-cite-add-face number skip gnus-cite-attribution-face)))))
160 (defun gnus-article-hide-citation ()
161 "Hide all cited text except attribution lines.
162 See the documentation for `gnus-article-highlight-citation'."
165 (set-buffer gnus-article-buffer)
166 (gnus-cite-parse-maybe)
167 (let ((buffer-read-only nil)
168 (alist gnus-cite-prefix-alist)
171 (setq numbers (cdr (car alist))
174 (setq number (car numbers)
175 numbers (cdr numbers))
177 (or (assq number gnus-cite-attribution-alist)
178 (put-text-property (point) (progn (forward-line 1) (point))
181 (defun gnus-article-hide-citation-maybe (&optional force)
182 "Hide cited text that has an attribution line.
183 This will do nothing unless at least `gnus-cite-hide-percentage'
184 percent ans at least `gnus-cite-hide-absolute' lines of the body is
185 cited text with attributions. When called interactively, these two
186 variables are ignored.
187 See also the documentation for `gnus-article-highlight-citation'."
188 (interactive (list 'force))
190 (set-buffer gnus-article-buffer)
191 (gnus-cite-parse-maybe)
192 (goto-char (point-min))
193 (search-forward "\n\n")
194 (let ((start (point))
195 (atts gnus-cite-attribution-alist)
196 (buffer-read-only nil)
199 (goto-char (point-max))
200 (re-search-backward gnus-signature-separator nil t)
201 (setq total (count-lines start (point)))
203 (setq hiden (+ hiden (length (cdr (assoc (cdr (car atts))
204 gnus-cite-prefix-alist))))
207 (and (> (* 100 hiden) (* gnus-cite-hide-percentage total))
208 (> hiden gnus-cite-hide-absolute)))
210 (setq atts gnus-cite-attribution-alist)
212 (setq total (cdr (assoc (cdr (car atts)) gnus-cite-prefix-alist))
215 (setq hiden (car total)
218 (or (assq hiden gnus-cite-attribution-alist)
219 (put-text-property (point) (progn (forward-line 1) (point))
220 'invisible t)))))))))
222 ;;; Internal functions:
224 (defun gnus-cite-parse-maybe ()
225 ;; Parse if the buffer has changes since last time.
226 (if (eq gnus-article-length (- (point-max) (point-min)))
228 (setq gnus-article-length (- (point-max) (point-min)))
231 (defun gnus-cite-parse ()
232 ;; Parse and connect citation prefixes and attribution lines.
233 (setq gnus-cite-prefix-alist nil
234 gnus-cite-attribution-alist nil
235 gnus-cite-loose-prefix-alist nil
236 gnus-cite-loose-attribution-alist nil)
237 ;; Parse current buffer searching for citation prefixes.
238 (goto-char (point-min))
239 (search-forward "\n\n")
240 (let ((line (1+ (count-lines (point-min) (point))))
243 (goto-char (point-max))
244 (re-search-backward gnus-signature-separator nil t)
246 alist entry prefix start begin end numbers)
247 ;; Get all potential prefixes in `alist'.
248 (while (< (point) max)
251 end (progn (beginning-of-line 2) (point))
254 ;; Ignore standard SuperCite attribution prefix.
255 (if (looking-at gnus-supercite-regexp)
257 (setq end (1+ (match-end 1)))
258 (setq end (1+ begin))))
259 ;; Ignore very long prefixes.
260 (if (> end (+ (point) gnus-cite-max-prefix))
261 (setq end (+ (point) gnus-cite-max-prefix)))
262 (while (re-search-forward gnus-cite-prefix-regexp (1- end) t)
264 (setq end (match-end 0)
265 prefix (buffer-substring begin end))
266 (set-text-properties 0 (length prefix) nil prefix)
267 (setq entry (assoc prefix alist))
269 (setcdr entry (cons line (cdr entry)))
270 (setq alist (cons (list prefix line) alist)))
273 (setq line (1+ line)))
274 ;; We got all the potential prefixes. Now create
275 ;; `gnus-cite-prefix-alist' containing the oldest prefix for each
276 ;; line that appears at least gnus-cite-minimum-match-count
277 ;; times. First sort them by length. Longer is older.
278 (setq alist (sort alist (lambda (a b)
279 (> (length (car a)) (length (car b))))))
281 (setq entry (car alist)
285 (cond ((null numbers)
286 ;; No lines with this prefix that wasn't also part of
289 ((< (length numbers) gnus-cite-minimum-match-count)
290 ;; Too few lines with this prefix. We keep it a bit
291 ;; longer in case it is an exact match for an attribution
292 ;; line, but we don't remove the line from other
294 (setq gnus-cite-prefix-alist
295 (cons entry gnus-cite-prefix-alist)))
297 (setq gnus-cite-prefix-alist (cons entry gnus-cite-prefix-alist))
298 ;; Remove articles from other prefixes.
302 (setq current (car loop)
305 (gnus-set-difference (cdr current) numbers))))))))
306 ;; No citations have been connected to attribution lines yet.
307 (setq gnus-cite-loose-prefix-alist (append gnus-cite-prefix-alist nil))
309 ;; Parse current buffer searching for attribution lines.
310 (goto-char (point-min))
311 (search-forward "\n\n")
312 (while (re-search-forward gnus-cite-attribution-postfix (point-max) t)
313 (let* ((start (match-beginning 0))
315 (wrote (count-lines (point-min) end))
316 (prefix (gnus-cite-find-prefix wrote))
317 ;; Check previous line for an attribution leader.
319 (beginning-of-line 1)
320 (and (looking-at gnus-supercite-secondary-regexp)
321 (buffer-substring (match-beginning 1)
325 (and (re-search-backward gnus-cite-attribution-prefix
327 (beginning-of-line 0)
330 (not (re-search-forward gnus-cite-attribution-postfix
332 (count-lines (point-min) (1+ (point)))))))
336 (setq gnus-cite-loose-attribution-alist
337 (cons (list wrote in prefix tag)
338 gnus-cite-loose-attribution-alist))))
339 ;; Find exact supercite citations.
340 (gnus-cite-match-attributions 'small nil
343 (concat "\\`" (regexp-quote prefix) "[ \t]*"
344 (regexp-quote tag) ">"))))
345 ;; Find loose supercite citations after attributions.
346 (gnus-cite-match-attributions 'small t
348 (if tag (concat "\\<" (regexp-quote tag) "\\>"))))
349 ;; Find loose supercite citations anywhere.
350 (gnus-cite-match-attributions 'small nil
352 (if tag (concat "\\<" (regexp-quote tag) "\\>"))))
353 ;; Find nested citations after attributions.
354 (gnus-cite-match-attributions 'small-if-unique t
356 (concat "\\`" (regexp-quote prefix) ".+")))
357 ;; Find nested citations anywhere.
358 (gnus-cite-match-attributions 'small nil
360 (concat "\\`" (regexp-quote prefix) ".+")))
361 ;; Remove loose prefixes with too few lines.
362 (let ((alist gnus-cite-loose-prefix-alist)
365 (setq entry (car alist)
367 (if (< (length (cdr entry)) gnus-cite-minimum-match-count)
368 (setq gnus-cite-prefix-alist
369 (delq entry gnus-cite-prefix-alist)
370 gnus-cite-loose-prefix-alist
371 (delq entry gnus-cite-loose-prefix-alist)))))
372 ;; Find flat attributions.
373 (gnus-cite-match-attributions 'first t nil)
374 ;; Find any attributions (are we getting desperate yet?).
375 (gnus-cite-match-attributions 'first nil nil))
377 (defun gnus-cite-match-attributions (sort after fun)
378 ;; Match all loose attributions and citations (SORT AFTER FUN) .
380 ;; If SORT is `small', the citation with the shortest prefix will be
381 ;; used, if it is `first' the first prefix will be used, if it is
382 ;; `small-if-unique' the shortest prefix will be used if the
383 ;; attribution line does not share its own prefix with other
384 ;; loose attribution lines, otherwise the first prefix will be used.
386 ;; If AFTER is non-nil, only citations after the attribution line
387 ;; will be concidered.
389 ;; If FUN is non-nil, it will be called with the arguments (WROTE
390 ;; PREFIX TAG) and expected to return a regular expression. Only
391 ;; citations whose prefix matches the regular expression will be
394 ;; WROTE is the attribution line number.
395 ;; PREFIX is the attribution line prefix.
396 ;; TAG is the SuperCite tag on the attribution line.
397 (let ((atts gnus-cite-loose-attribution-alist)
399 att wrote in prefix tag regexp limit smallest best size aprefix)
407 regexp (if fun (funcall fun prefix tag) "")
408 size (cond ((eq sort 'small) t)
409 ((eq sort 'first) nil)
410 (t (< (length (gnus-cite-find-loose prefix)) 2)))
411 limit (if after wrote -1)
414 (let ((cites gnus-cite-loose-prefix-alist)
415 cite candidate numbers first compare)
417 (setq cite (car cites)
421 first (apply 'min numbers)
422 compare (if size (length candidate) first))
425 (string-match regexp candidate)
431 (setq gnus-cite-loose-attribution-alist
432 (delq att gnus-cite-loose-attribution-alist))
433 (setq gnus-cite-attribution-alist
434 (cons (cons wrote (car best)) gnus-cite-attribution-alist))
436 (setq gnus-cite-attribution-alist
437 (cons (cons in (car best)) gnus-cite-attribution-alist)))
438 (if (memq best gnus-cite-loose-prefix-alist)
439 (let ((loop gnus-cite-prefix-alist)
442 (setq gnus-cite-loose-prefix-alist
443 (delq best gnus-cite-loose-prefix-alist))
445 (setq current (car loop)
447 (if (eq current best)
449 (setcdr current (gnus-set-difference (cdr current) numbers))
450 (if (null (cdr current))
451 (setq gnus-cite-loose-prefix-alist
452 (delq current gnus-cite-loose-prefix-alist)
453 atts (delq current atts)))))))))))
455 (defun gnus-cite-find-loose (prefix)
456 ;; Return a list of loose attribution lines prefixed by PREFIX.
457 (let* ((atts gnus-cite-loose-attribution-alist)
458 att line lines candidate)
463 (if (string-equal (gnus-cite-find-prefix line) prefix)
464 (setq lines (cons line lines))))
467 (defun gnus-cite-add-face (number prefix face)
468 ;; At line NUMBER, ignore PREFIX and add FACE to the rest of the line.
472 (forward-char (length prefix))
473 (skip-chars-forward " \t")
476 (skip-chars-backward " \t")
479 (overlay-put (make-overlay from to) 'face face)))))
481 (defun gnus-cite-toggle (prefix)
483 (set-buffer gnus-article-buffer)
484 (let ((buffer-read-only nil)
485 (numbers (cdr (assoc prefix gnus-cite-prefix-alist)))
488 (setq number (car numbers)
489 numbers (cdr numbers))
491 (cond ((get-text-property (point) 'invisible)
492 (put-text-property (point) (progn (forward-line 1) (point))
494 ((assq number gnus-cite-attribution-alist))
496 (put-text-property (point) (progn (forward-line 1) (point))
499 (defun gnus-cite-find-prefix (line)
500 ;; Return citation prefix for LINE.
501 (let ((alist gnus-cite-prefix-alist)
505 (setq entry (car alist)
507 (if (memq line (cdr entry))
508 (setq prefix (car entry))))
513 ;;; gnus-cite.el ends here