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.
52 (autoload 'mu-cite-get-prefix-method "mu-register")
53 (autoload 'mu-cite-get-prefix-register-method "mu-register")
54 (autoload 'mu-cite-get-prefix-register-verbose-method "mu-register")
56 (autoload 'mu-bbdb-get-prefix-method "mu-bbdb")
57 (autoload 'mu-bbdb-get-prefix-register-method "mu-bbdb")
58 (autoload 'mu-bbdb-get-prefix-register-verbose-method "mu-bbdb")
64 (defconst mu-cite-version "8.0")
71 "yet another citation tool for GNU Emacs."
76 (defvar mu-cite-default-methods-alist
80 (mu-cite-get-field-value "From"))))
84 (mu-cite-get-field-value "Date"))))
88 (mu-cite-get-field-value "Message-Id"))))
92 (mu-cite-get-field-value "Subject"))))
96 (mu-cite-get-field-value "X-Ml-Name"))))
97 (cons 'ml-count (function mu-cite-get-ml-count-method))
98 (cons 'address-structure
102 (std11-parse-address-string (mu-cite-get-value 'from))))))
106 (std11-full-name-string
107 (mu-cite-get-value 'address-structure)))))
111 (std11-address-string
112 (mu-cite-get-value 'address-structure)))))
116 (let ((ml-name (mu-cite-get-value 'ml-name)))
121 (mu-cite-get-value 'ml-count)
123 (mu-cite-get-value 'message-id))))))
127 (let ((id (mu-cite-get-value 'id)))
129 (format ">>>>> In %s \n" id)
134 (mu-cite-get-field-value "X-Attribution"))))
136 (cons 'prefix (function mu-cite-get-prefix-method))
137 (cons 'prefix-register
138 (function mu-cite-get-prefix-register-method))
139 (cons 'prefix-register-verbose
140 (function mu-cite-get-prefix-register-verbose-method))
143 (function mu-bbdb-get-prefix-method))
144 (cons 'bbdb-prefix-register
145 (function mu-bbdb-get-prefix-register-method))
146 (cons 'bbdb-prefix-register-verbose
147 (function mu-bbdb-get-prefix-register-verbose-method))
154 (defcustom mu-cite-cited-prefix-regexp
155 "\\(^[^ \t\n<>]+>+[ \t]*\\|^[ \t]*$\\)"
156 "Regexp to match the citation prefix.
157 If match, mu-cite doesn't insert citation prefix."
161 (defcustom mu-cite-prefix-format '(prefix-register-verbose "> ")
162 "List to represent citation prefix.
163 Each elements must be string or method name."
165 (nconc '(choice :tag "String or Method name")
168 (lambda (elem) (list 'choice-item (car elem))))
169 mu-cite-default-methods-alist)
170 '((symbol :tag "Other Method")
172 (choice-item :tag "String: \"> \"" "> ")
173 (string :tag "Other String"))))
176 (defcustom mu-cite-top-format '(in-id ">>>>>\t" from " wrote:\n")
177 "List to represent top string of citation.
178 Each elements must be string or method name."
181 '(choice :tag "String or Method name")
184 (lambda (elem) (list 'choice-item (car elem))))
185 mu-cite-default-methods-alist)
186 '((symbol :tag "Other Method")
188 (choice-item :tag "String: \">>>>>\\t\"" ">>>>>\t")
189 (choice-item :tag "String: \" wrote:\\n\"" " wrote:\n")
190 (string :tag "Other String"))))
197 (defcustom mu-cite-load-hook nil
198 "List of functions called after mu-cite is loaded.
199 Use this hook to add your own methods to `mu-cite-default-methods-alist'."
203 (defcustom mu-cite-instantiation-hook nil
204 "List of functions called just before narrowing to the message."
208 (defcustom mu-cite-pre-cite-hook nil
209 "List of functions called before citing a region of text."
213 (defcustom mu-cite-post-cite-hook nil
214 "List of functions called after citing a region of text."
222 (defvar mu-cite-get-field-value-method-alist nil
223 "Alist major-mode vs. function to get field-body of header.")
225 (defun mu-cite-get-field-value (name)
226 "Return the value of the header field NAME.
227 If the field is not found in the header, a method function which is
228 registered in variable `mu-cite-get-field-value-method-alist' is called."
229 (or (std11-field-body name)
230 (let ((method (assq major-mode mu-cite-get-field-value-method-alist)))
232 (funcall (cdr method) name)))))
241 (defcustom mu-cite-ml-count-field-list
242 '("X-Ml-Count" "X-Mail-Count" "X-Seqno" "X-Sequence" "Mailinglist-Id")
243 "List of header fields which contain sequence number of mailing list."
244 :type '(repeat (choice (choice-item "X-Ml-Count")
245 (choice-item "X-Mail-Count")
246 (choice-item "X-Seqno")
247 (choice-item "X-Sequence")
248 (choice-item "Mailinglist-Id")
250 (string :tag "Other")))
253 (defun mu-cite-get-ml-count-method ()
254 "A mu-cite method to return a ML-count.
255 This function searches a field about ML-count, which is specified by
256 variable `mu-cite-ml-count-field-list', in a header.
257 If the field is found, the function returns a number part of the
260 Notice that please use (mu-cite-get-value 'ml-count)
261 instead of call the function directly."
262 (let ((field-list mu-cite-ml-count-field-list))
265 (let* ((field (car field-list))
266 (ml-count (mu-cite-get-field-value field)))
267 (if (and ml-count (string-match "[0-9]+" ml-count))
268 (throw 'tag (match-string 0 ml-count)))
269 (setq field-list (cdr field-list)))))))
275 (defvar mu-cite-methods-alist nil)
277 (defun mu-cite-make-methods ()
278 (setq mu-cite-methods-alist
279 (copy-alist mu-cite-default-methods-alist))
280 (run-hooks 'mu-cite-instantiation-hook))
282 (defun mu-cite-get-value (item)
283 "Return current value of ITEM."
284 (let ((ret (cdr (assoc item mu-cite-methods-alist))))
287 (setq ret (save-excursion (funcall ret)))
288 (set-alist 'mu-cite-methods-alist item ret))
291 (defun mu-cite-eval-format (list)
294 (cond ((stringp elt) elt)
295 ((symbolp elt) (mu-cite-get-value elt)))))
303 (defun mu-cite-original ()
304 "Citing filter function.
305 This is callable from the various mail and news readers' reply
306 function according to the agreed upon standard."
308 (mu-cite-make-methods)
310 (if (< (mark t) (point))
311 (exchange-point-and-mark))
312 (narrow-to-region (point)(point-max))
313 (run-hooks 'mu-cite-pre-cite-hook)
314 (let ((last-point (point))
315 (top (mu-cite-eval-format mu-cite-top-format))
316 (prefix (mu-cite-eval-format mu-cite-prefix-format)))
317 (if (re-search-forward "^-*$" nil nil)
320 (delete-region last-point (point))
322 (setq last-point (point))
323 (while (< (point)(mark t))
324 (or (looking-at mu-cite-cited-prefix-regexp)
327 (goto-char last-point))
328 (run-hooks 'mu-cite-post-cite-hook)))
331 ;;; @ message editing utilities
334 (defcustom citation-mark-chars ">}|"
335 "String of characters for citation delimiter."
339 (defcustom citation-disable-chars "<{"
340 "String of characters not allowed as citation-prefix."
344 (defun-maybe-cond char-category (character)
345 "Return string of category mnemonics for CHAR in TABLE.
346 CHAR can be any multilingual character
347 TABLE defaults to the current buffer's category table."
348 ((and (subr-fboundp 'char-category-set)
349 (subr-fboundp 'category-set-mnemonics))
350 (category-set-mnemonics (char-category-set character))
352 ((fboundp 'char-category-list)
353 (mapconcat (lambda (chr)
354 (char-to-string (int-char chr)))
355 (char-category-list character)
359 (if (< (char-int character) 128)
364 (if (< (char-int character) 128)
369 (defun detect-paragraph-cited-prefix ()
371 (goto-char (point-min))
374 (buffer-substring (line-beginning-position)
375 (line-end-position)))
377 (while (and (= (forward-line) 0)
378 (setq str (buffer-substring
379 (progn (beginning-of-line)(point))
380 (progn (end-of-line)(point))))
381 (setq ret (string-compare-from-top prefix str)))
387 (cond ((> i 1) prefix)
389 (goto-char (point-min))
391 (narrow-to-region (point)
392 (+ (point)(length prefix)))
393 (goto-char (point-max))
394 (if (re-search-backward
395 (concat "[" citation-mark-chars "]") nil t)
397 (goto-char (match-end 0))
398 (if (looking-at "[ \t]+")
399 (goto-char (match-end 0)))
400 (buffer-substring (point-min)(point)))
403 (goto-char (point-max))
405 (concat "[" citation-disable-chars "]") nil t)
407 (concat "[" citation-mark-chars "]") nil t))
408 (goto-char (match-end 0))
409 (if (looking-at "[ \t]+")
410 (goto-char (match-end 0)))
411 (buffer-substring (point-min)(point)))
415 (defun fill-cited-region (beg end)
416 "Fill each of the paragraphs in the region as a cited text."
421 (and (search-backward "\n" nil t)
422 (setq end (match-end 0)))
423 (narrow-to-region beg end)
424 (let* ((fill-prefix (detect-paragraph-cited-prefix))
425 (pat (concat fill-prefix "\n")))
426 (goto-char (point-min))
427 (while (search-forward pat nil t)
428 (let ((b (match-beginning 0))
431 (if (and (> b (point-min))
432 (let ((cat (char-category
434 (or (string-match "a" cat)
435 (string-match "l" cat))))
437 (goto-char (point-min))
438 (fill-region (point-min) (point-max))))))
441 (defun compress-cited-prefix ()
442 "Compress nested cited prefixes."
445 (goto-char (point-min))
447 (concat "^" (regexp-quote mail-header-separator) "$") nil t)
448 (while (re-search-forward
449 (concat "^\\([ \t]*[^ \t\n" citation-mark-chars "]*["
450 citation-mark-chars "]\\)+") nil t)
451 (let* ((b (match-beginning 0))
453 (prefix (buffer-substring b e))
456 (if (string-match "<[^<>]+>" prefix)
458 (substring prefix 0 (match-beginning 0))))
460 (concat "\\([" citation-mark-chars "]+\\)[ \t]*")
462 (setq i (+ i (- (match-end 1)(match-beginning 1)))
464 pe (match-beginning 1)
467 (when (and ps (< ps pe))
469 (insert (concat (substring prefix ps pe) (make-string nest ?>))))
472 (defun replace-top-string (old new)
473 (interactive "*sOld string: \nsNew string: ")
474 (while (re-search-forward
475 (concat "^" (regexp-quote old)) nil t)
476 (replace-match new)))
478 (defun string-compare-from-top (str1 str2)
479 (let* ((len1 (length str1))
481 (len (min len1 len2))
484 (while (and (< p len)
486 (setq c1 (sref str1 p)
489 (setq p (char-next-index c1 p)))
491 (let ((matched (substring str1 0 p))
492 (r1 (and (< p len1)(substring str1 p)))
493 (r2 (and (< p len2)(substring str2 p))))
496 (list 'seq matched (list 'or r1 r2)))))))
502 ;; This part will be abolished in the future.
504 (define-obsolete-function-alias
505 'mu-cite/cite-original 'mu-cite-original)
506 (define-obsolete-function-alias
507 'mu-cite/get-field-value 'mu-cite-get-field-value)
508 (define-obsolete-function-alias
509 'mu-cite/get-value 'mu-cite-get-value)
511 (eval-when-compile (require 'static))
513 (static-when (featurep 'xemacs)
514 (define-obsolete-variable-alias
515 'mu-cite/default-methods-alist
516 'mu-cite-default-methods-alist)
517 (define-obsolete-variable-alias
518 'mu-cite/cited-prefix-regexp
519 'mu-cite-cited-prefix-regexp)
520 (define-obsolete-variable-alias
521 'mu-cite/prefix-format
522 'mu-cite-prefix-format)
523 (define-obsolete-variable-alias
526 (define-obsolete-variable-alias
527 'mu-cite/instantiation-hook
528 'mu-cite-instantiation-hook)
529 (define-obsolete-variable-alias
530 'mu-cite/pre-cite-hook
531 'mu-cite-pre-cite-hook)
532 (define-obsolete-variable-alias
533 'mu-cite/post-cite-hook
534 'mu-cite-post-cite-hook)
535 (define-obsolete-variable-alias
536 'mu-cite/get-field-value-method-alist
537 'mu-cite-get-field-value-method-alist)
538 (define-obsolete-variable-alias
539 'mu-cite/ml-count-field-list
540 'mu-cite-ml-count-field-list)
549 (run-hooks 'mu-cite-load-hook)
551 ;;; mu-cite.el ends here