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