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