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 <morioka@jaist.ac.jp>
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))
56 (defconst mu-cite/version "7.52")
62 (defvar mu-cite/cited-prefix-regexp "\\(^[^ \t\n<>]+>+[ \t]*\\|^[ \t]*$\\)"
63 "*Regexp to match the citation prefix.
64 If match, mu-cite doesn't insert citation prefix.")
66 (defvar mu-cite/prefix-format '(prefix-register-verbose "> ")
67 "*List to represent citation prefix.
68 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-load-hook nil
80 "*List of functions called after mu-cite is loaded.
81 Use this hook to add your own methods to `mu-cite/default-methods-alist'.")
83 (defvar mu-cite/instantiation-hook nil
84 "*List of functions called just before narrowing to the message.")
86 (defvar mu-cite/pre-cite-hook nil
87 "*List of functions called before citing a region of text.")
89 (defvar mu-cite/post-cite-hook nil
90 "*List of functions called after citing a region of text.")
96 (defvar mu-cite/get-field-value-method-alist nil
97 "Alist major-mode vs. function to get field-body of header.")
99 (defun mu-cite/get-field-value (name)
100 (or (std11-field-body name)
101 (let ((method (assq major-mode mu-cite/get-field-value-method-alist)))
103 (funcall (cdr method) name)
107 ;;; @ prefix registration
110 (defvar mu-cite/registration-file (expand-file-name "~/.mu-cite.el")
111 "*The name of the user environment file for mu-cite.")
113 (defvar mu-cite/allow-null-string-registration nil
114 "*If non-nil, null-string citation-name is registered.")
116 (defvar mu-cite/registration-symbol 'mu-cite/citation-name-alist)
118 (defvar mu-cite/citation-name-alist nil)
119 (or (eq 'mu-cite/citation-name-alist mu-cite/registration-symbol)
120 (setq mu-cite/citation-name-alist
121 (symbol-value mu-cite/registration-symbol))
123 (defvar mu-cite/minibuffer-history nil)
125 ;; get citation-name from the database
126 (defun mu-cite/get-citation-name (from)
127 (cdr (assoc from mu-cite/citation-name-alist))
130 ;; register citation-name to the database
131 (defun mu-cite/add-citation-name (name from)
132 (setq mu-cite/citation-name-alist
133 (put-alist from name mu-cite/citation-name-alist))
134 (mu-cite/save-registration-file)
137 ;; load/save registration file
138 (defun mu-cite/load-registration-file ()
139 (let* ((file mu-cite/registration-file)
140 (buffer (get-buffer-create " *mu-register*")))
141 (if (file-readable-p file)
146 (insert-file-contents file)
148 (eval-current-buffer))
149 (kill-buffer buffer))
151 (add-hook 'mu-cite-load-hook (function mu-cite/load-registration-file))
153 (defun mu-cite/save-registration-file ()
154 (let* ((file mu-cite/registration-file)
155 (buffer (get-buffer-create " *mu-register*")))
159 (setq buffer-file-name file)
161 (insert ";;; " (file-name-nondirectory file) "\n")
162 (insert ";;; This file is generated automatically by mu-cite "
163 mu-cite/version "\n\n")
165 (symbol-name mu-cite/registration-symbol)
168 (function prin1-to-string)
169 mu-cite/citation-name-alist "\n "))
172 (file-name-nondirectory file)
175 (kill-buffer buffer))))
184 (defvar mu-cite/ml-count-field-list
185 '("X-Ml-Count" "X-Mail-Count" "X-Seqno" "X-Sequence" "Mailinglist-Id")
186 "*List of header fields which contain sequence number of mailing list.")
188 (defun mu-cite/get-ml-count-method ()
189 (let ((field-list mu-cite/ml-count-field-list))
192 (let* ((field (car field-list))
193 (ml-count (mu-cite/get-field-value field)))
194 (if (and ml-count (string-match "[0-9]+" ml-count))
197 (match-beginning 0)(match-end 0))
199 (setq field-list (cdr field-list))
203 ;;; @@ prefix and registration
206 (defun mu-cite/get-prefix-method ()
207 (or (mu-cite/get-citation-name (mu-cite/get-value 'address))
211 (defun mu-cite/get-prefix-register-method ()
212 (let ((addr (mu-cite/get-value 'address)))
213 (or (mu-cite/get-citation-name addr)
215 (read-string "Citation name? "
216 (or (mu-cite/get-value 'x-attribution)
217 (mu-cite/get-value 'full-name))
218 'mu-cite/minibuffer-history)
220 (if (and (or mu-cite/allow-null-string-registration
221 (not (string-equal return "")))
222 (y-or-n-p (format "Register \"%s\"? " return)))
223 (mu-cite/add-citation-name return addr)
227 (defun mu-cite/get-prefix-register-verbose-method ()
228 (let* ((addr (mu-cite/get-value 'address))
229 (return1 (mu-cite/get-citation-name addr))
230 (return (read-string "Citation name? "
232 (mu-cite/get-value 'x-attribution)
233 (mu-cite/get-value 'full-name))
234 'mu-cite/minibuffer-history))
236 (if (and (or mu-cite/allow-null-string-registration
237 (not (string-equal return "")))
238 (not (string-equal return return1))
239 (y-or-n-p (format "Register \"%s\"? " return))
241 (mu-cite/add-citation-name return addr)
249 (defvar mu-cite/default-methods-alist
253 (mu-cite/get-field-value "From")
258 (mu-cite/get-field-value "Date")
263 (mu-cite/get-field-value "Message-Id")
268 (mu-cite/get-field-value "Subject")
273 (mu-cite/get-field-value "X-Ml-Name")
275 (cons 'ml-count (function mu-cite/get-ml-count-method))
276 (cons 'address-structure
280 (std11-parse-address-string (mu-cite/get-value 'from))
285 (std11-full-name-string
286 (mu-cite/get-value 'address-structure))
291 (std11-address-string
292 (mu-cite/get-value 'address-structure))
297 (let ((ml-name (mu-cite/get-value 'ml-name)))
302 (mu-cite/get-value 'ml-count)
304 (mu-cite/get-value 'message-id)
309 (let ((id (mu-cite/get-value 'id)))
311 (format ">>>>> In %s \n" id)
313 (cons 'prefix (function mu-cite/get-prefix-method))
314 (cons 'prefix-register
315 (function mu-cite/get-prefix-register-method))
316 (cons 'prefix-register-verbose
317 (function mu-cite/get-prefix-register-verbose-method))
321 (mu-cite/get-field-value "X-Attribution")
329 (defvar mu-cite/methods-alist nil)
331 (defun mu-cite/make-methods ()
332 (setq mu-cite/methods-alist
333 (copy-alist mu-cite/default-methods-alist))
334 (run-hooks 'mu-cite/instantiation-hook)
337 (defun mu-cite/get-value (item)
338 (let ((ret (cdr (assoc item mu-cite/methods-alist))))
341 (setq ret (funcall ret))
342 (set-alist 'mu-cite/methods-alist item ret)
346 (defun mu-cite/eval-format (list)
350 (cond ((stringp elt) elt)
351 ((symbolp elt) (mu-cite/get-value elt))
360 (defun mu-cite/cite-original ()
361 "Citing filter function.
362 This is callable from the various mail and news readers' reply
363 function according to the agreed upon standard."
365 (mu-cite/make-methods)
367 (if (< (mark t) (point))
368 (exchange-point-and-mark))
369 (narrow-to-region (point)(point-max))
370 (run-hooks 'mu-cite/pre-cite-hook)
371 (let ((last-point (point))
372 (top (mu-cite/eval-format mu-cite/top-format))
373 (prefix (mu-cite/eval-format mu-cite/prefix-format))
375 (if (re-search-forward "^-*$" nil nil)
379 (delete-region last-point (point))
381 (setq last-point (point))
382 (while (< (point)(mark t))
383 (or (looking-at mu-cite/cited-prefix-regexp)
386 (goto-char last-point)
388 (run-hooks 'mu-cite/post-cite-hook)
392 ;;; @ message editing utilities
395 (defvar citation-mark-chars ">}|"
396 "*String of characters for citation delimiter. [mu-cite.el]")
398 (defvar citation-disable-chars "<{"
399 "*String of characters not allowed as citation-prefix.")
401 (defun detect-paragraph-cited-prefix ()
403 (goto-char (point-min))
407 (progn (beginning-of-line)(point))
408 (progn (end-of-line)(point))
411 (while (and (= (forward-line) 0)
412 (setq str (buffer-substring
413 (progn (beginning-of-line)(point))
414 (progn (end-of-line)(point))))
415 (setq ret (string-compare-from-top prefix str))
423 (cond ((> i 1) prefix)
425 (goto-char (point-min))
427 (narrow-to-region (point)
428 (+ (point)(length prefix)))
429 (goto-char (point-max))
430 (if (re-search-backward
431 (concat "[" citation-mark-chars "]") nil t)
433 (goto-char (match-end 0))
434 (if (looking-at "[ \t]+")
435 (goto-char (match-end 0))
437 (buffer-substring (point-min)(point))
441 (goto-char (point-max))
443 (concat "[" citation-disable-chars "]") nil t)
445 (concat "[" citation-mark-chars "]") nil t)
447 (goto-char (match-end 0))
448 (if (looking-at "[ \t]+")
449 (goto-char (match-end 0))
451 (buffer-substring (point-min)(point))
456 (defun fill-cited-region (beg end)
461 (and (search-backward "\n" nil t)
462 (setq end (match-end 0))
464 (narrow-to-region beg end)
465 (let* ((fill-prefix (detect-paragraph-cited-prefix))
466 (pat (concat fill-prefix "\n"))
468 (goto-char (point-min))
469 (while (search-forward pat nil t)
470 (let ((b (match-beginning 0))
474 (if (and (> b (point-min))
475 (let ((cat (char-category
477 (or (string-match "a" cat)
478 (string-match "l" cat)
484 (goto-char (point-min))
485 (fill-region (point-min) (point-max))
488 (defun compress-cited-prefix ()
491 (goto-char (point-min))
493 (concat "^" (regexp-quote mail-header-separator) "$") nil t)
494 (while (re-search-forward
495 (concat "^\\([ \t]*[^ \t\n" citation-mark-chars "]*["
496 citation-mark-chars "]\\)+") nil t)
497 (let* ((b (match-beginning 0))
499 (prefix (buffer-substring b e))
502 (if (string-match "<[^<>]+>" prefix)
503 (setq prefix (substring prefix 0 (match-beginning 0)))
506 (concat "\\([" citation-mark-chars "]+\\)[ \t]*")
508 (setq i (+ i (- (match-end 1)(match-beginning 1)))
510 pe (match-beginning 1)
514 (if (and ps (< ps pe))
517 (insert (concat (substring prefix ps pe) (make-string nest ?>)))
520 (defun replace-top-string (old new)
521 (interactive "*sOld string: \nsNew string: ")
522 (while (re-search-forward
523 (concat "^" (regexp-quote old)) nil t)
527 (defun string-compare-from-top (str1 str2)
528 (let* ((len1 (length str1))
530 (len (min len1 len2))
533 (while (and (< p len)
535 (setq c1 (sref str1 p)
539 (setq p (+ p (char-length c1)))
542 (let ((matched (substring str1 0 p))
543 (r1 (and (< p len1)(substring str1 p)))
544 (r2 (and (< p len2)(substring str2 p)))
548 (list 'seq matched (list 'or r1 r2))
557 (run-hooks 'mu-cite-load-hook)
559 ;;; mu-cite.el ends here