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