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