*** empty log message ***
[gnus] / lisp / gnus-cite.el
1 ;;; gnus-cite.el --- parse citations in articles for Gnus
2 ;; Copyright (C) 1995 Free Software Foundation, Inc.
3
4 ;; Author: Per Abrahamsen <abraham@iesd.auc.dk>
5 ;; Keywords: news, mail
6
7 ;; This file is part of GNU Emacs.
8
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)
12 ;; any later version.
13
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.
18
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.
22
23 ;;; Commentary:
24
25 ;;; Code:
26
27 (require 'gnus)
28 (require 'gnus-msg)
29
30 (eval-and-compile
31   (autoload 'gnus-article-add-button "gnus-vis")
32   )
33
34 ;;; Customization:
35
36 (defvar gnus-cite-prefix-regexp 
37     "^[]>|:}+ ]*[]>|:}+]\\(.*>\\)?\\|^.*>"
38   "Regexp matching the longest possible citation prefix on a line.")
39
40 (defvar gnus-cite-max-prefix 20
41   "Maximal possible length for a citation prefix.")
42
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.")
48
49 (defvar gnus-supercite-secondary-regexp "^.*\"\\([^\"\n]+\\)\" +=="
50   "Regexp matching mangled SuperCite attribution lines.
51 The first regexp group should match the SuperCite attribution.")
52
53 (defvar gnus-cite-minimum-match-count 2
54   "Minimal number of identical prefix'es before we believe it is a citation.")
55
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
59 values:
60
61 dark: Create faces from `gnus-face-dark-name-list'.
62 light: Create faces from `gnus-face-light-name-list'.
63
64 The variable `gnus-make-foreground' determines whether the created
65 faces change the foreground or the background colors.")
66
67 (defvar gnus-cite-attribution-prefix "in article\\|in <"
68   "Regexp matching the beginning of an attribution line.")
69
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.")
74
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.")
78
79 (defvar gnus-cite-hide-percentage 50
80   "Only hide cited text if it is larger than this percent of the body.")
81
82 (defvar gnus-cite-hide-absolute 10
83   "Only hide cited text if there is at least this number of cited lines.")
84
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"
88     "turquoise")
89   "Names of light colors.")
90
91 (defvar gnus-face-dark-name-list
92   '("blue" "dark salmon" "firebrick"
93     "dark green" "dark orange" "dark khaki" "dark violet"
94     "dark turquoise")
95   "Names of dark colors.")
96
97 ;;; Internal Variables:
98
99 (defvar gnus-article-length nil)
100 ;; Length of article last time we parsed it.
101
102 (defvar gnus-cite-prefix-alist nil)
103 ;; Alist of citation prefixes.  
104 ;; The cdr is a list of lines with that prefix.
105
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.
110
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.
114
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.
122
123 ;;; Commands:
124
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'.
131
132 Text is concidered cited if at least `gnus-cite-minimum-match-count'
133 lines matches `gnus-cite-prefix-regexp' with the same prefix.  
134
135 Lines matching `gnus-cite-attribution-postfix' and perhaps
136 `gnus-cite-attribution-prefix' are concidered attribution lines."
137   (interactive)
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))))
145   (save-excursion
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.
154       (while alist
155         (setq entry (car alist)
156               alist (cdr alist)
157               prefix (car entry)
158               numbers (cdr entry)
159               face (car faces)
160               faces (or (cdr faces) gnus-cite-face-list)
161               face-alist (cons (cons prefix face) face-alist))
162         (while numbers
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)
170       (while alist
171         (setq entry (car alist)
172               alist (cdr alist)
173               number (car entry)
174               prefix (cdr entry)
175               skip (gnus-cite-find-prefix number)
176               face (cdr (assoc prefix face-alist)))
177         ;; Add attribution button.
178         (goto-line number)
179         (if (re-search-forward gnus-cite-attribution-postfix 
180                                (save-excursion (end-of-line 1) (point))
181                                t)
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)
189       (while alist
190         (setq entry (car alist)
191               alist (cdr alist)
192               number (car entry)
193               skip (gnus-cite-find-prefix number))
194         (gnus-cite-add-face number skip gnus-cite-attribution-face)))))
195
196 (defun gnus-article-hide-citation ()
197   "Hide all cited text except attribution lines.
198 See the documentation for `gnus-article-highlight-citation'."
199   (interactive)
200   (save-excursion
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)
206           numbers number)
207       (while alist
208         (setq numbers (cdr (car alist))
209               alist (cdr alist))
210         (while numbers
211           (setq number (car numbers)
212                 numbers (cdr numbers))
213           (goto-line number)
214           (or (assq number gnus-cite-attribution-alist)
215               (add-text-properties (point) (progn (forward-line 1) (point))
216                                    gnus-hidden-properties)))))))
217
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))
226   (save-excursion
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)
235           (hiden 0)
236           total)
237       (goto-char (point-max))
238       (re-search-backward gnus-signature-separator nil t)
239       (setq total (count-lines start (point)))
240       (while atts
241         (setq hiden (+ hiden (length (cdr (assoc (cdr (car atts))
242                                                  gnus-cite-prefix-alist))))
243               atts (cdr atts)))
244       (if (or force
245               (and (> (* 100 hiden) (* gnus-cite-hide-percentage total))
246                    (> hiden gnus-cite-hide-absolute)))
247           (progn
248             (setq atts gnus-cite-attribution-alist)
249             (while atts
250               (setq total (cdr (assoc (cdr (car atts)) gnus-cite-prefix-alist))
251                     atts (cdr atts))
252               (while total
253                 (setq hiden (car total)
254                       total (cdr total))
255                 (goto-line hiden)
256                 (or (assq hiden gnus-cite-attribution-alist)
257                     (add-text-properties (point) 
258                                          (progn (forward-line 1) (point))
259                                          gnus-hidden-properties)))))))))
260
261 ;;; Internal functions:
262
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)))
266       ()
267     (setq gnus-article-length (- (point-max) (point-min)))
268     (gnus-cite-parse)))
269
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))))
280         (case-fold-search t)
281         (max (save-excursion
282                (goto-char (point-max))
283                (re-search-backward gnus-signature-separator nil t)
284                (point)))
285         alist entry prefix start begin end numbers)
286     ;; Get all potential prefixes in `alist'.
287     (while (< (point) max)
288       ;; Each line.
289       (setq begin (point)
290             end (progn (beginning-of-line 2) (point))
291             start end)
292       (goto-char begin)
293       ;; Ignore standard SuperCite attribution prefix.
294       (if (looking-at gnus-supercite-regexp)
295           (if (match-end 1)
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)
302         ;; Each prefix.
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))
307         (if entry 
308             (setcdr entry (cons line (cdr entry)))
309           (setq alist (cons (list prefix line) alist)))
310         (goto-char begin))
311       (goto-char start)
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))))))
319     (while alist
320       (setq entry (car alist)
321             prefix (car entry)
322             numbers (cdr entry)
323             alist (cdr alist))
324       (cond ((null numbers)
325              ;; No lines with this prefix that wasn't also part of
326              ;; a longer prefix.
327              )
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
332              ;; prefixes. 
333              (setq gnus-cite-prefix-alist
334                    (cons entry gnus-cite-prefix-alist)))
335             (t
336              (setq gnus-cite-prefix-alist (cons entry gnus-cite-prefix-alist))
337              ;; Remove articles from other prefixes.
338              (let ((loop alist)
339                    current)
340                (while loop
341                  (setq current (car loop)
342                        loop (cdr loop))
343                  (setcdr current 
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))
347
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))
353            (end (match-end 0))
354            (wrote (count-lines (point-min) end))
355            (prefix (gnus-cite-find-prefix wrote))
356            ;; Check previous line for an attribution leader.
357            (tag (progn
358                   (beginning-of-line 1)
359                   (and (looking-at gnus-supercite-secondary-regexp)
360                        (buffer-substring (match-beginning 1)
361                                          (match-end 1)))))
362            (in (progn
363                  (goto-char start)
364                  (and (re-search-backward gnus-cite-attribution-prefix
365                                           (save-excursion
366                                             (beginning-of-line 0)
367                                             (point))
368                                           t)
369                       (not (re-search-forward gnus-cite-attribution-postfix
370                                               start t))
371                       (count-lines (point-min) (1+ (point)))))))
372       (if (eq wrote in)
373           (setq in nil))
374       (goto-char end)
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
380    (lambda (prefix tag)
381      (if tag
382          (concat "\\`" (regexp-quote prefix) "[ \t]*" 
383                  (regexp-quote tag) ">"))))
384   ;; Find loose supercite citations after attributions.
385   (gnus-cite-match-attributions 'small t
386    (lambda (prefix tag)
387      (if tag (concat "\\<" (regexp-quote tag) "\\>"))))
388   ;; Find loose supercite citations anywhere.
389   (gnus-cite-match-attributions 'small nil
390    (lambda (prefix tag)
391      (if tag (concat "\\<" (regexp-quote tag) "\\>"))))
392   ;; Find nested citations after attributions.
393   (gnus-cite-match-attributions 'small-if-unique t
394    (lambda (prefix tag)
395      (concat "\\`" (regexp-quote prefix) ".+")))
396   ;; Find nested citations anywhere.
397   (gnus-cite-match-attributions 'small nil
398    (lambda (prefix tag)
399      (concat "\\`" (regexp-quote prefix) ".+")))
400   ;; Remove loose prefixes with too few lines.
401   (let ((alist gnus-cite-loose-prefix-alist)
402         entry prefix)
403     (while alist
404       (setq entry (car alist)
405             alist (cdr 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))
415
416 (defun gnus-cite-match-attributions (sort after fun)
417   ;; Match all loose attributions and citations (SORT AFTER FUN) .
418   ;;
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.
424   ;;
425   ;; If AFTER is non-nil, only citations after the attribution line
426   ;; will be concidered.
427   ;;
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
431   ;; concidered. 
432   ;; 
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)
437         (case-fold-search t)
438         att wrote in prefix tag regexp limit smallest best size aprefix)
439     (while atts
440       (setq att (car atts)
441             atts (cdr atts)
442             wrote (nth 0 att)
443             in (nth 1 att)
444             prefix (nth 2 att)
445             tag (nth 3 att)
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)
451             smallest 1000000                   
452             best nil)
453       (let ((cites gnus-cite-loose-prefix-alist)
454             cite candidate numbers first compare)
455         (while cites
456           (setq cite (car cites)
457                 cites (cdr cites)
458                 candidate (car cite)
459                 numbers (cdr cite)
460                 first (apply 'min numbers)
461                 compare (if size (length candidate) first))
462           (and (> first limit)
463                regexp
464                (string-match regexp candidate)
465                (< compare smallest)
466                (setq best cite
467                      smallest compare))))
468       (if (null best)
469           ()
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))
474         (if in
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)
479                  (numbers (cdr best))
480                  current)
481              (setq gnus-cite-loose-prefix-alist
482                    (delq best gnus-cite-loose-prefix-alist))
483              (while loop
484                (setq current (car loop)
485                      loop (cdr loop))
486                (if (eq current best)
487                    ()
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)))))))))))
493
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)
498     (while atts
499       (setq att (car atts)
500             line (car att)
501             atts (cdr atts))
502       (if (string-equal (gnus-cite-find-prefix line) prefix)
503           (setq lines (cons line lines))))
504     lines))
505
506 (defun gnus-cite-add-face (number prefix face)
507   ;; At line NUMBER, ignore PREFIX and add FACE to the rest of the line.
508   (if face
509       (let ((inhibit-point-motion-hooks t)
510             from to)
511         (goto-line number)
512         (forward-char (length prefix))
513         (skip-chars-forward " \t")
514         (setq from (point))
515         (end-of-line 1)
516         (skip-chars-backward " \t")
517         (setq to (point))
518         (if (< from to)
519             (overlay-put (make-overlay from to) 'face face)))))
520
521 (defun gnus-cite-toggle (prefix)
522   (save-excursion
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)
527           number)
528       (while numbers
529         (setq number (car numbers)
530               numbers (cdr numbers))
531         (goto-line number)
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))
536               (t
537                (add-text-properties (point) (progn (forward-line 1) (point))
538                                     gnus-hidden-properties)))))))
539
540 (defun gnus-cite-find-prefix (line)
541   ;; Return citation prefix for LINE.
542   (let ((alist gnus-cite-prefix-alist)
543         (prefix "")
544         entry)
545     (while alist
546       (setq entry (car alist)
547             alist (cdr alist))
548       (if (memq line (cdr entry))
549           (setq prefix (car entry))))
550     prefix))
551
552 (provide 'gnus-cite)
553
554 ;;; gnus-cite.el ends here