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 future.
82 "yet another citation tool for GNU Emacs."
87 (defvar mu-cite-default-methods-alist
91 (mu-cite-get-field-value "From"))))
95 (mu-cite-get-field-value "Date"))))
99 (mu-cite-get-field-value "Message-Id"))))
103 (mu-cite-get-field-value "Subject"))))
107 (mu-cite-get-field-value "X-Ml-Name"))))
108 (cons 'ml-count (function mu-cite-get-ml-count-method))
109 (cons 'address-structure
113 (std11-parse-address-string (mu-cite-get-value 'from))))))
117 (std11-full-name-string
118 (mu-cite-get-value 'address-structure)))))
122 (std11-address-string
123 (mu-cite-get-value 'address-structure)))))
127 (let ((ml-name (mu-cite-get-value 'ml-name)))
132 (mu-cite-get-value 'ml-count)
134 (mu-cite-get-value 'message-id))))))
138 (let ((id (mu-cite-get-value 'id)))
140 (format ">>>>> In %s \n" id)
142 (cons 'prefix (function mu-cite-get-prefix-method))
143 (cons 'prefix-register
144 (function mu-cite-get-prefix-register-method))
145 (cons 'prefix-register-verbose
146 (function mu-cite-get-prefix-register-verbose-method))
150 (mu-cite-get-field-value "X-Attribution"))))
157 (defcustom mu-cite-cited-prefix-regexp
158 "\\(^[^ \t\n<>]+>+[ \t]*\\|^[ \t]*$\\)"
159 "Regexp to match the citation prefix.
160 If match, mu-cite doesn't insert citation prefix."
164 (defcustom mu-cite-prefix-format '(prefix-register-verbose "> ")
165 "List to represent citation prefix.
166 Each elements must be string or method name."
168 (nconc '(choice :tag "String or Method name")
171 (lambda (elem) (list 'choice-item (car elem))))
172 mu-cite-default-methods-alist)
173 '((symbol :tag "Other Method")
175 (choice-item :tag "String: \"> \"" "> ")
176 (string :tag "Other String"))))
179 (defcustom mu-cite-top-format '(in-id ">>>>>\t" from " wrote:\n")
180 "List to represent top string of citation.
181 Each elements must be string or method name."
184 '(choice :tag "String or Method name")
187 (lambda (elem) (list 'choice-item (car elem))))
188 mu-cite-default-methods-alist)
189 '((symbol :tag "Other Method")
191 (choice-item :tag "String: \">>>>>\\t\"" ">>>>>\t")
192 (choice-item :tag "String: \" wrote:\\n\"" " wrote:\n")
193 (string :tag "Other String"))))
200 (defcustom mu-cite-load-hook nil
201 "List of functions called after mu-cite is loaded.
202 Use this hook to add your own methods to `mu-cite-default-methods-alist'."
206 (defcustom mu-cite-instantiation-hook nil
207 "List of functions called just before narrowing to the message."
211 (defcustom mu-cite-pre-cite-hook nil
212 "List of functions called before citing a region of text."
216 (defcustom mu-cite-post-cite-hook nil
217 "List of functions called after citing a region of text."
225 (defvar mu-cite-get-field-value-method-alist nil
226 "Alist major-mode vs. function to get field-body of header.")
228 (defun mu-cite-get-field-value (name)
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 (let ((field-list mu-cite-ml-count-field-list))
257 (let* ((field (car field-list))
258 (ml-count (mu-cite-get-field-value field)))
259 (when (and ml-count (string-match "[0-9]+" ml-count))
262 (match-beginning 0)(match-end 0))))
263 (setq field-list (cdr field-list)))))))
269 (defvar mu-cite-methods-alist nil)
271 (defun mu-cite-make-methods ()
272 (setq mu-cite-methods-alist
273 (copy-alist mu-cite-default-methods-alist))
274 (run-hooks 'mu-cite-instantiation-hook))
276 (defun mu-cite-get-value (item)
277 (let ((ret (cdr (assoc item mu-cite-methods-alist))))
280 (setq ret (save-excursion (funcall ret)))
281 (set-alist 'mu-cite-methods-alist item ret))
284 (defun mu-cite-eval-format (list)
287 (cond ((stringp elt) elt)
288 ((symbolp elt) (mu-cite-get-value elt)))))
296 (defun mu-cite-original ()
297 "Citing filter function.
298 This is callable from the various mail and news readers' reply
299 function according to the agreed upon standard."
301 (mu-cite-make-methods)
303 (when (< (mark t) (point))
304 (exchange-point-and-mark))
305 (narrow-to-region (point)(point-max))
306 (run-hooks 'mu-cite-pre-cite-hook)
307 (let ((last-point (point))
308 (top (mu-cite-eval-format mu-cite-top-format))
309 (prefix (mu-cite-eval-format mu-cite-prefix-format)))
310 (when (re-search-forward "^-*$" nil nil)
313 (delete-region last-point (point))
315 (setq last-point (point))
316 (while (< (point)(mark t))
317 (unless (looking-at mu-cite-cited-prefix-regexp)
320 (goto-char last-point))
321 (run-hooks 'mu-cite-post-cite-hook)))
324 ;;; @ message editing utilities
327 (defcustom citation-mark-chars ">}|"
328 "String of characters for citation delimiter."
332 (defcustom citation-disable-chars "<{"
333 "String of characters not allowed as citation-prefix."
337 (defun detect-paragraph-cited-prefix ()
339 (goto-char (point-min))
343 (progn (beginning-of-line)(point))
344 (progn (end-of-line)(point))))
346 (while (and (= (forward-line) 0)
347 (setq str (buffer-substring
348 (progn (beginning-of-line)(point))
349 (progn (end-of-line)(point))))
350 (setq ret (string-compare-from-top prefix str)))
356 (cond ((> i 1) prefix)
358 (goto-char (point-min))
360 (narrow-to-region (point)
361 (+ (point)(length prefix)))
362 (goto-char (point-max))
363 (if (re-search-backward
364 (concat "[" citation-mark-chars "]") nil t)
366 (goto-char (match-end 0))
367 (when (looking-at "[ \t]+")
368 (goto-char (match-end 0)))
369 (buffer-substring (point-min)(point)))
372 (goto-char (point-max))
374 (concat "[" citation-disable-chars "]") nil t)
376 (concat "[" citation-mark-chars "]") nil t))
377 (goto-char (match-end 0))
378 (when (looking-at "[ \t]+")
379 (goto-char (match-end 0)))
380 (buffer-substring (point-min)(point)))
383 (defun fill-cited-region (beg end)
388 (and (search-backward "\n" nil t)
389 (setq end (match-end 0)))
390 (narrow-to-region beg end)
391 (let* ((fill-prefix (detect-paragraph-cited-prefix))
392 (pat (concat fill-prefix "\n")))
393 (goto-char (point-min))
394 (while (search-forward pat nil t)
395 (let ((b (match-beginning 0))
398 (when (and (> b (point-min))
399 (let ((cat (char-category
401 (or (string-match "a" cat)
402 (string-match "l" cat))))
404 (goto-char (point-min))
405 (fill-region (point-min) (point-max))))))
407 (defun compress-cited-prefix ()
410 (goto-char (point-min))
412 (concat "^" (regexp-quote mail-header-separator) "$") nil t)
413 (while (re-search-forward
414 (concat "^\\([ \t]*[^ \t\n" citation-mark-chars "]*["
415 citation-mark-chars "]\\)+") nil t)
416 (let* ((b (match-beginning 0))
418 (prefix (buffer-substring b e))
421 (when (string-match "<[^<>]+>" prefix)
422 (setq prefix (substring prefix 0 (match-beginning 0))))
424 (concat "\\([" citation-mark-chars "]+\\)[ \t]*")
426 (setq i (+ i (- (match-end 1)(match-beginning 1)))
428 pe (match-beginning 1)
431 (when (and ps (< ps pe))
433 (insert (concat (substring prefix ps pe) (make-string nest ?>))))
436 (defun replace-top-string (old new)
437 (interactive "*sOld string: \nsNew string: ")
438 (while (re-search-forward
439 (concat "^" (regexp-quote old)) nil t)
440 (replace-match new)))
442 (defun string-compare-from-top (str1 str2)
443 (let* ((len1 (length str1))
445 (len (min len1 len2))
448 (while (and (< p len)
450 (setq c1 (aref str1 p)
453 (setq p (+ p (char-length c1))))
455 (let ((matched (substring str1 0 p))
456 (r1 (and (< p len1)(substring str1 p)))
457 (r2 (and (< p len2)(substring str2 p))))
460 (list 'seq matched (list 'or r1 r2)))))))
466 ;; This part will be abolished in the future.
468 (define-obsolete-function-alias
469 'mu-cite/cite-original 'mu-cite-original)
470 (define-obsolete-function-alias
471 'mu-cite/get-field-value 'mu-cite-get-field-value)
472 (define-obsolete-function-alias
473 'mu-cite/get-value 'mu-cite-get-value)
475 (eval-when-compile (require 'static))
477 (static-when (featurep 'xemacs)
478 (define-obsolete-variable-alias
479 'mu-cite/default-methods-alist
480 'mu-cite-default-methods-alist)
481 (define-obsolete-variable-alias
482 'mu-cite/cited-prefix-regexp
483 'mu-cite-cited-prefix-regexp)
484 (define-obsolete-variable-alias
485 'mu-cite/prefix-format
486 'mu-cite-prefix-format)
487 (define-obsolete-variable-alias
490 (define-obsolete-variable-alias
491 'mu-cite/instantiation-hook
492 'mu-cite-instantiation-hook)
493 (define-obsolete-variable-alias
494 'mu-cite/pre-cite-hook
495 'mu-cite-pre-cite-hook)
496 (define-obsolete-variable-alias
497 'mu-cite/post-cite-hook
498 'mu-cite-post-cite-hook)
499 (define-obsolete-variable-alias
500 'mu-cite/get-field-value-method-alist
501 'mu-cite-get-field-value-method-alist)
502 (define-obsolete-variable-alias
503 'mu-cite/ml-count-field-list
504 'mu-cite-ml-count-field-list)
507 (static-unless (fboundp 'defvaralias)
511 (eval (list 'defvar (car elem) (cadr elem)))))
512 mu-cite-obsolete-variable-alist))
520 (run-hooks 'mu-cite-load-hook)
522 ;;; mu-cite.el ends here