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