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 in your ~/.emacs file:
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.1")
71 (defmacro mu-cite-remove-text-properties (string)
72 "Remove text properties from STRING which is read from minibuffer."
73 (if (or (featurep 'xemacs)
74 (boundp 'minibuffer-allow-text-properties);; Emacs 20.1 or later.
75 (not (fboundp 'set-text-properties)));; under Emacs 19.7.
77 (` (let ((obj (copy-sequence (, string))))
78 (set-text-properties 0 (length obj) nil obj)
86 "Yet another citation tool for GNU Emacs."
91 (defvar mu-cite-default-methods-alist
95 (mu-cite-get-field-value "From"))))
99 (mu-cite-get-field-value "Date"))))
103 (mu-cite-get-field-value "Message-Id"))))
107 (mu-cite-get-field-value "Subject"))))
111 (mu-cite-get-field-value "X-Ml-Name"))))
112 (cons 'ml-count (function mu-cite-get-ml-count-method))
113 (cons 'address-structure
117 (std11-parse-address-string (mu-cite-get-value 'from))))))
121 (std11-full-name-string
122 (mu-cite-get-value 'address-structure)))))
126 (std11-address-string
127 (mu-cite-get-value 'address-structure)))))
131 (let ((ml-name (mu-cite-get-value 'ml-name)))
136 (mu-cite-get-value 'ml-count)
138 (mu-cite-get-value 'message-id))))))
142 (let ((id (mu-cite-get-value 'id)))
144 (format ">>>>> In %s \n" id)
149 (mu-cite-get-field-value "X-Attribution"))))
151 (cons 'prefix (function mu-cite-get-prefix-method))
152 (cons 'prefix-register
153 (function mu-cite-get-prefix-register-method))
154 (cons 'prefix-register-verbose
155 (function mu-cite-get-prefix-register-verbose-method))
158 (function mu-bbdb-get-prefix-method))
159 (cons 'bbdb-prefix-register
160 (function mu-bbdb-get-prefix-register-method))
161 (cons 'bbdb-prefix-register-verbose
162 (function mu-bbdb-get-prefix-register-verbose-method))
169 (defcustom mu-cite-cited-prefix-regexp
170 "\\(^[^ \t\n<>]+>+[ \t]*\\|^[ \t]*$\\)"
171 "Regexp to match the citation prefix.
172 If match, mu-cite doesn't insert citation prefix."
176 (defcustom mu-cite-prefix-format '(prefix-register-verbose "> ")
177 "List to represent citation prefix.
178 Each elements must be a string or a method name."
188 :tag "Method or String"
192 (function (lambda (elem) (list 'choice-item (car elem))))
193 mu-cite-default-methods-alist)
194 '((symbol :tag "Method")
196 (choice-item :tag "String: \"> \"" "> ")
198 :set (function (lambda (symbol value)
199 (set-default symbol (delq nil value))))
202 (defcustom mu-cite-top-format '(in-id ">>>>>\t" from " wrote:\n")
203 "List to represent top string of citation.
204 Each elements must be a string or a method name."
213 :tag "Method or String"
217 (function (lambda (elem) (list 'choice-item (car elem))))
218 mu-cite-default-methods-alist)
219 '((symbol :tag "Method")
221 (choice-item :tag "String: \">>>>>\\t\"" ">>>>>\t")
222 (choice-item :tag "String: \" wrote:\\n\"" " wrote:\n")
223 (string :tag "String"))))))))
224 :set (function (lambda (symbol value)
225 (set-default symbol (delq nil value))))
232 (defcustom mu-cite-instantiation-hook nil
233 "List of functions called just before narrowing to the message."
237 (defcustom mu-cite-pre-cite-hook nil
238 "List of functions called before citing a region of text."
242 (defcustom mu-cite-post-cite-hook nil
243 "List of functions called after citing a region of text."
251 (defvar mu-cite-get-field-value-method-alist nil
252 "Alist major-mode vs. function to get field-body of header.")
254 (defun mu-cite-get-field-value (name)
255 "Return the value of the header field NAME.
256 If the field is not found in the header, a method function which is
257 registered in variable `mu-cite-get-field-value-method-alist' is called."
258 (or (std11-field-body name)
259 (let ((method (assq major-mode mu-cite-get-field-value-method-alist)))
261 (funcall (cdr method) name)))))
270 (defcustom mu-cite-ml-count-field-list
271 '("X-Ml-Count" "X-Mail-Count" "X-Seqno" "X-Sequence" "Mailinglist-Id")
272 "List of header fields which contains a sequence number of the mailing list."
273 :type '(repeat (choice :tag "Field Name"
274 (choice-item "X-Ml-Count")
275 (choice-item "X-Mail-Count")
276 (choice-item "X-Seqno")
277 (choice-item "X-Sequence")
278 (choice-item "Mailinglist-Id")
280 (string :tag "Other")))
281 :set (function (lambda (symbol value)
282 (set-default symbol (delq nil value))))
285 (defun mu-cite-get-ml-count-method ()
286 "A mu-cite method to return a ML-count.
287 This function searches a field about ML-count, which is specified by
288 the variable `mu-cite-ml-count-field-list', in a header.
289 If the field is found, the function returns a number part of the
292 Notice that please use (mu-cite-get-value 'ml-count)
293 instead of to call the function directly."
294 (let ((field-list mu-cite-ml-count-field-list))
297 (let* ((field (car field-list))
298 (ml-count (mu-cite-get-field-value field)))
299 (if (and ml-count (string-match "[0-9]+" ml-count))
300 (throw 'tag (match-string 0 ml-count)))
301 (setq field-list (cdr field-list)))))))
307 (defvar mu-cite-methods-alist nil)
309 (defun mu-cite-make-methods ()
310 (setq mu-cite-methods-alist
311 (copy-alist mu-cite-default-methods-alist))
312 (run-hooks 'mu-cite-instantiation-hook))
314 (defun mu-cite-get-value (item)
315 "Return a current value of ITEM."
316 (let ((ret (cdr (assoc item mu-cite-methods-alist))))
319 (setq ret (save-excursion (funcall ret)))
320 (set-alist 'mu-cite-methods-alist item ret))
323 (defun mu-cite-eval-format (list)
326 (cond ((stringp elt) elt)
327 ((symbolp elt) (mu-cite-get-value elt)))))
335 (defun mu-cite-original ()
336 "Citing filter function.
337 This is callable from the various mail and news readers' reply
338 function according to the agreed upon standard."
340 (mu-cite-make-methods)
342 (if (< (mark t) (point))
343 (exchange-point-and-mark))
344 (narrow-to-region (point)(point-max))
345 (run-hooks 'mu-cite-pre-cite-hook)
346 (let ((last-point (point))
347 (top (mu-cite-eval-format mu-cite-top-format))
348 (prefix (mu-cite-eval-format mu-cite-prefix-format)))
349 (if (re-search-forward "^-*$" nil nil)
352 (delete-region last-point (point))
354 (setq last-point (point))
355 (while (< (point)(mark t))
356 (or (looking-at mu-cite-cited-prefix-regexp)
359 (goto-char last-point))
360 (run-hooks 'mu-cite-post-cite-hook)))
363 ;;; @ message editing utilities
366 (defcustom citation-mark-chars ">}|"
367 "String of characters for citation delimiter."
371 (defcustom citation-disable-chars "<{"
372 "String of characters not allowed as citation-prefix."
376 (defun-maybe-cond char-category (character)
377 "Return a string of category mnemonics for CHAR in TABLE.
378 CHAR can be any multilingual character,
379 TABLE defaults to the current buffer's category table."
380 ((and (subr-fboundp 'char-category-set)
381 (subr-fboundp 'category-set-mnemonics))
382 (category-set-mnemonics (char-category-set character))
384 ((fboundp 'char-category-list)
385 (mapconcat (lambda (chr)
386 (char-to-string (int-char chr)))
387 (char-category-list character)
391 (if (< (char-int character) 128)
396 (if (< (char-int character) 128)
401 (defun detect-paragraph-cited-prefix ()
403 (goto-char (point-min))
406 (buffer-substring (line-beginning-position)
407 (line-end-position))))
410 (while (and (= (forward-line) 0)
411 (setq str (buffer-substring
412 (progn (beginning-of-line)(point))
413 (progn (end-of-line)(point))))
414 (setq ret (string-compare-from-top prefix str)))
419 (or (string-equal init prefix)
421 (cond ((> i 1) prefix)
423 (goto-char (point-min))
425 (narrow-to-region (point)
426 (+ (point)(length prefix)))
427 (goto-char (point-max))
428 (if (re-search-backward
429 (concat "[" citation-mark-chars "]") nil t)
431 (goto-char (match-end 0))
432 (if (looking-at "[ \t]+")
433 (goto-char (match-end 0)))
434 (buffer-substring (point-min)(point)))
437 (goto-char (point-max))
439 (concat "[" citation-disable-chars "]") nil t)
441 (concat "[" citation-mark-chars "]") nil t))
442 (goto-char (match-end 0))
443 (if (looking-at "[ \t]+")
444 (goto-char (match-end 0)))
445 (buffer-substring (line-beginning-position)(point)))
449 (defun fill-cited-region (beg end)
450 "Fill each of the paragraphs in the region as a cited text."
455 (and (search-backward "\n" nil t)
456 (setq end (match-end 0)))
457 (narrow-to-region beg end)
458 (let* ((fill-prefix (detect-paragraph-cited-prefix))
459 (fill-column (max (+ 1 (current-left-margin)
460 (string-width fill-prefix))
461 (current-fill-column)))
462 (pat (concat fill-prefix "\n"))
464 (goto-char (point-min))
465 (while (search-forward pat nil t)
466 (let ((b (match-beginning 0))
469 (if (and (> b (point-min))
470 (let ((cat (char-category
472 (or (string-match "a" cat)
473 (string-match "l" cat))))
475 (goto-char (point-min))
476 (fill-region (point-min) (point-max))))))
479 (defun compress-cited-prefix ()
480 "Compress nested cited prefixes."
483 (goto-char (point-min))
485 (concat "^" (regexp-quote mail-header-separator) "$") nil t)
486 (while (re-search-forward
487 (concat "^\\([ \t]*[^ \t\n" citation-mark-chars "]*["
488 citation-mark-chars "]\\)+") nil t)
489 (let* ((b (match-beginning 0))
491 (prefix (buffer-substring b e))
494 (if (string-match "<[^<>]+>" prefix)
496 (substring prefix 0 (match-beginning 0))))
498 (concat "\\([" citation-mark-chars "]+\\)[ \t]*")
500 (setq i (+ i (- (match-end 1)(match-beginning 1)))
502 pe (match-beginning 1)
505 (if (and ps (< ps pe))
508 (insert (concat (substring prefix ps pe)
509 (make-string nest ?>)))))
512 (defun replace-top-string (old new)
513 (interactive "*sOld string: \nsNew string: ")
514 (while (re-search-forward
515 (concat "^" (regexp-quote old)) nil t)
516 (replace-match new)))
518 (defun string-compare-from-top (str1 str2)
519 (let* ((len1 (length str1))
521 (len (min len1 len2))
524 (while (and (< p len)
526 (setq c1 (sref str1 p)
529 (setq p (char-next-index c1 p)))
531 (let ((matched (substring str1 0 p))
532 (r1 (and (< p len1)(substring str1 p)))
533 (r2 (and (< p len2)(substring str2 p))))
536 (list 'seq matched (list 'or r1 r2)))))))
544 (run-hooks 'mu-cite-load-hook)
546 ;;; mu-cite.el ends here