This commit was generated by cvs2svn to compensate for changes in r416,
[elisp/tm.git] / tm-def.el
1 ;;;
2 ;;; tm-def.el --- definition module for tm
3 ;;;
4 ;;; Copyright (C) 1995 Free Software Foundation, Inc.
5 ;;; Copyright (C) 1995,1996 MORIOKA Tomohiko
6 ;;;
7 ;;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
8 ;;; Version:
9 ;;;     $Id: tm-def.el,v 7.61 1996/07/15 14:03:57 morioka Exp $
10 ;;; Keywords: mail, news, MIME, multimedia, definition
11 ;;;
12 ;;; This file is part of tm (Tools for MIME).
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
17 ;;; (at your option) any later version.
18 ;;;
19 ;;; This program is distributed in the hope that it will be useful,
20 ;;; but 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 This program.  If not, write to the Free Software
26 ;;; Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
27 ;;;
28 ;;; Code:
29
30 (require 'emu)
31 (require 'tl-822)
32
33
34 ;;; @ variables
35 ;;;
36
37 (defvar mime/tmp-dir (or (getenv "TM_TMP_DIR") "/tmp/"))
38
39 (defvar mime/use-multi-frame
40   (and (>= emacs-major-version 19) window-system))
41
42 (defvar mime/find-file-function
43   (if mime/use-multi-frame
44       (function find-file-other-frame)
45     (function find-file)
46     ))
47
48 (defvar mime/output-buffer-window-is-shared-with-bbdb t
49   "*If t, mime/output-buffer window is shared with BBDB window.")
50
51
52 ;;; @ constants
53 ;;;
54
55 (defconst mime/output-buffer-name "*MIME-out*")
56 (defconst mime/temp-buffer-name " *MIME-temp*")
57
58
59 ;;; @ charset and encoding
60 ;;;
61
62 (defvar mime-charset-type-list
63   '((us-ascii           7 nil)
64     (iso-8859-1         8 "quoted-printable")
65     (iso-8859-2         8 "quoted-printable")
66     (iso-8859-3         8 "quoted-printable")
67     (iso-8859-4         8 "quoted-printable")
68     (iso-8859-5         8 "quoted-printable")
69     (koi8-r             8 "quoted-printable")
70     (iso-8859-7         8 "quoted-printable")
71     (iso-8859-8         8 "quoted-printable")
72     (iso-8859-9         8 "quoted-printable")
73     (iso-2022-jp        7 "base64")
74     (iso-2022-kr        7 "base64")
75     (euc-kr             8 "base64")
76     (big5               8 "base64")
77     (iso-2022-jp-2      7 "base64")
78     (iso-2022-int-1     7 "base64")
79     ))
80
81 (defun mime/encoding-name (transfer-level &optional not-omit)
82   (cond ((> transfer-level 8) "binary")
83         ((= transfer-level 8) "8bit")
84         (not-omit "7bit")
85         ))
86
87 (defun mime/make-charset-default-encoding-alist (transfer-level)
88   (mapcar (function
89            (lambda (charset-type)
90              (let ((charset  (upcase (symbol-name (car charset-type))))
91                    (type     (nth 1 charset-type))
92                    (encoding (nth 2 charset-type))
93                    )
94                (if (<= type transfer-level)
95                    (cons charset (mime/encoding-name type))
96                  (cons charset encoding)
97                  ))))
98           mime-charset-type-list))
99
100
101 ;;; @ coding-system
102 ;;;
103
104 (defun mime-charset-decode-region (beg end charset &optional encoding)
105   (decode-mime-charset-region beg end (or charset default-mime-charset))
106   )
107
108
109 ;;; @ button
110 ;;;
111
112 (defun tm:set-face-region (b e face)
113   (let ((overlay (tl:make-overlay b e)))
114     (tl:overlay-put overlay 'face face)
115     ))
116
117 (setq tm:button-face 'bold)
118 (setq tm:mouse-face 'highlight)
119
120 (defun tm:add-button (from to func &optional data)
121   "Create a button between FROM and TO with callback FUNC and data DATA."
122   (and tm:button-face
123        (tl:overlay-put (tl:make-overlay from to) 'face tm:button-face))
124   (tl:add-text-properties from to
125                           (append (and tm:mouse-face
126                                        (list 'mouse-face tm:mouse-face))
127                                   (list 'tm-callback func)
128                                   (and data (list 'tm-data data))
129                                   ))
130   )
131
132 (defvar tm:mother-button-dispatcher nil)
133
134 (defun tm:button-dispatcher (event)
135   "Select the button under point."
136   (interactive "e")
137   (let (buf point func data)
138     (save-window-excursion
139       (mouse-set-point event)
140       (setq buf (current-buffer)
141             point (point)
142             func (get-text-property (point) 'tm-callback)
143             data (get-text-property (point) 'tm-data)
144             )
145       )
146     (save-excursion
147       (set-buffer buf)
148       (goto-char point)
149       (if func
150           (apply func data)
151         (if (fboundp tm:mother-button-dispatcher)
152             (funcall tm:mother-button-dispatcher event)
153           )
154         ))))
155
156
157 ;;; @ for URL
158 ;;;
159
160 (defvar tm:URL-regexp
161   "\\(http\\|ftp\\|file\\|gopher\\|news\\|telnet\\|wais\\|mailto\\):\\(//[-a-zA-Z0-9_.]+:[0-9]*\\)?[-a-zA-Z0-9_=?#$@~`%&*+|\\/.,]*[-a-zA-Z0-9_=#$@~`%&*+|\\/]")
162
163 (defvar browse-url-browser-function nil)
164
165 (defun tm:browse-url (&optional url)
166   (if (fboundp browse-url-browser-function)
167       (if url 
168         (funcall browse-url-browser-function url)
169       (call-interactively browse-url-browser-function))
170     (if (fboundp tm:mother-button-dispatcher)
171         (call-interactively tm:mother-button-dispatcher)
172       )
173     ))
174
175
176 ;;; @ definitions about MIME
177 ;;;
178
179 (defconst mime/tspecials "][\000-\040()<>@,\;:\\\"/?.=")
180 (defconst mime/token-regexp (concat "[^" mime/tspecials "]+"))
181 (defconst mime/charset-regexp mime/token-regexp)
182
183 (defconst mime/content-type-subtype-regexp
184   (concat mime/token-regexp "/" mime/token-regexp))
185 (defconst mime/content-parameter-value-regexp
186   (concat "\\("
187           rfc822/quoted-string-regexp
188           "\\|[^; \t\n]*\\)"))
189
190 (defconst mime/disposition-type-regexp mime/token-regexp)
191
192
193 ;;; @@ Base64
194 ;;;
195
196 (defconst base64-token-regexp "[A-Za-z0-9+/=]")
197
198 (defconst mime/B-encoded-text-regexp
199   (concat "\\("
200           base64-token-regexp
201           base64-token-regexp
202           base64-token-regexp
203           base64-token-regexp
204           "\\)+"))
205 (defconst mime/B-encoding-and-encoded-text-regexp
206   (concat "\\(B\\)\\?" mime/B-encoded-text-regexp))
207
208
209 ;;; @@ Quoted-Printable
210 ;;;
211
212 (defconst quoted-printable-hex-chars "0123456789ABCDEF")
213 (defconst quoted-printable-octet-regexp
214   (concat "=[" quoted-printable-hex-chars
215           "][" quoted-printable-hex-chars "]"))
216
217 (defconst mime/Q-encoded-text-regexp
218   (concat "\\([^=?]\\|" quoted-printable-octet-regexp "\\)+"))
219 (defconst mime/Q-encoding-and-encoded-text-regexp
220   (concat "\\(Q\\)\\?" mime/Q-encoded-text-regexp))
221
222
223 ;;; @ rot13-47
224 ;;;
225 ;; caesar-region written by phr@prep.ai.mit.edu  Nov 86
226 ;; modified by tower@prep Nov 86
227 ;; gnus-caesar-region
228 ;; Modified by umerin@flab.flab.Fujitsu.JUNET for ROT47.
229 (defun tm:caesar-region (&optional n)
230   "Caesar rotation of region by N, default 13, for decrypting netnews.
231 ROT47 will be performed for Japanese text in any case."
232   (interactive (if current-prefix-arg   ; Was there a prefix arg?
233                    (list (prefix-numeric-value current-prefix-arg))
234                  (list nil)))
235   (cond ((not (numberp n)) (setq n 13))
236         (t (setq n (mod n 26))))        ;canonicalize N
237   (if (not (zerop n))           ; no action needed for a rot of 0
238       (progn
239         (if (or (not (boundp 'caesar-translate-table))
240                 (/= (aref caesar-translate-table ?a) (+ ?a n)))
241             (let ((i 0) (lower "abcdefghijklmnopqrstuvwxyz") upper)
242               (message "Building caesar-translate-table...")
243               (setq caesar-translate-table (make-vector 256 0))
244               (while (< i 256)
245                 (aset caesar-translate-table i i)
246                 (setq i (1+ i)))
247               (setq lower (concat lower lower) upper (upcase lower) i 0)
248               (while (< i 26)
249                 (aset caesar-translate-table (+ ?a i) (aref lower (+ i n)))
250                 (aset caesar-translate-table (+ ?A i) (aref upper (+ i n)))
251                 (setq i (1+ i)))
252               ;; ROT47 for Japanese text.
253               ;; Thanks to ichikawa@flab.fujitsu.junet.
254               (setq i 161)
255               (let ((t1 (logior ?O 128))
256                     (t2 (logior ?! 128))
257                     (t3 (logior ?~ 128)))
258                 (while (< i 256)
259                   (aset caesar-translate-table i
260                         (let ((v (aref caesar-translate-table i)))
261                           (if (<= v t1) (if (< v t2) v (+ v 47))
262                             (if (<= v t3) (- v 47) v))))
263                   (setq i (1+ i))))
264               (message "Building caesar-translate-table...done")))
265         (let ((from (region-beginning))
266               (to (region-end))
267               (i 0) str len)
268           (setq str (buffer-substring from to))
269           (setq len (length str))
270           (while (< i len)
271             (aset str i (aref caesar-translate-table (aref str i)))
272             (setq i (1+ i)))
273           (goto-char from)
274           (delete-region from to)
275           (insert str)))))
276
277
278 ;;; @ field
279 ;;;
280
281 (defun tm:set-fields (sym field-list &optional regexp-sym)
282   (or regexp-sym
283       (setq regexp-sym
284             (let ((name (symbol-name sym)))
285               (intern
286                (concat (if (string-match "\\(.*\\)-list" name)
287                            (substring name 0 (match-end 1))
288                          name)
289                        "-regexp")
290                )))
291       )
292   (set sym field-list)
293   (set regexp-sym
294        (concat "^" (apply (function regexp-or) field-list) ":"))
295   )
296
297 (defun tm:add-fields (sym field-list &optional regexp-sym)
298   (or regexp-sym
299       (setq regexp-sym
300             (let ((name (symbol-name sym)))
301               (intern
302                (concat (if (string-match "\\(.*\\)-list" name)
303                            (substring name 0 (match-end 1))
304                          name)
305                        "-regexp")
306                )))
307       )
308   (let ((fields (eval sym)))
309     (mapcar (function
310              (lambda (field)
311                (or (member field fields)
312                    (setq fields (cons field fields))
313                    )
314                ))
315             (reverse field-list)
316             )
317     (set regexp-sym
318          (concat "^" (apply (function regexp-or) fields) ":"))
319     (set sym fields)
320     ))
321
322 (defun tm:delete-fields (sym field-list &optional regexp-sym)
323   (or regexp-sym
324       (setq regexp-sym
325             (let ((name (symbol-name sym)))
326               (intern
327                (concat (if (string-match "\\(.*\\)-list" name)
328                            (substring name 0 (match-end 1))
329                          name)
330                        "-regexp")
331                )))
332       )
333   (let ((fields (eval sym)))
334     (mapcar (function
335              (lambda (field)
336                (setq fields (delete field fields))
337                ))
338             field-list)
339     (set regexp-sym
340          (concat "^" (apply (function regexp-or) fields) ":"))
341     (set sym fields)
342     ))
343
344
345 ;;; @ end
346 ;;;
347
348 (provide 'tm-def)
349
350 ;;; tm-def.el ends here