;; Applied patch from NISHIDA Masakazu <m_nisida@ca2.so-net.ne.jp>.
[elisp/mu-cite.git] / mu-cite.el
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.
4
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
9
10 ;; This file is part of MU (Message Utilities).
11
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.
16
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.
21
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.
26
27 ;;; Commentary:
28
29 ;; - How to use
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))
38 ;;      For EMACS 18
39 ;;              ;; for all but mh-e
40 ;;              (add-hook 'mail-yank-hooks (function mu-cite-original))
41 ;;              ;; for mh-e only
42 ;;              (add-hook 'mh-yank-hooks (function mu-cite-original))
43
44 ;;; Code:
45
46 ;; For picking up the macros `char-next-index', `with-temp-buffer', etc.
47 (require 'poem)
48
49 (require 'pcustom)
50 (require 'std11)
51 (require 'alist)
52
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")
56
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")
60
61
62 ;;; @ version
63 ;;;
64
65 (defconst mu-cite-version "8.1")
66
67
68 ;;; @ macro
69 ;;;
70
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.
76       string
77     (` (let ((obj (copy-sequence (, string))))
78          (set-text-properties 0 (length obj) nil obj)
79          obj))))
80
81
82 ;;; @ set up
83 ;;;
84
85 (defgroup mu-cite nil
86   "Yet another citation tool for GNU Emacs."
87   :prefix "mu-cite-"
88   :group 'mail
89   :group 'news)
90
91 (defvar mu-cite-default-methods-alist
92   (list (cons 'from
93               (function
94                (lambda ()
95                  (mu-cite-get-field-value "From"))))
96         (cons 'date
97               (function
98                (lambda ()
99                  (mu-cite-get-field-value "Date"))))
100         (cons 'message-id
101               (function
102                (lambda ()
103                  (mu-cite-get-field-value "Message-Id"))))
104         (cons 'subject
105               (function
106                (lambda ()
107                  (mu-cite-get-field-value "Subject"))))
108         (cons 'ml-name
109               (function
110                (lambda ()
111                  (mu-cite-get-field-value "X-Ml-Name"))))
112         (cons 'ml-count (function mu-cite-get-ml-count-method))
113         (cons 'address-structure
114               (function
115                (lambda ()
116                  (car
117                   (std11-parse-address-string (mu-cite-get-value 'from))))))
118         (cons 'full-name
119               (function
120                (lambda ()
121                  (std11-full-name-string
122                   (mu-cite-get-value 'address-structure)))))
123         (cons 'address
124               (function
125                (lambda ()
126                  (std11-address-string
127                   (mu-cite-get-value 'address-structure)))))
128         (cons 'id
129               (function
130                (lambda ()
131                  (let ((ml-name (mu-cite-get-value 'ml-name))
132                        (ml-count (mu-cite-get-value 'ml-count)))
133                    (if ml-name
134                        (concat "["
135                                ml-name
136                                (if ml-count
137                                    (concat " : No." ml-count))
138                                "]")
139                      (mu-cite-get-value 'message-id))))))
140         (cons 'in-id
141               (function
142                (lambda ()
143                  (let ((id (mu-cite-get-value 'id)))
144                    (if id
145                        (format ">>>>> In %s \n" id)
146                      "")))))
147         (cons 'x-attribution
148               (function
149                (lambda ()
150                  (mu-cite-get-field-value "X-Attribution"))))
151         (cons 'x-cite-me
152               (function
153                (lambda ()
154                  (mu-cite-get-field-value "X-Cite-Me"))))
155         ;; mu-register
156         (cons 'prefix (function mu-cite-get-prefix-method))
157         (cons 'prefix-register
158               (function mu-cite-get-prefix-register-method))
159         (cons 'prefix-register-verbose
160               (function mu-cite-get-prefix-register-verbose-method))
161         ;; mu-bbdb
162         (cons 'bbdb-prefix
163               (function mu-bbdb-get-prefix-method))
164         (cons 'bbdb-prefix-register
165               (function mu-bbdb-get-prefix-register-method))
166         (cons 'bbdb-prefix-register-verbose
167               (function mu-bbdb-get-prefix-register-verbose-method))
168         ))
169
170
171 ;;; @ formats
172 ;;;
173
174 (defcustom mu-cite-cited-prefix-regexp
175   "\\(^[^ \t\n<>]+>+[ \t]*\\|^[ \t]*$\\)"
176   "Regexp to match the citation prefix.
177 If match, mu-cite doesn't insert citation prefix."
178   :type 'regexp
179   :group 'mu-cite)
180
181 (defcustom mu-cite-prefix-format '(prefix-register-verbose "> ")
182   "List to represent citation prefix.
183 Each elements must be a string or a method name."
184   :type (list
185          'repeat
186          (list
187           'group
188           :convert-widget
189           (function
190            (lambda (widget)
191              (list
192               'choice
193               :tag "Method or String"
194               :args
195               (nconc
196                (mapcar
197                 (function (lambda (elem) (list 'choice-item (car elem))))
198                 mu-cite-default-methods-alist)
199                '((symbol :tag "Method")
200                  (const :tag "-" nil)
201                  (choice-item :tag "String: \"> \"" "> ")
202                  (string))))))))
203   :set (function (lambda (symbol value)
204                    (set-default symbol (delq nil value))))
205   :group 'mu-cite)
206
207 (defcustom mu-cite-top-format '(in-id ">>>>>\t" from " wrote:\n")
208   "List to represent top string of citation.
209 Each elements must be a string or a method name."
210   :type (list
211          'repeat
212          (list
213           'group
214           :convert-widget
215           (function
216            (lambda (widget)
217              (list 'choice
218                    :tag "Method or String"
219                    :args
220                    (nconc
221                     (mapcar
222                      (function (lambda (elem) (list 'choice-item (car elem))))
223                      mu-cite-default-methods-alist)
224                     '((symbol :tag "Method")
225                       (const :tag "-" nil)
226                       (choice-item :tag "String: \">>>>>\\t\"" ">>>>>\t")
227                       (choice-item :tag "String: \" wrote:\\n\"" " wrote:\n")
228                       (string :tag "String"))))))))
229   :set (function (lambda (symbol value)
230                    (set-default symbol (delq nil value))))
231   :group 'mu-cite)
232
233
234 ;;; @ hooks
235 ;;;
236
237 (defcustom mu-cite-instantiation-hook nil
238   "List of functions called just before narrowing to the message."
239   :type 'hook
240   :group 'mu-cite)
241
242 (defcustom mu-cite-pre-cite-hook nil
243   "List of functions called before citing a region of text."
244   :type 'hook
245   :group 'mu-cite)
246
247 (defcustom mu-cite-post-cite-hook nil
248   "List of functions called after citing a region of text."
249   :type 'hook
250   :group 'mu-cite)
251
252
253 ;;; @ field
254 ;;;
255
256 (defvar mu-cite-get-field-value-method-alist nil
257   "Alist major-mode vs. function to get field-body of header.")
258
259 (defun mu-cite-get-field-value (name)
260   "Return the value of the header field NAME.
261 If the field is not found in the header, a method function which is
262 registered in variable `mu-cite-get-field-value-method-alist' is called."
263   (or (std11-field-body name)
264       (let ((method (assq major-mode mu-cite-get-field-value-method-alist)))
265         (if method
266             (funcall (cdr method) name)))))
267
268
269 ;;; @ item methods
270 ;;;
271
272 ;;; @@ ML count
273 ;;;
274
275 (defcustom mu-cite-ml-count-field-list
276   '("X-Ml-Count" "X-Mail-Count" "X-Seqno" "X-Sequence" "Mailinglist-Id")
277   "List of header fields which contains a sequence number of the mailing list."
278   :type '(repeat (choice :tag "Field Name"
279                          (choice-item "X-Ml-Count")
280                          (choice-item "X-Mail-Count")
281                          (choice-item "X-Seqno")
282                          (choice-item "X-Sequence")
283                          (choice-item "Mailinglist-Id")
284                          (const :tag "-" nil)
285                          (string :tag "Other")))
286   :set (function (lambda (symbol value)
287                    (set-default symbol (delq nil value))))
288   :group 'mu-cite)
289
290 (defun mu-cite-get-ml-count-method ()
291   "A mu-cite method to return a ML-count.
292 This function searches a field about ML-count, which is specified by
293 the variable `mu-cite-ml-count-field-list', in a header.
294 If the field is found, the function returns a number part of the
295 field.
296
297 Notice that please use (mu-cite-get-value 'ml-count)
298 instead of to call the function directly."
299   (let ((field-list mu-cite-ml-count-field-list))
300     (catch 'tag
301       (while field-list
302         (let* ((field (car field-list))
303                (ml-count (mu-cite-get-field-value field)))
304           (if (and ml-count (string-match "[0-9]+" ml-count))
305               (throw 'tag (match-string 0 ml-count)))
306           (setq field-list (cdr field-list)))))))
307
308
309 ;;; @ fundamentals
310 ;;;
311
312 (defvar mu-cite-methods-alist nil)
313
314 (defun mu-cite-make-methods ()
315   (setq mu-cite-methods-alist
316         (copy-alist mu-cite-default-methods-alist))
317   (run-hooks 'mu-cite-instantiation-hook))
318
319 (defun mu-cite-get-value (item)
320   "Return a current value of ITEM."
321   (let ((ret (cdr (assoc item mu-cite-methods-alist))))
322     (if (functionp ret)
323         (prog1
324             (setq ret (save-excursion (funcall ret)))
325           (set-alist 'mu-cite-methods-alist item ret))
326       ret)))
327
328 (defun mu-cite-eval-format (list)
329   (mapconcat (function
330               (lambda (elt)
331                 (cond ((stringp elt) elt)
332                       ((symbolp elt) (mu-cite-get-value elt)))))
333              list ""))
334
335
336 ;;; @ main function
337 ;;;
338
339 ;;;###autoload
340 (defun mu-cite-original ()
341   "Citing filter function.
342 This is callable from the various mail and news readers' reply
343 function according to the agreed upon standard."
344   (interactive)
345   (mu-cite-make-methods)
346   (save-restriction
347     (if (< (mark t) (point))
348         (exchange-point-and-mark))
349     (narrow-to-region (point)(point-max))
350     (run-hooks 'mu-cite-pre-cite-hook)
351     (let ((last-point (point))
352           (top (mu-cite-eval-format mu-cite-top-format))
353           (prefix (mu-cite-eval-format mu-cite-prefix-format)))
354       (if (re-search-forward "^-*$" nil nil)
355           (forward-line 1))
356       (widen)
357       (delete-region last-point (point))
358       (insert top)
359       (setq last-point (point))
360       (while (< (point)(mark t))
361         (or (looking-at mu-cite-cited-prefix-regexp)
362             (insert prefix))
363         (forward-line 1))
364       (goto-char last-point))
365     (run-hooks 'mu-cite-post-cite-hook)))
366
367
368 ;;; @ message editing utilities
369 ;;;
370
371 (defcustom citation-mark-chars ">}|"
372   "String of characters for citation delimiter."
373   :type 'string
374   :group 'mu-cite)
375
376 (defcustom citation-disable-chars "<{"
377   "String of characters not allowed as citation-prefix."
378   :type 'string
379   :group 'mu-cite)
380
381 (eval-and-compile
382   ;; Force redefine the function `char-category' which may have been
383   ;; defined by emu.el.  They can be distinguished by the number of
384   ;; arguments.  Anyway, that is the best way not to use emu.el.
385   (if (and (fboundp 'char-category)
386            (subrp (symbol-function 'char-category)))
387       nil
388     (fmakunbound 'char-category)
389     (defun-maybe-cond char-category (character &optional table)
390       "Return a string of category mnemonics for CHAR in TABLE.
391 CHAR can be any multilingual characters,
392 TABLE defaults to the current buffer's category table."
393       ((and (subr-fboundp 'char-category-set)
394             (subr-fboundp 'category-set-mnemonics))
395        (category-set-mnemonics (char-category-set character)))
396       ((and (fboundp 'char-category-list)
397             ;; `char-category-list' returns a list of characters
398             ;; in XEmacs 21.2.25 and later, otherwise integers.
399             (characterp (car-safe (char-category-list ?a))))
400        (concat (char-category-list character)))
401       ((fboundp 'char-category-list)
402        (mapconcat (lambda (chr)
403                     (char-to-string (int-char chr)))
404                   (char-category-list character)
405                   ""))
406       ((boundp 'NEMACS)
407        (if (< (char-int character) 128)
408            "al"
409          "j"))
410       (t
411        (if (< (char-int character) 128)
412            "al"
413          "l")))))
414
415 (defun detect-paragraph-cited-prefix ()
416   (save-excursion
417     (goto-char (point-min))
418     (let ((i 0)
419           (prefix
420            (buffer-substring (line-beginning-position)
421                              (line-end-position))))
422       (let ((init prefix)
423             str ret)
424         (while (and (= (forward-line) 0)
425                     (setq str (buffer-substring
426                                (progn (beginning-of-line)(point))
427                                (progn (end-of-line)(point))))
428                     (setq ret (string-compare-from-top prefix str)))
429           (setq prefix
430                 (if (stringp ret)
431                     ret
432                   (car (cdr ret))))
433           (or (string-equal init prefix)
434               (setq i (1+ i)))))
435       (cond ((> i 1) prefix)
436             ((> i 0)
437              (goto-char (point-min))
438              (save-restriction
439                (narrow-to-region (point)
440                                  (+ (point)(length prefix)))
441                (goto-char (point-max))
442                (if (re-search-backward
443                     (concat "[" citation-mark-chars "]") nil t)
444                    (progn
445                      (goto-char (match-end 0))
446                      (if (looking-at "[ \t]+")
447                          (goto-char (match-end 0)))
448                      (buffer-substring (point-min)(point)))
449                  prefix)))
450             ((progn
451                (goto-char (point-max))
452                (re-search-backward
453                 (concat "[" citation-disable-chars "]") nil t)
454                (re-search-backward
455                 (concat "[" citation-mark-chars "]") nil t))
456              (goto-char (match-end 0))
457              (if (looking-at "[ \t]+")
458                  (goto-char (match-end 0)))
459              (buffer-substring (line-beginning-position)(point)))
460             (t "")))))
461
462 (defcustom fill-column-for-fill-cited-region nil
463   "Integer to override `fill-column' while `fill-cited-region' is being
464 executed."
465   :type (` (choice (const :tag "Off" nil)
466                    (integer (, default-fill-column))))
467   :group 'mu-cite)
468
469 ;;;###autoload
470 (defun fill-cited-region (beg end)
471   "Fill each of the paragraphs in the region as a cited text."
472   (interactive "*r")
473   (save-excursion
474     (save-restriction
475       (goto-char end)
476       (and (search-backward "\n" nil t)
477            (setq end (match-end 0)))
478       (narrow-to-region beg end)
479       (let* ((fill-prefix (detect-paragraph-cited-prefix))
480              (fill-column (max (+ 1 (current-left-margin)
481                                   (string-width fill-prefix))
482                                (or fill-column-for-fill-cited-region
483                                    (current-fill-column))))
484              (pat (concat fill-prefix "\n"))
485              filladapt-mode)
486         (goto-char (point-min))
487         (while (search-forward pat nil t)
488           (let ((b (match-beginning 0))
489                 (e (match-end 0)))
490             (delete-region b e)
491             (if (and (> b (point-min))
492                      (let ((cat (char-category
493                                  (char-before b))))
494                        (or (string-match "a" cat)
495                            (string-match "l" cat))))
496                 (insert " "))))
497         (goto-char (point-min))
498         (fill-region (point-min) (point-max))))))
499
500 ;;;###autoload
501 (defun compress-cited-prefix ()
502   "Compress nested cited prefixes."
503   (interactive)
504   (save-excursion
505     (goto-char (point-min))
506     (re-search-forward
507      (concat "^" (regexp-quote mail-header-separator) "$") nil t)
508     (while (re-search-forward
509             (concat "^\\([ \t]*[^ \t\n" citation-mark-chars "]*["
510                     citation-mark-chars "]\\)+") nil t)
511       (let* ((b (match-beginning 0))
512              (e (match-end 0))
513              (prefix (buffer-substring b e))
514              ps pe (s 0)
515              (nest (let ((i 0))
516                      (if (string-match "<[^<>]+>" prefix)
517                          (setq prefix
518                                (substring prefix 0 (match-beginning 0))))
519                      (while (string-match
520                              (concat "\\([" citation-mark-chars "]+\\)[ \t]*")
521                              prefix s)
522                        (setq i (+ i (- (match-end 1)(match-beginning 1)))
523                              ps s
524                              pe (match-beginning 1)
525                              s (match-end 0)))
526                      i)))
527         (if (and ps (< ps pe))
528             (progn
529               (delete-region b e)
530               (insert (concat (substring prefix ps pe)
531                               (make-string nest ?>)))))
532         ))))
533
534 (defun replace-top-string (old new)
535   (interactive "*sOld string: \nsNew string: ")
536   (while (re-search-forward
537           (concat "^" (regexp-quote old)) nil t)
538     (replace-match new)))
539
540 (defun string-compare-from-top (str1 str2)
541   (let* ((len1 (length str1))
542          (len2 (length str2))
543          (len (min len1 len2))
544          (p 0)
545          c1 c2)
546     (while (and (< p len)
547                 (progn
548                   (setq c1 (sref str1 p)
549                         c2 (sref str2 p))
550                   (eq c1 c2)))
551       (setq p (char-next-index c1 p)))
552     (and (> p 0)
553          (let ((matched (substring str1 0 p))
554                (r1 (and (< p len1)(substring str1 p)))
555                (r2 (and (< p len2)(substring str2 p))))
556            (if (eq r1 r2)
557                matched
558              (list 'seq matched (list 'or r1 r2)))))))
559
560
561 ;;; @ end
562 ;;;
563
564 (provide 'mu-cite)
565
566 (run-hooks 'mu-cite-load-hook)
567
568 ;;; mu-cite.el ends here