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