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")
70 ;; This part will be abolished in the near future.
72 (eval-when-compile (require 'static))
74 (defconst mu-cite-obsolete-variable-alist
75 '((mu-cite/default-methods-alist
76 mu-cite-default-methods-alist)
77 (mu-cite/cited-prefix-regexp
78 mu-cite-cited-prefix-regexp)
79 (mu-cite/prefix-format
80 mu-cite-prefix-format)
83 (mu-cite/instantiation-hook
84 mu-cite-instantiation-hook)
85 (mu-cite/pre-cite-hook
86 mu-cite-pre-cite-hook)
87 (mu-cite/post-cite-hook
88 mu-cite-post-cite-hook)
89 (mu-cite/get-field-value-method-alist
90 mu-cite-get-field-value-method-alist)
91 (mu-cite/ml-count-field-list
92 mu-cite-ml-count-field-list)))
94 (static-if (featurep 'xemacs)
95 (dolist (def mu-cite-obsolete-variable-alist)
96 (apply (function define-obsolete-variable-alias) def)))
102 (defgroup mu-cite nil
103 "yet another citation tool for GNU Emacs."
108 (defvar mu-cite-default-methods-alist
112 (mu-cite-get-field-value "From"))))
116 (mu-cite-get-field-value "Date"))))
120 (mu-cite-get-field-value "Message-Id"))))
124 (mu-cite-get-field-value "Subject"))))
128 (mu-cite-get-field-value "X-Ml-Name"))))
129 (cons 'ml-count (function mu-cite-get-ml-count-method))
130 (cons 'address-structure
134 (std11-parse-address-string (mu-cite-get-value 'from))))))
138 (std11-full-name-string
139 (mu-cite-get-value 'address-structure)))))
143 (std11-address-string
144 (mu-cite-get-value 'address-structure)))))
148 (let ((ml-name (mu-cite-get-value 'ml-name)))
153 (mu-cite-get-value 'ml-count)
155 (mu-cite-get-value 'message-id))))))
159 (let ((id (mu-cite-get-value 'id)))
161 (format ">>>>> In %s \n" id)
166 (mu-cite-get-field-value "X-Attribution"))))
168 (cons 'prefix (function mu-cite-get-prefix-method))
169 (cons 'prefix-register
170 (function mu-cite-get-prefix-register-method))
171 (cons 'prefix-register-verbose
172 (function mu-cite-get-prefix-register-verbose-method))
175 (function mu-bbdb-get-prefix-method))
176 (cons 'bbdb-prefix-register
177 (function mu-bbdb-get-prefix-register-method))
178 (cons 'bbdb-prefix-register-verbose
179 (function mu-bbdb-get-prefix-register-verbose-method))
186 (defcustom mu-cite-cited-prefix-regexp
187 "\\(^[^ \t\n<>]+>+[ \t]*\\|^[ \t]*$\\)"
188 "Regexp to match the citation prefix.
189 If match, mu-cite doesn't insert citation prefix."
193 (defcustom mu-cite-prefix-format '(prefix-register-verbose "> ")
194 "List to represent citation prefix.
195 Each elements must be string or method name."
197 (nconc '(choice :tag "String or Method name")
200 (lambda (elem) (list 'choice-item (car elem))))
201 mu-cite-default-methods-alist)
202 '((symbol :tag "Other Method")
204 (choice-item :tag "String: \"> \"" "> ")
205 (string :tag "Other String"))))
208 (defcustom mu-cite-top-format '(in-id ">>>>>\t" from " wrote:\n")
209 "List to represent top string of citation.
210 Each elements must be string or method name."
213 '(choice :tag "String or Method name")
216 (lambda (elem) (list 'choice-item (car elem))))
217 mu-cite-default-methods-alist)
218 '((symbol :tag "Other Method")
220 (choice-item :tag "String: \">>>>>\\t\"" ">>>>>\t")
221 (choice-item :tag "String: \" wrote:\\n\"" " wrote:\n")
222 (string :tag "Other String"))))
229 (defcustom mu-cite-instantiation-hook nil
230 "List of functions called just before narrowing to the message."
234 (defcustom mu-cite-pre-cite-hook nil
235 "List of functions called before citing a region of text."
239 (defcustom mu-cite-post-cite-hook nil
240 "List of functions called after citing a region of text."
248 (defvar mu-cite-get-field-value-method-alist nil
249 "Alist major-mode vs. function to get field-body of header.")
251 (defun mu-cite-get-field-value (name)
252 "Return the value of the header field NAME.
253 If the field is not found in the header, a method function which is
254 registered in variable `mu-cite-get-field-value-method-alist' is called."
255 (or (std11-field-body name)
256 (let ((method (assq major-mode mu-cite-get-field-value-method-alist)))
258 (funcall (cdr method) name)))))
267 (defcustom mu-cite-ml-count-field-list
268 '("X-Ml-Count" "X-Mail-Count" "X-Seqno" "X-Sequence" "Mailinglist-Id")
269 "List of header fields which contain sequence number of mailing list."
270 :type '(repeat (choice (choice-item "X-Ml-Count")
271 (choice-item "X-Mail-Count")
272 (choice-item "X-Seqno")
273 (choice-item "X-Sequence")
274 (choice-item "Mailinglist-Id")
276 (string :tag "Other")))
279 (defun mu-cite-get-ml-count-method ()
280 "A mu-cite method to return a ML-count.
281 This function searches a field about ML-count, which is specified by
282 variable `mu-cite-ml-count-field-list', in a header.
283 If the field is found, the function returns a number part of the
286 Notice that please use (mu-cite-get-value 'ml-count)
287 instead of call the function directly."
288 (let ((field-list mu-cite-ml-count-field-list))
291 (let* ((field (car field-list))
292 (ml-count (mu-cite-get-field-value field)))
293 (if (and ml-count (string-match "[0-9]+" ml-count))
294 (throw 'tag (match-string 0 ml-count)))
295 (setq field-list (cdr field-list)))))))
301 (defvar mu-cite-methods-alist nil)
303 (defun mu-cite-make-methods ()
304 (setq mu-cite-methods-alist
305 (copy-alist mu-cite-default-methods-alist))
306 (run-hooks 'mu-cite-instantiation-hook))
308 (defun mu-cite-get-value (item)
309 "Return current value of ITEM."
310 (let ((ret (cdr (assoc item mu-cite-methods-alist))))
313 (setq ret (save-excursion (funcall ret)))
314 (set-alist 'mu-cite-methods-alist item ret))
317 (defun mu-cite-eval-format (list)
320 (cond ((stringp elt) elt)
321 ((symbolp elt) (mu-cite-get-value elt)))))
329 (defun mu-cite-original ()
330 "Citing filter function.
331 This is callable from the various mail and news readers' reply
332 function according to the agreed upon standard."
334 (mu-cite-make-methods)
336 (if (< (mark t) (point))
337 (exchange-point-and-mark))
338 (narrow-to-region (point)(point-max))
339 (run-hooks 'mu-cite-pre-cite-hook)
340 (let ((last-point (point))
341 (top (mu-cite-eval-format mu-cite-top-format))
342 (prefix (mu-cite-eval-format mu-cite-prefix-format)))
343 (if (re-search-forward "^-*$" nil nil)
346 (delete-region last-point (point))
348 (setq last-point (point))
349 (while (< (point)(mark t))
350 (or (looking-at mu-cite-cited-prefix-regexp)
353 (goto-char last-point))
354 (run-hooks 'mu-cite-post-cite-hook)))
357 ;;; @ message editing utilities
360 (defcustom citation-mark-chars ">}|"
361 "String of characters for citation delimiter."
365 (defcustom citation-disable-chars "<{"
366 "String of characters not allowed as citation-prefix."
370 (defun-maybe-cond char-category (character)
371 "Return string of category mnemonics for CHAR in TABLE.
372 CHAR can be any multilingual character
373 TABLE defaults to the current buffer's category table."
374 ((and (subr-fboundp 'char-category-set)
375 (subr-fboundp 'category-set-mnemonics))
376 (category-set-mnemonics (char-category-set character))
378 ((fboundp 'char-category-list)
379 (mapconcat (lambda (chr)
380 (char-to-string (int-char chr)))
381 (char-category-list character)
385 (if (< (char-int character) 128)
390 (if (< (char-int character) 128)
395 (defun detect-paragraph-cited-prefix ()
397 (goto-char (point-min))
400 (buffer-substring (line-beginning-position)
401 (line-end-position)))
403 (while (and (= (forward-line) 0)
404 (setq str (buffer-substring
405 (progn (beginning-of-line)(point))
406 (progn (end-of-line)(point))))
407 (setq ret (string-compare-from-top prefix str)))
413 (cond ((> i 1) prefix)
415 (goto-char (point-min))
417 (narrow-to-region (point)
418 (+ (point)(length prefix)))
419 (goto-char (point-max))
420 (if (re-search-backward
421 (concat "[" citation-mark-chars "]") nil t)
423 (goto-char (match-end 0))
424 (if (looking-at "[ \t]+")
425 (goto-char (match-end 0)))
426 (buffer-substring (point-min)(point)))
429 (goto-char (point-max))
431 (concat "[" citation-disable-chars "]") nil t)
433 (concat "[" citation-mark-chars "]") nil t))
434 (goto-char (match-end 0))
435 (if (looking-at "[ \t]+")
436 (goto-char (match-end 0)))
437 (buffer-substring (point-min)(point)))
441 (defun fill-cited-region (beg end)
442 "Fill each of the paragraphs in the region as a cited text."
447 (and (search-backward "\n" nil t)
448 (setq end (match-end 0)))
449 (narrow-to-region beg end)
450 (let* ((fill-prefix (detect-paragraph-cited-prefix))
451 (pat (concat fill-prefix "\n")))
452 (goto-char (point-min))
453 (while (search-forward pat nil t)
454 (let ((b (match-beginning 0))
457 (if (and (> b (point-min))
458 (let ((cat (char-category
460 (or (string-match "a" cat)
461 (string-match "l" cat))))
463 (goto-char (point-min))
464 (fill-region (point-min) (point-max))))))
467 (defun compress-cited-prefix ()
468 "Compress nested cited prefixes."
471 (goto-char (point-min))
473 (concat "^" (regexp-quote mail-header-separator) "$") nil t)
474 (while (re-search-forward
475 (concat "^\\([ \t]*[^ \t\n" citation-mark-chars "]*["
476 citation-mark-chars "]\\)+") nil t)
477 (let* ((b (match-beginning 0))
479 (prefix (buffer-substring b e))
482 (if (string-match "<[^<>]+>" prefix)
484 (substring prefix 0 (match-beginning 0))))
486 (concat "\\([" citation-mark-chars "]+\\)[ \t]*")
488 (setq i (+ i (- (match-end 1)(match-beginning 1)))
490 pe (match-beginning 1)
493 (when (and ps (< ps pe))
495 (insert (concat (substring prefix ps pe) (make-string nest ?>))))
498 (defun replace-top-string (old new)
499 (interactive "*sOld string: \nsNew string: ")
500 (while (re-search-forward
501 (concat "^" (regexp-quote old)) nil t)
502 (replace-match new)))
504 (defun string-compare-from-top (str1 str2)
505 (let* ((len1 (length str1))
507 (len (min len1 len2))
510 (while (and (< p len)
512 (setq c1 (sref str1 p)
515 (setq p (char-next-index c1 p)))
517 (let ((matched (substring str1 0 p))
518 (r1 (and (< p len1)(substring str1 p)))
519 (r2 (and (< p len2)(substring str2 p))))
522 (list 'seq matched (list 'or r1 r2)))))))
528 ;; This part will be abolished in the future.
530 (define-obsolete-function-alias
531 'mu-cite/cite-original 'mu-cite-original)
532 (define-obsolete-function-alias
533 'mu-cite/get-field-value 'mu-cite-get-field-value)
534 (define-obsolete-function-alias
535 'mu-cite/get-value 'mu-cite-get-value)
543 (run-hooks 'mu-cite-load-hook)
545 ;; This part will be abolished in the future.
547 (static-unless (featurep 'xemacs)
548 (let ((rest mu-cite-obsolete-variable-alist)
551 (setq def (car rest))
552 (apply (function make-obsolete-variable) def)
553 (setq old-sym (car def)
554 new-sym (car (cdr def)))
555 (or (get new-sym 'saved-value) ; saved?
556 (not (eq (eval (car (get new-sym 'standard-value)))
557 (symbol-value new-sym))) ; set as new name?
558 (and (boundp old-sym) ; old name seems used
559 (or (eq (symbol-value new-sym)
560 (symbol-value old-sym))
561 (set new-sym (symbol-value old-sym)))))
562 (setq rest (cdr rest)))))
564 ;;; mu-cite.el ends here