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