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@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 ;; Pickup some macros, e.g. `with-temp-buffer', for old Emacsen.
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."
377 (defun-maybe-cond char-category (character)
378 "Return a string of category mnemonics for CHAR in TABLE.
379 CHAR can be any multilingual character,
380 TABLE defaults to the current buffer's category table."
381 ((and (subr-fboundp 'char-category-set)
382 (subr-fboundp 'category-set-mnemonics))
383 (category-set-mnemonics (char-category-set character))
385 ((fboundp 'char-category-list)
386 (mapconcat (lambda (chr)
387 (char-to-string (int-char chr)))
388 (char-category-list character)
392 (if (< (char-int character) 128)
397 (if (< (char-int character) 128)
402 (defun detect-paragraph-cited-prefix ()
404 (goto-char (point-min))
407 (buffer-substring (line-beginning-position)
408 (line-end-position))))
411 (while (and (= (forward-line) 0)
412 (setq str (buffer-substring
413 (progn (beginning-of-line)(point))
414 (progn (end-of-line)(point))))
415 (setq ret (string-compare-from-top prefix str)))
420 (or (string-equal init prefix)
422 (cond ((> i 1) prefix)
424 (goto-char (point-min))
426 (narrow-to-region (point)
427 (+ (point)(length prefix)))
428 (goto-char (point-max))
429 (if (re-search-backward
430 (concat "[" citation-mark-chars "]") nil t)
432 (goto-char (match-end 0))
433 (if (looking-at "[ \t]+")
434 (goto-char (match-end 0)))
435 (buffer-substring (point-min)(point)))
438 (goto-char (point-max))
440 (concat "[" citation-disable-chars "]") nil t)
442 (concat "[" citation-mark-chars "]") nil t))
443 (goto-char (match-end 0))
444 (if (looking-at "[ \t]+")
445 (goto-char (match-end 0)))
446 (buffer-substring (line-beginning-position)(point)))
450 (defun fill-cited-region (beg end)
451 "Fill each of the paragraphs in the region as a cited text."
456 (and (search-backward "\n" nil t)
457 (setq end (match-end 0)))
458 (narrow-to-region beg end)
459 (let* ((fill-prefix (detect-paragraph-cited-prefix))
460 (fill-column (max (+ 1 (current-left-margin)
461 (string-width fill-prefix))
462 (current-fill-column)))
463 (pat (concat fill-prefix "\n"))
465 (goto-char (point-min))
466 (while (search-forward pat nil t)
467 (let ((b (match-beginning 0))
470 (if (and (> b (point-min))
471 (let ((cat (char-category
473 (or (string-match "a" cat)
474 (string-match "l" cat))))
476 (goto-char (point-min))
477 (fill-region (point-min) (point-max))))))
480 (defun compress-cited-prefix ()
481 "Compress nested cited prefixes."
484 (goto-char (point-min))
486 (concat "^" (regexp-quote mail-header-separator) "$") nil t)
487 (while (re-search-forward
488 (concat "^\\([ \t]*[^ \t\n" citation-mark-chars "]*["
489 citation-mark-chars "]\\)+") nil t)
490 (let* ((b (match-beginning 0))
492 (prefix (buffer-substring b e))
495 (if (string-match "<[^<>]+>" prefix)
497 (substring prefix 0 (match-beginning 0))))
499 (concat "\\([" citation-mark-chars "]+\\)[ \t]*")
501 (setq i (+ i (- (match-end 1)(match-beginning 1)))
503 pe (match-beginning 1)
506 (if (and ps (< ps pe))
509 (insert (concat (substring prefix ps pe)
510 (make-string nest ?>)))))
513 (defun replace-top-string (old new)
514 (interactive "*sOld string: \nsNew string: ")
515 (while (re-search-forward
516 (concat "^" (regexp-quote old)) nil t)
517 (replace-match new)))
519 (defun string-compare-from-top (str1 str2)
520 (let* ((len1 (length str1))
522 (len (min len1 len2))
525 (while (and (< p len)
527 (setq c1 (sref str1 p)
530 (setq p (char-next-index c1 p)))
532 (let ((matched (substring str1 0 p))
533 (r1 (and (< p len1)(substring str1 p)))
534 (r2 (and (< p len2)(substring str2 p))))
537 (list 'seq matched (list 'or r1 r2)))))))
545 (run-hooks 'mu-cite-load-hook)
547 ;;; mu-cite.el ends here