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