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