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