e8df3f23fbef47b1eaf438630b4de3fd18b016b2
[gnus] / lisp / gnus-cite.el
1 ;;; gnus-cite.el --- Highlight GNUS article.
2 ;; Copyright (C) 1995 Free Software Foundation, Inc.
3
4 ;; Author: Per Abrahamsen <abraham@iesd.auc.dk>
5 ;; Keywords: news, mail
6
7 ;;; Commentary:
8
9 ;;; Code:
10
11 (require 'gnus)
12 (require 'gnus-vis)
13
14 ;;; Customization:
15
16 (defvar gnus-cite-prefix-regexp "^[^\n]*[]>|:}+]"
17   "Regexp matching the longest possible citation prefix on a line.")
18
19 (defvar gnus-cite-max-prefix 20
20   "Maximal possible length for a citation prefix.")
21
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.")
28
29 (defvar gnus-supercite-secondary-regexp "^.*\"\\([^\"\n]+\\)\" +=="
30   "Regexp matching mangled SuperCite attribution lines.
31 The first regexp group should match the SuperCite attribution.")
32
33 (defvar gnus-cite-minimum-match-count 2
34   "Minimal number of identical prefix'es before we believe it is a citation.")
35
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
39 values:
40
41 dark: Create faces from `gnus-face-dark-name-list'.
42 light: Create faces from `gnus-face-light-name-list'.
43
44 The variable `gnus-make-foreground' determines whether the created
45 faces change the foreground or the background colors.")
46
47 (defvar gnus-cite-attribution-prefix "in article\\|in <"
48   "Regexp matching the beginning of an attribution line.")
49
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.")
53
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.")
57
58 (defvar gnus-cite-hide-percentage 30
59   "Only hide cited text if it is larger than this percent of the body.")
60
61 (defvar gnus-cite-hide-absolute 5
62   "Only hide cited text if there is at least this number of cited lines.")
63
64
65 ;;; Internal Variables:
66
67 (defvar gnus-cite-prefix-alist nil)
68 ;; Alist of citation prefixes.  
69 ;; The cdr is a list of lines with that prefix.
70
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.
75
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.
79
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.
87
88 ;;; Commands:
89
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'.
96
97 Text is concidered cited if at least `gnus-cite-minimum-match-count'
98 lines matches `gnus-cite-prefix-regexp' with the same prefix.  
99
100 Lines matching `gnus-cite-attribution-postfix' and perhaps
101 `gnus-cite-attribution-prefix' are concidered attribution lines."
102   (interactive)
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))))
110   (save-excursion
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.
118       (while alist
119         (setq entry (car alist)
120               alist (cdr alist)
121               prefix (car entry)
122               numbers (cdr entry)
123               face (car faces)
124               faces (or (cdr faces) gnus-cite-face-list)
125               face-alist (cons (cons prefix face) face-alist))
126         (while numbers
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)
134       (while alist
135         (setq entry (car alist)
136               alist (cdr alist)
137               number (car entry)
138               prefix (cdr entry)
139               skip (gnus-cite-find-prefix number)
140               face (cdr (assoc prefix face-alist)))
141         ;; Add attribution button.
142         (goto-line number)
143         (if (re-search-forward gnus-cite-attribution-postfix 
144                                (save-excursion (end-of-line 1) (point))
145                                t)
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)
153       (while alist
154         (setq entry (car alist)
155               alist (cdr alist)
156               number (car entry)
157               skip (gnus-cite-find-prefix number))
158         (gnus-cite-add-face number skip gnus-cite-attribution-face)))))
159
160 (defun gnus-article-hide-citation ()
161   "Hide all cited text except attribution lines.
162 See the documentation for `gnus-article-highlight-citation'."
163   (interactive)
164   (save-excursion
165     (set-buffer gnus-article-buffer)
166     (gnus-cite-parse-maybe)
167     (let ((buffer-read-only nil)
168           (alist gnus-cite-prefix-alist)
169           numbers number)
170       (while alist
171         (setq numbers (cdr (car alist))
172               alist (cdr alist))
173         (while numbers
174           (setq number (car numbers)
175                 numbers (cdr numbers))
176           (goto-line number)
177           (or (assq number gnus-cite-attribution-alist)
178               (put-text-property (point) (progn (forward-line 1) (point))
179                                  'invisible t)))))))
180
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))
189   (save-excursion
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)
197           (hiden 0)
198           total)
199       (goto-char (point-max))
200       (re-search-backward gnus-signature-separator nil t)
201       (setq total (count-lines start (point)))
202       (while atts
203         (setq hiden (+ hiden (length (cdr (assoc (cdr (car atts))
204                                                  gnus-cite-prefix-alist))))
205               atts (cdr atts)))
206       (if (or force
207               (and (> (* 100 hiden) (* gnus-cite-hide-percentage total))
208                    (> hiden gnus-cite-hide-absolute)))
209           (progn
210             (setq atts gnus-cite-attribution-alist)
211             (while atts
212               (setq total (cdr (assoc (cdr (car atts)) gnus-cite-prefix-alist))
213                     atts (cdr atts))
214               (while total
215                 (setq hiden (car total)
216                       total (cdr total))
217                 (goto-line hiden)
218                 (or (assq hiden gnus-cite-attribution-alist)
219                     (put-text-property (point) (progn (forward-line 1) (point))
220                                        'invisible t)))))))))
221
222 ;;; Internal functions:
223
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)))
227       ()
228     (setq gnus-article-length (- (point-max) (point-min)))
229     (gnus-cite-parse)))
230
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))))
241         (case-fold-search t)
242         (max (save-excursion
243                (goto-char (point-max))
244                (re-search-backward gnus-signature-separator nil t)
245                (point)))
246         alist entry prefix start begin end numbers)
247     ;; Get all potential prefixes in `alist'.
248     (while (< (point) max)
249       ;; Each line.
250       (setq begin (point)
251             end (progn (beginning-of-line 2) (point))
252             start end)
253       (goto-char begin)
254       ;; Ignore standard SuperCite attribution prefix.
255       (if (looking-at gnus-supercite-regexp)
256           (if (match-end 1)
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)
263         ;; Each prefix.
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))
268         (if entry 
269             (setcdr entry (cons line (cdr entry)))
270           (setq alist (cons (list prefix line) alist)))
271         (goto-char begin))
272       (goto-char start)
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))))))
280     (while alist
281       (setq entry (car alist)
282             prefix (car entry)
283             numbers (cdr entry)
284             alist (cdr alist))
285       (cond ((null numbers)
286              ;; No lines with this prefix that wasn't also part of
287              ;; a longer prefix.
288              )
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
293              ;; prefixes. 
294              (setq gnus-cite-prefix-alist
295                    (cons entry gnus-cite-prefix-alist)))
296             (t
297              (setq gnus-cite-prefix-alist (cons entry gnus-cite-prefix-alist))
298              ;; Remove articles from other prefixes.
299              (let ((loop alist)
300                    current)
301                (while loop
302                  (setq current (car loop)
303                        loop (cdr loop))
304                  (setcdr current 
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))
308
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))
314            (end (match-end 0))
315            (wrote (count-lines (point-min) end))
316            (prefix (gnus-cite-find-prefix wrote))
317            ;; Check previous line for an attribution leader.
318            (tag (progn
319                   (beginning-of-line 1)
320                   (and (looking-at gnus-supercite-secondary-regexp)
321                        (buffer-substring (match-beginning 1)
322                                          (match-end 1)))))
323            (in (progn
324                  (goto-char start)
325                  (and (re-search-backward gnus-cite-attribution-prefix
326                                           (save-excursion
327                                             (beginning-of-line 0)
328                                             (point))
329                                           t)
330                       (not (re-search-forward gnus-cite-attribution-postfix
331                                               start t))
332                       (count-lines (point-min) (1+ (point)))))))
333       (if (eq wrote in)
334           (setq in nil))
335       (goto-char end)
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
341    (lambda (prefix tag)
342      (if tag
343          (concat "\\`" (regexp-quote prefix) "[ \t]*" 
344                  (regexp-quote tag) ">"))))
345   ;; Find loose supercite citations after attributions.
346   (gnus-cite-match-attributions 'small t
347    (lambda (prefix tag)
348      (if tag (concat "\\<" (regexp-quote tag) "\\>"))))
349   ;; Find loose supercite citations anywhere.
350   (gnus-cite-match-attributions 'small nil
351    (lambda (prefix tag)
352      (if tag (concat "\\<" (regexp-quote tag) "\\>"))))
353   ;; Find nested citations after attributions.
354   (gnus-cite-match-attributions 'small-if-unique t
355    (lambda (prefix tag)
356      (concat "\\`" (regexp-quote prefix) ".+")))
357   ;; Find nested citations anywhere.
358   (gnus-cite-match-attributions 'small nil
359    (lambda (prefix tag)
360      (concat "\\`" (regexp-quote prefix) ".+")))
361   ;; Remove loose prefixes with too few lines.
362   (let ((alist gnus-cite-loose-prefix-alist)
363         entry prefix)
364     (while alist
365       (setq entry (car alist)
366             alist (cdr 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))
376
377 (defun gnus-cite-match-attributions (sort after fun)
378   ;; Match all loose attributions and citations (SORT AFTER FUN) .
379   ;;
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.
385   ;;
386   ;; If AFTER is non-nil, only citations after the attribution line
387   ;; will be concidered.
388   ;;
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
392   ;; concidered. 
393   ;; 
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)
398         (case-fold-search t)
399         att wrote in prefix tag regexp limit smallest best size aprefix)
400     (while atts
401       (setq att (car atts)
402             atts (cdr atts)
403             wrote (nth 0 att)
404             in (nth 1 att)
405             prefix (nth 2 att)
406             tag (nth 3 att)
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)
412             smallest 1000000                   
413             best nil)
414       (let ((cites gnus-cite-loose-prefix-alist)
415             cite candidate numbers first compare)
416         (while cites
417           (setq cite (car cites)
418                 cites (cdr cites)
419                 candidate (car cite)
420                 numbers (cdr cite)
421                 first (apply 'min numbers)
422                 compare (if size (length candidate) first))
423           (and (> first limit)
424                regexp
425                (string-match regexp candidate)
426                (< compare smallest)
427                (setq best cite
428                      smallest compare))))
429       (if (null best)
430           ()
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))
435         (if in
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)
440                  (numbers (cdr best))
441                  current)
442              (setq gnus-cite-loose-prefix-alist
443                    (delq best gnus-cite-loose-prefix-alist))
444              (while loop
445                (setq current (car loop)
446                      loop (cdr loop))
447                (if (eq current best)
448                    ()
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)))))))))))
454
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)
459     (while atts
460       (setq att (car atts)
461             line (car att)
462             atts (cdr atts))
463       (if (string-equal (gnus-cite-find-prefix line) prefix)
464           (setq lines (cons line lines))))
465     lines))
466
467 (defun gnus-cite-add-face (number prefix face)
468   ;; At line NUMBER, ignore PREFIX and add FACE to the rest of the line.
469   (if face
470       (let (from to)
471         (goto-line number)
472         (forward-char (length prefix))
473         (skip-chars-forward " \t")
474         (setq from (point))
475         (end-of-line 1)
476         (skip-chars-backward " \t")
477         (setq to (point))
478         (if (< from to)
479             (overlay-put (make-overlay from to) 'face face)))))
480
481 (defun gnus-cite-toggle (prefix)
482   (save-excursion
483     (set-buffer gnus-article-buffer)
484     (let ((buffer-read-only nil)
485           (numbers (cdr (assoc prefix gnus-cite-prefix-alist)))
486           number)
487       (while numbers
488         (setq number (car numbers)
489               numbers (cdr numbers))
490         (goto-line number)
491         (cond ((get-text-property (point) 'invisible)
492                (put-text-property (point) (progn (forward-line 1) (point))
493                                   'invisible nil))
494               ((assq number gnus-cite-attribution-alist))
495               (t
496                (put-text-property (point) (progn (forward-line 1) (point))
497                                   'invisible t)))))))
498
499 (defun gnus-cite-find-prefix (line)
500   ;; Return citation prefix for LINE.
501   (let ((alist gnus-cite-prefix-alist)
502         (prefix "")
503         entry)
504     (while alist
505       (setq entry (car alist)
506             alist (cdr alist))
507       (if (memq line (cdr entry))
508           (setq prefix (car entry))))
509     prefix))
510
511 (provide 'gnus-cite)
512
513 ;;; gnus-cite.el ends here