1 ;;; mu-cite.el --- yet another citation tool for GNU Emacs
2 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2005, 2007
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., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, 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 ;; Don't use the function `char-category' which may have been
390 ;; defined by emu.el. Anyway, the best way is not to use emu.el.
391 (if (and (fboundp 'char-category)
392 (subrp (symbol-function 'char-category)))
393 (defalias 'mu-cite-char-category 'char-category)
394 (defun-maybe-cond mu-cite-char-category (character &optional table)
395 "Return a string of category mnemonics for CHARACTER in TABLE.
396 CHARACTER can be any multilingual characters,
397 TABLE defaults to the current buffer's category table (it is currently
399 ((and (subr-fboundp 'char-category-set)
400 (subr-fboundp 'category-set-mnemonics))
401 (category-set-mnemonics (char-category-set character)))
402 ((and (fboundp 'char-category-list)
403 ;; `char-category-list' returns a list of characters
404 ;; in XEmacs 21.2.25 and later, otherwise integers.
405 (characterp (car-safe (char-category-list ?a))))
406 (concat (char-category-list character)))
407 ((fboundp 'char-category-list)
408 (mapconcat (lambda (chr)
409 (char-to-string (int-char chr)))
410 (char-category-list character)
413 (if (< (char-int character) 128)
417 (if (< (char-int character) 128)
421 (defun detect-paragraph-cited-prefix ()
423 (goto-char (point-min))
426 (buffer-substring (line-beginning-position)
427 (line-end-position))))
430 (while (and (= (forward-line) 0)
431 (setq str (buffer-substring
432 (progn (beginning-of-line)(point))
433 (progn (end-of-line)(point))))
434 (setq ret (string-compare-from-top prefix str)))
439 (or (string-equal init prefix)
441 (cond ((> i 1) prefix)
443 (goto-char (point-min))
445 (narrow-to-region (point)
446 (+ (point)(length prefix)))
447 (goto-char (point-max))
448 (if (re-search-backward
449 (concat "[" citation-mark-chars "]") nil t)
451 (goto-char (match-end 0))
452 (if (looking-at "[ \t]+")
453 (goto-char (match-end 0)))
454 (buffer-substring (point-min)(point)))
457 (goto-char (point-max))
459 (concat "[" citation-disable-chars "]") nil t)
461 (concat "[" citation-mark-chars "]") nil t))
462 (goto-char (match-end 0))
463 (if (looking-at "[ \t]+")
464 (goto-char (match-end 0)))
465 (buffer-substring (line-beginning-position)(point)))
468 (defcustom fill-column-for-fill-cited-region nil
469 "Integer to override `fill-column' while `fill-cited-region' is being
470 executed. If you wish people call you ****-san, you may set the value
471 of `fill-column' to 60 in the buffer for message sending and set this
473 :type `(choice (const :tag "Off" nil)
474 (integer ,default-fill-column))
478 (defun fill-cited-region (beg end)
479 "Fill each of the paragraphs in the region as a cited text."
484 (and (search-backward "\n" nil t)
485 (setq end (match-end 0)))
486 (narrow-to-region beg end)
487 (let* ((fill-prefix (detect-paragraph-cited-prefix))
488 (fill-column (max (+ 1 (current-left-margin)
489 (string-width fill-prefix))
490 (or fill-column-for-fill-cited-region
491 (current-fill-column))))
492 (pat (concat fill-prefix "\n"))
494 (goto-char (point-min))
495 (while (search-forward pat nil t)
496 (let ((b (match-beginning 0))
499 (if (and (> b (point-min))
500 (let ((cat (mu-cite-char-category (char-before b))))
501 (or (string-match "a" cat)
502 (string-match "l" cat))))
504 (goto-char (point-min))
505 (fill-region (point-min) (point-max))))))
508 (defun compress-cited-prefix ()
509 "Compress nested cited prefixes."
512 (goto-char (point-min))
514 (concat "^" (regexp-quote mail-header-separator) "$") nil t)
515 (while (re-search-forward
516 (concat "^\\([ \t]*[^ \t\n" citation-mark-chars "]*["
517 citation-mark-chars "]\\)+") nil t)
518 (let* ((b (match-beginning 0))
520 (prefix (buffer-substring b e))
523 (if (string-match "<[^<>]+>" prefix)
525 (substring prefix 0 (match-beginning 0))))
527 (concat "\\([" citation-mark-chars "]+\\)[ \t]*")
529 (setq i (+ i (- (match-end 1)(match-beginning 1)))
531 pe (match-beginning 1)
534 (if (and ps (< ps pe))
537 (insert (concat (substring prefix ps pe)
538 (make-string nest ?>)))))
541 (defun replace-top-string (old new)
542 (interactive "*sOld string: \nsNew string: ")
543 (while (re-search-forward
544 (concat "^" (regexp-quote old)) nil t)
545 (replace-match new)))
547 (defun string-compare-from-top (str1 str2)
548 (let* ((len1 (length str1))
550 (len (min len1 len2))
553 (while (and (< p len)
555 (setq c1 (sref str1 p)
558 (setq p (char-next-index c1 p)))
560 (let ((matched (substring str1 0 p))
561 (r1 (and (< p len1)(substring str1 p)))
562 (r2 (and (< p len2)(substring str2 p))))
565 (list 'seq matched (list 'or r1 r2)))))))
573 (run-hooks 'mu-cite-load-hook)
575 ;;; mu-cite.el ends here