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