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-original "mu-cite" nil t)
35 ;; ;; for all but message-mode
36 ;; (add-hook 'mail-citation-hook (function mu-cite-original))
37 ;; ;; for message-mode only
38 ;; (setq message-cite-function (function mu-cite-original))
40 ;; ;; for all but mh-e
41 ;; (add-hook 'mail-yank-hooks (function mu-cite-original))
43 ;; (add-hook 'mh-yank-hooks (function mu-cite-original))
47 (eval-when-compile (require 'cl))
49 ;; Pickup some macros, e.g. `with-temp-buffer', for old Emacsen.
52 ;; Pickup `char-category' for XEmacs.
63 (defconst mu-cite-version "8.0")
69 ;; This part will be abolished in the future.
72 (eval-when-compile (require 'static))
74 (defvar mu-cite/registration-file)
75 (defvar mu-cite-registration-file)
76 (defmacro mu-cite-registration-file ()
77 (if (fboundp 'defvaralias)
78 'mu-cite-registration-file
79 '(if (and (not noninteractive)
80 (boundp 'mu-cite/registration-file))
83 (format "Obsolete variable `%s' found, use anyway? "
84 'mu-cite/registration-file))
85 (symbol-value 'mu-cite/registration-file)
86 mu-cite-registration-file)
87 (message "You should use `%s' instead of `%s'."
88 'mu-cite-registration-file 'mu-cite/registration-file)
90 mu-cite-registration-file)))
93 (defconst mu-cite-obsolete-variable-alist
94 '((mu-cite/allow-null-string-registration
95 mu-cite-allow-null-string-registration)
96 (mu-cite/citation-name-alist mu-cite-citation-name-alist)
97 (mu-cite/cited-prefix-regexp mu-cite-cited-prefix-regexp)
98 (mu-cite/default-methods-alist mu-cite-default-methods-alist)
99 (mu-cite/instantiation-hook mu-cite-instantiation-hook)
100 (mu-cite/minibuffer-history mu-cite-minibuffer-history)
101 (mu-cite/ml-count-field-list mu-cite-ml-count-field-list)
102 (mu-cite/post-cite-hook mu-cite-post-cite-hook)
103 (mu-cite/pre-cite-hook mu-cite-pre-cite-hook)
104 (mu-cite/prefix-format mu-cite-prefix-format)
105 (mu-cite/registration-file mu-cite-registration-file)
106 (mu-cite/top-format mu-cite-top-format)))
111 (apply (function make-obsolete-variable) elem)
112 (when (and (not noninteractive)
114 (apply (function message)
115 "WARNING: `%s' is an obsolete variable, use `%s' instead."
117 (static-if (fboundp 'defvaralias) ; It may exists in XEmacs.
118 (apply (function defvaralias) elem)
119 (when (boundp (car elem))
120 (eval (list 'defvar (cadr elem) (car elem)))))))
121 mu-cite-obsolete-variable-alist)
126 (defconst mu-cite-obsolete-function-alist
127 '((mu-cite/add-citation-name mu-cite-add-citation-name)
128 (mu-cite/cite-original mu-cite-original)
129 (mu-cite/eval-format mu-cite-eval-format)
130 (mu-cite/get-citation-name mu-cite-get-citation-name)
131 (mu-cite/get-field-value mu-cite-get-field-value)
132 (mu-cite/get-ml-count-method mu-cite-get-ml-count-method)
133 (mu-cite/get-prefix-method mu-cite-get-prefix-method)
134 (mu-cite/get-prefix-register-method
135 mu-cite-get-prefix-register-method)
136 (mu-cite/get-prefix-register-verbose-method
137 mu-cite-get-prefix-register-verbose-method)
138 (mu-cite/get-value mu-cite-get-value)
139 (mu-cite/load-registration-file mu-cite-load-registration-file)
140 (mu-cite/make-methods mu-cite-make-methods)
141 (mu-cite/save-registration-file mu-cite-save-registration-file)))
144 (function (lambda (elem)
145 (apply (function define-obsolete-function-alias) elem)))
146 mu-cite-obsolete-function-alist)
153 (defgroup mu-cite nil
154 "yet another citation tool for GNU Emacs."
159 (defvar mu-cite-default-methods-alist
163 (mu-cite-get-field-value "From"))))
167 (mu-cite-get-field-value "Date"))))
171 (mu-cite-get-field-value "Message-Id"))))
175 (mu-cite-get-field-value "Subject"))))
179 (mu-cite-get-field-value "X-Ml-Name"))))
180 (cons 'ml-count (function mu-cite-get-ml-count-method))
181 (cons 'address-structure
185 (std11-parse-address-string (mu-cite-get-value 'from))))))
189 (std11-full-name-string
190 (mu-cite-get-value 'address-structure)))))
194 (std11-address-string
195 (mu-cite-get-value 'address-structure)))))
199 (let ((ml-name (mu-cite-get-value 'ml-name)))
204 (mu-cite-get-value 'ml-count)
206 (mu-cite-get-value 'message-id))))))
210 (let ((id (mu-cite-get-value 'id)))
212 (format ">>>>> In %s \n" id)
214 (cons 'prefix (function mu-cite-get-prefix-method))
215 (cons 'prefix-register
216 (function mu-cite-get-prefix-register-method))
217 (cons 'prefix-register-verbose
218 (function mu-cite-get-prefix-register-verbose-method))
222 (mu-cite-get-field-value "X-Attribution"))))
229 (defcustom mu-cite-cited-prefix-regexp
230 "\\(^[^ \t\n<>]+>+[ \t]*\\|^[ \t]*$\\)"
231 "Regexp to match the citation prefix.
232 If match, mu-cite doesn't insert citation prefix."
236 (defcustom mu-cite-prefix-format '(prefix-register-verbose "> ")
237 "List to represent citation prefix.
238 Each elements must be string or method name."
240 (nconc '(choice :tag "String or Method name")
243 (lambda (elem) (list 'choice-item (car elem))))
244 mu-cite-default-methods-alist)
245 '((symbol :tag "Other Method")
247 (choice-item :tag "String: \"> \"" "> ")
248 (string :tag "Other String"))))
251 (defcustom mu-cite-top-format '(in-id ">>>>>\t" from " wrote:\n")
252 "List to represent top string of citation.
253 Each elements must be string or method name."
256 '(choice :tag "String or Method name")
259 (lambda (elem) (list 'choice-item (car elem))))
260 mu-cite-default-methods-alist)
261 '((symbol :tag "Other Method")
263 (choice-item :tag "String: \">>>>>\\t\"" ">>>>>\t")
264 (choice-item :tag "String: \" wrote:\\n\"" " wrote:\n")
265 (string :tag "Other String"))))
272 (defcustom mu-cite-load-hook nil
273 "List of functions called after mu-cite is loaded.
274 Use this hook to add your own methods to `mu-cite-default-methods-alist'."
278 (defcustom mu-cite-instantiation-hook nil
279 "List of functions called just before narrowing to the message."
283 (defcustom mu-cite-pre-cite-hook nil
284 "List of functions called before citing a region of text."
288 (defcustom mu-cite-post-cite-hook nil
289 "List of functions called after citing a region of text."
297 (defvar mu-cite-get-field-value-method-alist nil
298 "Alist major-mode vs. function to get field-body of header.")
300 (defun mu-cite-get-field-value (name)
301 (or (std11-field-body name)
302 (let ((method (assq major-mode mu-cite-get-field-value-method-alist)))
304 (funcall (cdr method) name)))))
307 ;;; @ prefix registration
310 (defcustom mu-cite-registration-file (expand-file-name "~/.mu-cite.el")
311 "The name of the user environment file for mu-cite."
315 (defcustom mu-cite-allow-null-string-registration nil
316 "If non-nil, null-string citation-name is registered."
320 (defcustom mu-cite-registration-file-coding-system-for-read nil
321 "Coding system for reading registration file."
324 (defcustom mu-cite-registration-file-coding-system-for-write nil
325 "Coding system for writing registration file."
328 (defcustom mu-cite-registration-file-modes 384
329 "Mode bits of `mu-cite-registration-file', as an integer."
333 (defvar mu-cite-registration-symbol 'mu-cite-citation-name-alist)
335 (defvar mu-cite-citation-name-alist nil)
336 (unless (eq 'mu-cite-citation-name-alist mu-cite-registration-symbol)
337 (setq mu-cite-citation-name-alist
338 (symbol-value mu-cite-registration-symbol)))
339 (defvar mu-cite-minibuffer-history nil)
341 ;; get citation-name from the database
342 (defun mu-cite-get-citation-name (from)
343 (cdr (assoc from mu-cite-citation-name-alist)))
345 ;; register citation-name to the database
346 (defun mu-cite-add-citation-name (name from)
347 (setq mu-cite-citation-name-alist
348 (put-alist from name mu-cite-citation-name-alist))
349 (mu-cite-save-registration-file))
351 ;; load/save registration file
352 ;;(defun mu-cite-load-registration-file ()
353 ;; (let ((file mu-cite-registration-file))
355 ;; (file-readable-p file))
359 ;; (` (let ((, mu-cite-registration-symbol))
360 ;; (if mu-cite-registration-file-coding-system-for-read
361 ;; (insert-file-contents-as-coding-system
362 ;; mu-cite-registration-file-coding-system-for-read
364 ;; (insert-file-contents file))
365 ;; (condition-case nil
367 ;; (eval-current-buffer)
368 ;; (, mu-cite-registration-symbol))
369 ;; (error nil))))))))
371 ;; (setq mu-cite-citation-name-alist alist))))))
372 (defun mu-cite-load-registration-file ()
373 (let ((file (mu-cite-registration-file)))
375 (file-readable-p file))
379 (` (let ((, mu-cite-registration-symbol)
380 mu-cite/citation-name-alist)
381 (if mu-cite-registration-file-coding-system-for-read
382 (insert-file-contents-as-coding-system
383 mu-cite-registration-file-coding-system-for-read
385 (insert-file-contents file))
388 (eval-current-buffer)
389 (or mu-cite/citation-name-alist
390 (, mu-cite-registration-symbol)))
393 (setq mu-cite-citation-name-alist alist))))))
394 (add-hook 'mu-cite-load-hook (function mu-cite-load-registration-file))
396 (defun mu-cite-save-registration-file ()
397 ;;(let ((file mu-cite-registration-file))
398 (let ((file (mu-cite-registration-file)))
401 (setq buffer-file-name file)
402 (insert ";;; " (file-name-nondirectory file) "\n")
403 (insert ";;; This file is generated automatically by mu-cite "
404 mu-cite-version "\n\n")
406 (symbol-name mu-cite-registration-symbol)
409 (function prin1-to-string)
410 mu-cite-citation-name-alist "\n "))
413 (file-name-nondirectory file)
415 (write-region 1 1 file nil 'nomsg)
417 (set-file-modes file mu-cite-registration-file-modes)
419 (if mu-cite-registration-file-coding-system-for-write
420 (save-buffer-as-coding-system
421 mu-cite-registration-file-coding-system-for-write)
431 (defcustom mu-cite-ml-count-field-list
432 '("X-Ml-Count" "X-Mail-Count" "X-Seqno" "X-Sequence" "Mailinglist-Id")
433 "List of header fields which contain sequence number of mailing list."
434 :type '(repeat (choice (choice-item "X-Ml-Count")
435 (choice-item "X-Mail-Count")
436 (choice-item "X-Seqno")
437 (choice-item "X-Sequence")
438 (choice-item "Mailinglist-Id")
440 (string :tag "Other")))
443 (defun mu-cite-get-ml-count-method ()
444 (let ((field-list mu-cite-ml-count-field-list))
447 (let* ((field (car field-list))
448 (ml-count (mu-cite-get-field-value field)))
449 (when (and ml-count (string-match "[0-9]+" ml-count))
452 (match-beginning 0)(match-end 0))))
453 (setq field-list (cdr field-list)))))))
456 ;;; @@ prefix and registration
459 (defun mu-cite-get-prefix-method ()
460 (or (mu-cite-get-citation-name (mu-cite-get-value 'address))
463 (defun mu-cite-get-prefix-register-method ()
464 (let ((addr (mu-cite-get-value 'address)))
465 (or (mu-cite-get-citation-name addr)
467 (read-string "Citation name? "
468 (or (mu-cite-get-value 'x-attribution)
469 (mu-cite-get-value 'full-name))
470 'mu-cite-minibuffer-history)))
471 (when (and (or mu-cite-allow-null-string-registration
472 (not (string-equal return "")))
473 (y-or-n-p (format "Register \"%s\"? " return)))
474 (mu-cite-add-citation-name return addr))
477 (defun mu-cite-get-prefix-register-verbose-method ()
478 (let* ((addr (mu-cite-get-value 'address))
479 (return1 (mu-cite-get-citation-name addr))
480 (return (read-string "Citation name? "
482 (mu-cite-get-value 'x-attribution)
483 (mu-cite-get-value 'full-name))
484 'mu-cite-minibuffer-history)))
485 (when (and (or mu-cite-allow-null-string-registration
486 (not (string-equal return "")))
487 (not (string-equal return return1))
488 (y-or-n-p (format "Register \"%s\"? " return)))
489 (mu-cite-add-citation-name return addr))
496 (defvar mu-cite-methods-alist nil)
498 (defun mu-cite-make-methods ()
499 (setq mu-cite-methods-alist
500 (copy-alist mu-cite-default-methods-alist))
501 (run-hooks 'mu-cite-instantiation-hook))
503 (defun mu-cite-get-value (item)
504 (let ((ret (cdr (assoc item mu-cite-methods-alist))))
507 (setq ret (save-excursion (funcall ret)))
508 (set-alist 'mu-cite-methods-alist item ret))
511 (defun mu-cite-eval-format (list)
514 (cond ((stringp elt) elt)
515 ((symbolp elt) (mu-cite-get-value elt)))))
523 (defun mu-cite-original ()
524 "Citing filter function.
525 This is callable from the various mail and news readers' reply
526 function according to the agreed upon standard."
528 (mu-cite-make-methods)
530 (when (< (mark t) (point))
531 (exchange-point-and-mark))
532 (narrow-to-region (point)(point-max))
533 (run-hooks 'mu-cite-pre-cite-hook)
534 (let ((last-point (point))
535 (top (mu-cite-eval-format mu-cite-top-format))
536 (prefix (mu-cite-eval-format mu-cite-prefix-format)))
537 (when (re-search-forward "^-*$" nil nil)
540 (delete-region last-point (point))
542 (setq last-point (point))
543 (while (< (point)(mark t))
544 (unless (looking-at mu-cite-cited-prefix-regexp)
547 (goto-char last-point))
548 (run-hooks 'mu-cite-post-cite-hook)))
551 ;;; @ message editing utilities
554 (defcustom citation-mark-chars ">}|"
555 "String of characters for citation delimiter."
559 (defcustom citation-disable-chars "<{"
560 "String of characters not allowed as citation-prefix."
564 (defun detect-paragraph-cited-prefix ()
566 (goto-char (point-min))
570 (progn (beginning-of-line)(point))
571 (progn (end-of-line)(point))))
573 (while (and (= (forward-line) 0)
574 (setq str (buffer-substring
575 (progn (beginning-of-line)(point))
576 (progn (end-of-line)(point))))
577 (setq ret (string-compare-from-top prefix str)))
583 (cond ((> i 1) prefix)
585 (goto-char (point-min))
587 (narrow-to-region (point)
588 (+ (point)(length prefix)))
589 (goto-char (point-max))
590 (if (re-search-backward
591 (concat "[" citation-mark-chars "]") nil t)
593 (goto-char (match-end 0))
594 (when (looking-at "[ \t]+")
595 (goto-char (match-end 0)))
596 (buffer-substring (point-min)(point)))
599 (goto-char (point-max))
601 (concat "[" citation-disable-chars "]") nil t)
603 (concat "[" citation-mark-chars "]") nil t))
604 (goto-char (match-end 0))
605 (when (looking-at "[ \t]+")
606 (goto-char (match-end 0)))
607 (buffer-substring (point-min)(point)))
610 (defun fill-cited-region (beg end)
615 (and (search-backward "\n" nil t)
616 (setq end (match-end 0)))
617 (narrow-to-region beg end)
618 (let* ((fill-prefix (detect-paragraph-cited-prefix))
619 (pat (concat fill-prefix "\n")))
620 (goto-char (point-min))
621 (while (search-forward pat nil t)
622 (let ((b (match-beginning 0))
625 (when (and (> b (point-min))
626 (let ((cat (char-category
628 (or (string-match "a" cat)
629 (string-match "l" cat))))
631 (goto-char (point-min))
632 (fill-region (point-min) (point-max))))))
634 (defun compress-cited-prefix ()
637 (goto-char (point-min))
639 (concat "^" (regexp-quote mail-header-separator) "$") nil t)
640 (while (re-search-forward
641 (concat "^\\([ \t]*[^ \t\n" citation-mark-chars "]*["
642 citation-mark-chars "]\\)+") nil t)
643 (let* ((b (match-beginning 0))
645 (prefix (buffer-substring b e))
648 (when (string-match "<[^<>]+>" prefix)
649 (setq prefix (substring prefix 0 (match-beginning 0))))
651 (concat "\\([" citation-mark-chars "]+\\)[ \t]*")
653 (setq i (+ i (- (match-end 1)(match-beginning 1)))
655 pe (match-beginning 1)
658 (when (and ps (< ps pe))
660 (insert (concat (substring prefix ps pe) (make-string nest ?>))))
663 (defun replace-top-string (old new)
664 (interactive "*sOld string: \nsNew string: ")
665 (while (re-search-forward
666 (concat "^" (regexp-quote old)) nil t)
667 (replace-match new)))
669 (defun string-compare-from-top (str1 str2)
670 (let* ((len1 (length str1))
672 (len (min len1 len2))
675 (while (and (< p len)
677 (setq c1 (aref str1 p)
680 (setq p (+ p (char-length c1))))
682 (let ((matched (substring str1 0 p))
683 (r1 (and (< p len1)(substring str1 p)))
684 (r2 (and (< p len2)(substring str2 p))))
687 (list 'seq matched (list 'or r1 r2)))))))
693 ;; This part will be abolished in the future.
695 (static-unless (fboundp 'defvaralias)
699 (eval (list 'defvar (car elem) (cadr elem)))))
700 mu-cite-obsolete-variable-alist))
708 (run-hooks 'mu-cite-load-hook)
710 ;;; mu-cite.el ends here