tm 7.100.2.
[elisp/tm.git] / tm-def.el
1 ;;; tm-def.el --- definition module for tm
2
3 ;; Copyright (C) 1995,1996 Free Software Foundation, Inc.
4
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; Version: $Id: tm-def.el,v 7.67 1996/12/27 20:39:43 morioka Exp $
7 ;; Keywords: mail, news, MIME, multimedia, definition
8
9 ;; This file is part of tm (Tools for MIME).
10
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.
15
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.
20
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.
25
26 ;;; Code:
27
28 (require 'emu)
29
30
31 ;;; @ variables
32 ;;;
33
34 (defvar mime/tmp-dir (or (getenv "TM_TMP_DIR") "/tmp/"))
35
36 (defvar mime/use-multi-frame
37   (and (>= emacs-major-version 19) window-system))
38
39 (defvar mime/find-file-function
40   (if mime/use-multi-frame
41       (function find-file-other-frame)
42     (function find-file)
43     ))
44
45 (defvar mime/output-buffer-window-is-shared-with-bbdb t
46   "*If t, mime/output-buffer window is shared with BBDB window.")
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 ;;; @ charset and encoding
57 ;;;
58
59 (defvar mime-charset-type-list
60   '((us-ascii           7 nil)
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")
72     (euc-kr             8 "base64")
73     (gb2312             8 "quoted-printable")
74     (big5               8 "base64")
75     (iso-2022-jp-2      7 "base64")
76     (iso-2022-int-1     7 "base64")
77     ))
78
79 (defun mime/encoding-name (transfer-level &optional not-omit)
80   (cond ((> transfer-level 8) "binary")
81         ((= transfer-level 8) "8bit")
82         (not-omit "7bit")
83         ))
84
85 (defun mime/make-charset-default-encoding-alist (transfer-level)
86   (mapcar (function
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))
91                    )
92                (if (<= type transfer-level)
93                    (cons charset (mime/encoding-name type))
94                  (cons charset encoding)
95                  ))))
96           mime-charset-type-list))
97
98
99 ;;; @ button
100 ;;;
101
102 (defun tm:set-face-region (b e face)
103   (let ((overlay (tl:make-overlay b e)))
104     (tl:overlay-put overlay 'face face)
105     ))
106
107 (defvar tm:button-face 'bold
108   "Face used for content-button or URL-button of MIME-Preview buffer.
109 \[tm-def.el]")
110
111 (defvar tm:mouse-face 'highlight
112   "Face used for MIME-preview buffer mouse highlighting. [tm-def.el]")
113
114 (defun tm:add-button (from to func &optional data)
115   "Create a button between FROM and TO with callback FUNC and data DATA."
116   (and tm:button-face
117        (tl:overlay-put (tl:make-overlay from to) 'face tm:button-face))
118   (tl:add-text-properties from to
119                           (append (and tm:mouse-face
120                                        (list 'mouse-face tm:mouse-face))
121                                   (list 'tm-callback func)
122                                   (and data (list 'tm-data data))
123                                   ))
124   )
125
126 (defvar tm:mother-button-dispatcher nil)
127
128 (defun tm:button-dispatcher (event)
129   "Select the button under point."
130   (interactive "e")
131   (let (buf point func data)
132     (save-window-excursion
133       (mouse-set-point event)
134       (setq buf (current-buffer)
135             point (point)
136             func (get-text-property (point) 'tm-callback)
137             data (get-text-property (point) 'tm-data)
138             )
139       )
140     (save-excursion
141       (set-buffer buf)
142       (goto-char point)
143       (if func
144           (apply func data)
145         (if (fboundp tm:mother-button-dispatcher)
146             (funcall tm:mother-button-dispatcher event)
147           )
148         ))))
149
150
151 ;;; @ for URL
152 ;;;
153
154 (defvar tm:URL-regexp
155   "\\(http\\|ftp\\|file\\|gopher\\|news\\|telnet\\|wais\\|mailto\\):\\(//[-a-zA-Z0-9_.]+:[0-9]*\\)?[-a-zA-Z0-9_=?#$@~`%&*+|\\/.,]*[-a-zA-Z0-9_=#$@~`%&*+|\\/]")
156
157 (defvar browse-url-browser-function nil)
158
159 (defun tm:browse-url (&optional url)
160   (if (fboundp browse-url-browser-function)
161       (if url 
162         (funcall browse-url-browser-function url)
163       (call-interactively browse-url-browser-function))
164     (if (fboundp tm:mother-button-dispatcher)
165         (call-interactively tm:mother-button-dispatcher)
166       )
167     ))
168
169
170 ;;; @ definitions about MIME
171 ;;;
172
173 (defconst mime/tspecials "][\000-\040()<>@,\;:\\\"/?.=")
174 (defconst mime/token-regexp (concat "[^" mime/tspecials "]+"))
175 (defconst mime/charset-regexp mime/token-regexp)
176
177 (defconst mime/content-type-subtype-regexp
178   (concat mime/token-regexp "/" mime/token-regexp))
179
180 (defconst mime/disposition-type-regexp mime/token-regexp)
181
182
183 ;;; @@ Base64
184 ;;;
185
186 (defconst base64-token-regexp "[A-Za-z0-9+/=]")
187 (defconst base64-token-padding-regexp "[A-Za-z0-9+/=]")
188
189 (defconst mime/B-encoded-text-regexp
190   (concat "\\(\\("
191           base64-token-regexp
192           base64-token-regexp
193           base64-token-regexp
194           base64-token-regexp
195           "\\)*"
196           base64-token-regexp
197           base64-token-regexp
198           base64-token-padding-regexp
199           base64-token-padding-regexp
200           "\\)"))
201
202 (defconst mime/B-encoding-and-encoded-text-regexp
203   (concat "\\(B\\)\\?" mime/B-encoded-text-regexp))
204
205
206 ;;; @@ Quoted-Printable
207 ;;;
208
209 (defconst quoted-printable-hex-chars "0123456789ABCDEF")
210 (defconst quoted-printable-octet-regexp
211   (concat "=[" quoted-printable-hex-chars
212           "][" quoted-printable-hex-chars "]"))
213
214 (defconst mime/Q-encoded-text-regexp
215   (concat "\\([^=?]\\|" quoted-printable-octet-regexp "\\)+"))
216 (defconst mime/Q-encoding-and-encoded-text-regexp
217   (concat "\\(Q\\)\\?" mime/Q-encoded-text-regexp))
218
219
220 ;;; @ rot13-47
221 ;;;
222 ;; caesar-region written by phr@prep.ai.mit.edu  Nov 86
223 ;; modified by tower@prep Nov 86
224 ;; gnus-caesar-region
225 ;; Modified by umerin@flab.flab.Fujitsu.JUNET for ROT47.
226 (defun tm:caesar-region (&optional n)
227   "Caesar rotation of region by N, default 13, for decrypting netnews.
228 ROT47 will be performed for Japanese text in any case."
229   (interactive (if current-prefix-arg   ; Was there a prefix arg?
230                    (list (prefix-numeric-value current-prefix-arg))
231                  (list nil)))
232   (cond ((not (numberp n)) (setq n 13))
233         (t (setq n (mod n 26))))        ;canonicalize N
234   (if (not (zerop n))           ; no action needed for a rot of 0
235       (progn
236         (if (or (not (boundp 'caesar-translate-table))
237                 (/= (aref caesar-translate-table ?a) (+ ?a n)))
238             (let ((i 0) (lower "abcdefghijklmnopqrstuvwxyz") upper)
239               (message "Building caesar-translate-table...")
240               (setq caesar-translate-table (make-vector 256 0))
241               (while (< i 256)
242                 (aset caesar-translate-table i i)
243                 (setq i (1+ i)))
244               (setq lower (concat lower lower) upper (upcase lower) i 0)
245               (while (< i 26)
246                 (aset caesar-translate-table (+ ?a i) (aref lower (+ i n)))
247                 (aset caesar-translate-table (+ ?A i) (aref upper (+ i n)))
248                 (setq i (1+ i)))
249               ;; ROT47 for Japanese text.
250               ;; Thanks to ichikawa@flab.fujitsu.junet.
251               (setq i 161)
252               (let ((t1 (logior ?O 128))
253                     (t2 (logior ?! 128))
254                     (t3 (logior ?~ 128)))
255                 (while (< i 256)
256                   (aset caesar-translate-table i
257                         (let ((v (aref caesar-translate-table i)))
258                           (if (<= v t1) (if (< v t2) v (+ v 47))
259                             (if (<= v t3) (- v 47) v))))
260                   (setq i (1+ i))))
261               (message "Building caesar-translate-table...done")))
262         (let ((from (region-beginning))
263               (to (region-end))
264               (i 0) str len)
265           (setq str (buffer-substring from to))
266           (setq len (length str))
267           (while (< i len)
268             (aset str i (aref caesar-translate-table (aref str i)))
269             (setq i (1+ i)))
270           (goto-char from)
271           (delete-region from to)
272           (insert str)))))
273
274
275 ;;; @ field
276 ;;;
277
278 (defun tm:set-fields (sym field-list &optional regexp-sym)
279   (or regexp-sym
280       (setq regexp-sym
281             (let ((name (symbol-name sym)))
282               (intern
283                (concat (if (string-match "\\(.*\\)-list" name)
284                            (substring name 0 (match-end 1))
285                          name)
286                        "-regexp")
287                )))
288       )
289   (set sym field-list)
290   (set regexp-sym
291        (concat "^" (apply (function regexp-or) field-list) ":"))
292   )
293
294 (defun tm:add-fields (sym field-list &optional regexp-sym)
295   (or regexp-sym
296       (setq regexp-sym
297             (let ((name (symbol-name sym)))
298               (intern
299                (concat (if (string-match "\\(.*\\)-list" name)
300                            (substring name 0 (match-end 1))
301                          name)
302                        "-regexp")
303                )))
304       )
305   (let ((fields (eval sym)))
306     (mapcar (function
307              (lambda (field)
308                (or (member field fields)
309                    (setq fields (cons field fields))
310                    )
311                ))
312             (reverse field-list)
313             )
314     (set regexp-sym
315          (concat "^" (apply (function regexp-or) fields) ":"))
316     (set sym fields)
317     ))
318
319 (defun tm:delete-fields (sym field-list &optional regexp-sym)
320   (or regexp-sym
321       (setq regexp-sym
322             (let ((name (symbol-name sym)))
323               (intern
324                (concat (if (string-match "\\(.*\\)-list" name)
325                            (substring name 0 (match-end 1))
326                          name)
327                        "-regexp")
328                )))
329       )
330   (let ((fields (eval sym)))
331     (mapcar (function
332              (lambda (field)
333                (setq fields (delete field fields))
334                ))
335             field-list)
336     (set regexp-sym
337          (concat "^" (apply (function regexp-or) fields) ":"))
338     (set sym fields)
339     ))
340
341
342 ;;; @ end
343 ;;;
344
345 (provide 'tm-def)
346
347 ;;; tm-def.el ends here