* mu-cite.el (fill-column-for-fill-cited-region): New user option.
[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         ;; mu-register
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))
157         ;; mu-bbdb
158         (cons 'bbdb-prefix
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))
164         ))
165
166
167 ;;; @ formats
168 ;;;
169
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."
174   :type 'regexp
175   :group 'mu-cite)
176
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."
180   :type (list
181          'repeat
182          (list
183           'group
184           :convert-widget
185           (function
186            (lambda (widget)
187              (list
188               'choice
189               :tag "Method or String"
190               :args
191               (nconc
192                (mapcar
193                 (function (lambda (elem) (list 'choice-item (car elem))))
194                 mu-cite-default-methods-alist)
195                '((symbol :tag "Method")
196                  (const :tag "-" nil)
197                  (choice-item :tag "String: \"> \"" "> ")
198                  (string))))))))
199   :set (function (lambda (symbol value)
200                    (set-default symbol (delq nil value))))
201   :group 'mu-cite)
202
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."
206   :type (list
207          'repeat
208          (list
209           'group
210           :convert-widget
211           (function
212            (lambda (widget)
213              (list 'choice
214                    :tag "Method or String"
215                    :args
216                    (nconc
217                     (mapcar
218                      (function (lambda (elem) (list 'choice-item (car elem))))
219                      mu-cite-default-methods-alist)
220                     '((symbol :tag "Method")
221                       (const :tag "-" nil)
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))))
227   :group 'mu-cite)
228
229
230 ;;; @ hooks
231 ;;;
232
233 (defcustom mu-cite-instantiation-hook nil
234   "List of functions called just before narrowing to the message."
235   :type 'hook
236   :group 'mu-cite)
237
238 (defcustom mu-cite-pre-cite-hook nil
239   "List of functions called before citing a region of text."
240   :type 'hook
241   :group 'mu-cite)
242
243 (defcustom mu-cite-post-cite-hook nil
244   "List of functions called after citing a region of text."
245   :type 'hook
246   :group 'mu-cite)
247
248
249 ;;; @ field
250 ;;;
251
252 (defvar mu-cite-get-field-value-method-alist nil
253   "Alist major-mode vs. function to get field-body of header.")
254
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)))
261         (if method
262             (funcall (cdr method) name)))))
263
264
265 ;;; @ item methods
266 ;;;
267
268 ;;; @@ ML count
269 ;;;
270
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")
280                          (const :tag "-" nil)
281                          (string :tag "Other")))
282   :set (function (lambda (symbol value)
283                    (set-default symbol (delq nil value))))
284   :group 'mu-cite)
285
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
291 field.
292
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))
296     (catch 'tag
297       (while 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)))))))
303
304
305 ;;; @ fundamentals
306 ;;;
307
308 (defvar mu-cite-methods-alist nil)
309
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))
314
315 (defun mu-cite-get-value (item)
316   "Return a current value of ITEM."
317   (let ((ret (cdr (assoc item mu-cite-methods-alist))))
318     (if (functionp ret)
319         (prog1
320             (setq ret (save-excursion (funcall ret)))
321           (set-alist 'mu-cite-methods-alist item ret))
322       ret)))
323
324 (defun mu-cite-eval-format (list)
325   (mapconcat (function
326               (lambda (elt)
327                 (cond ((stringp elt) elt)
328                       ((symbolp elt) (mu-cite-get-value elt)))))
329              list ""))
330
331
332 ;;; @ main function
333 ;;;
334
335 ;;;###autoload
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."
340   (interactive)
341   (mu-cite-make-methods)
342   (save-restriction
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)
351           (forward-line 1))
352       (widen)
353       (delete-region last-point (point))
354       (insert top)
355       (setq last-point (point))
356       (while (< (point)(mark t))
357         (or (looking-at mu-cite-cited-prefix-regexp)
358             (insert prefix))
359         (forward-line 1))
360       (goto-char last-point))
361     (run-hooks 'mu-cite-post-cite-hook)))
362
363
364 ;;; @ message editing utilities
365 ;;;
366
367 (defcustom citation-mark-chars ">}|"
368   "String of characters for citation delimiter."
369   :type 'string
370   :group 'mu-cite)
371
372 (defcustom citation-disable-chars "<{"
373   "String of characters not allowed as citation-prefix."
374   :type 'string
375   :group 'mu-cite)
376
377 (eval-and-compile
378   ;; Force redefine the function `char-category' which may have been
379   ;; defined by emu.el.  They can be distinguished by the number of
380   ;; arguments.  Anyway, that is the best way not to use emu.el.
381   (if (and (fboundp 'char-category)
382            (subrp (symbol-function 'char-category)))
383       nil
384     (fmakunbound 'char-category)
385     (defun-maybe-cond char-category (character &optional table)
386       "Return a string of category mnemonics for CHAR in TABLE.
387 CHAR can be any multilingual characters,
388 TABLE defaults to the current buffer's category table."
389       ((and (subr-fboundp 'char-category-set)
390             (subr-fboundp 'category-set-mnemonics))
391        (category-set-mnemonics (char-category-set character)))
392       ((and (fboundp 'char-category-list)
393             ;; `char-category-list' returns a list of characters
394             ;; in XEmacs 21.2.25 and later, otherwise integers.
395             (characterp (car-safe (char-category-list ?a))))
396        (concat (char-category-list character)))
397       ((fboundp 'char-category-list)
398        (mapconcat (lambda (chr)
399                     (char-to-string (int-char chr)))
400                   (char-category-list character)
401                   ""))
402       ((boundp 'NEMACS)
403        (if (< (char-int character) 128)
404            "al"
405          "j"))
406       (t
407        (if (< (char-int character) 128)
408            "al"
409          "l")))))
410
411 (defun detect-paragraph-cited-prefix ()
412   (save-excursion
413     (goto-char (point-min))
414     (let ((i 0)
415           (prefix
416            (buffer-substring (line-beginning-position)
417                              (line-end-position))))
418       (let ((init prefix)
419             str ret)
420         (while (and (= (forward-line) 0)
421                     (setq str (buffer-substring
422                                (progn (beginning-of-line)(point))
423                                (progn (end-of-line)(point))))
424                     (setq ret (string-compare-from-top prefix str)))
425           (setq prefix
426                 (if (stringp ret)
427                     ret
428                   (car (cdr ret))))
429           (or (string-equal init prefix)
430               (setq i (1+ i)))))
431       (cond ((> i 1) prefix)
432             ((> i 0)
433              (goto-char (point-min))
434              (save-restriction
435                (narrow-to-region (point)
436                                  (+ (point)(length prefix)))
437                (goto-char (point-max))
438                (if (re-search-backward
439                     (concat "[" citation-mark-chars "]") nil t)
440                    (progn
441                      (goto-char (match-end 0))
442                      (if (looking-at "[ \t]+")
443                          (goto-char (match-end 0)))
444                      (buffer-substring (point-min)(point)))
445                  prefix)))
446             ((progn
447                (goto-char (point-max))
448                (re-search-backward
449                 (concat "[" citation-disable-chars "]") nil t)
450                (re-search-backward
451                 (concat "[" citation-mark-chars "]") nil t))
452              (goto-char (match-end 0))
453              (if (looking-at "[ \t]+")
454                  (goto-char (match-end 0)))
455              (buffer-substring (line-beginning-position)(point)))
456             (t "")))))
457
458 (defcustom fill-column-for-fill-cited-region nil
459   "Integer to override `fill-column' while `fill-cited-region' is being
460 executed."
461   :type (` (choice (const :tag "Off" nil)
462                    (integer (, default-fill-column))))
463   :group 'mu-cite)
464
465 ;;;###autoload
466 (defun fill-cited-region (beg end)
467   "Fill each of the paragraphs in the region as a cited text."
468   (interactive "*r")
469   (save-excursion
470     (save-restriction
471       (goto-char end)
472       (and (search-backward "\n" nil t)
473            (setq end (match-end 0)))
474       (narrow-to-region beg end)
475       (let* ((fill-prefix (detect-paragraph-cited-prefix))
476              (fill-column (max (+ 1 (current-left-margin)
477                                   (string-width fill-prefix))
478                                (or fill-column-for-fill-cited-region
479                                    (current-fill-column))))
480              (pat (concat fill-prefix "\n"))
481              filladapt-mode)
482         (goto-char (point-min))
483         (while (search-forward pat nil t)
484           (let ((b (match-beginning 0))
485                 (e (match-end 0)))
486             (delete-region b e)
487             (if (and (> b (point-min))
488                      (let ((cat (char-category
489                                  (char-before b))))
490                        (or (string-match "a" cat)
491                            (string-match "l" cat))))
492                 (insert " "))))
493         (goto-char (point-min))
494         (fill-region (point-min) (point-max))))))
495
496 ;;;###autoload
497 (defun compress-cited-prefix ()
498   "Compress nested cited prefixes."
499   (interactive)
500   (save-excursion
501     (goto-char (point-min))
502     (re-search-forward
503      (concat "^" (regexp-quote mail-header-separator) "$") nil t)
504     (while (re-search-forward
505             (concat "^\\([ \t]*[^ \t\n" citation-mark-chars "]*["
506                     citation-mark-chars "]\\)+") nil t)
507       (let* ((b (match-beginning 0))
508              (e (match-end 0))
509              (prefix (buffer-substring b e))
510              ps pe (s 0)
511              (nest (let ((i 0))
512                      (if (string-match "<[^<>]+>" prefix)
513                          (setq prefix
514                                (substring prefix 0 (match-beginning 0))))
515                      (while (string-match
516                              (concat "\\([" citation-mark-chars "]+\\)[ \t]*")
517                              prefix s)
518                        (setq i (+ i (- (match-end 1)(match-beginning 1)))
519                              ps s
520                              pe (match-beginning 1)
521                              s (match-end 0)))
522                      i)))
523         (if (and ps (< ps pe))
524             (progn
525               (delete-region b e)
526               (insert (concat (substring prefix ps pe)
527                               (make-string nest ?>)))))
528         ))))
529
530 (defun replace-top-string (old new)
531   (interactive "*sOld string: \nsNew string: ")
532   (while (re-search-forward
533           (concat "^" (regexp-quote old)) nil t)
534     (replace-match new)))
535
536 (defun string-compare-from-top (str1 str2)
537   (let* ((len1 (length str1))
538          (len2 (length str2))
539          (len (min len1 len2))
540          (p 0)
541          c1 c2)
542     (while (and (< p len)
543                 (progn
544                   (setq c1 (sref str1 p)
545                         c2 (sref str2 p))
546                   (eq c1 c2)))
547       (setq p (char-next-index c1 p)))
548     (and (> p 0)
549          (let ((matched (substring str1 0 p))
550                (r1 (and (< p len1)(substring str1 p)))
551                (r2 (and (< p len2)(substring str2 p))))
552            (if (eq r1 r2)
553                matched
554              (list 'seq matched (list 'or r1 r2)))))))
555
556
557 ;;; @ end
558 ;;;
559
560 (provide 'mu-cite)
561
562 (run-hooks 'mu-cite-load-hook)
563
564 ;;; mu-cite.el ends here