1 ;;; tm-def.el --- definition module for tm
3 ;; Copyright (C) 1995,1996 Free Software Foundation, Inc.
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; Version: $Id: tm-def.el,v 7.69 1997/01/21 06:13:15 shuhei-k Exp $
7 ;; Keywords: mail, news, MIME, multimedia, definition
9 ;; This file is part of tm (Tools for MIME).
11 ;; This program is free software; you can redistribute it and/or
12 ;; modify it under the terms of the GNU General Public License as
13 ;; published by the Free Software Foundation; either version 2, or (at
14 ;; your option) any later version.
16 ;; This program is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ;; General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
34 (defvar mime/tmp-dir (or (getenv "TM_TMP_DIR") "/tmp/"))
36 (defvar mime/use-multi-frame
37 (and (>= emacs-major-version 19) window-system))
39 (defvar mime/find-file-function
40 (if mime/use-multi-frame
41 (function find-file-other-frame)
45 (defvar mime/output-buffer-window-is-shared-with-bbdb t
46 "*If t, mime/output-buffer window is shared with BBDB window.")
52 (defconst mime/output-buffer-name "*MIME-out*")
53 (defconst mime/temp-buffer-name " *MIME-temp*")
56 ;;; @ charset and encoding
59 (defvar mime-charset-type-list
61 (iso-8859-1 8 "quoted-printable")
62 (iso-8859-2 8 "quoted-printable")
63 (iso-8859-3 8 "quoted-printable")
64 (iso-8859-4 8 "quoted-printable")
65 (iso-8859-5 8 "quoted-printable")
66 (koi8-r 8 "quoted-printable")
67 (iso-8859-7 8 "quoted-printable")
68 (iso-8859-8 8 "quoted-printable")
69 (iso-8859-9 8 "quoted-printable")
70 (iso-2022-jp 7 "base64")
71 (iso-2022-kr 7 "base64")
73 (gb2312 8 "quoted-printable")
75 (iso-2022-jp-2 7 "base64")
76 (iso-2022-int-1 7 "base64")
79 (defun mime/encoding-name (transfer-level &optional not-omit)
80 (cond ((> transfer-level 8) "binary")
81 ((= transfer-level 8) "8bit")
85 (defun mime/make-charset-default-encoding-alist (transfer-level)
87 (lambda (charset-type)
88 (let ((charset (upcase (symbol-name (car charset-type))))
89 (type (nth 1 charset-type))
90 (encoding (nth 2 charset-type))
92 (if (<= type transfer-level)
93 (cons charset (mime/encoding-name type))
94 (cons charset encoding)
96 mime-charset-type-list))
102 (defun tm:set-face-region (b e face)
103 (let ((overlay (tl:make-overlay b e)))
104 (tl:overlay-put overlay 'face face)
107 (defvar tm:button-face 'bold
108 "Face used for content-button or URL-button of MIME-Preview buffer.
111 (defvar tm:mouse-face 'highlight
112 "Face used for MIME-preview buffer mouse highlighting. [tm-def.el]")
114 (defvar tm:warning-face nil
115 "Face used for invalid encoded-word.")
117 (defun tm:add-button (from to func &optional data)
118 "Create a button between FROM and TO with callback FUNC and data DATA."
120 (tl:overlay-put (tl:make-overlay from to) 'face tm:button-face))
121 (tl:add-text-properties from to
122 (append (and tm:mouse-face
123 (list 'mouse-face tm:mouse-face))
124 (list 'tm-callback func)
125 (and data (list 'tm-data data))
129 (defvar tm:mother-button-dispatcher nil)
131 (defun tm:button-dispatcher (event)
132 "Select the button under point."
134 (let (buf point func data)
135 (save-window-excursion
136 (mouse-set-point event)
137 (setq buf (current-buffer)
139 func (get-text-property (point) 'tm-callback)
140 data (get-text-property (point) 'tm-data)
148 (if (fboundp tm:mother-button-dispatcher)
149 (funcall tm:mother-button-dispatcher event)
157 (defvar tm:URL-regexp
158 "\\(http\\|ftp\\|file\\|gopher\\|news\\|telnet\\|wais\\|mailto\\):\\(//[-a-zA-Z0-9_.]+:[0-9]*\\)?[-a-zA-Z0-9_=?#$@~`%&*+|\\/.,]*[-a-zA-Z0-9_=#$@~`%&*+|\\/]")
160 (defvar browse-url-browser-function nil)
162 (defun tm:browse-url (&optional url)
163 (if (fboundp browse-url-browser-function)
165 (funcall browse-url-browser-function url)
166 (call-interactively browse-url-browser-function))
167 (if (fboundp tm:mother-button-dispatcher)
168 (call-interactively tm:mother-button-dispatcher)
173 ;;; @ definitions about MIME
176 (defconst mime/tspecials "][\000-\040()<>@,\;:\\\"/?.=")
177 (defconst mime/token-regexp (concat "[^" mime/tspecials "]+"))
178 (defconst mime/charset-regexp mime/token-regexp)
180 (defconst mime/content-type-subtype-regexp
181 (concat mime/token-regexp "/" mime/token-regexp))
183 (defconst mime/disposition-type-regexp mime/token-regexp)
189 (defconst base64-token-regexp "[A-Za-z0-9+/]")
190 (defconst base64-token-padding-regexp "[A-Za-z0-9+/=]")
192 (defconst mime/B-encoded-text-regexp
201 base64-token-padding-regexp
202 base64-token-padding-regexp
205 (defconst mime/B-encoding-and-encoded-text-regexp
206 (concat "\\(B\\)\\?" mime/B-encoded-text-regexp))
209 ;;; @@ Quoted-Printable
212 (defconst quoted-printable-hex-chars "0123456789ABCDEF")
213 (defconst quoted-printable-octet-regexp
214 (concat "=[" quoted-printable-hex-chars
215 "][" quoted-printable-hex-chars "]"))
217 (defconst mime/Q-encoded-text-regexp
218 (concat "\\([^=?]\\|" quoted-printable-octet-regexp "\\)+"))
219 (defconst mime/Q-encoding-and-encoded-text-regexp
220 (concat "\\(Q\\)\\?" mime/Q-encoded-text-regexp))
225 ;; caesar-region written by phr@prep.ai.mit.edu Nov 86
226 ;; modified by tower@prep Nov 86
227 ;; gnus-caesar-region
228 ;; Modified by umerin@flab.flab.Fujitsu.JUNET for ROT47.
229 (defun tm:caesar-region (&optional n)
230 "Caesar rotation of region by N, default 13, for decrypting netnews.
231 ROT47 will be performed for Japanese text in any case."
232 (interactive (if current-prefix-arg ; Was there a prefix arg?
233 (list (prefix-numeric-value current-prefix-arg))
235 (cond ((not (numberp n)) (setq n 13))
236 (t (setq n (mod n 26)))) ;canonicalize N
237 (if (not (zerop n)) ; no action needed for a rot of 0
239 (if (or (not (boundp 'caesar-translate-table))
240 (/= (aref caesar-translate-table ?a) (+ ?a n)))
241 (let ((i 0) (lower "abcdefghijklmnopqrstuvwxyz") upper)
242 (message "Building caesar-translate-table...")
243 (setq caesar-translate-table (make-vector 256 0))
245 (aset caesar-translate-table i i)
247 (setq lower (concat lower lower) upper (upcase lower) i 0)
249 (aset caesar-translate-table (+ ?a i) (aref lower (+ i n)))
250 (aset caesar-translate-table (+ ?A i) (aref upper (+ i n)))
252 ;; ROT47 for Japanese text.
253 ;; Thanks to ichikawa@flab.fujitsu.junet.
255 (let ((t1 (logior ?O 128))
257 (t3 (logior ?~ 128)))
259 (aset caesar-translate-table i
260 (let ((v (aref caesar-translate-table i)))
261 (if (<= v t1) (if (< v t2) v (+ v 47))
262 (if (<= v t3) (- v 47) v))))
264 (message "Building caesar-translate-table...done")))
265 (let ((from (region-beginning))
268 (setq str (buffer-substring from to))
269 (setq len (length str))
271 (aset str i (aref caesar-translate-table (aref str i)))
274 (delete-region from to)
281 (defun tm:set-fields (sym field-list &optional regexp-sym)
284 (let ((name (symbol-name sym)))
286 (concat (if (string-match "\\(.*\\)-list" name)
287 (substring name 0 (match-end 1))
294 (concat "^" (apply (function regexp-or) field-list) ":"))
297 (defun tm:add-fields (sym field-list &optional regexp-sym)
300 (let ((name (symbol-name sym)))
302 (concat (if (string-match "\\(.*\\)-list" name)
303 (substring name 0 (match-end 1))
308 (let ((fields (eval sym)))
311 (or (member field fields)
312 (setq fields (cons field fields))
318 (concat "^" (apply (function regexp-or) fields) ":"))
322 (defun tm:delete-fields (sym field-list &optional regexp-sym)
325 (let ((name (symbol-name sym)))
327 (concat (if (string-match "\\(.*\\)-list" name)
328 (substring name 0 (match-end 1))
333 (let ((fields (eval sym)))
336 (setq fields (delete field fields))
340 (concat "^" (apply (function regexp-or) fields) ":"))
350 ;;; tm-def.el ends here