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 (boundp 'mu-cite/registration-file)
82 (format "Obsolete variable `%s' found, use anyway? "
83 'mu-cite/registration-file))
84 mu-cite/registration-file
85 mu-cite-registration-file)
86 (message "You should use `%s' instead of `%s'."
87 'mu-cite-registration-file 'mu-cite/registration-file)
89 mu-cite-registration-file)))
92 (defconst mu-cite-obsolete-variable-alist
93 '((mu-cite/allow-null-string-registration
94 mu-cite-allow-null-string-registration)
95 (mu-cite/citation-name-alist mu-cite-citation-name-alist)
96 (mu-cite/cited-prefix-regexp mu-cite-cited-prefix-regexp)
97 (mu-cite/default-methods-alist mu-cite-default-methods-alist)
98 (mu-cite/instantiation-hook mu-cite-instantiation-hook)
99 (mu-cite/minibuffer-history mu-cite-minibuffer-history)
100 (mu-cite/ml-count-field-list mu-cite-ml-count-field-list)
101 (mu-cite/post-cite-hook mu-cite-post-cite-hook)
102 (mu-cite/pre-cite-hook mu-cite-pre-cite-hook)
103 (mu-cite/prefix-format mu-cite-prefix-format)
104 (mu-cite/registration-file mu-cite-registration-file)
105 (mu-cite/top-format mu-cite-top-format)))
110 (apply (function make-obsolete-variable) elem)
111 (when (boundp (car elem))
112 (apply (function message)
113 "WARNING: `%s' is an obsolete variable, use `%s' instead."
115 (static-if (fboundp 'defvaralias) ; It may exists in XEmacs.
116 (apply (function defvaralias) elem)
117 (when (boundp (car elem))
118 (eval (list 'defvar (cadr elem) (car elem)))))))
119 mu-cite-obsolete-variable-alist)
124 (defconst mu-cite-obsolete-function-alist
125 '((mu-cite/add-citation-name mu-cite-add-citation-name)
126 (mu-cite/cite-original mu-cite-original)
127 (mu-cite/eval-format mu-cite-eval-format)
128 (mu-cite/get-citation-name mu-cite-get-citation-name)
129 (mu-cite/get-field-value mu-cite-get-field-value)
130 (mu-cite/get-ml-count-method mu-cite-get-ml-count-method)
131 (mu-cite/get-prefix-method mu-cite-get-prefix-method)
132 (mu-cite/get-prefix-register-method
133 mu-cite-get-prefix-register-method)
134 (mu-cite/get-prefix-register-verbose-method
135 mu-cite-get-prefix-register-verbose-method)
136 (mu-cite/get-value mu-cite-get-value)
137 (mu-cite/load-registration-file mu-cite-load-registration-file)
138 (mu-cite/make-methods mu-cite-make-methods)
139 (mu-cite/save-registration-file mu-cite-save-registration-file)))
142 (function (lambda (elem)
143 (apply (function define-obsolete-function-alias) elem)))
144 mu-cite-obsolete-function-alist)
151 (defgroup mu-cite nil
152 "yet another citation tool for GNU Emacs."
157 (defvar mu-cite-default-methods-alist
161 (mu-cite-get-field-value "From"))))
165 (mu-cite-get-field-value "Date"))))
169 (mu-cite-get-field-value "Message-Id"))))
173 (mu-cite-get-field-value "Subject"))))
177 (mu-cite-get-field-value "X-Ml-Name"))))
178 (cons 'ml-count (function mu-cite-get-ml-count-method))
179 (cons 'address-structure
183 (std11-parse-address-string (mu-cite-get-value 'from))))))
187 (std11-full-name-string
188 (mu-cite-get-value 'address-structure)))))
192 (std11-address-string
193 (mu-cite-get-value 'address-structure)))))
197 (let ((ml-name (mu-cite-get-value 'ml-name)))
202 (mu-cite-get-value 'ml-count)
204 (mu-cite-get-value 'message-id))))))
208 (let ((id (mu-cite-get-value 'id)))
210 (format ">>>>> In %s \n" id)
212 (cons 'prefix (function mu-cite-get-prefix-method))
213 (cons 'prefix-register
214 (function mu-cite-get-prefix-register-method))
215 (cons 'prefix-register-verbose
216 (function mu-cite-get-prefix-register-verbose-method))
220 (mu-cite-get-field-value "X-Attribution"))))
227 (defcustom mu-cite-cited-prefix-regexp
228 "\\(^[^ \t\n<>]+>+[ \t]*\\|^[ \t]*$\\)"
229 "Regexp to match the citation prefix.
230 If match, mu-cite doesn't insert citation prefix."
234 (defcustom mu-cite-prefix-format '(prefix-register-verbose "> ")
235 "List to represent citation prefix.
236 Each elements must be string or method name."
238 (nconc '(choice :tag "String or Method name")
241 (lambda (elem) (list 'choice-item (car elem))))
242 mu-cite-default-methods-alist)
243 '((symbol :tag "Other Method")
245 (choice-item :tag "String: \"> \"" "> ")
246 (string :tag "Other String"))))
249 (defcustom mu-cite-top-format '(in-id ">>>>>\t" from " wrote:\n")
250 "List to represent top string of citation.
251 Each elements must be string or method name."
254 '(choice :tag "String or Method name")
257 (lambda (elem) (list 'choice-item (car elem))))
258 mu-cite-default-methods-alist)
259 '((symbol :tag "Other Method")
261 (choice-item :tag "String: \">>>>>\\t\"" ">>>>>\t")
262 (choice-item :tag "String: \" wrote:\\n\"" " wrote:\n")
263 (string :tag "Other String"))))
270 (defcustom mu-cite-load-hook nil
271 "List of functions called after mu-cite is loaded.
272 Use this hook to add your own methods to `mu-cite-default-methods-alist'."
276 (defcustom mu-cite-instantiation-hook nil
277 "List of functions called just before narrowing to the message."
281 (defcustom mu-cite-pre-cite-hook nil
282 "List of functions called before citing a region of text."
286 (defcustom mu-cite-post-cite-hook nil
287 "List of functions called after citing a region of text."
295 (defvar mu-cite-get-field-value-method-alist nil
296 "Alist major-mode vs. function to get field-body of header.")
298 (defun mu-cite-get-field-value (name)
299 (or (std11-field-body name)
300 (let ((method (assq major-mode mu-cite-get-field-value-method-alist)))
302 (funcall (cdr method) name)))))
305 ;;; @ prefix registration
308 (defcustom mu-cite-registration-file (expand-file-name "~/.mu-cite.el")
309 "The name of the user environment file for mu-cite."
313 (defcustom mu-cite-allow-null-string-registration nil
314 "If non-nil, null-string citation-name is registered."
318 (defcustom mu-cite-registration-file-coding-system-for-read nil
319 "Coding system for reading registration file."
322 (defcustom mu-cite-registration-file-coding-system-for-write nil
323 "Coding system for writing registration file."
326 (defcustom mu-cite-registration-file-modes 384
327 "Mode bits of `mu-cite-registration-file', as an integer."
331 (defvar mu-cite-registration-symbol 'mu-cite-citation-name-alist)
333 (defvar mu-cite-citation-name-alist nil)
334 (unless (eq 'mu-cite-citation-name-alist mu-cite-registration-symbol)
335 (setq mu-cite-citation-name-alist
336 (symbol-value mu-cite-registration-symbol)))
337 (defvar mu-cite-minibuffer-history nil)
339 ;; get citation-name from the database
340 (defun mu-cite-get-citation-name (from)
341 (cdr (assoc from mu-cite-citation-name-alist)))
343 ;; register citation-name to the database
344 (defun mu-cite-add-citation-name (name from)
345 (setq mu-cite-citation-name-alist
346 (put-alist from name mu-cite-citation-name-alist))
347 (mu-cite-save-registration-file))
349 ;; load/save registration file
350 ;;(defun mu-cite-load-registration-file ()
351 ;; (let ((file mu-cite-registration-file))
352 ;; (when (file-readable-p file)
353 ;; (let ((alist (with-temp-buffer
355 ;; (` (let ((, mu-cite-registration-symbol))
356 ;; (insert-file-contents-as-coding-system
357 ;; mu-cite-registration-file-coding-system-for-read
359 ;; (condition-case nil
361 ;; (eval-current-buffer)
362 ;; (, mu-cite-registration-symbol))
363 ;; (error nil))))))))
365 ;; (setq mu-cite-citation-name-alist alist))))))
366 (defun mu-cite-load-registration-file ()
367 (let ((file (mu-cite-registration-file)))
368 (when (file-readable-p file)
369 (let ((alist (with-temp-buffer
371 (` (let ((, mu-cite-registration-symbol)
372 mu-cite/citation-name-alist)
373 (insert-file-contents-as-coding-system
374 mu-cite-registration-file-coding-system-for-read
378 (eval-current-buffer)
379 (or mu-cite/citation-name-alist
380 (, mu-cite-registration-symbol)))
383 (setq mu-cite-citation-name-alist alist))))))
384 (add-hook 'mu-cite-load-hook (function mu-cite-load-registration-file))
386 (defun mu-cite-save-registration-file ()
387 ;;(let ((file mu-cite-registration-file))
388 (let ((file (mu-cite-registration-file)))
390 (setq buffer-file-name file)
391 (insert ";;; " (file-name-nondirectory file) "\n")
392 (insert ";;; This file is generated automatically by mu-cite "
393 mu-cite-version "\n\n")
395 (symbol-name mu-cite-registration-symbol)
398 (function prin1-to-string)
399 mu-cite-citation-name-alist "\n "))
402 (file-name-nondirectory file)
404 (write-region 1 1 file nil 'nomsg)
406 (set-file-modes file mu-cite-registration-file-modes)
408 (save-buffer-as-coding-system
409 mu-cite-registration-file-coding-system-for-write))))
418 (defcustom mu-cite-ml-count-field-list
419 '("X-Ml-Count" "X-Mail-Count" "X-Seqno" "X-Sequence" "Mailinglist-Id")
420 "List of header fields which contain sequence number of mailing list."
421 :type '(repeat (choice (choice-item "X-Ml-Count")
422 (choice-item "X-Mail-Count")
423 (choice-item "X-Seqno")
424 (choice-item "X-Sequence")
425 (choice-item "Mailinglist-Id")
427 (string :tag "Other")))
430 (defun mu-cite-get-ml-count-method ()
431 (let ((field-list mu-cite-ml-count-field-list))
434 (let* ((field (car field-list))
435 (ml-count (mu-cite-get-field-value field)))
436 (when (and ml-count (string-match "[0-9]+" ml-count))
439 (match-beginning 0)(match-end 0))))
440 (setq field-list (cdr field-list)))))))
443 ;;; @@ prefix and registration
446 (defun mu-cite-get-prefix-method ()
447 (or (mu-cite-get-citation-name (mu-cite-get-value 'address))
450 (defun mu-cite-get-prefix-register-method ()
451 (let ((addr (mu-cite-get-value 'address)))
452 (or (mu-cite-get-citation-name addr)
454 (read-string "Citation name? "
455 (or (mu-cite-get-value 'x-attribution)
456 (mu-cite-get-value 'full-name))
457 'mu-cite-minibuffer-history)))
458 (when (and (or mu-cite-allow-null-string-registration
459 (not (string-equal return "")))
460 (y-or-n-p (format "Register \"%s\"? " return)))
461 (mu-cite-add-citation-name return addr))
464 (defun mu-cite-get-prefix-register-verbose-method ()
465 (let* ((addr (mu-cite-get-value 'address))
466 (return1 (mu-cite-get-citation-name addr))
467 (return (read-string "Citation name? "
469 (mu-cite-get-value 'x-attribution)
470 (mu-cite-get-value 'full-name))
471 'mu-cite-minibuffer-history)))
472 (when (and (or mu-cite-allow-null-string-registration
473 (not (string-equal return "")))
474 (not (string-equal return return1))
475 (y-or-n-p (format "Register \"%s\"? " return)))
476 (mu-cite-add-citation-name return addr))
483 (defvar mu-cite-methods-alist nil)
485 (defun mu-cite-make-methods ()
486 (setq mu-cite-methods-alist
487 (copy-alist mu-cite-default-methods-alist))
488 (run-hooks 'mu-cite-instantiation-hook))
490 (defun mu-cite-get-value (item)
491 (let ((ret (cdr (assoc item mu-cite-methods-alist))))
494 (setq ret (save-excursion (funcall ret)))
495 (set-alist 'mu-cite-methods-alist item ret))
498 (defun mu-cite-eval-format (list)
501 (cond ((stringp elt) elt)
502 ((symbolp elt) (mu-cite-get-value elt)))))
510 (defun mu-cite-original ()
511 "Citing filter function.
512 This is callable from the various mail and news readers' reply
513 function according to the agreed upon standard."
515 (mu-cite-make-methods)
517 (when (< (mark t) (point))
518 (exchange-point-and-mark))
519 (narrow-to-region (point)(point-max))
520 (run-hooks 'mu-cite-pre-cite-hook)
521 (let ((last-point (point))
522 (top (mu-cite-eval-format mu-cite-top-format))
523 (prefix (mu-cite-eval-format mu-cite-prefix-format)))
524 (when (re-search-forward "^-*$" nil nil)
527 (delete-region last-point (point))
529 (setq last-point (point))
530 (while (< (point)(mark t))
531 (unless (looking-at mu-cite-cited-prefix-regexp)
534 (goto-char last-point))
535 (run-hooks 'mu-cite-post-cite-hook)))
538 ;;; @ message editing utilities
541 (defcustom citation-mark-chars ">}|"
542 "String of characters for citation delimiter."
546 (defcustom citation-disable-chars "<{"
547 "String of characters not allowed as citation-prefix."
551 (defun detect-paragraph-cited-prefix ()
553 (goto-char (point-min))
557 (progn (beginning-of-line)(point))
558 (progn (end-of-line)(point))))
560 (while (and (= (forward-line) 0)
561 (setq str (buffer-substring
562 (progn (beginning-of-line)(point))
563 (progn (end-of-line)(point))))
564 (setq ret (string-compare-from-top prefix str)))
570 (cond ((> i 1) prefix)
572 (goto-char (point-min))
574 (narrow-to-region (point)
575 (+ (point)(length prefix)))
576 (goto-char (point-max))
577 (if (re-search-backward
578 (concat "[" citation-mark-chars "]") nil t)
580 (goto-char (match-end 0))
581 (when (looking-at "[ \t]+")
582 (goto-char (match-end 0)))
583 (buffer-substring (point-min)(point)))
586 (goto-char (point-max))
588 (concat "[" citation-disable-chars "]") nil t)
590 (concat "[" citation-mark-chars "]") nil t))
591 (goto-char (match-end 0))
592 (when (looking-at "[ \t]+")
593 (goto-char (match-end 0)))
594 (buffer-substring (point-min)(point)))
597 (defun fill-cited-region (beg end)
602 (and (search-backward "\n" nil t)
603 (setq end (match-end 0)))
604 (narrow-to-region beg end)
605 (let* ((fill-prefix (detect-paragraph-cited-prefix))
606 (pat (concat fill-prefix "\n")))
607 (goto-char (point-min))
608 (while (search-forward pat nil t)
609 (let ((b (match-beginning 0))
612 (when (and (> b (point-min))
613 (let ((cat (char-category
615 (or (string-match "a" cat)
616 (string-match "l" cat))))
618 (goto-char (point-min))
619 (fill-region (point-min) (point-max))))))
621 (defun compress-cited-prefix ()
624 (goto-char (point-min))
626 (concat "^" (regexp-quote mail-header-separator) "$") nil t)
627 (while (re-search-forward
628 (concat "^\\([ \t]*[^ \t\n" citation-mark-chars "]*["
629 citation-mark-chars "]\\)+") nil t)
630 (let* ((b (match-beginning 0))
632 (prefix (buffer-substring b e))
635 (when (string-match "<[^<>]+>" prefix)
636 (setq prefix (substring prefix 0 (match-beginning 0))))
638 (concat "\\([" citation-mark-chars "]+\\)[ \t]*")
640 (setq i (+ i (- (match-end 1)(match-beginning 1)))
642 pe (match-beginning 1)
645 (when (and ps (< ps pe))
647 (insert (concat (substring prefix ps pe) (make-string nest ?>))))
650 (defun replace-top-string (old new)
651 (interactive "*sOld string: \nsNew string: ")
652 (while (re-search-forward
653 (concat "^" (regexp-quote old)) nil t)
654 (replace-match new)))
656 (defun string-compare-from-top (str1 str2)
657 (let* ((len1 (length str1))
659 (len (min len1 len2))
662 (while (and (< p len)
664 (setq c1 (aref str1 p)
667 (setq p (+ p (char-length c1))))
669 (let ((matched (substring str1 0 p))
670 (r1 (and (< p len1)(substring str1 p)))
671 (r2 (and (< p len2)(substring str2 p))))
674 (list 'seq matched (list 'or r1 r2)))))))
680 ;; This part will be abolished in the future.
682 (static-unless (fboundp 'defvaralias)
686 (eval (list 'defvar (car elem) (cadr elem)))))
687 mu-cite-obsolete-variable-alist))
695 (run-hooks 'mu-cite-load-hook)
697 ;;; mu-cite.el ends here