Cleaning up more.
[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 (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             (fill-region (point-min) (point-max)))
447           (set-marker (caar marks) nil)
448           (setq marks (cdr marks)))
449         (when marks
450           (set-marker (caar marks) nil))
451         ;; All this information is now incorrect.
452         (setq gnus-cite-prefix-alist nil
453               gnus-cite-attribution-alist nil
454               gnus-cite-loose-prefix-alist nil
455               gnus-cite-loose-attribution-alist nil
456               gnus-cite-article nil)))))
457
458 (defun gnus-article-hide-citation (&optional arg force)
459   "Toggle hiding of all cited text except attribution lines.
460 See the documentation for `gnus-article-highlight-citation'.
461 If given a negative prefix, always show; if given a positive prefix,
462 always hide."
463   (interactive (append (gnus-article-hidden-arg) (list 'force)))
464   (gnus-set-format 'cited-opened-text-button t)
465   (gnus-set-format 'cited-closed-text-button t)
466   (save-excursion
467     (set-buffer gnus-article-buffer)
468     (let ((buffer-read-only nil)
469           marks
470           (inhibit-point-motion-hooks t)
471           (props (nconc (list 'article-type 'cite)
472                         gnus-hidden-properties))
473           (point (point-min))
474           found beg end start)
475       (while (setq point
476                    (text-property-any point (point-max)
477                                       'gnus-callback
478                                       'gnus-article-toggle-cited-text))
479         (setq found t)
480         (goto-char point)
481         (gnus-article-toggle-cited-text
482          (get-text-property point 'gnus-data) arg)
483         (forward-line 1)
484         (setq point (point)))
485       (unless found
486         (setq marks (gnus-dissect-cited-text))
487         (while marks
488           (setq beg nil
489                 end nil)
490           (while (and marks (string= (cdar marks) ""))
491             (setq marks (cdr marks)))
492           (when marks
493             (setq beg (caar marks)))
494           (while (and marks (not (string= (cdar marks) "")))
495             (setq marks (cdr marks)))
496           (when marks
497             (setq end (caar marks)))
498           ;; Skip past lines we want to leave visible.
499           (when (and beg end gnus-cited-lines-visible)
500             (goto-char beg)
501             (forward-line (if (consp gnus-cited-lines-visible)
502                               (car gnus-cited-lines-visible)
503                             gnus-cited-lines-visible))
504             (if (>= (point) end)
505                 (setq beg nil)
506               (setq beg (point-marker))
507               (when (consp gnus-cited-lines-visible)
508                 (goto-char end)
509                 (forward-line (- (cdr gnus-cited-lines-visible)))
510                 (if (<= (point) beg)
511                     (setq beg nil)
512                   (setq end (point-marker))))))
513           (when (and beg end)
514             ;; We use markers for the end-points to facilitate later
515             ;; wrapping and mangling of text.
516             (setq beg (set-marker (make-marker) beg)
517                   end (set-marker (make-marker) end))
518             (gnus-add-text-properties-when 'article-type nil beg end props)
519             (goto-char beg)
520             (unless (save-excursion (search-backward "\n\n" nil t))
521               (insert "\n"))
522             (put-text-property
523              (setq start (point-marker))
524              (progn
525                (gnus-article-add-button
526                 (point)
527                 (progn (eval gnus-cited-closed-text-button-line-format-spec)
528                        (point))
529                 `gnus-article-toggle-cited-text
530                 (list (cons beg end) start))
531                (point))
532              'article-type 'annotation)
533             (set-marker beg (point))))))))
534
535 (defun gnus-article-toggle-cited-text (args &optional arg)
536   "Toggle hiding the text in REGION.
537 ARG can be nil or a number.  Positive means hide, negative
538 means show, nil means toggle."
539   (let* ((region (car args))
540          (beg (car region))
541          (end (cdr region))
542          (start (cadr args))
543          (hidden
544           (text-property-any beg (1- end) 'article-type 'cite))
545          (inhibit-point-motion-hooks t)
546          buffer-read-only)
547     (when (or (null arg)
548               (zerop arg)
549               (and (> arg 0) (not hidden))
550               (and (< arg 0) hidden))
551       (if hidden
552           (gnus-remove-text-properties-when
553            'article-type 'cite beg end
554            (cons 'article-type (cons 'cite
555                                      gnus-hidden-properties)))
556         (gnus-add-text-properties-when
557          'article-type nil beg end
558          (cons 'article-type (cons 'cite
559                                    gnus-hidden-properties))))
560       (save-excursion
561         (goto-char start)
562         (gnus-delete-line)
563         (put-text-property
564          (point)
565          (progn
566            (gnus-article-add-button
567             (point)
568             (progn (eval
569                     (if hidden
570                         gnus-cited-opened-text-button-line-format-spec
571                       gnus-cited-closed-text-button-line-format-spec))
572                    (point))
573             `gnus-article-toggle-cited-text
574             args)
575            (point))
576          'article-type 'annotation)))))
577
578 (defun gnus-article-hide-citation-maybe (&optional arg force)
579   "Toggle hiding of cited text that has an attribution line.
580 If given a negative prefix, always show; if given a positive prefix,
581 always hide.
582 This will do nothing unless at least `gnus-cite-hide-percentage'
583 percent and at least `gnus-cite-hide-absolute' lines of the body is
584 cited text with attributions.  When called interactively, these two
585 variables are ignored.
586 See also the documentation for `gnus-article-highlight-citation'."
587   (interactive (append (gnus-article-hidden-arg) '(force)))
588   (unless (gnus-article-check-hidden-text 'cite arg)
589     (save-excursion
590       (set-buffer gnus-article-buffer)
591       (gnus-cite-parse-maybe force)
592       (article-goto-body)
593       (let ((start (point))
594             (atts gnus-cite-attribution-alist)
595             (buffer-read-only nil)
596             (inhibit-point-motion-hooks t)
597             (hidden 0)
598             total)
599         (goto-char (point-max))
600         (gnus-article-search-signature)
601         (setq total (count-lines start (point)))
602         (while atts
603           (setq hidden (+ hidden (length (cdr (assoc (cdar atts)
604                                                      gnus-cite-prefix-alist))))
605                 atts (cdr atts)))
606         (when (or force
607                   (and (> (* 100 hidden) (* gnus-cite-hide-percentage total))
608                        (> hidden gnus-cite-hide-absolute)))
609           (setq atts gnus-cite-attribution-alist)
610           (while atts
611             (setq total (cdr (assoc (cdar atts) gnus-cite-prefix-alist))
612                   atts (cdr atts))
613             (while total
614               (setq hidden (car total)
615                     total (cdr total))
616               (goto-char (point-min))
617               (forward-line (1- hidden))
618               (unless (assq hidden gnus-cite-attribution-alist)
619                 (gnus-add-text-properties
620                  (point) (progn (forward-line 1) (point))
621                  (nconc (list 'article-type 'cite)
622                         gnus-hidden-properties))))))))))
623
624 (defun gnus-article-hide-citation-in-followups ()
625   "Hide cited text in non-root articles."
626   (interactive)
627   (save-excursion
628     (set-buffer gnus-article-buffer)
629     (let ((article (cdr gnus-article-current)))
630       (unless (save-excursion
631                 (set-buffer gnus-summary-buffer)
632                 (gnus-article-displayed-root-p article))
633         (gnus-article-hide-citation)))))
634
635 ;;; Internal functions:
636
637 (defun gnus-cite-parse-maybe (&optional force no-overlay)
638   "Always parse the buffer."
639   (gnus-cite-localize)
640   ;;Reset parser information.
641   (setq gnus-cite-prefix-alist nil
642         gnus-cite-attribution-alist nil
643         gnus-cite-loose-prefix-alist nil
644         gnus-cite-loose-attribution-alist nil)
645   (unless no-overlay
646     (gnus-cite-delete-overlays))
647   ;; Parse if not too large.
648   (if (and gnus-cite-parse-max-size
649            (> (buffer-size) gnus-cite-parse-max-size))
650       ()
651     (setq gnus-cite-article (cons (car gnus-article-current)
652                                   (cdr gnus-article-current)))
653     (gnus-cite-parse-wrapper)))
654
655 (defun gnus-cite-delete-overlays ()
656   (dolist (overlay gnus-cite-overlay-list)
657     (when (or (not (gnus-overlay-end overlay))
658               (and (>= (gnus-overlay-end overlay) (point-min))
659                    (<= (gnus-overlay-end overlay) (point-max))))
660       (setq gnus-cite-overlay-list (delete overlay gnus-cite-overlay-list))
661       (gnus-delete-overlay overlay))))
662
663 (defun gnus-cite-parse-wrapper ()
664   ;; Wrap chopped gnus-cite-parse.
665   (article-goto-body)
666   (let ((inhibit-point-motion-hooks t))
667     (save-excursion
668       (gnus-cite-parse-attributions))
669     (save-excursion
670       (gnus-cite-parse))
671     (save-excursion
672       (gnus-cite-connect-attributions))))
673
674 (defun gnus-cite-parse ()
675   ;; Parse and connect citation prefixes and attribution lines.
676
677   ;; Parse current buffer searching for citation prefixes.
678   (let ((line (1+ (count-lines (point-min) (point))))
679         (case-fold-search t)
680         (max (save-excursion
681                (goto-char (point-max))
682                (gnus-article-search-signature)
683                (point)))
684         (prefix-regexp (concat "^\\(" message-cite-prefix-regexp "\\)"))
685         alist entry start begin end numbers prefix guess-limit mc-flag)
686     ;; Get all potential prefixes in `alist'.
687     (while (< (point) max)
688       ;; Each line.
689       (setq begin (point)
690             guess-limit (progn (skip-chars-forward "^> \t\r\n") (point))
691             end (progn (beginning-of-line 2) (point))
692             start end)
693       (goto-char begin)
694       ;; Ignore standard Supercite attribution prefix.
695       (when (and (< guess-limit (+ begin gnus-cite-max-prefix))
696                  (looking-at gnus-supercite-regexp))
697         (if (match-end 1)
698             (setq end (1+ (match-end 1)))
699           (setq end (1+ begin))))
700       ;; Ignore very long prefixes.
701       (when (> end (+ begin gnus-cite-max-prefix))
702         (setq end (+ begin gnus-cite-max-prefix)))
703       (while (re-search-forward prefix-regexp (1- end) t)
704         ;; Each prefix.
705         (setq end (match-end 0)
706               prefix (buffer-substring begin end))
707         (gnus-set-text-properties 0 (length prefix) nil prefix)
708         (setq entry (assoc prefix alist))
709         (if entry
710             (setcdr entry (cons line (cdr entry)))
711           (push (list prefix line) alist))
712         (goto-char begin))
713       (goto-char start)
714       (setq line (1+ line)))
715     ;; We got all the potential prefixes.  Now create
716     ;; `gnus-cite-prefix-alist' containing the oldest prefix for each
717     ;; line that appears at least gnus-cite-minimum-match-count
718     ;; times.  First sort them by length.  Longer is older.
719     (setq alist (sort alist (lambda (a b)
720                               (> (length (car a)) (length (car b))))))
721     (while alist
722       (setq entry (car alist)
723             prefix (car entry)
724             numbers (cdr entry)
725             alist (cdr alist))
726       (cond ((null numbers)
727              ;; No lines with this prefix that wasn't also part of
728              ;; a longer prefix.
729              )
730             ((< (length numbers) gnus-cite-minimum-match-count)
731              ;; Too few lines with this prefix.  We keep it a bit
732              ;; longer in case it is an exact match for an attribution
733              ;; line, but we don't remove the line from other
734              ;; prefixes.
735              (push entry gnus-cite-prefix-alist))
736             (t
737              (push entry
738                    gnus-cite-prefix-alist)
739              ;; Remove articles from other prefixes.
740              (let ((loop alist)
741                    current)
742                (while loop
743                  (setq current (car loop)
744                        loop (cdr loop))
745                  (setcdr current
746                          (gnus-set-difference (cdr current) numbers)))))))))
747
748 (defun gnus-cite-parse-attributions ()
749   (let (al-alist)
750     ;; Parse attributions
751     (while (re-search-forward gnus-cite-attribution-suffix (point-max) t)
752       (let* ((start (match-beginning 0))
753              (end (match-end 0))
754              (wrote (count-lines (point-min) end))
755              (prefix (gnus-cite-find-prefix wrote))
756              ;; Check previous line for an attribution leader.
757              (tag (progn
758                     (beginning-of-line 1)
759                     (when (looking-at gnus-supercite-secondary-regexp)
760                       (buffer-substring (match-beginning 1)
761                                         (match-end 1)))))
762              (in (progn
763                    (goto-char start)
764                    (and (re-search-backward gnus-cite-attribution-prefix
765                                             (save-excursion
766                                               (beginning-of-line 0)
767                                               (point))
768                                             t)
769                         (not (re-search-forward gnus-cite-attribution-suffix
770                                                 start t))
771                         (count-lines (point-min) (1+ (point)))))))
772         (when (eq wrote in)
773           (setq in nil))
774         (goto-char end)
775         ;; don't add duplicates
776         (let ((al (buffer-substring (save-excursion (beginning-of-line 0)
777                                                     (1+ (point)))
778                                     end)))
779           (if (not (assoc al al-alist))
780               (progn
781                 (push (list wrote in prefix tag)
782                       gnus-cite-loose-attribution-alist)
783                 (push (cons al t) al-alist))))))))
784
785 (defun gnus-cite-connect-attributions ()
786   ;; Connect attributions to citations
787
788   ;; No citations have been connected to attribution lines yet.
789   (setq gnus-cite-loose-prefix-alist (append gnus-cite-prefix-alist nil))
790
791   ;; Parse current buffer searching for attribution lines.
792   ;; Find exact supercite citations.
793   (gnus-cite-match-attributions 'small nil
794                                 (lambda (prefix tag)
795                                   (when tag
796                                     (concat "\\`"
797                                             (regexp-quote prefix) "[ \t]*"
798                                             (regexp-quote tag) ">"))))
799   ;; Find loose supercite citations after attributions.
800   (gnus-cite-match-attributions 'small t
801                                 (lambda (prefix tag)
802                                   (when tag
803                                     (concat "\\<"
804                                             (regexp-quote tag)
805                                             "\\>"))))
806   ;; Find loose supercite citations anywhere.
807   (gnus-cite-match-attributions 'small nil
808                                 (lambda (prefix tag)
809                                   (when tag
810                                     (concat "\\<"
811                                             (regexp-quote tag)
812                                             "\\>"))))
813   ;; Find nested citations after attributions.
814   (gnus-cite-match-attributions 'small-if-unique t
815                                 (lambda (prefix tag)
816                                   (concat "\\`" (regexp-quote prefix) ".+")))
817   ;; Find nested citations anywhere.
818   (gnus-cite-match-attributions 'small nil
819                                 (lambda (prefix tag)
820                                   (concat "\\`" (regexp-quote prefix) ".+")))
821   ;; Remove loose prefixes with too few lines.
822   (let ((alist gnus-cite-loose-prefix-alist)
823         entry)
824     (while alist
825       (setq entry (car alist)
826             alist (cdr alist))
827       (when (< (length (cdr entry)) gnus-cite-minimum-match-count)
828         (setq gnus-cite-prefix-alist
829               (delq entry gnus-cite-prefix-alist)
830               gnus-cite-loose-prefix-alist
831               (delq entry gnus-cite-loose-prefix-alist)))))
832   ;; Find flat attributions.
833   (gnus-cite-match-attributions 'first t nil)
834   ;; Find any attributions (are we getting desperate yet?).
835   (gnus-cite-match-attributions 'first nil nil))
836
837 (defun gnus-cite-match-attributions (sort after fun)
838   ;; Match all loose attributions and citations (SORT AFTER FUN) .
839   ;;
840   ;; If SORT is `small', the citation with the shortest prefix will be
841   ;; used, if it is `first' the first prefix will be used, if it is
842   ;; `small-if-unique' the shortest prefix will be used if the
843   ;; attribution line does not share its own prefix with other
844   ;; loose attribution lines, otherwise the first prefix will be used.
845   ;;
846   ;; If AFTER is non-nil, only citations after the attribution line
847   ;; will be considered.
848   ;;
849   ;; If FUN is non-nil, it will be called with the arguments (WROTE
850   ;; PREFIX TAG) and expected to return a regular expression.  Only
851   ;; citations whose prefix matches the regular expression will be
852   ;; considered.
853   ;;
854   ;; WROTE is the attribution line number.
855   ;; PREFIX is the attribution line prefix.
856   ;; TAG is the Supercite tag on the attribution line.
857   (let ((atts gnus-cite-loose-attribution-alist)
858         (case-fold-search t)
859         att wrote in prefix tag regexp limit smallest best size)
860     (while atts
861       (setq att (car atts)
862             atts (cdr atts)
863             wrote (nth 0 att)
864             in (nth 1 att)
865             prefix (nth 2 att)
866             tag (nth 3 att)
867             regexp (if fun (funcall fun prefix tag) "")
868             size (cond ((eq sort 'small) t)
869                        ((eq sort 'first) nil)
870                        (t (< (length (gnus-cite-find-loose prefix)) 2)))
871             limit (if after wrote -1)
872             smallest 1000000
873             best nil)
874       (let ((cites gnus-cite-loose-prefix-alist)
875             cite candidate numbers first compare)
876         (while cites
877           (setq cite (car cites)
878                 cites (cdr cites)
879                 candidate (car cite)
880                 numbers (cdr cite)
881                 first (apply 'min numbers)
882                 compare (if size (length candidate) first))
883           (and (> first limit)
884                regexp
885                (string-match regexp candidate)
886                (< compare smallest)
887                (setq best cite
888                      smallest compare))))
889       (if (null best)
890           ()
891         (setq gnus-cite-loose-attribution-alist
892               (delq att gnus-cite-loose-attribution-alist))
893         (push (cons wrote (car best)) gnus-cite-attribution-alist)
894         (when in
895           (push (cons in (car best)) gnus-cite-attribution-alist))
896         (when (memq best gnus-cite-loose-prefix-alist)
897           (let ((loop gnus-cite-prefix-alist)
898                 (numbers (cdr best))
899                 current)
900             (setq gnus-cite-loose-prefix-alist
901                   (delq best gnus-cite-loose-prefix-alist))
902             (while loop
903               (setq current (car loop)
904                     loop (cdr loop))
905               (if (eq current best)
906                   ()
907                 (setcdr current (gnus-set-difference (cdr current) numbers))
908                 (when (null (cdr current))
909                   (setq gnus-cite-loose-prefix-alist
910                         (delq current gnus-cite-loose-prefix-alist)
911                         atts (delq current atts)))))))))))
912
913 (defun gnus-cite-find-loose (prefix)
914   ;; Return a list of loose attribution lines prefixed by PREFIX.
915   (let* ((atts gnus-cite-loose-attribution-alist)
916          att line lines)
917     (while atts
918       (setq att (car atts)
919             line (car att)
920             atts (cdr atts))
921       (when (string-equal (gnus-cite-find-prefix line) prefix)
922         (push line lines)))
923     lines))
924
925 (defun gnus-cite-add-face (number prefix face)
926   ;; At line NUMBER, ignore PREFIX and add FACE to the rest of the line.
927   (when face
928     (let ((inhibit-point-motion-hooks t)
929           from to overlay)
930       (goto-char (point-min))
931       (when (zerop (forward-line (1- number)))
932         (static-if (or (featurep 'xemacs)
933                        (and (eq emacs-major-version 20)
934                             (>= emacs-minor-version 3))
935                        (>= emacs-major-version 21))
936             (forward-char (length prefix))
937           (move-to-column (string-width prefix)))
938         (skip-chars-forward " \t")
939         (setq from (point))
940         (end-of-line 1)
941         (skip-chars-backward " \t")
942         (setq to (point))
943         (when (< from to)
944           (push (setq overlay (gnus-make-overlay from to))
945                 gnus-cite-overlay-list)
946           (gnus-overlay-put overlay 'face face))))))
947
948 (defun gnus-cite-toggle (prefix)
949   (save-excursion
950     (set-buffer gnus-article-buffer)
951     (gnus-cite-parse-maybe nil t)
952     (let ((buffer-read-only nil)
953           (numbers (cdr (assoc prefix gnus-cite-prefix-alist)))
954           (inhibit-point-motion-hooks t)
955           number)
956       (while numbers
957         (setq number (car numbers)
958               numbers (cdr numbers))
959         (goto-char (point-min))
960         (forward-line (1- number))
961         (cond ((get-text-property (point) 'invisible)
962                (remove-text-properties (point) (progn (forward-line 1) (point))
963                                        gnus-hidden-properties))
964               ((assq number gnus-cite-attribution-alist))
965               (t
966                (gnus-add-text-properties
967                 (point) (progn (forward-line 1) (point))
968                 (nconc (list 'article-type 'cite)
969                        gnus-hidden-properties))))))))
970
971 (defun gnus-cite-find-prefix (line)
972   ;; Return citation prefix for LINE.
973   (let ((alist gnus-cite-prefix-alist)
974         (prefix "")
975         entry)
976     (while alist
977       (setq entry (car alist)
978             alist (cdr alist))
979       (when (memq line (cdr entry))
980         (setq prefix (car entry))))
981     prefix))
982
983 (defun gnus-cite-localize ()
984   "Make the citation variables local to the article buffer."
985   (let ((vars '(gnus-cite-article
986                 gnus-cite-overlay-list gnus-cite-prefix-alist
987                 gnus-cite-attribution-alist gnus-cite-loose-prefix-alist
988                 gnus-cite-loose-attribution-alist)))
989     (while vars
990       (make-local-variable (pop vars)))))
991
992 (gnus-ems-redefine)
993
994 (provide 'gnus-cite)
995
996 ;; Local Variables:
997 ;; coding: iso-8859-1
998 ;; End:
999
1000 ;;; gnus-cite.el ends here