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"))))
152 (cons 'prefix (function mu-cite-get-prefix-method))
153 (cons 'prefix-register
154 (function mu-cite-get-prefix-register-method))
155 (cons 'prefix-register-verbose
156 (function mu-cite-get-prefix-register-verbose-method))
159 (function mu-bbdb-get-prefix-method))
160 (cons 'bbdb-prefix-register
161 (function mu-bbdb-get-prefix-register-method))
162 (cons 'bbdb-prefix-register-verbose
163 (function mu-bbdb-get-prefix-register-verbose-method))
170 (defcustom mu-cite-cited-prefix-regexp
171 "\\(^[^ \t\n<>]+>+[ \t]*\\|^[ \t]*$\\)"
172 "Regexp to match the citation prefix.
173 If match, mu-cite doesn't insert citation prefix."
177 (defcustom mu-cite-prefix-format '(prefix-register-verbose "> ")
178 "List to represent citation prefix.
179 Each elements must be a string or a method name."
189 :tag "Method or String"
193 (function (lambda (elem) (list 'choice-item (car elem))))
194 mu-cite-default-methods-alist)
195 '((symbol :tag "Method")
197 (choice-item :tag "String: \"> \"" "> ")
199 :set (function (lambda (symbol value)
200 (set-default symbol (delq nil value))))
203 (defcustom mu-cite-top-format '(in-id ">>>>>\t" from " wrote:\n")
204 "List to represent top string of citation.
205 Each elements must be a string or a method name."
214 :tag "Method or String"
218 (function (lambda (elem) (list 'choice-item (car elem))))
219 mu-cite-default-methods-alist)
220 '((symbol :tag "Method")
222 (choice-item :tag "String: \">>>>>\\t\"" ">>>>>\t")
223 (choice-item :tag "String: \" wrote:\\n\"" " wrote:\n")
224 (string :tag "String"))))))))
225 :set (function (lambda (symbol value)
226 (set-default symbol (delq nil value))))
233 (defcustom mu-cite-instantiation-hook nil
234 "List of functions called just before narrowing to the message."
238 (defcustom mu-cite-pre-cite-hook nil
239 "List of functions called before citing a region of text."
243 (defcustom mu-cite-post-cite-hook nil
244 "List of functions called after citing a region of text."
252 (defvar mu-cite-get-field-value-method-alist nil
253 "Alist major-mode vs. function to get field-body of header.")
255 (defun mu-cite-get-field-value (name)
256 "Return the value of the header field NAME.
257 If the field is not found in the header, a method function which is
258 registered in variable `mu-cite-get-field-value-method-alist' is called."
259 (or (std11-field-body name)
260 (let ((method (assq major-mode mu-cite-get-field-value-method-alist)))
262 (funcall (cdr method) name)))))
271 (defcustom mu-cite-ml-count-field-list
272 '("X-Ml-Count" "X-Mail-Count" "X-Seqno" "X-Sequence" "Mailinglist-Id")
273 "List of header fields which contains a sequence number of the mailing list."
274 :type '(repeat (choice :tag "Field Name"
275 (choice-item "X-Ml-Count")
276 (choice-item "X-Mail-Count")
277 (choice-item "X-Seqno")
278 (choice-item "X-Sequence")
279 (choice-item "Mailinglist-Id")
281 (string :tag "Other")))
282 :set (function (lambda (symbol value)
283 (set-default symbol (delq nil value))))
286 (defun mu-cite-get-ml-count-method ()
287 "A mu-cite method to return a ML-count.
288 This function searches a field about ML-count, which is specified by
289 the variable `mu-cite-ml-count-field-list', in a header.
290 If the field is found, the function returns a number part of the
293 Notice that please use (mu-cite-get-value 'ml-count)
294 instead of to call the function directly."
295 (let ((field-list mu-cite-ml-count-field-list))
298 (let* ((field (car field-list))
299 (ml-count (mu-cite-get-field-value field)))
300 (if (and ml-count (string-match "[0-9]+" ml-count))
301 (throw 'tag (match-string 0 ml-count)))
302 (setq field-list (cdr field-list)))))))
308 (defvar mu-cite-methods-alist nil)
310 (defun mu-cite-make-methods ()
311 (setq mu-cite-methods-alist
312 (copy-alist mu-cite-default-methods-alist))
313 (run-hooks 'mu-cite-instantiation-hook))
315 (defun mu-cite-get-value (item)
316 "Return a current value of ITEM."
317 (let ((ret (cdr (assoc item mu-cite-methods-alist))))
320 (setq ret (save-excursion (funcall ret)))
321 (set-alist 'mu-cite-methods-alist item ret))
324 (defun mu-cite-eval-format (list)
327 (cond ((stringp elt) elt)
328 ((symbolp elt) (mu-cite-get-value elt)))))
336 (defun mu-cite-original ()
337 "Citing filter function.
338 This is callable from the various mail and news readers' reply
339 function according to the agreed upon standard."
341 (mu-cite-make-methods)
343 (if (< (mark t) (point))
344 (exchange-point-and-mark))
345 (narrow-to-region (point)(point-max))
346 (run-hooks 'mu-cite-pre-cite-hook)
347 (let ((last-point (point))
348 (top (mu-cite-eval-format mu-cite-top-format))
349 (prefix (mu-cite-eval-format mu-cite-prefix-format)))
350 (if (re-search-forward "^-*$" nil nil)
353 (delete-region last-point (point))
355 (setq last-point (point))
356 (while (< (point)(mark t))
357 (or (looking-at mu-cite-cited-prefix-regexp)
360 (goto-char last-point))
361 (run-hooks 'mu-cite-post-cite-hook)))
364 ;;; @ message editing utilities
367 (defcustom citation-mark-chars ">}|"
368 "String of characters for citation delimiter."
372 (defcustom citation-disable-chars "<{"
373 "String of characters not allowed as citation-prefix."
378 ;; Force redefine the function `char-category' which may have been
379 ;; defined by emu.el. They can be distinguished by the number of
380 ;; arguments. Anyway, that is the best way not to use emu.el.
381 (if (and (fboundp 'char-category)
382 (subrp (symbol-function 'char-category)))
384 (fmakunbound 'char-category)
385 (defun-maybe-cond char-category (character &optional table)
386 "Return a string of category mnemonics for CHAR in TABLE.
387 CHAR can be any multilingual characters,
388 TABLE defaults to the current buffer's category table."
389 ((and (subr-fboundp 'char-category-set)
390 (subr-fboundp 'category-set-mnemonics))
391 (category-set-mnemonics (char-category-set character)))
392 ((and (fboundp 'char-category-list)
393 ;; `char-category-list' returns a list of characters
394 ;; in XEmacs 21.2.25 and later, otherwise integers.
395 (characterp (car-safe (char-category-list ?a))))
396 (concat (char-category-list character)))
397 ((fboundp 'char-category-list)
398 (mapconcat (lambda (chr)
399 (char-to-string (int-char chr)))
400 (char-category-list character)
403 (if (< (char-int character) 128)
407 (if (< (char-int character) 128)
411 (defun detect-paragraph-cited-prefix ()
413 (goto-char (point-min))
416 (buffer-substring (line-beginning-position)
417 (line-end-position))))
420 (while (and (= (forward-line) 0)
421 (setq str (buffer-substring
422 (progn (beginning-of-line)(point))
423 (progn (end-of-line)(point))))
424 (setq ret (string-compare-from-top prefix str)))
429 (or (string-equal init prefix)
431 (cond ((> i 1) prefix)
433 (goto-char (point-min))
435 (narrow-to-region (point)
436 (+ (point)(length prefix)))
437 (goto-char (point-max))
438 (if (re-search-backward
439 (concat "[" citation-mark-chars "]") nil t)
441 (goto-char (match-end 0))
442 (if (looking-at "[ \t]+")
443 (goto-char (match-end 0)))
444 (buffer-substring (point-min)(point)))
447 (goto-char (point-max))
449 (concat "[" citation-disable-chars "]") nil t)
451 (concat "[" citation-mark-chars "]") nil t))
452 (goto-char (match-end 0))
453 (if (looking-at "[ \t]+")
454 (goto-char (match-end 0)))
455 (buffer-substring (line-beginning-position)(point)))
458 (defcustom fill-column-for-fill-cited-region nil
459 "Integer to override `fill-column' while `fill-cited-region' is being
461 :type (` (choice (const :tag "Off" nil)
462 (integer (, default-fill-column))))
466 (defun fill-cited-region (beg end)
467 "Fill each of the paragraphs in the region as a cited text."
472 (and (search-backward "\n" nil t)
473 (setq end (match-end 0)))
474 (narrow-to-region beg end)
475 (let* ((fill-prefix (detect-paragraph-cited-prefix))
476 (fill-column (max (+ 1 (current-left-margin)
477 (string-width fill-prefix))
478 (or fill-column-for-fill-cited-region
479 (current-fill-column))))
480 (pat (concat fill-prefix "\n"))
482 (goto-char (point-min))
483 (while (search-forward pat nil t)
484 (let ((b (match-beginning 0))
487 (if (and (> b (point-min))
488 (let ((cat (char-category
490 (or (string-match "a" cat)
491 (string-match "l" cat))))
493 (goto-char (point-min))
494 (fill-region (point-min) (point-max))))))
497 (defun compress-cited-prefix ()
498 "Compress nested cited prefixes."
501 (goto-char (point-min))
503 (concat "^" (regexp-quote mail-header-separator) "$") nil t)
504 (while (re-search-forward
505 (concat "^\\([ \t]*[^ \t\n" citation-mark-chars "]*["
506 citation-mark-chars "]\\)+") nil t)
507 (let* ((b (match-beginning 0))
509 (prefix (buffer-substring b e))
512 (if (string-match "<[^<>]+>" prefix)
514 (substring prefix 0 (match-beginning 0))))
516 (concat "\\([" citation-mark-chars "]+\\)[ \t]*")
518 (setq i (+ i (- (match-end 1)(match-beginning 1)))
520 pe (match-beginning 1)
523 (if (and ps (< ps pe))
526 (insert (concat (substring prefix ps pe)
527 (make-string nest ?>)))))
530 (defun replace-top-string (old new)
531 (interactive "*sOld string: \nsNew string: ")
532 (while (re-search-forward
533 (concat "^" (regexp-quote old)) nil t)
534 (replace-match new)))
536 (defun string-compare-from-top (str1 str2)
537 (let* ((len1 (length str1))
539 (len (min len1 len2))
542 (while (and (< p len)
544 (setq c1 (sref str1 p)
547 (setq p (char-next-index c1 p)))
549 (let ((matched (substring str1 0 p))
550 (r1 (and (< p len1)(substring str1 p)))
551 (r2 (and (< p len2)(substring str2 p))))
554 (list 'seq matched (list 'or r1 r2)))))))
562 (run-hooks 'mu-cite-load-hook)
564 ;;; mu-cite.el ends here