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 ;; Shuhei KOBAYASHI <shuhei@aqua.ocn.ne.jp>
7 ;; Maintainer: Katsumi Yamaoka <yamaoka@jpl.org>
8 ;; Keywords: mail, news, citation
10 ;; This file is part of MU (Message Utilities).
12 ;; This program is free software; you can redistribute it and/or
13 ;; modify it under the terms of the GNU General Public License as
14 ;; published by the Free Software Foundation; either version 2, or (at
15 ;; your option) any later version.
17 ;; This program is distributed in the hope that it will be useful, but
18 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 ;; General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
30 ;; 1. bytecompile this file and copy it to the apropriate directory.
31 ;; 2. put the following lines to your ~/.emacs:
32 ;; for EMACS 19 or later and XEmacs
33 ;; (autoload 'mu-cite-original "mu-cite" nil t)
34 ;; ;; for all but message-mode
35 ;; (add-hook 'mail-citation-hook (function mu-cite-original))
36 ;; ;; for message-mode only
37 ;; (setq message-cite-function (function mu-cite-original))
39 ;; ;; for all but mh-e
40 ;; (add-hook 'mail-yank-hooks (function mu-cite-original))
42 ;; (add-hook 'mh-yank-hooks (function mu-cite-original))
46 ;; Pickup some macros, e.g. `with-temp-buffer', for old Emacsen.
54 (autoload 'mu-cite-get-prefix-method "mu-register")
55 (autoload 'mu-cite-get-prefix-register-method "mu-register")
56 (autoload 'mu-cite-get-prefix-register-verbose-method "mu-register")
58 (autoload 'mu-bbdb-get-prefix-method "mu-bbdb")
59 (autoload 'mu-bbdb-get-prefix-register-method "mu-bbdb")
60 (autoload 'mu-bbdb-get-prefix-register-verbose-method "mu-bbdb")
66 (defconst mu-cite-version "8.0")
72 ;; This part will be abolished in the future.
74 (eval-when-compile (require 'static))
76 (defconst mu-cite-obsolete-variable-alist
77 '((mu-cite/cited-prefix-regexp mu-cite-cited-prefix-regexp)
78 (mu-cite/default-methods-alist mu-cite-default-methods-alist)
79 (mu-cite/get-field-value-method-alist
80 mu-cite-get-field-value-method-alist)
81 (mu-cite/instantiation-hook mu-cite-instantiation-hook)
82 (mu-cite/ml-count-field-list mu-cite-ml-count-field-list)
83 (mu-cite/post-cite-hook mu-cite-post-cite-hook)
84 (mu-cite/pre-cite-hook mu-cite-pre-cite-hook)
85 (mu-cite/prefix-format mu-cite-prefix-format)
86 (mu-cite/top-format mu-cite-top-format)))
88 (static-if (featurep 'xemacs)
89 (dolist (def mu-cite-obsolete-variable-alist)
90 (apply (function define-obsolete-variable-alias) def)))
92 (dolist (def '((mu-cite/cite-original mu-cite-original)
93 (mu-cite/get-field-value mu-cite-get-field-value)
94 (mu-cite/get-value mu-cite-get-value)))
95 (apply (function define-obsolete-function-alias) def))
101 (defgroup mu-cite nil
102 "yet another citation tool for GNU Emacs."
107 (defvar mu-cite-default-methods-alist
111 (mu-cite-get-field-value "From"))))
115 (mu-cite-get-field-value "Date"))))
119 (mu-cite-get-field-value "Message-Id"))))
123 (mu-cite-get-field-value "Subject"))))
127 (mu-cite-get-field-value "X-Ml-Name"))))
128 (cons 'ml-count (function mu-cite-get-ml-count-method))
129 (cons 'address-structure
133 (std11-parse-address-string (mu-cite-get-value 'from))))))
137 (std11-full-name-string
138 (mu-cite-get-value 'address-structure)))))
142 (std11-address-string
143 (mu-cite-get-value 'address-structure)))))
147 (let ((ml-name (mu-cite-get-value 'ml-name)))
152 (mu-cite-get-value 'ml-count)
154 (mu-cite-get-value 'message-id))))))
158 (let ((id (mu-cite-get-value 'id)))
160 (format ">>>>> In %s \n" id)
165 (mu-cite-get-field-value "X-Attribution"))))
167 (cons 'prefix (function mu-cite-get-prefix-method))
168 (cons 'prefix-register
169 (function mu-cite-get-prefix-register-method))
170 (cons 'prefix-register-verbose
171 (function mu-cite-get-prefix-register-verbose-method))
174 (function mu-bbdb-get-prefix-method))
175 (cons 'bbdb-prefix-register
176 (function mu-bbdb-get-prefix-register-method))
177 (cons 'bbdb-prefix-register-verbose
178 (function mu-bbdb-get-prefix-register-verbose-method))
181 (defun mu-cite-method-list ()
182 (mapcar (function car) mu-cite-default-methods-alist))
188 (defvar widget-mu-cite-method-prompt-value-history nil
189 "History of input to `widget-mu-cite-method-prompt-value'.")
191 (define-widget 'mu-cite-method 'symbol
195 :prompt-history 'widget-mu-cite-method-prompt-value-history
196 :prompt-value 'widget-mu-cite-method-prompt-value
197 :action 'widget-mu-cite-method-action)
199 (defun widget-mu-cite-method-prompt-value (widget prompt value unbound)
200 ;; Read mu-cite-method from minibuffer.
202 (completing-read (format "%s (default %s) " prompt value)
205 (list (symbol-name sym))
207 (mu-cite-method-list)))))
209 (defun widget-mu-cite-method-action (widget &optional event)
210 ;; Read a mu-cite-method from the minibuffer.
212 (widget-mu-cite-method-prompt-value
214 (widget-apply widget :menu-tag-get)
215 (widget-value widget)
217 (widget-value-set widget answer)
218 (widget-apply widget :notify widget event)
221 (defcustom mu-cite-cited-prefix-regexp
222 "\\(^[^ \t\n<>]+>+[ \t]*\\|^[ \t]*$\\)"
223 "Regexp to match the citation prefix.
224 If match, mu-cite doesn't insert citation prefix."
228 (defcustom mu-cite-prefix-format '(prefix-register-verbose "> ")
229 "List to represent citation prefix.
230 Each elements must be string or method name."
232 (choice :tag "String or Method name"
235 (choice-item :tag "String: \"> \"" "> ")
236 (string :tag "Other String")))
239 (defcustom mu-cite-top-format '(in-id ">>>>>\t" from " wrote:\n")
240 "List to represent top string of citation.
241 Each elements must be string or method name."
243 (choice :tag "String or Method name"
246 (choice-item :tag "String: \">>>>>\\t\"" ">>>>>\t")
247 (choice-item :tag "String: \" wrote:\\n\"" " wrote:\n")
248 (string :tag "Other String")))
255 (defcustom mu-cite-instantiation-hook nil
256 "List of functions called just before narrowing to the message."
260 (defcustom mu-cite-pre-cite-hook nil
261 "List of functions called before citing a region of text."
265 (defcustom mu-cite-post-cite-hook nil
266 "List of functions called after citing a region of text."
274 (defvar mu-cite-get-field-value-method-alist nil
275 "Alist major-mode vs. function to get field-body of header.")
277 (defun mu-cite-get-field-value (name)
278 "Return the value of the header field NAME.
279 If the field is not found in the header, a method function which is
280 registered in variable `mu-cite-get-field-value-method-alist' is called."
281 (or (std11-field-body name)
282 (let ((method (assq major-mode mu-cite-get-field-value-method-alist)))
284 (funcall (cdr method) name)))))
293 (defcustom mu-cite-ml-count-field-list
294 '("X-Ml-Count" "X-Mail-Count" "X-Seqno" "X-Sequence" "Mailinglist-Id")
295 "List of header fields which contain sequence number of mailing list."
296 :type '(repeat (choice (choice-item "X-Ml-Count")
297 (choice-item "X-Mail-Count")
298 (choice-item "X-Seqno")
299 (choice-item "X-Sequence")
300 (choice-item "Mailinglist-Id")
302 (string :tag "Other")))
305 (defun mu-cite-get-ml-count-method ()
306 "A mu-cite method to return a ML-count.
307 This function searches a field about ML-count, which is specified by
308 variable `mu-cite-ml-count-field-list', in a header.
309 If the field is found, the function returns a number part of the
312 Notice that please use (mu-cite-get-value 'ml-count)
313 instead of call the function directly."
314 (let ((field-list mu-cite-ml-count-field-list))
317 (let* ((field (car field-list))
318 (ml-count (mu-cite-get-field-value field)))
319 (if (and ml-count (string-match "[0-9]+" ml-count))
320 (throw 'tag (match-string 0 ml-count)))
321 (setq field-list (cdr field-list)))))))
327 (defvar mu-cite-methods-alist nil)
329 (defun mu-cite-make-methods ()
330 (setq mu-cite-methods-alist
331 (copy-alist mu-cite-default-methods-alist))
332 (run-hooks 'mu-cite-instantiation-hook))
334 (defun mu-cite-get-value (item)
335 "Return current value of ITEM."
336 (let ((ret (cdr (assoc item mu-cite-methods-alist))))
339 (setq ret (save-excursion (funcall ret)))
340 (set-alist 'mu-cite-methods-alist item ret))
343 (defun mu-cite-eval-format (list)
346 (cond ((stringp elt) elt)
347 ((symbolp elt) (mu-cite-get-value elt)))))
355 (defun mu-cite-original ()
356 "Citing filter function.
357 This is callable from the various mail and news readers' reply
358 function according to the agreed upon standard."
360 (mu-cite-make-methods)
362 (if (< (mark t) (point))
363 (exchange-point-and-mark))
364 (narrow-to-region (point)(point-max))
365 (run-hooks 'mu-cite-pre-cite-hook)
366 (let ((last-point (point))
367 (top (mu-cite-eval-format mu-cite-top-format))
368 (prefix (mu-cite-eval-format mu-cite-prefix-format)))
369 (if (re-search-forward "^-*$" nil nil)
372 (delete-region last-point (point))
374 (setq last-point (point))
375 (while (< (point)(mark t))
376 (or (looking-at mu-cite-cited-prefix-regexp)
379 (goto-char last-point))
380 (run-hooks 'mu-cite-post-cite-hook)))
383 ;;; @ message editing utilities
386 (defcustom citation-mark-chars ">}|"
387 "String of characters for citation delimiter."
391 (defcustom citation-disable-chars "<{"
392 "String of characters not allowed as citation-prefix."
396 (defun-maybe-cond char-category (character)
397 "Return string of category mnemonics for CHAR in TABLE.
398 CHAR can be any multilingual character
399 TABLE defaults to the current buffer's category table."
400 ((and (subr-fboundp 'char-category-set)
401 (subr-fboundp 'category-set-mnemonics))
402 (category-set-mnemonics (char-category-set character))
404 ((fboundp 'char-category-list)
405 (mapconcat (lambda (chr)
406 (char-to-string (int-char chr)))
407 (char-category-list character)
411 (if (< (char-int character) 128)
416 (if (< (char-int character) 128)
421 (defun detect-paragraph-cited-prefix ()
423 (goto-char (point-min))
426 (buffer-substring (line-beginning-position)
427 (line-end-position)))
429 (while (and (= (forward-line) 0)
430 (setq str (buffer-substring
431 (progn (beginning-of-line)(point))
432 (progn (end-of-line)(point))))
433 (setq ret (string-compare-from-top prefix str)))
439 (cond ((> i 1) prefix)
441 (goto-char (point-min))
443 (narrow-to-region (point)
444 (+ (point)(length prefix)))
445 (goto-char (point-max))
446 (if (re-search-backward
447 (concat "[" citation-mark-chars "]") nil t)
449 (goto-char (match-end 0))
450 (if (looking-at "[ \t]+")
451 (goto-char (match-end 0)))
452 (buffer-substring (point-min)(point)))
455 (goto-char (point-max))
457 (concat "[" citation-disable-chars "]") nil t)
459 (concat "[" citation-mark-chars "]") nil t))
460 (goto-char (match-end 0))
461 (if (looking-at "[ \t]+")
462 (goto-char (match-end 0)))
463 (buffer-substring (point-min)(point)))
467 (defun fill-cited-region (beg end)
468 "Fill each of the paragraphs in the region as a cited text."
473 (and (search-backward "\n" nil t)
474 (setq end (match-end 0)))
475 (narrow-to-region beg end)
476 (let* ((fill-prefix (detect-paragraph-cited-prefix))
477 (pat (concat fill-prefix "\n")))
478 (goto-char (point-min))
479 (while (search-forward pat nil t)
480 (let ((b (match-beginning 0))
483 (if (and (> b (point-min))
484 (let ((cat (char-category
486 (or (string-match "a" cat)
487 (string-match "l" cat))))
489 (goto-char (point-min))
490 (fill-region (point-min) (point-max))))))
493 (defun compress-cited-prefix ()
494 "Compress nested cited prefixes."
497 (goto-char (point-min))
499 (concat "^" (regexp-quote mail-header-separator) "$") nil t)
500 (while (re-search-forward
501 (concat "^\\([ \t]*[^ \t\n" citation-mark-chars "]*["
502 citation-mark-chars "]\\)+") nil t)
503 (let* ((b (match-beginning 0))
505 (prefix (buffer-substring b e))
508 (if (string-match "<[^<>]+>" prefix)
510 (substring prefix 0 (match-beginning 0))))
512 (concat "\\([" citation-mark-chars "]+\\)[ \t]*")
514 (setq i (+ i (- (match-end 1)(match-beginning 1)))
516 pe (match-beginning 1)
519 (when (and ps (< ps pe))
521 (insert (concat (substring prefix ps pe) (make-string nest ?>))))
524 (defun replace-top-string (old new)
525 (interactive "*sOld string: \nsNew string: ")
526 (while (re-search-forward
527 (concat "^" (regexp-quote old)) nil t)
528 (replace-match new)))
530 (defun string-compare-from-top (str1 str2)
531 (let* ((len1 (length str1))
533 (len (min len1 len2))
536 (while (and (< p len)
538 (setq c1 (aref str1 p)
541 (setq p (char-next-index c1 p)))
543 (let ((matched (substring str1 0 p))
544 (r1 (and (< p len1)(substring str1 p)))
545 (r2 (and (< p len2)(substring str2 p))))
548 (list 'seq matched (list 'or r1 r2)))))))
556 (run-hooks 'mu-cite-load-hook)
558 ;; This part will be abolished in the future.
560 (static-unless (featurep 'xemacs)
561 (let ((rest mu-cite-obsolete-variable-alist)
564 (setq def (car rest))
565 (apply (function make-obsolete-variable) def)
566 (setq old-sym (car def)
567 new-sym (car (cdr def)))
568 (or (get new-sym 'saved-value) ; saved?
569 (not (eq (eval (car (get new-sym 'standard-value)))
570 (symbol-value new-sym))) ; set as new name?
571 (and (boundp old-sym) ; old name seems used
572 (or (eq (symbol-value new-sym)
573 (symbol-value old-sym))
574 (set new-sym (symbol-value old-sym)))))
575 (setq rest (cdr rest)))))
577 ;;; mu-cite.el ends here