*** 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 
57   (if (eq gnus-display-type 'color)
58       (if (eq gnus-background-mode 'dark) 'light 'dark)
59     '(italic))
60   "Faces used for displaying different citations.
61 It is either a list of face names, or one of the following special
62 values:
63
64 dark: Create faces from `gnus-face-dark-name-list'.
65 light: Create faces from `gnus-face-light-name-list'.
66
67 The variable `gnus-make-foreground' determines whether the created
68 faces change the foreground or the background colors.")
69
70 (defvar gnus-cite-attribution-prefix "in article\\|in <"
71   "Regexp matching the beginning of an attribution line.")
72
73 (defvar gnus-cite-attribution-postfix
74   "\\(wrote\\|writes\\|said\\|says\\):[ \t]*$"
75   "Regexp matching the end of an attribution line.
76 The text matching the first grouping will be used as a button.")
77
78 (defvar gnus-cite-attribution-face 'underline
79   "Face used for attribution lines.
80 It is merged with the face for the cited text belonging to the attribution.")
81
82 (defvar gnus-cite-hide-percentage 50
83   "Only hide cited text if it is larger than this percent of the body.")
84
85 (defvar gnus-cite-hide-absolute 10
86   "Only hide cited text if there is at least this number of cited lines.")
87
88 (defvar gnus-face-light-name-list
89   '("light blue" "light cyan" "light yellow" "light pink"
90     "pale green" "beige" "orange" "magenta" "violet" "medium purple"
91     "turquoise")
92   "Names of light colors.")
93
94 (defvar gnus-face-dark-name-list
95   '("dark salmon" "firebrick"
96     "dark green" "dark orange" "dark khaki" "dark violet"
97     "dark turquoise")
98   "Names of dark colors.")
99
100 ;;; Internal Variables:
101
102 (defvar gnus-article-length nil)
103 ;; Length of article last time we parsed it.
104
105 (defvar gnus-cite-prefix-alist nil)
106 ;; Alist of citation prefixes.  
107 ;; The cdr is a list of lines with that prefix.
108
109 (defvar gnus-cite-attribution-alist nil)
110 ;; Alist of attribution lines.
111 ;; The car is a line number.
112 ;; The cdr is the prefix for the citation started by that line.
113
114 (defvar gnus-cite-loose-prefix-alist nil)
115 ;; Alist of citation prefixes that have no matching attribution.
116 ;; The cdr is a list of lines with that prefix.
117
118 (defvar gnus-cite-loose-attribution-alist nil)
119 ;; Alist of attribution lines that have no matching citation.
120 ;; Each member has the form (WROTE IN PREFIX TAG), where
121 ;; WROTE: is the attribution line number
122 ;; IN: is the line number of the previous line if part of the same attribution,
123 ;; PREFIX: Is the citation prefix of the attribution line(s), and
124 ;; TAG: Is a SuperCite tag, if any.
125
126 ;;; Commands:
127
128 (defun gnus-article-highlight-citation ()
129   "Highlight cited text.
130 Each citation in the article will be highlighted with a different face.
131 The faces are taken from `gnus-cite-face-list'.
132 Attribution lines are highlighted with the sameface as the
133 corresponding citation merged with `gnus-cite-attribution-face'.
134
135 Text is considered cited if at least `gnus-cite-minimum-match-count'
136 lines matches `gnus-cite-prefix-regexp' with the same prefix.  
137
138 Lines matching `gnus-cite-attribution-postfix' and perhaps
139 `gnus-cite-attribution-prefix' are considered attribution lines."
140   (interactive)
141   ;; Create dark or light faces if necessary.
142   (cond ((eq gnus-cite-face-list 'light)
143          (setq gnus-cite-face-list
144              (mapcar 'gnus-make-face gnus-face-light-name-list)))
145         ((eq gnus-cite-face-list 'dark)
146          (setq gnus-cite-face-list
147              (mapcar 'gnus-make-face gnus-face-dark-name-list))))
148   (save-excursion
149     (set-buffer gnus-article-buffer)
150     (gnus-cite-parse-maybe)
151     (let ((buffer-read-only nil)
152           (alist gnus-cite-prefix-alist)
153           (faces gnus-cite-face-list)
154           (inhibit-point-motion-hooks t)
155           face entry prefix skip numbers number face-alist)
156       ;; Loop through citation prefixes.
157       (while alist
158         (setq entry (car alist)
159               alist (cdr alist)
160               prefix (car entry)
161               numbers (cdr entry)
162               face (car faces)
163               faces (or (cdr faces) gnus-cite-face-list)
164               face-alist (cons (cons prefix face) face-alist))
165         (while numbers
166           (setq number (car numbers)
167                 numbers (cdr numbers))
168           (and (not (assq number gnus-cite-attribution-alist))
169                (not (assq number gnus-cite-loose-attribution-alist))
170                (gnus-cite-add-face number prefix face))))
171       ;; Loop through attribution lines.
172       (setq alist gnus-cite-attribution-alist)
173       (while alist
174         (setq entry (car alist)
175               alist (cdr alist)
176               number (car entry)
177               prefix (cdr entry)
178               skip (gnus-cite-find-prefix number)
179               face (cdr (assoc prefix face-alist)))
180         ;; Add attribution button.
181         (goto-line number)
182         (if (re-search-forward gnus-cite-attribution-postfix 
183                                (save-excursion (end-of-line 1) (point))
184                                t)
185             (gnus-article-add-button (match-beginning 1) (match-end 1)
186                                      'gnus-cite-toggle prefix))
187         ;; Highlight attribution line.
188         (gnus-cite-add-face number skip face)
189         (gnus-cite-add-face number skip gnus-cite-attribution-face))
190       ;; Loop through attribution lines.
191       (setq alist gnus-cite-loose-attribution-alist)
192       (while alist
193         (setq entry (car alist)
194               alist (cdr alist)
195               number (car entry)
196               skip (gnus-cite-find-prefix number))
197         (gnus-cite-add-face number skip gnus-cite-attribution-face)))))
198
199 (defun gnus-article-hide-citation ()
200   "Hide all cited text except attribution lines.
201 See the documentation for `gnus-article-highlight-citation'."
202   (interactive)
203   (save-excursion
204     (set-buffer gnus-article-buffer)
205     (gnus-cite-parse-maybe)
206     (let ((buffer-read-only nil)
207           (alist gnus-cite-prefix-alist)
208           (inhibit-point-motion-hooks t)
209           numbers number)
210       (while alist
211         (setq numbers (cdr (car alist))
212               alist (cdr alist))
213         (while numbers
214           (setq number (car numbers)
215                 numbers (cdr numbers))
216           (goto-line number)
217           (or (assq number gnus-cite-attribution-alist)
218               (add-text-properties (point) (progn (forward-line 1) (point))
219                                    gnus-hidden-properties)))))))
220
221 (defun gnus-article-hide-citation-maybe (&optional force)
222   "Hide cited text that has an attribution line.
223 This will do nothing unless at least `gnus-cite-hide-percentage'
224 percent ans at least `gnus-cite-hide-absolute' lines of the body is
225 cited text with attributions.  When called interactively, these two
226 variables are ignored.
227 See also the documentation for `gnus-article-highlight-citation'."
228   (interactive (list 'force))
229   (save-excursion
230     (set-buffer gnus-article-buffer)
231     (gnus-cite-parse-maybe)
232     (goto-char (point-min))
233     (search-forward "\n\n")
234     (let ((start (point))
235           (atts gnus-cite-attribution-alist)
236           (buffer-read-only nil)
237           (inhibit-point-motion-hooks t)
238           (hiden 0)
239           total)
240       (goto-char (point-max))
241       (re-search-backward gnus-signature-separator nil t)
242       (setq total (count-lines start (point)))
243       (while atts
244         (setq hiden (+ hiden (length (cdr (assoc (cdr (car atts))
245                                                  gnus-cite-prefix-alist))))
246               atts (cdr atts)))
247       (if (or force
248               (and (> (* 100 hiden) (* gnus-cite-hide-percentage total))
249                    (> hiden gnus-cite-hide-absolute)))
250           (progn
251             (setq atts gnus-cite-attribution-alist)
252             (while atts
253               (setq total (cdr (assoc (cdr (car atts)) gnus-cite-prefix-alist))
254                     atts (cdr atts))
255               (while total
256                 (setq hiden (car total)
257                       total (cdr total))
258                 (goto-line hiden)
259                 (or (assq hiden gnus-cite-attribution-alist)
260                     (add-text-properties (point) 
261                                          (progn (forward-line 1) (point))
262                                          gnus-hidden-properties)))))))))
263
264 ;;; Internal functions:
265
266 (defun gnus-cite-parse-maybe ()
267   ;; Parse if the buffer has changes since last time.
268   (if (eq gnus-article-length (- (point-max) (point-min)))
269       ()
270     (setq gnus-article-length (- (point-max) (point-min)))
271     (gnus-cite-parse)))
272
273 (defun gnus-cite-parse ()
274   ;; Parse and connect citation prefixes and attribution lines.
275   (setq gnus-cite-prefix-alist nil
276         gnus-cite-attribution-alist nil
277         gnus-cite-loose-prefix-alist nil
278         gnus-cite-loose-attribution-alist nil)
279   ;; Parse current buffer searching for citation prefixes.
280   (goto-char (point-min))
281   (search-forward "\n\n")
282   (let ((line (1+ (count-lines (point-min) (point))))
283         (case-fold-search t)
284         (max (save-excursion
285                (goto-char (point-max))
286                (re-search-backward gnus-signature-separator nil t)
287                (point)))
288         alist entry start begin end numbers prefix)
289     ;; Get all potential prefixes in `alist'.
290     (while (< (point) max)
291       ;; Each line.
292       (setq begin (point)
293             end (progn (beginning-of-line 2) (point))
294             start end)
295       (goto-char begin)
296       ;; Ignore standard SuperCite attribution prefix.
297       (if (looking-at gnus-supercite-regexp)
298           (if (match-end 1)
299               (setq end (1+ (match-end 1)))
300             (setq end (1+ begin))))
301       ;; Ignore very long prefixes.
302       (if (> end (+ (point) gnus-cite-max-prefix))
303           (setq end (+ (point) gnus-cite-max-prefix)))
304       (while (re-search-forward gnus-cite-prefix-regexp (1- end) t)
305         ;; Each prefix.
306         (setq end (match-end 0)
307               prefix (buffer-substring begin end))
308         (set-text-properties 0 (length prefix) nil prefix)
309         (setq entry (assoc prefix alist))
310         (if entry 
311             (setcdr entry (cons line (cdr entry)))
312           (setq alist (cons (list prefix line) alist)))
313         (goto-char begin))
314       (goto-char start)
315       (setq line (1+ line)))
316     ;; We got all the potential prefixes.  Now create
317     ;; `gnus-cite-prefix-alist' containing the oldest prefix for each
318     ;; line that appears at least gnus-cite-minimum-match-count
319     ;; times. First sort them by length.  Longer is older.
320     (setq alist (sort alist (lambda (a b)
321                               (> (length (car a)) (length (car b))))))
322     (while alist
323       (setq entry (car alist)
324             prefix (car entry)
325             numbers (cdr entry)
326             alist (cdr alist))
327       (cond ((null numbers)
328              ;; No lines with this prefix that wasn't also part of
329              ;; a longer prefix.
330              )
331             ((< (length numbers) gnus-cite-minimum-match-count)
332              ;; Too few lines with this prefix.  We keep it a bit
333              ;; longer in case it is an exact match for an attribution
334              ;; line, but we don't remove the line from other
335              ;; prefixes. 
336              (setq gnus-cite-prefix-alist
337                    (cons entry gnus-cite-prefix-alist)))
338             (t
339              (setq gnus-cite-prefix-alist (cons entry gnus-cite-prefix-alist))
340              ;; Remove articles from other prefixes.
341              (let ((loop alist)
342                    current)
343                (while loop
344                  (setq current (car loop)
345                        loop (cdr loop))
346                  (setcdr current 
347                          (gnus-set-difference (cdr current) numbers))))))))
348   ;; No citations have been connected to attribution lines yet.
349   (setq gnus-cite-loose-prefix-alist (append gnus-cite-prefix-alist nil))
350
351   ;; Parse current buffer searching for attribution lines.
352   (goto-char (point-min))
353   (search-forward "\n\n")
354   (while (re-search-forward gnus-cite-attribution-postfix (point-max) t)
355     (let* ((start (match-beginning 0))
356            (end (match-end 0))
357            (wrote (count-lines (point-min) end))
358            (prefix (gnus-cite-find-prefix wrote))
359            ;; Check previous line for an attribution leader.
360            (tag (progn
361                   (beginning-of-line 1)
362                   (and (looking-at gnus-supercite-secondary-regexp)
363                        (buffer-substring (match-beginning 1)
364                                          (match-end 1)))))
365            (in (progn
366                  (goto-char start)
367                  (and (re-search-backward gnus-cite-attribution-prefix
368                                           (save-excursion
369                                             (beginning-of-line 0)
370                                             (point))
371                                           t)
372                       (not (re-search-forward gnus-cite-attribution-postfix
373                                               start t))
374                       (count-lines (point-min) (1+ (point)))))))
375       (if (eq wrote in)
376           (setq in nil))
377       (goto-char end)
378       (setq gnus-cite-loose-attribution-alist
379             (cons (list wrote in prefix tag)
380                   gnus-cite-loose-attribution-alist))))
381   ;; Find exact supercite citations.
382   (gnus-cite-match-attributions 'small nil
383    (lambda (prefix tag)
384      (if tag
385          (concat "\\`" (regexp-quote prefix) "[ \t]*" 
386                  (regexp-quote tag) ">"))))
387   ;; Find loose supercite citations after attributions.
388   (gnus-cite-match-attributions 'small t
389    (lambda (prefix tag)
390      (if tag (concat "\\<" (regexp-quote tag) "\\>"))))
391   ;; Find loose supercite citations anywhere.
392   (gnus-cite-match-attributions 'small nil
393    (lambda (prefix tag)
394      (if tag (concat "\\<" (regexp-quote tag) "\\>"))))
395   ;; Find nested citations after attributions.
396   (gnus-cite-match-attributions 'small-if-unique t
397    (lambda (prefix tag)
398      (concat "\\`" (regexp-quote prefix) ".+")))
399   ;; Find nested citations anywhere.
400   (gnus-cite-match-attributions 'small nil
401    (lambda (prefix tag)
402      (concat "\\`" (regexp-quote prefix) ".+")))
403   ;; Remove loose prefixes with too few lines.
404   (let ((alist gnus-cite-loose-prefix-alist)
405         entry)
406     (while alist
407       (setq entry (car alist)
408             alist (cdr alist))
409       (if (< (length (cdr entry)) gnus-cite-minimum-match-count)
410           (setq gnus-cite-prefix-alist
411                 (delq entry gnus-cite-prefix-alist)
412                 gnus-cite-loose-prefix-alist
413                 (delq entry gnus-cite-loose-prefix-alist)))))
414   ;; Find flat attributions.
415   (gnus-cite-match-attributions 'first t nil)
416   ;; Find any attributions (are we getting desperate yet?).
417   (gnus-cite-match-attributions 'first nil nil))
418
419 (defun gnus-cite-match-attributions (sort after fun)
420   ;; Match all loose attributions and citations (SORT AFTER FUN) .
421   ;;
422   ;; If SORT is `small', the citation with the shortest prefix will be
423   ;; used, if it is `first' the first prefix will be used, if it is
424   ;; `small-if-unique' the shortest prefix will be used if the
425   ;; attribution line does not share its own prefix with other
426   ;; loose attribution lines, otherwise the first prefix will be used.
427   ;;
428   ;; If AFTER is non-nil, only citations after the attribution line
429   ;; will be concidered.
430   ;;
431   ;; If FUN is non-nil, it will be called with the arguments (WROTE
432   ;; PREFIX TAG) and expected to return a regular expression.  Only
433   ;; citations whose prefix matches the regular expression will be
434   ;; concidered. 
435   ;; 
436   ;; WROTE is the attribution line number.
437   ;; PREFIX is the attribution line prefix.
438   ;; TAG is the SuperCite tag on the attribution line.
439   (let ((atts gnus-cite-loose-attribution-alist)
440         (case-fold-search t)
441         att wrote in prefix tag regexp limit smallest best size)
442     (while atts
443       (setq att (car atts)
444             atts (cdr atts)
445             wrote (nth 0 att)
446             in (nth 1 att)
447             prefix (nth 2 att)
448             tag (nth 3 att)
449             regexp (if fun (funcall fun prefix tag) "")
450             size (cond ((eq sort 'small) t)
451                        ((eq sort 'first) nil)
452                        (t (< (length (gnus-cite-find-loose prefix)) 2)))
453             limit (if after wrote -1)
454             smallest 1000000                   
455             best nil)
456       (let ((cites gnus-cite-loose-prefix-alist)
457             cite candidate numbers first compare)
458         (while cites
459           (setq cite (car cites)
460                 cites (cdr cites)
461                 candidate (car cite)
462                 numbers (cdr cite)
463                 first (apply 'min numbers)
464                 compare (if size (length candidate) first))
465           (and (> first limit)
466                regexp
467                (string-match regexp candidate)
468                (< compare smallest)
469                (setq best cite
470                      smallest compare))))
471       (if (null best)
472           ()
473         (setq gnus-cite-loose-attribution-alist
474               (delq att gnus-cite-loose-attribution-alist))
475         (setq gnus-cite-attribution-alist 
476               (cons (cons wrote (car best)) gnus-cite-attribution-alist))
477         (if in
478             (setq gnus-cite-attribution-alist 
479                   (cons (cons in (car best)) gnus-cite-attribution-alist)))
480         (if (memq best gnus-cite-loose-prefix-alist)
481            (let ((loop gnus-cite-prefix-alist)
482                  (numbers (cdr best))
483                  current)
484              (setq gnus-cite-loose-prefix-alist
485                    (delq best gnus-cite-loose-prefix-alist))
486              (while loop
487                (setq current (car loop)
488                      loop (cdr loop))
489                (if (eq current best)
490                    ()
491                  (setcdr current (gnus-set-difference (cdr current) numbers))
492                  (if (null (cdr current))
493                      (setq gnus-cite-loose-prefix-alist
494                            (delq current gnus-cite-loose-prefix-alist)
495                            atts (delq current atts)))))))))))
496
497 (defun gnus-cite-find-loose (prefix)
498   ;; Return a list of loose attribution lines prefixed by PREFIX.
499   (let* ((atts gnus-cite-loose-attribution-alist)
500          att line lines)
501     (while atts
502       (setq att (car atts)
503             line (car att)
504             atts (cdr atts))
505       (if (string-equal (gnus-cite-find-prefix line) prefix)
506           (setq lines (cons line lines))))
507     lines))
508
509 (defun gnus-cite-add-face (number prefix face)
510   ;; At line NUMBER, ignore PREFIX and add FACE to the rest of the line.
511   (if face
512       (let ((inhibit-point-motion-hooks t)
513             from to)
514         (goto-line number)
515         (forward-char (length prefix))
516         (skip-chars-forward " \t")
517         (setq from (point))
518         (end-of-line 1)
519         (skip-chars-backward " \t")
520         (setq to (point))
521         (if (< from to)
522             (overlay-put (make-overlay from to) 'face face)))))
523
524 (defun gnus-cite-toggle (prefix)
525   (save-excursion
526     (set-buffer gnus-article-buffer)
527     (let ((buffer-read-only nil)
528           (numbers (cdr (assoc prefix gnus-cite-prefix-alist)))
529           (inhibit-point-motion-hooks t)
530           number)
531       (while numbers
532         (setq number (car numbers)
533               numbers (cdr numbers))
534         (goto-line number)
535         (cond ((get-text-property (point) 'invisible)
536                (remove-text-properties (point) (progn (forward-line 1) (point))
537                                        gnus-hidden-properties))
538               ((assq number gnus-cite-attribution-alist))
539               (t
540                (add-text-properties (point) (progn (forward-line 1) (point))
541                                     gnus-hidden-properties)))))))
542
543 (defun gnus-cite-find-prefix (line)
544   ;; Return citation prefix for LINE.
545   (let ((alist gnus-cite-prefix-alist)
546         (prefix "")
547         entry)
548     (while alist
549       (setq entry (car alist)
550             alist (cdr alist))
551       (if (memq line (cdr entry))
552           (setq prefix (car entry))))
553     prefix))
554
555 (gnus-ems-redefine)
556
557 (provide 'gnus-cite)
558
559 ;;; gnus-cite.el ends here