Update for the new CVS server.
[elisp/mu-cite.git] / mu-cite.el
1 ;;; mu-cite.el --- yet another citation tool for GNU Emacs
2
3 ;; Copyright (C) 1995,1996,1997,1998,1999 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 ;; Pickup some macros, e.g. `with-temp-buffer', for old Emacsen.
47 (require 'poe)
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                    (if ml-name
133                        (concat "["
134                                ml-name
135                                " : No."
136                                (mu-cite-get-value 'ml-count)
137                                "]")
138                      (mu-cite-get-value 'message-id))))))
139         (cons 'in-id
140               (function
141                (lambda ()
142                  (let ((id (mu-cite-get-value 'id)))
143                    (if id
144                        (format ">>>>> In %s \n" id)
145                      "")))))
146         (cons 'x-attribution
147               (function
148                (lambda ()
149                  (mu-cite-get-field-value "X-Attribution"))))
150         ;; mu-register
151         (cons 'prefix (function mu-cite-get-prefix-method))
152         (cons 'prefix-register
153               (function mu-cite-get-prefix-register-method))
154         (cons 'prefix-register-verbose
155               (function mu-cite-get-prefix-register-verbose-method))
156         ;; mu-bbdb
157         (cons 'bbdb-prefix
158               (function mu-bbdb-get-prefix-method))
159         (cons 'bbdb-prefix-register
160               (function mu-bbdb-get-prefix-register-method))
161         (cons 'bbdb-prefix-register-verbose
162               (function mu-bbdb-get-prefix-register-verbose-method))
163         ))
164
165
166 ;;; @ formats
167 ;;;
168
169 (defcustom mu-cite-cited-prefix-regexp
170   "\\(^[^ \t\n<>]+>+[ \t]*\\|^[ \t]*$\\)"
171   "Regexp to match the citation prefix.
172 If match, mu-cite doesn't insert citation prefix."
173   :type 'regexp
174   :group 'mu-cite)
175
176 (defcustom mu-cite-prefix-format '(prefix-register-verbose "> ")
177   "List to represent citation prefix.
178 Each elements must be a string or a method name."
179   :type (list
180          'repeat
181          (list
182           'group
183           :convert-widget
184           (function
185            (lambda (widget)
186              (list
187               'choice
188               :tag "Method or String"
189               :args
190               (nconc
191                (mapcar
192                 (function (lambda (elem) (list 'choice-item (car elem))))
193                 mu-cite-default-methods-alist)
194                '((symbol :tag "Method")
195                  (const :tag "-" nil)
196                  (choice-item :tag "String: \"> \"" "> ")
197                  (string))))))))
198   :set (function (lambda (symbol value)
199                    (set-default symbol (delq nil value))))
200   :group 'mu-cite)
201
202 (defcustom mu-cite-top-format '(in-id ">>>>>\t" from " wrote:\n")
203   "List to represent top string of citation.
204 Each elements must be a string or a method name."
205   :type (list
206          'repeat
207          (list
208           'group
209           :convert-widget
210           (function
211            (lambda (widget)
212              (list 'choice
213                    :tag "Method or String"
214                    :args
215                    (nconc
216                     (mapcar
217                      (function (lambda (elem) (list 'choice-item (car elem))))
218                      mu-cite-default-methods-alist)
219                     '((symbol :tag "Method")
220                       (const :tag "-" nil)
221                       (choice-item :tag "String: \">>>>>\\t\"" ">>>>>\t")
222                       (choice-item :tag "String: \" wrote:\\n\"" " wrote:\n")
223                       (string :tag "String"))))))))
224   :set (function (lambda (symbol value)
225                    (set-default symbol (delq nil value))))
226   :group 'mu-cite)
227
228
229 ;;; @ hooks
230 ;;;
231
232 (defcustom mu-cite-instantiation-hook nil
233   "List of functions called just before narrowing to the message."
234   :type 'hook
235   :group 'mu-cite)
236
237 (defcustom mu-cite-pre-cite-hook nil
238   "List of functions called before citing a region of text."
239   :type 'hook
240   :group 'mu-cite)
241
242 (defcustom mu-cite-post-cite-hook nil
243   "List of functions called after citing a region of text."
244   :type 'hook
245   :group 'mu-cite)
246
247
248 ;;; @ field
249 ;;;
250
251 (defvar mu-cite-get-field-value-method-alist nil
252   "Alist major-mode vs. function to get field-body of header.")
253
254 (defun mu-cite-get-field-value (name)
255   "Return the value of the header field NAME.
256 If the field is not found in the header, a method function which is
257 registered in variable `mu-cite-get-field-value-method-alist' is called."
258   (or (std11-field-body name)
259       (let ((method (assq major-mode mu-cite-get-field-value-method-alist)))
260         (if method
261             (funcall (cdr method) name)))))
262
263
264 ;;; @ item methods
265 ;;;
266
267 ;;; @@ ML count
268 ;;;
269
270 (defcustom mu-cite-ml-count-field-list
271   '("X-Ml-Count" "X-Mail-Count" "X-Seqno" "X-Sequence" "Mailinglist-Id")
272   "List of header fields which contains a sequence number of the mailing list."
273   :type '(repeat (choice :tag "Field Name"
274                          (choice-item "X-Ml-Count")
275                          (choice-item "X-Mail-Count")
276                          (choice-item "X-Seqno")
277                          (choice-item "X-Sequence")
278                          (choice-item "Mailinglist-Id")
279                          (const :tag "-" nil)
280                          (string :tag "Other")))
281   :set (function (lambda (symbol value)
282                    (set-default symbol (delq nil value))))
283   :group 'mu-cite)
284
285 (defun mu-cite-get-ml-count-method ()
286   "A mu-cite method to return a ML-count.
287 This function searches a field about ML-count, which is specified by
288 the variable `mu-cite-ml-count-field-list', in a header.
289 If the field is found, the function returns a number part of the
290 field.
291
292 Notice that please use (mu-cite-get-value 'ml-count)
293 instead of to call the function directly."
294   (let ((field-list mu-cite-ml-count-field-list))
295     (catch 'tag
296       (while field-list
297         (let* ((field (car field-list))
298                (ml-count (mu-cite-get-field-value field)))
299           (if (and ml-count (string-match "[0-9]+" ml-count))
300               (throw 'tag (match-string 0 ml-count)))
301           (setq field-list (cdr field-list)))))))
302
303
304 ;;; @ fundamentals
305 ;;;
306
307 (defvar mu-cite-methods-alist nil)
308
309 (defun mu-cite-make-methods ()
310   (setq mu-cite-methods-alist
311         (copy-alist mu-cite-default-methods-alist))
312   (run-hooks 'mu-cite-instantiation-hook))
313
314 (defun mu-cite-get-value (item)
315   "Return a current value of ITEM."
316   (let ((ret (cdr (assoc item mu-cite-methods-alist))))
317     (if (functionp ret)
318         (prog1
319             (setq ret (save-excursion (funcall ret)))
320           (set-alist 'mu-cite-methods-alist item ret))
321       ret)))
322
323 (defun mu-cite-eval-format (list)
324   (mapconcat (function
325               (lambda (elt)
326                 (cond ((stringp elt) elt)
327                       ((symbolp elt) (mu-cite-get-value elt)))))
328              list ""))
329
330
331 ;;; @ main function
332 ;;;
333
334 ;;;###autoload
335 (defun mu-cite-original ()
336   "Citing filter function.
337 This is callable from the various mail and news readers' reply
338 function according to the agreed upon standard."
339   (interactive)
340   (mu-cite-make-methods)
341   (save-restriction
342     (if (< (mark t) (point))
343         (exchange-point-and-mark))
344     (narrow-to-region (point)(point-max))
345     (run-hooks 'mu-cite-pre-cite-hook)
346     (let ((last-point (point))
347           (top (mu-cite-eval-format mu-cite-top-format))
348           (prefix (mu-cite-eval-format mu-cite-prefix-format)))
349       (if (re-search-forward "^-*$" nil nil)
350           (forward-line 1))
351       (widen)
352       (delete-region last-point (point))
353       (insert top)
354       (setq last-point (point))
355       (while (< (point)(mark t))
356         (or (looking-at mu-cite-cited-prefix-regexp)
357             (insert prefix))
358         (forward-line 1))
359       (goto-char last-point))
360     (run-hooks 'mu-cite-post-cite-hook)))
361
362
363 ;;; @ message editing utilities
364 ;;;
365
366 (defcustom citation-mark-chars ">}|"
367   "String of characters for citation delimiter."
368   :type 'string
369   :group 'mu-cite)
370
371 (defcustom citation-disable-chars "<{"
372   "String of characters not allowed as citation-prefix."
373   :type 'string
374   :group 'mu-cite)
375
376 (defun-maybe-cond char-category (character)
377   "Return a string of category mnemonics for CHAR in TABLE.
378 CHAR can be any multilingual character,
379 TABLE defaults to the current buffer's category table."
380   ((and (subr-fboundp 'char-category-set)
381         (subr-fboundp 'category-set-mnemonics))
382    (category-set-mnemonics (char-category-set character))
383    )
384   ((fboundp 'char-category-list)
385    (mapconcat (lambda (chr)
386                 (char-to-string (int-char chr)))
387               (char-category-list character)
388               "")
389    )
390   ((boundp 'NEMACS)
391    (if (< (char-int character) 128)
392        "al"
393      "j")
394    )
395   (t
396    (if (< (char-int character) 128)
397        "al"
398      "l")
399    ))
400
401 (defun detect-paragraph-cited-prefix ()
402   (save-excursion
403     (goto-char (point-min))
404     (let ((i 0)
405           (prefix
406            (buffer-substring (line-beginning-position)
407                              (line-end-position))))
408       (let ((init prefix)
409             str ret)
410         (while (and (= (forward-line) 0)
411                     (setq str (buffer-substring
412                                (progn (beginning-of-line)(point))
413                                (progn (end-of-line)(point))))
414                     (setq ret (string-compare-from-top prefix str)))
415           (setq prefix
416                 (if (stringp ret)
417                     ret
418                   (car (cdr ret))))
419           (or (string-equal init prefix)
420               (setq i (1+ i)))))
421       (cond ((> i 1) prefix)
422             ((> i 0)
423              (goto-char (point-min))
424              (save-restriction
425                (narrow-to-region (point)
426                                  (+ (point)(length prefix)))
427                (goto-char (point-max))
428                (if (re-search-backward
429                     (concat "[" citation-mark-chars "]") nil t)
430                    (progn
431                      (goto-char (match-end 0))
432                      (if (looking-at "[ \t]+")
433                          (goto-char (match-end 0)))
434                      (buffer-substring (point-min)(point)))
435                  prefix)))
436             ((progn
437                (goto-char (point-max))
438                (re-search-backward
439                 (concat "[" citation-disable-chars "]") nil t)
440                (re-search-backward
441                 (concat "[" citation-mark-chars "]") nil t))
442              (goto-char (match-end 0))
443              (if (looking-at "[ \t]+")
444                  (goto-char (match-end 0)))
445              (buffer-substring (line-beginning-position)(point)))
446             (t "")))))
447
448 ;;;###autoload
449 (defun fill-cited-region (beg end)
450   "Fill each of the paragraphs in the region as a cited text."
451   (interactive "*r")
452   (save-excursion
453     (save-restriction
454       (goto-char end)
455       (and (search-backward "\n" nil t)
456            (setq end (match-end 0)))
457       (narrow-to-region beg end)
458       (let* ((fill-prefix (detect-paragraph-cited-prefix))
459              (fill-column (max (+ 1 (current-left-margin)
460                                   (string-width fill-prefix))
461                                (current-fill-column)))
462              (pat (concat fill-prefix "\n"))
463              filladapt-mode)
464         (goto-char (point-min))
465         (while (search-forward pat nil t)
466           (let ((b (match-beginning 0))
467                 (e (match-end 0)))
468             (delete-region b e)
469             (if (and (> b (point-min))
470                      (let ((cat (char-category
471                                  (char-before b))))
472                        (or (string-match "a" cat)
473                            (string-match "l" cat))))
474                 (insert " "))))
475         (goto-char (point-min))
476         (fill-region (point-min) (point-max))))))
477
478 ;;;###autoload
479 (defun compress-cited-prefix ()
480   "Compress nested cited prefixes."
481   (interactive)
482   (save-excursion
483     (goto-char (point-min))
484     (re-search-forward
485      (concat "^" (regexp-quote mail-header-separator) "$") nil t)
486     (while (re-search-forward
487             (concat "^\\([ \t]*[^ \t\n" citation-mark-chars "]*["
488                     citation-mark-chars "]\\)+") nil t)
489       (let* ((b (match-beginning 0))
490              (e (match-end 0))
491              (prefix (buffer-substring b e))
492              ps pe (s 0)
493              (nest (let ((i 0))
494                      (if (string-match "<[^<>]+>" prefix)
495                          (setq prefix
496                                (substring prefix 0 (match-beginning 0))))
497                      (while (string-match
498                              (concat "\\([" citation-mark-chars "]+\\)[ \t]*")
499                              prefix s)
500                        (setq i (+ i (- (match-end 1)(match-beginning 1)))
501                              ps s
502                              pe (match-beginning 1)
503                              s (match-end 0)))
504                      i)))
505         (if (and ps (< ps pe))
506             (progn
507               (delete-region b e)
508               (insert (concat (substring prefix ps pe)
509                               (make-string nest ?>)))))
510         ))))
511
512 (defun replace-top-string (old new)
513   (interactive "*sOld string: \nsNew string: ")
514   (while (re-search-forward
515           (concat "^" (regexp-quote old)) nil t)
516     (replace-match new)))
517
518 (defun string-compare-from-top (str1 str2)
519   (let* ((len1 (length str1))
520          (len2 (length str2))
521          (len (min len1 len2))
522          (p 0)
523          c1 c2)
524     (while (and (< p len)
525                 (progn
526                   (setq c1 (sref str1 p)
527                         c2 (sref str2 p))
528                   (eq c1 c2)))
529       (setq p (char-next-index c1 p)))
530     (and (> p 0)
531          (let ((matched (substring str1 0 p))
532                (r1 (and (< p len1)(substring str1 p)))
533                (r2 (and (< p len2)(substring str2 p))))
534            (if (eq r1 r2)
535                matched
536              (list 'seq matched (list 'or r1 r2)))))))
537
538
539 ;;; @ end
540 ;;;
541
542 (provide 'mu-cite)
543
544 (run-hooks 'mu-cite-load-hook)
545
546 ;;; mu-cite.el ends here