1 ;;; mu-cite.el --- yet another citation tool for GNU Emacs
3 ;; Copyright (C) 1995,1996,1997,1998 Free Software Foundation, Inc.
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: 7.51 $
10 ;; Keywords: mail, news, citation
12 ;; This file is part of MU (Message Utilities).
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.
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.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs; see the file COPYING. If not, write to the
26 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
27 ;; Boston, MA 02111-1307, USA.
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))
41 ;; ;; for all but mh-e
42 ;; (add-hook 'mail-yank-hooks (function mu-cite/cite-original))
44 ;; (add-hook 'mh-yank-hooks (function mu-cite/cite-original))
57 (defsubst get-version-string (id)
58 "Return a version-string from RCS ID."
59 (and (string-match ",v \\([0-9][0-9.][0-9.]+\\)" id)
60 (substring id (match-beginning 1)(match-end 1))
63 (defconst mu-cite/RCS-ID
64 "$Id: mu-cite.el,v 7.51 1998/02/13 10:11:54 morioka Exp $")
65 (defconst mu-cite/version (get-version-string mu-cite/RCS-ID))
71 (defvar mu-cite/cited-prefix-regexp "\\(^[^ \t\n<>]+>+[ \t]*\\|^[ \t]*$\\)"
72 "*Regexp to match the citation prefix.
73 If match, mu-cite doesn't insert citation prefix.")
75 (defvar mu-cite/prefix-format '(prefix-register-verbose "> ")
76 "*List to represent citation prefix.
77 Each elements must be string or method name.")
79 (defvar mu-cite/top-format '(in-id
80 ">>>>> " from " wrote:\n")
81 "*List to represent top string of citation.
82 Each elements must be string or method name.")
88 (defvar mu-cite-load-hook nil
89 "*List of functions called after mu-cite is loaded.
90 Use this hook to add your own methods to `mu-cite/default-methods-alist'.")
92 (defvar mu-cite/instantiation-hook nil
93 "*List of functions called just before narrowing to the message.")
95 (defvar mu-cite/pre-cite-hook nil
96 "*List of functions called before citing a region of text.")
98 (defvar mu-cite/post-cite-hook nil
99 "*List of functions called after citing a region of text.")
105 (defvar mu-cite/get-field-value-method-alist nil
106 "Alist major-mode vs. function to get field-body of header.")
108 (defun mu-cite/get-field-value (name)
109 (or (std11-field-body name)
110 (let ((method (assq major-mode mu-cite/get-field-value-method-alist)))
112 (funcall (cdr method) name)
116 ;;; @ prefix registration
119 (defvar mu-cite/registration-file (expand-file-name "~/.mu-cite.el")
120 "*The name of the user environment file for mu-cite.")
122 (defvar mu-cite/allow-null-string-registration nil
123 "*If non-nil, null-string citation-name is registered.")
125 (defvar mu-cite/registration-symbol 'mu-cite/citation-name-alist)
127 (defvar mu-cite/citation-name-alist nil)
128 (or (eq 'mu-cite/citation-name-alist mu-cite/registration-symbol)
129 (setq mu-cite/citation-name-alist
130 (symbol-value mu-cite/registration-symbol))
132 (defvar mu-cite/minibuffer-history nil)
134 ;; get citation-name from the database
135 (defun mu-cite/get-citation-name (from)
136 (cdr (assoc from mu-cite/citation-name-alist))
139 ;; register citation-name to the database
140 (defun mu-cite/add-citation-name (name from)
141 (setq mu-cite/citation-name-alist
142 (put-alist from name mu-cite/citation-name-alist))
143 (mu-cite/save-registration-file)
146 ;; load/save registration file
147 (defun mu-cite/load-registration-file ()
148 (let* ((file mu-cite/registration-file)
149 (buffer (get-buffer-create " *mu-register*")))
150 (if (file-readable-p file)
155 (insert-file-contents file)
157 (eval-current-buffer))
158 (kill-buffer buffer))
160 (add-hook 'mu-cite-load-hook (function mu-cite/load-registration-file))
162 (defun mu-cite/save-registration-file ()
163 (let* ((file mu-cite/registration-file)
164 (buffer (get-buffer-create " *mu-register*")))
168 (setq buffer-file-name file)
170 (insert ";;; " (file-name-nondirectory file) "\n")
171 (insert ";;; This file is generated automatically by mu-cite "
172 mu-cite/version "\n\n")
174 (symbol-name mu-cite/registration-symbol)
177 (function prin1-to-string)
178 mu-cite/citation-name-alist "\n "))
181 (file-name-nondirectory file)
184 (kill-buffer buffer))))
193 (defvar mu-cite/ml-count-field-list
194 '("X-Ml-Count" "X-Mail-Count" "X-Seqno" "X-Sequence" "Mailinglist-Id")
195 "*List of header fields which contain sequence number of mailing list.")
197 (defun mu-cite/get-ml-count-method ()
198 (let ((field-list mu-cite/ml-count-field-list))
201 (let* ((field (car field-list))
202 (ml-count (mu-cite/get-field-value field)))
203 (if (and ml-count (string-match "[0-9]+" ml-count))
206 (match-beginning 0)(match-end 0))
208 (setq field-list (cdr field-list))
212 ;;; @@ prefix and registration
215 (defun mu-cite/get-prefix-method ()
216 (or (mu-cite/get-citation-name (mu-cite/get-value 'address))
220 (defun mu-cite/get-prefix-register-method ()
221 (let ((addr (mu-cite/get-value 'address)))
222 (or (mu-cite/get-citation-name addr)
224 (read-string "Citation name? "
225 (or (mu-cite/get-value 'x-attribution)
226 (mu-cite/get-value 'full-name))
227 'mu-cite/minibuffer-history)
229 (if (and (or mu-cite/allow-null-string-registration
230 (not (string-equal return "")))
231 (y-or-n-p (format "Register \"%s\"? " return)))
232 (mu-cite/add-citation-name return addr)
236 (defun mu-cite/get-prefix-register-verbose-method ()
237 (let* ((addr (mu-cite/get-value 'address))
238 (return1 (mu-cite/get-citation-name addr))
239 (return (read-string "Citation name? "
241 (mu-cite/get-value 'x-attribution)
242 (mu-cite/get-value 'full-name))
243 'mu-cite/minibuffer-history))
245 (if (and (or mu-cite/allow-null-string-registration
246 (not (string-equal return "")))
247 (not (string-equal return return1))
248 (y-or-n-p (format "Register \"%s\"? " return))
250 (mu-cite/add-citation-name return addr)
258 (defvar mu-cite/default-methods-alist
262 (mu-cite/get-field-value "From")
267 (mu-cite/get-field-value "Date")
272 (mu-cite/get-field-value "Message-Id")
277 (mu-cite/get-field-value "Subject")
282 (mu-cite/get-field-value "X-Ml-Name")
284 (cons 'ml-count (function mu-cite/get-ml-count-method))
285 (cons 'address-structure
289 (std11-parse-address-string (mu-cite/get-value 'from))
294 (std11-full-name-string
295 (mu-cite/get-value 'address-structure))
300 (std11-address-string
301 (mu-cite/get-value 'address-structure))
306 (let ((ml-name (mu-cite/get-value 'ml-name)))
311 (mu-cite/get-value 'ml-count)
313 (mu-cite/get-value 'message-id)
318 (let ((id (mu-cite/get-value 'id)))
320 (format ">>>>> In %s \n" id)
322 (cons 'prefix (function mu-cite/get-prefix-method))
323 (cons 'prefix-register
324 (function mu-cite/get-prefix-register-method))
325 (cons 'prefix-register-verbose
326 (function mu-cite/get-prefix-register-verbose-method))
330 (mu-cite/get-field-value "X-Attribution")
338 (defvar mu-cite/methods-alist nil)
340 (defun mu-cite/make-methods ()
341 (setq mu-cite/methods-alist
342 (copy-alist mu-cite/default-methods-alist))
343 (run-hooks 'mu-cite/instantiation-hook)
346 (defun mu-cite/get-value (item)
347 (let ((ret (cdr (assoc item mu-cite/methods-alist))))
350 (setq ret (funcall ret))
351 (set-alist 'mu-cite/methods-alist item ret)
355 (defun mu-cite/eval-format (list)
359 (cond ((stringp elt) elt)
360 ((symbolp elt) (mu-cite/get-value elt))
369 (defun mu-cite/cite-original ()
370 "Citing filter function.
371 This is callable from the various mail and news readers' reply
372 function according to the agreed upon standard."
374 (mu-cite/make-methods)
376 (if (< (mark t) (point))
377 (exchange-point-and-mark))
378 (narrow-to-region (point)(point-max))
379 (run-hooks 'mu-cite/pre-cite-hook)
380 (let ((last-point (point))
381 (top (mu-cite/eval-format mu-cite/top-format))
382 (prefix (mu-cite/eval-format mu-cite/prefix-format))
384 (if (re-search-forward "^-*$" nil nil)
388 (delete-region last-point (point))
390 (setq last-point (point))
391 (while (< (point)(mark t))
392 (or (looking-at mu-cite/cited-prefix-regexp)
395 (goto-char last-point)
397 (run-hooks 'mu-cite/post-cite-hook)
401 ;;; @ message editing utilities
404 (defvar citation-mark-chars ">}|"
405 "*String of characters for citation delimiter. [mu-cite.el]")
407 (defvar citation-disable-chars "<{"
408 "*String of characters not allowed as citation-prefix.")
410 (defun detect-paragraph-cited-prefix ()
412 (goto-char (point-min))
416 (progn (beginning-of-line)(point))
417 (progn (end-of-line)(point))
420 (while (and (= (forward-line) 0)
421 (setq str (buffer-substring
422 (progn (beginning-of-line)(point))
423 (progn (end-of-line)(point))))
424 (setq ret (string-compare-from-top prefix str))
432 (cond ((> i 1) prefix)
434 (goto-char (point-min))
436 (narrow-to-region (point)
437 (+ (point)(length prefix)))
438 (goto-char (point-max))
439 (if (re-search-backward
440 (concat "[" citation-mark-chars "]") nil t)
442 (goto-char (match-end 0))
443 (if (looking-at "[ \t]+")
444 (goto-char (match-end 0))
446 (buffer-substring (point-min)(point))
450 (goto-char (point-max))
452 (concat "[" citation-disable-chars "]") nil t)
454 (concat "[" citation-mark-chars "]") nil t)
456 (goto-char (match-end 0))
457 (if (looking-at "[ \t]+")
458 (goto-char (match-end 0))
460 (buffer-substring (point-min)(point))
465 (defun fill-cited-region (beg end)
470 (and (search-backward "\n" nil t)
471 (setq end (match-end 0))
473 (narrow-to-region beg end)
474 (let* ((fill-prefix (detect-paragraph-cited-prefix))
475 (pat (concat fill-prefix "\n"))
477 (goto-char (point-min))
478 (while (search-forward pat nil t)
479 (let ((b (match-beginning 0))
483 (if (and (> b (point-min))
484 (let ((cat (char-category
486 (or (string-match "a" cat)
487 (string-match "l" cat)
493 (goto-char (point-min))
494 (fill-region (point-min) (point-max))
497 (defun compress-cited-prefix ()
500 (goto-char (point-min))
502 (concat "^" (regexp-quote mail-header-separator) "$") nil t)
503 (while (re-search-forward
504 (concat "^\\([ \t]*[^ \t\n" citation-mark-chars "]*["
505 citation-mark-chars "]\\)+") nil t)
506 (let* ((b (match-beginning 0))
508 (prefix (buffer-substring b e))
511 (if (string-match "<[^<>]+>" prefix)
512 (setq prefix (substring prefix 0 (match-beginning 0)))
515 (concat "\\([" citation-mark-chars "]+\\)[ \t]*")
517 (setq i (+ i (- (match-end 1)(match-beginning 1)))
519 pe (match-beginning 1)
523 (if (and ps (< ps pe))
526 (insert (concat (substring prefix ps pe) (make-string nest ?>)))
529 (defun replace-top-string (old new)
530 (interactive "*sOld string: \nsNew string: ")
531 (while (re-search-forward
532 (concat "^" (regexp-quote old)) nil t)
536 (defun string-compare-from-top (str1 str2)
537 (let* ((len1 (length str1))
539 (len (min len1 len2))
542 (while (and (< p len)
544 (setq c1 (sref str1 p)
548 (setq p (+ p (char-length c1)))
551 (let ((matched (substring str1 0 p))
552 (r1 (and (< p len1)(substring str1 p)))
553 (r2 (and (< p len2)(substring str2 p)))
557 (list 'seq matched (list 'or r1 r2))
566 (run-hooks 'mu-cite-load-hook)
568 ;;; mu-cite.el ends here