tm 7.102.
[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.44 $
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.44 1997/01/13 15:49:13 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
99   (list (cons 'mh-letter-mode
100               (function
101                (lambda (name)
102                  (if (and (stringp mh-sent-from-folder)
103                           (numberp mh-sent-from-msg))
104                      (save-excursion
105                        (set-buffer mh-sent-from-folder)
106                        (set-buffer mh-show-buffer)
107                        (and (boundp 'mime::preview/article-buffer)
108                             (bufferp mime::preview/article-buffer)
109                             (set-buffer mime::preview/article-buffer))
110                        (std11-field-body name)
111                        ))
112                  )))))
113
114 (defun mu-cite/get-field-value (name)
115   (or (std11-field-body name)
116       (let ((method (assq major-mode mu-cite/get-field-value-method-alist)))
117         (if method
118             (funcall (cdr method) name)
119           ))))
120
121
122 ;;; @ prefix registration
123 ;;;
124
125 (defvar mu-cite/registration-file
126   (expand-file-name "~/.mu-cite.el")
127   "*The name of the user environment file for mu-cite.")
128
129 (defvar mu-cite/allow-null-string-registration nil
130   "*If non-nil, null-string citation-name is registered.")
131
132 (defvar mu-cite/registration-symbol 'mu-cite/citation-name-alist)
133
134 (defvar mu-cite/citation-name-alist nil)
135 (load mu-cite/registration-file t t t)
136 (or (eq 'mu-cite/citation-name-alist mu-cite/registration-symbol)
137     (setq mu-cite/citation-name-alist
138           (symbol-value mu-cite/registration-symbol))
139     )
140 (defvar mu-cite/minibuffer-history nil)
141
142 ;; get citation-name from the database
143 (defun mu-cite/get-citation-name (from)
144   (assoc-value from mu-cite/citation-name-alist)
145   )
146
147 ;; register citation-name to the database
148 (defun mu-cite/add-citation-name (name from)
149   (setq mu-cite/citation-name-alist
150         (put-alist from name mu-cite/citation-name-alist))
151   (mu-cite/save-to-file)
152   )
153
154 ;; save to file
155 (defun mu-cite/save-to-file ()
156   (let* ((filename mu-cite/registration-file)
157          (buffer (get-buffer-create " *mu-register*")))
158     (save-excursion
159       (set-buffer buffer)
160       (setq buffer-file-name filename)
161       (erase-buffer)
162       (insert
163        (format ";;; %s\n" (file-name-nondirectory filename)))
164       (insert
165        (format ";;; This file is generated automatically by mu-cite %s.\n\n"
166                mu-cite/version))
167       (insert (format "(setq %s\n      '(" mu-cite/registration-symbol))
168       (insert (mapconcat
169                (function prin1-to-string)
170                mu-cite/citation-name-alist "\n        "))
171       (insert "\n        ))\n\n")
172       (insert
173        (format ";;; %s ends here.\n" (file-name-nondirectory filename)))
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 (assoc-value 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   (mapconcat (function
348               (lambda (elt)
349                 (cond ((stringp elt) elt)
350                       ((symbolp elt) (mu-cite/get-value elt))
351                       )))
352              list "")
353   )
354
355
356 ;;; @ main function
357 ;;;
358
359 (defun mu-cite/cite-original ()
360   "Citing filter function.
361 This is callable from the various mail and news readers' reply
362 function according to the agreed upon standard."
363   (interactive)
364   (mu-cite/make-methods)
365   (save-restriction
366     (if (< (mark t) (point))
367         (exchange-point-and-mark))
368     (narrow-to-region (point)(point-max))
369     (run-hooks 'mu-cite/pre-cite-hook)
370     (let ((last-point (point))
371           (top (mu-cite/eval-format mu-cite/top-format))
372           (prefix (mu-cite/eval-format mu-cite/prefix-format))
373           )
374       (if (re-search-forward "^-*$" nil nil)
375           (forward-line 1)
376         )
377       (widen)
378       (delete-region last-point (point))
379       (insert top)
380       (setq last-point (point))
381       (while (< (point)(mark t))
382         (or (looking-at mu-cite/cited-prefix-regexp)
383             (insert prefix))
384         (forward-line 1))
385       (goto-char last-point)
386       )
387     (run-hooks 'mu-cite/post-cite-hook)
388     ))
389
390
391 ;;; @ message editing utilities
392 ;;;
393   
394 (defvar citation-mark-chars ">}|"
395   "*String of characters for citation delimiter. [mu-cite.el]")
396
397 (defun detect-paragraph-cited-prefix ()
398   (save-excursion
399     (goto-char (point-min))
400     (let ((i 0)
401           (prefix
402            (buffer-substring
403             (progn (beginning-of-line)(point))
404             (progn (end-of-line)(point))
405             ))
406           str ret)
407       (while (and (= (forward-line) 0)
408                   (setq str (buffer-substring
409                              (progn (beginning-of-line)(point))
410                              (progn (end-of-line)(point))))
411                   (setq ret (string-compare-from-top prefix str))
412                   )
413         (setq prefix (second ret))
414         (setq i (1+ i))
415         )
416       (cond ((> i 1) prefix)
417             ((> i 0)
418              (goto-char (point-min))
419              (save-restriction
420                (narrow-to-region (point)
421                                  (+ (point)(length prefix)))
422                (goto-char (point-max))
423                (if (re-search-backward
424                     (concat "[" citation-mark-chars "]") nil t)
425                    (progn
426                      (goto-char (match-end 0))
427                      (if (looking-at "[ \t]+")
428                          (goto-char (match-end 0))
429                        )
430                      (buffer-substring (point-min)(point))
431                      )
432                  prefix)))
433             ((progn
434                (goto-char (point-max))
435                (re-search-backward (concat "[" citation-mark-chars "]")
436                                    nil t)
437                )
438              (goto-char (match-end 0))
439              (if (looking-at "[ \t]+")
440                  (goto-char (match-end 0))
441                )
442              (buffer-substring (point-min)(point))
443              )
444             (t ""))
445       )))
446
447 (defun fill-cited-region (beg end)
448   (interactive "*r")
449   (save-excursion
450     (save-restriction
451       (goto-char end)
452       (and (search-backward "\n" nil t)
453            (setq end (match-end 0))
454            )
455       (narrow-to-region beg end)
456       (let* ((fill-prefix (detect-paragraph-cited-prefix))
457              (pat (concat fill-prefix "\n"))
458              )
459         (goto-char (point-min))
460         (while (search-forward pat nil t)
461           (let ((b (match-beginning 0))
462                 (e (match-end 0))
463                 )
464             (delete-region b e)
465             (if (and (> b (point-min))
466                      (let ((cat (char-category
467                                  (char-before b))))
468                        (or (string-match "a" cat)
469                            (string-match "l" cat)
470                            ))
471                      )
472                 (insert " ")
473               ))
474           )
475         (goto-char (point-min))
476         (fill-region (point-min) (point-max))
477         ))))
478
479 (defun compress-cited-prefix ()
480   (interactive)
481   (save-excursion
482     (goto-char (point-min))
483     (re-search-forward
484      (concat "^" (regexp-quote mail-header-separator) "$") nil t)
485     (while (re-search-forward
486             (concat "^\\([ \t]*[^ \t\n" citation-mark-chars "]*["
487                     citation-mark-chars "]\\)+") nil t)
488       (let* ((b (match-beginning 0))
489              (e (match-end 0))
490              (prefix (buffer-substring b e))
491              ps pe (s 0)
492              (nest (let ((i 0))
493                      (if (string-match "<[^<>]+>" prefix)
494                          (setq prefix (substring prefix 0 (match-beginning 0)))
495                        )
496                      (while (string-match
497                              (concat "\\([" citation-mark-chars "]+\\)[ \t]*")
498                              prefix s)
499                        (setq i (+ i (- (match-end 1)(match-beginning 1)))
500                              ps s
501                              pe (match-beginning 1)
502                              s (match-end 0)
503                              ))
504                      i)))
505         (if (and ps (< ps pe))
506             (progn
507               (delete-region b e)
508               (insert (concat (substring prefix ps pe) (make-string nest ?>)))
509               ))))))
510
511 (defun replace-top-string (old new)
512   (interactive "*sOld string: \nsNew string: ")
513   (while (re-search-forward
514           (concat "^" (regexp-quote old)) nil t)
515     (replace-match new)
516     ))
517
518
519 ;;; @ end
520 ;;;
521
522 (provide 'mu-cite)
523
524 (run-hooks 'mu-cite-load-hook)
525
526 ;;; mu-cite.el ends here