(mime-condition-type-alist): New variable.
[elisp/semi.git] / semi-def.el
1 ;;; semi-def.el --- definition module for SEMI
2
3 ;; Copyright (C) 1995,1996,1997,1998 Free Software Foundation, Inc.
4
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; Keywords: definition, MIME, multimedia, mail, news
7
8 ;; This file is part of SEMI (Spadework for Emacs MIME Interfaces).
9
10 ;; This program is free software; you can redistribute it and/or
11 ;; modify it under the terms of the GNU General Public License as
12 ;; published by the Free Software Foundation; either version 2, or (at
13 ;; your option) any later version.
14
15 ;; This program is distributed in the hope that it will be useful, but
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 ;; General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Code:
26
27 (require 'emu)
28
29 (eval-when-compile (require 'cl))
30
31
32 (defconst mime-module-version '("SEMI" "Itoigawa" 1 3 5)
33   "Implementation name, version name and numbers of MIME-kernel package.")
34
35 (autoload 'mule-caesar-region "mule-caesar"
36   "Caesar rotation of current region." t)
37
38
39 ;;; @ variables
40 ;;;
41
42 (defvar mime/use-multi-frame
43   (and (>= emacs-major-version 19) window-system))
44
45 (defvar mime/find-file-function
46   (if mime/use-multi-frame
47       (function find-file-other-frame)
48     (function find-file)
49     ))
50
51
52 ;;; @ constants
53 ;;;
54
55 (defconst mime-echo-buffer-name "*MIME-echo*"
56   "Name of buffer to display MIME-playing information.")
57
58 (defconst mime-temp-buffer-name " *MIME-temp*")
59
60
61 ;;; @ button
62 ;;;
63
64 (defcustom mime-button-face 'bold
65   "Face used for content-button or URL-button of MIME-Preview buffer."
66   :group 'mime
67   :type 'face)
68
69 (defcustom mime-button-mouse-face 'highlight
70   "Face used for MIME-preview buffer mouse highlighting."
71   :group 'mime
72   :type 'face)
73
74 (defsubst mime-add-button (from to function &optional data)
75   "Create a button between FROM and TO with callback FUNCTION and DATA."
76   (let ((overlay (make-overlay from to)))
77     (and mime-button-face
78          (overlay-put overlay 'face mime-button-face))
79     (and mime-button-mouse-face
80          (overlay-put overlay 'mouse-face mime-button-mouse-face))
81     (add-text-properties from to (list 'mime-button-callback function))
82     (and data
83          (add-text-properties from to (list 'mime-button-data data)))
84     ;;(add-text-properties from to (list 'keymap widget-keymap))
85     ))
86
87 (defsubst mime-insert-button (string function &optional data)
88   "Insert STRING as button with callback FUNCTION and DATA."
89   (save-restriction
90     (narrow-to-region (point)(point))
91     (insert (concat "[" string "]"))
92     ;; (widget-push-button-value-create
93     ;;  (widget-convert 'push-button
94     ;;                  :notify (lambda (&rest ignore)
95     ;;                            (mime-preview-play-current-entity)
96     ;;                            )
97     ;;                  string))
98     (insert "\n")
99     (mime-add-button (point-min)(point-max) function data)
100     ))
101
102 (defvar mime-button-mother-dispatcher nil)
103
104 (defun mime-button-dispatcher (event)
105   "Select the button under point."
106   (interactive "e")
107   (let (buf point func data)
108     (save-window-excursion
109       (mouse-set-point event)
110       (setq buf (current-buffer)
111             point (point)
112             func (get-text-property (point) 'mime-button-callback)
113             data (get-text-property (point) 'mime-button-data)
114             )
115       )
116     (save-excursion
117       (set-buffer buf)
118       (goto-char point)
119       (if func
120           (apply func data)
121         (if (fboundp mime-button-mother-dispatcher)
122             (funcall mime-button-mother-dispatcher event)
123           )
124         ))))
125
126
127 ;;; @ menu
128 ;;;
129
130 (if window-system
131     (if (featurep 'xemacs)
132         (defun select-menu-alist (title menu-alist)
133           (let (ret)
134             (popup-menu
135              (list* title
136                     "---"
137                     (mapcar (function
138                              (lambda (cell)
139                                (vector (car cell)
140                                        `(progn
141                                           (setq ret ',(cdr cell))
142                                           (throw 'exit nil)
143                                           )
144                                        t)
145                                ))
146                             menu-alist)
147                     ))
148             (recursive-edit)
149             ret))
150       (defun select-menu-alist (title menu-alist)
151         (x-popup-menu
152          (list '(1 1) (selected-window))
153          (list title (cons title menu-alist))
154          ))
155       )
156   (defun select-menu-alist (title menu-alist)
157     (cdr
158      (assoc (completing-read (concat title " : ") menu-alist)
159             menu-alist)
160      ))
161   )
162
163
164 ;;; @ PGP
165 ;;;
166
167 (defvar pgp-function-alist
168   '(
169     ;; for mime-pgp
170     (verify             mc-verify                       "mc-toplev")
171     (decrypt            mc-decrypt                      "mc-toplev")
172     (fetch-key          mc-pgp-fetch-key                "mc-pgp")
173     (snarf-keys         mc-snarf-keys                   "mc-toplev")
174     ;; for mime-edit
175     (mime-sign          mime-mc-pgp-sign-region         "mime-mc")
176     (traditional-sign   mc-pgp-sign-region              "mc-pgp")
177     (encrypt            mime-mc-pgp-encrypt-region      "mime-mc")
178     (insert-key         mc-insert-public-key            "mc-toplev")
179     )
180   "Alist of service names vs. corresponding functions and its filenames.
181 Each element looks like (SERVICE FUNCTION FILE).
182
183 SERVICE is a symbol of PGP processing.  It allows `verify', `decrypt',
184 `fetch-key', `snarf-keys', `mime-sign', `traditional-sign', `encrypt'
185 or `insert-key'.
186
187 Function is a symbol of function to do specified SERVICE.
188
189 FILE is string of filename which has definition of corresponding
190 FUNCTION.")
191
192 (defmacro pgp-function (method)
193   "Return function to do service METHOD."
194   `(cadr (assq ,method (symbol-value 'pgp-function-alist)))
195   )
196
197 (mapcar (function
198          (lambda (method)
199            (autoload (cadr method)(nth 2 method))
200            ))
201         pgp-function-alist)
202
203
204 ;;; @ field
205 ;;;
206
207 (defun tm:set-fields (sym field-list &optional regexp-sym)
208   (or regexp-sym
209       (setq regexp-sym
210             (let ((name (symbol-name sym)))
211               (intern
212                (concat (if (string-match "\\(.*\\)-list" name)
213                            (substring name 0 (match-end 1))
214                          name)
215                        "-regexp")
216                )))
217       )
218   (set sym field-list)
219   (set regexp-sym
220        (concat "^" (apply (function regexp-or) field-list) ":"))
221   )
222
223 (defun tm:add-fields (sym field-list &optional regexp-sym)
224   (or regexp-sym
225       (setq regexp-sym
226             (let ((name (symbol-name sym)))
227               (intern
228                (concat (if (string-match "\\(.*\\)-list" name)
229                            (substring name 0 (match-end 1))
230                          name)
231                        "-regexp")
232                )))
233       )
234   (let ((fields (eval sym)))
235     (mapcar (function
236              (lambda (field)
237                (or (member field fields)
238                    (setq fields (cons field fields))
239                    )
240                ))
241             (reverse field-list)
242             )
243     (set regexp-sym
244          (concat "^" (apply (function regexp-or) fields) ":"))
245     (set sym fields)
246     ))
247
248 (defun tm:delete-fields (sym field-list &optional regexp-sym)
249   (or regexp-sym
250       (setq regexp-sym
251             (let ((name (symbol-name sym)))
252               (intern
253                (concat (if (string-match "\\(.*\\)-list" name)
254                            (substring name 0 (match-end 1))
255                          name)
256                        "-regexp")
257                )))
258       )
259   (let ((fields (eval sym)))
260     (mapcar (function
261              (lambda (field)
262                (setq fields (delete field fields))
263                ))
264             field-list)
265     (set regexp-sym
266          (concat "^" (apply (function regexp-or) fields) ":"))
267     (set sym fields)
268     ))
269
270
271 ;;; @ RCS version
272 ;;;
273
274 (defsubst get-version-string (id)
275   "Return a version-string from RCS ID."
276   (and (string-match ",v \\([0-9][0-9.][0-9.]+\\)" id)
277        (substring id (match-beginning 1)(match-end 1))
278        ))
279
280
281 ;;; @ Other Utility
282 ;;;
283
284 (defun call-after-loaded (module func &optional hook-name)
285   "If MODULE is provided, then FUNC is called.
286 Otherwise func is set to MODULE-load-hook.
287 If optional argument HOOK-NAME is specified,
288 it is used as hook to set."
289   (if (featurep module)
290       (funcall func)
291     (or hook-name
292         (setq hook-name (intern (concat (symbol-name module) "-load-hook")))
293         )
294     (add-hook hook-name func)
295     ))
296
297
298 (defvar mime-condition-type-alist
299   '((preview . mime-preview-condition)
300     (action . mime-acting-condition)))
301
302 (defvar mime-condition-mode-alist
303   '((with-default . ctree-set-calist-with-default)
304     (t . ctree-set-calist-strictly)))
305
306 (defun mime-add-condition (target-type condition &optional mode file)
307   "Add CONDITION to database specified by TARGET-TYPE.
308 TARGET-TYPE must be 'preview or 'action.  
309 If optional argument MODE is 'strict or nil (omitted), CONDITION is
310 added strictly.
311 If optional argument MODE is 'with-default, CONDITION is added with
312 default rule.
313 If optional argument FILE is specified, it is loaded when CONDITION is
314 activate."
315   (let ((sym (cdr (assq target-type mime-condition-type-alist))))
316     (if sym
317         (let ((func (cdr (assq mode mime-condition-mode-alist))))
318           (if (fboundp func)
319               (progn
320                 (funcall func sym condition)
321                 (if file
322                     (let ((method (cdr (assq 'method condition))))
323                       (autoload method file)
324                       ))
325                 )
326             (error "Function for mode `%s' is not found." mode)
327             ))
328       (error "Variable for target-type `%s' is not found." target-type)
329       )))
330
331
332 ;;; @ end
333 ;;;
334
335 (provide 'semi-def)
336
337 ;;; semi-def.el ends here