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