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