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-k@jaist.ac.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 (eval-when-compile (require 'cl))
48 ;; Pickup some macros, e.g. `with-temp-buffer', for old Emacsen.
51 ;; Pickup `char-category' for XEmacs.
58 (autoload 'mu-cite-get-prefix-method "mu-register")
59 (autoload 'mu-cite-get-prefix-register-method "mu-register")
60 (autoload 'mu-cite-get-prefix-register-verbose-method "mu-register")
66 (defconst mu-cite-version "8.0")
72 ;; This part will be abolished in the near future.
75 (eval-when-compile (require 'static))
78 (defconst mu-cite-obsolete-variable-alist
79 '((mu-cite/cited-prefix-regexp mu-cite-cited-prefix-regexp)
80 (mu-cite/default-methods-alist mu-cite-default-methods-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)))
91 (apply (function make-obsolete-variable) elem)
92 (when (and (not noninteractive)
94 (apply (function message)
95 "WARNING: `%s' is an obsolete variable, use `%s' instead."
97 (static-if (fboundp 'defvaralias) ; It may exists in XEmacs.
98 (apply (function defvaralias) elem)
99 (when (boundp (car elem))
100 (eval (list 'defvar (cadr elem) (car elem)))))))
101 mu-cite-obsolete-variable-alist)
106 (defconst mu-cite-obsolete-function-alist
107 '((mu-cite/cite-original mu-cite-original)
108 (mu-cite/eval-format mu-cite-eval-format)
109 (mu-cite/get-field-value mu-cite-get-field-value)
110 (mu-cite/get-ml-count-method mu-cite-get-ml-count-method)
111 (mu-cite/get-value mu-cite-get-value)
112 (mu-cite/make-methods mu-cite-make-methods)))
115 (function (lambda (elem)
116 (apply (function define-obsolete-function-alias) elem)))
117 mu-cite-obsolete-function-alist)
124 (defgroup mu-cite nil
125 "yet another citation tool for GNU Emacs."
130 (defvar mu-cite-default-methods-alist
134 (mu-cite-get-field-value "From"))))
138 (mu-cite-get-field-value "Date"))))
142 (mu-cite-get-field-value "Message-Id"))))
146 (mu-cite-get-field-value "Subject"))))
150 (mu-cite-get-field-value "X-Ml-Name"))))
151 (cons 'ml-count (function mu-cite-get-ml-count-method))
152 (cons 'address-structure
156 (std11-parse-address-string (mu-cite-get-value 'from))))))
160 (std11-full-name-string
161 (mu-cite-get-value 'address-structure)))))
165 (std11-address-string
166 (mu-cite-get-value 'address-structure)))))
170 (let ((ml-name (mu-cite-get-value 'ml-name)))
175 (mu-cite-get-value 'ml-count)
177 (mu-cite-get-value 'message-id))))))
181 (let ((id (mu-cite-get-value 'id)))
183 (format ">>>>> In %s \n" id)
185 (cons 'prefix (function mu-cite-get-prefix-method))
186 (cons 'prefix-register
187 (function mu-cite-get-prefix-register-method))
188 (cons 'prefix-register-verbose
189 (function mu-cite-get-prefix-register-verbose-method))
193 (mu-cite-get-field-value "X-Attribution"))))
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."
211 (nconc '(choice :tag "String or Method name")
214 (lambda (elem) (list 'choice-item (car elem))))
215 mu-cite-default-methods-alist)
216 '((symbol :tag "Other Method")
218 (choice-item :tag "String: \"> \"" "> ")
219 (string :tag "Other String"))))
222 (defcustom mu-cite-top-format '(in-id ">>>>>\t" from " wrote:\n")
223 "List to represent top string of citation.
224 Each elements must be string or method name."
227 '(choice :tag "String or Method name")
230 (lambda (elem) (list 'choice-item (car elem))))
231 mu-cite-default-methods-alist)
232 '((symbol :tag "Other Method")
234 (choice-item :tag "String: \">>>>>\\t\"" ">>>>>\t")
235 (choice-item :tag "String: \" wrote:\\n\"" " wrote:\n")
236 (string :tag "Other String"))))
243 (defcustom mu-cite-load-hook nil
244 "List of functions called after mu-cite is loaded.
245 Use this hook to add your own methods to `mu-cite-default-methods-alist'."
249 (defcustom mu-cite-instantiation-hook nil
250 "List of functions called just before narrowing to the message."
254 (defcustom mu-cite-pre-cite-hook nil
255 "List of functions called before citing a region of text."
259 (defcustom mu-cite-post-cite-hook nil
260 "List of functions called after citing a region of text."
268 (defvar mu-cite-get-field-value-method-alist nil
269 "Alist major-mode vs. function to get field-body of header.")
271 (defun mu-cite-get-field-value (name)
272 (or (std11-field-body name)
273 (let ((method (assq major-mode mu-cite-get-field-value-method-alist)))
275 (funcall (cdr method) name)))))
284 (defcustom mu-cite-ml-count-field-list
285 '("X-Ml-Count" "X-Mail-Count" "X-Seqno" "X-Sequence" "Mailinglist-Id")
286 "List of header fields which contain sequence number of mailing list."
287 :type '(repeat (choice (choice-item "X-Ml-Count")
288 (choice-item "X-Mail-Count")
289 (choice-item "X-Seqno")
290 (choice-item "X-Sequence")
291 (choice-item "Mailinglist-Id")
293 (string :tag "Other")))
296 (defun mu-cite-get-ml-count-method ()
297 (let ((field-list mu-cite-ml-count-field-list))
300 (let* ((field (car field-list))
301 (ml-count (mu-cite-get-field-value field)))
302 (when (and ml-count (string-match "[0-9]+" ml-count))
305 (match-beginning 0)(match-end 0))))
306 (setq field-list (cdr field-list)))))))
312 (defvar mu-cite-methods-alist nil)
314 (defun mu-cite-make-methods ()
315 (setq mu-cite-methods-alist
316 (copy-alist mu-cite-default-methods-alist))
317 (run-hooks 'mu-cite-instantiation-hook))
319 (defun mu-cite-get-value (item)
320 (let ((ret (cdr (assoc item mu-cite-methods-alist))))
323 (setq ret (save-excursion (funcall ret)))
324 (set-alist 'mu-cite-methods-alist item ret))
327 (defun mu-cite-eval-format (list)
330 (cond ((stringp elt) elt)
331 ((symbolp elt) (mu-cite-get-value elt)))))
339 (defun mu-cite-original ()
340 "Citing filter function.
341 This is callable from the various mail and news readers' reply
342 function according to the agreed upon standard."
344 (mu-cite-make-methods)
346 (when (< (mark t) (point))
347 (exchange-point-and-mark))
348 (narrow-to-region (point)(point-max))
349 (run-hooks 'mu-cite-pre-cite-hook)
350 (let ((last-point (point))
351 (top (mu-cite-eval-format mu-cite-top-format))
352 (prefix (mu-cite-eval-format mu-cite-prefix-format)))
353 (when (re-search-forward "^-*$" nil nil)
356 (delete-region last-point (point))
358 (setq last-point (point))
359 (while (< (point)(mark t))
360 (unless (looking-at mu-cite-cited-prefix-regexp)
363 (goto-char last-point))
364 (run-hooks 'mu-cite-post-cite-hook)))
367 ;;; @ message editing utilities
370 (defcustom citation-mark-chars ">}|"
371 "String of characters for citation delimiter."
375 (defcustom citation-disable-chars "<{"
376 "String of characters not allowed as citation-prefix."
380 (defun detect-paragraph-cited-prefix ()
382 (goto-char (point-min))
386 (progn (beginning-of-line)(point))
387 (progn (end-of-line)(point))))
389 (while (and (= (forward-line) 0)
390 (setq str (buffer-substring
391 (progn (beginning-of-line)(point))
392 (progn (end-of-line)(point))))
393 (setq ret (string-compare-from-top prefix str)))
399 (cond ((> i 1) prefix)
401 (goto-char (point-min))
403 (narrow-to-region (point)
404 (+ (point)(length prefix)))
405 (goto-char (point-max))
406 (if (re-search-backward
407 (concat "[" citation-mark-chars "]") nil t)
409 (goto-char (match-end 0))
410 (when (looking-at "[ \t]+")
411 (goto-char (match-end 0)))
412 (buffer-substring (point-min)(point)))
415 (goto-char (point-max))
417 (concat "[" citation-disable-chars "]") nil t)
419 (concat "[" citation-mark-chars "]") nil t))
420 (goto-char (match-end 0))
421 (when (looking-at "[ \t]+")
422 (goto-char (match-end 0)))
423 (buffer-substring (point-min)(point)))
426 (defun fill-cited-region (beg end)
431 (and (search-backward "\n" nil t)
432 (setq end (match-end 0)))
433 (narrow-to-region beg end)
434 (let* ((fill-prefix (detect-paragraph-cited-prefix))
435 (pat (concat fill-prefix "\n")))
436 (goto-char (point-min))
437 (while (search-forward pat nil t)
438 (let ((b (match-beginning 0))
441 (when (and (> b (point-min))
442 (let ((cat (char-category
444 (or (string-match "a" cat)
445 (string-match "l" cat))))
447 (goto-char (point-min))
448 (fill-region (point-min) (point-max))))))
450 (defun compress-cited-prefix ()
453 (goto-char (point-min))
455 (concat "^" (regexp-quote mail-header-separator) "$") nil t)
456 (while (re-search-forward
457 (concat "^\\([ \t]*[^ \t\n" citation-mark-chars "]*["
458 citation-mark-chars "]\\)+") nil t)
459 (let* ((b (match-beginning 0))
461 (prefix (buffer-substring b e))
464 (when (string-match "<[^<>]+>" prefix)
465 (setq prefix (substring prefix 0 (match-beginning 0))))
467 (concat "\\([" citation-mark-chars "]+\\)[ \t]*")
469 (setq i (+ i (- (match-end 1)(match-beginning 1)))
471 pe (match-beginning 1)
474 (when (and ps (< ps pe))
476 (insert (concat (substring prefix ps pe) (make-string nest ?>))))
479 (defun replace-top-string (old new)
480 (interactive "*sOld string: \nsNew string: ")
481 (while (re-search-forward
482 (concat "^" (regexp-quote old)) nil t)
483 (replace-match new)))
485 (defun string-compare-from-top (str1 str2)
486 (let* ((len1 (length str1))
488 (len (min len1 len2))
491 (while (and (< p len)
493 (setq c1 (aref str1 p)
496 (setq p (+ p (char-length c1))))
498 (let ((matched (substring str1 0 p))
499 (r1 (and (< p len1)(substring str1 p)))
500 (r2 (and (< p len2)(substring str2 p))))
503 (list 'seq matched (list 'or r1 r2)))))))
509 ;; This part will be abolished in the near future.
511 (static-unless (fboundp 'defvaralias)
515 (eval (list 'defvar (car elem) (cadr elem)))))
516 mu-cite-obsolete-variable-alist))
524 (run-hooks 'mu-cite-load-hook)
526 ;;; mu-cite.el ends here