1 ;;; mu-cite.el --- yet another citation tool for GNU Emacs
3 ;; Copyright (C) 1995,1996,1997,1998,1999 Free Software Foundation, Inc.
5 ;; Author: MORIOKA Tomohiko <tomo@m17n.org>
6 ;; MINOURA Makoto <minoura@netlaputa.or.jp>
7 ;; Shuhei KOBAYASHI <shuhei-k@jaist.ac.jp>
8 ;; Maintainer: Katsumi Yamaoka <yamaoka@jpl.org>
9 ;; Keywords: mail, news, citation
11 ;; This file is part of MU (Message Utilities).
13 ;; This program is free software; you can redistribute it and/or
14 ;; modify it under the terms of the GNU General Public License as
15 ;; published by the Free Software Foundation; either version 2, or (at
16 ;; your option) any later version.
18 ;; This program is distributed in the hope that it will be useful, but
19 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 ;; General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
31 ;; 1. bytecompile this file and copy it to the apropriate directory.
32 ;; 2. put the following lines to your ~/.emacs:
33 ;; for EMACS 19 or later and XEmacs
34 ;; (autoload 'mu-cite/cite-original "mu-cite" nil t)
35 ;; ;; for all but message-mode
36 ;; (add-hook 'mail-citation-hook 'mu-cite/cite-original)
37 ;; ;; for message-mode only
38 ;; (setq message-cite-function (function mu-cite/cite-original))
40 ;; ;; for all but mh-e
41 ;; (add-hook 'mail-yank-hooks (function mu-cite/cite-original))
43 ;; (add-hook 'mh-yank-hooks (function mu-cite/cite-original))
54 (defconst mu-cite/version "7.52")
60 (defvar mu-cite/cited-prefix-regexp "\\(^[^ \t\n<>]+>+[ \t]*\\|^[ \t]*$\\)"
61 "*Regexp to match the citation prefix.
62 If match, mu-cite doesn't insert citation prefix.")
64 (defvar mu-cite/prefix-format '(prefix-register-verbose "> ")
65 "*List to represent citation prefix.
66 Each elements must be string or method name.")
68 (defvar mu-cite/top-format '(in-id
69 ">>>>> " from " wrote:\n")
70 "*List to represent top string of citation.
71 Each elements must be string or method name.")
77 (defvar mu-cite-load-hook nil
78 "*List of functions called after mu-cite is loaded.
79 Use this hook to add your own methods to `mu-cite/default-methods-alist'.")
81 (defvar mu-cite/instantiation-hook nil
82 "*List of functions called just before narrowing to the message.")
84 (defvar mu-cite/pre-cite-hook nil
85 "*List of functions called before citing a region of text.")
87 (defvar mu-cite/post-cite-hook nil
88 "*List of functions called after citing a region of text.")
94 (defvar mu-cite/get-field-value-method-alist nil
95 "Alist major-mode vs. function to get field-body of header.")
97 (defun mu-cite/get-field-value (name)
98 (or (std11-field-body name)
99 (let ((method (assq major-mode mu-cite/get-field-value-method-alist)))
101 (funcall (cdr method) name)
105 ;;; @ prefix registration
108 (defvar mu-cite/registration-file (expand-file-name "~/.mu-cite.el")
109 "*The name of the user environment file for mu-cite.")
111 (defvar mu-cite/allow-null-string-registration nil
112 "*If non-nil, null-string citation-name is registered.")
114 (defvar mu-cite/registration-symbol 'mu-cite/citation-name-alist)
116 (defvar mu-cite/citation-name-alist nil)
117 (or (eq 'mu-cite/citation-name-alist mu-cite/registration-symbol)
118 (setq mu-cite/citation-name-alist
119 (symbol-value mu-cite/registration-symbol))
121 (defvar mu-cite/minibuffer-history nil)
123 ;; get citation-name from the database
124 (defun mu-cite/get-citation-name (from)
125 (cdr (assoc from mu-cite/citation-name-alist))
128 ;; register citation-name to the database
129 (defun mu-cite/add-citation-name (name from)
130 (setq mu-cite/citation-name-alist
131 (put-alist from name mu-cite/citation-name-alist))
132 (mu-cite/save-registration-file)
135 ;; load/save registration file
136 (defun mu-cite/load-registration-file ()
137 (let* ((file mu-cite/registration-file)
138 (buffer (get-buffer-create " *mu-register*")))
139 (if (file-readable-p file)
144 (insert-file-contents file)
146 (eval-current-buffer))
147 (kill-buffer buffer))
149 (add-hook 'mu-cite-load-hook (function mu-cite/load-registration-file))
151 (defun mu-cite/save-registration-file ()
152 (let* ((file mu-cite/registration-file)
153 (buffer (get-buffer-create " *mu-register*")))
157 (setq buffer-file-name file)
159 (insert ";;; " (file-name-nondirectory file) "\n")
160 (insert ";;; This file is generated automatically by mu-cite "
161 mu-cite/version "\n\n")
163 (symbol-name mu-cite/registration-symbol)
166 (function prin1-to-string)
167 mu-cite/citation-name-alist "\n "))
170 (file-name-nondirectory file)
173 (kill-buffer buffer))))
182 (defvar mu-cite/ml-count-field-list
183 '("X-Ml-Count" "X-Mail-Count" "X-Seqno" "X-Sequence" "Mailinglist-Id")
184 "*List of header fields which contain sequence number of mailing list.")
186 (defun mu-cite/get-ml-count-method ()
187 (let ((field-list mu-cite/ml-count-field-list))
190 (let* ((field (car field-list))
191 (ml-count (mu-cite/get-field-value field)))
192 (if (and ml-count (string-match "[0-9]+" ml-count))
195 (match-beginning 0)(match-end 0))
197 (setq field-list (cdr field-list))
201 ;;; @@ prefix and registration
204 (defun mu-cite/get-prefix-method ()
205 (or (mu-cite/get-citation-name (mu-cite/get-value 'address))
209 (defun mu-cite/get-prefix-register-method ()
210 (let ((addr (mu-cite/get-value 'address)))
211 (or (mu-cite/get-citation-name addr)
213 (read-string "Citation name? "
214 (or (mu-cite/get-value 'x-attribution)
215 (mu-cite/get-value 'full-name))
216 'mu-cite/minibuffer-history)
218 (if (and (or mu-cite/allow-null-string-registration
219 (not (string-equal return "")))
220 (y-or-n-p (format "Register \"%s\"? " return)))
221 (mu-cite/add-citation-name return addr)
225 (defun mu-cite/get-prefix-register-verbose-method ()
226 (let* ((addr (mu-cite/get-value 'address))
227 (return1 (mu-cite/get-citation-name addr))
228 (return (read-string "Citation name? "
230 (mu-cite/get-value 'x-attribution)
231 (mu-cite/get-value 'full-name))
232 'mu-cite/minibuffer-history))
234 (if (and (or mu-cite/allow-null-string-registration
235 (not (string-equal return "")))
236 (not (string-equal return return1))
237 (y-or-n-p (format "Register \"%s\"? " return))
239 (mu-cite/add-citation-name return addr)
247 (defvar mu-cite/default-methods-alist
251 (mu-cite/get-field-value "From")
256 (mu-cite/get-field-value "Date")
261 (mu-cite/get-field-value "Message-Id")
266 (mu-cite/get-field-value "Subject")
271 (mu-cite/get-field-value "X-Ml-Name")
273 (cons 'ml-count (function mu-cite/get-ml-count-method))
274 (cons 'address-structure
278 (std11-parse-address-string (mu-cite/get-value 'from))
283 (std11-full-name-string
284 (mu-cite/get-value 'address-structure))
289 (std11-address-string
290 (mu-cite/get-value 'address-structure))
295 (let ((ml-name (mu-cite/get-value 'ml-name)))
300 (mu-cite/get-value 'ml-count)
302 (mu-cite/get-value 'message-id)
307 (let ((id (mu-cite/get-value 'id)))
309 (format ">>>>> In %s \n" id)
311 (cons 'prefix (function mu-cite/get-prefix-method))
312 (cons 'prefix-register
313 (function mu-cite/get-prefix-register-method))
314 (cons 'prefix-register-verbose
315 (function mu-cite/get-prefix-register-verbose-method))
319 (mu-cite/get-field-value "X-Attribution")
327 (defvar mu-cite/methods-alist nil)
329 (defun mu-cite/make-methods ()
330 (setq mu-cite/methods-alist
331 (copy-alist mu-cite/default-methods-alist))
332 (run-hooks 'mu-cite/instantiation-hook)
335 (defun mu-cite/get-value (item)
336 (let ((ret (cdr (assoc item mu-cite/methods-alist))))
339 (setq ret (funcall ret))
340 (set-alist 'mu-cite/methods-alist item ret)
344 (defun mu-cite/eval-format (list)
348 (cond ((stringp elt) elt)
349 ((symbolp elt) (mu-cite/get-value elt))
358 (defun mu-cite/cite-original ()
359 "Citing filter function.
360 This is callable from the various mail and news readers' reply
361 function according to the agreed upon standard."
363 (mu-cite/make-methods)
365 (if (< (mark t) (point))
366 (exchange-point-and-mark))
367 (narrow-to-region (point)(point-max))
368 (run-hooks 'mu-cite/pre-cite-hook)
369 (let ((last-point (point))
370 (top (mu-cite/eval-format mu-cite/top-format))
371 (prefix (mu-cite/eval-format mu-cite/prefix-format))
373 (if (re-search-forward "^-*$" nil nil)
377 (delete-region last-point (point))
379 (setq last-point (point))
380 (while (< (point)(mark t))
381 (or (looking-at mu-cite/cited-prefix-regexp)
384 (goto-char last-point)
386 (run-hooks 'mu-cite/post-cite-hook)
390 ;;; @ message editing utilities
393 (defvar citation-mark-chars ">}|"
394 "*String of characters for citation delimiter. [mu-cite.el]")
396 (defvar citation-disable-chars "<{"
397 "*String of characters not allowed as citation-prefix.")
399 (defun detect-paragraph-cited-prefix ()
401 (goto-char (point-min))
405 (progn (beginning-of-line)(point))
406 (progn (end-of-line)(point))
409 (while (and (= (forward-line) 0)
410 (setq str (buffer-substring
411 (progn (beginning-of-line)(point))
412 (progn (end-of-line)(point))))
413 (setq ret (string-compare-from-top prefix str))
421 (cond ((> i 1) prefix)
423 (goto-char (point-min))
425 (narrow-to-region (point)
426 (+ (point)(length prefix)))
427 (goto-char (point-max))
428 (if (re-search-backward
429 (concat "[" citation-mark-chars "]") nil t)
431 (goto-char (match-end 0))
432 (if (looking-at "[ \t]+")
433 (goto-char (match-end 0))
435 (buffer-substring (point-min)(point))
439 (goto-char (point-max))
441 (concat "[" citation-disable-chars "]") nil t)
443 (concat "[" citation-mark-chars "]") nil t)
445 (goto-char (match-end 0))
446 (if (looking-at "[ \t]+")
447 (goto-char (match-end 0))
449 (buffer-substring (point-min)(point))
454 (defun fill-cited-region (beg end)
459 (and (search-backward "\n" nil t)
460 (setq end (match-end 0))
462 (narrow-to-region beg end)
463 (let* ((fill-prefix (detect-paragraph-cited-prefix))
464 (pat (concat fill-prefix "\n"))
466 (goto-char (point-min))
467 (while (search-forward pat nil t)
468 (let ((b (match-beginning 0))
472 (if (and (> b (point-min))
473 (let ((cat (char-category
475 (or (string-match "a" cat)
476 (string-match "l" cat)
482 (goto-char (point-min))
483 (fill-region (point-min) (point-max))
486 (defun compress-cited-prefix ()
489 (goto-char (point-min))
491 (concat "^" (regexp-quote mail-header-separator) "$") nil t)
492 (while (re-search-forward
493 (concat "^\\([ \t]*[^ \t\n" citation-mark-chars "]*["
494 citation-mark-chars "]\\)+") nil t)
495 (let* ((b (match-beginning 0))
497 (prefix (buffer-substring b e))
500 (if (string-match "<[^<>]+>" prefix)
501 (setq prefix (substring prefix 0 (match-beginning 0)))
504 (concat "\\([" citation-mark-chars "]+\\)[ \t]*")
506 (setq i (+ i (- (match-end 1)(match-beginning 1)))
508 pe (match-beginning 1)
512 (if (and ps (< ps pe))
515 (insert (concat (substring prefix ps pe) (make-string nest ?>)))
518 (defun replace-top-string (old new)
519 (interactive "*sOld string: \nsNew string: ")
520 (while (re-search-forward
521 (concat "^" (regexp-quote old)) nil t)
525 (defun string-compare-from-top (str1 str2)
526 (let* ((len1 (length str1))
528 (len (min len1 len2))
531 (while (and (< p len)
533 (setq c1 (sref str1 p)
537 (setq p (+ p (char-length c1)))
540 (let ((matched (substring str1 0 p))
541 (r1 (and (< p len1)(substring str1 p)))
542 (r2 (and (< p len2)(substring str2 p)))
546 (list 'seq matched (list 'or r1 r2))
555 (run-hooks 'mu-cite-load-hook)
557 ;;; mu-cite.el ends here