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