1 ;;; mu-cite.el --- yet another citation tool for GNU Emacs
3 ;; Copyright (C) 1995,1996 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.36 $
10 ;; Keywords: mail, news, citation
12 ;; This file is part of tl (Tiny Library).
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 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.
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))
56 (defconst mu-cite/RCS-ID
57 "$Id: mu-cite.el,v 7.36 1996/08/30 04:25:34 morioka Exp $")
58 (defconst mu-cite/version (get-version-string mu-cite/RCS-ID))
64 (defvar cited-prefix-regexp "^[^ \t>]*[>|]+[ \t#]*")
65 (defvar mu-cite/cited-prefix-regexp "\\(^[^ \t\n>]+>+[ \t]*\\|^[ \t]*$\\)")
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.")
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.")
88 (defvar mu-cite/get-field-value-method-alist
89 (list (cons 'mh-letter-mode
92 (if (and (stringp mh-sent-from-folder)
93 (numberp mh-sent-from-msg))
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)
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)))
108 (funcall (cdr method) name)
112 ;;; @ prefix registration
115 (defvar mu-cite/registration-file
116 (expand-file-name "~/.mu-cite.el")
117 "*The name of the user environment file for mu-cite.")
119 (defvar mu-cite/allow-null-string-registration nil
120 "*If non-nil, null-string citation-name is registered.")
122 (defvar mu-cite/registration-symbol 'mu-cite/citation-name-alist)
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))
130 (defvar mu-cite/minibuffer-history nil)
132 ;; get citation-name from the database
133 (defun mu-cite/get-citation-name (from)
134 (assoc-value from mu-cite/citation-name-alist)
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)
145 (defun mu-cite/save-to-file ()
146 (let* ((filename mu-cite/registration-file)
147 (buffer (get-buffer-create " *mu-register*")))
150 (setq buffer-file-name filename)
153 (format ";;; %s\n" (file-name-nondirectory filename)))
155 (format ";;; This file is generated automatically by mu-cite %s.\n\n"
157 (insert (format "(setq %s\n '(" mu-cite/registration-symbol))
159 (function prin1-to-string)
160 mu-cite/citation-name-alist "\n "))
163 (format ";;; %s ends here.\n" (file-name-nondirectory filename)))
165 (kill-buffer buffer)))
174 (defvar mu-cite/ml-count-field-list
175 '("X-Ml-Count" "X-Mail-Count" "X-Seqno" "X-Sequence" "Mailinglist-Id"))
177 (defun mu-cite/get-ml-count-method ()
178 (let ((field-list mu-cite/ml-count-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))
186 (match-beginning 0)(match-end 0))
188 (setq field-list (cdr field-list))
192 ;;; @@ prefix and registration
195 (defun mu-cite/get-prefix-method ()
196 (or (mu-cite/get-citation-name (mu-cite/get-value 'address))
200 (defun mu-cite/get-prefix-register-method ()
201 (let ((addr (mu-cite/get-value 'address)))
202 (or (mu-cite/get-citation-name addr)
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)
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)
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? "
221 (mu-cite/get-value 'x-attribution)
222 (mu-cite/get-value 'full-name))
223 'mu-cite/minibuffer-history))
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))
230 (mu-cite/add-citation-name return addr)
238 (defvar mu-cite/default-methods-alist
242 (mu-cite/get-field-value "From")
247 (mu-cite/get-field-value "Date")
252 (mu-cite/get-field-value "Message-Id")
257 (mu-cite/get-field-value "Subject")
262 (mu-cite/get-field-value "X-Ml-Name")
264 (cons 'ml-count (function mu-cite/get-ml-count-method))
265 (cons 'address-structure
269 (std11-parse-address-string (mu-cite/get-value 'from))
274 (std11-full-name-string
275 (mu-cite/get-value 'address-structure))
280 (std11-address-string
281 (mu-cite/get-value 'address-structure))
286 (let ((ml-name (mu-cite/get-value 'ml-name)))
291 (mu-cite/get-value 'ml-count)
293 (mu-cite/get-value 'message-id)
298 (let ((id (mu-cite/get-value 'id)))
300 (format ">>>>> In %s \n" id)
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))
310 (mu-cite/get-field-value "X-Attribution")
318 (defvar mu-cite/methods-alist nil)
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)
326 (defun mu-cite/get-value (item)
327 (let ((ret (assoc-value item mu-cite/methods-alist)))
330 (setq ret (funcall ret))
331 (set-alist 'mu-cite/methods-alist item ret)
335 (defun mu-cite/eval-format (list)
338 (cond ((stringp elt) elt)
339 ((symbolp elt) (mu-cite/get-value elt))
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."
353 (mu-cite/make-methods)
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))
363 (if (re-search-forward "^$\\|^-+$" nil nil)
367 (delete-region last-point (point))
369 (setq last-point (point))
370 (while (< (point)(mark t))
371 (or (looking-at mu-cite/cited-prefix-regexp)
374 (goto-char last-point)
376 (run-hooks 'mu-cite/post-cite-hook)
380 ;;; @ message editing utilities
383 (defun fill-cited-region (beg end)
392 (narrow-to-region beg end)
393 (goto-char (point-min))
395 (let* ((str1 (buffer-substring
396 (progn (beginning-of-line)(point))
397 (progn (end-of-line)(point))
399 (str2 (let ((p0 (point)))
401 (if (> (count-lines p0 (point)) 0)
403 (progn (beginning-of-line)(point))
404 (progn (end-of-line)(point))
406 (ret (string-compare-from-top str1 str2))
410 (goto-char (point-min))
411 (if (re-search-forward cited-prefix-regexp nil t)
412 (buffer-substring (match-beginning 0) (match-end 0))
414 (pat (concat "\n" fill-prefix))
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)))
427 (goto-char (point-min))
428 (fill-region (point-min) (point-max))
431 (defvar citation-mark-chars ">}|")
433 (defun compress-cited-prefix ()
436 (goto-char (point-min))
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))
444 (prefix (buffer-substring b e))
447 (if (string-match "<[^<>]+>" prefix)
448 (setq prefix (substring prefix 0 (match-beginning 0)))
451 (concat "\\([" citation-mark-chars "]+\\)[ \t]*")
453 (setq i (+ i (- (match-end 1)(match-beginning 1)))
455 pe (match-beginning 1)
459 (if (and ps (< ps pe))
462 (insert (concat (substring prefix ps pe) (make-string nest ?>)))
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)
478 (run-hooks 'mu-cite-load-hook)
480 ;;; mu-cite.el ends here