tm 7.42.1.
[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.15 1996/02/06 02:43:06 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-type-list
85   '(("US-ASCII"       7 nil)
86     ("ISO-8859-1"     8 "quoted-printable")
87     ("ISO-8859-2"     8 "quoted-printable")
88     ("ISO-8859-3"     8 "quoted-printable")
89     ("ISO-8859-4"     8 "quoted-printable")
90     ("ISO-8859-5"     8 "quoted-printable")
91     ("KOI8-R"         8 "quoted-printable")
92     ("ISO-8859-7"     8 "quoted-printable")
93     ("ISO-8859-8"     8 "quoted-printable")
94     ("ISO-8859-9"     8 "quoted-printable")
95     ("ISO-2022-JP"    7 "base64")
96     ("ISO-2022-KR"    7 "base64")
97     ("EUC-KR"         8 "base64")
98     ("ISO-2022-JP-2"  7 "base64")
99     ("ISO-2022-INT-1" 7 "base64")
100     ))
101
102 (defun mime/encoding-name (transfer-level)
103   (cond ((> transfer-level 8) "binary")
104         ((= transfer-level 8) "8bit")
105         ))
106
107 (defun mime/make-charset-default-encoding-alist (transfer-level)
108   (mapcar (function
109            (lambda (charset-type)
110              (let ((charset  (car charset-type))
111                    (type     (nth 1 charset-type))
112                    (encoding (nth 2 charset-type))
113                    )
114                (if (<= type transfer-level)
115                    (cons charset (mime/encoding-name type))
116                  (cons charset encoding)
117                  ))))
118           mime/charset-type-list))
119
120 (defvar mime/default-transfer-level 7
121   "*A number of network transfer level. It should be 7 or 8.")
122
123 (defvar mime/charset-default-encoding-alist
124   (mime/make-charset-default-encoding-alist mime/default-transfer-level))
125
126 (defun mime/text-toggle-transfer-level (&optional transfer-level)
127   "Toggle transfer-level is 7bit or 8bit through.
128
129 Optional TRANSFER-LEVEL is a number of transfer-level, 7 or 8."
130   (interactive)
131   (if (numberp transfer-level)
132       (setq mime/default-transfer-level transfer-level)
133     (if (< mime/default-transfer-level 8)
134         (setq mime/default-transfer-level 8)
135       (setq mime/default-transfer-level 7)
136       ))
137   (setq mime/charset-default-encoding-alist
138         (mime/make-charset-default-encoding-alist
139          mime/default-transfer-level))
140   (message (format "Current transfer-level is %d bit"
141                    mime/default-transfer-level))
142   )
143
144
145 ;;; @ coding-system
146 ;;;
147
148 (defvar mime/default-coding-system *ctext*)
149
150
151 ;;; @ button
152 ;;;
153
154 (defun tm:set-face-region (b e face)
155   (let ((overlay (tl:make-overlay b e)))
156     (tl:overlay-put overlay 'face face)
157     ))
158
159 (setq tm:button-face 'bold)
160 (setq tm:mouse-face 'highlight)
161
162 (defun tm:add-button (from to func &optional data)
163   "Create a button between FROM and TO with callback FUNC and data DATA."
164   (and tm:button-face
165        (tl:overlay-put (tl:make-overlay from to) 'face tm:button-face))
166   (tl:add-text-properties from to
167                           (append (and tm:mouse-face
168                                        (list 'mouse-face tm:mouse-face))
169                                   (list 'tm-callback func)
170                                   (and data (list 'tm-data data))
171                                   ))
172   )
173
174 (defvar tm:mother-button-dispatcher nil)
175
176 (defun tm:button-dispatcher (event)
177   "Select the button under point."
178   (interactive "e")
179   (mouse-set-point event)
180   (let ((func (get-text-property (point) 'tm-callback))
181         (data (get-text-property (point) 'tm-data))
182         )
183     (if func
184         (apply func data)
185       (if (fboundp tm:mother-button-dispatcher)
186           (funcall tm:mother-button-dispatcher event)
187         )
188       )))
189
190
191 ;;; @ for URL
192 ;;;
193
194 (defvar tm:URL-regexp
195   "\\(http\\|ftp\\|file\\|gopher\\|news\\|telnet\\|wais\\|mailto\\):\\(//[-a-zA-Z0-9_.]+:[0-9]*\\)?[-a-zA-Z0-9_=?#$@~`%&*+|\\/.,]*[-a-zA-Z0-9_=#$@~`%&*+|\\/]")
196
197 (defvar browse-url-browser-function nil)
198
199 (defun tm:browse-url (&optional url)
200   (if (fboundp browse-url-browser-function)
201       (call-interactively browse-url-browser-function)
202     (if (fboundp tm:mother-button-dispatcher)
203         (call-interactively tm:mother-button-dispatcher)
204       )
205     ))
206
207
208 ;;; @ definitions about MIME
209 ;;;
210
211 (defconst mime/tspecials "][\000-\040()<>@,\;:\\\"/?.=")
212 (defconst mime/token-regexp (concat "[^" mime/tspecials "]+"))
213 (defconst mime/charset-regexp mime/token-regexp)
214
215 (defconst mime/content-type-subtype-regexp
216   (concat mime/token-regexp "/" mime/token-regexp))
217 (defconst mime/content-parameter-value-regexp
218   (concat "\\("
219           rfc822/quoted-string-regexp
220           "\\|[^; \t\n]*\\)"))
221
222 (defconst mime/disposition-type-regexp mime/token-regexp)
223
224
225 ;;; @@ Base64
226 ;;;
227
228 (defconst base64-token-regexp "[A-Za-z0-9+/=]")
229
230 (defconst mime/B-encoded-text-regexp
231   (concat "\\("
232           base64-token-regexp
233           base64-token-regexp
234           base64-token-regexp
235           base64-token-regexp
236           "\\)+"))
237 (defconst mime/B-encoding-and-encoded-text-regexp
238   (concat "\\(B\\)\\?" mime/B-encoded-text-regexp))
239
240
241 ;;; @@ Quoted-Printable
242 ;;;
243
244 (defconst quoted-printable-hex-chars "0123456789ABCDEF")
245 (defconst quoted-printable-octet-regexp
246   (concat "=[" quoted-printable-hex-chars
247           "][" quoted-printable-hex-chars "]"))
248
249 (defconst mime/Q-encoded-text-regexp
250   (concat "\\([^=?]\\|" quoted-printable-octet-regexp "\\)+"))
251 (defconst mime/Q-encoding-and-encoded-text-regexp
252   (concat "\\(Q\\)\\?" mime/Q-encoded-text-regexp))
253
254
255 ;;; @ rot13-47
256 ;;;
257 ;; caesar-region written by phr@prep.ai.mit.edu  Nov 86
258 ;; modified by tower@prep Nov 86
259 ;; gnus-caesar-region
260 ;; Modified by umerin@flab.flab.Fujitsu.JUNET for ROT47.
261 (defun tm:caesar-region (&optional n)
262   "Caesar rotation of region by N, default 13, for decrypting netnews.
263 ROT47 will be performed for Japanese text in any case."
264   (interactive (if current-prefix-arg   ; Was there a prefix arg?
265                    (list (prefix-numeric-value current-prefix-arg))
266                  (list nil)))
267   (cond ((not (numberp n)) (setq n 13))
268         (t (setq n (mod n 26))))        ;canonicalize N
269   (if (not (zerop n))           ; no action needed for a rot of 0
270       (progn
271         (if (or (not (boundp 'caesar-translate-table))
272                 (/= (aref caesar-translate-table ?a) (+ ?a n)))
273             (let ((i 0) (lower "abcdefghijklmnopqrstuvwxyz") upper)
274               (message "Building caesar-translate-table...")
275               (setq caesar-translate-table (make-vector 256 0))
276               (while (< i 256)
277                 (aset caesar-translate-table i i)
278                 (setq i (1+ i)))
279               (setq lower (concat lower lower) upper (upcase lower) i 0)
280               (while (< i 26)
281                 (aset caesar-translate-table (+ ?a i) (aref lower (+ i n)))
282                 (aset caesar-translate-table (+ ?A i) (aref upper (+ i n)))
283                 (setq i (1+ i)))
284               ;; ROT47 for Japanese text.
285               ;; Thanks to ichikawa@flab.fujitsu.junet.
286               (setq i 161)
287               (let ((t1 (logior ?O 128))
288                     (t2 (logior ?! 128))
289                     (t3 (logior ?~ 128)))
290                 (while (< i 256)
291                   (aset caesar-translate-table i
292                         (let ((v (aref caesar-translate-table i)))
293                           (if (<= v t1) (if (< v t2) v (+ v 47))
294                             (if (<= v t3) (- v 47) v))))
295                   (setq i (1+ i))))
296               (message "Building caesar-translate-table...done")))
297         (let ((from (region-beginning))
298               (to (region-end))
299               (i 0) str len)
300           (setq str (buffer-substring from to))
301           (setq len (length str))
302           (while (< i len)
303             (aset str i (aref caesar-translate-table (aref str i)))
304             (setq i (1+ i)))
305           (goto-char from)
306           (delete-region from to)
307           (insert str)))))
308
309
310 ;;; @ field
311 ;;;
312
313 (defun tm:set-fields (sym field-list &optional regexp-sym)
314   (or regexp-sym
315       (setq regexp-sym
316             (let ((name (symbol-name sym)))
317               (intern
318                (concat (if (string-match "\\(.*\\)-list" name)
319                            (substring name 0 (match-end 1))
320                          name)
321                        "-regexp")
322                )))
323       )
324   (set sym field-list)
325   (set regexp-sym
326        (concat "^" (apply (function regexp-or) field-list) ":"))
327   )
328
329 (defun tm:add-fields (sym field-list &optional regexp-sym)
330   (or regexp-sym
331       (setq regexp-sym
332             (let ((name (symbol-name sym)))
333               (intern
334                (concat (if (string-match "\\(.*\\)-list" name)
335                            (substring name 0 (match-end 1))
336                          name)
337                        "-regexp")
338                )))
339       )
340   (let ((fields (eval sym)))
341     (mapcar (function
342              (lambda (field)
343                (or (member field fields)
344                    (setq fields (cons field fields))
345                    )
346                ))
347             (reverse field-list)
348             )
349     (set regexp-sym
350          (concat "^" (apply (function regexp-or) fields) ":"))
351     (set sym fields)
352     ))
353
354 (defun tm:delete-fields (sym field-list &optional regexp-sym)
355   (or regexp-sym
356       (setq regexp-sym
357             (let ((name (symbol-name sym)))
358               (intern
359                (concat (if (string-match "\\(.*\\)-list" name)
360                            (substring name 0 (match-end 1))
361                          name)
362                        "-regexp")
363                )))
364       )
365   (let ((fields (eval sym)))
366     (mapcar (function
367              (lambda (field)
368                (setq fields (delete field fields))
369                ))
370             field-list)
371     (set regexp-sym
372          (concat "^" (apply (function regexp-or) fields) ":"))
373     (set sym fields)
374     ))
375
376
377 ;;; @ end
378 ;;;
379
380 (provide 'tm-def)
381
382 ;;; tm-def.el ends here