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