comparison lisp/tl/mu-cite.el @ 4:b82b59fe008d r19-15b3

Import from CVS: tag r19-15b3
author cvs
date Mon, 13 Aug 2007 08:46:56 +0200
parents
children
comparison
equal deleted inserted replaced
3:30df88044ec6 4:b82b59fe008d
1 ;;; mu-cite.el --- yet another citation tool for GNU Emacs
2
3 ;; Copyright (C) 1995,1996 Free Software Foundation, Inc.
4
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; MINOURA Makoto <minoura@netlaputa.or.jp>
7 ;; Shuhei KOBAYASHI <shuhei-k@jaist.ac.jp>
8 ;; Maintainer: Shuhei KOBAYASHI <shuhei-k@jaist.ac.jp>
9 ;; Version: $Revision: 1.1.1.1 $
10 ;; Keywords: mail, news, citation
11
12 ;; This file is part of tl (Tiny Library).
13
14 ;; This program is free software; you can redistribute it and/or
15 ;; modify it under the terms of the GNU General Public License as
16 ;; published by the Free Software Foundation; either version 2, or (at
17 ;; your option) any later version.
18
19 ;; This program is distributed in the hope that it will be useful, but
20 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 ;; General Public License for more details.
23
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with this program; see the file COPYING. If not, write to
26 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
27 ;; Boston, MA 02111-1307, USA.
28
29 ;;; Commentary:
30
31 ;; - How to use
32 ;; 1. bytecompile this file and copy it to the apropriate directory.
33 ;; 2. put the following lines to your ~/.emacs:
34 ;; for EMACS 19 or later and XEmacs
35 ;; (autoload 'mu-cite/cite-original "mu-cite" nil t)
36 ;; ;; for all but message-mode
37 ;; (add-hook 'mail-citation-hook 'mu-cite/cite-original)
38 ;; ;; for message-mode only
39 ;; (setq message-cite-function (function mu-cite/cite-original))
40 ;; for EMACS 18
41 ;; ;; for all but mh-e
42 ;; (add-hook 'mail-yank-hooks (function mu-cite/cite-original))
43 ;; ;; for mh-e only
44 ;; (add-hook 'mh-yank-hooks (function mu-cite/cite-original))
45
46 ;;; Code:
47
48 (require 'std11)
49 (require 'tl-str)
50 (require 'tl-list)
51
52
53 ;;; @ version
54 ;;;
55
56 (defconst mu-cite/RCS-ID
57 "$Id: mu-cite.el,v 1.1.1.1 1996/12/18 03:55:31 steve Exp $")
58 (defconst mu-cite/version (get-version-string mu-cite/RCS-ID))
59
60
61 ;;; @ formats
62 ;;;
63
64 (defvar cited-prefix-regexp "^[^ \t>]*[>|]+[ \t#]*")
65 (defvar mu-cite/cited-prefix-regexp "\\(^[^ \t\n>]+>+[ \t]*\\|^[ \t]*$\\)")
66
67 (defvar mu-cite/prefix-format '(prefix-register-verbose "> ")
68 "*List to represent citation prefix.
69 Each elements must be string or method name.")
70 (defvar mu-cite/top-format '(in-id
71 ">>>>> " from " wrote:\n")
72 "*List to represent top string of citation.
73 Each elements must be string or method name.")
74
75
76 ;;; @ hooks
77 ;;;
78
79 (defvar mu-cite/pre-cite-hook nil
80 "*List of functions called before citing a region of text.")
81 (defvar mu-cite/post-cite-hook nil
82 "*List of functions called after citing a region of text.")
83
84
85 ;;; @ field
86 ;;;
87
88 (defvar mu-cite/get-field-value-method-alist
89 (list (cons 'mh-letter-mode
90 (function
91 (lambda (name)
92 (if (and (stringp mh-sent-from-folder)
93 (numberp mh-sent-from-msg))
94 (save-excursion
95 (set-buffer mh-sent-from-folder)
96 (set-buffer mh-show-buffer)
97 (and (boundp 'mime::preview/article-buffer)
98 (bufferp mime::preview/article-buffer)
99 (set-buffer mime::preview/article-buffer))
100 (std11-field-body name)
101 ))
102 )))))
103
104 (defun mu-cite/get-field-value (name)
105 (or (std11-field-body name)
106 (let ((method (assq major-mode mu-cite/get-field-value-method-alist)))
107 (if method
108 (funcall (cdr method) name)
109 ))))
110
111
112 ;;; @ prefix registration
113 ;;;
114
115 (defvar mu-cite/registration-file
116 (expand-file-name "~/.mu-cite.el")
117 "*The name of the user environment file for mu-cite.")
118
119 (defvar mu-cite/allow-null-string-registration nil
120 "*If non-nil, null-string citation-name is registered.")
121
122 (defvar mu-cite/registration-symbol 'mu-cite/citation-name-alist)
123
124 (defvar mu-cite/citation-name-alist nil)
125 (load mu-cite/registration-file t t t)
126 (or (eq 'mu-cite/citation-name-alist mu-cite/registration-symbol)
127 (setq mu-cite/citation-name-alist
128 (symbol-value mu-cite/registration-symbol))
129 )
130 (defvar mu-cite/minibuffer-history nil)
131
132 ;; get citation-name from the database
133 (defun mu-cite/get-citation-name (from)
134 (assoc-value from mu-cite/citation-name-alist)
135 )
136
137 ;; register citation-name to the database
138 (defun mu-cite/add-citation-name (name from)
139 (setq mu-cite/citation-name-alist
140 (put-alist from name mu-cite/citation-name-alist))
141 (mu-cite/save-to-file)
142 )
143
144 ;; save to file
145 (defun mu-cite/save-to-file ()
146 (let* ((filename mu-cite/registration-file)
147 (buffer (get-buffer-create " *mu-register*")))
148 (save-excursion
149 (set-buffer buffer)
150 (setq buffer-file-name filename)
151 (erase-buffer)
152 (insert
153 (format ";;; %s\n" (file-name-nondirectory filename)))
154 (insert
155 (format ";;; This file is generated automatically by mu-cite %s.\n\n"
156 mu-cite/version))
157 (insert (format "(setq %s\n '(" mu-cite/registration-symbol))
158 (insert (mapconcat
159 (function prin1-to-string)
160 mu-cite/citation-name-alist "\n "))
161 (insert "\n ))\n\n")
162 (insert
163 (format ";;; %s ends here.\n" (file-name-nondirectory filename)))
164 (save-buffer))
165 (kill-buffer buffer)))
166
167
168 ;;; @ item methods
169 ;;;
170
171 ;;; @@ ML count
172 ;;;
173
174 (defvar mu-cite/ml-count-field-list
175 '("X-Ml-Count" "X-Mail-Count" "X-Seqno" "X-Sequence" "Mailinglist-Id"))
176
177 (defun mu-cite/get-ml-count-method ()
178 (let ((field-list mu-cite/ml-count-field-list))
179 (catch 'tag
180 (while field-list
181 (let* ((field (car field-list))
182 (ml-count (mu-cite/get-field-value field)))
183 (if (and ml-count (string-match "[0-9]+" ml-count))
184 (throw 'tag
185 (substring ml-count
186 (match-beginning 0)(match-end 0))
187 ))
188 (setq field-list (cdr field-list))
189 )))))
190
191
192 ;;; @@ prefix and registration
193 ;;;
194
195 (defun mu-cite/get-prefix-method ()
196 (or (mu-cite/get-citation-name (mu-cite/get-value 'address))
197 ">")
198 )
199
200 (defun mu-cite/get-prefix-register-method ()
201 (let ((addr (mu-cite/get-value 'address)))
202 (or (mu-cite/get-citation-name addr)
203 (let ((return
204 (read-string "Citation name? "
205 (or (mu-cite/get-value 'x-attribution)
206 (mu-cite/get-value 'full-name))
207 'mu-cite/minibuffer-history)
208 ))
209 (if (and (or mu-cite/allow-null-string-registration
210 (not (string-equal return "")))
211 (y-or-n-p (format "Register \"%s\"? " return)))
212 (mu-cite/add-citation-name return addr)
213 )
214 return))))
215
216 (defun mu-cite/get-prefix-register-verbose-method ()
217 (let* ((addr (mu-cite/get-value 'address))
218 (return1 (mu-cite/get-citation-name addr))
219 (return (read-string "Citation name? "
220 (or return1
221 (mu-cite/get-value 'x-attribution)
222 (mu-cite/get-value 'full-name))
223 'mu-cite/minibuffer-history))
224 )
225 (if (and (or mu-cite/allow-null-string-registration
226 (not (string-equal return "")))
227 (not (string-equal return return1))
228 (y-or-n-p (format "Register \"%s\"? " return))
229 )
230 (mu-cite/add-citation-name return addr)
231 )
232 return))
233
234
235 ;;; @@ set up
236 ;;;
237
238 (defvar mu-cite/default-methods-alist
239 (list (cons 'from
240 (function
241 (lambda ()
242 (mu-cite/get-field-value "From")
243 )))
244 (cons 'date
245 (function
246 (lambda ()
247 (mu-cite/get-field-value "Date")
248 )))
249 (cons 'message-id
250 (function
251 (lambda ()
252 (mu-cite/get-field-value "Message-Id")
253 )))
254 (cons 'subject
255 (function
256 (lambda ()
257 (mu-cite/get-field-value "Subject")
258 )))
259 (cons 'ml-name
260 (function
261 (lambda ()
262 (mu-cite/get-field-value "X-Ml-Name")
263 )))
264 (cons 'ml-count (function mu-cite/get-ml-count-method))
265 (cons 'address-structure
266 (function
267 (lambda ()
268 (car
269 (std11-parse-address-string (mu-cite/get-value 'from))
270 ))))
271 (cons 'full-name
272 (function
273 (lambda ()
274 (std11-full-name-string
275 (mu-cite/get-value 'address-structure))
276 )))
277 (cons 'address
278 (function
279 (lambda ()
280 (std11-address-string
281 (mu-cite/get-value 'address-structure))
282 )))
283 (cons 'id
284 (function
285 (lambda ()
286 (let ((ml-name (mu-cite/get-value 'ml-name)))
287 (if ml-name
288 (concat "["
289 ml-name
290 " : No."
291 (mu-cite/get-value 'ml-count)
292 "]")
293 (mu-cite/get-value 'message-id)
294 )))))
295 (cons 'in-id
296 (function
297 (lambda ()
298 (let ((id (mu-cite/get-value 'id)))
299 (if id
300 (format ">>>>> In %s \n" id)
301 "")))))
302 (cons 'prefix (function mu-cite/get-prefix-method))
303 (cons 'prefix-register
304 (function mu-cite/get-prefix-register-method))
305 (cons 'prefix-register-verbose
306 (function mu-cite/get-prefix-register-verbose-method))
307 (cons 'x-attribution
308 (function
309 (lambda ()
310 (mu-cite/get-field-value "X-Attribution")
311 )))
312 ))
313
314
315 ;;; @ fundamentals
316 ;;;
317
318 (defvar mu-cite/methods-alist nil)
319
320 (defun mu-cite/make-methods ()
321 (setq mu-cite/methods-alist
322 (copy-alist mu-cite/default-methods-alist))
323 (run-hooks 'mu-cite/instantiation-hook)
324 )
325
326 (defun mu-cite/get-value (item)
327 (let ((ret (assoc-value item mu-cite/methods-alist)))
328 (if (functionp ret)
329 (prog1
330 (setq ret (funcall ret))
331 (set-alist 'mu-cite/methods-alist item ret)
332 )
333 ret)))
334
335 (defun mu-cite/eval-format (list)
336 (mapconcat (function
337 (lambda (elt)
338 (cond ((stringp elt) elt)
339 ((symbolp elt) (mu-cite/get-value elt))
340 )))
341 list "")
342 )
343
344
345 ;;; @ main function
346 ;;;
347
348 (defun mu-cite/cite-original ()
349 "Citing filter function.
350 This is callable from the various mail and news readers' reply
351 function according to the agreed upon standard."
352 (interactive)
353 (mu-cite/make-methods)
354 (save-restriction
355 (if (< (mark t) (point))
356 (exchange-point-and-mark))
357 (narrow-to-region (point)(point-max))
358 (run-hooks 'mu-cite/pre-cite-hook)
359 (let ((last-point (point))
360 (top (mu-cite/eval-format mu-cite/top-format))
361 (prefix (mu-cite/eval-format mu-cite/prefix-format))
362 )
363 (if (re-search-forward "^$\\|^-+$" nil nil)
364 (forward-line 1)
365 )
366 (widen)
367 (delete-region last-point (point))
368 (insert top)
369 (setq last-point (point))
370 (while (< (point)(mark t))
371 (or (looking-at mu-cite/cited-prefix-regexp)
372 (insert prefix))
373 (forward-line 1))
374 (goto-char last-point)
375 )
376 (run-hooks 'mu-cite/post-cite-hook)
377 ))
378
379
380 ;;; @ message editing utilities
381 ;;;
382
383 (defun fill-cited-region (beg end)
384 (interactive "*r")
385 (save-excursion
386 (save-restriction
387 (goto-char end)
388 (while (not (eolp))
389 (backward-char)
390 )
391 (setq end (point))
392 (narrow-to-region beg end)
393 (goto-char (point-min))
394 (let* ((fill-prefix
395 (let* ((str1 (buffer-substring
396 (progn (beginning-of-line)(point))
397 (progn (end-of-line)(point))
398 ))
399 (str2 (let ((p0 (point)))
400 (forward-line)
401 (if (> (count-lines p0 (point)) 0)
402 (buffer-substring
403 (progn (beginning-of-line)(point))
404 (progn (end-of-line)(point))
405 ))))
406 (ret (string-compare-from-top str1 str2))
407 )
408 (if ret
409 (nth 1 ret)
410 (goto-char (point-min))
411 (if (re-search-forward cited-prefix-regexp nil t)
412 (buffer-substring (match-beginning 0) (match-end 0))
413 ))))
414 (pat (concat "\n" fill-prefix))
415 )
416 (goto-char (point-min))
417 (while (search-forward pat nil t)
418 (if (and (> (match-beginning 0) (point-min))
419 (member (char-category
420 (char-before (match-beginning 0)))
421 '("a" "l"))
422 )
423 (replace-match " ")
424 (replace-match "")
425 )
426 )
427 (goto-char (point-min))
428 (fill-region (point-min) (point-max))
429 ))))
430
431 (defvar citation-mark-chars ">}|")
432
433 (defun compress-cited-prefix ()
434 (interactive)
435 (save-excursion
436 (goto-char (point-min))
437 (re-search-forward
438 (concat "^" (regexp-quote mail-header-separator) "$") nil t)
439 (while (re-search-forward
440 (concat "^\\([ \t]*[^ \t\n" citation-mark-chars "]*["
441 citation-mark-chars "]\\)+") nil t)
442 (let* ((b (match-beginning 0))
443 (e (match-end 0))
444 (prefix (buffer-substring b e))
445 ps pe (s 0)
446 (nest (let ((i 0))
447 (if (string-match "<[^<>]+>" prefix)
448 (setq prefix (substring prefix 0 (match-beginning 0)))
449 )
450 (while (string-match
451 (concat "\\([" citation-mark-chars "]+\\)[ \t]*")
452 prefix s)
453 (setq i (+ i (- (match-end 1)(match-beginning 1)))
454 ps s
455 pe (match-beginning 1)
456 s (match-end 0)
457 ))
458 i)))
459 (if (and ps (< ps pe))
460 (progn
461 (delete-region b e)
462 (insert (concat (substring prefix ps pe) (make-string nest ?>)))
463 ))))))
464
465 (defun replace-top-string (old new)
466 (interactive "*sOld string: \nsNew string: ")
467 (while (re-search-forward
468 (concat "^" (regexp-quote old)) nil t)
469 (replace-match new)
470 ))
471
472
473 ;;; @ end
474 ;;;
475
476 (provide 'mu-cite)
477
478 (run-hooks 'mu-cite-load-hook)
479
480 ;;; mu-cite.el ends here