1 ;;; mu-cite.el --- yet another citation tool for GNU Emacs
2 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001
3 ;; 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 ;; For picking up the macros `char-next-index', `with-temp-buffer', etc.
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))
132 (ml-count (mu-cite-get-value 'ml-count)))
137 (concat " : No." ml-count))
139 (mu-cite-get-value 'message-id))))))
143 (let ((id (mu-cite-get-value 'id)))
145 (format ">>>>> In %s \n" id)
150 (mu-cite-get-field-value "X-Attribution"))))
154 (mu-cite-get-field-value "X-Cite-Me"))))
156 (cons 'prefix (function mu-cite-get-prefix-method))
157 (cons 'prefix-register
158 (function mu-cite-get-prefix-register-method))
159 (cons 'prefix-register-verbose
160 (function mu-cite-get-prefix-register-verbose-method))
163 (function mu-bbdb-get-prefix-method))
164 (cons 'bbdb-prefix-register
165 (function mu-bbdb-get-prefix-register-method))
166 (cons 'bbdb-prefix-register-verbose
167 (function mu-bbdb-get-prefix-register-verbose-method))
174 (defcustom mu-cite-cited-prefix-regexp
175 "\\(^[^ \t\n<>]+>+[ \t]*\\|^[ \t]*$\\)"
176 "Regexp to match the citation prefix.
177 If match, mu-cite doesn't insert citation prefix."
181 (defcustom mu-cite-prefix-format '(prefix-register-verbose "> ")
182 "List to represent citation prefix.
183 Each elements must be a string or a method name."
193 :tag "Method or String"
197 (function (lambda (elem) (list 'choice-item (car elem))))
198 mu-cite-default-methods-alist)
199 '((symbol :tag "Method")
201 (choice-item :tag "String: \"> \"" "> ")
203 :set (function (lambda (symbol value)
204 (set-default symbol (delq nil value))))
207 (defcustom mu-cite-top-format '(in-id ">>>>>\t" from " wrote:\n")
208 "List to represent top string of citation.
209 Each elements must be a string or a method name."
218 :tag "Method or String"
222 (function (lambda (elem) (list 'choice-item (car elem))))
223 mu-cite-default-methods-alist)
224 '((symbol :tag "Method")
226 (choice-item :tag "String: \">>>>>\\t\"" ">>>>>\t")
227 (choice-item :tag "String: \" wrote:\\n\"" " wrote:\n")
228 (string :tag "String"))))))))
229 :set (function (lambda (symbol value)
230 (set-default symbol (delq nil value))))
237 (defcustom mu-cite-instantiation-hook nil
238 "List of functions called just before narrowing to the message."
242 (defcustom mu-cite-pre-cite-hook nil
243 "List of functions called before citing a region of text."
247 (defcustom mu-cite-post-cite-hook nil
248 "List of functions called after citing a region of text."
256 (defvar mu-cite-get-field-value-method-alist nil
257 "Alist major-mode vs. function to get field-body of header.")
259 (defun mu-cite-get-field-value (name)
260 "Return the value of the header field NAME.
261 If the field is not found in the header, a method function which is
262 registered in variable `mu-cite-get-field-value-method-alist' is called."
263 (or (std11-field-body name)
264 (let ((method (assq major-mode mu-cite-get-field-value-method-alist)))
266 (funcall (cdr method) name)))))
275 (defcustom mu-cite-ml-count-field-list
276 '("X-Ml-Count" "X-Mail-Count" "X-Seqno" "X-Sequence" "Mailinglist-Id")
277 "List of header fields which contains a sequence number of the mailing list."
278 :type '(repeat (choice :tag "Field Name"
279 (choice-item "X-Ml-Count")
280 (choice-item "X-Mail-Count")
281 (choice-item "X-Seqno")
282 (choice-item "X-Sequence")
283 (choice-item "Mailinglist-Id")
285 (string :tag "Other")))
286 :set (function (lambda (symbol value)
287 (set-default symbol (delq nil value))))
290 (defun mu-cite-get-ml-count-method ()
291 "A mu-cite method to return a ML-count.
292 This function searches a field about ML-count, which is specified by
293 the variable `mu-cite-ml-count-field-list', in a header.
294 If the field is found, the function returns a number part of the
297 Notice that please use (mu-cite-get-value 'ml-count)
298 instead of to call the function directly."
299 (let ((field-list mu-cite-ml-count-field-list))
302 (let* ((field (car field-list))
303 (ml-count (mu-cite-get-field-value field)))
304 (if (and ml-count (string-match "[0-9]+" ml-count))
305 (throw 'tag (match-string 0 ml-count)))
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 "Return a current value of ITEM."
321 (let ((ret (cdr (assoc item mu-cite-methods-alist))))
324 (setq ret (save-excursion (funcall ret)))
325 (set-alist 'mu-cite-methods-alist item ret))
328 (defun mu-cite-eval-format (list)
331 (cond ((stringp elt) elt)
332 ((symbolp elt) (mu-cite-get-value elt)))))
340 (defun mu-cite-original ()
341 "Citing filter function.
342 This is callable from the various mail and news readers' reply
343 function according to the agreed upon standard."
345 (mu-cite-make-methods)
347 (if (< (mark t) (point))
348 (exchange-point-and-mark))
349 (narrow-to-region (point)(point-max))
350 (run-hooks 'mu-cite-pre-cite-hook)
351 (let ((last-point (point))
352 (top (mu-cite-eval-format mu-cite-top-format))
353 (prefix (mu-cite-eval-format mu-cite-prefix-format)))
354 (if (re-search-forward "^-*$" nil nil)
357 (delete-region last-point (point))
359 (setq last-point (point))
360 (while (< (point)(mark t))
361 (or (looking-at mu-cite-cited-prefix-regexp)
364 (goto-char last-point))
365 (run-hooks 'mu-cite-post-cite-hook)))
368 ;;; @ message editing utilities
371 (defcustom citation-mark-chars ">}|"
372 "String of characters for citation delimiter."
376 (defcustom citation-disable-chars "<{"
377 "String of characters not allowed as citation-prefix."
382 ;; Force redefine the function `char-category' which may have been
383 ;; defined by emu.el. They can be distinguished by the number of
384 ;; arguments. Anyway, that is the best way not to use emu.el.
385 (if (and (fboundp 'char-category)
386 (subrp (symbol-function 'char-category)))
388 (fmakunbound 'char-category)
389 (defun-maybe-cond char-category (character &optional table)
390 "Return a string of category mnemonics for CHAR in TABLE.
391 CHAR can be any multilingual characters,
392 TABLE defaults to the current buffer's category table."
393 ((and (subr-fboundp 'char-category-set)
394 (subr-fboundp 'category-set-mnemonics))
395 (category-set-mnemonics (char-category-set character)))
396 ((and (fboundp 'char-category-list)
397 ;; `char-category-list' returns a list of characters
398 ;; in XEmacs 21.2.25 and later, otherwise integers.
399 (characterp (car-safe (char-category-list ?a))))
400 (concat (char-category-list character)))
401 ((fboundp 'char-category-list)
402 (mapconcat (lambda (chr)
403 (char-to-string (int-char chr)))
404 (char-category-list character)
407 (if (< (char-int character) 128)
411 (if (< (char-int character) 128)
415 (defun detect-paragraph-cited-prefix ()
417 (goto-char (point-min))
420 (buffer-substring (line-beginning-position)
421 (line-end-position))))
424 (while (and (= (forward-line) 0)
425 (setq str (buffer-substring
426 (progn (beginning-of-line)(point))
427 (progn (end-of-line)(point))))
428 (setq ret (string-compare-from-top prefix str)))
433 (or (string-equal init prefix)
435 (cond ((> i 1) prefix)
437 (goto-char (point-min))
439 (narrow-to-region (point)
440 (+ (point)(length prefix)))
441 (goto-char (point-max))
442 (if (re-search-backward
443 (concat "[" citation-mark-chars "]") nil t)
445 (goto-char (match-end 0))
446 (if (looking-at "[ \t]+")
447 (goto-char (match-end 0)))
448 (buffer-substring (point-min)(point)))
451 (goto-char (point-max))
453 (concat "[" citation-disable-chars "]") nil t)
455 (concat "[" citation-mark-chars "]") nil t))
456 (goto-char (match-end 0))
457 (if (looking-at "[ \t]+")
458 (goto-char (match-end 0)))
459 (buffer-substring (line-beginning-position)(point)))
462 (defcustom fill-column-for-fill-cited-region nil
463 "Integer to override `fill-column' while `fill-cited-region' is being
465 :type (` (choice (const :tag "Off" nil)
466 (integer (, default-fill-column))))
470 (defun fill-cited-region (beg end)
471 "Fill each of the paragraphs in the region as a cited text."
476 (and (search-backward "\n" nil t)
477 (setq end (match-end 0)))
478 (narrow-to-region beg end)
479 (let* ((fill-prefix (detect-paragraph-cited-prefix))
480 (fill-column (max (+ 1 (current-left-margin)
481 (string-width fill-prefix))
482 (or fill-column-for-fill-cited-region
483 (current-fill-column))))
484 (pat (concat fill-prefix "\n"))
486 (goto-char (point-min))
487 (while (search-forward pat nil t)
488 (let ((b (match-beginning 0))
491 (if (and (> b (point-min))
492 (let ((cat (char-category
494 (or (string-match "a" cat)
495 (string-match "l" cat))))
497 (goto-char (point-min))
498 (fill-region (point-min) (point-max))))))
501 (defun compress-cited-prefix ()
502 "Compress nested cited prefixes."
505 (goto-char (point-min))
507 (concat "^" (regexp-quote mail-header-separator) "$") nil t)
508 (while (re-search-forward
509 (concat "^\\([ \t]*[^ \t\n" citation-mark-chars "]*["
510 citation-mark-chars "]\\)+") nil t)
511 (let* ((b (match-beginning 0))
513 (prefix (buffer-substring b e))
516 (if (string-match "<[^<>]+>" prefix)
518 (substring prefix 0 (match-beginning 0))))
520 (concat "\\([" citation-mark-chars "]+\\)[ \t]*")
522 (setq i (+ i (- (match-end 1)(match-beginning 1)))
524 pe (match-beginning 1)
527 (if (and ps (< ps pe))
530 (insert (concat (substring prefix ps pe)
531 (make-string nest ?>)))))
534 (defun replace-top-string (old new)
535 (interactive "*sOld string: \nsNew string: ")
536 (while (re-search-forward
537 (concat "^" (regexp-quote old)) nil t)
538 (replace-match new)))
540 (defun string-compare-from-top (str1 str2)
541 (let* ((len1 (length str1))
543 (len (min len1 len2))
546 (while (and (< p len)
548 (setq c1 (sref str1 p)
551 (setq p (char-next-index c1 p)))
553 (let ((matched (substring str1 0 p))
554 (r1 (and (< p len1)(substring str1 p)))
555 (r2 (and (< p len2)(substring str2 p))))
558 (list 'seq matched (list 'or r1 r2)))))))
566 (run-hooks 'mu-cite-load-hook)
568 ;;; mu-cite.el ends here