- Add autoload setting for `mu-cite-get-prefix-method',
[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-k@jaist.ac.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 to your ~/.emacs:
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 (eval-when-compile (require 'cl))
47
48 ;; Pickup some macros, e.g. `with-temp-buffer', for old Emacsen.
49 (require 'poe)
50
51 ;; Pickup `char-category' for XEmacs.
52 (require 'emu)
53
54 (require 'custom)
55 (require 'std11)
56 (require 'alist)
57
58 (autoload 'mu-cite-get-prefix-method "mu-register")
59 (autoload 'mu-cite-get-prefix-register-method "mu-register")
60 (autoload 'mu-cite-get-prefix-register-verbose-method "mu-register")
61
62
63 ;;; @ version
64 ;;;
65
66 (defconst mu-cite-version "8.0")
67
68
69 ;;; @ obsoletes
70 ;;;
71
72 ;; This part will be abolished in the future.
73
74 ;; variables
75
76
77
78 ;;; @ set up
79 ;;;
80
81 (defgroup mu-cite nil
82   "yet another citation tool for GNU Emacs."
83   :prefix "mu-cite-"
84   :group 'mail
85   :group 'news)
86
87 (defvar mu-cite-default-methods-alist
88   (list (cons 'from
89               (function
90                (lambda ()
91                  (mu-cite-get-field-value "From"))))
92         (cons 'date
93               (function
94                (lambda ()
95                  (mu-cite-get-field-value "Date"))))
96         (cons 'message-id
97               (function
98                (lambda ()
99                  (mu-cite-get-field-value "Message-Id"))))
100         (cons 'subject
101               (function
102                (lambda ()
103                  (mu-cite-get-field-value "Subject"))))
104         (cons 'ml-name
105               (function
106                (lambda ()
107                  (mu-cite-get-field-value "X-Ml-Name"))))
108         (cons 'ml-count (function mu-cite-get-ml-count-method))
109         (cons 'address-structure
110               (function
111                (lambda ()
112                  (car
113                   (std11-parse-address-string (mu-cite-get-value 'from))))))
114         (cons 'full-name
115               (function
116                (lambda ()
117                  (std11-full-name-string
118                   (mu-cite-get-value 'address-structure)))))
119         (cons 'address
120               (function
121                (lambda ()
122                  (std11-address-string
123                   (mu-cite-get-value 'address-structure)))))
124         (cons 'id
125               (function
126                (lambda ()
127                  (let ((ml-name (mu-cite-get-value 'ml-name)))
128                    (if ml-name
129                        (concat "["
130                                ml-name
131                                " : No."
132                                (mu-cite-get-value 'ml-count)
133                                "]")
134                      (mu-cite-get-value 'message-id))))))
135         (cons 'in-id
136               (function
137                (lambda ()
138                  (let ((id (mu-cite-get-value 'id)))
139                    (if id
140                        (format ">>>>> In %s \n" id)
141                      "")))))
142         (cons 'prefix (function mu-cite-get-prefix-method))
143         (cons 'prefix-register
144               (function mu-cite-get-prefix-register-method))
145         (cons 'prefix-register-verbose
146               (function mu-cite-get-prefix-register-verbose-method))
147         (cons 'x-attribution
148               (function
149                (lambda ()
150                  (mu-cite-get-field-value "X-Attribution"))))
151         ))
152
153
154 ;;; @ formats
155 ;;;
156
157 (defcustom mu-cite-cited-prefix-regexp
158   "\\(^[^ \t\n<>]+>+[ \t]*\\|^[ \t]*$\\)"
159   "Regexp to match the citation prefix.
160 If match, mu-cite doesn't insert citation prefix."
161   :type 'regexp
162   :group 'mu-cite)
163
164 (defcustom mu-cite-prefix-format '(prefix-register-verbose "> ")
165   "List to represent citation prefix.
166 Each elements must be string or method name."
167   :type (list 'repeat
168               (nconc '(choice :tag "String or Method name")
169                      (mapcar
170                       (function
171                        (lambda (elem) (list 'choice-item (car elem))))
172                       mu-cite-default-methods-alist)
173                      '((symbol :tag "Other Method")
174                        (item "-")
175                        (choice-item :tag "String: \"> \"" "> ")
176                        (string :tag "Other String"))))
177   :group 'mu-cite)
178
179 (defcustom mu-cite-top-format '(in-id ">>>>>\t" from " wrote:\n")
180   "List to represent top string of citation.
181 Each elements must be string or method name."
182   :type (list 'repeat
183               (nconc
184                '(choice :tag "String or Method name")
185                (mapcar
186                 (function
187                  (lambda (elem) (list 'choice-item (car elem))))
188                 mu-cite-default-methods-alist)
189                '((symbol :tag "Other Method")
190                  (item "-")
191                  (choice-item :tag "String: \">>>>>\\t\"" ">>>>>\t")
192                  (choice-item :tag "String: \" wrote:\\n\"" " wrote:\n")
193                  (string :tag "Other String"))))
194   :group 'mu-cite)
195
196
197 ;;; @ hooks
198 ;;;
199
200 (defcustom mu-cite-load-hook nil
201   "List of functions called after mu-cite is loaded.
202 Use this hook to add your own methods to `mu-cite-default-methods-alist'."
203   :type 'hook
204   :group 'mu-cite)
205
206 (defcustom mu-cite-instantiation-hook nil
207   "List of functions called just before narrowing to the message."
208   :type 'hook
209   :group 'mu-cite)
210
211 (defcustom mu-cite-pre-cite-hook nil
212   "List of functions called before citing a region of text."
213   :type 'hook
214   :group 'mu-cite)
215
216 (defcustom mu-cite-post-cite-hook nil
217   "List of functions called after citing a region of text."
218   :type 'hook
219   :group 'mu-cite)
220
221
222 ;;; @ field
223 ;;;
224
225 (defvar mu-cite-get-field-value-method-alist nil
226   "Alist major-mode vs. function to get field-body of header.")
227
228 (defun mu-cite-get-field-value (name)
229   (or (std11-field-body name)
230       (let ((method (assq major-mode mu-cite-get-field-value-method-alist)))
231         (when method
232           (funcall (cdr method) name)))))
233
234
235 ;;; @ item methods
236 ;;;
237
238 ;;; @@ ML count
239 ;;;
240
241 (defcustom mu-cite-ml-count-field-list
242   '("X-Ml-Count" "X-Mail-Count" "X-Seqno" "X-Sequence" "Mailinglist-Id")
243   "List of header fields which contain sequence number of mailing list."
244   :type '(repeat (choice (choice-item "X-Ml-Count")
245                          (choice-item "X-Mail-Count")
246                          (choice-item "X-Seqno")
247                          (choice-item "X-Sequence")
248                          (choice-item "Mailinglist-Id")
249                          (item "-")
250                          (string :tag "Other")))
251   :group 'mu-cite)
252
253 (defun mu-cite-get-ml-count-method ()
254   (let ((field-list mu-cite-ml-count-field-list))
255     (catch 'tag
256       (while field-list
257         (let* ((field (car field-list))
258                (ml-count (mu-cite-get-field-value field)))
259           (when (and ml-count (string-match "[0-9]+" ml-count))
260             (throw 'tag
261                    (substring ml-count
262                               (match-beginning 0)(match-end 0))))
263           (setq field-list (cdr field-list)))))))
264
265
266 ;;; @ fundamentals
267 ;;;
268
269 (defvar mu-cite-methods-alist nil)
270
271 (defun mu-cite-make-methods ()
272   (setq mu-cite-methods-alist
273         (copy-alist mu-cite-default-methods-alist))
274   (run-hooks 'mu-cite-instantiation-hook))
275
276 (defun mu-cite-get-value (item)
277   (let ((ret (cdr (assoc item mu-cite-methods-alist))))
278     (if (functionp ret)
279         (prog1
280             (setq ret (save-excursion (funcall ret)))
281           (set-alist 'mu-cite-methods-alist item ret))
282       ret)))
283
284 (defun mu-cite-eval-format (list)
285   (mapconcat (function
286               (lambda (elt)
287                 (cond ((stringp elt) elt)
288                       ((symbolp elt) (mu-cite-get-value elt)))))
289              list ""))
290
291
292 ;;; @ main function
293 ;;;
294
295 ;;;###autoload
296 (defun mu-cite-original ()
297   "Citing filter function.
298 This is callable from the various mail and news readers' reply
299 function according to the agreed upon standard."
300   (interactive)
301   (mu-cite-make-methods)
302   (save-restriction
303     (when (< (mark t) (point))
304       (exchange-point-and-mark))
305     (narrow-to-region (point)(point-max))
306     (run-hooks 'mu-cite-pre-cite-hook)
307     (let ((last-point (point))
308           (top (mu-cite-eval-format mu-cite-top-format))
309           (prefix (mu-cite-eval-format mu-cite-prefix-format)))
310       (when (re-search-forward "^-*$" nil nil)
311         (forward-line 1))
312       (widen)
313       (delete-region last-point (point))
314       (insert top)
315       (setq last-point (point))
316       (while (< (point)(mark t))
317         (unless (looking-at mu-cite-cited-prefix-regexp)
318           (insert prefix))
319         (forward-line 1))
320       (goto-char last-point))
321     (run-hooks 'mu-cite-post-cite-hook)))
322
323
324 ;;; @ message editing utilities
325 ;;;
326
327 (defcustom citation-mark-chars ">}|"
328   "String of characters for citation delimiter."
329   :type 'string
330   :group 'mu-cite)
331
332 (defcustom citation-disable-chars "<{"
333   "String of characters not allowed as citation-prefix."
334   :type 'string
335   :group 'mu-cite)
336
337 (defun detect-paragraph-cited-prefix ()
338   (save-excursion
339     (goto-char (point-min))
340     (let ((i 0)
341           (prefix
342            (buffer-substring
343             (progn (beginning-of-line)(point))
344             (progn (end-of-line)(point))))
345           str ret)
346       (while (and (= (forward-line) 0)
347                   (setq str (buffer-substring
348                              (progn (beginning-of-line)(point))
349                              (progn (end-of-line)(point))))
350                   (setq ret (string-compare-from-top prefix str)))
351         (setq prefix
352               (if (stringp ret)
353                   ret
354                 (cadr ret)))
355         (incf i))
356       (cond ((> i 1) prefix)
357             ((> i 0)
358              (goto-char (point-min))
359              (save-restriction
360                (narrow-to-region (point)
361                                  (+ (point)(length prefix)))
362                (goto-char (point-max))
363                (if (re-search-backward
364                     (concat "[" citation-mark-chars "]") nil t)
365                    (progn
366                      (goto-char (match-end 0))
367                      (when (looking-at "[ \t]+")
368                        (goto-char (match-end 0)))
369                      (buffer-substring (point-min)(point)))
370                  prefix)))
371             ((progn
372                (goto-char (point-max))
373                (re-search-backward
374                 (concat "[" citation-disable-chars "]") nil t)
375                (re-search-backward
376                 (concat "[" citation-mark-chars "]") nil t))
377              (goto-char (match-end 0))
378              (when (looking-at "[ \t]+")
379                (goto-char (match-end 0)))
380              (buffer-substring (point-min)(point)))
381             (t "")))))
382
383 (defun fill-cited-region (beg end)
384   (interactive "*r")
385   (save-excursion
386     (save-restriction
387       (goto-char end)
388       (and (search-backward "\n" nil t)
389            (setq end (match-end 0)))
390       (narrow-to-region beg end)
391       (let* ((fill-prefix (detect-paragraph-cited-prefix))
392              (pat (concat fill-prefix "\n")))
393         (goto-char (point-min))
394         (while (search-forward pat nil t)
395           (let ((b (match-beginning 0))
396                 (e (match-end 0)))
397             (delete-region b e)
398             (when (and (> b (point-min))
399                        (let ((cat (char-category
400                                    (char-before b))))
401                          (or (string-match "a" cat)
402                              (string-match "l" cat))))
403               (insert " "))))
404         (goto-char (point-min))
405         (fill-region (point-min) (point-max))))))
406
407 (defun compress-cited-prefix ()
408   (interactive)
409   (save-excursion
410     (goto-char (point-min))
411     (re-search-forward
412      (concat "^" (regexp-quote mail-header-separator) "$") nil t)
413     (while (re-search-forward
414             (concat "^\\([ \t]*[^ \t\n" citation-mark-chars "]*["
415                     citation-mark-chars "]\\)+") nil t)
416       (let* ((b (match-beginning 0))
417              (e (match-end 0))
418              (prefix (buffer-substring b e))
419              ps pe (s 0)
420              (nest (let ((i 0))
421                      (when (string-match "<[^<>]+>" prefix)
422                        (setq prefix (substring prefix 0 (match-beginning 0))))
423                      (while (string-match
424                              (concat "\\([" citation-mark-chars "]+\\)[ \t]*")
425                              prefix s)
426                        (setq i (+ i (- (match-end 1)(match-beginning 1)))
427                              ps s
428                              pe (match-beginning 1)
429                              s (match-end 0)))
430                      i)))
431         (when (and ps (< ps pe))
432           (delete-region b e)
433           (insert (concat (substring prefix ps pe) (make-string nest ?>))))
434         ))))
435
436 (defun replace-top-string (old new)
437   (interactive "*sOld string: \nsNew string: ")
438   (while (re-search-forward
439           (concat "^" (regexp-quote old)) nil t)
440     (replace-match new)))
441
442 (defun string-compare-from-top (str1 str2)
443   (let* ((len1 (length str1))
444          (len2 (length str2))
445          (len (min len1 len2))
446          (p 0)
447          c1 c2)
448     (while (and (< p len)
449                 (progn
450                   (setq c1 (aref str1 p)
451                         c2 (aref str2 p))
452                   (eq c1 c2)))
453       (setq p (+ p (char-length c1))))
454     (and (> p 0)
455          (let ((matched (substring str1 0 p))
456                (r1 (and (< p len1)(substring str1 p)))
457                (r2 (and (< p len2)(substring str2 p))))
458            (if (eq r1 r2)
459                matched
460              (list 'seq matched (list 'or r1 r2)))))))
461
462
463 ;;; @ obsoletes
464 ;;;
465
466 ;; This part will be abolished in the future.
467
468 (define-obsolete-function-alias
469   'mu-cite/cite-original 'mu-cite-original)
470 (define-obsolete-function-alias
471   'mu-cite/get-field-value 'mu-cite-get-field-value)
472 (define-obsolete-function-alias
473   'mu-cite/get-value 'mu-cite-get-value)
474
475 (eval-when-compile (require 'static))
476
477 (static-when (featurep 'xemacs)
478   (define-obsolete-variable-alias
479     'mu-cite/default-methods-alist
480     'mu-cite-default-methods-alist)
481   (define-obsolete-variable-alias
482     'mu-cite/cited-prefix-regexp
483     'mu-cite-cited-prefix-regexp)
484   (define-obsolete-variable-alias
485     'mu-cite/prefix-format
486     'mu-cite-prefix-format)
487   (define-obsolete-variable-alias
488     'mu-cite/top-format
489     'mu-cite-top-format)
490   (define-obsolete-variable-alias
491     'mu-cite/instantiation-hook
492     'mu-cite-instantiation-hook)
493   (define-obsolete-variable-alias
494     'mu-cite/pre-cite-hook
495     'mu-cite-pre-cite-hook)
496   (define-obsolete-variable-alias
497     'mu-cite/post-cite-hook
498     'mu-cite-post-cite-hook)
499   (define-obsolete-variable-alias
500     'mu-cite/get-field-value-method-alist
501     'mu-cite-get-field-value-method-alist)
502   (define-obsolete-variable-alias
503     'mu-cite/ml-count-field-list
504     'mu-cite-ml-count-field-list)
505   )
506
507 (static-unless (fboundp 'defvaralias)
508   (mapcar
509    (function
510     (lambda (elem)
511       (eval (list 'defvar (car elem) (cadr elem)))))
512    mu-cite-obsolete-variable-alist))
513
514
515 ;;; @ end
516 ;;;
517
518 (provide 'mu-cite)
519
520 (run-hooks 'mu-cite-load-hook)
521
522 ;;; mu-cite.el ends here