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.
76 (defmacro 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 (define-obsolete-function-alias
93 (function mu-cite/cite-original) (function mu-cite-original))
94 (define-obsolete-function-alias
95 (function mu-cite/get-field-value) (function mu-cite-get-field-value))
96 (define-obsolete-function-alias
97 (function mu-cite/get-value) (function mu-cite-get-value))
103 (defmacro mu-cite-remove-text-properties (string)
104 "Remove text properties from STRING which is read from minibuffer."
105 (if (or (featurep 'xemacs)
106 (boundp 'minibuffer-allow-text-properties);; Emacs 20.1 or later.
107 (not (fboundp 'set-text-properties)));; under Emacs 19.7.
109 (` (let ((obj (copy-sequence (, string))))
110 (set-text-properties 0 (length obj) nil obj)
117 (defgroup mu-cite nil
118 "yet another citation tool for GNU Emacs."
123 (defvar mu-cite-default-methods-alist
127 (mu-cite-get-field-value "From"))))
131 (mu-cite-get-field-value "Date"))))
135 (mu-cite-get-field-value "Message-Id"))))
139 (mu-cite-get-field-value "Subject"))))
143 (mu-cite-get-field-value "X-Ml-Name"))))
144 (cons 'ml-count (function mu-cite-get-ml-count-method))
145 (cons 'address-structure
149 (std11-parse-address-string (mu-cite-get-value 'from))))))
153 (std11-full-name-string
154 (mu-cite-get-value 'address-structure)))))
158 (std11-address-string
159 (mu-cite-get-value 'address-structure)))))
163 (let ((ml-name (mu-cite-get-value 'ml-name)))
168 (mu-cite-get-value 'ml-count)
170 (mu-cite-get-value 'message-id))))))
174 (let ((id (mu-cite-get-value 'id)))
176 (format ">>>>> In %s \n" id)
181 (mu-cite-get-field-value "X-Attribution"))))
183 (cons 'prefix (function mu-cite-get-prefix-method))
184 (cons 'prefix-register
185 (function mu-cite-get-prefix-register-method))
186 (cons 'prefix-register-verbose
187 (function mu-cite-get-prefix-register-verbose-method))
190 (function mu-bbdb-get-prefix-method))
191 (cons 'bbdb-prefix-register
192 (function mu-bbdb-get-prefix-register-method))
193 (cons 'bbdb-prefix-register-verbose
194 (function mu-bbdb-get-prefix-register-verbose-method))
201 (defcustom mu-cite-cited-prefix-regexp
202 "\\(^[^ \t\n<>]+>+[ \t]*\\|^[ \t]*$\\)"
203 "Regexp to match the citation prefix.
204 If match, mu-cite doesn't insert citation prefix."
208 (defcustom mu-cite-prefix-format '(prefix-register-verbose "> ")
209 "List to represent citation prefix.
210 Each elements must be string or method name."
220 :tag "Method or String"
224 (function (lambda (elem) (list 'choice-item (car elem))))
225 mu-cite-default-methods-alist)
226 '((symbol :tag "Method")
228 (choice-item :tag "String: \"> \"" "> ")
230 :set (function (lambda (symbol value)
231 (set-default symbol (delq nil value))))
234 (defcustom mu-cite-top-format '(in-id ">>>>>\t" from " wrote:\n")
235 "List to represent top string of citation.
236 Each elements must be string or method name."
245 :tag "Method or String"
249 (function (lambda (elem) (list 'choice-item (car elem))))
250 mu-cite-default-methods-alist)
251 '((symbol :tag "Method")
253 (choice-item :tag "String: \">>>>>\\t\"" ">>>>>\t")
254 (choice-item :tag "String: \" wrote:\\n\"" " wrote:\n")
255 (string :tag "String"))))))))
256 :set (function (lambda (symbol value)
257 (set-default symbol (delq nil value))))
264 (defcustom mu-cite-instantiation-hook nil
265 "List of functions called just before narrowing to the message."
269 (defcustom mu-cite-pre-cite-hook nil
270 "List of functions called before citing a region of text."
274 (defcustom mu-cite-post-cite-hook nil
275 "List of functions called after citing a region of text."
283 (defvar mu-cite-get-field-value-method-alist nil
284 "Alist major-mode vs. function to get field-body of header.")
286 (defun mu-cite-get-field-value (name)
287 "Return the value of the header field NAME.
288 If the field is not found in the header, a method function which is
289 registered in variable `mu-cite-get-field-value-method-alist' is called."
290 (or (std11-field-body name)
291 (let ((method (assq major-mode mu-cite-get-field-value-method-alist)))
293 (funcall (cdr method) name)))))
302 (defcustom mu-cite-ml-count-field-list
303 '("X-Ml-Count" "X-Mail-Count" "X-Seqno" "X-Sequence" "Mailinglist-Id")
304 "List of header fields which contain sequence number of mailing list."
305 :type '(repeat (choice :tag "Field Name"
306 (choice-item "X-Ml-Count")
307 (choice-item "X-Mail-Count")
308 (choice-item "X-Seqno")
309 (choice-item "X-Sequence")
310 (choice-item "Mailinglist-Id")
312 (string :tag "Other")))
313 :set (function (lambda (symbol value)
314 (set-default symbol (delq nil value))))
317 (defun mu-cite-get-ml-count-method ()
318 "A mu-cite method to return a ML-count.
319 This function searches a field about ML-count, which is specified by
320 variable `mu-cite-ml-count-field-list', in a header.
321 If the field is found, the function returns a number part of the
324 Notice that please use (mu-cite-get-value 'ml-count)
325 instead of call the function directly."
326 (let ((field-list mu-cite-ml-count-field-list))
329 (let* ((field (car field-list))
330 (ml-count (mu-cite-get-field-value field)))
331 (if (and ml-count (string-match "[0-9]+" ml-count))
332 (throw 'tag (match-string 0 ml-count)))
333 (setq field-list (cdr field-list)))))))
339 (defvar mu-cite-methods-alist nil)
341 (defun mu-cite-make-methods ()
342 (setq mu-cite-methods-alist
343 (copy-alist mu-cite-default-methods-alist))
344 (run-hooks 'mu-cite-instantiation-hook))
346 (defun mu-cite-get-value (item)
347 "Return current value of ITEM."
348 (let ((ret (cdr (assoc item mu-cite-methods-alist))))
351 (setq ret (save-excursion (funcall ret)))
352 (set-alist 'mu-cite-methods-alist item ret))
355 (defun mu-cite-eval-format (list)
358 (cond ((stringp elt) elt)
359 ((symbolp elt) (mu-cite-get-value elt)))))
367 (defun mu-cite-original ()
368 "Citing filter function.
369 This is callable from the various mail and news readers' reply
370 function according to the agreed upon standard."
372 (mu-cite-make-methods)
374 (if (< (mark t) (point))
375 (exchange-point-and-mark))
376 (narrow-to-region (point)(point-max))
377 (run-hooks 'mu-cite-pre-cite-hook)
378 (let ((last-point (point))
379 (top (mu-cite-eval-format mu-cite-top-format))
380 (prefix (mu-cite-eval-format mu-cite-prefix-format)))
381 (if (re-search-forward "^-*$" nil nil)
384 (delete-region last-point (point))
386 (setq last-point (point))
387 (while (< (point)(mark t))
388 (or (looking-at mu-cite-cited-prefix-regexp)
391 (goto-char last-point))
392 (run-hooks 'mu-cite-post-cite-hook)))
395 ;;; @ message editing utilities
398 (defcustom citation-mark-chars ">}|"
399 "String of characters for citation delimiter."
403 (defcustom citation-disable-chars "<{"
404 "String of characters not allowed as citation-prefix."
408 (defun-maybe-cond char-category (character)
409 "Return string of category mnemonics for CHAR in TABLE.
410 CHAR can be any multilingual character
411 TABLE defaults to the current buffer's category table."
412 ((and (subr-fboundp 'char-category-set)
413 (subr-fboundp 'category-set-mnemonics))
414 (category-set-mnemonics (char-category-set character))
416 ((fboundp 'char-category-list)
417 (mapconcat (lambda (chr)
418 (char-to-string (int-char chr)))
419 (char-category-list character)
423 (if (< (char-int character) 128)
428 (if (< (char-int character) 128)
433 (defun detect-paragraph-cited-prefix ()
435 (goto-char (point-min))
438 (buffer-substring (line-beginning-position)
439 (line-end-position)))
441 (while (and (= (forward-line) 0)
442 (setq str (buffer-substring
443 (progn (beginning-of-line)(point))
444 (progn (end-of-line)(point))))
445 (setq ret (string-compare-from-top prefix str)))
451 (cond ((> i 1) prefix)
453 (goto-char (point-min))
455 (narrow-to-region (point)
456 (+ (point)(length prefix)))
457 (goto-char (point-max))
458 (if (re-search-backward
459 (concat "[" citation-mark-chars "]") nil t)
461 (goto-char (match-end 0))
462 (if (looking-at "[ \t]+")
463 (goto-char (match-end 0)))
464 (buffer-substring (point-min)(point)))
467 (goto-char (point-max))
469 (concat "[" citation-disable-chars "]") nil t)
471 (concat "[" citation-mark-chars "]") nil t))
472 (goto-char (match-end 0))
473 (if (looking-at "[ \t]+")
474 (goto-char (match-end 0)))
475 (buffer-substring (point-min)(point)))
479 (defun fill-cited-region (beg end)
480 "Fill each of the paragraphs in the region as a cited text."
485 (and (search-backward "\n" nil t)
486 (setq end (match-end 0)))
487 (narrow-to-region beg end)
488 (let* ((fill-prefix (detect-paragraph-cited-prefix))
489 (pat (concat fill-prefix "\n")))
490 (goto-char (point-min))
491 (while (search-forward pat nil t)
492 (let ((b (match-beginning 0))
495 (if (and (> b (point-min))
496 (let ((cat (char-category
498 (or (string-match "a" cat)
499 (string-match "l" cat))))
501 (goto-char (point-min))
502 (fill-region (point-min) (point-max))))))
505 (defun compress-cited-prefix ()
506 "Compress nested cited prefixes."
509 (goto-char (point-min))
511 (concat "^" (regexp-quote mail-header-separator) "$") nil t)
512 (while (re-search-forward
513 (concat "^\\([ \t]*[^ \t\n" citation-mark-chars "]*["
514 citation-mark-chars "]\\)+") nil t)
515 (let* ((b (match-beginning 0))
517 (prefix (buffer-substring b e))
520 (if (string-match "<[^<>]+>" prefix)
522 (substring prefix 0 (match-beginning 0))))
524 (concat "\\([" citation-mark-chars "]+\\)[ \t]*")
526 (setq i (+ i (- (match-end 1)(match-beginning 1)))
528 pe (match-beginning 1)
531 (if (and ps (< ps pe))
534 (insert (concat (substring prefix ps pe)
535 (make-string nest ?>)))))
538 (defun replace-top-string (old new)
539 (interactive "*sOld string: \nsNew string: ")
540 (while (re-search-forward
541 (concat "^" (regexp-quote old)) nil t)
542 (replace-match new)))
544 (defun string-compare-from-top (str1 str2)
545 (let* ((len1 (length str1))
547 (len (min len1 len2))
550 (while (and (< p len)
552 (setq c1 (sref str1 p)
555 (setq p (char-next-index c1 p)))
557 (let ((matched (substring str1 0 p))
558 (r1 (and (< p len1)(substring str1 p)))
559 (r2 (and (< p len2)(substring str2 p))))
562 (list 'seq matched (list 'or r1 r2)))))))
570 (run-hooks 'mu-cite-load-hook)
572 ;; This part will be abolished in the future.
574 (static-unless (featurep 'xemacs)
575 (let ((rest (mu-cite-obsolete-variable-alist))
578 (setq def (car rest))
579 (apply (function make-obsolete-variable) def)
580 (setq old-sym (car def)
581 new-sym (car (cdr def)))
582 (or (get new-sym 'saved-value) ; saved?
583 (not (eq (eval (car (get new-sym 'standard-value)))
584 (symbol-value new-sym))) ; set as new name?
585 (and (boundp old-sym) ; old name seems used
586 (or (eq (symbol-value new-sym)
587 (symbol-value old-sym))
588 (set new-sym (symbol-value old-sym)))))
589 (setq rest (cdr rest)))))
591 ;;; mu-cite.el ends here