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