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