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