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