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