Feeding back from `t-gnus-6_14' into `pgnus-ichikawa'.
[elisp/gnus.git-] / lisp / gnus-cite.el
1 ;;; gnus-cite.el --- parse citations in articles for Gnus  -*- coding: iso-latin-1 -*-
2
3 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000
4 ;;        Free Software Foundation, Inc.
5
6 ;; Author: Per Abhiddenware
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (eval-when-compile (require 'cl))
30 (eval-when-compile (require 'static))
31
32 (require 'gnus)
33 (require 'gnus-art)
34 (require 'gnus-range)
35
36 ;;; Customization:
37
38 (defgroup gnus-cite nil
39   "Citation."
40   :prefix "gnus-cite-"
41   :link '(custom-manual "(gnus)Article Highlighting")
42   :group 'gnus-article)
43
44 (defcustom gnus-cite-reply-regexp
45   "^\\(Subject: Re\\|In-Reply-To\\|References\\):"
46   "*If headers match this regexp it is reasonable to believe that
47 article has citations."
48   :group 'gnus-cite
49   :type 'string)
50
51 (defcustom gnus-cite-always-check nil
52   "Check article always for citations.  Set it t to check all articles."
53   :group 'gnus-cite
54   :type '(choice (const :tag "no" nil)
55                  (const :tag "yes" t)))
56
57 (defcustom gnus-cited-opened-text-button-line-format "%(%{[-]%}%)\n"
58   "Format of opened cited text buttons."
59   :group 'gnus-cite
60   :type 'string)
61
62 (defcustom gnus-cited-closed-text-button-line-format "%(%{[+]%}%)\n"
63   "Format of closed cited text buttons."
64   :group 'gnus-cite
65   :type 'string)
66
67 (defcustom gnus-cited-lines-visible nil
68   "The number of lines of hidden cited text to remain visible.
69 Or a pair (cons) of numbers which are the number of lines at the top
70 and bottom of the text, respectively, to remain visible."
71   :group 'gnus-cite
72   :type '(choice (const :tag "none" nil)
73                  integer
74                  (cons :tag "Top and Bottom" integer integer)))
75
76 (defcustom gnus-cite-parse-max-size 25000
77   "Maximum article size (in bytes) where parsing citations is allowed.
78 Set it to nil to parse all articles."
79   :group 'gnus-cite
80   :type '(choice (const :tag "all" nil)
81                  integer))
82
83 (defcustom gnus-cite-prefix-regexp
84   "^[]>»|:}+ ]*[]>»|:}+]\\(.*>»\\)?\\|^.*>"
85   "*Regexp matching the longest possible citation prefix on a line."
86   :group 'gnus-cite
87   :type 'regexp)
88
89 (defcustom gnus-cite-max-prefix 20
90   "Maximum possible length for a citation prefix."
91   :group 'gnus-cite
92   :type 'integer)
93
94 (defcustom gnus-supercite-regexp
95   (concat "^\\(" gnus-cite-prefix-regexp "\\)? *"
96           ">>>>> +\"\\([^\"\n]+\\)\" +==")
97   "*Regexp matching normal Supercite attribution lines.
98 The first grouping must match prefixes added by other packages."
99   :group 'gnus-cite
100   :type 'regexp)
101
102 (defcustom gnus-supercite-secondary-regexp "^.*\"\\([^\"\n]+\\)\" +=="
103   "Regexp matching mangled Supercite attribution lines.
104 The first regexp group should match the Supercite attribution."
105   :group 'gnus-cite
106   :type 'regexp)
107
108 (defcustom gnus-cite-minimum-match-count 2
109   "Minimum number of identical prefixes before we believe it's a citation."
110   :group 'gnus-cite
111   :type 'integer)
112
113 (defcustom gnus-cite-attribution-prefix
114   "In article\\|in <\\|On \\(Mon\\|Tue\\|Wed\\|Thu\\|Fri\\|Sat\\|Sun\\),\\|-----Original Message-----"
115   "*Regexp matching the beginning of an attribution line."
116   :group 'gnus-cite
117   :type 'regexp)
118
119 (defcustom gnus-cite-attribution-suffix
120   "\\(\\(wrote\\|writes\\|said\\|says\\|>\\)\\(:\\|\\.\\.\\.\\)\\|-----Original Message-----\\)[ \t]*$"
121   "*Regexp matching the end of an attribution line.
122 The text matching the first grouping will be used as a button."
123   :group 'gnus-cite
124   :type 'regexp)
125
126 (defface gnus-cite-attribution-face '((t
127                                        (:italic t)))
128   "Face used for attribution lines.")
129
130 (defcustom gnus-cite-attribution-face 'gnus-cite-attribution-face
131   "Face used for attribution lines.
132 It is merged with the face for the cited text belonging to the attribution."
133   :group 'gnus-cite
134   :type 'face)
135
136 (defface gnus-cite-face-1 '((((class color)
137                               (background dark))
138                              (:foreground "light blue"))
139                             (((class color)
140                               (background light))
141                              (:foreground "MidnightBlue"))
142                             (t
143                              (:italic t)))
144   "Citation face.")
145
146 (defface gnus-cite-face-2 '((((class color)
147                               (background dark))
148                              (:foreground "light cyan"))
149                             (((class color)
150                               (background light))
151                              (:foreground "firebrick"))
152                             (t
153                              (:italic t)))
154   "Citation face.")
155
156 (defface gnus-cite-face-3 '((((class color)
157                               (background dark))
158                              (:foreground "light yellow"))
159                             (((class color)
160                               (background light))
161                              (:foreground "dark green"))
162                             (t
163                              (:italic t)))
164   "Citation face.")
165
166 (defface gnus-cite-face-4 '((((class color)
167                               (background dark))
168                              (:foreground "light pink"))
169                             (((class color)
170                               (background light))
171                              (:foreground "OrangeRed"))
172                             (t
173                              (:italic t)))
174   "Citation face.")
175
176 (defface gnus-cite-face-5 '((((class color)
177                               (background dark))
178                              (:foreground "pale green"))
179                             (((class color)
180                               (background light))
181                              (:foreground "dark khaki"))
182                             (t
183                              (:italic t)))
184   "Citation face.")
185
186 (defface gnus-cite-face-6 '((((class color)
187                               (background dark))
188                              (:foreground "beige"))
189                             (((class color)
190                               (background light))
191                              (:foreground "dark violet"))
192                             (t
193                              (:italic t)))
194   "Citation face.")
195
196 (defface gnus-cite-face-7 '((((class color)
197                               (background dark))
198                              (:foreground "orange"))
199                             (((class color)
200                               (background light))
201                              (:foreground "SteelBlue4"))
202                             (t
203                              (:italic t)))
204   "Citation face.")
205
206 (defface gnus-cite-face-8 '((((class color)
207                               (background dark))
208                              (:foreground "magenta"))
209                             (((class color)
210                               (background light))
211                              (:foreground "magenta"))
212                             (t
213                              (:italic t)))
214   "Citation face.")
215
216 (defface gnus-cite-face-9 '((((class color)
217                               (background dark))
218                              (:foreground "violet"))
219                             (((class color)
220                               (background light))
221                              (:foreground "violet"))
222                             (t
223                              (:italic t)))
224   "Citation face.")
225
226 (defface gnus-cite-face-10 '((((class color)
227                                (background dark))
228                               (:foreground "medium purple"))
229                              (((class color)
230                                (background light))
231                               (:foreground "medium purple"))
232                              (t
233                               (:italic t)))
234   "Citation face.")
235
236 (defface gnus-cite-face-11 '((((class color)
237                                (background dark))
238                               (:foreground "turquoise"))
239                              (((class color)
240                                (background light))
241                               (:foreground "turquoise"))
242                              (t
243                               (:italic t)))
244   "Citation face.")
245
246 (defcustom gnus-cite-face-list
247   '(gnus-cite-face-1 gnus-cite-face-2 gnus-cite-face-3 gnus-cite-face-4
248                      gnus-cite-face-5 gnus-cite-face-6 gnus-cite-face-7 gnus-cite-face-8
249                      gnus-cite-face-9 gnus-cite-face-10 gnus-cite-face-11)
250   "*List of faces used for highlighting citations.
251
252 When there are citations from multiple articles in the same message,
253 Gnus will try to give each citation from each article its own face.
254 This should make it easier to see who wrote what."
255   :group 'gnus-cite
256   :type '(repeat face))
257
258 (defcustom gnus-cite-hide-percentage 50
259   "Only hide excess citation if above this percentage of the body."
260   :group 'gnus-cite
261   :type 'number)
262
263 (defcustom gnus-cite-hide-absolute 10
264   "Only hide excess citation if above this number of lines in the body."
265   :group 'gnus-cite
266   :type 'integer)
267
268 ;;; Internal Variables:
269
270 (defvar gnus-cite-article nil)
271 (defvar gnus-cite-overlay-list nil)
272
273 (defvar gnus-cite-prefix-alist nil)
274 ;; Alist of citation prefixes.
275 ;; The cdr is a list of lines with that prefix.
276
277 (defvar gnus-cite-attribution-alist nil)
278 ;; Alist of attribution lines.
279 ;; The car is a line number.
280 ;; The cdr is the prefix for the citation started by that line.
281
282 (defvar gnus-cite-loose-prefix-alist nil)
283 ;; Alist of citation prefixes that have no matching attribution.
284 ;; The cdr is a list of lines with that prefix.
285
286 (defvar gnus-cite-loose-attribution-alist nil)
287 ;; Alist of attribution lines that have no matching citation.
288 ;; Each member has the form (WROTE IN PREFIX TAG), where
289 ;; WROTE: is the attribution line number
290 ;; IN: is the line number of the previous line if part of the same attribution,
291 ;; PREFIX: Is the citation prefix of the attribution line(s), and
292 ;; TAG: Is a Supercite tag, if any.
293
294 (defvar gnus-cited-opened-text-button-line-format-alist
295   `((?b (marker-position beg) ?d)
296     (?e (marker-position end) ?d)
297     (?n (count-lines beg end) ?d)
298     (?l (- end beg) ?d)))
299 (defvar gnus-cited-opened-text-button-line-format-spec nil)
300 (defvar gnus-cited-closed-text-button-line-format-alist
301   gnus-cited-opened-text-button-line-format-alist)
302 (defvar gnus-cited-closed-text-button-line-format-spec nil)
303
304
305 ;;; Commands:
306
307 (defun gnus-article-highlight-citation (&optional force)
308   "Highlight cited text.
309 Each citation in the article will be highlighted with a different face.
310 The faces are taken from `gnus-cite-face-list'.
311 Attribution lines are highlighted with the same face as the
312 corresponding citation merged with `gnus-cite-attribution-face'.
313
314 Text is considered cited if at least `gnus-cite-minimum-match-count'
315 lines matches `gnus-cite-prefix-regexp' with the same prefix.
316
317 Lines matching `gnus-cite-attribution-suffix' and perhaps
318 `gnus-cite-attribution-prefix' are considered attribution lines."
319   (interactive (list 'force))
320   (save-excursion
321     (set-buffer gnus-article-buffer)
322     (gnus-cite-parse-maybe force)
323     (let ((buffer-read-only nil)
324           (alist gnus-cite-prefix-alist)
325           (faces gnus-cite-face-list)
326           (inhibit-point-motion-hooks t)
327           face entry prefix skip numbers number face-alist)
328       ;; Loop through citation prefixes.
329       (while alist
330         (setq entry (car alist)
331               alist (cdr alist)
332               prefix (car entry)
333               numbers (cdr entry)
334               face (car faces)
335               faces (or (cdr faces) gnus-cite-face-list)
336               face-alist (cons (cons prefix face) face-alist))
337         (while numbers
338           (setq number (car numbers)
339                 numbers (cdr numbers))
340           (and (not (assq number gnus-cite-attribution-alist))
341                (not (assq number gnus-cite-loose-attribution-alist))
342                (gnus-cite-add-face number prefix face))))
343       ;; Loop through attribution lines.
344       (setq alist gnus-cite-attribution-alist)
345       (while alist
346         (setq entry (car alist)
347               alist (cdr alist)
348               number (car entry)
349               prefix (cdr entry)
350               skip (gnus-cite-find-prefix number)
351               face (cdr (assoc prefix face-alist)))
352         ;; Add attribution button.
353         (goto-char (point-min))
354         (forward-line (1- number))
355         (when (re-search-forward gnus-cite-attribution-suffix
356                                  (save-excursion (end-of-line 1) (point))
357                                  t)
358           (gnus-article-add-button (match-beginning 1) (match-end 1)
359                                    'gnus-cite-toggle prefix))
360         ;; Highlight attribution line.
361         (gnus-cite-add-face number skip face)
362         (gnus-cite-add-face number skip gnus-cite-attribution-face))
363       ;; Loop through attribution lines.
364       (setq alist gnus-cite-loose-attribution-alist)
365       (while alist
366         (setq entry (car alist)
367               alist (cdr alist)
368               number (car entry)
369               skip (gnus-cite-find-prefix number))
370         (gnus-cite-add-face number skip gnus-cite-attribution-face)))))
371
372 (defun gnus-dissect-cited-text ()
373   "Dissect the article buffer looking for cited text."
374   (save-excursion
375     (set-buffer gnus-article-buffer)
376     (gnus-cite-parse-maybe nil t)
377     (let ((alist gnus-cite-prefix-alist)
378           prefix numbers number marks m)
379       ;; Loop through citation prefixes.
380       (while alist
381         (setq numbers (pop alist)
382               prefix (pop numbers))
383         (while numbers
384           (setq number (pop numbers))
385           (goto-char (point-min))
386           (forward-line number)
387           (push (cons (point-marker) "") marks)
388           (while (and numbers
389                       (= (1- number) (car numbers)))
390             (setq number (pop numbers)))
391           (goto-char (point-min))
392           (forward-line (1- number))
393           (push (cons (point-marker) prefix) marks)))
394       ;; Skip to the beginning of the body.
395       (article-goto-body)
396       (push (cons (point-marker) "") marks)
397       ;; Find the end of the body.
398       (goto-char (point-max))
399       (gnus-article-search-signature)
400       (push (cons (point-marker) "") marks)
401       ;; Sort the marks.
402       (setq marks (sort marks 'car-less-than-car))
403       (let ((omarks marks))
404         (setq marks nil)
405         (while (cdr omarks)
406           (if (= (caar omarks) (caadr omarks))
407               (progn
408                 (unless (equal (cdar omarks) "")
409                   (push (car omarks) marks))
410                 (unless (equal (cdadr omarks) "")
411                   (push (cadr omarks) marks))
412                 (unless (and (equal (cdar omarks) "")
413                              (equal (cdadr omarks) "")
414                              (not (cddr omarks)))
415                   (setq omarks (cdr omarks))))
416             (push (car omarks) marks))
417           (setq omarks (cdr omarks)))
418         (when (car omarks)
419           (push (car omarks) marks))
420         (setq marks (setq m (nreverse marks)))
421         (while (cddr m)
422           (if (and (equal (cdadr m) "")
423                    (equal (cdar m) (cdaddr m))
424                    (goto-char (caadr m))
425                    (forward-line 1)
426                    (= (point) (caaddr m)))
427               (setcdr m (cdddr m))
428             (setq m (cdr m))))
429         marks))))
430
431 (defun gnus-article-fill-cited-article (&optional force width)
432   "Do word wrapping in the current article.
433 If WIDTH (the numerical prefix), use that text width when filling."
434   (interactive (list t current-prefix-arg))
435   (save-excursion
436     (set-buffer gnus-article-buffer)
437     (let ((buffer-read-only nil)
438           (inhibit-point-motion-hooks t)
439           (marks (gnus-dissect-cited-text))
440           (adaptive-fill-mode nil)
441           (filladapt-mode nil)
442           (fill-column (if width (prefix-numeric-value width) fill-column)))
443       (save-restriction
444         (while (cdr marks)
445           (narrow-to-region (caar marks) (caadr marks))
446           (let ((adaptive-fill-regexp
447                  (concat "^" (regexp-quote (cdar marks)) " *"))
448                 (fill-prefix (cdar marks)))
449             (fill-region (point-min) (point-max)))
450           (set-marker (caar marks) nil)
451           (setq marks (cdr marks)))
452         (when marks
453           (set-marker (caar marks) nil))
454         ;; All this information is now incorrect.
455         (setq gnus-cite-prefix-alist nil
456               gnus-cite-attribution-alist nil
457               gnus-cite-loose-prefix-alist nil
458               gnus-cite-loose-attribution-alist nil
459               gnus-cite-article nil)))))
460
461 (defun gnus-article-hide-citation (&optional arg force)
462   "Toggle hiding of all cited text except attribution lines.
463 See the documentation for `gnus-article-highlight-citation'.
464 If given a negative prefix, always show; if given a positive prefix,
465 always hide."
466   (interactive (append (gnus-article-hidden-arg) (list 'force)))
467   (gnus-set-format 'cited-opened-text-button t)
468   (gnus-set-format 'cited-closed-text-button t)
469   (save-excursion
470     (set-buffer gnus-article-buffer)
471       (let ((buffer-read-only nil)
472             marks
473             (inhibit-point-motion-hooks t)
474             (props (nconc (list 'article-type 'cite)
475                           gnus-hidden-properties))
476             (point (point-min))
477             found beg end start)
478         (while (setq point 
479                      (text-property-any point (point-max) 
480                                         'gnus-callback
481                                         'gnus-article-toggle-cited-text))
482           (setq found t)
483           (goto-char point)
484           (gnus-article-toggle-cited-text
485            (get-text-property point 'gnus-data) arg)
486           (forward-line 1)
487           (setq point (point)))
488         (unless found
489           (setq marks (gnus-dissect-cited-text))
490           (while marks
491             (setq beg nil
492                   end nil)
493             (while (and marks (string= (cdar marks) ""))
494               (setq marks (cdr marks)))
495             (when marks
496               (setq beg (caar marks)))
497             (while (and marks (not (string= (cdar marks) "")))
498               (setq marks (cdr marks)))
499             (when marks
500             (setq end (caar marks)))
501             ;; Skip past lines we want to leave visible.
502             (when (and beg end gnus-cited-lines-visible)
503               (goto-char beg)
504               (forward-line (if (consp gnus-cited-lines-visible)
505                                 (car gnus-cited-lines-visible)
506                               gnus-cited-lines-visible))
507               (if (>= (point) end)
508                   (setq beg nil)
509                 (setq beg (point-marker))
510                 (when (consp gnus-cited-lines-visible)
511                   (goto-char end)
512                   (forward-line (- (cdr gnus-cited-lines-visible)))
513                   (if (<= (point) beg)
514                       (setq beg nil)
515                   (setq end (point-marker))))))
516             (when (and beg end)
517               ;; We use markers for the end-points to facilitate later
518               ;; wrapping and mangling of text.
519               (setq beg (set-marker (make-marker) beg)
520                     end (set-marker (make-marker) end))
521               (gnus-add-text-properties-when 'article-type nil beg end props)
522               (goto-char beg)
523               (unless (save-excursion (search-backward "\n\n" nil t))
524                 (insert "\n"))
525               (put-text-property
526                (setq start (point-marker))
527                (progn
528                (gnus-article-add-button
529                 (point)
530                 (progn (eval gnus-cited-closed-text-button-line-format-spec)
531                        (point))
532                 `gnus-article-toggle-cited-text
533                 (list (cons beg end) start))
534                (point))
535                'article-type 'annotation)
536               (set-marker beg (point))))))))
537
538 (defun gnus-article-toggle-cited-text (args &optional arg)
539   "Toggle hiding the text in REGION.
540 ARG can be nil or a number.  Positive means hide, negative
541 means show, nil means toggle."
542   (let* ((region (car args))
543          (beg (car region))
544          (end (cdr region))
545          (start (cadr args))
546          (hidden
547           (text-property-any beg (1- end) 'article-type 'cite))
548          (inhibit-point-motion-hooks t)
549          buffer-read-only)
550     (when (or (null arg)
551               (zerop arg)
552               (and (> arg 0) (not hidden))
553               (and (< arg 0) hidden))
554       (if hidden
555           (gnus-remove-text-properties-when
556            'article-type 'cite beg end 
557            (cons 'article-type (cons 'cite
558                                      gnus-hidden-properties)))
559         (gnus-add-text-properties-when
560          'article-type nil beg end 
561          (cons 'article-type (cons 'cite
562                                    gnus-hidden-properties))))
563       (save-excursion
564         (goto-char start)
565         (gnus-delete-line)
566         (put-text-property
567          (point)
568          (progn
569            (gnus-article-add-button
570             (point)
571             (progn (eval
572                     (if hidden
573                         gnus-cited-opened-text-button-line-format-spec
574                       gnus-cited-closed-text-button-line-format-spec))
575                    (point))
576             `gnus-article-toggle-cited-text
577             args)
578            (point))
579          'article-type 'annotation)))))
580
581 (defun gnus-article-hide-citation-maybe (&optional arg force)
582   "Toggle hiding of cited text that has an attribution line.
583 If given a negative prefix, always show; if given a positive prefix,
584 always hide.
585 This will do nothing unless at least `gnus-cite-hide-percentage'
586 percent and at least `gnus-cite-hide-absolute' lines of the body is
587 cited text with attributions.  When called interactively, these two
588 variables are ignored.
589 See also the documentation for `gnus-article-highlight-citation'."
590   (interactive (append (gnus-article-hidden-arg) '(force)))
591   (unless (gnus-article-check-hidden-text 'cite arg)
592     (save-excursion
593       (set-buffer gnus-article-buffer)
594       (gnus-cite-parse-maybe force)
595       (article-goto-body)
596       (let ((start (point))
597             (atts gnus-cite-attribution-alist)
598             (buffer-read-only nil)
599             (inhibit-point-motion-hooks t)
600             (hidden 0)
601             total)
602         (goto-char (point-max))
603         (gnus-article-search-signature)
604         (setq total (count-lines start (point)))
605         (while atts
606           (setq hidden (+ hidden (length (cdr (assoc (cdar atts)
607                                                      gnus-cite-prefix-alist))))
608                 atts (cdr atts)))
609         (when (or force
610                   (and (> (* 100 hidden) (* gnus-cite-hide-percentage total))
611                        (> hidden gnus-cite-hide-absolute)))
612           (setq atts gnus-cite-attribution-alist)
613           (while atts
614             (setq total (cdr (assoc (cdar atts) gnus-cite-prefix-alist))
615                   atts (cdr atts))
616             (while total
617               (setq hidden (car total)
618                     total (cdr total))
619               (goto-char (point-min))
620               (forward-line (1- hidden))
621               (unless (assq hidden gnus-cite-attribution-alist)
622                 (gnus-add-text-properties
623                  (point) (progn (forward-line 1) (point))
624                  (nconc (list 'article-type 'cite)
625                         gnus-hidden-properties))))))))))
626
627 (defun gnus-article-hide-citation-in-followups ()
628   "Hide cited text in non-root articles."
629   (interactive)
630   (save-excursion
631     (set-buffer gnus-article-buffer)
632     (let ((article (cdr gnus-article-current)))
633       (unless (save-excursion
634                 (set-buffer gnus-summary-buffer)
635                 (gnus-article-displayed-root-p article))
636         (gnus-article-hide-citation)))))
637
638 ;;; Internal functions:
639
640 (defun gnus-cite-parse-maybe (&optional force no-overlay)
641   "Always parse the buffer."
642   (gnus-cite-localize)
643   ;;Reset parser information.
644   (setq gnus-cite-prefix-alist nil
645         gnus-cite-attribution-alist nil
646         gnus-cite-loose-prefix-alist nil
647         gnus-cite-loose-attribution-alist nil)
648   (unless no-overlay
649     (gnus-cite-delete-overlays))
650   ;; Parse if not too large.
651   (if (and gnus-cite-parse-max-size
652            (> (buffer-size) gnus-cite-parse-max-size))
653       ()
654     (setq gnus-cite-article (cons (car gnus-article-current)
655                                   (cdr gnus-article-current)))
656     (gnus-cite-parse-wrapper)))
657
658 (defun gnus-cite-delete-overlays ()
659   (dolist (overlay gnus-cite-overlay-list)
660     (when (or (not (gnus-overlay-end overlay))
661               (and (>= (gnus-overlay-end overlay) (point-min))
662                    (<= (gnus-overlay-end overlay) (point-max))))
663       (setq gnus-cite-overlay-list (delete overlay gnus-cite-overlay-list))
664       (gnus-delete-overlay overlay))))
665
666 (defun gnus-cite-parse-wrapper ()
667   ;; Wrap chopped gnus-cite-parse.
668   (article-goto-body)
669   (let ((inhibit-point-motion-hooks t))
670     (save-excursion
671       (gnus-cite-parse-attributions))
672     (save-excursion
673       (gnus-cite-parse))
674     (save-excursion
675       (gnus-cite-connect-attributions))))
676
677 (defun gnus-cite-parse ()
678   ;; Parse and connect citation prefixes and attribution lines.
679
680   ;; Parse current buffer searching for citation prefixes.
681   (let ((line (1+ (count-lines (point-min) (point))))
682         (case-fold-search t)
683         (max (save-excursion
684                (goto-char (point-max))
685                (gnus-article-search-signature)
686                (point)))
687         (prefix-regexp (concat "^\\(" gnus-cite-prefix-regexp "\\)"))
688         alist entry start begin end numbers prefix guess-limit mc-flag)
689     ;; Get all potential prefixes in `alist'.
690     (while (< (point) max)
691       ;; Each line.
692       (setq begin (point)
693             guess-limit (progn (skip-chars-forward "^> \t\r\n") (point))
694             end (progn (beginning-of-line 2) (point))
695             start end)
696       (goto-char begin)
697       ;; Ignore standard Supercite attribution prefix.
698       (when (and (< guess-limit (+ begin gnus-cite-max-prefix))
699                  (looking-at gnus-supercite-regexp))
700         (if (match-end 1)
701             (setq end (1+ (match-end 1)))
702           (setq end (1+ begin))))
703       ;; Ignore very long prefixes.
704       (when (> end (+ begin gnus-cite-max-prefix))
705         (setq end (+ begin gnus-cite-max-prefix)))
706       (while (re-search-forward prefix-regexp (1- end) t)
707         ;; Each prefix.
708         (setq end (match-end 0)
709               prefix (buffer-substring begin end))
710         (gnus-set-text-properties 0 (length prefix) nil prefix)
711         (setq entry (assoc prefix alist))
712         (if entry
713             (setcdr entry (cons line (cdr entry)))
714           (push (list prefix line) alist))
715         (goto-char begin))
716       (goto-char start)
717       (setq line (1+ line)))
718     ;; We got all the potential prefixes.  Now create
719     ;; `gnus-cite-prefix-alist' containing the oldest prefix for each
720     ;; line that appears at least gnus-cite-minimum-match-count
721     ;; times.  First sort them by length.  Longer is older.
722     (setq alist (sort alist (lambda (a b)
723                               (> (length (car a)) (length (car b))))))
724     (while alist
725       (setq entry (car alist)
726             prefix (car entry)
727             numbers (cdr entry)
728             alist (cdr alist))
729       (cond ((null numbers)
730              ;; No lines with this prefix that wasn't also part of
731              ;; a longer prefix.
732              )
733             ((< (length numbers) gnus-cite-minimum-match-count)
734              ;; Too few lines with this prefix.  We keep it a bit
735              ;; longer in case it is an exact match for an attribution
736              ;; line, but we don't remove the line from other
737              ;; prefixes.
738              (push entry gnus-cite-prefix-alist))
739             (t
740              (push entry
741                    gnus-cite-prefix-alist)
742              ;; Remove articles from other prefixes.
743              (let ((loop alist)
744                    current)
745                (while loop
746                  (setq current (car loop)
747                        loop (cdr loop))
748                  (setcdr current
749                          (gnus-set-difference (cdr current) numbers)))))))))
750
751 (defun gnus-cite-parse-attributions ()
752   (let (al-alist)
753     ;; Parse attributions
754     (while (re-search-forward gnus-cite-attribution-suffix (point-max) t)
755       (let* ((start (match-beginning 0))
756              (end (match-end 0))
757              (wrote (count-lines (point-min) end))
758              (prefix (gnus-cite-find-prefix wrote))
759              ;; Check previous line for an attribution leader.
760              (tag (progn
761                     (beginning-of-line 1)
762                     (when (looking-at gnus-supercite-secondary-regexp)
763                       (buffer-substring (match-beginning 1)
764                                         (match-end 1)))))
765              (in (progn
766                    (goto-char start)
767                    (and (re-search-backward gnus-cite-attribution-prefix
768                                             (save-excursion
769                                               (beginning-of-line 0)
770                                               (point))
771                                             t)
772                         (not (re-search-forward gnus-cite-attribution-suffix
773                                                 start t))
774                         (count-lines (point-min) (1+ (point)))))))
775         (when (eq wrote in)
776           (setq in nil))
777         (goto-char end)
778         ;; don't add duplicates
779         (let ((al (buffer-substring (save-excursion (beginning-of-line 0)
780                                                     (1+ (point)))
781                                     end)))
782           (if (not (assoc al al-alist))
783               (progn
784                 (push (list wrote in prefix tag)
785                       gnus-cite-loose-attribution-alist)
786                 (push (cons al t) al-alist))))))))
787
788 (defun gnus-cite-connect-attributions ()
789   ;; Connect attributions to citations
790
791   ;; No citations have been connected to attribution lines yet.
792   (setq gnus-cite-loose-prefix-alist (append gnus-cite-prefix-alist nil))
793
794   ;; Parse current buffer searching for attribution lines.
795   ;; Find exact supercite citations.
796   (gnus-cite-match-attributions 'small nil
797                                 (lambda (prefix tag)
798                                   (when tag
799                                     (concat "\\`"
800                                             (regexp-quote prefix) "[ \t]*"
801                                             (regexp-quote tag) ">"))))
802   ;; Find loose supercite citations after attributions.
803   (gnus-cite-match-attributions 'small t
804                                 (lambda (prefix tag)
805                                   (when tag
806                                     (concat "\\<"
807                                             (regexp-quote tag)
808                                             "\\>"))))
809   ;; Find loose supercite citations anywhere.
810   (gnus-cite-match-attributions 'small nil
811                                 (lambda (prefix tag)
812                                   (when tag
813                                     (concat "\\<"
814                                             (regexp-quote tag)
815                                             "\\>"))))
816   ;; Find nested citations after attributions.
817   (gnus-cite-match-attributions 'small-if-unique t
818                                 (lambda (prefix tag)
819                                   (concat "\\`" (regexp-quote prefix) ".+")))
820   ;; Find nested citations anywhere.
821   (gnus-cite-match-attributions 'small nil
822                                 (lambda (prefix tag)
823                                   (concat "\\`" (regexp-quote prefix) ".+")))
824   ;; Remove loose prefixes with too few lines.
825   (let ((alist gnus-cite-loose-prefix-alist)
826         entry)
827     (while alist
828       (setq entry (car alist)
829             alist (cdr alist))
830       (when (< (length (cdr entry)) gnus-cite-minimum-match-count)
831         (setq gnus-cite-prefix-alist
832               (delq entry gnus-cite-prefix-alist)
833               gnus-cite-loose-prefix-alist
834               (delq entry gnus-cite-loose-prefix-alist)))))
835   ;; Find flat attributions.
836   (gnus-cite-match-attributions 'first t nil)
837   ;; Find any attributions (are we getting desperate yet?).
838   (gnus-cite-match-attributions 'first nil nil))
839
840 (defun gnus-cite-match-attributions (sort after fun)
841   ;; Match all loose attributions and citations (SORT AFTER FUN) .
842   ;;
843   ;; If SORT is `small', the citation with the shortest prefix will be
844   ;; used, if it is `first' the first prefix will be used, if it is
845   ;; `small-if-unique' the shortest prefix will be used if the
846   ;; attribution line does not share its own prefix with other
847   ;; loose attribution lines, otherwise the first prefix will be used.
848   ;;
849   ;; If AFTER is non-nil, only citations after the attribution line
850   ;; will be considered.
851   ;;
852   ;; If FUN is non-nil, it will be called with the arguments (WROTE
853   ;; PREFIX TAG) and expected to return a regular expression.  Only
854   ;; citations whose prefix matches the regular expression will be
855   ;; considered.
856   ;;
857   ;; WROTE is the attribution line number.
858   ;; PREFIX is the attribution line prefix.
859   ;; TAG is the Supercite tag on the attribution line.
860   (let ((atts gnus-cite-loose-attribution-alist)
861         (case-fold-search t)
862         att wrote in prefix tag regexp limit smallest best size)
863     (while atts
864       (setq att (car atts)
865             atts (cdr atts)
866             wrote (nth 0 att)
867             in (nth 1 att)
868             prefix (nth 2 att)
869             tag (nth 3 att)
870             regexp (if fun (funcall fun prefix tag) "")
871             size (cond ((eq sort 'small) t)
872                        ((eq sort 'first) nil)
873                        (t (< (length (gnus-cite-find-loose prefix)) 2)))
874             limit (if after wrote -1)
875             smallest 1000000
876             best nil)
877       (let ((cites gnus-cite-loose-prefix-alist)
878             cite candidate numbers first compare)
879         (while cites
880           (setq cite (car cites)
881                 cites (cdr cites)
882                 candidate (car cite)
883                 numbers (cdr cite)
884                 first (apply 'min numbers)
885                 compare (if size (length candidate) first))
886           (and (> first limit)
887                regexp
888                (string-match regexp candidate)
889                (< compare smallest)
890                (setq best cite
891                      smallest compare))))
892       (if (null best)
893           ()
894         (setq gnus-cite-loose-attribution-alist
895               (delq att gnus-cite-loose-attribution-alist))
896         (push (cons wrote (car best)) gnus-cite-attribution-alist)
897         (when in
898           (push (cons in (car best)) gnus-cite-attribution-alist))
899         (when (memq best gnus-cite-loose-prefix-alist)
900           (let ((loop gnus-cite-prefix-alist)
901                 (numbers (cdr best))
902                 current)
903             (setq gnus-cite-loose-prefix-alist
904                   (delq best gnus-cite-loose-prefix-alist))
905             (while loop
906               (setq current (car loop)
907                     loop (cdr loop))
908               (if (eq current best)
909                   ()
910                 (setcdr current (gnus-set-difference (cdr current) numbers))
911                 (when (null (cdr current))
912                   (setq gnus-cite-loose-prefix-alist
913                         (delq current gnus-cite-loose-prefix-alist)
914                         atts (delq current atts)))))))))))
915
916 (defun gnus-cite-find-loose (prefix)
917   ;; Return a list of loose attribution lines prefixed by PREFIX.
918   (let* ((atts gnus-cite-loose-attribution-alist)
919          att line lines)
920     (while atts
921       (setq att (car atts)
922             line (car att)
923             atts (cdr atts))
924       (when (string-equal (gnus-cite-find-prefix line) prefix)
925         (push line lines)))
926     lines))
927
928 (defun gnus-cite-add-face (number prefix face)
929   ;; At line NUMBER, ignore PREFIX and add FACE to the rest of the line.
930   (when face
931     (let ((inhibit-point-motion-hooks t)
932           from to overlay)
933       (goto-char (point-min))
934       (when (zerop (forward-line (1- number)))
935         (static-if (or (featurep 'xemacs)
936                        (and (eq emacs-major-version 20)
937                             (>= emacs-minor-version 3))
938                        (>= emacs-major-version 21))
939             (forward-char (length prefix))
940           (move-to-column (string-width prefix)))
941         (skip-chars-forward " \t")
942         (setq from (point))
943         (end-of-line 1)
944         (skip-chars-backward " \t")
945         (setq to (point))
946         (when (< from to)
947           (push (setq overlay (gnus-make-overlay from to))
948                 gnus-cite-overlay-list)
949           (gnus-overlay-put overlay 'face face))))))
950
951 (defun gnus-cite-toggle (prefix)
952   (save-excursion
953     (set-buffer gnus-article-buffer)
954     (gnus-cite-parse-maybe nil t)
955     (let ((buffer-read-only nil)
956           (numbers (cdr (assoc prefix gnus-cite-prefix-alist)))
957           (inhibit-point-motion-hooks t)
958           number)
959       (while numbers
960         (setq number (car numbers)
961               numbers (cdr numbers))
962         (goto-char (point-min))
963         (forward-line (1- number))
964         (cond ((get-text-property (point) 'invisible)
965                (remove-text-properties (point) (progn (forward-line 1) (point))
966                                        gnus-hidden-properties))
967               ((assq number gnus-cite-attribution-alist))
968               (t
969                (gnus-add-text-properties
970                 (point) (progn (forward-line 1) (point))
971                 (nconc (list 'article-type 'cite)
972                        gnus-hidden-properties))))))))
973
974 (defun gnus-cite-find-prefix (line)
975   ;; Return citation prefix for LINE.
976   (let ((alist gnus-cite-prefix-alist)
977         (prefix "")
978         entry)
979     (while alist
980       (setq entry (car alist)
981             alist (cdr alist))
982       (when (memq line (cdr entry))
983         (setq prefix (car entry))))
984     prefix))
985
986 (defun gnus-cite-localize ()
987   "Make the citation variables local to the article buffer."
988   (let ((vars '(gnus-cite-article
989                 gnus-cite-overlay-list gnus-cite-prefix-alist
990                 gnus-cite-attribution-alist gnus-cite-loose-prefix-alist
991                 gnus-cite-loose-attribution-alist)))
992     (while vars
993       (make-local-variable (pop vars)))))
994
995 (gnus-ems-redefine)
996
997 (provide 'gnus-cite)
998
999 ;; Local Variables:
1000 ;; coding: iso-8859-1
1001 ;; End:
1002
1003 ;;; gnus-cite.el ends here