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