tm 7.59.
[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.42 1996/05/09 18:24:39 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   (let* (csl
168          (f (if (and running-xemacs-20 (featurep 'mule))
169                 (progn
170                   (setq csl (coding-system-list))
171                   (function
172                    (lambda (a cell)
173                      (if (memq (cdr cell) csl)
174                          (cons cell a)
175                        a))))
176               (function
177                (lambda (a cell)
178                  (let ((sym (symbol-concat "*" (cdr cell) "*")))
179                    (if (boundp sym)
180                        (cons (cons (car cell) (symbol-value sym)) a)
181                      a))))
182               )))
183     (foldr f nil
184            '(("ISO-2022-JP"     . junet)
185              ("ISO-2022-KR"     . iso-2022-kr)
186              ("EUC-KR"          . euc-kr)
187              ("ISO-8859-1"      . ctext)
188              ("ISO-8859-2"      . iso-8859-2)
189              ("ISO-8859-3"      . iso-8859-3)
190              ("ISO-8859-4"      . iso-8859-4)
191              ("ISO-8859-5"      . iso-8859-5)
192              ("KOI8-R"          . koi8)
193              ("ISO-8859-7"      . iso-8859-7)
194              ("ISO-8859-8"      . iso-8859-8)
195              ("ISO-8859-9"      . iso-8859-9)
196              ("ISO-2022-JP-2"   . iso-2022-ss2-7)
197              ("X-ISO-2022-JP-2" . iso-2022-ss2-7)
198              ("ISO-2022-INT-1"  . iso-2022-int-1)
199              ("SHIFT_JIS"       . sjis)
200              ("X-SHIFTJIS"      . sjis)
201              ("BIG5"            . big5)
202              ))))
203
204 (defvar mime/default-coding-system *ctext*)
205
206 (defun mime-charset-encode-string (str charset)
207   (let ((cs (assoc charset mime/charset-coding-system-alist)))
208     (if cs
209         (character-encode-string str (cdr cs))
210       )))
211
212 (defun mime-charset-decode-string (str charset)
213   (let ((cs (assoc charset mime/charset-coding-system-alist)))
214     (if cs
215         (character-decode-string str (cdr cs))
216       )))
217
218 (defun mime-charset-encode-region (beg end charset &optional encoding)
219   (let ((ct
220          (if (stringp charset)
221              (cdr (assoc (upcase charset) mime/charset-coding-system-alist))
222            mime/default-coding-system)))
223     (if ct
224         (character-encode-region beg end ct)
225       )))
226
227 (defun mime-charset-decode-region (beg end charset &optional encoding)
228   (let ((ct
229          (if (stringp charset)
230              (cdr (assoc (upcase charset) mime/charset-coding-system-alist))
231            mime/default-coding-system)))
232     (if ct
233         (character-decode-region beg end ct)
234       )))
235
236
237 ;;; @ button
238 ;;;
239
240 (defun tm:set-face-region (b e face)
241   (let ((overlay (tl:make-overlay b e)))
242     (tl:overlay-put overlay 'face face)
243     ))
244
245 (setq tm:button-face 'bold)
246 (setq tm:mouse-face 'highlight)
247
248 (defun tm:add-button (from to func &optional data)
249   "Create a button between FROM and TO with callback FUNC and data DATA."
250   (and tm:button-face
251        (tl:overlay-put (tl:make-overlay from to) 'face tm:button-face))
252   (tl:add-text-properties from to
253                           (append (and tm:mouse-face
254                                        (list 'mouse-face tm:mouse-face))
255                                   (list 'tm-callback func)
256                                   (and data (list 'tm-data data))
257                                   ))
258   )
259
260 (defvar tm:mother-button-dispatcher nil)
261
262 (defun tm:button-dispatcher (event)
263   "Select the button under point."
264   (interactive "e")
265   (mouse-set-point event)
266   (let ((func (get-text-property (point) 'tm-callback))
267         (data (get-text-property (point) 'tm-data))
268         )
269     (if func
270         (apply func data)
271       (if (fboundp tm:mother-button-dispatcher)
272           (funcall tm:mother-button-dispatcher event)
273         )
274       )))
275
276
277 ;;; @ for URL
278 ;;;
279
280 (defvar tm:URL-regexp
281   "\\(http\\|ftp\\|file\\|gopher\\|news\\|telnet\\|wais\\|mailto\\):\\(//[-a-zA-Z0-9_.]+:[0-9]*\\)?[-a-zA-Z0-9_=?#$@~`%&*+|\\/.,]*[-a-zA-Z0-9_=#$@~`%&*+|\\/]")
282
283 (defvar browse-url-browser-function nil)
284
285 (defun tm:browse-url (&optional url)
286   (if (fboundp browse-url-browser-function)
287       (call-interactively browse-url-browser-function)
288     (if (fboundp tm:mother-button-dispatcher)
289         (call-interactively tm:mother-button-dispatcher)
290       )
291     ))
292
293
294 ;;; @ definitions about MIME
295 ;;;
296
297 (defconst mime/tspecials "][\000-\040()<>@,\;:\\\"/?.=")
298 (defconst mime/token-regexp (concat "[^" mime/tspecials "]+"))
299 (defconst mime/charset-regexp mime/token-regexp)
300
301 (defconst mime/content-type-subtype-regexp
302   (concat mime/token-regexp "/" mime/token-regexp))
303 (defconst mime/content-parameter-value-regexp
304   (concat "\\("
305           rfc822/quoted-string-regexp
306           "\\|[^; \t\n]*\\)"))
307
308 (defconst mime/disposition-type-regexp mime/token-regexp)
309
310
311 ;;; @@ Base64
312 ;;;
313
314 (defconst base64-token-regexp "[A-Za-z0-9+/=]")
315
316 (defconst mime/B-encoded-text-regexp
317   (concat "\\("
318           base64-token-regexp
319           base64-token-regexp
320           base64-token-regexp
321           base64-token-regexp
322           "\\)+"))
323 (defconst mime/B-encoding-and-encoded-text-regexp
324   (concat "\\(B\\)\\?" mime/B-encoded-text-regexp))
325
326
327 ;;; @@ Quoted-Printable
328 ;;;
329
330 (defconst quoted-printable-hex-chars "0123456789ABCDEF")
331 (defconst quoted-printable-octet-regexp
332   (concat "=[" quoted-printable-hex-chars
333           "][" quoted-printable-hex-chars "]"))
334
335 (defconst mime/Q-encoded-text-regexp
336   (concat "\\([^=?]\\|" quoted-printable-octet-regexp "\\)+"))
337 (defconst mime/Q-encoding-and-encoded-text-regexp
338   (concat "\\(Q\\)\\?" mime/Q-encoded-text-regexp))
339
340
341 ;;; @ rot13-47
342 ;;;
343 ;; caesar-region written by phr@prep.ai.mit.edu  Nov 86
344 ;; modified by tower@prep Nov 86
345 ;; gnus-caesar-region
346 ;; Modified by umerin@flab.flab.Fujitsu.JUNET for ROT47.
347 (defun tm:caesar-region (&optional n)
348   "Caesar rotation of region by N, default 13, for decrypting netnews.
349 ROT47 will be performed for Japanese text in any case."
350   (interactive (if current-prefix-arg   ; Was there a prefix arg?
351                    (list (prefix-numeric-value current-prefix-arg))
352                  (list nil)))
353   (cond ((not (numberp n)) (setq n 13))
354         (t (setq n (mod n 26))))        ;canonicalize N
355   (if (not (zerop n))           ; no action needed for a rot of 0
356       (progn
357         (if (or (not (boundp 'caesar-translate-table))
358                 (/= (aref caesar-translate-table ?a) (+ ?a n)))
359             (let ((i 0) (lower "abcdefghijklmnopqrstuvwxyz") upper)
360               (message "Building caesar-translate-table...")
361               (setq caesar-translate-table (make-vector 256 0))
362               (while (< i 256)
363                 (aset caesar-translate-table i i)
364                 (setq i (1+ i)))
365               (setq lower (concat lower lower) upper (upcase lower) i 0)
366               (while (< i 26)
367                 (aset caesar-translate-table (+ ?a i) (aref lower (+ i n)))
368                 (aset caesar-translate-table (+ ?A i) (aref upper (+ i n)))
369                 (setq i (1+ i)))
370               ;; ROT47 for Japanese text.
371               ;; Thanks to ichikawa@flab.fujitsu.junet.
372               (setq i 161)
373               (let ((t1 (logior ?O 128))
374                     (t2 (logior ?! 128))
375                     (t3 (logior ?~ 128)))
376                 (while (< i 256)
377                   (aset caesar-translate-table i
378                         (let ((v (aref caesar-translate-table i)))
379                           (if (<= v t1) (if (< v t2) v (+ v 47))
380                             (if (<= v t3) (- v 47) v))))
381                   (setq i (1+ i))))
382               (message "Building caesar-translate-table...done")))
383         (let ((from (region-beginning))
384               (to (region-end))
385               (i 0) str len)
386           (setq str (buffer-substring from to))
387           (setq len (length str))
388           (while (< i len)
389             (aset str i (aref caesar-translate-table (aref str i)))
390             (setq i (1+ i)))
391           (goto-char from)
392           (delete-region from to)
393           (insert str)))))
394
395
396 ;;; @ field
397 ;;;
398
399 (defun tm:set-fields (sym field-list &optional regexp-sym)
400   (or regexp-sym
401       (setq regexp-sym
402             (let ((name (symbol-name sym)))
403               (intern
404                (concat (if (string-match "\\(.*\\)-list" name)
405                            (substring name 0 (match-end 1))
406                          name)
407                        "-regexp")
408                )))
409       )
410   (set sym field-list)
411   (set regexp-sym
412        (concat "^" (apply (function regexp-or) field-list) ":"))
413   )
414
415 (defun tm:add-fields (sym field-list &optional regexp-sym)
416   (or regexp-sym
417       (setq regexp-sym
418             (let ((name (symbol-name sym)))
419               (intern
420                (concat (if (string-match "\\(.*\\)-list" name)
421                            (substring name 0 (match-end 1))
422                          name)
423                        "-regexp")
424                )))
425       )
426   (let ((fields (eval sym)))
427     (mapcar (function
428              (lambda (field)
429                (or (member field fields)
430                    (setq fields (cons field fields))
431                    )
432                ))
433             (reverse field-list)
434             )
435     (set regexp-sym
436          (concat "^" (apply (function regexp-or) fields) ":"))
437     (set sym fields)
438     ))
439
440 (defun tm:delete-fields (sym field-list &optional regexp-sym)
441   (or regexp-sym
442       (setq regexp-sym
443             (let ((name (symbol-name sym)))
444               (intern
445                (concat (if (string-match "\\(.*\\)-list" name)
446                            (substring name 0 (match-end 1))
447                          name)
448                        "-regexp")
449                )))
450       )
451   (let ((fields (eval sym)))
452     (mapcar (function
453              (lambda (field)
454                (setq fields (delete field fields))
455                ))
456             field-list)
457     (set regexp-sym
458          (concat "^" (apply (function regexp-or) fields) ":"))
459     (set sym fields)
460     ))
461
462
463 ;;; @ end
464 ;;;
465
466 (provide 'tm-def)
467
468 ;;; tm-def.el ends here