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