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