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