Function caesar-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.49 1997-05-09 01:17:55 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 ;;; @ field
175 ;;;
176
177 (defsubst regexp-or (&rest args)
178   (concat "\\(" (mapconcat (function identity) args "\\|") "\\)"))
179
180 (defun tm:set-fields (sym field-list &optional regexp-sym)
181   (or regexp-sym
182       (setq regexp-sym
183             (let ((name (symbol-name sym)))
184               (intern
185                (concat (if (string-match "\\(.*\\)-list" name)
186                            (substring name 0 (match-end 1))
187                          name)
188                        "-regexp")
189                )))
190       )
191   (set sym field-list)
192   (set regexp-sym
193        (concat "^" (apply (function regexp-or) field-list) ":"))
194   )
195
196 (defun tm:add-fields (sym field-list &optional regexp-sym)
197   (or regexp-sym
198       (setq regexp-sym
199             (let ((name (symbol-name sym)))
200               (intern
201                (concat (if (string-match "\\(.*\\)-list" name)
202                            (substring name 0 (match-end 1))
203                          name)
204                        "-regexp")
205                )))
206       )
207   (let ((fields (eval sym)))
208     (mapcar (function
209              (lambda (field)
210                (or (member field fields)
211                    (setq fields (cons field fields))
212                    )
213                ))
214             (reverse field-list)
215             )
216     (set regexp-sym
217          (concat "^" (apply (function regexp-or) fields) ":"))
218     (set sym fields)
219     ))
220
221 (defun tm:delete-fields (sym field-list &optional regexp-sym)
222   (or regexp-sym
223       (setq regexp-sym
224             (let ((name (symbol-name sym)))
225               (intern
226                (concat (if (string-match "\\(.*\\)-list" name)
227                            (substring name 0 (match-end 1))
228                          name)
229                        "-regexp")
230                )))
231       )
232   (let ((fields (eval sym)))
233     (mapcar (function
234              (lambda (field)
235                (setq fields (delete field fields))
236                ))
237             field-list)
238     (set regexp-sym
239          (concat "^" (apply (function regexp-or) fields) ":"))
240     (set sym fields)
241     ))
242
243
244 ;;; @ RCS version
245 ;;;
246
247 (defsubst get-version-string (id)
248   "Return a version-string from RCS ID."
249   (and (string-match ",v \\([0-9][0-9.][0-9.]+\\)" id)
250        (substring id (match-beginning 1)(match-end 1))
251        ))
252
253
254 ;;; @ Other Utility
255 ;;;
256
257 (defsubst eliminate-top-spaces (string)
258   "Eliminate top sequence of space or tab in STRING."
259   (if (string-match "^[ \t]+" string)
260       (substring string (match-end 0))
261     string))
262
263 (defun call-after-loaded (module func &optional hook-name)
264   "If MODULE is provided, then FUNC is called.
265 Otherwise func is set to MODULE-load-hook.
266 If optional argument HOOK-NAME is specified,
267 it is used as hook to set."
268   (if (featurep module)
269       (funcall func)
270     (or hook-name
271         (setq hook-name (intern (concat (symbol-name module) "-load-hook")))
272         )
273     (add-hook hook-name func)
274     ))
275
276
277 ;;; @ end
278 ;;;
279
280 (provide 'mime-def)
281
282 ;;; mime-def.el ends here