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.
53 (autoload 'mu-cite-get-prefix-method "mu-register")
54 (autoload 'mu-cite-get-prefix-register-method "mu-register")
55 (autoload 'mu-cite-get-prefix-register-verbose-method "mu-register")
57 (autoload 'mu-bbdb-get-prefix-method "mu-bbdb")
58 (autoload 'mu-bbdb-get-prefix-register-method "mu-bbdb")
59 (autoload 'mu-bbdb-get-prefix-register-verbose-method "mu-bbdb")
65 (defconst mu-cite-version "8.0")
71 ;; This part will be abolished in the future.
75 (defmacro mu-cite-obsolete-variable-alist ()
76 ''((mu-cite/cited-prefix-regexp mu-cite-cited-prefix-regexp)
77 (mu-cite/default-methods-alist mu-cite-default-methods-alist)
78 (mu-cite/get-field-value-method-alist
79 mu-cite-get-field-value-method-alist)
80 (mu-cite/instantiation-hook mu-cite-instantiation-hook)
81 (mu-cite/ml-count-field-list mu-cite-ml-count-field-list)
82 (mu-cite/post-cite-hook mu-cite-post-cite-hook)
83 (mu-cite/pre-cite-hook mu-cite-pre-cite-hook)
84 (mu-cite/prefix-format mu-cite-prefix-format)
85 (mu-cite/top-format mu-cite-top-format))))
87 (static-if (featurep 'xemacs)
88 (dolist (def (mu-cite-obsolete-variable-alist))
89 (apply (function define-obsolete-variable-alias) def)))
91 (define-obsolete-function-alias
92 (function mu-cite/cite-original) (function mu-cite-original))
93 (define-obsolete-function-alias
94 (function mu-cite/get-field-value) (function mu-cite-get-field-value))
95 (define-obsolete-function-alias
96 (function mu-cite/get-value) (function mu-cite-get-value))
102 (defmacro mu-cite-remove-text-properties (string)
103 "Remove text properties from STRING which is read from minibuffer."
104 (if (or (featurep 'xemacs)
105 (boundp 'minibuffer-allow-text-properties);; Emacs 20.1 or later.
106 (not (fboundp 'set-text-properties)));; under Emacs 19.7.
108 (` (let ((obj (copy-sequence (, string))))
109 (set-text-properties 0 (length obj) nil obj)
116 (defgroup mu-cite nil
117 "yet another citation tool for GNU Emacs."
122 (defvar mu-cite-default-methods-alist
126 (mu-cite-get-field-value "From"))))
130 (mu-cite-get-field-value "Date"))))
134 (mu-cite-get-field-value "Message-Id"))))
138 (mu-cite-get-field-value "Subject"))))
142 (mu-cite-get-field-value "X-Ml-Name"))))
143 (cons 'ml-count (function mu-cite-get-ml-count-method))
144 (cons 'address-structure
148 (std11-parse-address-string (mu-cite-get-value 'from))))))
152 (std11-full-name-string
153 (mu-cite-get-value 'address-structure)))))
157 (std11-address-string
158 (mu-cite-get-value 'address-structure)))))
162 (let ((ml-name (mu-cite-get-value 'ml-name)))
167 (mu-cite-get-value 'ml-count)
169 (mu-cite-get-value 'message-id))))))
173 (let ((id (mu-cite-get-value 'id)))
175 (format ">>>>> In %s \n" id)
180 (mu-cite-get-field-value "X-Attribution"))))
182 (cons 'prefix (function mu-cite-get-prefix-method))
183 (cons 'prefix-register
184 (function mu-cite-get-prefix-register-method))
185 (cons 'prefix-register-verbose
186 (function mu-cite-get-prefix-register-verbose-method))
189 (function mu-bbdb-get-prefix-method))
190 (cons 'bbdb-prefix-register
191 (function mu-bbdb-get-prefix-register-method))
192 (cons 'bbdb-prefix-register-verbose
193 (function mu-bbdb-get-prefix-register-verbose-method))
200 (defcustom mu-cite-cited-prefix-regexp
201 "\\(^[^ \t\n<>]+>+[ \t]*\\|^[ \t]*$\\)"
202 "Regexp to match the citation prefix.
203 If match, mu-cite doesn't insert citation prefix."
207 (defcustom mu-cite-prefix-format '(prefix-register-verbose "> ")
208 "List to represent citation prefix.
209 Each elements must be string or method name."
219 :tag "Method or String"
223 (function (lambda (elem) (list 'choice-item (car elem))))
224 mu-cite-default-methods-alist)
225 '((symbol :tag "Method")
227 (choice-item :tag "String: \"> \"" "> ")
229 :set (function (lambda (symbol value)
230 (set-default symbol (delq nil value))))
233 (defcustom mu-cite-top-format '(in-id ">>>>>\t" from " wrote:\n")
234 "List to represent top string of citation.
235 Each elements must be string or method name."
244 :tag "Method or String"
248 (function (lambda (elem) (list 'choice-item (car elem))))
249 mu-cite-default-methods-alist)
250 '((symbol :tag "Method")
252 (choice-item :tag "String: \">>>>>\\t\"" ">>>>>\t")
253 (choice-item :tag "String: \" wrote:\\n\"" " wrote:\n")
254 (string :tag "String"))))))))
255 :set (function (lambda (symbol value)
256 (set-default symbol (delq nil value))))
263 (defcustom mu-cite-instantiation-hook nil
264 "List of functions called just before narrowing to the message."
268 (defcustom mu-cite-pre-cite-hook nil
269 "List of functions called before citing a region of text."
273 (defcustom mu-cite-post-cite-hook nil
274 "List of functions called after citing a region of text."
282 (defvar mu-cite-get-field-value-method-alist nil
283 "Alist major-mode vs. function to get field-body of header.")
285 (defun mu-cite-get-field-value (name)
286 "Return the value of the header field NAME.
287 If the field is not found in the header, a method function which is
288 registered in variable `mu-cite-get-field-value-method-alist' is called."
289 (or (std11-field-body name)
290 (let ((method (assq major-mode mu-cite-get-field-value-method-alist)))
292 (funcall (cdr method) name)))))
301 (defcustom mu-cite-ml-count-field-list
302 '("X-Ml-Count" "X-Mail-Count" "X-Seqno" "X-Sequence" "Mailinglist-Id")
303 "List of header fields which contain sequence number of mailing list."
304 :type '(repeat (choice :tag "Field Name"
305 (choice-item "X-Ml-Count")
306 (choice-item "X-Mail-Count")
307 (choice-item "X-Seqno")
308 (choice-item "X-Sequence")
309 (choice-item "Mailinglist-Id")
311 (string :tag "Other")))
312 :set (function (lambda (symbol value)
313 (set-default symbol (delq nil value))))
316 (defun mu-cite-get-ml-count-method ()
317 "A mu-cite method to return a ML-count.
318 This function searches a field about ML-count, which is specified by
319 variable `mu-cite-ml-count-field-list', in a header.
320 If the field is found, the function returns a number part of the
323 Notice that please use (mu-cite-get-value 'ml-count)
324 instead of call the function directly."
325 (let ((field-list mu-cite-ml-count-field-list))
328 (let* ((field (car field-list))
329 (ml-count (mu-cite-get-field-value field)))
330 (if (and ml-count (string-match "[0-9]+" ml-count))
331 (throw 'tag (match-string 0 ml-count)))
332 (setq field-list (cdr field-list)))))))
338 (defvar mu-cite-methods-alist nil)
340 (defun mu-cite-make-methods ()
341 (setq mu-cite-methods-alist
342 (copy-alist mu-cite-default-methods-alist))
343 (run-hooks 'mu-cite-instantiation-hook))
345 (defun mu-cite-get-value (item)
346 "Return current value of ITEM."
347 (let ((ret (cdr (assoc item mu-cite-methods-alist))))
350 (setq ret (save-excursion (funcall ret)))
351 (set-alist 'mu-cite-methods-alist item ret))
354 (defun mu-cite-eval-format (list)
357 (cond ((stringp elt) elt)
358 ((symbolp elt) (mu-cite-get-value elt)))))
366 (defun mu-cite-original ()
367 "Citing filter function.
368 This is callable from the various mail and news readers' reply
369 function according to the agreed upon standard."
371 (mu-cite-make-methods)
373 (if (< (mark t) (point))
374 (exchange-point-and-mark))
375 (narrow-to-region (point)(point-max))
376 (run-hooks 'mu-cite-pre-cite-hook)
377 (let ((last-point (point))
378 (top (mu-cite-eval-format mu-cite-top-format))
379 (prefix (mu-cite-eval-format mu-cite-prefix-format)))
380 (if (re-search-forward "^-*$" nil nil)
383 (delete-region last-point (point))
385 (setq last-point (point))
386 (while (< (point)(mark t))
387 (or (looking-at mu-cite-cited-prefix-regexp)
390 (goto-char last-point))
391 (run-hooks 'mu-cite-post-cite-hook)))
394 ;;; @ message editing utilities
397 (defcustom citation-mark-chars ">}|"
398 "String of characters for citation delimiter."
402 (defcustom citation-disable-chars "<{"
403 "String of characters not allowed as citation-prefix."
407 (defun-maybe-cond char-category (character)
408 "Return string of category mnemonics for CHAR in TABLE.
409 CHAR can be any multilingual character
410 TABLE defaults to the current buffer's category table."
411 ((and (subr-fboundp 'char-category-set)
412 (subr-fboundp 'category-set-mnemonics))
413 (category-set-mnemonics (char-category-set character))
415 ((fboundp 'char-category-list)
416 (mapconcat (lambda (chr)
417 (char-to-string (int-char chr)))
418 (char-category-list character)
422 (if (< (char-int character) 128)
427 (if (< (char-int character) 128)
432 (defun detect-paragraph-cited-prefix ()
434 (goto-char (point-min))
437 (buffer-substring (line-beginning-position)
438 (line-end-position)))
440 (while (and (= (forward-line) 0)
441 (setq str (buffer-substring
442 (progn (beginning-of-line)(point))
443 (progn (end-of-line)(point))))
444 (setq ret (string-compare-from-top prefix str)))
450 (cond ((> i 1) prefix)
452 (goto-char (point-min))
454 (narrow-to-region (point)
455 (+ (point)(length prefix)))
456 (goto-char (point-max))
457 (if (re-search-backward
458 (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)))
466 (goto-char (point-max))
468 (concat "[" citation-disable-chars "]") nil t)
470 (concat "[" citation-mark-chars "]") nil t))
471 (goto-char (match-end 0))
472 (if (looking-at "[ \t]+")
473 (goto-char (match-end 0)))
474 (buffer-substring (point-min)(point)))
478 (defun fill-cited-region (beg end)
479 "Fill each of the paragraphs in the region as a cited text."
484 (and (search-backward "\n" nil t)
485 (setq end (match-end 0)))
486 (narrow-to-region beg end)
487 (let* ((fill-prefix (detect-paragraph-cited-prefix))
488 (pat (concat fill-prefix "\n")))
489 (goto-char (point-min))
490 (while (search-forward pat nil t)
491 (let ((b (match-beginning 0))
494 (if (and (> b (point-min))
495 (let ((cat (char-category
497 (or (string-match "a" cat)
498 (string-match "l" cat))))
500 (goto-char (point-min))
501 (fill-region (point-min) (point-max))))))
504 (defun compress-cited-prefix ()
505 "Compress nested cited prefixes."
508 (goto-char (point-min))
510 (concat "^" (regexp-quote mail-header-separator) "$") nil t)
511 (while (re-search-forward
512 (concat "^\\([ \t]*[^ \t\n" citation-mark-chars "]*["
513 citation-mark-chars "]\\)+") nil t)
514 (let* ((b (match-beginning 0))
516 (prefix (buffer-substring b e))
519 (if (string-match "<[^<>]+>" prefix)
521 (substring prefix 0 (match-beginning 0))))
523 (concat "\\([" citation-mark-chars "]+\\)[ \t]*")
525 (setq i (+ i (- (match-end 1)(match-beginning 1)))
527 pe (match-beginning 1)
530 (if (and ps (< ps pe))
533 (insert (concat (substring prefix ps pe)
534 (make-string nest ?>)))))
537 (defun replace-top-string (old new)
538 (interactive "*sOld string: \nsNew string: ")
539 (while (re-search-forward
540 (concat "^" (regexp-quote old)) nil t)
541 (replace-match new)))
543 (defun string-compare-from-top (str1 str2)
544 (let* ((len1 (length str1))
546 (len (min len1 len2))
549 (while (and (< p len)
551 (setq c1 (sref str1 p)
554 (setq p (char-next-index c1 p)))
556 (let ((matched (substring str1 0 p))
557 (r1 (and (< p len1)(substring str1 p)))
558 (r2 (and (< p len2)(substring str2 p))))
561 (list 'seq matched (list 'or r1 r2)))))))
569 (run-hooks 'mu-cite-load-hook)
571 ;; This part will be abolished in the future.
573 (static-unless (featurep 'xemacs)
574 (let ((rest (mu-cite-obsolete-variable-alist))
577 (setq def (car rest))
578 (apply (function make-obsolete-variable) def)
579 (setq old-sym (car def)
580 new-sym (car (cdr def)))
581 (or (get new-sym 'saved-value) ; saved?
582 (not (eq (eval (car (get new-sym 'standard-value)))
583 (symbol-value new-sym))) ; set as new name?
584 (and (boundp old-sym) ; old name seems used
585 (or (eq (symbol-value new-sym)
586 (symbol-value old-sym))
587 (set new-sym (symbol-value old-sym)))))
588 (setq rest (cdr rest)))))
590 ;;; mu-cite.el ends here