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 ;; We have need to pickup the function `char-category' for XEmacs which
47 ;; is defined in `emu'. It requires `poem' recursively for picking up
48 ;; the macros `char-next-index', `with-temp-buffer', etc.
55 (autoload 'mu-cite-get-prefix-method "mu-register")
56 (autoload 'mu-cite-get-prefix-register-method "mu-register")
57 (autoload 'mu-cite-get-prefix-register-verbose-method "mu-register")
59 (autoload 'mu-bbdb-get-prefix-method "mu-bbdb")
60 (autoload 'mu-bbdb-get-prefix-register-method "mu-bbdb")
61 (autoload 'mu-bbdb-get-prefix-register-verbose-method "mu-bbdb")
67 (defconst mu-cite-version "8.1")
73 (defmacro mu-cite-remove-text-properties (string)
74 "Remove text properties from STRING which is read from minibuffer."
75 (if (or (featurep 'xemacs)
76 (boundp 'minibuffer-allow-text-properties);; Emacs 20.1 or later.
77 (not (fboundp 'set-text-properties)));; under Emacs 19.7.
79 (` (let ((obj (copy-sequence (, string))))
80 (set-text-properties 0 (length obj) nil obj)
88 "Yet another citation tool for GNU Emacs."
93 (defvar mu-cite-default-methods-alist
97 (mu-cite-get-field-value "From"))))
101 (mu-cite-get-field-value "Date"))))
105 (mu-cite-get-field-value "Message-Id"))))
109 (mu-cite-get-field-value "Subject"))))
113 (mu-cite-get-field-value "X-Ml-Name"))))
114 (cons 'ml-count (function mu-cite-get-ml-count-method))
115 (cons 'address-structure
119 (std11-parse-address-string (mu-cite-get-value 'from))))))
123 (std11-full-name-string
124 (mu-cite-get-value 'address-structure)))))
128 (std11-address-string
129 (mu-cite-get-value 'address-structure)))))
133 (let ((ml-name (mu-cite-get-value 'ml-name))
134 (ml-count (mu-cite-get-value 'ml-count)))
139 (concat " : No." ml-count))
141 (mu-cite-get-value 'message-id))))))
145 (let ((id (mu-cite-get-value 'id)))
147 (format ">>>>> In %s \n" id)
152 (mu-cite-get-field-value "X-Attribution"))))
154 (cons 'prefix (function mu-cite-get-prefix-method))
155 (cons 'prefix-register
156 (function mu-cite-get-prefix-register-method))
157 (cons 'prefix-register-verbose
158 (function mu-cite-get-prefix-register-verbose-method))
161 (function mu-bbdb-get-prefix-method))
162 (cons 'bbdb-prefix-register
163 (function mu-bbdb-get-prefix-register-method))
164 (cons 'bbdb-prefix-register-verbose
165 (function mu-bbdb-get-prefix-register-verbose-method))
172 (defcustom mu-cite-cited-prefix-regexp
173 "\\(^[^ \t\n<>]+>+[ \t]*\\|^[ \t]*$\\)"
174 "Regexp to match the citation prefix.
175 If match, mu-cite doesn't insert citation prefix."
179 (defcustom mu-cite-prefix-format '(prefix-register-verbose "> ")
180 "List to represent citation prefix.
181 Each elements must be a string or a method name."
191 :tag "Method or String"
195 (function (lambda (elem) (list 'choice-item (car elem))))
196 mu-cite-default-methods-alist)
197 '((symbol :tag "Method")
199 (choice-item :tag "String: \"> \"" "> ")
201 :set (function (lambda (symbol value)
202 (set-default symbol (delq nil value))))
205 (defcustom mu-cite-top-format '(in-id ">>>>>\t" from " wrote:\n")
206 "List to represent top string of citation.
207 Each elements must be a string or a method name."
216 :tag "Method or String"
220 (function (lambda (elem) (list 'choice-item (car elem))))
221 mu-cite-default-methods-alist)
222 '((symbol :tag "Method")
224 (choice-item :tag "String: \">>>>>\\t\"" ">>>>>\t")
225 (choice-item :tag "String: \" wrote:\\n\"" " wrote:\n")
226 (string :tag "String"))))))))
227 :set (function (lambda (symbol value)
228 (set-default symbol (delq nil value))))
235 (defcustom mu-cite-instantiation-hook nil
236 "List of functions called just before narrowing to the message."
240 (defcustom mu-cite-pre-cite-hook nil
241 "List of functions called before citing a region of text."
245 (defcustom mu-cite-post-cite-hook nil
246 "List of functions called after citing a region of text."
254 (defvar mu-cite-get-field-value-method-alist nil
255 "Alist major-mode vs. function to get field-body of header.")
257 (defun mu-cite-get-field-value (name)
258 "Return the value of the header field NAME.
259 If the field is not found in the header, a method function which is
260 registered in variable `mu-cite-get-field-value-method-alist' is called."
261 (or (std11-field-body name)
262 (let ((method (assq major-mode mu-cite-get-field-value-method-alist)))
264 (funcall (cdr method) name)))))
273 (defcustom mu-cite-ml-count-field-list
274 '("X-Ml-Count" "X-Mail-Count" "X-Seqno" "X-Sequence" "Mailinglist-Id")
275 "List of header fields which contains a sequence number of the mailing list."
276 :type '(repeat (choice :tag "Field Name"
277 (choice-item "X-Ml-Count")
278 (choice-item "X-Mail-Count")
279 (choice-item "X-Seqno")
280 (choice-item "X-Sequence")
281 (choice-item "Mailinglist-Id")
283 (string :tag "Other")))
284 :set (function (lambda (symbol value)
285 (set-default symbol (delq nil value))))
288 (defun mu-cite-get-ml-count-method ()
289 "A mu-cite method to return a ML-count.
290 This function searches a field about ML-count, which is specified by
291 the variable `mu-cite-ml-count-field-list', in a header.
292 If the field is found, the function returns a number part of the
295 Notice that please use (mu-cite-get-value 'ml-count)
296 instead of to call the function directly."
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 (if (and ml-count (string-match "[0-9]+" ml-count))
303 (throw 'tag (match-string 0 ml-count)))
304 (setq field-list (cdr field-list)))))))
310 (defvar mu-cite-methods-alist nil)
312 (defun mu-cite-make-methods ()
313 (setq mu-cite-methods-alist
314 (copy-alist mu-cite-default-methods-alist))
315 (run-hooks 'mu-cite-instantiation-hook))
317 (defun mu-cite-get-value (item)
318 "Return a current value of ITEM."
319 (let ((ret (cdr (assoc item mu-cite-methods-alist))))
322 (setq ret (save-excursion (funcall ret)))
323 (set-alist 'mu-cite-methods-alist item ret))
326 (defun mu-cite-eval-format (list)
329 (cond ((stringp elt) elt)
330 ((symbolp elt) (mu-cite-get-value elt)))))
338 (defun mu-cite-original ()
339 "Citing filter function.
340 This is callable from the various mail and news readers' reply
341 function according to the agreed upon standard."
343 (mu-cite-make-methods)
345 (if (< (mark t) (point))
346 (exchange-point-and-mark))
347 (narrow-to-region (point)(point-max))
348 (run-hooks 'mu-cite-pre-cite-hook)
349 (let ((last-point (point))
350 (top (mu-cite-eval-format mu-cite-top-format))
351 (prefix (mu-cite-eval-format mu-cite-prefix-format)))
352 (if (re-search-forward "^-*$" nil nil)
355 (delete-region last-point (point))
357 (setq last-point (point))
358 (while (< (point)(mark t))
359 (or (looking-at mu-cite-cited-prefix-regexp)
362 (goto-char last-point))
363 (run-hooks 'mu-cite-post-cite-hook)))
366 ;;; @ message editing utilities
369 (defcustom citation-mark-chars ">}|"
370 "String of characters for citation delimiter."
374 (defcustom citation-disable-chars "<{"
375 "String of characters not allowed as citation-prefix."
379 (defun-maybe-cond char-category (character)
380 "Return a string of category mnemonics for CHAR in TABLE.
381 CHAR can be any multilingual character,
382 TABLE defaults to the current buffer's category table."
383 ((and (subr-fboundp 'char-category-set)
384 (subr-fboundp 'category-set-mnemonics))
385 (category-set-mnemonics (char-category-set character))
387 ((fboundp 'char-category-list)
388 (mapconcat (lambda (chr)
389 (char-to-string (int-char chr)))
390 (char-category-list character)
394 (if (< (char-int character) 128)
399 (if (< (char-int character) 128)
404 (defun detect-paragraph-cited-prefix ()
406 (goto-char (point-min))
409 (buffer-substring (line-beginning-position)
410 (line-end-position))))
413 (while (and (= (forward-line) 0)
414 (setq str (buffer-substring
415 (progn (beginning-of-line)(point))
416 (progn (end-of-line)(point))))
417 (setq ret (string-compare-from-top prefix str)))
422 (or (string-equal init prefix)
424 (cond ((> i 1) prefix)
426 (goto-char (point-min))
428 (narrow-to-region (point)
429 (+ (point)(length prefix)))
430 (goto-char (point-max))
431 (if (re-search-backward
432 (concat "[" citation-mark-chars "]") nil t)
434 (goto-char (match-end 0))
435 (if (looking-at "[ \t]+")
436 (goto-char (match-end 0)))
437 (buffer-substring (point-min)(point)))
440 (goto-char (point-max))
442 (concat "[" citation-disable-chars "]") nil t)
444 (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 (line-beginning-position)(point)))
452 (defun fill-cited-region (beg end)
453 "Fill each of the paragraphs in the region as a cited text."
458 (and (search-backward "\n" nil t)
459 (setq end (match-end 0)))
460 (narrow-to-region beg end)
461 (let* ((fill-prefix (detect-paragraph-cited-prefix))
462 (fill-column (max (+ 1 (current-left-margin)
463 (string-width fill-prefix))
464 (current-fill-column)))
465 (pat (concat fill-prefix "\n"))
467 (goto-char (point-min))
468 (while (search-forward pat nil t)
469 (let ((b (match-beginning 0))
472 (if (and (> b (point-min))
473 (let ((cat (char-category
475 (or (string-match "a" cat)
476 (string-match "l" cat))))
478 (goto-char (point-min))
479 (fill-region (point-min) (point-max))))))
482 (defun compress-cited-prefix ()
483 "Compress nested cited prefixes."
486 (goto-char (point-min))
488 (concat "^" (regexp-quote mail-header-separator) "$") nil t)
489 (while (re-search-forward
490 (concat "^\\([ \t]*[^ \t\n" citation-mark-chars "]*["
491 citation-mark-chars "]\\)+") nil t)
492 (let* ((b (match-beginning 0))
494 (prefix (buffer-substring b e))
497 (if (string-match "<[^<>]+>" prefix)
499 (substring prefix 0 (match-beginning 0))))
501 (concat "\\([" citation-mark-chars "]+\\)[ \t]*")
503 (setq i (+ i (- (match-end 1)(match-beginning 1)))
505 pe (match-beginning 1)
508 (if (and ps (< ps pe))
511 (insert (concat (substring prefix ps pe)
512 (make-string nest ?>)))))
515 (defun replace-top-string (old new)
516 (interactive "*sOld string: \nsNew string: ")
517 (while (re-search-forward
518 (concat "^" (regexp-quote old)) nil t)
519 (replace-match new)))
521 (defun string-compare-from-top (str1 str2)
522 (let* ((len1 (length str1))
524 (len (min len1 len2))
527 (while (and (< p len)
529 (setq c1 (sref str1 p)
532 (setq p (char-next-index c1 p)))
534 (let ((matched (substring str1 0 p))
535 (r1 (and (< p len1)(substring str1 p)))
536 (r2 (and (< p len2)(substring str2 p))))
539 (list 'seq matched (list 'or r1 r2)))))))
547 (run-hooks 'mu-cite-load-hook)
549 ;;; mu-cite.el ends here