]> code.delx.au - gnu-emacs/blob - lisp/gnus/gnus-cite.el
Require cl at compile time.
[gnu-emacs] / lisp / gnus / gnus-cite.el
1 ;;; gnus-cite.el --- parse citations in articles for Gnus
2 ;; Copyright (C) 1995,96,97 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 (eval-when-compile (require 'cl))
29
30 (require 'gnus)
31 (require 'gnus-art)
32 (require 'gnus-range)
33
34 ;;; Customization:
35
36 (defgroup gnus-cite nil
37 "Citation."
38 :prefix "gnus-cite-"
39 :link '(custom-manual "(gnus)Article Highlighting")
40 :group 'gnus-article)
41
42 (defcustom gnus-cite-reply-regexp
43 "^\\(Subject: Re\\|In-Reply-To\\|References\\):"
44 "If headers match this regexp it is reasonable to believe that
45 article has citations."
46 :group 'gnus-cite
47 :type 'string)
48
49 (defcustom gnus-cite-always-check nil
50 "Check article always for citations. Set it t to check all articles."
51 :group 'gnus-cite
52 :type '(choice (const :tag "no" nil)
53 (const :tag "yes" t)))
54
55 (defcustom gnus-cited-text-button-line-format "%(%{[...]%}%)\n"
56 "Format of cited text buttons."
57 :group 'gnus-cite
58 :type 'string)
59
60 (defcustom gnus-cited-lines-visible nil
61 "The number of lines of hidden cited text to remain visible."
62 :group 'gnus-cite
63 :type '(choice (const :tag "none" nil)
64 integer))
65
66 (defcustom gnus-cite-parse-max-size 25000
67 "Maximum article size (in bytes) where parsing citations is allowed.
68 Set it to nil to parse all articles."
69 :group 'gnus-cite
70 :type '(choice (const :tag "all" nil)
71 integer))
72
73 (defcustom gnus-cite-prefix-regexp
74 "^[]>|:}+ ]*[]>|:}+]\\(.*>\\)?\\|^.*>"
75 "Regexp matching the longest possible citation prefix on a line."
76 :group 'gnus-cite
77 :type 'regexp)
78
79 (defcustom gnus-cite-max-prefix 20
80 "Maximum possible length for a citation prefix."
81 :group 'gnus-cite
82 :type 'integer)
83
84 (defcustom gnus-supercite-regexp
85 (concat "^\\(" gnus-cite-prefix-regexp "\\)? *"
86 ">>>>> +\"\\([^\"\n]+\\)\" +==")
87 "Regexp matching normal Supercite attribution lines.
88 The first grouping must match prefixes added by other packages."
89 :group 'gnus-cite
90 :type 'regexp)
91
92 (defcustom gnus-supercite-secondary-regexp "^.*\"\\([^\"\n]+\\)\" +=="
93 "Regexp matching mangled Supercite attribution lines.
94 The first regexp group should match the Supercite attribution."
95 :group 'gnus-cite
96 :type 'regexp)
97
98 (defcustom gnus-cite-minimum-match-count 2
99 "Minimum number of identical prefixes before we believe it's a citation."
100 :group 'gnus-cite
101 :type 'integer)
102
103 (defcustom gnus-cite-attribution-prefix "in article\\|in <"
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\\):[ \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 (:underline 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
261 (defvar gnus-cite-prefix-alist nil)
262 ;; Alist of citation prefixes.
263 ;; The cdr is a list of lines with that prefix.
264
265 (defvar gnus-cite-attribution-alist nil)
266 ;; Alist of attribution lines.
267 ;; The car is a line number.
268 ;; The cdr is the prefix for the citation started by that line.
269
270 (defvar gnus-cite-loose-prefix-alist nil)
271 ;; Alist of citation prefixes that have no matching attribution.
272 ;; The cdr is a list of lines with that prefix.
273
274 (defvar gnus-cite-loose-attribution-alist nil)
275 ;; Alist of attribution lines that have no matching citation.
276 ;; Each member has the form (WROTE IN PREFIX TAG), where
277 ;; WROTE: is the attribution line number
278 ;; IN: is the line number of the previous line if part of the same attribution,
279 ;; PREFIX: Is the citation prefix of the attribution line(s), and
280 ;; TAG: Is a Supercite tag, if any.
281
282 (defvar gnus-cited-text-button-line-format-alist
283 `((?b (marker-position beg) ?d)
284 (?e (marker-position end) ?d)
285 (?l (- end beg) ?d)))
286 (defvar gnus-cited-text-button-line-format-spec nil)
287
288 ;;; Commands:
289
290 (defun gnus-article-highlight-citation (&optional force)
291 "Highlight cited text.
292 Each citation in the article will be highlighted with a different face.
293 The faces are taken from `gnus-cite-face-list'.
294 Attribution lines are highlighted with the same face as the
295 corresponding citation merged with `gnus-cite-attribution-face'.
296
297 Text is considered cited if at least `gnus-cite-minimum-match-count'
298 lines matches `gnus-cite-prefix-regexp' with the same prefix.
299
300 Lines matching `gnus-cite-attribution-suffix' and perhaps
301 `gnus-cite-attribution-prefix' are considered attribution lines."
302 (interactive (list 'force))
303 (save-excursion
304 (set-buffer gnus-article-buffer)
305 (gnus-cite-parse-maybe force)
306 (let ((buffer-read-only nil)
307 (alist gnus-cite-prefix-alist)
308 (faces gnus-cite-face-list)
309 (inhibit-point-motion-hooks t)
310 face entry prefix skip numbers number face-alist)
311 ;; Loop through citation prefixes.
312 (while alist
313 (setq entry (car alist)
314 alist (cdr alist)
315 prefix (car entry)
316 numbers (cdr entry)
317 face (car faces)
318 faces (or (cdr faces) gnus-cite-face-list)
319 face-alist (cons (cons prefix face) face-alist))
320 (while numbers
321 (setq number (car numbers)
322 numbers (cdr numbers))
323 (and (not (assq number gnus-cite-attribution-alist))
324 (not (assq number gnus-cite-loose-attribution-alist))
325 (gnus-cite-add-face number prefix face))))
326 ;; Loop through attribution lines.
327 (setq alist gnus-cite-attribution-alist)
328 (while alist
329 (setq entry (car alist)
330 alist (cdr alist)
331 number (car entry)
332 prefix (cdr entry)
333 skip (gnus-cite-find-prefix number)
334 face (cdr (assoc prefix face-alist)))
335 ;; Add attribution button.
336 (goto-line number)
337 (when (re-search-forward gnus-cite-attribution-suffix
338 (save-excursion (end-of-line 1) (point))
339 t)
340 (gnus-article-add-button (match-beginning 1) (match-end 1)
341 'gnus-cite-toggle prefix))
342 ;; Highlight attribution line.
343 (gnus-cite-add-face number skip face)
344 (gnus-cite-add-face number skip gnus-cite-attribution-face))
345 ;; Loop through attribution lines.
346 (setq alist gnus-cite-loose-attribution-alist)
347 (while alist
348 (setq entry (car alist)
349 alist (cdr alist)
350 number (car entry)
351 skip (gnus-cite-find-prefix number))
352 (gnus-cite-add-face number skip gnus-cite-attribution-face)))))
353
354 (defun gnus-dissect-cited-text ()
355 "Dissect the article buffer looking for cited text."
356 (save-excursion
357 (set-buffer gnus-article-buffer)
358 (gnus-cite-parse-maybe)
359 (let ((alist gnus-cite-prefix-alist)
360 prefix numbers number marks m)
361 ;; Loop through citation prefixes.
362 (while alist
363 (setq numbers (pop alist)
364 prefix (pop numbers))
365 (while numbers
366 (setq number (pop numbers))
367 (goto-char (point-min))
368 (forward-line number)
369 (push (cons (point-marker) "") marks)
370 (while (and numbers
371 (= (1- number) (car numbers)))
372 (setq number (pop numbers)))
373 (goto-char (point-min))
374 (forward-line (1- number))
375 (push (cons (point-marker) prefix) marks)))
376 ;; Skip to the beginning of the body.
377 (goto-char (point-min))
378 (search-forward "\n\n" nil t)
379 (push (cons (point-marker) "") marks)
380 ;; Find the end of the body.
381 (goto-char (point-max))
382 (gnus-article-search-signature)
383 (push (cons (point-marker) "") marks)
384 ;; Sort the marks.
385 (setq marks (sort marks (lambda (m1 m2) (< (car m1) (car m2)))))
386 (let ((omarks marks))
387 (setq marks nil)
388 (while (cdr omarks)
389 (if (= (caar omarks) (caadr omarks))
390 (progn
391 (unless (equal (cdar omarks) "")
392 (push (car omarks) marks))
393 (unless (equal (cdadr omarks) "")
394 (push (cadr omarks) marks))
395 (unless (and (equal (cdar omarks) "")
396 (equal (cdadr omarks) "")
397 (not (cddr omarks)))
398 (setq omarks (cdr omarks))))
399 (push (car omarks) marks))
400 (setq omarks (cdr omarks)))
401 (when (car omarks)
402 (push (car omarks) marks))
403 (setq marks (setq m (nreverse marks)))
404 (while (cddr m)
405 (if (and (equal (cdadr m) "")
406 (equal (cdar m) (cdaddr m))
407 (goto-char (caadr m))
408 (forward-line 1)
409 (= (point) (caaddr m)))
410 (setcdr m (cdddr m))
411 (setq m (cdr m))))
412 marks))))
413
414 (defun gnus-article-fill-cited-article (&optional force width)
415 "Do word wrapping in the current article.
416 If WIDTH (the numerical prefix), use that text width when filling."
417 (interactive (list t current-prefix-arg))
418 (save-excursion
419 (set-buffer gnus-article-buffer)
420 (let ((buffer-read-only nil)
421 (inhibit-point-motion-hooks t)
422 (marks (gnus-dissect-cited-text))
423 (adaptive-fill-mode nil)
424 (filladapt-mode nil)
425 (fill-column (if width (prefix-numeric-value width) fill-column)))
426 (save-restriction
427 (while (cdr marks)
428 (widen)
429 (narrow-to-region (caar marks) (caadr marks))
430 (let ((adaptive-fill-regexp
431 (concat "^" (regexp-quote (cdar marks)) " *"))
432 (fill-prefix (cdar marks)))
433 (fill-region (point-min) (point-max)))
434 (set-marker (caar marks) nil)
435 (setq marks (cdr marks)))
436 (when marks
437 (set-marker (caar marks) nil))
438 ;; All this information is now incorrect.
439 (setq gnus-cite-prefix-alist nil
440 gnus-cite-attribution-alist nil
441 gnus-cite-loose-prefix-alist nil
442 gnus-cite-loose-attribution-alist nil)))))
443
444 (defun gnus-article-hide-citation (&optional arg force)
445 "Toggle hiding of all cited text except attribution lines.
446 See the documentation for `gnus-article-highlight-citation'.
447 If given a negative prefix, always show; if given a positive prefix,
448 always hide."
449 (interactive (append (gnus-article-hidden-arg) (list 'force)))
450 (setq gnus-cited-text-button-line-format-spec
451 (gnus-parse-format gnus-cited-text-button-line-format
452 gnus-cited-text-button-line-format-alist t))
453 (save-excursion
454 (set-buffer gnus-article-buffer)
455 (cond
456 ((gnus-article-check-hidden-text 'cite arg)
457 t)
458 ((gnus-article-text-type-exists-p 'cite)
459 (let ((buffer-read-only nil))
460 (gnus-article-hide-text-of-type 'cite)))
461 (t
462 (let ((buffer-read-only nil)
463 (marks (gnus-dissect-cited-text))
464 (inhibit-point-motion-hooks t)
465 (props (nconc (list 'article-type 'cite)
466 gnus-hidden-properties))
467 beg end)
468 (while marks
469 (setq beg nil
470 end nil)
471 (while (and marks (string= (cdar marks) ""))
472 (setq marks (cdr marks)))
473 (when marks
474 (setq beg (caar marks)))
475 (while (and marks (not (string= (cdar marks) "")))
476 (setq marks (cdr marks)))
477 (when marks
478 (setq end (caar marks)))
479 ;; Skip past lines we want to leave visible.
480 (when (and beg end gnus-cited-lines-visible)
481 (goto-char beg)
482 (forward-line gnus-cited-lines-visible)
483 (if (>= (point) end)
484 (setq beg nil)
485 (setq beg (point-marker))))
486 (when (and beg end)
487 (gnus-add-text-properties beg end props)
488 (goto-char beg)
489 (unless (save-excursion (search-backward "\n\n" nil t))
490 (insert "\n"))
491 (put-text-property
492 (point)
493 (progn
494 (gnus-article-add-button
495 (point)
496 (progn (eval gnus-cited-text-button-line-format-spec) (point))
497 `gnus-article-toggle-cited-text (cons beg end))
498 (point))
499 'article-type 'annotation)
500 (set-marker beg (point)))))))))
501
502 (defun gnus-article-toggle-cited-text (region)
503 "Toggle hiding the text in REGION."
504 (let (buffer-read-only)
505 (funcall
506 (if (text-property-any
507 (car region) (1- (cdr region))
508 (car gnus-hidden-properties) (cadr gnus-hidden-properties))
509 'remove-text-properties 'gnus-add-text-properties)
510 (car region) (cdr region) gnus-hidden-properties)))
511
512 (defun gnus-article-hide-citation-maybe (&optional arg force)
513 "Toggle hiding of cited text that has an attribution line.
514 If given a negative prefix, always show; if given a positive prefix,
515 always hide.
516 This will do nothing unless at least `gnus-cite-hide-percentage'
517 percent and at least `gnus-cite-hide-absolute' lines of the body is
518 cited text with attributions. When called interactively, these two
519 variables are ignored.
520 See also the documentation for `gnus-article-highlight-citation'."
521 (interactive (append (gnus-article-hidden-arg) (list 'force)))
522 (unless (gnus-article-check-hidden-text 'cite arg)
523 (save-excursion
524 (set-buffer gnus-article-buffer)
525 (gnus-cite-parse-maybe force)
526 (goto-char (point-min))
527 (search-forward "\n\n" nil t)
528 (let ((start (point))
529 (atts gnus-cite-attribution-alist)
530 (buffer-read-only nil)
531 (inhibit-point-motion-hooks t)
532 (hiden 0)
533 total)
534 (goto-char (point-max))
535 (gnus-article-search-signature)
536 (setq total (count-lines start (point)))
537 (while atts
538 (setq hiden (+ hiden (length (cdr (assoc (cdar atts)
539 gnus-cite-prefix-alist))))
540 atts (cdr atts)))
541 (when (or force
542 (and (> (* 100 hiden) (* gnus-cite-hide-percentage total))
543 (> hiden gnus-cite-hide-absolute)))
544 (setq atts gnus-cite-attribution-alist)
545 (while atts
546 (setq total (cdr (assoc (cdar atts) gnus-cite-prefix-alist))
547 atts (cdr atts))
548 (while total
549 (setq hiden (car total)
550 total (cdr total))
551 (goto-line hiden)
552 (unless (assq hiden gnus-cite-attribution-alist)
553 (gnus-add-text-properties
554 (point) (progn (forward-line 1) (point))
555 (nconc (list 'article-type 'cite)
556 gnus-hidden-properties))))))))))
557
558 (defun gnus-article-hide-citation-in-followups ()
559 "Hide cited text in non-root articles."
560 (interactive)
561 (save-excursion
562 (set-buffer gnus-article-buffer)
563 (let ((article (cdr gnus-article-current)))
564 (unless (save-excursion
565 (set-buffer gnus-summary-buffer)
566 (gnus-article-displayed-root-p article))
567 (gnus-article-hide-citation)))))
568
569 ;;; Internal functions:
570
571 (defun gnus-cite-parse-maybe (&optional force)
572 ;; Parse if the buffer has changes since last time.
573 (if (equal gnus-cite-article gnus-article-current)
574 ()
575 ;;Reset parser information.
576 (setq gnus-cite-prefix-alist nil
577 gnus-cite-attribution-alist nil
578 gnus-cite-loose-prefix-alist nil
579 gnus-cite-loose-attribution-alist nil)
580 ;; Parse if not too large.
581 (if (and (not force)
582 gnus-cite-parse-max-size
583 (> (buffer-size) gnus-cite-parse-max-size))
584 ()
585 (setq gnus-cite-article (cons (car gnus-article-current)
586 (cdr gnus-article-current)))
587 (gnus-cite-parse-wrapper))))
588
589 (defun gnus-cite-parse-wrapper ()
590 ;; Wrap chopped gnus-cite-parse
591 (goto-char (point-min))
592 (unless (search-forward "\n\n" nil t)
593 (goto-char (point-max)))
594 (save-excursion
595 (gnus-cite-parse-attributions))
596 ;; Try to avoid check citation if there is no reason to believe
597 ;; that article has citations
598 (if (or gnus-cite-always-check
599 (save-excursion
600 (re-search-backward gnus-cite-reply-regexp nil t))
601 gnus-cite-loose-attribution-alist)
602 (progn (save-excursion
603 (gnus-cite-parse))
604 (save-excursion
605 (gnus-cite-connect-attributions)))))
606
607 (defun gnus-cite-parse ()
608 ;; Parse and connect citation prefixes and attribution lines.
609
610 ;; Parse current buffer searching for citation prefixes.
611 (let ((line (1+ (count-lines (point-min) (point))))
612 (case-fold-search t)
613 (max (save-excursion
614 (goto-char (point-max))
615 (gnus-article-search-signature)
616 (point)))
617 alist entry start begin end numbers prefix)
618 ;; Get all potential prefixes in `alist'.
619 (while (< (point) max)
620 ;; Each line.
621 (setq begin (point)
622 end (progn (beginning-of-line 2) (point))
623 start end)
624 (goto-char begin)
625 ;; Ignore standard Supercite attribution prefix.
626 (when (looking-at gnus-supercite-regexp)
627 (if (match-end 1)
628 (setq end (1+ (match-end 1)))
629 (setq end (1+ begin))))
630 ;; Ignore very long prefixes.
631 (when (> end (+ (point) gnus-cite-max-prefix))
632 (setq end (+ (point) gnus-cite-max-prefix)))
633 (while (re-search-forward gnus-cite-prefix-regexp (1- end) t)
634 ;; Each prefix.
635 (setq end (match-end 0)
636 prefix (buffer-substring begin end))
637 (gnus-set-text-properties 0 (length prefix) nil prefix)
638 (setq entry (assoc prefix alist))
639 (if entry
640 (setcdr entry (cons line (cdr entry)))
641 (push (list prefix line) alist))
642 (goto-char begin))
643 (goto-char start)
644 (setq line (1+ line)))
645 ;; We got all the potential prefixes. Now create
646 ;; `gnus-cite-prefix-alist' containing the oldest prefix for each
647 ;; line that appears at least gnus-cite-minimum-match-count
648 ;; times. First sort them by length. Longer is older.
649 (setq alist (sort alist (lambda (a b)
650 (> (length (car a)) (length (car b))))))
651 (while alist
652 (setq entry (car alist)
653 prefix (car entry)
654 numbers (cdr entry)
655 alist (cdr alist))
656 (cond ((null numbers)
657 ;; No lines with this prefix that wasn't also part of
658 ;; a longer prefix.
659 )
660 ((< (length numbers) gnus-cite-minimum-match-count)
661 ;; Too few lines with this prefix. We keep it a bit
662 ;; longer in case it is an exact match for an attribution
663 ;; line, but we don't remove the line from other
664 ;; prefixes.
665 (push entry gnus-cite-prefix-alist))
666 (t
667 (push entry
668 gnus-cite-prefix-alist)
669 ;; Remove articles from other prefixes.
670 (let ((loop alist)
671 current)
672 (while loop
673 (setq current (car loop)
674 loop (cdr loop))
675 (setcdr current
676 (gnus-set-difference (cdr current) numbers)))))))))
677
678 (defun gnus-cite-parse-attributions ()
679 (let (al-alist)
680 ;; Parse attributions
681 (while (re-search-forward gnus-cite-attribution-suffix (point-max) t)
682 (let* ((start (match-beginning 0))
683 (end (match-end 0))
684 (wrote (count-lines (point-min) end))
685 (prefix (gnus-cite-find-prefix wrote))
686 ;; Check previous line for an attribution leader.
687 (tag (progn
688 (beginning-of-line 1)
689 (when (looking-at gnus-supercite-secondary-regexp)
690 (buffer-substring (match-beginning 1)
691 (match-end 1)))))
692 (in (progn
693 (goto-char start)
694 (and (re-search-backward gnus-cite-attribution-prefix
695 (save-excursion
696 (beginning-of-line 0)
697 (point))
698 t)
699 (not (re-search-forward gnus-cite-attribution-suffix
700 start t))
701 (count-lines (point-min) (1+ (point)))))))
702 (when (eq wrote in)
703 (setq in nil))
704 (goto-char end)
705 ;; don't add duplicates
706 (let ((al (buffer-substring (save-excursion (beginning-of-line 0)
707 (1+ (point)))
708 end)))
709 (if (not (assoc al al-alist))
710 (progn
711 (push (list wrote in prefix tag)
712 gnus-cite-loose-attribution-alist)
713 (push (cons al t) al-alist))))))))
714
715 (defun gnus-cite-connect-attributions ()
716 ;; Connect attributions to citations
717
718 ;; No citations have been connected to attribution lines yet.
719 (setq gnus-cite-loose-prefix-alist (append gnus-cite-prefix-alist nil))
720
721 ;; Parse current buffer searching for attribution lines.
722 ;; Find exact supercite citations.
723 (gnus-cite-match-attributions 'small nil
724 (lambda (prefix tag)
725 (when tag
726 (concat "\\`"
727 (regexp-quote prefix) "[ \t]*"
728 (regexp-quote tag) ">"))))
729 ;; Find loose supercite citations after attributions.
730 (gnus-cite-match-attributions 'small t
731 (lambda (prefix tag)
732 (when tag
733 (concat "\\<"
734 (regexp-quote tag)
735 "\\>"))))
736 ;; Find loose supercite citations anywhere.
737 (gnus-cite-match-attributions 'small nil
738 (lambda (prefix tag)
739 (when tag
740 (concat "\\<"
741 (regexp-quote tag)
742 "\\>"))))
743 ;; Find nested citations after attributions.
744 (gnus-cite-match-attributions 'small-if-unique t
745 (lambda (prefix tag)
746 (concat "\\`" (regexp-quote prefix) ".+")))
747 ;; Find nested citations anywhere.
748 (gnus-cite-match-attributions 'small nil
749 (lambda (prefix tag)
750 (concat "\\`" (regexp-quote prefix) ".+")))
751 ;; Remove loose prefixes with too few lines.
752 (let ((alist gnus-cite-loose-prefix-alist)
753 entry)
754 (while alist
755 (setq entry (car alist)
756 alist (cdr alist))
757 (when (< (length (cdr entry)) gnus-cite-minimum-match-count)
758 (setq gnus-cite-prefix-alist
759 (delq entry gnus-cite-prefix-alist)
760 gnus-cite-loose-prefix-alist
761 (delq entry gnus-cite-loose-prefix-alist)))))
762 ;; Find flat attributions.
763 (gnus-cite-match-attributions 'first t nil)
764 ;; Find any attributions (are we getting desperate yet?).
765 (gnus-cite-match-attributions 'first nil nil))
766
767 (defun gnus-cite-match-attributions (sort after fun)
768 ;; Match all loose attributions and citations (SORT AFTER FUN) .
769 ;;
770 ;; If SORT is `small', the citation with the shortest prefix will be
771 ;; used, if it is `first' the first prefix will be used, if it is
772 ;; `small-if-unique' the shortest prefix will be used if the
773 ;; attribution line does not share its own prefix with other
774 ;; loose attribution lines, otherwise the first prefix will be used.
775 ;;
776 ;; If AFTER is non-nil, only citations after the attribution line
777 ;; will be considered.
778 ;;
779 ;; If FUN is non-nil, it will be called with the arguments (WROTE
780 ;; PREFIX TAG) and expected to return a regular expression. Only
781 ;; citations whose prefix matches the regular expression will be
782 ;; considered.
783 ;;
784 ;; WROTE is the attribution line number.
785 ;; PREFIX is the attribution line prefix.
786 ;; TAG is the Supercite tag on the attribution line.
787 (let ((atts gnus-cite-loose-attribution-alist)
788 (case-fold-search t)
789 att wrote in prefix tag regexp limit smallest best size)
790 (while atts
791 (setq att (car atts)
792 atts (cdr atts)
793 wrote (nth 0 att)
794 in (nth 1 att)
795 prefix (nth 2 att)
796 tag (nth 3 att)
797 regexp (if fun (funcall fun prefix tag) "")
798 size (cond ((eq sort 'small) t)
799 ((eq sort 'first) nil)
800 (t (< (length (gnus-cite-find-loose prefix)) 2)))
801 limit (if after wrote -1)
802 smallest 1000000
803 best nil)
804 (let ((cites gnus-cite-loose-prefix-alist)
805 cite candidate numbers first compare)
806 (while cites
807 (setq cite (car cites)
808 cites (cdr cites)
809 candidate (car cite)
810 numbers (cdr cite)
811 first (apply 'min numbers)
812 compare (if size (length candidate) first))
813 (and (> first limit)
814 regexp
815 (string-match regexp candidate)
816 (< compare smallest)
817 (setq best cite
818 smallest compare))))
819 (if (null best)
820 ()
821 (setq gnus-cite-loose-attribution-alist
822 (delq att gnus-cite-loose-attribution-alist))
823 (push (cons wrote (car best)) gnus-cite-attribution-alist)
824 (when in
825 (push (cons in (car best)) gnus-cite-attribution-alist))
826 (when (memq best gnus-cite-loose-prefix-alist)
827 (let ((loop gnus-cite-prefix-alist)
828 (numbers (cdr best))
829 current)
830 (setq gnus-cite-loose-prefix-alist
831 (delq best gnus-cite-loose-prefix-alist))
832 (while loop
833 (setq current (car loop)
834 loop (cdr loop))
835 (if (eq current best)
836 ()
837 (setcdr current (gnus-set-difference (cdr current) numbers))
838 (when (null (cdr current))
839 (setq gnus-cite-loose-prefix-alist
840 (delq current gnus-cite-loose-prefix-alist)
841 atts (delq current atts)))))))))))
842
843 (defun gnus-cite-find-loose (prefix)
844 ;; Return a list of loose attribution lines prefixed by PREFIX.
845 (let* ((atts gnus-cite-loose-attribution-alist)
846 att line lines)
847 (while atts
848 (setq att (car atts)
849 line (car att)
850 atts (cdr atts))
851 (when (string-equal (gnus-cite-find-prefix line) prefix)
852 (push line lines)))
853 lines))
854
855 (defun gnus-cite-add-face (number prefix face)
856 ;; At line NUMBER, ignore PREFIX and add FACE to the rest of the line.
857 (when face
858 (let ((inhibit-point-motion-hooks t)
859 from to)
860 (goto-line number)
861 (unless (eobp);; Sometimes things become confused.
862 (forward-char (length prefix))
863 (skip-chars-forward " \t")
864 (setq from (point))
865 (end-of-line 1)
866 (skip-chars-backward " \t")
867 (setq to (point))
868 (when (< from to)
869 (gnus-overlay-put (gnus-make-overlay from to) 'face face))))))
870
871 (defun gnus-cite-toggle (prefix)
872 (save-excursion
873 (set-buffer gnus-article-buffer)
874 (let ((buffer-read-only nil)
875 (numbers (cdr (assoc prefix gnus-cite-prefix-alist)))
876 (inhibit-point-motion-hooks t)
877 number)
878 (while numbers
879 (setq number (car numbers)
880 numbers (cdr numbers))
881 (goto-line number)
882 (cond ((get-text-property (point) 'invisible)
883 (remove-text-properties (point) (progn (forward-line 1) (point))
884 gnus-hidden-properties))
885 ((assq number gnus-cite-attribution-alist))
886 (t
887 (gnus-add-text-properties
888 (point) (progn (forward-line 1) (point))
889 (nconc (list 'article-type 'cite)
890 gnus-hidden-properties))))))))
891
892 (defun gnus-cite-find-prefix (line)
893 ;; Return citation prefix for LINE.
894 (let ((alist gnus-cite-prefix-alist)
895 (prefix "")
896 entry)
897 (while alist
898 (setq entry (car alist)
899 alist (cdr alist))
900 (when (memq line (cdr entry))
901 (setq prefix (car entry))))
902 prefix))
903
904 (gnus-add-shutdown 'gnus-cache-close 'gnus)
905
906 (defun gnus-cache-close ()
907 (setq gnus-cite-prefix-alist nil))
908
909 (gnus-ems-redefine)
910
911 (provide 'gnus-cite)
912
913 ;;; gnus-cite.el ends here