* mu-cite.el: Require `emu' as well as `poem' because we have need to pickup
[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 ;; 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.
49 (require 'emu)
50
51 (require 'pcustom)
52 (require 'std11)
53 (require 'alist)
54
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")
58
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")
62
63
64 ;;; @ version
65 ;;;
66
67 (defconst mu-cite-version "8.1")
68
69
70 ;;; @ macro
71 ;;;
72
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.
78       string
79     (` (let ((obj (copy-sequence (, string))))
80          (set-text-properties 0 (length obj) nil obj)
81          obj))))
82
83
84 ;;; @ set up
85 ;;;
86
87 (defgroup mu-cite nil
88   "Yet another citation tool for GNU Emacs."
89   :prefix "mu-cite-"
90   :group 'mail
91   :group 'news)
92
93 (defvar mu-cite-default-methods-alist
94   (list (cons 'from
95               (function
96                (lambda ()
97                  (mu-cite-get-field-value "From"))))
98         (cons 'date
99               (function
100                (lambda ()
101                  (mu-cite-get-field-value "Date"))))
102         (cons 'message-id
103               (function
104                (lambda ()
105                  (mu-cite-get-field-value "Message-Id"))))
106         (cons 'subject
107               (function
108                (lambda ()
109                  (mu-cite-get-field-value "Subject"))))
110         (cons 'ml-name
111               (function
112                (lambda ()
113                  (mu-cite-get-field-value "X-Ml-Name"))))
114         (cons 'ml-count (function mu-cite-get-ml-count-method))
115         (cons 'address-structure
116               (function
117                (lambda ()
118                  (car
119                   (std11-parse-address-string (mu-cite-get-value 'from))))))
120         (cons 'full-name
121               (function
122                (lambda ()
123                  (std11-full-name-string
124                   (mu-cite-get-value 'address-structure)))))
125         (cons 'address
126               (function
127                (lambda ()
128                  (std11-address-string
129                   (mu-cite-get-value 'address-structure)))))
130         (cons 'id
131               (function
132                (lambda ()
133                  (let ((ml-name (mu-cite-get-value 'ml-name))
134                        (ml-count (mu-cite-get-value 'ml-count)))
135                    (if ml-name
136                        (concat "["
137                                ml-name
138                                (if ml-count
139                                    (concat " : No." ml-count))
140                                "]")
141                      (mu-cite-get-value 'message-id))))))
142         (cons 'in-id
143               (function
144                (lambda ()
145                  (let ((id (mu-cite-get-value 'id)))
146                    (if id
147                        (format ">>>>> In %s \n" id)
148                      "")))))
149         (cons 'x-attribution
150               (function
151                (lambda ()
152                  (mu-cite-get-field-value "X-Attribution"))))
153         ;; mu-register
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))
159         ;; mu-bbdb
160         (cons 'bbdb-prefix
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))
166         ))
167
168
169 ;;; @ formats
170 ;;;
171
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."
176   :type 'regexp
177   :group 'mu-cite)
178
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."
182   :type (list
183          'repeat
184          (list
185           'group
186           :convert-widget
187           (function
188            (lambda (widget)
189              (list
190               'choice
191               :tag "Method or String"
192               :args
193               (nconc
194                (mapcar
195                 (function (lambda (elem) (list 'choice-item (car elem))))
196                 mu-cite-default-methods-alist)
197                '((symbol :tag "Method")
198                  (const :tag "-" nil)
199                  (choice-item :tag "String: \"> \"" "> ")
200                  (string))))))))
201   :set (function (lambda (symbol value)
202                    (set-default symbol (delq nil value))))
203   :group 'mu-cite)
204
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."
208   :type (list
209          'repeat
210          (list
211           'group
212           :convert-widget
213           (function
214            (lambda (widget)
215              (list 'choice
216                    :tag "Method or String"
217                    :args
218                    (nconc
219                     (mapcar
220                      (function (lambda (elem) (list 'choice-item (car elem))))
221                      mu-cite-default-methods-alist)
222                     '((symbol :tag "Method")
223                       (const :tag "-" nil)
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))))
229   :group 'mu-cite)
230
231
232 ;;; @ hooks
233 ;;;
234
235 (defcustom mu-cite-instantiation-hook nil
236   "List of functions called just before narrowing to the message."
237   :type 'hook
238   :group 'mu-cite)
239
240 (defcustom mu-cite-pre-cite-hook nil
241   "List of functions called before citing a region of text."
242   :type 'hook
243   :group 'mu-cite)
244
245 (defcustom mu-cite-post-cite-hook nil
246   "List of functions called after citing a region of text."
247   :type 'hook
248   :group 'mu-cite)
249
250
251 ;;; @ field
252 ;;;
253
254 (defvar mu-cite-get-field-value-method-alist nil
255   "Alist major-mode vs. function to get field-body of header.")
256
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)))
263         (if method
264             (funcall (cdr method) name)))))
265
266
267 ;;; @ item methods
268 ;;;
269
270 ;;; @@ ML count
271 ;;;
272
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")
282                          (const :tag "-" nil)
283                          (string :tag "Other")))
284   :set (function (lambda (symbol value)
285                    (set-default symbol (delq nil value))))
286   :group 'mu-cite)
287
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
293 field.
294
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))
298     (catch 'tag
299       (while 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)))))))
305
306
307 ;;; @ fundamentals
308 ;;;
309
310 (defvar mu-cite-methods-alist nil)
311
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))
316
317 (defun mu-cite-get-value (item)
318   "Return a current value of ITEM."
319   (let ((ret (cdr (assoc item mu-cite-methods-alist))))
320     (if (functionp ret)
321         (prog1
322             (setq ret (save-excursion (funcall ret)))
323           (set-alist 'mu-cite-methods-alist item ret))
324       ret)))
325
326 (defun mu-cite-eval-format (list)
327   (mapconcat (function
328               (lambda (elt)
329                 (cond ((stringp elt) elt)
330                       ((symbolp elt) (mu-cite-get-value elt)))))
331              list ""))
332
333
334 ;;; @ main function
335 ;;;
336
337 ;;;###autoload
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."
342   (interactive)
343   (mu-cite-make-methods)
344   (save-restriction
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)
353           (forward-line 1))
354       (widen)
355       (delete-region last-point (point))
356       (insert top)
357       (setq last-point (point))
358       (while (< (point)(mark t))
359         (or (looking-at mu-cite-cited-prefix-regexp)
360             (insert prefix))
361         (forward-line 1))
362       (goto-char last-point))
363     (run-hooks 'mu-cite-post-cite-hook)))
364
365
366 ;;; @ message editing utilities
367 ;;;
368
369 (defcustom citation-mark-chars ">}|"
370   "String of characters for citation delimiter."
371   :type 'string
372   :group 'mu-cite)
373
374 (defcustom citation-disable-chars "<{"
375   "String of characters not allowed as citation-prefix."
376   :type 'string
377   :group 'mu-cite)
378
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))
386    )
387   ((fboundp 'char-category-list)
388    (mapconcat (lambda (chr)
389                 (char-to-string (int-char chr)))
390               (char-category-list character)
391               "")
392    )
393   ((boundp 'NEMACS)
394    (if (< (char-int character) 128)
395        "al"
396      "j")
397    )
398   (t
399    (if (< (char-int character) 128)
400        "al"
401      "l")
402    ))
403
404 (defun detect-paragraph-cited-prefix ()
405   (save-excursion
406     (goto-char (point-min))
407     (let ((i 0)
408           (prefix
409            (buffer-substring (line-beginning-position)
410                              (line-end-position))))
411       (let ((init prefix)
412             str ret)
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)))
418           (setq prefix
419                 (if (stringp ret)
420                     ret
421                   (car (cdr ret))))
422           (or (string-equal init prefix)
423               (setq i (1+ i)))))
424       (cond ((> i 1) prefix)
425             ((> i 0)
426              (goto-char (point-min))
427              (save-restriction
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)
433                    (progn
434                      (goto-char (match-end 0))
435                      (if (looking-at "[ \t]+")
436                          (goto-char (match-end 0)))
437                      (buffer-substring (point-min)(point)))
438                  prefix)))
439             ((progn
440                (goto-char (point-max))
441                (re-search-backward
442                 (concat "[" citation-disable-chars "]") nil t)
443                (re-search-backward
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)))
449             (t "")))))
450
451 ;;;###autoload
452 (defun fill-cited-region (beg end)
453   "Fill each of the paragraphs in the region as a cited text."
454   (interactive "*r")
455   (save-excursion
456     (save-restriction
457       (goto-char end)
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"))
466              filladapt-mode)
467         (goto-char (point-min))
468         (while (search-forward pat nil t)
469           (let ((b (match-beginning 0))
470                 (e (match-end 0)))
471             (delete-region b e)
472             (if (and (> b (point-min))
473                      (let ((cat (char-category
474                                  (char-before b))))
475                        (or (string-match "a" cat)
476                            (string-match "l" cat))))
477                 (insert " "))))
478         (goto-char (point-min))
479         (fill-region (point-min) (point-max))))))
480
481 ;;;###autoload
482 (defun compress-cited-prefix ()
483   "Compress nested cited prefixes."
484   (interactive)
485   (save-excursion
486     (goto-char (point-min))
487     (re-search-forward
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))
493              (e (match-end 0))
494              (prefix (buffer-substring b e))
495              ps pe (s 0)
496              (nest (let ((i 0))
497                      (if (string-match "<[^<>]+>" prefix)
498                          (setq prefix
499                                (substring prefix 0 (match-beginning 0))))
500                      (while (string-match
501                              (concat "\\([" citation-mark-chars "]+\\)[ \t]*")
502                              prefix s)
503                        (setq i (+ i (- (match-end 1)(match-beginning 1)))
504                              ps s
505                              pe (match-beginning 1)
506                              s (match-end 0)))
507                      i)))
508         (if (and ps (< ps pe))
509             (progn
510               (delete-region b e)
511               (insert (concat (substring prefix ps pe)
512                               (make-string nest ?>)))))
513         ))))
514
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)))
520
521 (defun string-compare-from-top (str1 str2)
522   (let* ((len1 (length str1))
523          (len2 (length str2))
524          (len (min len1 len2))
525          (p 0)
526          c1 c2)
527     (while (and (< p len)
528                 (progn
529                   (setq c1 (sref str1 p)
530                         c2 (sref str2 p))
531                   (eq c1 c2)))
532       (setq p (char-next-index c1 p)))
533     (and (> p 0)
534          (let ((matched (substring str1 0 p))
535                (r1 (and (< p len1)(substring str1 p)))
536                (r2 (and (< p len2)(substring str2 p))))
537            (if (eq r1 r2)
538                matched
539              (list 'seq matched (list 'or r1 r2)))))))
540
541
542 ;;; @ end
543 ;;;
544
545 (provide 'mu-cite)
546
547 (run-hooks 'mu-cite-load-hook)
548
549 ;;; mu-cite.el ends here