Overlay is required by emu.
[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.47 1997-04-05 06:20:34 morioka 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 'cl)
29 (require 'emu)
30
31
32 ;;; @ variables
33 ;;;
34
35 (defvar mime/use-multi-frame
36   (and (>= emacs-major-version 19) window-system))
37
38 (defvar mime/find-file-function
39   (if mime/use-multi-frame
40       (function find-file-other-frame)
41     (function find-file)
42     ))
43
44
45 ;;; @ constants
46 ;;;
47
48 (defconst mime-echo-buffer-name "*MIME-echo*"
49   "Name of buffer to display MIME-playing information.")
50
51 (defconst mime/temp-buffer-name " *MIME-temp*")
52
53
54 ;;; @ definitions about MIME
55 ;;;
56
57 (defconst mime/tspecials "][\000-\040()<>@,\;:\\\"/?.=")
58 (defconst mime/token-regexp (concat "[^" mime/tspecials "]+"))
59 (defconst mime-charset-regexp mime/token-regexp)
60
61 (defconst mime/content-type-subtype-regexp
62   (concat mime/token-regexp "/" mime/token-regexp))
63
64 (defconst mime/disposition-type-regexp mime/token-regexp)
65
66
67 ;;; @ button
68 ;;;
69
70 (defvar mime-button-face 'bold
71   "Face used for content-button or URL-button of MIME-Preview buffer.")
72
73 (defvar mime-button-mouse-face 'highlight
74   "Face used for MIME-preview buffer mouse highlighting.")
75
76 (defsubst mime-add-button (from to func &optional data)
77   "Create a button between FROM and TO with callback FUNC and data DATA."
78   (and mime-button-face
79        (overlay-put (make-overlay from to) 'face mime-button-face))
80   (let ((props (cons 'mime-button-callback
81                      (cons func
82                            (if data
83                                (list 'mime-button-data data)
84                              )))))
85     (if mime-button-mouse-face
86         (setq props (cons 'mouse-face (cons mime-button-mouse-face props)))
87       )
88     (add-text-properties from to props)
89     ))
90
91 (defvar mime-button-mother-dispatcher nil)
92
93 (defun mime-button-dispatcher (event)
94   "Select the button under point."
95   (interactive "e")
96   (let (buf point func data)
97     (save-window-excursion
98       (mouse-set-point event)
99       (setq buf (current-buffer)
100             point (point)
101             func (get-text-property (point) 'mime-button-callback)
102             data (get-text-property (point) 'mime-button-data)
103             )
104       )
105     (save-excursion
106       (set-buffer buf)
107       (goto-char point)
108       (if func
109           (apply func data)
110         (if (fboundp mime-button-mother-dispatcher)
111             (funcall mime-button-mother-dispatcher event)
112           )
113         ))))
114
115
116 ;;; @ PGP
117 ;;;
118
119 (defvar pgp-function-alist
120   '(
121     ;; for mime-pgp
122     (verify             mc-verify                       "mc-toplev")
123     (decrypt            mc-decrypt                      "mc-toplev")
124     (fetch-key          mc-pgp-fetch-key                "mc-pgp")
125     (snarf-keys         mc-snarf-keys                   "mc-toplev")
126     ;; for mime-edit
127     (mime-sign          mime-mc-pgp-sign-region         "mime-mc")
128     (traditional-sign   mc-pgp-sign-region              "mc-pgp")
129     (encrypt            mime-mc-pgp-encrypt-region      "mime-mc")
130     (insert-key         mc-insert-public-key            "mc-toplev")
131     )
132   "Alist of service names vs. corresponding functions and its filenames.
133 Each element looks like (SERVICE FUNCTION FILE).
134
135 SERVICE is a symbol of PGP processing.  It allows `verify', `decrypt',
136 `fetch-key', `snarf-keys', `mime-sign', `traditional-sign', `encrypt'
137 or `insert-key'.
138
139 Function is a symbol of function to do specified SERVICE.
140
141 FILE is string of filename which has definition of corresponding
142 FUNCTION.")
143
144 (defmacro pgp-function (method)
145   "Return function to do service METHOD."
146   (` (car (cdr (assq (, method) (symbol-value 'pgp-function-alist)))))
147   )
148
149 (mapcar (function
150          (lambda (method)
151            (autoload (second method)(third method))
152            ))
153         pgp-function-alist)
154
155
156 ;;; @ method selector kernel
157 ;;;
158
159 (require 'atype)
160
161 ;;; @@ field unifier
162 ;;;
163
164 (defun field-unifier-for-mode (a b)
165   (let ((va (cdr a)))
166     (if (if (consp va)
167             (member (cdr b) va)
168           (equal va (cdr b))
169           )
170         (list nil b nil)
171       )))
172
173
174 ;;; @ rot13-47
175 ;;;
176 ;; caesar-region written by phr@prep.ai.mit.edu  Nov 86
177 ;; modified by tower@prep Nov 86
178 ;; gnus-caesar-region
179 ;; Modified by umerin@flab.flab.Fujitsu.JUNET for ROT47.
180 (defun tm:caesar-region (&optional n)
181   "Caesar rotation of region by N, default 13, for decrypting netnews.
182 ROT47 will be performed for Japanese text in any case."
183   (interactive (if current-prefix-arg   ; Was there a prefix arg?
184                    (list (prefix-numeric-value current-prefix-arg))
185                  (list nil)))
186   (cond ((not (numberp n)) (setq n 13))
187         (t (setq n (mod n 26))))        ;canonicalize N
188   (if (not (zerop n))           ; no action needed for a rot of 0
189       (progn
190         (if (or (not (boundp 'caesar-translate-table))
191                 (/= (aref caesar-translate-table ?a) (+ ?a n)))
192             (let ((i 0) (lower "abcdefghijklmnopqrstuvwxyz") upper)
193               (message "Building caesar-translate-table...")
194               (setq caesar-translate-table (make-vector 256 0))
195               (while (< i 256)
196                 (aset caesar-translate-table i i)
197                 (setq i (1+ i)))
198               (setq lower (concat lower lower) upper (upcase lower) i 0)
199               (while (< i 26)
200                 (aset caesar-translate-table (+ ?a i) (aref lower (+ i n)))
201                 (aset caesar-translate-table (+ ?A i) (aref upper (+ i n)))
202                 (setq i (1+ i)))
203               ;; ROT47 for Japanese text.
204               ;; Thanks to ichikawa@flab.fujitsu.junet.
205               (setq i 161)
206               (let ((t1 (logior ?O 128))
207                     (t2 (logior ?! 128))
208                     (t3 (logior ?~ 128)))
209                 (while (< i 256)
210                   (aset caesar-translate-table i
211                         (let ((v (aref caesar-translate-table i)))
212                           (if (<= v t1) (if (< v t2) v (+ v 47))
213                             (if (<= v t3) (- v 47) v))))
214                   (setq i (1+ i))))
215               (message "Building caesar-translate-table...done")))
216         (let ((from (region-beginning))
217               (to (region-end))
218               (i 0) str len)
219           (setq str (buffer-substring from to))
220           (setq len (length str))
221           (while (< i len)
222             (aset str i (aref caesar-translate-table (aref str i)))
223             (setq i (1+ i)))
224           (goto-char from)
225           (delete-region from to)
226           (insert str)))))
227
228
229 ;;; @ field
230 ;;;
231
232 (defsubst regexp-or (&rest args)
233   (concat "\\(" (mapconcat (function identity) args "\\|") "\\)"))
234
235 (defun tm:set-fields (sym field-list &optional regexp-sym)
236   (or regexp-sym
237       (setq regexp-sym
238             (let ((name (symbol-name sym)))
239               (intern
240                (concat (if (string-match "\\(.*\\)-list" name)
241                            (substring name 0 (match-end 1))
242                          name)
243                        "-regexp")
244                )))
245       )
246   (set sym field-list)
247   (set regexp-sym
248        (concat "^" (apply (function regexp-or) field-list) ":"))
249   )
250
251 (defun tm:add-fields (sym field-list &optional regexp-sym)
252   (or regexp-sym
253       (setq regexp-sym
254             (let ((name (symbol-name sym)))
255               (intern
256                (concat (if (string-match "\\(.*\\)-list" name)
257                            (substring name 0 (match-end 1))
258                          name)
259                        "-regexp")
260                )))
261       )
262   (let ((fields (eval sym)))
263     (mapcar (function
264              (lambda (field)
265                (or (member field fields)
266                    (setq fields (cons field fields))
267                    )
268                ))
269             (reverse field-list)
270             )
271     (set regexp-sym
272          (concat "^" (apply (function regexp-or) fields) ":"))
273     (set sym fields)
274     ))
275
276 (defun tm:delete-fields (sym field-list &optional regexp-sym)
277   (or regexp-sym
278       (setq regexp-sym
279             (let ((name (symbol-name sym)))
280               (intern
281                (concat (if (string-match "\\(.*\\)-list" name)
282                            (substring name 0 (match-end 1))
283                          name)
284                        "-regexp")
285                )))
286       )
287   (let ((fields (eval sym)))
288     (mapcar (function
289              (lambda (field)
290                (setq fields (delete field fields))
291                ))
292             field-list)
293     (set regexp-sym
294          (concat "^" (apply (function regexp-or) fields) ":"))
295     (set sym fields)
296     ))
297
298
299 ;;; @ RCS version
300 ;;;
301
302 (defsubst get-version-string (id)
303   "Return a version-string from RCS ID."
304   (and (string-match ",v \\([0-9][0-9.][0-9.]+\\)" id)
305        (substring id (match-beginning 1)(match-end 1))
306        ))
307
308
309 ;;; @ Other Utility
310 ;;;
311
312 (defsubst eliminate-top-spaces (string)
313   "Eliminate top sequence of space or tab in STRING."
314   (if (string-match "^[ \t]+" string)
315       (substring string (match-end 0))
316     string))
317
318 (defun call-after-loaded (module func &optional hook-name)
319   "If MODULE is provided, then FUNC is called.
320 Otherwise func is set to MODULE-load-hook.
321 If optional argument HOOK-NAME is specified,
322 it is used as hook to set."
323   (if (featurep module)
324       (funcall func)
325     (or hook-name
326         (setq hook-name (intern (concat (symbol-name module) "-load-hook")))
327         )
328     (add-hook hook-name func)
329     ))
330
331
332 ;;; @ end
333 ;;;
334
335 (provide 'mime-def)
336
337 ;;; mime-def.el ends here