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