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