1 ;;; mu-cite.el --- yet another citation tool for GNU Emacs
3 ;; Copyright (C) 1995,1996,1997 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.49 $
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))
56 (defconst mu-cite/RCS-ID
57 "$Id: mu-cite.el,v 7.49 1997/03/18 15:07:56 morioka Exp $")
58 (defconst mu-cite/version (get-version-string mu-cite/RCS-ID))
64 (defvar mu-cite/cited-prefix-regexp "\\(^[^ \t\n<>]+>+[ \t]*\\|^[ \t]*$\\)"
65 "*Regexp to match the citation prefix.
66 If match, mu-cite doesn't insert citation prefix.")
68 (defvar mu-cite/prefix-format '(prefix-register-verbose "> ")
69 "*List to represent citation prefix.
70 Each elements must be string or method name.")
72 (defvar mu-cite/top-format '(in-id
73 ">>>>> " from " wrote:\n")
74 "*List to represent top string of citation.
75 Each elements must be string or method name.")
81 (defvar mu-cite-load-hook nil
82 "*List of functions called after mu-cite is loaded.
83 Use this hook to add your own methods to `mu-cite/default-methods-alist'.")
85 (defvar mu-cite/instantiation-hook nil
86 "*List of functions called just before narrowing to the message.")
88 (defvar mu-cite/pre-cite-hook nil
89 "*List of functions called before citing a region of text.")
91 (defvar mu-cite/post-cite-hook nil
92 "*List of functions called after citing a region of text.")
98 (defvar mu-cite/get-field-value-method-alist nil
99 "Alist major-mode vs. function to get field-body of header.")
101 (defun mu-cite/get-field-value (name)
102 (or (std11-field-body name)
103 (let ((method (assq major-mode mu-cite/get-field-value-method-alist)))
105 (funcall (cdr method) name)
109 ;;; @ prefix registration
112 (defvar mu-cite/registration-file (expand-file-name "~/.mu-cite.el")
113 "*The name of the user environment file for mu-cite.")
115 (defvar mu-cite/allow-null-string-registration nil
116 "*If non-nil, null-string citation-name is registered.")
118 (defvar mu-cite/registration-symbol 'mu-cite/citation-name-alist)
120 (defvar mu-cite/citation-name-alist nil)
121 (or (eq 'mu-cite/citation-name-alist mu-cite/registration-symbol)
122 (setq mu-cite/citation-name-alist
123 (symbol-value mu-cite/registration-symbol))
125 (defvar mu-cite/minibuffer-history nil)
127 ;; get citation-name from the database
128 (defun mu-cite/get-citation-name (from)
129 (assoc-value from mu-cite/citation-name-alist)
132 ;; register citation-name to the database
133 (defun mu-cite/add-citation-name (name from)
134 (setq mu-cite/citation-name-alist
135 (put-alist from name mu-cite/citation-name-alist))
136 (mu-cite/save-registration-file)
139 ;; load/save registration file
140 (defun mu-cite/load-registration-file ()
141 (let* ((file mu-cite/registration-file)
142 (buffer (get-buffer-create " *mu-register*")))
143 (if (file-readable-p file)
148 (insert-file-contents file)
150 (eval-current-buffer))
151 (kill-buffer buffer))
153 (add-hook 'mu-cite-load-hook (function mu-cite/load-registration-file))
155 (defun mu-cite/save-registration-file ()
156 (let* ((file mu-cite/registration-file)
157 (buffer (get-buffer-create " *mu-register*")))
161 (setq buffer-file-name file)
163 (insert ";;; " (file-name-nondirectory file) "\n")
164 (insert ";;; This file is generated automatically by mu-cite "
165 mu-cite/version "\n\n")
167 (symbol-name mu-cite/registration-symbol)
170 (function prin1-to-string)
171 mu-cite/citation-name-alist "\n "))
174 (file-name-nondirectory file)
177 (kill-buffer buffer))))
186 (defvar mu-cite/ml-count-field-list
187 '("X-Ml-Count" "X-Mail-Count" "X-Seqno" "X-Sequence" "Mailinglist-Id")
188 "*List of header fields which contain sequence number of mailing list.")
190 (defun mu-cite/get-ml-count-method ()
191 (let ((field-list mu-cite/ml-count-field-list))
194 (let* ((field (car field-list))
195 (ml-count (mu-cite/get-field-value field)))
196 (if (and ml-count (string-match "[0-9]+" ml-count))
199 (match-beginning 0)(match-end 0))
201 (setq field-list (cdr field-list))
205 ;;; @@ prefix and registration
208 (defun mu-cite/get-prefix-method ()
209 (or (mu-cite/get-citation-name (mu-cite/get-value 'address))
213 (defun mu-cite/get-prefix-register-method ()
214 (let ((addr (mu-cite/get-value 'address)))
215 (or (mu-cite/get-citation-name addr)
217 (read-string "Citation name? "
218 (or (mu-cite/get-value 'x-attribution)
219 (mu-cite/get-value 'full-name))
220 'mu-cite/minibuffer-history)
222 (if (and (or mu-cite/allow-null-string-registration
223 (not (string-equal return "")))
224 (y-or-n-p (format "Register \"%s\"? " return)))
225 (mu-cite/add-citation-name return addr)
229 (defun mu-cite/get-prefix-register-verbose-method ()
230 (let* ((addr (mu-cite/get-value 'address))
231 (return1 (mu-cite/get-citation-name addr))
232 (return (read-string "Citation name? "
234 (mu-cite/get-value 'x-attribution)
235 (mu-cite/get-value 'full-name))
236 'mu-cite/minibuffer-history))
238 (if (and (or mu-cite/allow-null-string-registration
239 (not (string-equal return "")))
240 (not (string-equal return return1))
241 (y-or-n-p (format "Register \"%s\"? " return))
243 (mu-cite/add-citation-name return addr)
251 (defvar mu-cite/default-methods-alist
255 (mu-cite/get-field-value "From")
260 (mu-cite/get-field-value "Date")
265 (mu-cite/get-field-value "Message-Id")
270 (mu-cite/get-field-value "Subject")
275 (mu-cite/get-field-value "X-Ml-Name")
277 (cons 'ml-count (function mu-cite/get-ml-count-method))
278 (cons 'address-structure
282 (std11-parse-address-string (mu-cite/get-value 'from))
287 (std11-full-name-string
288 (mu-cite/get-value 'address-structure))
293 (std11-address-string
294 (mu-cite/get-value 'address-structure))
299 (let ((ml-name (mu-cite/get-value 'ml-name)))
304 (mu-cite/get-value 'ml-count)
306 (mu-cite/get-value 'message-id)
311 (let ((id (mu-cite/get-value 'id)))
313 (format ">>>>> In %s \n" id)
315 (cons 'prefix (function mu-cite/get-prefix-method))
316 (cons 'prefix-register
317 (function mu-cite/get-prefix-register-method))
318 (cons 'prefix-register-verbose
319 (function mu-cite/get-prefix-register-verbose-method))
323 (mu-cite/get-field-value "X-Attribution")
331 (defvar mu-cite/methods-alist nil)
333 (defun mu-cite/make-methods ()
334 (setq mu-cite/methods-alist
335 (copy-alist mu-cite/default-methods-alist))
336 (run-hooks 'mu-cite/instantiation-hook)
339 (defun mu-cite/get-value (item)
340 (let ((ret (assoc-value item mu-cite/methods-alist)))
343 (setq ret (funcall ret))
344 (set-alist 'mu-cite/methods-alist item ret)
348 (defun mu-cite/eval-format (list)
351 (cond ((stringp elt) elt)
352 ((symbolp elt) (mu-cite/get-value elt))
361 (defun mu-cite/cite-original ()
362 "Citing filter function.
363 This is callable from the various mail and news readers' reply
364 function according to the agreed upon standard."
366 (mu-cite/make-methods)
368 (if (< (mark t) (point))
369 (exchange-point-and-mark))
370 (narrow-to-region (point)(point-max))
371 (run-hooks 'mu-cite/pre-cite-hook)
372 (let ((last-point (point))
373 (top (mu-cite/eval-format mu-cite/top-format))
374 (prefix (mu-cite/eval-format mu-cite/prefix-format))
376 (if (re-search-forward "^-*$" nil nil)
380 (delete-region last-point (point))
382 (setq last-point (point))
383 (while (< (point)(mark t))
384 (or (looking-at mu-cite/cited-prefix-regexp)
387 (goto-char last-point)
389 (run-hooks 'mu-cite/post-cite-hook)
393 ;;; @ message editing utilities
396 (defvar citation-mark-chars ">}|"
397 "*String of characters for citation delimiter. [mu-cite.el]")
399 (defvar citation-disable-chars "<{"
400 "*String of characters not allowed as citation-prefix.")
402 (defun detect-paragraph-cited-prefix ()
404 (goto-char (point-min))
408 (progn (beginning-of-line)(point))
409 (progn (end-of-line)(point))
412 (while (and (= (forward-line) 0)
413 (setq str (buffer-substring
414 (progn (beginning-of-line)(point))
415 (progn (end-of-line)(point))))
416 (setq ret (string-compare-from-top prefix str))
424 (cond ((> i 1) prefix)
426 (goto-char (point-min))
428 (narrow-to-region (point)
429 (+ (point)(length prefix)))
430 (goto-char (point-max))
431 (if (re-search-backward
432 (concat "[" citation-mark-chars "]") nil t)
434 (goto-char (match-end 0))
435 (if (looking-at "[ \t]+")
436 (goto-char (match-end 0))
438 (buffer-substring (point-min)(point))
442 (goto-char (point-max))
444 (concat "[" citation-disable-chars "]") nil t)
446 (concat "[" citation-mark-chars "]") nil t)
448 (goto-char (match-end 0))
449 (if (looking-at "[ \t]+")
450 (goto-char (match-end 0))
452 (buffer-substring (point-min)(point))
457 (defun fill-cited-region (beg end)
462 (and (search-backward "\n" nil t)
463 (setq end (match-end 0))
465 (narrow-to-region beg end)
466 (let* ((fill-prefix (detect-paragraph-cited-prefix))
467 (pat (concat fill-prefix "\n"))
469 (goto-char (point-min))
470 (while (search-forward pat nil t)
471 (let ((b (match-beginning 0))
475 (if (and (> b (point-min))
476 (let ((cat (char-category
478 (or (string-match "a" cat)
479 (string-match "l" cat)
485 (goto-char (point-min))
486 (fill-region (point-min) (point-max))
489 (defun compress-cited-prefix ()
492 (goto-char (point-min))
494 (concat "^" (regexp-quote mail-header-separator) "$") nil t)
495 (while (re-search-forward
496 (concat "^\\([ \t]*[^ \t\n" citation-mark-chars "]*["
497 citation-mark-chars "]\\)+") nil t)
498 (let* ((b (match-beginning 0))
500 (prefix (buffer-substring b e))
503 (if (string-match "<[^<>]+>" prefix)
504 (setq prefix (substring prefix 0 (match-beginning 0)))
507 (concat "\\([" citation-mark-chars "]+\\)[ \t]*")
509 (setq i (+ i (- (match-end 1)(match-beginning 1)))
511 pe (match-beginning 1)
515 (if (and ps (< ps pe))
518 (insert (concat (substring prefix ps pe) (make-string nest ?>)))
521 (defun replace-top-string (old new)
522 (interactive "*sOld string: \nsNew string: ")
523 (while (re-search-forward
524 (concat "^" (regexp-quote old)) nil t)
534 (run-hooks 'mu-cite-load-hook)
536 ;;; mu-cite.el ends here