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