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