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.20 1996/03/05 21:12:40 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 ;;; @ for various Emacs variants
59 (cond ((boundp 'MULE) (require 'tm-mule))
60 ((boundp 'NEMACS)(require 'tm-nemacs))
61 (t (require 'tm-orig))
65 ;;; @ charset and encoding
68 (defun mime/find-charset (lcl)
70 (or (cdr (some-element
73 (subsetp lcl (car elt))
75 mime/lc-charset-alist)
80 (defun mime/find-charset-region (beg end)
81 (mime/find-charset (cons lc-ascii (find-charset-region beg end)))
84 (defvar mime/charset-type-list
86 ("ISO-8859-1" 8 "quoted-printable")
87 ("ISO-8859-2" 8 "quoted-printable")
88 ("ISO-8859-3" 8 "quoted-printable")
89 ("ISO-8859-4" 8 "quoted-printable")
90 ("ISO-8859-5" 8 "quoted-printable")
91 ("KOI8-R" 8 "quoted-printable")
92 ("ISO-8859-7" 8 "quoted-printable")
93 ("ISO-8859-8" 8 "quoted-printable")
94 ("ISO-8859-9" 8 "quoted-printable")
95 ("ISO-2022-JP" 7 "base64")
96 ("ISO-2022-KR" 7 "base64")
98 ("ISO-2022-JP-2" 7 "base64")
99 ("ISO-2022-INT-1" 7 "base64")
102 (defun mime/encoding-name (transfer-level &optional not-omit)
103 (cond ((> transfer-level 8) "binary")
104 ((= transfer-level 8) "8bit")
108 (defun mime/make-charset-default-encoding-alist (transfer-level)
110 (lambda (charset-type)
111 (let ((charset (car charset-type))
112 (type (nth 1 charset-type))
113 (encoding (nth 2 charset-type))
115 (if (<= type transfer-level)
116 (cons charset (mime/encoding-name type))
117 (cons charset encoding)
119 mime/charset-type-list))
125 (defvar mime/default-coding-system *ctext*)
131 (defun tm:set-face-region (b e face)
132 (let ((overlay (tl:make-overlay b e)))
133 (tl:overlay-put overlay 'face face)
136 (setq tm:button-face 'bold)
137 (setq tm:mouse-face 'highlight)
139 (defun tm:add-button (from to func &optional data)
140 "Create a button between FROM and TO with callback FUNC and data DATA."
142 (tl:overlay-put (tl:make-overlay from to) 'face tm:button-face))
143 (tl:add-text-properties from to
144 (append (and tm:mouse-face
145 (list 'mouse-face tm:mouse-face))
146 (list 'tm-callback func)
147 (and data (list 'tm-data data))
151 (defvar tm:mother-button-dispatcher nil)
153 (defun tm:button-dispatcher (event)
154 "Select the button under point."
156 (mouse-set-point event)
157 (let ((func (get-text-property (point) 'tm-callback))
158 (data (get-text-property (point) 'tm-data))
162 (if (fboundp tm:mother-button-dispatcher)
163 (funcall tm:mother-button-dispatcher event)
171 (defvar tm:URL-regexp
172 "\\(http\\|ftp\\|file\\|gopher\\|news\\|telnet\\|wais\\|mailto\\):\\(//[-a-zA-Z0-9_.]+:[0-9]*\\)?[-a-zA-Z0-9_=?#$@~`%&*+|\\/.,]*[-a-zA-Z0-9_=#$@~`%&*+|\\/]")
174 (defvar browse-url-browser-function nil)
176 (defun tm:browse-url (&optional url)
177 (if (fboundp browse-url-browser-function)
178 (call-interactively browse-url-browser-function)
179 (if (fboundp tm:mother-button-dispatcher)
180 (call-interactively tm:mother-button-dispatcher)
185 ;;; @ definitions about MIME
188 (defconst mime/tspecials "][\000-\040()<>@,\;:\\\"/?.=")
189 (defconst mime/token-regexp (concat "[^" mime/tspecials "]+"))
190 (defconst mime/charset-regexp mime/token-regexp)
192 (defconst mime/content-type-subtype-regexp
193 (concat mime/token-regexp "/" mime/token-regexp))
194 (defconst mime/content-parameter-value-regexp
196 rfc822/quoted-string-regexp
199 (defconst mime/disposition-type-regexp mime/token-regexp)
205 (defconst base64-token-regexp "[A-Za-z0-9+/=]")
207 (defconst mime/B-encoded-text-regexp
214 (defconst mime/B-encoding-and-encoded-text-regexp
215 (concat "\\(B\\)\\?" mime/B-encoded-text-regexp))
218 ;;; @@ Quoted-Printable
221 (defconst quoted-printable-hex-chars "0123456789ABCDEF")
222 (defconst quoted-printable-octet-regexp
223 (concat "=[" quoted-printable-hex-chars
224 "][" quoted-printable-hex-chars "]"))
226 (defconst mime/Q-encoded-text-regexp
227 (concat "\\([^=?]\\|" quoted-printable-octet-regexp "\\)+"))
228 (defconst mime/Q-encoding-and-encoded-text-regexp
229 (concat "\\(Q\\)\\?" mime/Q-encoded-text-regexp))
234 ;; caesar-region written by phr@prep.ai.mit.edu Nov 86
235 ;; modified by tower@prep Nov 86
236 ;; gnus-caesar-region
237 ;; Modified by umerin@flab.flab.Fujitsu.JUNET for ROT47.
238 (defun tm:caesar-region (&optional n)
239 "Caesar rotation of region by N, default 13, for decrypting netnews.
240 ROT47 will be performed for Japanese text in any case."
241 (interactive (if current-prefix-arg ; Was there a prefix arg?
242 (list (prefix-numeric-value current-prefix-arg))
244 (cond ((not (numberp n)) (setq n 13))
245 (t (setq n (mod n 26)))) ;canonicalize N
246 (if (not (zerop n)) ; no action needed for a rot of 0
248 (if (or (not (boundp 'caesar-translate-table))
249 (/= (aref caesar-translate-table ?a) (+ ?a n)))
250 (let ((i 0) (lower "abcdefghijklmnopqrstuvwxyz") upper)
251 (message "Building caesar-translate-table...")
252 (setq caesar-translate-table (make-vector 256 0))
254 (aset caesar-translate-table i i)
256 (setq lower (concat lower lower) upper (upcase lower) i 0)
258 (aset caesar-translate-table (+ ?a i) (aref lower (+ i n)))
259 (aset caesar-translate-table (+ ?A i) (aref upper (+ i n)))
261 ;; ROT47 for Japanese text.
262 ;; Thanks to ichikawa@flab.fujitsu.junet.
264 (let ((t1 (logior ?O 128))
266 (t3 (logior ?~ 128)))
268 (aset caesar-translate-table i
269 (let ((v (aref caesar-translate-table i)))
270 (if (<= v t1) (if (< v t2) v (+ v 47))
271 (if (<= v t3) (- v 47) v))))
273 (message "Building caesar-translate-table...done")))
274 (let ((from (region-beginning))
277 (setq str (buffer-substring from to))
278 (setq len (length str))
280 (aset str i (aref caesar-translate-table (aref str i)))
283 (delete-region from to)
290 (defun tm:set-fields (sym field-list &optional regexp-sym)
293 (let ((name (symbol-name sym)))
295 (concat (if (string-match "\\(.*\\)-list" name)
296 (substring name 0 (match-end 1))
303 (concat "^" (apply (function regexp-or) field-list) ":"))
306 (defun tm:add-fields (sym field-list &optional regexp-sym)
309 (let ((name (symbol-name sym)))
311 (concat (if (string-match "\\(.*\\)-list" name)
312 (substring name 0 (match-end 1))
317 (let ((fields (eval sym)))
320 (or (member field fields)
321 (setq fields (cons field fields))
327 (concat "^" (apply (function regexp-or) fields) ":"))
331 (defun tm:delete-fields (sym field-list &optional regexp-sym)
334 (let ((name (symbol-name sym)))
336 (concat (if (string-match "\\(.*\\)-list" name)
337 (substring name 0 (match-end 1))
342 (let ((fields (eval sym)))
345 (setq fields (delete field fields))
349 (concat "^" (apply (function regexp-or) fields) ":"))
359 ;;; tm-def.el ends here