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