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