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