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