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