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