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