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