tm 7.52.2.
[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.34 1996/04/23 00:15:47 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 ;;; @ leading-character and charset
57 ;;;
58
59 (defvar mime/lc-charset-alist
60   (foldr (function
61           (lambda (a cell)
62             (or (catch 'tag
63                   (cons (cons (foldr (function
64                                       (lambda (a sym)
65                                         (if (boundp sym)
66                                             (cons (symbol-value sym) a)
67                                           (throw 'tag nil)
68                                           )))
69                                      nil
70                                      (car cell))
71                               (cdr cell))
72                         a))
73                 a)
74             ))
75          nil
76          '(((lc-ascii)         . "US-ASCII")
77            ((lc-ascii lc-ltn1) . "ISO-8859-1")
78            ((lc-ascii lc-ltn2) . "ISO-8859-2")
79            ((lc-ascii lc-ltn3) . "ISO-8859-3")
80            ((lc-ascii lc-ltn4) . "ISO-8859-4")
81 ;;;        ((lc-ascii lc-crl)  . "ISO-8859-5")
82            ((lc-ascii lc-crl)  . "KOI8-R")
83            ((lc-ascii lc-grk)  . "ISO-8859-7")
84            ((lc-ascii lc-hbw)  . "ISO-8859-8")
85            ((lc-ascii lc-ltn5) . "ISO-8859-9")
86            ((lc-ascii lc-jp)   . "ISO-2022-JP")
87            ((lc-ascii lc-kr)   . "EUC-KR")
88            ((lc-ascii
89              lc-big5-1
90              lc-big5-2)        . "BIG5")
91            ((lc-ascii
92              lc-jp lc-cn
93              lc-kr lc-jp2
94              lc-ltn1 lc-grk)   . "ISO-2022-JP-2")
95            ((lc-ascii
96              lc-jp lc-cn
97              lc-kr lc-jp2
98              lc-cns1 lc-cns2
99              lc-ltn1 lc-grk)   . "ISO-2022-INT-1")
100            )))
101
102 (defvar mime/unknown-charset "ISO-2022-INT-1")
103
104
105 ;;; @ charset and encoding
106 ;;;
107
108 (defun mime/find-charset (lcl)
109   (if lcl
110       (or (cdr (some-element
111                 (function
112                  (lambda (elt)
113                    (subsetp lcl (car elt))
114                    ))
115                 mime/lc-charset-alist)
116                )
117           mime/unknown-charset)
118     ))
119
120 (defun mime/find-charset-region (beg end)
121   (mime/find-charset (cons lc-ascii (find-charset-region beg end)))
122   )
123
124 (defvar mime/charset-type-list
125   '(("US-ASCII"       7 nil)
126     ("ISO-8859-1"     8 "quoted-printable")
127     ("ISO-8859-2"     8 "quoted-printable")
128     ("ISO-8859-3"     8 "quoted-printable")
129     ("ISO-8859-4"     8 "quoted-printable")
130     ("ISO-8859-5"     8 "quoted-printable")
131     ("KOI8-R"         8 "quoted-printable")
132     ("ISO-8859-7"     8 "quoted-printable")
133     ("ISO-8859-8"     8 "quoted-printable")
134     ("ISO-8859-9"     8 "quoted-printable")
135     ("ISO-2022-JP"    7 "base64")
136     ("ISO-2022-KR"    7 "base64")
137     ("EUC-KR"         8 "base64")
138     ("BIG5"           8 "base64")
139     ("ISO-2022-JP-2"  7 "base64")
140     ("ISO-2022-INT-1" 7 "base64")
141     ))
142
143 (defun mime/encoding-name (transfer-level &optional not-omit)
144   (cond ((> transfer-level 8) "binary")
145         ((= transfer-level 8) "8bit")
146         (not-omit "7bit")
147         ))
148
149 (defun mime/make-charset-default-encoding-alist (transfer-level)
150   (mapcar (function
151            (lambda (charset-type)
152              (let ((charset  (car charset-type))
153                    (type     (nth 1 charset-type))
154                    (encoding (nth 2 charset-type))
155                    )
156                (if (<= type transfer-level)
157                    (cons charset (mime/encoding-name type))
158                  (cons charset encoding)
159                  ))))
160           mime/charset-type-list))
161
162
163 ;;; @ coding-system
164 ;;;
165
166 (defvar mime/charset-coding-system-alist
167   (foldr (function
168           (lambda (a cell)
169             (if (boundp (cdr cell))
170                 (cons (cons (car cell) (symbol-value (cdr cell))) a)
171               a)))
172          nil
173          '(("ISO-2022-JP"     . *junet*)
174            ("ISO-2022-KR"     . *iso-2022-kr*)
175            ("EUC-KR"          . *euc-kr*)
176            ("ISO-8859-1"      . *ctext*)
177            ("ISO-8859-2"      . *iso-8859-2*)
178            ("ISO-8859-3"      . *iso-8859-3*)
179            ("ISO-8859-4"      . *iso-8859-4*)
180            ("ISO-8859-5"      . *iso-8859-5*)
181            ("KOI8-R"          . *koi8*)
182            ("ISO-8859-7"      . *iso-8859-7*)
183            ("ISO-8859-8"      . *iso-8859-8*)
184            ("ISO-8859-9"      . *iso-8859-9*)
185            ("ISO-2022-JP-2"   . *iso-2022-ss2-7*)
186            ("X-ISO-2022-JP-2" . *iso-2022-ss2-7*)
187            ("ISO-2022-INT-1"  . *iso-2022-int-1*)
188            ("SHIFT_JIS"       . *sjis*)
189            ("X-SHIFTJIS"      . *sjis*)
190            ("BIG5"            . *big5*)
191            )))
192
193 (defvar mime/default-coding-system *ctext*)
194
195 (defun mime/convert-string-to-emacs (str charset)
196   (let ((cs (cdr (assoc charset mime/charset-coding-system-alist))))
197     (if cs
198         (code-convert-string str cs *internal*)
199       )))
200
201 (defun mime/convert-string-from-emacs (str charset)
202   (let ((cs (cdr (assoc charset mime/charset-coding-system-alist))))
203     (if cs
204         (code-convert-string str *internal* cs)
205       )))
206
207 (defun mime/code-convert-region-to-emacs (beg end charset &optional encoding)
208   (let ((ct
209          (if (stringp charset)
210              (cdr (assoc (upcase charset) mime/charset-coding-system-alist))
211            mime/default-coding-system)))
212     (if ct
213         (code-convert-region beg end ct *internal*)
214       )))
215
216 (defun mime/code-convert-region-from-emacs (beg end charset &optional encoding)
217   (let ((ct
218          (if (stringp charset)
219              (cdr (assoc (upcase charset) mime/charset-coding-system-alist))
220            mime/default-coding-system)))
221     (if ct
222         (code-convert-region beg end *internal* ct)
223       )))
224
225
226 ;;; @ button
227 ;;;
228
229 (defun tm:set-face-region (b e face)
230   (let ((overlay (tl:make-overlay b e)))
231     (tl:overlay-put overlay 'face face)
232     ))
233
234 (setq tm:button-face 'bold)
235 (setq tm:mouse-face 'highlight)
236
237 (defun tm:add-button (from to func &optional data)
238   "Create a button between FROM and TO with callback FUNC and data DATA."
239   (and tm:button-face
240        (tl:overlay-put (tl:make-overlay from to) 'face tm:button-face))
241   (tl:add-text-properties from to
242                           (append (and tm:mouse-face
243                                        (list 'mouse-face tm:mouse-face))
244                                   (list 'tm-callback func)
245                                   (and data (list 'tm-data data))
246                                   ))
247   )
248
249 (defvar tm:mother-button-dispatcher nil)
250
251 (defun tm:button-dispatcher (event)
252   "Select the button under point."
253   (interactive "e")
254   (mouse-set-point event)
255   (let ((func (get-text-property (point) 'tm-callback))
256         (data (get-text-property (point) 'tm-data))
257         )
258     (if func
259         (apply func data)
260       (if (fboundp tm:mother-button-dispatcher)
261           (funcall tm:mother-button-dispatcher event)
262         )
263       )))
264
265
266 ;;; @ for URL
267 ;;;
268
269 (defvar tm:URL-regexp
270   "\\(http\\|ftp\\|file\\|gopher\\|news\\|telnet\\|wais\\|mailto\\):\\(//[-a-zA-Z0-9_.]+:[0-9]*\\)?[-a-zA-Z0-9_=?#$@~`%&*+|\\/.,]*[-a-zA-Z0-9_=#$@~`%&*+|\\/]")
271
272 (defvar browse-url-browser-function nil)
273
274 (defun tm:browse-url (&optional url)
275   (if (fboundp browse-url-browser-function)
276       (call-interactively browse-url-browser-function)
277     (if (fboundp tm:mother-button-dispatcher)
278         (call-interactively tm:mother-button-dispatcher)
279       )
280     ))
281
282
283 ;;; @ definitions about MIME
284 ;;;
285
286 (defconst mime/tspecials "][\000-\040()<>@,\;:\\\"/?.=")
287 (defconst mime/token-regexp (concat "[^" mime/tspecials "]+"))
288 (defconst mime/charset-regexp mime/token-regexp)
289
290 (defconst mime/content-type-subtype-regexp
291   (concat mime/token-regexp "/" mime/token-regexp))
292 (defconst mime/content-parameter-value-regexp
293   (concat "\\("
294           rfc822/quoted-string-regexp
295           "\\|[^; \t\n]*\\)"))
296
297 (defconst mime/disposition-type-regexp mime/token-regexp)
298
299
300 ;;; @@ Base64
301 ;;;
302
303 (defconst base64-token-regexp "[A-Za-z0-9+/=]")
304
305 (defconst mime/B-encoded-text-regexp
306   (concat "\\("
307           base64-token-regexp
308           base64-token-regexp
309           base64-token-regexp
310           base64-token-regexp
311           "\\)+"))
312 (defconst mime/B-encoding-and-encoded-text-regexp
313   (concat "\\(B\\)\\?" mime/B-encoded-text-regexp))
314
315
316 ;;; @@ Quoted-Printable
317 ;;;
318
319 (defconst quoted-printable-hex-chars "0123456789ABCDEF")
320 (defconst quoted-printable-octet-regexp
321   (concat "=[" quoted-printable-hex-chars
322           "][" quoted-printable-hex-chars "]"))
323
324 (defconst mime/Q-encoded-text-regexp
325   (concat "\\([^=?]\\|" quoted-printable-octet-regexp "\\)+"))
326 (defconst mime/Q-encoding-and-encoded-text-regexp
327   (concat "\\(Q\\)\\?" mime/Q-encoded-text-regexp))
328
329
330 ;;; @ rot13-47
331 ;;;
332 ;; caesar-region written by phr@prep.ai.mit.edu  Nov 86
333 ;; modified by tower@prep Nov 86
334 ;; gnus-caesar-region
335 ;; Modified by umerin@flab.flab.Fujitsu.JUNET for ROT47.
336 (defun tm:caesar-region (&optional n)
337   "Caesar rotation of region by N, default 13, for decrypting netnews.
338 ROT47 will be performed for Japanese text in any case."
339   (interactive (if current-prefix-arg   ; Was there a prefix arg?
340                    (list (prefix-numeric-value current-prefix-arg))
341                  (list nil)))
342   (cond ((not (numberp n)) (setq n 13))
343         (t (setq n (mod n 26))))        ;canonicalize N
344   (if (not (zerop n))           ; no action needed for a rot of 0
345       (progn
346         (if (or (not (boundp 'caesar-translate-table))
347                 (/= (aref caesar-translate-table ?a) (+ ?a n)))
348             (let ((i 0) (lower "abcdefghijklmnopqrstuvwxyz") upper)
349               (message "Building caesar-translate-table...")
350               (setq caesar-translate-table (make-vector 256 0))
351               (while (< i 256)
352                 (aset caesar-translate-table i i)
353                 (setq i (1+ i)))
354               (setq lower (concat lower lower) upper (upcase lower) i 0)
355               (while (< i 26)
356                 (aset caesar-translate-table (+ ?a i) (aref lower (+ i n)))
357                 (aset caesar-translate-table (+ ?A i) (aref upper (+ i n)))
358                 (setq i (1+ i)))
359               ;; ROT47 for Japanese text.
360               ;; Thanks to ichikawa@flab.fujitsu.junet.
361               (setq i 161)
362               (let ((t1 (logior ?O 128))
363                     (t2 (logior ?! 128))
364                     (t3 (logior ?~ 128)))
365                 (while (< i 256)
366                   (aset caesar-translate-table i
367                         (let ((v (aref caesar-translate-table i)))
368                           (if (<= v t1) (if (< v t2) v (+ v 47))
369                             (if (<= v t3) (- v 47) v))))
370                   (setq i (1+ i))))
371               (message "Building caesar-translate-table...done")))
372         (let ((from (region-beginning))
373               (to (region-end))
374               (i 0) str len)
375           (setq str (buffer-substring from to))
376           (setq len (length str))
377           (while (< i len)
378             (aset str i (aref caesar-translate-table (aref str i)))
379             (setq i (1+ i)))
380           (goto-char from)
381           (delete-region from to)
382           (insert str)))))
383
384
385 ;;; @ field
386 ;;;
387
388 (defun tm:set-fields (sym field-list &optional regexp-sym)
389   (or regexp-sym
390       (setq regexp-sym
391             (let ((name (symbol-name sym)))
392               (intern
393                (concat (if (string-match "\\(.*\\)-list" name)
394                            (substring name 0 (match-end 1))
395                          name)
396                        "-regexp")
397                )))
398       )
399   (set sym field-list)
400   (set regexp-sym
401        (concat "^" (apply (function regexp-or) field-list) ":"))
402   )
403
404 (defun tm:add-fields (sym field-list &optional regexp-sym)
405   (or regexp-sym
406       (setq regexp-sym
407             (let ((name (symbol-name sym)))
408               (intern
409                (concat (if (string-match "\\(.*\\)-list" name)
410                            (substring name 0 (match-end 1))
411                          name)
412                        "-regexp")
413                )))
414       )
415   (let ((fields (eval sym)))
416     (mapcar (function
417              (lambda (field)
418                (or (member field fields)
419                    (setq fields (cons field fields))
420                    )
421                ))
422             (reverse field-list)
423             )
424     (set regexp-sym
425          (concat "^" (apply (function regexp-or) fields) ":"))
426     (set sym fields)
427     ))
428
429 (defun tm:delete-fields (sym field-list &optional regexp-sym)
430   (or regexp-sym
431       (setq regexp-sym
432             (let ((name (symbol-name sym)))
433               (intern
434                (concat (if (string-match "\\(.*\\)-list" name)
435                            (substring name 0 (match-end 1))
436                          name)
437                        "-regexp")
438                )))
439       )
440   (let ((fields (eval sym)))
441     (mapcar (function
442              (lambda (field)
443                (setq fields (delete field fields))
444                ))
445             field-list)
446     (set regexp-sym
447          (concat "^" (apply (function regexp-or) fields) ":"))
448     (set sym fields)
449     ))
450
451
452 ;;; @ end
453 ;;;
454
455 (provide 'tm-def)
456
457 ;;; tm-def.el ends here