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