Update.
[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 Free Software Foundation, Inc.
4
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;;         MINOURA Makoto <minoura@netlaputa.or.jp>
7 ;;         Shuhei KOBAYASHI <shuhei-k@jaist.ac.jp>
8 ;; Maintainer: Shuhei KOBAYASHI <shuhei-k@jaist.ac.jp>
9 ;; Version: $Revision: 7.51 $
10 ;; Keywords: mail, news, citation
11
12 ;; This file is part of MU (Message Utilities).
13
14 ;; This program is free software; you can redistribute it and/or
15 ;; modify it under the terms of the GNU General Public License as
16 ;; published by the Free Software Foundation; either version 2, or (at
17 ;; your option) any later version.
18
19 ;; This program is distributed in the hope that it will be useful, but
20 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22 ;; General Public License for more details.
23
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
26 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
27 ;; Boston, MA 02111-1307, USA.
28
29 ;;; Commentary:
30
31 ;; - How to use
32 ;;   1. bytecompile this file and copy it to the apropriate directory.
33 ;;   2. put the following lines to your ~/.emacs:
34 ;;      for EMACS 19 or later and XEmacs
35 ;;              (autoload 'mu-cite/cite-original "mu-cite" nil t)
36 ;;              ;; for all but message-mode
37 ;;              (add-hook 'mail-citation-hook 'mu-cite/cite-original)
38 ;;              ;; for message-mode only
39 ;;              (setq message-cite-function (function mu-cite/cite-original))
40 ;;      for EMACS 18
41 ;;              ;; for all but mh-e
42 ;;              (add-hook 'mail-yank-hooks (function mu-cite/cite-original))
43 ;;              ;; for mh-e only
44 ;;              (add-hook 'mh-yank-hooks (function mu-cite/cite-original))
45
46 ;;; Code:
47
48 (require 'std11)
49 ;;(require 'tl-str)
50 ;;(require 'tl-list)
51 (require 'alist)
52
53
54 ;;; @ version
55 ;;;
56
57 (defsubst get-version-string (id)
58   "Return a version-string from RCS ID."
59   (and (string-match ",v \\([0-9][0-9.][0-9.]+\\)" id)
60        (substring id (match-beginning 1)(match-end 1))
61        ))
62
63 (defconst mu-cite/RCS-ID
64   "$Id: mu-cite.el,v 7.51 1998/02/13 10:11:54 morioka Exp $")
65 (defconst mu-cite/version (get-version-string mu-cite/RCS-ID))
66
67
68 ;;; @ formats
69 ;;;
70
71 (defvar mu-cite/cited-prefix-regexp "\\(^[^ \t\n<>]+>+[ \t]*\\|^[ \t]*$\\)"
72   "*Regexp to match the citation prefix.
73 If match, mu-cite doesn't insert citation prefix.")
74
75 (defvar mu-cite/prefix-format '(prefix-register-verbose "> ")
76   "*List to represent citation prefix.
77 Each elements must be string or method name.")
78
79 (defvar mu-cite/top-format '(in-id
80                              ">>>>>     " from " wrote:\n")
81   "*List to represent top string of citation.
82 Each elements must be string or method name.")
83
84
85 ;;; @ hooks
86 ;;;
87
88 (defvar mu-cite-load-hook nil
89   "*List of functions called after mu-cite is loaded.
90 Use this hook to add your own methods to `mu-cite/default-methods-alist'.")
91
92 (defvar mu-cite/instantiation-hook nil
93   "*List of functions called just before narrowing to the message.")
94
95 (defvar mu-cite/pre-cite-hook nil
96   "*List of functions called before citing a region of text.")
97
98 (defvar mu-cite/post-cite-hook nil
99   "*List of functions called after citing a region of text.")
100
101
102 ;;; @ field
103 ;;;
104
105 (defvar mu-cite/get-field-value-method-alist nil
106   "Alist major-mode vs. function to get field-body of header.")
107
108 (defun mu-cite/get-field-value (name)
109   (or (std11-field-body name)
110       (let ((method (assq major-mode mu-cite/get-field-value-method-alist)))
111         (if method
112             (funcall (cdr method) name)
113           ))))
114
115
116 ;;; @ prefix registration
117 ;;;
118
119 (defvar mu-cite/registration-file (expand-file-name "~/.mu-cite.el")
120   "*The name of the user environment file for mu-cite.")
121
122 (defvar mu-cite/allow-null-string-registration nil
123   "*If non-nil, null-string citation-name is registered.")
124
125 (defvar mu-cite/registration-symbol 'mu-cite/citation-name-alist)
126
127 (defvar mu-cite/citation-name-alist nil)
128 (or (eq 'mu-cite/citation-name-alist mu-cite/registration-symbol)
129     (setq mu-cite/citation-name-alist
130           (symbol-value mu-cite/registration-symbol))
131     )
132 (defvar mu-cite/minibuffer-history nil)
133
134 ;; get citation-name from the database
135 (defun mu-cite/get-citation-name (from)
136   (cdr (assoc from mu-cite/citation-name-alist))
137   )
138
139 ;; register citation-name to the database
140 (defun mu-cite/add-citation-name (name from)
141   (setq mu-cite/citation-name-alist
142         (put-alist from name mu-cite/citation-name-alist))
143   (mu-cite/save-registration-file)
144   )
145
146 ;; load/save registration file
147 (defun mu-cite/load-registration-file ()
148   (let* ((file mu-cite/registration-file)
149          (buffer (get-buffer-create " *mu-register*")))
150     (if (file-readable-p file)
151         (unwind-protect
152             (save-excursion
153               (set-buffer buffer)
154               (erase-buffer)
155               (insert-file-contents file)
156               ;; (eval-buffer)
157               (eval-current-buffer))
158           (kill-buffer buffer))
159       )))
160 (add-hook 'mu-cite-load-hook (function mu-cite/load-registration-file))
161
162 (defun mu-cite/save-registration-file ()
163   (let* ((file mu-cite/registration-file)
164          (buffer (get-buffer-create " *mu-register*")))
165     (unwind-protect
166         (save-excursion
167           (set-buffer buffer)
168           (setq buffer-file-name file)
169           (erase-buffer)
170           (insert ";;; " (file-name-nondirectory file) "\n")
171           (insert ";;; This file is generated automatically by mu-cite "
172                   mu-cite/version "\n\n")
173           (insert "(setq "
174                   (symbol-name mu-cite/registration-symbol)
175                   "\n      '(")
176           (insert (mapconcat
177                    (function prin1-to-string)
178                    mu-cite/citation-name-alist "\n        "))
179           (insert "\n        ))\n\n")
180           (insert ";;; "
181                   (file-name-nondirectory file)
182                   " ends here.\n")
183           (save-buffer))
184       (kill-buffer buffer))))
185
186
187 ;;; @ item methods
188 ;;;
189
190 ;;; @@ ML count
191 ;;;
192
193 (defvar mu-cite/ml-count-field-list
194   '("X-Ml-Count" "X-Mail-Count" "X-Seqno" "X-Sequence" "Mailinglist-Id")
195   "*List of header fields which contain sequence number of mailing list.")
196
197 (defun mu-cite/get-ml-count-method ()
198   (let ((field-list mu-cite/ml-count-field-list))
199     (catch 'tag
200       (while field-list
201         (let* ((field (car field-list))
202                (ml-count (mu-cite/get-field-value field)))
203           (if (and ml-count (string-match "[0-9]+" ml-count))
204               (throw 'tag
205                      (substring ml-count
206                                 (match-beginning 0)(match-end 0))
207                      ))
208           (setq field-list (cdr field-list))
209           )))))
210
211
212 ;;; @@ prefix and registration
213 ;;;
214
215 (defun mu-cite/get-prefix-method ()
216   (or (mu-cite/get-citation-name (mu-cite/get-value 'address))
217       ">")
218   )
219
220 (defun mu-cite/get-prefix-register-method ()
221   (let ((addr (mu-cite/get-value 'address)))
222     (or (mu-cite/get-citation-name addr)
223         (let ((return
224                (read-string "Citation name? "
225                             (or (mu-cite/get-value 'x-attribution)
226                                 (mu-cite/get-value 'full-name))
227                             'mu-cite/minibuffer-history)
228                ))
229           (if (and (or mu-cite/allow-null-string-registration
230                        (not (string-equal return "")))
231                    (y-or-n-p (format "Register \"%s\"? " return)))
232               (mu-cite/add-citation-name return addr)
233             )
234           return))))
235
236 (defun mu-cite/get-prefix-register-verbose-method ()
237   (let* ((addr (mu-cite/get-value 'address))
238          (return1 (mu-cite/get-citation-name addr))
239          (return (read-string "Citation name? "
240                               (or return1
241                                   (mu-cite/get-value 'x-attribution)
242                                   (mu-cite/get-value 'full-name))
243                               'mu-cite/minibuffer-history))
244          )
245     (if (and (or mu-cite/allow-null-string-registration
246                  (not (string-equal return "")))
247              (not (string-equal return return1))
248              (y-or-n-p (format "Register \"%s\"? " return))
249              )
250         (mu-cite/add-citation-name return addr)
251       )
252     return))
253
254
255 ;;; @@ set up
256 ;;;
257
258 (defvar mu-cite/default-methods-alist
259   (list (cons 'from
260               (function
261                (lambda ()
262                  (mu-cite/get-field-value "From")
263                  )))
264         (cons 'date
265               (function
266                (lambda ()
267                  (mu-cite/get-field-value "Date")
268                  )))
269         (cons 'message-id
270               (function
271                (lambda ()
272                  (mu-cite/get-field-value "Message-Id")
273                  )))
274         (cons 'subject
275               (function
276                (lambda ()
277                  (mu-cite/get-field-value "Subject")
278                  )))
279         (cons 'ml-name
280               (function
281                (lambda ()
282                  (mu-cite/get-field-value "X-Ml-Name")
283                  )))
284         (cons 'ml-count (function mu-cite/get-ml-count-method))
285         (cons 'address-structure
286               (function
287                (lambda ()
288                  (car
289                   (std11-parse-address-string (mu-cite/get-value 'from))
290                   ))))
291         (cons 'full-name
292               (function
293                (lambda ()
294                  (std11-full-name-string
295                   (mu-cite/get-value 'address-structure))
296                  )))
297         (cons 'address
298               (function
299                (lambda ()
300                  (std11-address-string
301                   (mu-cite/get-value 'address-structure))
302                  )))
303         (cons 'id
304               (function
305                (lambda ()
306                  (let ((ml-name (mu-cite/get-value 'ml-name)))
307                    (if ml-name
308                        (concat "["
309                                ml-name
310                                " : No."
311                                (mu-cite/get-value 'ml-count)
312                                "]")
313                      (mu-cite/get-value 'message-id)
314                      )))))
315         (cons 'in-id
316               (function
317                (lambda ()
318                  (let ((id (mu-cite/get-value 'id)))
319                    (if id
320                        (format ">>>>> In %s \n" id)
321                      "")))))
322         (cons 'prefix (function mu-cite/get-prefix-method))
323         (cons 'prefix-register
324               (function mu-cite/get-prefix-register-method))
325         (cons 'prefix-register-verbose
326               (function mu-cite/get-prefix-register-verbose-method))
327         (cons 'x-attribution
328               (function
329                (lambda ()
330                  (mu-cite/get-field-value "X-Attribution")
331                  )))
332         ))
333
334
335 ;;; @ fundamentals
336 ;;;
337
338 (defvar mu-cite/methods-alist nil)
339
340 (defun mu-cite/make-methods ()
341   (setq mu-cite/methods-alist
342         (copy-alist mu-cite/default-methods-alist))
343   (run-hooks 'mu-cite/instantiation-hook)
344   )
345
346 (defun mu-cite/get-value (item)
347   (let ((ret (cdr (assoc item mu-cite/methods-alist))))
348     (if (functionp ret)
349         (prog1
350             (setq ret (funcall ret))
351           (set-alist 'mu-cite/methods-alist item ret)
352           )
353       ret)))
354
355 (defun mu-cite/eval-format (list)
356   (save-excursion
357     (mapconcat (function
358                 (lambda (elt)
359                   (cond ((stringp elt) elt)
360                         ((symbolp elt) (mu-cite/get-value elt))
361                         )))
362                list "")
363     ))
364
365
366 ;;; @ main function
367 ;;;
368
369 (defun mu-cite/cite-original ()
370   "Citing filter function.
371 This is callable from the various mail and news readers' reply
372 function according to the agreed upon standard."
373   (interactive)
374   (mu-cite/make-methods)
375   (save-restriction
376     (if (< (mark t) (point))
377         (exchange-point-and-mark))
378     (narrow-to-region (point)(point-max))
379     (run-hooks 'mu-cite/pre-cite-hook)
380     (let ((last-point (point))
381           (top (mu-cite/eval-format mu-cite/top-format))
382           (prefix (mu-cite/eval-format mu-cite/prefix-format))
383           )
384       (if (re-search-forward "^-*$" nil nil)
385           (forward-line 1)
386         )
387       (widen)
388       (delete-region last-point (point))
389       (insert top)
390       (setq last-point (point))
391       (while (< (point)(mark t))
392         (or (looking-at mu-cite/cited-prefix-regexp)
393             (insert prefix))
394         (forward-line 1))
395       (goto-char last-point)
396       )
397     (run-hooks 'mu-cite/post-cite-hook)
398     ))
399
400
401 ;;; @ message editing utilities
402 ;;;
403
404 (defvar citation-mark-chars ">}|"
405   "*String of characters for citation delimiter. [mu-cite.el]")
406
407 (defvar citation-disable-chars "<{"
408   "*String of characters not allowed as citation-prefix.")
409
410 (defun detect-paragraph-cited-prefix ()
411   (save-excursion
412     (goto-char (point-min))
413     (let ((i 0)
414           (prefix
415            (buffer-substring
416             (progn (beginning-of-line)(point))
417             (progn (end-of-line)(point))
418             ))
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                   )
426         (setq prefix
427               (if (stringp ret)
428                   ret
429                 (cadr ret)))
430         (setq i (1+ i))
431         )
432       (cond ((> i 1) prefix)
433             ((> i 0)
434              (goto-char (point-min))
435              (save-restriction
436                (narrow-to-region (point)
437                                  (+ (point)(length prefix)))
438                (goto-char (point-max))
439                (if (re-search-backward
440                     (concat "[" citation-mark-chars "]") nil t)
441                    (progn
442                      (goto-char (match-end 0))
443                      (if (looking-at "[ \t]+")
444                          (goto-char (match-end 0))
445                        )
446                      (buffer-substring (point-min)(point))
447                      )
448                  prefix)))
449             ((progn
450                (goto-char (point-max))
451                (re-search-backward
452                 (concat "[" citation-disable-chars "]") nil t)
453                (re-search-backward
454                 (concat "[" citation-mark-chars "]") nil t)
455                )
456              (goto-char (match-end 0))
457              (if (looking-at "[ \t]+")
458                  (goto-char (match-end 0))
459                )
460              (buffer-substring (point-min)(point))
461              )
462             (t ""))
463       )))
464
465 (defun fill-cited-region (beg end)
466   (interactive "*r")
467   (save-excursion
468     (save-restriction
469       (goto-char end)
470       (and (search-backward "\n" nil t)
471            (setq end (match-end 0))
472            )
473       (narrow-to-region beg end)
474       (let* ((fill-prefix (detect-paragraph-cited-prefix))
475              (pat (concat fill-prefix "\n"))
476              )
477         (goto-char (point-min))
478         (while (search-forward pat nil t)
479           (let ((b (match-beginning 0))
480                 (e (match-end 0))
481                 )
482             (delete-region b e)
483             (if (and (> b (point-min))
484                      (let ((cat (char-category
485                                  (char-before b))))
486                        (or (string-match "a" cat)
487                            (string-match "l" cat)
488                            ))
489                      )
490                 (insert " ")
491               ))
492           )
493         (goto-char (point-min))
494         (fill-region (point-min) (point-max))
495         ))))
496
497 (defun compress-cited-prefix ()
498   (interactive)
499   (save-excursion
500     (goto-char (point-min))
501     (re-search-forward
502      (concat "^" (regexp-quote mail-header-separator) "$") nil t)
503     (while (re-search-forward
504             (concat "^\\([ \t]*[^ \t\n" citation-mark-chars "]*["
505                     citation-mark-chars "]\\)+") nil t)
506       (let* ((b (match-beginning 0))
507              (e (match-end 0))
508              (prefix (buffer-substring b e))
509              ps pe (s 0)
510              (nest (let ((i 0))
511                      (if (string-match "<[^<>]+>" prefix)
512                          (setq prefix (substring prefix 0 (match-beginning 0)))
513                        )
514                      (while (string-match
515                              (concat "\\([" citation-mark-chars "]+\\)[ \t]*")
516                              prefix s)
517                        (setq i (+ i (- (match-end 1)(match-beginning 1)))
518                              ps s
519                              pe (match-beginning 1)
520                              s (match-end 0)
521                              ))
522                      i)))
523         (if (and ps (< ps pe))
524             (progn
525               (delete-region b e)
526               (insert (concat (substring prefix ps pe) (make-string nest ?>)))
527               ))))))
528
529 (defun replace-top-string (old new)
530   (interactive "*sOld string: \nsNew string: ")
531   (while (re-search-forward
532           (concat "^" (regexp-quote old)) nil t)
533     (replace-match new)
534     ))
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                   ))
548       (setq p (+ p (char-length c1)))
549       )
550     (and (> p 0)
551          (let ((matched (substring str1 0 p))
552                (r1 (and (< p len1)(substring str1 p)))
553                (r2 (and (< p len2)(substring str2 p)))
554                )
555            (if (eq r1 r2)
556                matched
557              (list 'seq matched (list 'or r1 r2))
558              )))))
559
560
561 ;;; @ end
562 ;;;
563
564 (provide 'mu-cite)
565
566 (run-hooks 'mu-cite-load-hook)
567
568 ;;; mu-cite.el ends here