Mercurial > emacs
comparison lisp/gnus/gnus-cite.el @ 17493:e6935c08cf0b
Initial revision
author | Lars Magne Ingebrigtsen <larsi@gnus.org> |
---|---|
date | Wed, 16 Apr 1997 22:13:18 +0000 |
parents | |
children | 6f6cf9184e93 |
comparison
equal
deleted
inserted
replaced
17492:59dc7e0fc186 | 17493:e6935c08cf0b |
---|---|
1 ;;; gnus-cite.el --- parse citations in articles for Gnus | |
2 ;; Copyright (C) 1995,96,97 Free Software Foundation, Inc. | |
3 | |
4 ;; Author: Per Abrahamsen <abraham@iesd.auc.dk> | |
5 ;; Keywords: news, mail | |
6 | |
7 ;; This file is part of GNU Emacs. | |
8 | |
9 ;; GNU Emacs is free software; you can redistribute it and/or modify | |
10 ;; it under the terms of the GNU General Public License as published by | |
11 ;; the Free Software Foundation; either version 2, or (at your option) | |
12 ;; any later version. | |
13 | |
14 ;; GNU Emacs is distributed in the hope that it will be useful, | |
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 ;; GNU General Public License for more details. | |
18 | |
19 ;; You should have received a copy of the GNU General Public License | |
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
22 ;; Boston, MA 02111-1307, USA. | |
23 | |
24 ;;; Commentary: | |
25 | |
26 ;;; Code: | |
27 | |
28 (require 'gnus) | |
29 (require 'gnus-art) | |
30 (require 'gnus-range) | |
31 | |
32 ;;; Customization: | |
33 | |
34 (defgroup gnus-cite nil | |
35 "Citation." | |
36 :prefix "gnus-cite-" | |
37 :link '(custom-manual "(gnus)Article Highlighting") | |
38 :group 'gnus-article) | |
39 | |
40 (defcustom gnus-cite-reply-regexp | |
41 "^\\(Subject: Re\\|In-Reply-To\\|References\\):" | |
42 "If headers match this regexp it is reasonable to believe that | |
43 article has citations." | |
44 :group 'gnus-cite | |
45 :type 'string) | |
46 | |
47 (defcustom gnus-cite-always-check nil | |
48 "Check article always for citations. Set it t to check all articles." | |
49 :group 'gnus-cite | |
50 :type '(choice (const :tag "no" nil) | |
51 (const :tag "yes" t))) | |
52 | |
53 (defcustom gnus-cited-text-button-line-format "%(%{[...]%}%)\n" | |
54 "Format of cited text buttons." | |
55 :group 'gnus-cite | |
56 :type 'string) | |
57 | |
58 (defcustom gnus-cited-lines-visible nil | |
59 "The number of lines of hidden cited text to remain visible." | |
60 :group 'gnus-cite | |
61 :type '(choice (const :tag "none" nil) | |
62 integer)) | |
63 | |
64 (defcustom gnus-cite-parse-max-size 25000 | |
65 "Maximum article size (in bytes) where parsing citations is allowed. | |
66 Set it to nil to parse all articles." | |
67 :group 'gnus-cite | |
68 :type '(choice (const :tag "all" nil) | |
69 integer)) | |
70 | |
71 (defcustom gnus-cite-prefix-regexp | |
72 "^[]>|:}+ ]*[]>|:}+]\\(.*>\\)?\\|^.*>" | |
73 "Regexp matching the longest possible citation prefix on a line." | |
74 :group 'gnus-cite | |
75 :type 'regexp) | |
76 | |
77 (defcustom gnus-cite-max-prefix 20 | |
78 "Maximum possible length for a citation prefix." | |
79 :group 'gnus-cite | |
80 :type 'integer) | |
81 | |
82 (defcustom gnus-supercite-regexp | |
83 (concat "^\\(" gnus-cite-prefix-regexp "\\)? *" | |
84 ">>>>> +\"\\([^\"\n]+\\)\" +==") | |
85 "Regexp matching normal Supercite attribution lines. | |
86 The first grouping must match prefixes added by other packages." | |
87 :group 'gnus-cite | |
88 :type 'regexp) | |
89 | |
90 (defcustom gnus-supercite-secondary-regexp "^.*\"\\([^\"\n]+\\)\" +==" | |
91 "Regexp matching mangled Supercite attribution lines. | |
92 The first regexp group should match the Supercite attribution." | |
93 :group 'gnus-cite | |
94 :type 'regexp) | |
95 | |
96 (defcustom gnus-cite-minimum-match-count 2 | |
97 "Minimum number of identical prefixes before we believe it's a citation." | |
98 :group 'gnus-cite | |
99 :type 'integer) | |
100 | |
101 (defcustom gnus-cite-attribution-prefix "in article\\|in <" | |
102 "Regexp matching the beginning of an attribution line." | |
103 :group 'gnus-cite | |
104 :type 'regexp) | |
105 | |
106 (defcustom gnus-cite-attribution-suffix | |
107 "\\(wrote\\|writes\\|said\\|says\\):[ \t]*$" | |
108 "Regexp matching the end of an attribution line. | |
109 The text matching the first grouping will be used as a button." | |
110 :group 'gnus-cite | |
111 :type 'regexp) | |
112 | |
113 (defface gnus-cite-attribution-face '((t | |
114 (:underline t))) | |
115 "Face used for attribution lines.") | |
116 | |
117 (defcustom gnus-cite-attribution-face 'gnus-cite-attribution-face | |
118 "Face used for attribution lines. | |
119 It is merged with the face for the cited text belonging to the attribution." | |
120 :group 'gnus-cite | |
121 :type 'face) | |
122 | |
123 (defface gnus-cite-face-1 '((((class color) | |
124 (background dark)) | |
125 (:foreground "light blue")) | |
126 (((class color) | |
127 (background light)) | |
128 (:foreground "MidnightBlue")) | |
129 (t | |
130 (:italic t))) | |
131 "Citation face.") | |
132 | |
133 (defface gnus-cite-face-2 '((((class color) | |
134 (background dark)) | |
135 (:foreground "light cyan")) | |
136 (((class color) | |
137 (background light)) | |
138 (:foreground "firebrick")) | |
139 (t | |
140 (:italic t))) | |
141 "Citation face.") | |
142 | |
143 (defface gnus-cite-face-3 '((((class color) | |
144 (background dark)) | |
145 (:foreground "light yellow")) | |
146 (((class color) | |
147 (background light)) | |
148 (:foreground "dark green")) | |
149 (t | |
150 (:italic t))) | |
151 "Citation face.") | |
152 | |
153 (defface gnus-cite-face-4 '((((class color) | |
154 (background dark)) | |
155 (:foreground "light pink")) | |
156 (((class color) | |
157 (background light)) | |
158 (:foreground "OrangeRed")) | |
159 (t | |
160 (:italic t))) | |
161 "Citation face.") | |
162 | |
163 (defface gnus-cite-face-5 '((((class color) | |
164 (background dark)) | |
165 (:foreground "pale green")) | |
166 (((class color) | |
167 (background light)) | |
168 (:foreground "dark khaki")) | |
169 (t | |
170 (:italic t))) | |
171 "Citation face.") | |
172 | |
173 (defface gnus-cite-face-6 '((((class color) | |
174 (background dark)) | |
175 (:foreground "beige")) | |
176 (((class color) | |
177 (background light)) | |
178 (:foreground "dark violet")) | |
179 (t | |
180 (:italic t))) | |
181 "Citation face.") | |
182 | |
183 (defface gnus-cite-face-7 '((((class color) | |
184 (background dark)) | |
185 (:foreground "orange")) | |
186 (((class color) | |
187 (background light)) | |
188 (:foreground "SteelBlue4")) | |
189 (t | |
190 (:italic t))) | |
191 "Citation face.") | |
192 | |
193 (defface gnus-cite-face-8 '((((class color) | |
194 (background dark)) | |
195 (:foreground "magenta")) | |
196 (((class color) | |
197 (background light)) | |
198 (:foreground "magenta")) | |
199 (t | |
200 (:italic t))) | |
201 "Citation face.") | |
202 | |
203 (defface gnus-cite-face-9 '((((class color) | |
204 (background dark)) | |
205 (:foreground "violet")) | |
206 (((class color) | |
207 (background light)) | |
208 (:foreground "violet")) | |
209 (t | |
210 (:italic t))) | |
211 "Citation face.") | |
212 | |
213 (defface gnus-cite-face-10 '((((class color) | |
214 (background dark)) | |
215 (:foreground "medium purple")) | |
216 (((class color) | |
217 (background light)) | |
218 (:foreground "medium purple")) | |
219 (t | |
220 (:italic t))) | |
221 "Citation face.") | |
222 | |
223 (defface gnus-cite-face-11 '((((class color) | |
224 (background dark)) | |
225 (:foreground "turquoise")) | |
226 (((class color) | |
227 (background light)) | |
228 (:foreground "turquoise")) | |
229 (t | |
230 (:italic t))) | |
231 "Citation face.") | |
232 | |
233 (defcustom gnus-cite-face-list | |
234 '(gnus-cite-face-1 gnus-cite-face-2 gnus-cite-face-3 gnus-cite-face-4 | |
235 gnus-cite-face-5 gnus-cite-face-6 gnus-cite-face-7 gnus-cite-face-8 | |
236 gnus-cite-face-9 gnus-cite-face-10 gnus-cite-face-11) | |
237 "List of faces used for highlighting citations. | |
238 | |
239 When there are citations from multiple articles in the same message, | |
240 Gnus will try to give each citation from each article its own face. | |
241 This should make it easier to see who wrote what." | |
242 :group 'gnus-cite | |
243 :type '(repeat face)) | |
244 | |
245 (defcustom gnus-cite-hide-percentage 50 | |
246 "Only hide excess citation if above this percentage of the body." | |
247 :group 'gnus-cite | |
248 :type 'number) | |
249 | |
250 (defcustom gnus-cite-hide-absolute 10 | |
251 "Only hide excess citation if above this number of lines in the body." | |
252 :group 'gnus-cite | |
253 :type 'integer) | |
254 | |
255 ;;; Internal Variables: | |
256 | |
257 (defvar gnus-cite-article nil) | |
258 | |
259 (defvar gnus-cite-prefix-alist nil) | |
260 ;; Alist of citation prefixes. | |
261 ;; The cdr is a list of lines with that prefix. | |
262 | |
263 (defvar gnus-cite-attribution-alist nil) | |
264 ;; Alist of attribution lines. | |
265 ;; The car is a line number. | |
266 ;; The cdr is the prefix for the citation started by that line. | |
267 | |
268 (defvar gnus-cite-loose-prefix-alist nil) | |
269 ;; Alist of citation prefixes that have no matching attribution. | |
270 ;; The cdr is a list of lines with that prefix. | |
271 | |
272 (defvar gnus-cite-loose-attribution-alist nil) | |
273 ;; Alist of attribution lines that have no matching citation. | |
274 ;; Each member has the form (WROTE IN PREFIX TAG), where | |
275 ;; WROTE: is the attribution line number | |
276 ;; IN: is the line number of the previous line if part of the same attribution, | |
277 ;; PREFIX: Is the citation prefix of the attribution line(s), and | |
278 ;; TAG: Is a Supercite tag, if any. | |
279 | |
280 (defvar gnus-cited-text-button-line-format-alist | |
281 `((?b (marker-position beg) ?d) | |
282 (?e (marker-position end) ?d) | |
283 (?l (- end beg) ?d))) | |
284 (defvar gnus-cited-text-button-line-format-spec nil) | |
285 | |
286 ;;; Commands: | |
287 | |
288 (defun gnus-article-highlight-citation (&optional force) | |
289 "Highlight cited text. | |
290 Each citation in the article will be highlighted with a different face. | |
291 The faces are taken from `gnus-cite-face-list'. | |
292 Attribution lines are highlighted with the same face as the | |
293 corresponding citation merged with `gnus-cite-attribution-face'. | |
294 | |
295 Text is considered cited if at least `gnus-cite-minimum-match-count' | |
296 lines matches `gnus-cite-prefix-regexp' with the same prefix. | |
297 | |
298 Lines matching `gnus-cite-attribution-suffix' and perhaps | |
299 `gnus-cite-attribution-prefix' are considered attribution lines." | |
300 (interactive (list 'force)) | |
301 (save-excursion | |
302 (set-buffer gnus-article-buffer) | |
303 (gnus-cite-parse-maybe force) | |
304 (let ((buffer-read-only nil) | |
305 (alist gnus-cite-prefix-alist) | |
306 (faces gnus-cite-face-list) | |
307 (inhibit-point-motion-hooks t) | |
308 face entry prefix skip numbers number face-alist) | |
309 ;; Loop through citation prefixes. | |
310 (while alist | |
311 (setq entry (car alist) | |
312 alist (cdr alist) | |
313 prefix (car entry) | |
314 numbers (cdr entry) | |
315 face (car faces) | |
316 faces (or (cdr faces) gnus-cite-face-list) | |
317 face-alist (cons (cons prefix face) face-alist)) | |
318 (while numbers | |
319 (setq number (car numbers) | |
320 numbers (cdr numbers)) | |
321 (and (not (assq number gnus-cite-attribution-alist)) | |
322 (not (assq number gnus-cite-loose-attribution-alist)) | |
323 (gnus-cite-add-face number prefix face)))) | |
324 ;; Loop through attribution lines. | |
325 (setq alist gnus-cite-attribution-alist) | |
326 (while alist | |
327 (setq entry (car alist) | |
328 alist (cdr alist) | |
329 number (car entry) | |
330 prefix (cdr entry) | |
331 skip (gnus-cite-find-prefix number) | |
332 face (cdr (assoc prefix face-alist))) | |
333 ;; Add attribution button. | |
334 (goto-line number) | |
335 (when (re-search-forward gnus-cite-attribution-suffix | |
336 (save-excursion (end-of-line 1) (point)) | |
337 t) | |
338 (gnus-article-add-button (match-beginning 1) (match-end 1) | |
339 'gnus-cite-toggle prefix)) | |
340 ;; Highlight attribution line. | |
341 (gnus-cite-add-face number skip face) | |
342 (gnus-cite-add-face number skip gnus-cite-attribution-face)) | |
343 ;; Loop through attribution lines. | |
344 (setq alist gnus-cite-loose-attribution-alist) | |
345 (while alist | |
346 (setq entry (car alist) | |
347 alist (cdr alist) | |
348 number (car entry) | |
349 skip (gnus-cite-find-prefix number)) | |
350 (gnus-cite-add-face number skip gnus-cite-attribution-face))))) | |
351 | |
352 (defun gnus-dissect-cited-text () | |
353 "Dissect the article buffer looking for cited text." | |
354 (save-excursion | |
355 (set-buffer gnus-article-buffer) | |
356 (gnus-cite-parse-maybe) | |
357 (let ((alist gnus-cite-prefix-alist) | |
358 prefix numbers number marks m) | |
359 ;; Loop through citation prefixes. | |
360 (while alist | |
361 (setq numbers (pop alist) | |
362 prefix (pop numbers)) | |
363 (while numbers | |
364 (setq number (pop numbers)) | |
365 (goto-char (point-min)) | |
366 (forward-line number) | |
367 (push (cons (point-marker) "") marks) | |
368 (while (and numbers | |
369 (= (1- number) (car numbers))) | |
370 (setq number (pop numbers))) | |
371 (goto-char (point-min)) | |
372 (forward-line (1- number)) | |
373 (push (cons (point-marker) prefix) marks))) | |
374 ;; Skip to the beginning of the body. | |
375 (goto-char (point-min)) | |
376 (search-forward "\n\n" nil t) | |
377 (push (cons (point-marker) "") marks) | |
378 ;; Find the end of the body. | |
379 (goto-char (point-max)) | |
380 (gnus-article-search-signature) | |
381 (push (cons (point-marker) "") marks) | |
382 ;; Sort the marks. | |
383 (setq marks (sort marks (lambda (m1 m2) (< (car m1) (car m2))))) | |
384 (let ((omarks marks)) | |
385 (setq marks nil) | |
386 (while (cdr omarks) | |
387 (if (= (caar omarks) (caadr omarks)) | |
388 (progn | |
389 (unless (equal (cdar omarks) "") | |
390 (push (car omarks) marks)) | |
391 (unless (equal (cdadr omarks) "") | |
392 (push (cadr omarks) marks)) | |
393 (unless (and (equal (cdar omarks) "") | |
394 (equal (cdadr omarks) "") | |
395 (not (cddr omarks))) | |
396 (setq omarks (cdr omarks)))) | |
397 (push (car omarks) marks)) | |
398 (setq omarks (cdr omarks))) | |
399 (when (car omarks) | |
400 (push (car omarks) marks)) | |
401 (setq marks (setq m (nreverse marks))) | |
402 (while (cddr m) | |
403 (if (and (equal (cdadr m) "") | |
404 (equal (cdar m) (cdaddr m)) | |
405 (goto-char (caadr m)) | |
406 (forward-line 1) | |
407 (= (point) (caaddr m))) | |
408 (setcdr m (cdddr m)) | |
409 (setq m (cdr m)))) | |
410 marks)))) | |
411 | |
412 (defun gnus-article-fill-cited-article (&optional force width) | |
413 "Do word wrapping in the current article. | |
414 If WIDTH (the numerical prefix), use that text width when filling." | |
415 (interactive (list t current-prefix-arg)) | |
416 (save-excursion | |
417 (set-buffer gnus-article-buffer) | |
418 (let ((buffer-read-only nil) | |
419 (inhibit-point-motion-hooks t) | |
420 (marks (gnus-dissect-cited-text)) | |
421 (adaptive-fill-mode nil) | |
422 (filladapt-mode nil) | |
423 (fill-column (if width (prefix-numeric-value width) fill-column))) | |
424 (save-restriction | |
425 (while (cdr marks) | |
426 (widen) | |
427 (narrow-to-region (caar marks) (caadr marks)) | |
428 (let ((adaptive-fill-regexp | |
429 (concat "^" (regexp-quote (cdar marks)) " *")) | |
430 (fill-prefix (cdar marks))) | |
431 (fill-region (point-min) (point-max))) | |
432 (set-marker (caar marks) nil) | |
433 (setq marks (cdr marks))) | |
434 (when marks | |
435 (set-marker (caar marks) nil)) | |
436 ;; All this information is now incorrect. | |
437 (setq gnus-cite-prefix-alist nil | |
438 gnus-cite-attribution-alist nil | |
439 gnus-cite-loose-prefix-alist nil | |
440 gnus-cite-loose-attribution-alist nil))))) | |
441 | |
442 (defun gnus-article-hide-citation (&optional arg force) | |
443 "Toggle hiding of all cited text except attribution lines. | |
444 See the documentation for `gnus-article-highlight-citation'. | |
445 If given a negative prefix, always show; if given a positive prefix, | |
446 always hide." | |
447 (interactive (append (gnus-article-hidden-arg) (list 'force))) | |
448 (setq gnus-cited-text-button-line-format-spec | |
449 (gnus-parse-format gnus-cited-text-button-line-format | |
450 gnus-cited-text-button-line-format-alist t)) | |
451 (save-excursion | |
452 (set-buffer gnus-article-buffer) | |
453 (cond | |
454 ((gnus-article-check-hidden-text 'cite arg) | |
455 t) | |
456 ((gnus-article-text-type-exists-p 'cite) | |
457 (let ((buffer-read-only nil)) | |
458 (gnus-article-hide-text-of-type 'cite))) | |
459 (t | |
460 (let ((buffer-read-only nil) | |
461 (marks (gnus-dissect-cited-text)) | |
462 (inhibit-point-motion-hooks t) | |
463 (props (nconc (list 'article-type 'cite) | |
464 gnus-hidden-properties)) | |
465 beg end) | |
466 (while marks | |
467 (setq beg nil | |
468 end nil) | |
469 (while (and marks (string= (cdar marks) "")) | |
470 (setq marks (cdr marks))) | |
471 (when marks | |
472 (setq beg (caar marks))) | |
473 (while (and marks (not (string= (cdar marks) ""))) | |
474 (setq marks (cdr marks))) | |
475 (when marks | |
476 (setq end (caar marks))) | |
477 ;; Skip past lines we want to leave visible. | |
478 (when (and beg end gnus-cited-lines-visible) | |
479 (goto-char beg) | |
480 (forward-line gnus-cited-lines-visible) | |
481 (if (>= (point) end) | |
482 (setq beg nil) | |
483 (setq beg (point-marker)))) | |
484 (when (and beg end) | |
485 (gnus-add-text-properties beg end props) | |
486 (goto-char beg) | |
487 (unless (save-excursion (search-backward "\n\n" nil t)) | |
488 (insert "\n")) | |
489 (put-text-property | |
490 (point) | |
491 (progn | |
492 (gnus-article-add-button | |
493 (point) | |
494 (progn (eval gnus-cited-text-button-line-format-spec) (point)) | |
495 `gnus-article-toggle-cited-text (cons beg end)) | |
496 (point)) | |
497 'article-type 'annotation) | |
498 (set-marker beg (point))))))))) | |
499 | |
500 (defun gnus-article-toggle-cited-text (region) | |
501 "Toggle hiding the text in REGION." | |
502 (let (buffer-read-only) | |
503 (funcall | |
504 (if (text-property-any | |
505 (car region) (1- (cdr region)) | |
506 (car gnus-hidden-properties) (cadr gnus-hidden-properties)) | |
507 'remove-text-properties 'gnus-add-text-properties) | |
508 (car region) (cdr region) gnus-hidden-properties))) | |
509 | |
510 (defun gnus-article-hide-citation-maybe (&optional arg force) | |
511 "Toggle hiding of cited text that has an attribution line. | |
512 If given a negative prefix, always show; if given a positive prefix, | |
513 always hide. | |
514 This will do nothing unless at least `gnus-cite-hide-percentage' | |
515 percent and at least `gnus-cite-hide-absolute' lines of the body is | |
516 cited text with attributions. When called interactively, these two | |
517 variables are ignored. | |
518 See also the documentation for `gnus-article-highlight-citation'." | |
519 (interactive (append (gnus-article-hidden-arg) (list 'force))) | |
520 (unless (gnus-article-check-hidden-text 'cite arg) | |
521 (save-excursion | |
522 (set-buffer gnus-article-buffer) | |
523 (gnus-cite-parse-maybe force) | |
524 (goto-char (point-min)) | |
525 (search-forward "\n\n" nil t) | |
526 (let ((start (point)) | |
527 (atts gnus-cite-attribution-alist) | |
528 (buffer-read-only nil) | |
529 (inhibit-point-motion-hooks t) | |
530 (hiden 0) | |
531 total) | |
532 (goto-char (point-max)) | |
533 (gnus-article-search-signature) | |
534 (setq total (count-lines start (point))) | |
535 (while atts | |
536 (setq hiden (+ hiden (length (cdr (assoc (cdar atts) | |
537 gnus-cite-prefix-alist)))) | |
538 atts (cdr atts))) | |
539 (when (or force | |
540 (and (> (* 100 hiden) (* gnus-cite-hide-percentage total)) | |
541 (> hiden gnus-cite-hide-absolute))) | |
542 (setq atts gnus-cite-attribution-alist) | |
543 (while atts | |
544 (setq total (cdr (assoc (cdar atts) gnus-cite-prefix-alist)) | |
545 atts (cdr atts)) | |
546 (while total | |
547 (setq hiden (car total) | |
548 total (cdr total)) | |
549 (goto-line hiden) | |
550 (unless (assq hiden gnus-cite-attribution-alist) | |
551 (gnus-add-text-properties | |
552 (point) (progn (forward-line 1) (point)) | |
553 (nconc (list 'article-type 'cite) | |
554 gnus-hidden-properties)))))))))) | |
555 | |
556 (defun gnus-article-hide-citation-in-followups () | |
557 "Hide cited text in non-root articles." | |
558 (interactive) | |
559 (save-excursion | |
560 (set-buffer gnus-article-buffer) | |
561 (let ((article (cdr gnus-article-current))) | |
562 (unless (save-excursion | |
563 (set-buffer gnus-summary-buffer) | |
564 (gnus-article-displayed-root-p article)) | |
565 (gnus-article-hide-citation))))) | |
566 | |
567 ;;; Internal functions: | |
568 | |
569 (defun gnus-cite-parse-maybe (&optional force) | |
570 ;; Parse if the buffer has changes since last time. | |
571 (if (equal gnus-cite-article gnus-article-current) | |
572 () | |
573 ;;Reset parser information. | |
574 (setq gnus-cite-prefix-alist nil | |
575 gnus-cite-attribution-alist nil | |
576 gnus-cite-loose-prefix-alist nil | |
577 gnus-cite-loose-attribution-alist nil) | |
578 ;; Parse if not too large. | |
579 (if (and (not force) | |
580 gnus-cite-parse-max-size | |
581 (> (buffer-size) gnus-cite-parse-max-size)) | |
582 () | |
583 (setq gnus-cite-article (cons (car gnus-article-current) | |
584 (cdr gnus-article-current))) | |
585 (gnus-cite-parse-wrapper)))) | |
586 | |
587 (defun gnus-cite-parse-wrapper () | |
588 ;; Wrap chopped gnus-cite-parse | |
589 (goto-char (point-min)) | |
590 (unless (search-forward "\n\n" nil t) | |
591 (goto-char (point-max))) | |
592 (save-excursion | |
593 (gnus-cite-parse-attributions)) | |
594 ;; Try to avoid check citation if there is no reason to believe | |
595 ;; that article has citations | |
596 (if (or gnus-cite-always-check | |
597 (save-excursion | |
598 (re-search-backward gnus-cite-reply-regexp nil t)) | |
599 gnus-cite-loose-attribution-alist) | |
600 (progn (save-excursion | |
601 (gnus-cite-parse)) | |
602 (save-excursion | |
603 (gnus-cite-connect-attributions))))) | |
604 | |
605 (defun gnus-cite-parse () | |
606 ;; Parse and connect citation prefixes and attribution lines. | |
607 | |
608 ;; Parse current buffer searching for citation prefixes. | |
609 (let ((line (1+ (count-lines (point-min) (point)))) | |
610 (case-fold-search t) | |
611 (max (save-excursion | |
612 (goto-char (point-max)) | |
613 (gnus-article-search-signature) | |
614 (point))) | |
615 alist entry start begin end numbers prefix) | |
616 ;; Get all potential prefixes in `alist'. | |
617 (while (< (point) max) | |
618 ;; Each line. | |
619 (setq begin (point) | |
620 end (progn (beginning-of-line 2) (point)) | |
621 start end) | |
622 (goto-char begin) | |
623 ;; Ignore standard Supercite attribution prefix. | |
624 (when (looking-at gnus-supercite-regexp) | |
625 (if (match-end 1) | |
626 (setq end (1+ (match-end 1))) | |
627 (setq end (1+ begin)))) | |
628 ;; Ignore very long prefixes. | |
629 (when (> end (+ (point) gnus-cite-max-prefix)) | |
630 (setq end (+ (point) gnus-cite-max-prefix))) | |
631 (while (re-search-forward gnus-cite-prefix-regexp (1- end) t) | |
632 ;; Each prefix. | |
633 (setq end (match-end 0) | |
634 prefix (buffer-substring begin end)) | |
635 (gnus-set-text-properties 0 (length prefix) nil prefix) | |
636 (setq entry (assoc prefix alist)) | |
637 (if entry | |
638 (setcdr entry (cons line (cdr entry))) | |
639 (push (list prefix line) alist)) | |
640 (goto-char begin)) | |
641 (goto-char start) | |
642 (setq line (1+ line))) | |
643 ;; We got all the potential prefixes. Now create | |
644 ;; `gnus-cite-prefix-alist' containing the oldest prefix for each | |
645 ;; line that appears at least gnus-cite-minimum-match-count | |
646 ;; times. First sort them by length. Longer is older. | |
647 (setq alist (sort alist (lambda (a b) | |
648 (> (length (car a)) (length (car b)))))) | |
649 (while alist | |
650 (setq entry (car alist) | |
651 prefix (car entry) | |
652 numbers (cdr entry) | |
653 alist (cdr alist)) | |
654 (cond ((null numbers) | |
655 ;; No lines with this prefix that wasn't also part of | |
656 ;; a longer prefix. | |
657 ) | |
658 ((< (length numbers) gnus-cite-minimum-match-count) | |
659 ;; Too few lines with this prefix. We keep it a bit | |
660 ;; longer in case it is an exact match for an attribution | |
661 ;; line, but we don't remove the line from other | |
662 ;; prefixes. | |
663 (push entry gnus-cite-prefix-alist)) | |
664 (t | |
665 (push entry | |
666 gnus-cite-prefix-alist) | |
667 ;; Remove articles from other prefixes. | |
668 (let ((loop alist) | |
669 current) | |
670 (while loop | |
671 (setq current (car loop) | |
672 loop (cdr loop)) | |
673 (setcdr current | |
674 (gnus-set-difference (cdr current) numbers))))))))) | |
675 | |
676 (defun gnus-cite-parse-attributions () | |
677 (let (al-alist) | |
678 ;; Parse attributions | |
679 (while (re-search-forward gnus-cite-attribution-suffix (point-max) t) | |
680 (let* ((start (match-beginning 0)) | |
681 (end (match-end 0)) | |
682 (wrote (count-lines (point-min) end)) | |
683 (prefix (gnus-cite-find-prefix wrote)) | |
684 ;; Check previous line for an attribution leader. | |
685 (tag (progn | |
686 (beginning-of-line 1) | |
687 (when (looking-at gnus-supercite-secondary-regexp) | |
688 (buffer-substring (match-beginning 1) | |
689 (match-end 1))))) | |
690 (in (progn | |
691 (goto-char start) | |
692 (and (re-search-backward gnus-cite-attribution-prefix | |
693 (save-excursion | |
694 (beginning-of-line 0) | |
695 (point)) | |
696 t) | |
697 (not (re-search-forward gnus-cite-attribution-suffix | |
698 start t)) | |
699 (count-lines (point-min) (1+ (point))))))) | |
700 (when (eq wrote in) | |
701 (setq in nil)) | |
702 (goto-char end) | |
703 ;; don't add duplicates | |
704 (let ((al (buffer-substring (save-excursion (beginning-of-line 0) | |
705 (1+ (point))) | |
706 end))) | |
707 (if (not (assoc al al-alist)) | |
708 (progn | |
709 (push (list wrote in prefix tag) | |
710 gnus-cite-loose-attribution-alist) | |
711 (push (cons al t) al-alist)))))))) | |
712 | |
713 (defun gnus-cite-connect-attributions () | |
714 ;; Connect attributions to citations | |
715 | |
716 ;; No citations have been connected to attribution lines yet. | |
717 (setq gnus-cite-loose-prefix-alist (append gnus-cite-prefix-alist nil)) | |
718 | |
719 ;; Parse current buffer searching for attribution lines. | |
720 ;; Find exact supercite citations. | |
721 (gnus-cite-match-attributions 'small nil | |
722 (lambda (prefix tag) | |
723 (when tag | |
724 (concat "\\`" | |
725 (regexp-quote prefix) "[ \t]*" | |
726 (regexp-quote tag) ">")))) | |
727 ;; Find loose supercite citations after attributions. | |
728 (gnus-cite-match-attributions 'small t | |
729 (lambda (prefix tag) | |
730 (when tag | |
731 (concat "\\<" | |
732 (regexp-quote tag) | |
733 "\\>")))) | |
734 ;; Find loose supercite citations anywhere. | |
735 (gnus-cite-match-attributions 'small nil | |
736 (lambda (prefix tag) | |
737 (when tag | |
738 (concat "\\<" | |
739 (regexp-quote tag) | |
740 "\\>")))) | |
741 ;; Find nested citations after attributions. | |
742 (gnus-cite-match-attributions 'small-if-unique t | |
743 (lambda (prefix tag) | |
744 (concat "\\`" (regexp-quote prefix) ".+"))) | |
745 ;; Find nested citations anywhere. | |
746 (gnus-cite-match-attributions 'small nil | |
747 (lambda (prefix tag) | |
748 (concat "\\`" (regexp-quote prefix) ".+"))) | |
749 ;; Remove loose prefixes with too few lines. | |
750 (let ((alist gnus-cite-loose-prefix-alist) | |
751 entry) | |
752 (while alist | |
753 (setq entry (car alist) | |
754 alist (cdr alist)) | |
755 (when (< (length (cdr entry)) gnus-cite-minimum-match-count) | |
756 (setq gnus-cite-prefix-alist | |
757 (delq entry gnus-cite-prefix-alist) | |
758 gnus-cite-loose-prefix-alist | |
759 (delq entry gnus-cite-loose-prefix-alist))))) | |
760 ;; Find flat attributions. | |
761 (gnus-cite-match-attributions 'first t nil) | |
762 ;; Find any attributions (are we getting desperate yet?). | |
763 (gnus-cite-match-attributions 'first nil nil)) | |
764 | |
765 (defun gnus-cite-match-attributions (sort after fun) | |
766 ;; Match all loose attributions and citations (SORT AFTER FUN) . | |
767 ;; | |
768 ;; If SORT is `small', the citation with the shortest prefix will be | |
769 ;; used, if it is `first' the first prefix will be used, if it is | |
770 ;; `small-if-unique' the shortest prefix will be used if the | |
771 ;; attribution line does not share its own prefix with other | |
772 ;; loose attribution lines, otherwise the first prefix will be used. | |
773 ;; | |
774 ;; If AFTER is non-nil, only citations after the attribution line | |
775 ;; will be considered. | |
776 ;; | |
777 ;; If FUN is non-nil, it will be called with the arguments (WROTE | |
778 ;; PREFIX TAG) and expected to return a regular expression. Only | |
779 ;; citations whose prefix matches the regular expression will be | |
780 ;; considered. | |
781 ;; | |
782 ;; WROTE is the attribution line number. | |
783 ;; PREFIX is the attribution line prefix. | |
784 ;; TAG is the Supercite tag on the attribution line. | |
785 (let ((atts gnus-cite-loose-attribution-alist) | |
786 (case-fold-search t) | |
787 att wrote in prefix tag regexp limit smallest best size) | |
788 (while atts | |
789 (setq att (car atts) | |
790 atts (cdr atts) | |
791 wrote (nth 0 att) | |
792 in (nth 1 att) | |
793 prefix (nth 2 att) | |
794 tag (nth 3 att) | |
795 regexp (if fun (funcall fun prefix tag) "") | |
796 size (cond ((eq sort 'small) t) | |
797 ((eq sort 'first) nil) | |
798 (t (< (length (gnus-cite-find-loose prefix)) 2))) | |
799 limit (if after wrote -1) | |
800 smallest 1000000 | |
801 best nil) | |
802 (let ((cites gnus-cite-loose-prefix-alist) | |
803 cite candidate numbers first compare) | |
804 (while cites | |
805 (setq cite (car cites) | |
806 cites (cdr cites) | |
807 candidate (car cite) | |
808 numbers (cdr cite) | |
809 first (apply 'min numbers) | |
810 compare (if size (length candidate) first)) | |
811 (and (> first limit) | |
812 regexp | |
813 (string-match regexp candidate) | |
814 (< compare smallest) | |
815 (setq best cite | |
816 smallest compare)))) | |
817 (if (null best) | |
818 () | |
819 (setq gnus-cite-loose-attribution-alist | |
820 (delq att gnus-cite-loose-attribution-alist)) | |
821 (push (cons wrote (car best)) gnus-cite-attribution-alist) | |
822 (when in | |
823 (push (cons in (car best)) gnus-cite-attribution-alist)) | |
824 (when (memq best gnus-cite-loose-prefix-alist) | |
825 (let ((loop gnus-cite-prefix-alist) | |
826 (numbers (cdr best)) | |
827 current) | |
828 (setq gnus-cite-loose-prefix-alist | |
829 (delq best gnus-cite-loose-prefix-alist)) | |
830 (while loop | |
831 (setq current (car loop) | |
832 loop (cdr loop)) | |
833 (if (eq current best) | |
834 () | |
835 (setcdr current (gnus-set-difference (cdr current) numbers)) | |
836 (when (null (cdr current)) | |
837 (setq gnus-cite-loose-prefix-alist | |
838 (delq current gnus-cite-loose-prefix-alist) | |
839 atts (delq current atts))))))))))) | |
840 | |
841 (defun gnus-cite-find-loose (prefix) | |
842 ;; Return a list of loose attribution lines prefixed by PREFIX. | |
843 (let* ((atts gnus-cite-loose-attribution-alist) | |
844 att line lines) | |
845 (while atts | |
846 (setq att (car atts) | |
847 line (car att) | |
848 atts (cdr atts)) | |
849 (when (string-equal (gnus-cite-find-prefix line) prefix) | |
850 (push line lines))) | |
851 lines)) | |
852 | |
853 (defun gnus-cite-add-face (number prefix face) | |
854 ;; At line NUMBER, ignore PREFIX and add FACE to the rest of the line. | |
855 (when face | |
856 (let ((inhibit-point-motion-hooks t) | |
857 from to) | |
858 (goto-line number) | |
859 (unless (eobp);; Sometimes things become confused. | |
860 (forward-char (length prefix)) | |
861 (skip-chars-forward " \t") | |
862 (setq from (point)) | |
863 (end-of-line 1) | |
864 (skip-chars-backward " \t") | |
865 (setq to (point)) | |
866 (when (< from to) | |
867 (gnus-overlay-put (gnus-make-overlay from to) 'face face)))))) | |
868 | |
869 (defun gnus-cite-toggle (prefix) | |
870 (save-excursion | |
871 (set-buffer gnus-article-buffer) | |
872 (let ((buffer-read-only nil) | |
873 (numbers (cdr (assoc prefix gnus-cite-prefix-alist))) | |
874 (inhibit-point-motion-hooks t) | |
875 number) | |
876 (while numbers | |
877 (setq number (car numbers) | |
878 numbers (cdr numbers)) | |
879 (goto-line number) | |
880 (cond ((get-text-property (point) 'invisible) | |
881 (remove-text-properties (point) (progn (forward-line 1) (point)) | |
882 gnus-hidden-properties)) | |
883 ((assq number gnus-cite-attribution-alist)) | |
884 (t | |
885 (gnus-add-text-properties | |
886 (point) (progn (forward-line 1) (point)) | |
887 (nconc (list 'article-type 'cite) | |
888 gnus-hidden-properties)))))))) | |
889 | |
890 (defun gnus-cite-find-prefix (line) | |
891 ;; Return citation prefix for LINE. | |
892 (let ((alist gnus-cite-prefix-alist) | |
893 (prefix "") | |
894 entry) | |
895 (while alist | |
896 (setq entry (car alist) | |
897 alist (cdr alist)) | |
898 (when (memq line (cdr entry)) | |
899 (setq prefix (car entry)))) | |
900 prefix)) | |
901 | |
902 (gnus-add-shutdown 'gnus-cache-close 'gnus) | |
903 | |
904 (defun gnus-cache-close () | |
905 (setq gnus-cite-prefix-alist nil)) | |
906 | |
907 (gnus-ems-redefine) | |
908 | |
909 (provide 'gnus-cite) | |
910 | |
911 ;;; gnus-cite.el ends here |