mu-cite-980221.
[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   (mapconcat (function
357               (lambda (elt)
358                 (cond ((stringp elt) elt)
359                       ((symbolp elt) (mu-cite/get-value elt))
360                       )))
361              list "")
362   )
363
364
365 ;;; @ main function
366 ;;;
367
368 (defun mu-cite/cite-original ()
369   "Citing filter function.
370 This is callable from the various mail and news readers' reply
371 function according to the agreed upon standard."
372   (interactive)
373   (mu-cite/make-methods)
374   (save-restriction
375     (if (< (mark t) (point))
376         (exchange-point-and-mark))
377     (narrow-to-region (point)(point-max))
378     (run-hooks 'mu-cite/pre-cite-hook)
379     (let ((last-point (point))
380           (top (mu-cite/eval-format mu-cite/top-format))
381           (prefix (mu-cite/eval-format mu-cite/prefix-format))
382           )
383       (if (re-search-forward "^-*$" nil nil)
384           (forward-line 1)
385         )
386       (widen)
387       (delete-region last-point (point))
388       (insert top)
389       (setq last-point (point))
390       (while (< (point)(mark t))
391         (or (looking-at mu-cite/cited-prefix-regexp)
392             (insert prefix))
393         (forward-line 1))
394       (goto-char last-point)
395       )
396     (run-hooks 'mu-cite/post-cite-hook)
397     ))
398
399
400 ;;; @ message editing utilities
401 ;;;
402
403 (defvar citation-mark-chars ">}|"
404   "*String of characters for citation delimiter. [mu-cite.el]")
405
406 (defvar citation-disable-chars "<{"
407   "*String of characters not allowed as citation-prefix.")
408
409 (defun detect-paragraph-cited-prefix ()
410   (save-excursion
411     (goto-char (point-min))
412     (let ((i 0)
413           (prefix
414            (buffer-substring
415             (progn (beginning-of-line)(point))
416             (progn (end-of-line)(point))
417             ))
418           str ret)
419       (while (and (= (forward-line) 0)
420                   (setq str (buffer-substring
421                              (progn (beginning-of-line)(point))
422                              (progn (end-of-line)(point))))
423                   (setq ret (string-compare-from-top prefix str))
424                   )
425         (setq prefix
426               (if (stringp ret)
427                   ret
428                 (second ret)))
429         (setq i (1+ i))
430         )
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                        )
445                      (buffer-substring (point-min)(point))
446                      )
447                  prefix)))
448             ((progn
449                (goto-char (point-max))
450                (re-search-backward
451                 (concat "[" citation-disable-chars "]") nil t)
452                (re-search-backward
453                 (concat "[" citation-mark-chars "]") nil t)
454                )
455              (goto-char (match-end 0))
456              (if (looking-at "[ \t]+")
457                  (goto-char (match-end 0))
458                )
459              (buffer-substring (point-min)(point))
460              )
461             (t ""))
462       )))
463
464 (defun fill-cited-region (beg end)
465   (interactive "*r")
466   (save-excursion
467     (save-restriction
468       (goto-char end)
469       (and (search-backward "\n" nil t)
470            (setq end (match-end 0))
471            )
472       (narrow-to-region beg end)
473       (let* ((fill-prefix (detect-paragraph-cited-prefix))
474              (pat (concat fill-prefix "\n"))
475              )
476         (goto-char (point-min))
477         (while (search-forward pat nil t)
478           (let ((b (match-beginning 0))
479                 (e (match-end 0))
480                 )
481             (delete-region b e)
482             (if (and (> b (point-min))
483                      (let ((cat (char-category
484                                  (char-before b))))
485                        (or (string-match "a" cat)
486                            (string-match "l" cat)
487                            ))
488                      )
489                 (insert " ")
490               ))
491           )
492         (goto-char (point-min))
493         (fill-region (point-min) (point-max))
494         ))))
495
496 (defun compress-cited-prefix ()
497   (interactive)
498   (save-excursion
499     (goto-char (point-min))
500     (re-search-forward
501      (concat "^" (regexp-quote mail-header-separator) "$") nil t)
502     (while (re-search-forward
503             (concat "^\\([ \t]*[^ \t\n" citation-mark-chars "]*["
504                     citation-mark-chars "]\\)+") nil t)
505       (let* ((b (match-beginning 0))
506              (e (match-end 0))
507              (prefix (buffer-substring b e))
508              ps pe (s 0)
509              (nest (let ((i 0))
510                      (if (string-match "<[^<>]+>" prefix)
511                          (setq prefix (substring prefix 0 (match-beginning 0)))
512                        )
513                      (while (string-match
514                              (concat "\\([" citation-mark-chars "]+\\)[ \t]*")
515                              prefix s)
516                        (setq i (+ i (- (match-end 1)(match-beginning 1)))
517                              ps s
518                              pe (match-beginning 1)
519                              s (match-end 0)
520                              ))
521                      i)))
522         (if (and ps (< ps pe))
523             (progn
524               (delete-region b e)
525               (insert (concat (substring prefix ps pe) (make-string nest ?>)))
526               ))))))
527
528 (defun replace-top-string (old new)
529   (interactive "*sOld string: \nsNew string: ")
530   (while (re-search-forward
531           (concat "^" (regexp-quote old)) nil t)
532     (replace-match new)
533     ))
534
535 (defun string-compare-from-top (str1 str2)
536   (let* ((len1 (length str1))
537          (len2 (length str2))
538          (len (min len1 len2))
539          (p 0)
540          c1 c2)
541     (while (and (< p len)
542                 (progn
543                   (setq c1 (sref str1 p)
544                         c2 (sref str2 p))
545                   (eq c1 c2)
546                   ))
547       (setq p (+ p (char-length c1)))
548       )
549     (and (> p 0)
550          (let ((matched (substring str1 0 p))
551                (r1 (and (< p len1)(substring str1 p)))
552                (r2 (and (< p len2)(substring str2 p)))
553                )
554            (if (eq r1 r2)
555                matched
556              (list 'seq matched (list 'or r1 r2))
557              )))))
558
559
560 ;;; @ end
561 ;;;
562
563 (provide 'mu-cite)
564
565 (run-hooks 'mu-cite-load-hook)
566
567 ;;; mu-cite.el ends here