26fcd6d9307f11a0c6e68b4d2933fbbafd931c1d
[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.12 1996/01/12 13:59:13 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
49 ;;; @ constants
50 ;;;
51
52 (defconst mime/output-buffer-name "*MIME-out*")
53 (defconst mime/temp-buffer-name " *MIME-temp*")
54
55
56 ;;; @ for various Emacs variants
57 ;;;
58
59 (cond ((boundp 'MULE)  (require 'tm-mule))
60       ((boundp 'NEMACS)(require 'tm-nemacs))
61       (t               (require 'tm-orig))
62       )
63
64
65 ;;; @ charset and encoding
66 ;;;
67
68 (defun mime/find-charset (lcl)
69   (if lcl
70       (or (cdr (some-element
71                 (function
72                  (lambda (elt)
73                    (subsetp lcl (car elt))
74                    ))
75                 mime/lc-charset-alist)
76                )
77           mime/unknown-charset)
78     ))
79
80 (defun mime/find-charset-region (beg end)
81   (mime/find-charset (cons lc-ascii (find-charset-region beg end)))
82   )
83
84 (defvar mime/charset-default-encoding-alist
85   '(("US-ASCII"       . nil)
86     ("ISO-8859-1"     . "quoted-printable")
87     ("ISO-8859-2"     . "quoted-printable")
88     ("ISO-8859-3"     . "quoted-printable")
89     ("ISO-8859-4"     . "quoted-printable")
90 ;;; ("ISO-8859-5"     . "quoted-printable")
91     ("KOI8-R"         . "quoted-printable")
92     ("ISO-8859-7"     . "quoted-printable")
93     ("ISO-8859-8"     . "quoted-printable")
94     ("ISO-8859-9"     . "quoted-printable")
95     ("ISO-2022-JP"    . nil)
96     ("ISO-2022-KR"    . nil)
97     ("EUC-KR"         . nil)
98     ("ISO-2022-JP-2"  . nil)
99     ("ISO-2022-INT-1" . nil)
100     ))
101
102
103 ;;; @ button
104 ;;;
105
106 (defun tm:set-face-region (b e face)
107   (let ((overlay (tl:make-overlay b e)))
108     (tl:overlay-put overlay 'face face)
109     ))
110
111 (setq tm:button-face 'bold)
112 (setq tm:mouse-face 'highlight)
113
114 (defun tm:add-button (from to func &optional data)
115   "Create a button between FROM and TO with callback FUNC and data DATA."
116   (and tm:button-face
117        (tl:overlay-put (tl:make-overlay from to) 'face tm:button-face))
118   (tl:add-text-properties from to
119                           (append (and tm:mouse-face
120                                        (list 'mouse-face tm:mouse-face))
121                                   (list 'tm-callback func)
122                                   (and data (list 'tm-data data))
123                                   ))
124   )
125
126 (defvar tm:mother-button-dispatcher nil)
127
128 (defun tm:button-dispatcher (event)
129   "Select the button under point."
130   (interactive "e")
131   (mouse-set-point event)
132   (let ((func (get-text-property (point) 'tm-callback))
133         (data (get-text-property (point) 'tm-data))
134         )
135     (if func
136         (apply func data)
137       (if (fboundp tm:mother-button-dispatcher)
138           (funcall tm:mother-button-dispatcher event)
139         )
140       )))
141
142
143 ;;; @ for URL
144 ;;;
145
146 (defvar tm:URL-regexp
147   "\\(http\\|ftp\\|file\\|gopher\\|news\\|telnet\\|wais\\|mailto\\):\\(//[-a-zA-Z0-9_.]+:[0-9]*\\)?[-a-zA-Z0-9_=?#$@~`%&*+|\\/.,]*[-a-zA-Z0-9_=#$@~`%&*+|\\/]")
148
149 (defvar browse-url-browser-function nil)
150
151 (defun tm:browse-url (&optional url)
152   (if (fboundp browse-url-browser-function)
153       (call-interactively browse-url-browser-function)
154     (if (fboundp tm:mother-button-dispatcher)
155         (call-interactively tm:mother-button-dispatcher)
156       )
157     ))
158
159
160 ;;; @ definitions about MIME
161 ;;;
162
163 (defconst mime/tspecials "][\000-\040()<>@,\;:\\\"/?.=")
164 (defconst mime/token-regexp (concat "[^" mime/tspecials "]+"))
165 (defconst mime/charset-regexp mime/token-regexp)
166
167 (defconst mime/content-type-subtype-regexp
168   (concat mime/token-regexp "/" mime/token-regexp))
169 (defconst mime/content-parameter-value-regexp
170   (concat "\\("
171           rfc822/quoted-string-regexp
172           "\\|[^; \t\n]*\\)"))
173
174 (defconst mime/disposition-type-regexp mime/token-regexp)
175
176
177 ;;; @@ Base64
178 ;;;
179
180 (defconst base64-token-regexp "[A-Za-z0-9+/=]")
181
182 (defconst mime/B-encoded-text-regexp
183   (concat "\\("
184           base64-token-regexp
185           base64-token-regexp
186           base64-token-regexp
187           base64-token-regexp
188           "\\)+"))
189 (defconst mime/B-encoding-and-encoded-text-regexp
190   (concat "\\(B\\)\\?" mime/B-encoded-text-regexp))
191
192
193 ;;; @@ Quoted-Printable
194 ;;;
195
196 (defconst quoted-printable-hex-chars "0123456789ABCDEF")
197 (defconst quoted-printable-octet-regexp
198   (concat "=[" quoted-printable-hex-chars
199           "][" quoted-printable-hex-chars "]"))
200
201 (defconst mime/Q-encoded-text-regexp
202   (concat "\\([^=?]\\|" quoted-printable-octet-regexp "\\)+"))
203 (defconst mime/Q-encoding-and-encoded-text-regexp
204   (concat "\\(Q\\)\\?" mime/Q-encoded-text-regexp))
205
206
207 ;;; @ rot13-47
208 ;;;
209 ;; caesar-region written by phr@prep.ai.mit.edu  Nov 86
210 ;; modified by tower@prep Nov 86
211 ;; gnus-caesar-region
212 ;; Modified by umerin@flab.flab.Fujitsu.JUNET for ROT47.
213 (defun tm:caesar-region (&optional n)
214   "Caesar rotation of region by N, default 13, for decrypting netnews.
215 ROT47 will be performed for Japanese text in any case."
216   (interactive (if current-prefix-arg   ; Was there a prefix arg?
217                    (list (prefix-numeric-value current-prefix-arg))
218                  (list nil)))
219   (cond ((not (numberp n)) (setq n 13))
220         (t (setq n (mod n 26))))        ;canonicalize N
221   (if (not (zerop n))           ; no action needed for a rot of 0
222       (progn
223         (if (or (not (boundp 'caesar-translate-table))
224                 (/= (aref caesar-translate-table ?a) (+ ?a n)))
225             (let ((i 0) (lower "abcdefghijklmnopqrstuvwxyz") upper)
226               (message "Building caesar-translate-table...")
227               (setq caesar-translate-table (make-vector 256 0))
228               (while (< i 256)
229                 (aset caesar-translate-table i i)
230                 (setq i (1+ i)))
231               (setq lower (concat lower lower) upper (upcase lower) i 0)
232               (while (< i 26)
233                 (aset caesar-translate-table (+ ?a i) (aref lower (+ i n)))
234                 (aset caesar-translate-table (+ ?A i) (aref upper (+ i n)))
235                 (setq i (1+ i)))
236               ;; ROT47 for Japanese text.
237               ;; Thanks to ichikawa@flab.fujitsu.junet.
238               (setq i 161)
239               (let ((t1 (logior ?O 128))
240                     (t2 (logior ?! 128))
241                     (t3 (logior ?~ 128)))
242                 (while (< i 256)
243                   (aset caesar-translate-table i
244                         (let ((v (aref caesar-translate-table i)))
245                           (if (<= v t1) (if (< v t2) v (+ v 47))
246                             (if (<= v t3) (- v 47) v))))
247                   (setq i (1+ i))))
248               (message "Building caesar-translate-table...done")))
249         (let ((from (region-beginning))
250               (to (region-end))
251               (i 0) str len)
252           (setq str (buffer-substring from to))
253           (setq len (length str))
254           (while (< i len)
255             (aset str i (aref caesar-translate-table (aref str i)))
256             (setq i (1+ i)))
257           (goto-char from)
258           (delete-region from to)
259           (insert str)))))
260
261
262 ;;; @ field
263 ;;;
264
265 (defun tm:set-fields (sym field-list &optional regexp-sym)
266   (or regexp-sym
267       (setq regexp-sym
268             (let ((name (symbol-name sym)))
269               (intern
270                (concat (if (string-match "\\(.*\\)-list" name)
271                            (substring name 0 (match-end 1))
272                          name)
273                        "-regexp")
274                )))
275       )
276   (set sym field-list)
277   (set regexp-sym
278        (concat "^" (apply (function regexp-or) field-list) ":"))
279   )
280
281 (defun tm:add-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   (let ((fields (eval sym)))
293     (mapcar (function
294              (lambda (field)
295                (or (member field fields)
296                    (setq fields (cons field fields))
297                    )
298                ))
299             (reverse field-list)
300             )
301     (set regexp-sym
302          (concat "^" (apply (function regexp-or) fields) ":"))
303     (set sym fields)
304     ))
305
306 (defun tm:delete-fields (sym field-list &optional regexp-sym)
307   (or regexp-sym
308       (setq regexp-sym
309             (let ((name (symbol-name sym)))
310               (intern
311                (concat (if (string-match "\\(.*\\)-list" name)
312                            (substring name 0 (match-end 1))
313                          name)
314                        "-regexp")
315                )))
316       )
317   (let ((fields (eval sym)))
318     (mapcar (function
319              (lambda (field)
320                (setq fields (delete field fields))
321                ))
322             field-list)
323     (set regexp-sym
324          (concat "^" (apply (function regexp-or) fields) ":"))
325     (set sym fields)
326     ))
327
328
329 ;;; @ end
330 ;;;
331
332 (provide 'tm-def)
333
334 ;;; tm-def.el ends here