*** empty log message ***
[gnus] / lisp / gnus-cite.el
1 ;;; gnus-cite.el --- parse citations in articles for Gnus
2 ;; Copyright (C) 1995,96 Free Software Foundation, Inc.
3
4 ;; Author: Per Abrahamsen <abraham@iesd.auc.dk>
5 ;; Keywords: news, mail
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 (require 'gnus)
29 (require 'gnus-msg)
30 (require 'gnus-ems)
31 (eval-when-compile (require 'cl))
32
33 (eval-and-compile
34   (autoload 'gnus-article-add-button "gnus-vis"))
35
36 ;;; Customization:
37
38 (defvar gnus-cited-text-button-line-format "%(%{[...]%}%)"
39   "Format of cited text buttons.")
40
41 (defvar gnus-cited-lines-visible nil
42   "The number of lines of hidden cited text to remain visible.")
43
44 (defvar gnus-cite-parse-max-size 25000
45   "Maximum article size (in bytes) where parsing citations is allowed.
46 Set it to nil to parse all articles.")
47
48 (defvar gnus-cite-prefix-regexp 
49     "^[]>|:}+ ]*[]>|:}+]\\(.*>\\)?\\|^.*>"
50   "Regexp matching the longest possible citation prefix on a line.")
51
52 (defvar gnus-cite-max-prefix 20
53   "Maximum possible length for a citation prefix.")
54
55 (defvar gnus-supercite-regexp 
56   (concat "^\\(" gnus-cite-prefix-regexp "\\)? *"
57           ">>>>> +\"\\([^\"\n]+\\)\" +==")
58   "Regexp matching normal SuperCite attribution lines.
59 The first grouping must match prefixes added by other packages.")
60
61 (defvar gnus-supercite-secondary-regexp "^.*\"\\([^\"\n]+\\)\" +=="
62   "Regexp matching mangled SuperCite attribution lines.
63 The first regexp group should match the SuperCite attribution.")
64
65 (defvar gnus-cite-minimum-match-count 2
66   "Minimum number of identical prefixes before we believe it's a citation.")
67
68 ;see gnus-cus.el
69 ;(defvar gnus-cite-face-list 
70 ;  (if (eq gnus-display-type 'color)
71 ;      (if (eq gnus-background-mode 'dark) 'light 'dark)
72 ;    '(italic))
73 ;  "Faces used for displaying different citations.
74 ;It is either a list of face names, or one of the following special
75 ;values:
76
77 ;dark: Create faces from `gnus-face-dark-name-list'.
78 ;light: Create faces from `gnus-face-light-name-list'.
79
80 ;The variable `gnus-make-foreground' determines whether the created
81 ;faces change the foreground or the background colors.")
82
83 (defvar gnus-cite-attribution-prefix "in article\\|in <"
84   "Regexp matching the beginning of an attribution line.")
85
86 (defvar gnus-cite-attribution-suffix
87   "\\(wrote\\|writes\\|said\\|says\\):[ \t]*$"
88   "Regexp matching the end of an attribution line.
89 The text matching the first grouping will be used as a button.")
90
91 ;see gnus-cus.el
92 ;(defvar gnus-cite-attribution-face 'underline
93 ;  "Face used for attribution lines.
94 ;It is merged with the face for the cited text belonging to the attribution.")
95
96 ;see gnus-cus.el
97 ;(defvar gnus-cite-hide-percentage 50
98 ;  "Only hide cited text if it is larger than this percent of the body.")
99
100 ;see gnus-cus.el
101 ;(defvar gnus-cite-hide-absolute 10
102 ;  "Only hide cited text if there is at least this number of cited lines.")
103
104 ;see gnus-cus.el
105 ;(defvar gnus-face-light-name-list
106 ;  '("light blue" "light cyan" "light yellow" "light pink"
107 ;    "pale green" "beige" "orange" "magenta" "violet" "medium purple"
108 ;    "turquoise")
109 ;  "Names of light colors.")
110
111 ;see gnus-cus.el
112 ;(defvar gnus-face-dark-name-list
113 ;  '("dark salmon" "firebrick"
114 ;    "dark green" "dark orange" "dark khaki" "dark violet"
115 ;    "dark turquoise")
116 ;  "Names of dark colors.")
117
118 ;;; Internal Variables:
119
120 (defvar gnus-article-length nil)
121 ;; Length of article last time we parsed it.
122 ;; BUG! KLUDGE! UGLY! FIX ME!
123
124 (defvar gnus-cite-prefix-alist nil)
125 ;; Alist of citation prefixes.  
126 ;; The cdr is a list of lines with that prefix.
127
128 (defvar gnus-cite-attribution-alist nil)
129 ;; Alist of attribution lines.
130 ;; The car is a line number.
131 ;; The cdr is the prefix for the citation started by that line.
132
133 (defvar gnus-cite-loose-prefix-alist nil)
134 ;; Alist of citation prefixes that have no matching attribution.
135 ;; The cdr is a list of lines with that prefix.
136
137 (defvar gnus-cite-loose-attribution-alist nil)
138 ;; Alist of attribution lines that have no matching citation.
139 ;; Each member has the form (WROTE IN PREFIX TAG), where
140 ;; WROTE: is the attribution line number
141 ;; IN: is the line number of the previous line if part of the same attribution,
142 ;; PREFIX: Is the citation prefix of the attribution line(s), and
143 ;; TAG: Is a SuperCite tag, if any.
144
145 (defvar gnus-cited-text-button-line-format-alist 
146   `((?b beg ?d)
147     (?e end ?d)
148     (?l (- end beg) ?d)))
149 (defvar gnus-cited-text-button-line-format-spec nil)
150
151 ;;; Commands:
152
153 (defun gnus-article-highlight-citation (&optional force)
154   "Highlight cited text.
155 Each citation in the article will be highlighted with a different face.
156 The faces are taken from `gnus-cite-face-list'.
157 Attribution lines are highlighted with the same face as the
158 corresponding citation merged with `gnus-cite-attribution-face'.
159
160 Text is considered cited if at least `gnus-cite-minimum-match-count'
161 lines matches `gnus-cite-prefix-regexp' with the same prefix.  
162
163 Lines matching `gnus-cite-attribution-suffix' and perhaps
164 `gnus-cite-attribution-prefix' are considered attribution lines."
165   (interactive (list 'force))
166   ;; Create dark or light faces if necessary.
167   (cond ((eq gnus-cite-face-list 'light)
168          (setq gnus-cite-face-list
169                (mapcar 'gnus-make-face gnus-face-light-name-list)))
170         ((eq gnus-cite-face-list 'dark)
171          (setq gnus-cite-face-list
172                (mapcar 'gnus-make-face gnus-face-dark-name-list))))
173   (save-excursion
174     (set-buffer gnus-article-buffer)
175     (gnus-cite-parse-maybe force)
176     (let ((buffer-read-only nil)
177           (alist gnus-cite-prefix-alist)
178           (faces gnus-cite-face-list)
179           (inhibit-point-motion-hooks t)
180           face entry prefix skip numbers number face-alist)
181       ;; Loop through citation prefixes.
182       (while alist
183         (setq entry (car alist)
184               alist (cdr alist)
185               prefix (car entry)
186               numbers (cdr entry)
187               face (car faces)
188               faces (or (cdr faces) gnus-cite-face-list)
189               face-alist (cons (cons prefix face) face-alist))
190         (while numbers
191           (setq number (car numbers)
192                 numbers (cdr numbers))
193           (and (not (assq number gnus-cite-attribution-alist))
194                (not (assq number gnus-cite-loose-attribution-alist))
195                (gnus-cite-add-face number prefix face))))
196       ;; Loop through attribution lines.
197       (setq alist gnus-cite-attribution-alist)
198       (while alist
199         (setq entry (car alist)
200               alist (cdr alist)
201               number (car entry)
202               prefix (cdr entry)
203               skip (gnus-cite-find-prefix number)
204               face (cdr (assoc prefix face-alist)))
205         ;; Add attribution button.
206         (goto-line number)
207         (if (re-search-forward gnus-cite-attribution-suffix 
208                                (save-excursion (end-of-line 1) (point))
209                                t)
210             (gnus-article-add-button (match-beginning 1) (match-end 1)
211                                      'gnus-cite-toggle prefix))
212         ;; Highlight attribution line.
213         (gnus-cite-add-face number skip face)
214         (gnus-cite-add-face number skip gnus-cite-attribution-face))
215       ;; Loop through attribution lines.
216       (setq alist gnus-cite-loose-attribution-alist)
217       (while alist
218         (setq entry (car alist)
219               alist (cdr alist)
220               number (car entry)
221               skip (gnus-cite-find-prefix number))
222         (gnus-cite-add-face number skip gnus-cite-attribution-face)))))
223
224 (defun gnus-dissect-cited-text ()
225   "Dissect the article buffer looking for cited text."
226   (save-excursion
227     (set-buffer gnus-article-buffer)
228     (gnus-cite-parse-maybe)
229     (let ((alist gnus-cite-prefix-alist)
230           prefix numbers number marks)
231       ;; Loop through citation prefixes.
232       (while alist
233         (setq numbers (pop alist)
234               prefix (pop numbers))
235         (while numbers
236           (setq number (pop numbers))
237           (goto-char (point-min))
238           (forward-line number)
239           (push (cons (point-marker) "") marks)
240           (while (and numbers
241                       (= (1- number) (car numbers)))
242             (setq number (pop numbers)))
243           (goto-char (point-min))
244           (forward-line (1- number))
245           (push (cons (point-marker) prefix) marks)))
246       (goto-char (point-min))
247       (search-forward "\n\n" nil t)
248       (push (cons (point-marker) "") marks)
249       (goto-char (point-max))
250       (re-search-backward gnus-signature-separator nil t)
251       (push (cons (point-marker) "") marks)
252       (setq marks (sort marks (lambda (m1 m2) (< (car m1) (car m2)))))
253       (let* ((omarks marks))
254         (setq marks nil)
255         (while (cdr omarks)
256           (if (= (caar omarks) (caadr omarks))
257               (progn
258                 (unless (equal (cdar omarks) "")
259                   (push (car omarks) marks))
260                 (unless (equal (cdadr omarks) "")
261                   (push (cadr omarks) marks))
262                 (setq omarks (cdr omarks)))
263             (push (car omarks) marks))
264           (setq omarks (cdr omarks)))
265         (when (car omarks)
266           (push (car omarks) marks))
267         (nreverse marks)))))
268
269 (defun gnus-article-fill-cited-article (&optional force)
270   "Do word wrapping in the current article."
271   (interactive (list t))
272   (save-excursion
273     (set-buffer gnus-article-buffer)
274     (let ((buffer-read-only nil)
275           (inhibit-point-motion-hooks t)
276           (marks (gnus-dissect-cited-text))
277           (adaptive-fill-mode nil))
278       (save-restriction
279         (while (cdr marks)
280           (widen)
281           (narrow-to-region (car (car marks)) (car (cadr marks)))
282           (let ((adaptive-fill-regexp (concat "^" (regexp-quote
283                                                    (cdr (car marks)))
284                                               " *"))
285                 (fill-prefix (cdr (car marks))))
286             (fill-region (point-min) (point-max)))
287           (set-marker (caar marks) nil)
288           (setq marks (cdr marks)))
289         (set-marker (caar marks) nil)))))
290
291 (defun gnus-article-hide-citation (&optional arg force)
292   "Toggle hiding of all cited text except attribution lines.
293 See the documentation for `gnus-article-highlight-citation'.
294 If given a negative prefix, always show; if given a positive prefix,
295 always hide."
296   (interactive (list current-prefix-arg 'force))
297   (setq gnus-cited-text-button-line-format-spec 
298         (gnus-parse-format gnus-cited-text-button-line-format 
299                            gnus-cited-text-button-line-format-alist t))
300   (unless (gnus-article-check-hidden-text 'cite arg)
301     (save-excursion
302       (set-buffer gnus-article-buffer)
303       (let ((buffer-read-only nil)
304             (marks (gnus-dissect-cited-text))
305             (inhibit-point-motion-hooks t)
306             (props (nconc (list 'gnus-type 'cite)
307                           gnus-hidden-properties))
308             beg end)
309         (while marks
310           (setq beg nil
311                 end nil)
312           (while (and marks (string= (cdar marks) ""))
313             (setq marks (cdr marks)))
314           (when marks 
315             (setq beg (caar marks)))
316           (while (and marks (not (string= (cdar marks) "")))
317             (setq marks (cdr marks)))
318           (when marks
319             (setq end (caar marks)))
320           ;; Skip past lines we want to leave visible.
321           (when (and beg gnus-cited-lines-visible)
322             (goto-char beg)
323             (forward-line gnus-cited-lines-visible)
324             (if (> (point) end)
325                 (setq beg nil)
326               (setq beg (point))))
327           (when (and beg end)
328             (add-text-properties beg end props)
329             (goto-char beg)
330             (put-text-property beg end 'gnus-type 'cite)
331             (gnus-article-add-button
332              (point)
333              (progn (eval gnus-cited-text-button-line-format-spec) (point))
334              `gnus-article-toggle-cited-text (cons beg end))))))))
335
336 (defun gnus-article-toggle-cited-text (region)
337   "Toggle hiding the text in REGION."
338   (let (buffer-read-only)
339     (funcall
340      (if (text-property-any
341           (car region) (cdr region) 
342           (car gnus-hidden-properties) (cadr gnus-hidden-properties))
343          'remove-text-properties 'add-text-properties)
344      (car region) (cdr region) gnus-hidden-properties)))
345
346 (defun gnus-article-hide-citation-maybe (&optional arg force)
347   "Toggle hiding of cited text that has an attribution line.
348 If given a negative prefix, always show; if given a positive prefix,
349 always hide.
350 This will do nothing unless at least `gnus-cite-hide-percentage'
351 percent and at least `gnus-cite-hide-absolute' lines of the body is
352 cited text with attributions.  When called interactively, these two
353 variables are ignored.
354 See also the documentation for `gnus-article-highlight-citation'."
355   (interactive (list current-prefix-arg 'force))
356   (unless (gnus-article-check-hidden-text 'cite arg)
357     (save-excursion
358       (set-buffer gnus-article-buffer)
359       (gnus-cite-parse-maybe force)
360       (goto-char (point-min))
361       (search-forward "\n\n" nil t)
362       (let ((start (point))
363             (atts gnus-cite-attribution-alist)
364             (buffer-read-only nil)
365             (inhibit-point-motion-hooks t)
366             (hiden 0)
367             total)
368         (goto-char (point-max))
369         (re-search-backward gnus-signature-separator nil t)
370         (setq total (count-lines start (point)))
371         (while atts
372           (setq hiden (+ hiden (length (cdr (assoc (cdr (car atts))
373                                                    gnus-cite-prefix-alist))))
374                 atts (cdr atts)))
375         (if (or force
376                 (and (> (* 100 hiden) (* gnus-cite-hide-percentage total))
377                      (> hiden gnus-cite-hide-absolute)))
378             (progn
379               (setq atts gnus-cite-attribution-alist)
380               (while atts
381                 (setq total (cdr (assoc (cdr (car atts)) 
382                                         gnus-cite-prefix-alist))
383                       atts (cdr atts))
384                 (while total
385                   (setq hiden (car total)
386                         total (cdr total))
387                   (goto-line hiden)
388                   (or (assq hiden gnus-cite-attribution-alist)
389                       (add-text-properties 
390                        (point) (progn (forward-line 1) (point))
391                        (nconc (list 'gnus-type 'cite)
392                               gnus-hidden-properties)))))))))))
393
394 (defun gnus-article-hide-citation-in-followups ()
395   "Hide cited text in non-root articles."
396   (interactive)
397   (save-excursion
398     (set-buffer gnus-article-buffer)
399     (let ((article (cdr gnus-article-current)))
400       (unless (save-excursion
401                 (set-buffer gnus-summary-buffer)
402                 (gnus-root-id (mail-header-id 
403                                (gnus-summary-article-header article))))
404         (gnus-article-hide-citation)))))
405
406 ;;; Internal functions:
407
408 (defun gnus-cite-parse-maybe (&optional force)
409   ;; Parse if the buffer has changes since last time.
410   (if (eq gnus-article-length (- (point-max) (point-min)))
411       ()
412     ;;Reset parser information.
413     (setq gnus-cite-prefix-alist nil
414           gnus-cite-attribution-alist nil
415           gnus-cite-loose-prefix-alist nil
416           gnus-cite-loose-attribution-alist nil)
417     ;; Parse if not too large.
418     (if (and (not force) 
419              gnus-cite-parse-max-size
420              (> (buffer-size) gnus-cite-parse-max-size))
421         ()
422       (setq gnus-article-length (- (point-max) (point-min)))
423       (gnus-cite-parse))))
424
425 (defun gnus-cite-parse ()
426   ;; Parse and connect citation prefixes and attribution lines.
427   
428   ;; Parse current buffer searching for citation prefixes.
429   (goto-char (point-min))
430   (or (search-forward "\n\n" nil t)
431       (goto-char (point-max)))
432   (let ((line (1+ (count-lines (point-min) (point))))
433         (case-fold-search t)
434         (max (save-excursion
435                (goto-char (point-max))
436                (re-search-backward gnus-signature-separator nil t)
437                (point)))
438         alist entry start begin end numbers prefix)
439     ;; Get all potential prefixes in `alist'.
440     (while (< (point) max)
441       ;; Each line.
442       (setq begin (point)
443             end (progn (beginning-of-line 2) (point))
444             start end)
445       (goto-char begin)
446       ;; Ignore standard SuperCite attribution prefix.
447       (if (looking-at gnus-supercite-regexp)
448           (if (match-end 1)
449               (setq end (1+ (match-end 1)))
450             (setq end (1+ begin))))
451       ;; Ignore very long prefixes.
452       (if (> end (+ (point) gnus-cite-max-prefix))
453           (setq end (+ (point) gnus-cite-max-prefix)))
454       (while (re-search-forward gnus-cite-prefix-regexp (1- end) t)
455         ;; Each prefix.
456         (setq end (match-end 0)
457               prefix (buffer-substring begin end))
458         (set-text-properties 0 (length prefix) nil prefix)
459         (setq entry (assoc prefix alist))
460         (if entry 
461             (setcdr entry (cons line (cdr entry)))
462           (setq alist (cons (list prefix line) alist)))
463         (goto-char begin))
464       (goto-char start)
465       (setq line (1+ line)))
466     ;; We got all the potential prefixes.  Now create
467     ;; `gnus-cite-prefix-alist' containing the oldest prefix for each
468     ;; line that appears at least gnus-cite-minimum-match-count
469     ;; times. First sort them by length.  Longer is older.
470     (setq alist (sort alist (lambda (a b)
471                               (> (length (car a)) (length (car b))))))
472     (while alist
473       (setq entry (car alist)
474             prefix (car entry)
475             numbers (cdr entry)
476             alist (cdr alist))
477       (cond ((null numbers)
478              ;; No lines with this prefix that wasn't also part of
479              ;; a longer prefix.
480              )
481             ((< (length numbers) gnus-cite-minimum-match-count)
482              ;; Too few lines with this prefix.  We keep it a bit
483              ;; longer in case it is an exact match for an attribution
484              ;; line, but we don't remove the line from other
485              ;; prefixes. 
486              (setq gnus-cite-prefix-alist
487                    (cons entry gnus-cite-prefix-alist)))
488             (t
489              (setq gnus-cite-prefix-alist (cons entry
490                                                 gnus-cite-prefix-alist))
491              ;; Remove articles from other prefixes.
492              (let ((loop alist)
493                    current)
494                (while loop
495                  (setq current (car loop)
496                        loop (cdr loop))
497                  (setcdr current 
498                          (gnus-set-difference (cdr current) numbers))))))))
499   ;; No citations have been connected to attribution lines yet.
500   (setq gnus-cite-loose-prefix-alist (append gnus-cite-prefix-alist nil))
501
502   ;; Parse current buffer searching for attribution lines.
503   (goto-char (point-min))
504   (search-forward "\n\n" nil t)
505   (while (re-search-forward gnus-cite-attribution-suffix (point-max) t)
506     (let* ((start (match-beginning 0))
507            (end (match-end 0))
508            (wrote (count-lines (point-min) end))
509            (prefix (gnus-cite-find-prefix wrote))
510            ;; Check previous line for an attribution leader.
511            (tag (progn
512                   (beginning-of-line 1)
513                   (and (looking-at gnus-supercite-secondary-regexp)
514                        (buffer-substring (match-beginning 1)
515                                          (match-end 1)))))
516            (in (progn
517                  (goto-char start)
518                  (and (re-search-backward gnus-cite-attribution-prefix
519                                           (save-excursion
520                                             (beginning-of-line 0)
521                                             (point))
522                                           t)
523                       (not (re-search-forward gnus-cite-attribution-suffix
524                                               start t))
525                       (count-lines (point-min) (1+ (point)))))))
526       (if (eq wrote in)
527           (setq in nil))
528       (goto-char end)
529       (setq gnus-cite-loose-attribution-alist
530             (cons (list wrote in prefix tag)
531                   gnus-cite-loose-attribution-alist))))
532   ;; Find exact supercite citations.
533   (gnus-cite-match-attributions 'small nil
534                                 (lambda (prefix tag)
535                                   (if tag
536                                       (concat "\\`" 
537                                               (regexp-quote prefix) "[ \t]*" 
538                                               (regexp-quote tag) ">"))))
539   ;; Find loose supercite citations after attributions.
540   (gnus-cite-match-attributions 'small t
541                                 (lambda (prefix tag)
542                                   (if tag (concat "\\<"
543                                                   (regexp-quote tag)
544                                                   "\\>"))))
545   ;; Find loose supercite citations anywhere.
546   (gnus-cite-match-attributions 'small nil
547                                 (lambda (prefix tag)
548                                   (if tag (concat "\\<"
549                                                   (regexp-quote tag)
550                                                   "\\>"))))
551   ;; Find nested citations after attributions.
552   (gnus-cite-match-attributions 'small-if-unique t
553                                 (lambda (prefix tag)
554                                   (concat "\\`" (regexp-quote prefix) ".+")))
555   ;; Find nested citations anywhere.
556   (gnus-cite-match-attributions 'small nil
557                                 (lambda (prefix tag)
558                                   (concat "\\`" (regexp-quote prefix) ".+")))
559   ;; Remove loose prefixes with too few lines.
560   (let ((alist gnus-cite-loose-prefix-alist)
561         entry)
562     (while alist
563       (setq entry (car alist)
564             alist (cdr alist))
565       (if (< (length (cdr entry)) gnus-cite-minimum-match-count)
566           (setq gnus-cite-prefix-alist
567                 (delq entry gnus-cite-prefix-alist)
568                 gnus-cite-loose-prefix-alist
569                 (delq entry gnus-cite-loose-prefix-alist)))))
570   ;; Find flat attributions.
571   (gnus-cite-match-attributions 'first t nil)
572   ;; Find any attributions (are we getting desperate yet?).
573   (gnus-cite-match-attributions 'first nil nil))
574
575 (defun gnus-cite-match-attributions (sort after fun)
576   ;; Match all loose attributions and citations (SORT AFTER FUN) .
577   ;;
578   ;; If SORT is `small', the citation with the shortest prefix will be
579   ;; used, if it is `first' the first prefix will be used, if it is
580   ;; `small-if-unique' the shortest prefix will be used if the
581   ;; attribution line does not share its own prefix with other
582   ;; loose attribution lines, otherwise the first prefix will be used.
583   ;;
584   ;; If AFTER is non-nil, only citations after the attribution line
585   ;; will be considered.
586   ;;
587   ;; If FUN is non-nil, it will be called with the arguments (WROTE
588   ;; PREFIX TAG) and expected to return a regular expression.  Only
589   ;; citations whose prefix matches the regular expression will be
590   ;; considered. 
591   ;; 
592   ;; WROTE is the attribution line number.
593   ;; PREFIX is the attribution line prefix.
594   ;; TAG is the SuperCite tag on the attribution line.
595   (let ((atts gnus-cite-loose-attribution-alist)
596         (case-fold-search t)
597         att wrote in prefix tag regexp limit smallest best size)
598     (while atts
599       (setq att (car atts)
600             atts (cdr atts)
601             wrote (nth 0 att)
602             in (nth 1 att)
603             prefix (nth 2 att)
604             tag (nth 3 att)
605             regexp (if fun (funcall fun prefix tag) "")
606             size (cond ((eq sort 'small) t)
607                        ((eq sort 'first) nil)
608                        (t (< (length (gnus-cite-find-loose prefix)) 2)))
609             limit (if after wrote -1)
610             smallest 1000000                   
611             best nil)
612       (let ((cites gnus-cite-loose-prefix-alist)
613             cite candidate numbers first compare)
614         (while cites
615           (setq cite (car cites)
616                 cites (cdr cites)
617                 candidate (car cite)
618                 numbers (cdr cite)
619                 first (apply 'min numbers)
620                 compare (if size (length candidate) first))
621           (and (> first limit)
622                regexp
623                (string-match regexp candidate)
624                (< compare smallest)
625                (setq best cite
626                      smallest compare))))
627       (if (null best)
628           ()
629         (setq gnus-cite-loose-attribution-alist
630               (delq att gnus-cite-loose-attribution-alist))
631         (setq gnus-cite-attribution-alist 
632               (cons (cons wrote (car best)) gnus-cite-attribution-alist))
633         (if in
634             (setq gnus-cite-attribution-alist 
635                   (cons (cons in (car best)) gnus-cite-attribution-alist)))
636         (if (memq best gnus-cite-loose-prefix-alist)
637             (let ((loop gnus-cite-prefix-alist)
638                   (numbers (cdr best))
639                   current)
640               (setq gnus-cite-loose-prefix-alist
641                     (delq best gnus-cite-loose-prefix-alist))
642               (while loop
643                 (setq current (car loop)
644                       loop (cdr loop))
645                 (if (eq current best)
646                     ()
647                   (setcdr current (gnus-set-difference (cdr current) numbers))
648                   (if (null (cdr current))
649                       (setq gnus-cite-loose-prefix-alist
650                             (delq current gnus-cite-loose-prefix-alist)
651                             atts (delq current atts)))))))))))
652
653 (defun gnus-cite-find-loose (prefix)
654   ;; Return a list of loose attribution lines prefixed by PREFIX.
655   (let* ((atts gnus-cite-loose-attribution-alist)
656          att line lines)
657     (while atts
658       (setq att (car atts)
659             line (car att)
660             atts (cdr atts))
661       (if (string-equal (gnus-cite-find-prefix line) prefix)
662           (setq lines (cons line lines))))
663     lines))
664
665 (defun gnus-cite-add-face (number prefix face)
666   ;; At line NUMBER, ignore PREFIX and add FACE to the rest of the line.
667   (if face
668       (let ((inhibit-point-motion-hooks t)
669             from to)
670         (goto-line number)
671         (forward-char (length prefix))
672         (skip-chars-forward " \t")
673         (setq from (point))
674         (end-of-line 1)
675         (skip-chars-backward " \t")
676         (setq to (point))
677         (if (< from to)
678             (gnus-overlay-put (gnus-make-overlay from to) 'face face)))))
679
680 (defun gnus-cite-toggle (prefix)
681   (save-excursion
682     (set-buffer gnus-article-buffer)
683     (let ((buffer-read-only nil)
684           (numbers (cdr (assoc prefix gnus-cite-prefix-alist)))
685           (inhibit-point-motion-hooks t)
686           number)
687       (while numbers
688         (setq number (car numbers)
689               numbers (cdr numbers))
690         (goto-line number)
691         (cond ((get-text-property (point) 'invisible)
692                (remove-text-properties (point) (progn (forward-line 1) (point))
693                                        gnus-hidden-properties))
694               ((assq number gnus-cite-attribution-alist))
695               (t
696                (add-text-properties 
697                 (point) (progn (forward-line 1) (point))
698                  (nconc (list 'gnus-type 'cite)
699                         gnus-hidden-properties))))))))
700
701 (defun gnus-cite-find-prefix (line)
702   ;; Return citation prefix for LINE.
703   (let ((alist gnus-cite-prefix-alist)
704         (prefix "")
705         entry)
706     (while alist
707       (setq entry (car alist)
708             alist (cdr alist))
709       (if (memq line (cdr entry))
710           (setq prefix (car entry))))
711     prefix))
712
713 (gnus-ems-redefine)
714
715 (provide 'gnus-cite)
716
717 ;;; gnus-cite.el ends here