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