2 ;;; tm-def.el --- definition module for tm
4 ;;; Copyright (C) 1995 Free Software Foundation, Inc.
5 ;;; Copyright (C) 1995,1996 MORIOKA Tomohiko
7 ;;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
9 ;;; $Id: tm-def.el,v 7.46 1996/06/26 15:32:13 morioka Exp $
10 ;;; Keywords: mail, news, MIME, multimedia, definition
12 ;;; This file is part of tm (Tools for MIME).
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.
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.
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.
37 (defvar mime/tmp-dir (or (getenv "TM_TMP_DIR") "/tmp/"))
39 (defvar mime/use-multi-frame
40 (and (>= emacs-major-version 19) window-system))
42 (defvar mime/find-file-function
43 (if mime/use-multi-frame
44 (function find-file-other-frame)
52 (defconst mime/output-buffer-name "*MIME-out*")
53 (defconst mime/temp-buffer-name " *MIME-temp*")
56 ;;; @ leading-character and charset
59 (defvar mime/lc-charset-alist
63 (cons (cons (foldr (function
66 (cons (symbol-value sym) a)
76 '(((charset-ascii) . "US-ASCII")
78 charset-latin-1) . "ISO-8859-1")
80 charset-latin-2) . "ISO-8859-2")
82 charset-latin-3) . "ISO-8859-3")
84 charset-latin-4) . "ISO-8859-4")
86 ;;; charset-cyrillic) . "ISO-8859-5")
88 charset-cyrillic) . "KOI8-R")
90 charset-arabic) . "ISO-8859-6")
92 charset-greek) . "ISO-8859-7")
94 charset-hebrew) . "ISO-8859-8")
96 charset-latin-5) . "ISO-8859-9")
98 charset-jisx0208) . "ISO-2022-JP")
100 charset-ksc5601) . "EUC-KR")
103 charset-big5-2) . "BIG5")
110 charset-greek) . "ISO-2022-JP-2")
119 charset-greek) . "ISO-2022-INT-1")
122 (defvar mime/unknown-charset "ISO-2022-INT-1")
125 ;;; @ charset and encoding
128 (defun mime/find-charset (lcl)
130 (or (cdr (some-element
133 (subsetp lcl (car elt))
135 mime/lc-charset-alist)
137 mime/unknown-charset)
140 (defun mime/find-charset-region (beg end)
141 (mime/find-charset (cons charset-ascii (find-charset-region beg end)))
144 (defvar mime/charset-type-list
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")
159 ("ISO-2022-JP-2" 7 "base64")
160 ("ISO-2022-INT-1" 7 "base64")
163 (defun mime/encoding-name (transfer-level &optional not-omit)
164 (cond ((> transfer-level 8) "binary")
165 ((= transfer-level 8) "8bit")
169 (defun mime/make-charset-default-encoding-alist (transfer-level)
171 (lambda (charset-type)
172 (let ((charset (car charset-type))
173 (type (nth 1 charset-type))
174 (encoding (nth 2 charset-type))
176 (if (<= type transfer-level)
177 (cons charset (mime/encoding-name type))
178 (cons charset encoding)
180 mime/charset-type-list))
186 (defvar mime/charset-coding-system-alist
188 (f (if (and running-xemacs-20 (featurep 'mule))
190 (setq csl (coding-system-list))
193 (if (memq (cdr cell) csl)
198 (let ((sym (symbol-concat "*" (cdr cell) "*")))
200 (cons (cons (car cell) (symbol-value sym)) a)
204 '(("ISO-2022-JP" . junet)
205 ("ISO-2022-KR" . iso-2022-kr)
207 ("GB2312" . euc-china)
208 ("ISO-8859-1" . ctext)
209 ("ISO-8859-2" . iso-8859-2)
210 ("ISO-8859-3" . iso-8859-3)
211 ("ISO-8859-4" . iso-8859-4)
212 ("ISO-8859-5" . iso-8859-5)
214 ("ISO-8859-7" . iso-8859-7)
215 ("ISO-8859-8" . iso-8859-8)
216 ("ISO-8859-9" . iso-8859-9)
217 ("ISO-2022-JP-2" . iso-2022-ss2-7)
218 ("X-ISO-2022-JP-2" . iso-2022-ss2-7)
219 ("ISO-2022-INT-1" . iso-2022-int-1)
221 ("X-SHIFTJIS" . sjis)
225 (defvar mime/default-coding-system *ctext*)
227 (defun mime-charset-encode-string (str charset)
228 (let ((cs (assoc charset mime/charset-coding-system-alist)))
230 (character-encode-string str (cdr cs))
233 (defun mime-charset-decode-string (str charset)
234 (let ((cs (assoc charset mime/charset-coding-system-alist)))
236 (character-decode-string str (cdr cs))
239 (defun mime-charset-encode-region (beg end charset &optional encoding)
241 (if (stringp charset)
242 (cdr (assoc (upcase charset) mime/charset-coding-system-alist))
243 mime/default-coding-system)))
245 (character-encode-region beg end ct)
248 (defun mime-charset-decode-region (beg end charset &optional encoding)
250 (if (stringp charset)
251 (cdr (assoc (upcase charset) mime/charset-coding-system-alist))
252 mime/default-coding-system)))
254 (character-decode-region beg end ct)
261 (defun tm:set-face-region (b e face)
262 (let ((overlay (tl:make-overlay b e)))
263 (tl:overlay-put overlay 'face face)
266 (setq tm:button-face 'bold)
267 (setq tm:mouse-face 'highlight)
269 (defun tm:add-button (from to func &optional data)
270 "Create a button between FROM and TO with callback FUNC and data DATA."
272 (tl:overlay-put (tl:make-overlay from to) 'face tm:button-face))
273 (tl:add-text-properties from to
274 (append (and tm:mouse-face
275 (list 'mouse-face tm:mouse-face))
276 (list 'tm-callback func)
277 (and data (list 'tm-data data))
281 (defvar tm:mother-button-dispatcher nil)
283 (defun tm:button-dispatcher (event)
284 "Select the button under point."
286 (mouse-set-point event)
287 (let ((func (get-text-property (point) 'tm-callback))
288 (data (get-text-property (point) 'tm-data))
292 (if (fboundp tm:mother-button-dispatcher)
293 (funcall tm:mother-button-dispatcher event)
301 (defvar tm:URL-regexp
302 "\\(http\\|ftp\\|file\\|gopher\\|news\\|telnet\\|wais\\|mailto\\):\\(//[-a-zA-Z0-9_.]+:[0-9]*\\)?[-a-zA-Z0-9_=?#$@~`%&*+|\\/.,]*[-a-zA-Z0-9_=#$@~`%&*+|\\/]")
304 (defvar browse-url-browser-function nil)
306 (defun tm:browse-url (&optional url)
307 (if (fboundp browse-url-browser-function)
309 (funcall browse-url-browser-function url)
310 (call-interactively browse-url-browser-function))
311 (if (fboundp tm:mother-button-dispatcher)
312 (call-interactively tm:mother-button-dispatcher)
317 ;;; @ definitions about MIME
320 (defconst mime/tspecials "][\000-\040()<>@,\;:\\\"/?.=")
321 (defconst mime/token-regexp (concat "[^" mime/tspecials "]+"))
322 (defconst mime/charset-regexp mime/token-regexp)
324 (defconst mime/content-type-subtype-regexp
325 (concat mime/token-regexp "/" mime/token-regexp))
326 (defconst mime/content-parameter-value-regexp
328 rfc822/quoted-string-regexp
331 (defconst mime/disposition-type-regexp mime/token-regexp)
337 (defconst base64-token-regexp "[A-Za-z0-9+/=]")
339 (defconst mime/B-encoded-text-regexp
346 (defconst mime/B-encoding-and-encoded-text-regexp
347 (concat "\\(B\\)\\?" mime/B-encoded-text-regexp))
350 ;;; @@ Quoted-Printable
353 (defconst quoted-printable-hex-chars "0123456789ABCDEF")
354 (defconst quoted-printable-octet-regexp
355 (concat "=[" quoted-printable-hex-chars
356 "][" quoted-printable-hex-chars "]"))
358 (defconst mime/Q-encoded-text-regexp
359 (concat "\\([^=?]\\|" quoted-printable-octet-regexp "\\)+"))
360 (defconst mime/Q-encoding-and-encoded-text-regexp
361 (concat "\\(Q\\)\\?" mime/Q-encoded-text-regexp))
366 ;; caesar-region written by phr@prep.ai.mit.edu Nov 86
367 ;; modified by tower@prep Nov 86
368 ;; gnus-caesar-region
369 ;; Modified by umerin@flab.flab.Fujitsu.JUNET for ROT47.
370 (defun tm:caesar-region (&optional n)
371 "Caesar rotation of region by N, default 13, for decrypting netnews.
372 ROT47 will be performed for Japanese text in any case."
373 (interactive (if current-prefix-arg ; Was there a prefix arg?
374 (list (prefix-numeric-value current-prefix-arg))
376 (cond ((not (numberp n)) (setq n 13))
377 (t (setq n (mod n 26)))) ;canonicalize N
378 (if (not (zerop n)) ; no action needed for a rot of 0
380 (if (or (not (boundp 'caesar-translate-table))
381 (/= (aref caesar-translate-table ?a) (+ ?a n)))
382 (let ((i 0) (lower "abcdefghijklmnopqrstuvwxyz") upper)
383 (message "Building caesar-translate-table...")
384 (setq caesar-translate-table (make-vector 256 0))
386 (aset caesar-translate-table i i)
388 (setq lower (concat lower lower) upper (upcase lower) i 0)
390 (aset caesar-translate-table (+ ?a i) (aref lower (+ i n)))
391 (aset caesar-translate-table (+ ?A i) (aref upper (+ i n)))
393 ;; ROT47 for Japanese text.
394 ;; Thanks to ichikawa@flab.fujitsu.junet.
396 (let ((t1 (logior ?O 128))
398 (t3 (logior ?~ 128)))
400 (aset caesar-translate-table i
401 (let ((v (aref caesar-translate-table i)))
402 (if (<= v t1) (if (< v t2) v (+ v 47))
403 (if (<= v t3) (- v 47) v))))
405 (message "Building caesar-translate-table...done")))
406 (let ((from (region-beginning))
409 (setq str (buffer-substring from to))
410 (setq len (length str))
412 (aset str i (aref caesar-translate-table (aref str i)))
415 (delete-region from to)
422 (defun tm:set-fields (sym field-list &optional regexp-sym)
425 (let ((name (symbol-name sym)))
427 (concat (if (string-match "\\(.*\\)-list" name)
428 (substring name 0 (match-end 1))
435 (concat "^" (apply (function regexp-or) field-list) ":"))
438 (defun tm:add-fields (sym field-list &optional regexp-sym)
441 (let ((name (symbol-name sym)))
443 (concat (if (string-match "\\(.*\\)-list" name)
444 (substring name 0 (match-end 1))
449 (let ((fields (eval sym)))
452 (or (member field fields)
453 (setq fields (cons field fields))
459 (concat "^" (apply (function regexp-or) fields) ":"))
463 (defun tm:delete-fields (sym field-list &optional regexp-sym)
466 (let ((name (symbol-name sym)))
468 (concat (if (string-match "\\(.*\\)-list" name)
469 (substring name 0 (match-end 1))
474 (let ((fields (eval sym)))
477 (setq fields (delete field fields))
481 (concat "^" (apply (function regexp-or) fields) ":"))
491 ;;; tm-def.el ends here