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