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