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