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 (cond ((featurep 'xemacs)
74 (` (let ((string (copy-sequence (, string))))
75 (map-extents (function (lambda (extent maparg)
76 (delete-extent extent))
77 string 0 (length string)))
79 ((or (boundp 'minibuffer-allow-text-properties);; Emacs 20.1 or later.
80 (not (fboundp 'set-text-properties)));; under Emacs 19.7.
83 (` (let ((string (copy-sequence (, string))))
84 (set-text-properties 0 (length string) nil string)
92 "Yet another citation tool for GNU Emacs."
97 (defvar mu-cite-default-methods-alist
101 (mu-cite-get-field-value "From"))))
105 (mu-cite-get-field-value "Date"))))
109 (mu-cite-get-field-value "Message-Id"))))
113 (mu-cite-get-field-value "Subject"))))
117 (mu-cite-get-field-value "X-Ml-Name"))))
118 (cons 'ml-count (function mu-cite-get-ml-count-method))
119 (cons 'address-structure
123 (std11-parse-address-string (mu-cite-get-value 'from))))))
127 (std11-full-name-string
128 (mu-cite-get-value 'address-structure)))))
132 (std11-address-string
133 (mu-cite-get-value 'address-structure)))))
137 (let ((ml-name (mu-cite-get-value 'ml-name))
138 (ml-count (mu-cite-get-value 'ml-count)))
143 (concat " : No." ml-count))
145 (mu-cite-get-value 'message-id))))))
149 (let ((id (mu-cite-get-value 'id)))
151 (format ">>>>> In %s \n" id)
156 (mu-cite-get-field-value "X-Attribution"))))
160 (mu-cite-get-field-value "X-Cite-Me"))))
162 (cons 'prefix (function mu-cite-get-prefix-method))
163 (cons 'prefix-register
164 (function mu-cite-get-prefix-register-method))
165 (cons 'prefix-register-verbose
166 (function mu-cite-get-prefix-register-verbose-method))
169 (function mu-bbdb-get-prefix-method))
170 (cons 'bbdb-prefix-register
171 (function mu-bbdb-get-prefix-register-method))
172 (cons 'bbdb-prefix-register-verbose
173 (function mu-bbdb-get-prefix-register-verbose-method))
180 (defcustom mu-cite-cited-prefix-regexp
181 "\\(^[^ \t\n<>]+>+[ \t]*\\|^[ \t]*$\\)"
182 "Regexp to match the citation prefix.
183 If match, mu-cite doesn't insert citation prefix."
187 (defcustom mu-cite-prefix-format '(prefix-register-verbose "> ")
188 "List to represent citation prefix.
189 Each elements must be a string or a method name."
199 :tag "Method or String"
203 (function (lambda (elem) (list 'choice-item (car elem))))
204 mu-cite-default-methods-alist)
205 '((symbol :tag "Method")
207 (choice-item :tag "String: \"> \"" "> ")
209 :set (function (lambda (symbol value)
210 (set-default symbol (delq nil value))))
213 (defcustom mu-cite-top-format '(in-id ">>>>>\t" from " wrote:\n")
214 "List to represent top string of citation.
215 Each elements must be a string or a method name."
224 :tag "Method or String"
228 (function (lambda (elem) (list 'choice-item (car elem))))
229 mu-cite-default-methods-alist)
230 '((symbol :tag "Method")
232 (choice-item :tag "String: \">>>>>\\t\"" ">>>>>\t")
233 (choice-item :tag "String: \" wrote:\\n\"" " wrote:\n")
234 (string :tag "String"))))))))
235 :set (function (lambda (symbol value)
236 (set-default symbol (delq nil value))))
243 (defcustom mu-cite-instantiation-hook nil
244 "List of functions called just before narrowing to the message."
248 (defcustom mu-cite-pre-cite-hook nil
249 "List of functions called before citing a region of text."
253 (defcustom mu-cite-post-cite-hook nil
254 "List of functions called after citing a region of text."
262 (defvar mu-cite-get-field-value-method-alist nil
263 "Alist major-mode vs. function to get field-body of header.")
265 (defun mu-cite-get-field-value (name)
266 "Return the value of the header field NAME.
267 If the field is not found in the header, a method function which is
268 registered in variable `mu-cite-get-field-value-method-alist' is called."
269 (or (std11-field-body name)
270 (let ((method (assq major-mode mu-cite-get-field-value-method-alist)))
272 (funcall (cdr method) name)))))
281 (defcustom mu-cite-ml-count-field-list
282 '("X-Ml-Count" "X-Mail-Count" "X-Seqno" "X-Sequence" "Mailinglist-Id")
283 "List of header fields which contains a sequence number of the mailing list."
284 :type '(repeat (choice :tag "Field Name"
285 (choice-item "X-Ml-Count")
286 (choice-item "X-Mail-Count")
287 (choice-item "X-Seqno")
288 (choice-item "X-Sequence")
289 (choice-item "Mailinglist-Id")
291 (string :tag "Other")))
292 :set (function (lambda (symbol value)
293 (set-default symbol (delq nil value))))
296 (defun mu-cite-get-ml-count-method ()
297 "A mu-cite method to return a ML-count.
298 This function searches a field about ML-count, which is specified by
299 the variable `mu-cite-ml-count-field-list', in a header.
300 If the field is found, the function returns a number part of the
303 Notice that please use (mu-cite-get-value 'ml-count)
304 instead of to call the function directly."
305 (let ((field-list mu-cite-ml-count-field-list))
308 (let* ((field (car field-list))
309 (ml-count (mu-cite-get-field-value field)))
310 (if (and ml-count (string-match "[0-9]+" ml-count))
311 (throw 'tag (match-string 0 ml-count)))
312 (setq field-list (cdr field-list)))))))
318 (defvar mu-cite-methods-alist nil)
320 (defun mu-cite-make-methods ()
321 (setq mu-cite-methods-alist
322 (copy-alist mu-cite-default-methods-alist))
323 (run-hooks 'mu-cite-instantiation-hook))
325 (defun mu-cite-get-value (item)
326 "Return a current value of ITEM."
327 (let ((ret (cdr (assoc item mu-cite-methods-alist))))
330 (setq ret (save-excursion (funcall ret)))
331 (set-alist 'mu-cite-methods-alist item ret))
334 (defun mu-cite-eval-format (list)
337 (cond ((stringp elt) elt)
338 ((symbolp elt) (mu-cite-get-value elt)))))
346 (defun mu-cite-original ()
347 "Citing filter function.
348 This is callable from the various mail and news readers' reply
349 function according to the agreed upon standard."
351 (mu-cite-make-methods)
353 (if (< (mark t) (point))
354 (exchange-point-and-mark))
355 (narrow-to-region (point)(point-max))
356 (run-hooks 'mu-cite-pre-cite-hook)
357 (let ((last-point (point))
358 (top (mu-cite-eval-format mu-cite-top-format))
359 (prefix (mu-cite-eval-format mu-cite-prefix-format)))
360 (if (re-search-forward "^-*$" nil nil)
363 (delete-region last-point (point))
365 (setq last-point (point))
366 (while (< (point)(mark t))
367 (or (and mu-cite-cited-prefix-regexp
368 (looking-at mu-cite-cited-prefix-regexp))
371 (goto-char last-point))
372 (run-hooks 'mu-cite-post-cite-hook)))
375 ;;; @ message editing utilities
378 (defcustom citation-mark-chars ">}|"
379 "String of characters for citation delimiter."
383 (defcustom citation-disable-chars "<{"
384 "String of characters not allowed as citation-prefix."
389 ;; Force redefine the function `char-category' which may have been
390 ;; defined by emu.el. They can be distinguished by the number of
391 ;; arguments. Anyway, that is the best way not to use emu.el.
392 (if (and (fboundp 'char-category)
393 (subrp (symbol-function 'char-category)))
395 (fmakunbound 'char-category)
396 (defun-maybe-cond char-category (character &optional table)
397 "Return a string of category mnemonics for CHAR in TABLE.
398 CHAR can be any multilingual characters,
399 TABLE defaults to the current buffer's category table."
400 ((and (subr-fboundp 'char-category-set)
401 (subr-fboundp 'category-set-mnemonics))
402 (category-set-mnemonics (char-category-set character)))
403 ((and (fboundp 'char-category-list)
404 ;; `char-category-list' returns a list of characters
405 ;; in XEmacs 21.2.25 and later, otherwise integers.
406 (characterp (car-safe (char-category-list ?a))))
407 (concat (char-category-list character)))
408 ((fboundp 'char-category-list)
409 (mapconcat (lambda (chr)
410 (char-to-string (int-char chr)))
411 (char-category-list character)
414 (if (< (char-int character) 128)
418 (if (< (char-int character) 128)
422 (defun detect-paragraph-cited-prefix ()
424 (goto-char (point-min))
427 (buffer-substring (line-beginning-position)
428 (line-end-position))))
431 (while (and (= (forward-line) 0)
432 (setq str (buffer-substring
433 (progn (beginning-of-line)(point))
434 (progn (end-of-line)(point))))
435 (setq ret (string-compare-from-top prefix str)))
440 (or (string-equal init prefix)
442 (cond ((> i 1) prefix)
444 (goto-char (point-min))
446 (narrow-to-region (point)
447 (+ (point)(length prefix)))
448 (goto-char (point-max))
449 (if (re-search-backward
450 (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 (point-min)(point)))
458 (goto-char (point-max))
460 (concat "[" citation-disable-chars "]") nil t)
462 (concat "[" citation-mark-chars "]") nil t))
463 (goto-char (match-end 0))
464 (if (looking-at "[ \t]+")
465 (goto-char (match-end 0)))
466 (buffer-substring (line-beginning-position)(point)))
469 (defcustom fill-column-for-fill-cited-region nil
470 "Integer to override `fill-column' while `fill-cited-region' is being
471 executed. If you wish people call you ****-san, you may set the value
472 of `fill-column' to 60 in the buffer for message sending and set this
474 :type (` (choice (const :tag "Off" nil)
475 (integer (, default-fill-column))))
479 (defun fill-cited-region (beg end)
480 "Fill each of the paragraphs in the region as a cited text."
485 (and (search-backward "\n" nil t)
486 (setq end (match-end 0)))
487 (narrow-to-region beg end)
488 (let* ((fill-prefix (detect-paragraph-cited-prefix))
489 (fill-column (max (+ 1 (current-left-margin)
490 (string-width fill-prefix))
491 (or fill-column-for-fill-cited-region
492 (current-fill-column))))
493 (pat (concat fill-prefix "\n"))
495 (goto-char (point-min))
496 (while (search-forward pat nil t)
497 (let ((b (match-beginning 0))
500 (if (and (> b (point-min))
501 (let ((cat (char-category
503 (or (string-match "a" cat)
504 (string-match "l" cat))))
506 (goto-char (point-min))
507 (fill-region (point-min) (point-max))))))
510 (defun compress-cited-prefix ()
511 "Compress nested cited prefixes."
514 (goto-char (point-min))
516 (concat "^" (regexp-quote mail-header-separator) "$") nil t)
517 (while (re-search-forward
518 (concat "^\\([ \t]*[^ \t\n" citation-mark-chars "]*["
519 citation-mark-chars "]\\)+") nil t)
520 (let* ((b (match-beginning 0))
522 (prefix (buffer-substring b e))
525 (if (string-match "<[^<>]+>" prefix)
527 (substring prefix 0 (match-beginning 0))))
529 (concat "\\([" citation-mark-chars "]+\\)[ \t]*")
531 (setq i (+ i (- (match-end 1)(match-beginning 1)))
533 pe (match-beginning 1)
536 (if (and ps (< ps pe))
539 (insert (concat (substring prefix ps pe)
540 (make-string nest ?>)))))
543 (defun replace-top-string (old new)
544 (interactive "*sOld string: \nsNew string: ")
545 (while (re-search-forward
546 (concat "^" (regexp-quote old)) nil t)
547 (replace-match new)))
549 (defun string-compare-from-top (str1 str2)
550 (let* ((len1 (length str1))
552 (len (min len1 len2))
555 (while (and (< p len)
557 (setq c1 (sref str1 p)
560 (setq p (char-next-index c1 p)))
562 (let ((matched (substring str1 0 p))
563 (r1 (and (< p len1)(substring str1 p)))
564 (r2 (and (< p len2)(substring str2 p))))
567 (list 'seq matched (list 'or r1 r2)))))))
575 (run-hooks 'mu-cite-load-hook)
577 ;;; mu-cite.el ends here