Variable `mime-charset-type-list' was moved to mime-edit.el.
[elisp/semi.git] / mime-def.el
1 ;;; mime-def.el --- definition module for SEMI
2
3 ;; Copyright (C) 1995,1996,1997 Free Software Foundation, Inc.
4
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; Version: $Id: mime-def.el,v 0.8 1997-02-25 06:15:38 tmorioka Exp $
7 ;; Keywords: definition, MIME, multimedia, mail, news
8
9 ;; This file is part of SEMI (SEMI is Emacs MIME Interfaces).
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 (defun mime/encoding-name (transfer-level &optional not-omit)
60   (cond ((> transfer-level 8) "binary")
61         ((= transfer-level 8) "8bit")
62         (not-omit "7bit")
63         ))
64
65 (defun mime/make-charset-default-encoding-alist (transfer-level)
66   (mapcar (function
67            (lambda (charset-type)
68              (let ((charset  (upcase (symbol-name (car charset-type))))
69                    (type     (nth 1 charset-type))
70                    (encoding (nth 2 charset-type))
71                    )
72                (if (<= type transfer-level)
73                    (cons charset (mime/encoding-name type))
74                  (cons charset encoding)
75                  ))))
76           mime-charset-type-list))
77
78
79 ;;; @ button
80 ;;;
81
82 (defun tm:set-face-region (b e face)
83   (let ((overlay (tl:make-overlay b e)))
84     (tl:overlay-put overlay 'face face)
85     ))
86
87 (defvar tm:button-face 'bold
88   "Face used for content-button or URL-button of MIME-Preview buffer.
89 \[mime-def.el]")
90
91 (defvar tm:mouse-face 'highlight
92   "Face used for MIME-preview buffer mouse highlighting. [mime-def.el]")
93
94 (defvar tm:warning-face nil
95   "Face used for invalid encoded-word.")
96
97 (defun tm:add-button (from to func &optional data)
98   "Create a button between FROM and TO with callback FUNC and data DATA."
99   (and tm:button-face
100        (tl:overlay-put (tl:make-overlay from to) 'face tm:button-face))
101   (tl:add-text-properties from to
102                           (append (and tm:mouse-face
103                                        (list 'mouse-face tm:mouse-face))
104                                   (list 'mime-callback func)
105                                   (and data (list 'mime-data data))
106                                   ))
107   )
108
109 (defvar tm:mother-button-dispatcher nil)
110
111 (defun tm:button-dispatcher (event)
112   "Select the button under point."
113   (interactive "e")
114   (let (buf point func data)
115     (save-window-excursion
116       (mouse-set-point event)
117       (setq buf (current-buffer)
118             point (point)
119             func (get-text-property (point) 'mime-callback)
120             data (get-text-property (point) 'mime-data)
121             )
122       )
123     (save-excursion
124       (set-buffer buf)
125       (goto-char point)
126       (if func
127           (apply func data)
128         (if (fboundp tm:mother-button-dispatcher)
129             (funcall tm:mother-button-dispatcher event)
130           )
131         ))))
132
133
134 ;;; @ for URL
135 ;;;
136
137 (defvar tm:URL-regexp
138   "\\(http\\|ftp\\|file\\|gopher\\|news\\|telnet\\|wais\\|mailto\\):\\(//[-a-zA-Z0-9_.]+:[0-9]*\\)?[-a-zA-Z0-9_=?#$@~`%&*+|\\/.,]*[-a-zA-Z0-9_=#$@~`%&*+|\\/]")
139
140 (defvar browse-url-browser-function nil)
141
142 (defun tm:browse-url (&optional url)
143   (if (fboundp browse-url-browser-function)
144       (if url 
145         (funcall browse-url-browser-function url)
146       (call-interactively browse-url-browser-function))
147     (if (fboundp tm:mother-button-dispatcher)
148         (call-interactively tm:mother-button-dispatcher)
149       )
150     ))
151
152
153 ;;; @ PGP
154 ;;;
155
156 (defvar pgp-function-alist
157   '(
158     ;; for mime-pgp
159     (verify             mc-verify                       "mc-toplev")
160     (decrypt            mc-decrypt                      "mc-toplev")
161     (fetch-key          mc-pgp-fetch-key                "mc-pgp")
162     (snarf-keys         mc-snarf-keys                   "mc-toplev")
163     ;; for mime-edit
164     (mime-sign          tm:mc-pgp-sign-region           "mime-edit-mc")
165     (traditional-sign   mc-pgp-sign-region              "mc-pgp")
166     (encrypt            tm:mc-pgp-encrypt-region        "mime-edit-mc")
167     (insert-key         mc-insert-public-key            "mc-toplev")
168     )
169   "Alist of service names vs. corresponding functions and its filenames.
170 Each element looks like (SERVICE FUNCTION FILE).
171
172 SERVICE is a symbol of PGP processing.  It allows `verify', `decrypt',
173 `fetch-key', `snarf-keys', `mime-sign', `traditional-sign', `encrypt'
174 or `insert-key'.
175
176 Function is a symbol of function to do specified SERVICE.
177
178 FILE is string of filename which has definition of corresponding
179 FUNCTION.")
180
181 (defmacro pgp-function (method)
182   "Return function to do service METHOD."
183   (` (car (cdr (assq (, method) (symbol-value 'pgp-function-alist)))))
184   )
185
186 (mapcar (function
187          (lambda (method)
188            (autoload (second method)(third method))
189            ))
190         pgp-function-alist)
191
192
193 ;;; @ definitions about MIME
194 ;;;
195
196 (defconst mime/tspecials "][\000-\040()<>@,\;:\\\"/?.=")
197 (defconst mime/token-regexp (concat "[^" mime/tspecials "]+"))
198 (defconst mime-charset-regexp mime/token-regexp)
199
200 (defconst mime/content-type-subtype-regexp
201   (concat mime/token-regexp "/" mime/token-regexp))
202
203 (defconst mime/disposition-type-regexp mime/token-regexp)
204
205
206 ;;; @ rot13-47
207 ;;;
208 ;; caesar-region written by phr@prep.ai.mit.edu  Nov 86
209 ;; modified by tower@prep Nov 86
210 ;; gnus-caesar-region
211 ;; Modified by umerin@flab.flab.Fujitsu.JUNET for ROT47.
212 (defun tm:caesar-region (&optional n)
213   "Caesar rotation of region by N, default 13, for decrypting netnews.
214 ROT47 will be performed for Japanese text in any case."
215   (interactive (if current-prefix-arg   ; Was there a prefix arg?
216                    (list (prefix-numeric-value current-prefix-arg))
217                  (list nil)))
218   (cond ((not (numberp n)) (setq n 13))
219         (t (setq n (mod n 26))))        ;canonicalize N
220   (if (not (zerop n))           ; no action needed for a rot of 0
221       (progn
222         (if (or (not (boundp 'caesar-translate-table))
223                 (/= (aref caesar-translate-table ?a) (+ ?a n)))
224             (let ((i 0) (lower "abcdefghijklmnopqrstuvwxyz") upper)
225               (message "Building caesar-translate-table...")
226               (setq caesar-translate-table (make-vector 256 0))
227               (while (< i 256)
228                 (aset caesar-translate-table i i)
229                 (setq i (1+ i)))
230               (setq lower (concat lower lower) upper (upcase lower) i 0)
231               (while (< i 26)
232                 (aset caesar-translate-table (+ ?a i) (aref lower (+ i n)))
233                 (aset caesar-translate-table (+ ?A i) (aref upper (+ i n)))
234                 (setq i (1+ i)))
235               ;; ROT47 for Japanese text.
236               ;; Thanks to ichikawa@flab.fujitsu.junet.
237               (setq i 161)
238               (let ((t1 (logior ?O 128))
239                     (t2 (logior ?! 128))
240                     (t3 (logior ?~ 128)))
241                 (while (< i 256)
242                   (aset caesar-translate-table i
243                         (let ((v (aref caesar-translate-table i)))
244                           (if (<= v t1) (if (< v t2) v (+ v 47))
245                             (if (<= v t3) (- v 47) v))))
246                   (setq i (1+ i))))
247               (message "Building caesar-translate-table...done")))
248         (let ((from (region-beginning))
249               (to (region-end))
250               (i 0) str len)
251           (setq str (buffer-substring from to))
252           (setq len (length str))
253           (while (< i len)
254             (aset str i (aref caesar-translate-table (aref str i)))
255             (setq i (1+ i)))
256           (goto-char from)
257           (delete-region from to)
258           (insert str)))))
259
260
261 ;;; @ field
262 ;;;
263
264 (defun tm:set-fields (sym field-list &optional regexp-sym)
265   (or regexp-sym
266       (setq regexp-sym
267             (let ((name (symbol-name sym)))
268               (intern
269                (concat (if (string-match "\\(.*\\)-list" name)
270                            (substring name 0 (match-end 1))
271                          name)
272                        "-regexp")
273                )))
274       )
275   (set sym field-list)
276   (set regexp-sym
277        (concat "^" (apply (function regexp-or) field-list) ":"))
278   )
279
280 (defun tm:add-fields (sym field-list &optional regexp-sym)
281   (or regexp-sym
282       (setq regexp-sym
283             (let ((name (symbol-name sym)))
284               (intern
285                (concat (if (string-match "\\(.*\\)-list" name)
286                            (substring name 0 (match-end 1))
287                          name)
288                        "-regexp")
289                )))
290       )
291   (let ((fields (eval sym)))
292     (mapcar (function
293              (lambda (field)
294                (or (member field fields)
295                    (setq fields (cons field fields))
296                    )
297                ))
298             (reverse field-list)
299             )
300     (set regexp-sym
301          (concat "^" (apply (function regexp-or) fields) ":"))
302     (set sym fields)
303     ))
304
305 (defun tm:delete-fields (sym field-list &optional regexp-sym)
306   (or regexp-sym
307       (setq regexp-sym
308             (let ((name (symbol-name sym)))
309               (intern
310                (concat (if (string-match "\\(.*\\)-list" name)
311                            (substring name 0 (match-end 1))
312                          name)
313                        "-regexp")
314                )))
315       )
316   (let ((fields (eval sym)))
317     (mapcar (function
318              (lambda (field)
319                (setq fields (delete field fields))
320                ))
321             field-list)
322     (set regexp-sym
323          (concat "^" (apply (function regexp-or) fields) ":"))
324     (set sym fields)
325     ))
326
327
328 ;;; @ RCS version
329 ;;;
330
331 (defsubst get-version-string (id)
332   "Return a version-string from RCS ID."
333   (and (string-match ",v \\([0-9][0-9.][0-9.]+\\)" id)
334        (substring id (match-beginning 1)(match-end 1))
335        ))
336
337
338 ;;; @ end
339 ;;;
340
341 (provide 'mime-def)
342
343 ;;; mime-def.el ends here