`mime/content-decoding-condition' -> `mime-acting-condition'.
[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.50 1997-05-09 01:22:21 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 (autoload 'mule-caesar-region "mule-caesar"
32   "Caesar rotation of current region." t)
33
34
35 ;;; @ variables
36 ;;;
37
38 (defvar mime/use-multi-frame
39   (and (>= emacs-major-version 19) window-system))
40
41 (defvar mime/find-file-function
42   (if mime/use-multi-frame
43       (function find-file-other-frame)
44     (function find-file)
45     ))
46
47
48 ;;; @ constants
49 ;;;
50
51 (defconst mime-echo-buffer-name "*MIME-echo*"
52   "Name of buffer to display MIME-playing information.")
53
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 (defvar mime-button-face 'bold
74   "Face used for content-button or URL-button of MIME-Preview buffer.")
75
76 (defvar mime-button-mouse-face 'highlight
77   "Face used for MIME-preview buffer mouse highlighting.")
78
79 (defsubst mime-add-button (from to func &optional data)
80   "Create a button between FROM and TO with callback FUNC and data DATA."
81   (and mime-button-face
82        (overlay-put (make-overlay from to) 'face mime-button-face))
83   (let ((props (cons 'mime-button-callback
84                      (cons func
85                            (if data
86                                (list 'mime-button-data data)
87                              )))))
88     (if mime-button-mouse-face
89         (setq props (cons 'mouse-face (cons mime-button-mouse-face props)))
90       )
91     (add-text-properties from to props)
92     ))
93
94 (defvar mime-button-mother-dispatcher nil)
95
96 (defun mime-button-dispatcher (event)
97   "Select the button under point."
98   (interactive "e")
99   (let (buf point func data)
100     (save-window-excursion
101       (mouse-set-point event)
102       (setq buf (current-buffer)
103             point (point)
104             func (get-text-property (point) 'mime-button-callback)
105             data (get-text-property (point) 'mime-button-data)
106             )
107       )
108     (save-excursion
109       (set-buffer buf)
110       (goto-char point)
111       (if func
112           (apply func data)
113         (if (fboundp mime-button-mother-dispatcher)
114             (funcall mime-button-mother-dispatcher event)
115           )
116         ))))
117
118
119 ;;; @ PGP
120 ;;;
121
122 (defvar pgp-function-alist
123   '(
124     ;; for mime-pgp
125     (verify             mc-verify                       "mc-toplev")
126     (decrypt            mc-decrypt                      "mc-toplev")
127     (fetch-key          mc-pgp-fetch-key                "mc-pgp")
128     (snarf-keys         mc-snarf-keys                   "mc-toplev")
129     ;; for mime-edit
130     (mime-sign          mime-mc-pgp-sign-region         "mime-mc")
131     (traditional-sign   mc-pgp-sign-region              "mc-pgp")
132     (encrypt            mime-mc-pgp-encrypt-region      "mime-mc")
133     (insert-key         mc-insert-public-key            "mc-toplev")
134     )
135   "Alist of service names vs. corresponding functions and its filenames.
136 Each element looks like (SERVICE FUNCTION FILE).
137
138 SERVICE is a symbol of PGP processing.  It allows `verify', `decrypt',
139 `fetch-key', `snarf-keys', `mime-sign', `traditional-sign', `encrypt'
140 or `insert-key'.
141
142 Function is a symbol of function to do specified SERVICE.
143
144 FILE is string of filename which has definition of corresponding
145 FUNCTION.")
146
147 (defmacro pgp-function (method)
148   "Return function to do service METHOD."
149   (` (car (cdr (assq (, method) (symbol-value 'pgp-function-alist)))))
150   )
151
152 (mapcar (function
153          (lambda (method)
154            (autoload (second method)(third method))
155            ))
156         pgp-function-alist)
157
158
159 ;;; @ method selector kernel
160 ;;;
161
162 (require 'atype)
163
164 ;;; @@ field unifier
165 ;;;
166
167 (defun field-unifier-for-mode (a b)
168   (let ((va (cdr a)))
169     (if (if (consp va)
170             (member (cdr b) va)
171           (equal va (cdr b))
172           )
173         (list nil b nil)
174       )))
175
176
177 ;;; @ field
178 ;;;
179
180 (defsubst regexp-or (&rest args)
181   (concat "\\(" (mapconcat (function identity) args "\\|") "\\)"))
182
183 (defun tm:set-fields (sym field-list &optional regexp-sym)
184   (or regexp-sym
185       (setq regexp-sym
186             (let ((name (symbol-name sym)))
187               (intern
188                (concat (if (string-match "\\(.*\\)-list" name)
189                            (substring name 0 (match-end 1))
190                          name)
191                        "-regexp")
192                )))
193       )
194   (set sym field-list)
195   (set regexp-sym
196        (concat "^" (apply (function regexp-or) field-list) ":"))
197   )
198
199 (defun tm:add-fields (sym field-list &optional regexp-sym)
200   (or regexp-sym
201       (setq regexp-sym
202             (let ((name (symbol-name sym)))
203               (intern
204                (concat (if (string-match "\\(.*\\)-list" name)
205                            (substring name 0 (match-end 1))
206                          name)
207                        "-regexp")
208                )))
209       )
210   (let ((fields (eval sym)))
211     (mapcar (function
212              (lambda (field)
213                (or (member field fields)
214                    (setq fields (cons field fields))
215                    )
216                ))
217             (reverse field-list)
218             )
219     (set regexp-sym
220          (concat "^" (apply (function regexp-or) fields) ":"))
221     (set sym fields)
222     ))
223
224 (defun tm:delete-fields (sym field-list &optional regexp-sym)
225   (or regexp-sym
226       (setq regexp-sym
227             (let ((name (symbol-name sym)))
228               (intern
229                (concat (if (string-match "\\(.*\\)-list" name)
230                            (substring name 0 (match-end 1))
231                          name)
232                        "-regexp")
233                )))
234       )
235   (let ((fields (eval sym)))
236     (mapcar (function
237              (lambda (field)
238                (setq fields (delete field fields))
239                ))
240             field-list)
241     (set regexp-sym
242          (concat "^" (apply (function regexp-or) fields) ":"))
243     (set sym fields)
244     ))
245
246
247 ;;; @ RCS version
248 ;;;
249
250 (defsubst get-version-string (id)
251   "Return a version-string from RCS ID."
252   (and (string-match ",v \\([0-9][0-9.][0-9.]+\\)" id)
253        (substring id (match-beginning 1)(match-end 1))
254        ))
255
256
257 ;;; @ Other Utility
258 ;;;
259
260 (defsubst eliminate-top-spaces (string)
261   "Eliminate top sequence of space or tab in STRING."
262   (if (string-match "^[ \t]+" string)
263       (substring string (match-end 0))
264     string))
265
266 (defun call-after-loaded (module func &optional hook-name)
267   "If MODULE is provided, then FUNC is called.
268 Otherwise func is set to MODULE-load-hook.
269 If optional argument HOOK-NAME is specified,
270 it is used as hook to set."
271   (if (featurep module)
272       (funcall func)
273     (or hook-name
274         (setq hook-name (intern (concat (symbol-name module) "-load-hook")))
275         )
276     (add-hook hook-name func)
277     ))
278
279
280 ;;; @ end
281 ;;;
282
283 (provide 'mime-def)
284
285 ;;; mime-def.el ends here