fe52207e175c93fb5fc73440d3b3076a11c196b0
[elisp/semi.git] / semi-def.el
1 ;;; semi-def.el --- definition module for REMI
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 (Sample of Emacs MIME Implementation).
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" "Namerikawa" 1 5 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 ;;; @ constants
40 ;;;
41
42 (defconst mime-echo-buffer-name "*MIME-echo*"
43   "Name of buffer to display MIME-playing information.")
44
45 (defconst mime-temp-buffer-name " *MIME-temp*")
46
47
48 ;;; @ button
49 ;;;
50
51 (defcustom mime-button-face 'bold
52   "Face used for content-button or URL-button of MIME-Preview buffer."
53   :group 'mime
54   :type 'face)
55
56 (defcustom mime-button-mouse-face 'highlight
57   "Face used for MIME-preview buffer mouse highlighting."
58   :group 'mime
59   :type 'face)
60
61 (defsubst mime-add-button (from to function &optional data)
62   "Create a button between FROM and TO with callback FUNCTION and DATA."
63   (let ((overlay (make-overlay from to)))
64     (and mime-button-face
65          (overlay-put overlay 'face mime-button-face))
66     (and mime-button-mouse-face
67          (overlay-put overlay 'mouse-face mime-button-mouse-face))
68     (add-text-properties from to (list 'mime-button-callback function))
69     (and data
70          (add-text-properties from to (list 'mime-button-data data)))
71     ))
72
73 (defsubst mime-insert-button (string function &optional data)
74   "Insert STRING as button with callback FUNCTION and DATA."
75   (save-restriction
76     (narrow-to-region (point)(point))
77     (insert (concat "[" string "]\n"))
78     (mime-add-button (point-min)(point-max) function data)
79     ))
80
81 (defvar mime-button-mother-dispatcher nil)
82
83 (defun mime-button-dispatcher (event)
84   "Select the button under point."
85   (interactive "e")
86   (let (buf point func data)
87     (save-window-excursion
88       (mouse-set-point event)
89       (setq buf (current-buffer)
90             point (point)
91             func (get-text-property (point) 'mime-button-callback)
92             data (get-text-property (point) 'mime-button-data)
93             ))
94     (save-excursion
95       (set-buffer buf)
96       (goto-char point)
97       (if func
98           (apply func data)
99         (if (fboundp mime-button-mother-dispatcher)
100             (funcall mime-button-mother-dispatcher event)
101           )
102         ))))
103
104
105 ;;; @ menu
106 ;;;
107
108 (if window-system
109     (if (featurep 'xemacs)
110         (defun select-menu-alist (title menu-alist)
111           (let (ret)
112             (popup-menu
113              (list* title
114                     "---"
115                     (mapcar (function
116                              (lambda (cell)
117                                (vector (car cell)
118                                        `(progn
119                                           (setq ret ',(cdr cell))
120                                           (throw 'exit nil)
121                                           )
122                                        t)
123                                ))
124                             menu-alist)
125                     ))
126             (recursive-edit)
127             ret))
128       (defun select-menu-alist (title menu-alist)
129         (x-popup-menu
130          (list '(1 1) (selected-window))
131          (list title (cons title menu-alist))
132          ))
133       )
134   (defun select-menu-alist (title menu-alist)
135     (cdr
136      (assoc (completing-read (concat title " : ") menu-alist)
137             menu-alist)
138      ))
139   )
140
141
142 ;;; @ PGP
143 ;;;
144
145 (defvar pgp-function-alist
146   '(
147     ;; for mime-pgp
148     (verify             mc-verify                       "mc-toplev")
149     (decrypt            mc-decrypt                      "mc-toplev")
150     (fetch-key          mc-pgp-fetch-key                "mc-pgp")
151     (snarf-keys         mc-snarf-keys                   "mc-toplev")
152     ;; for mime-edit
153     (mime-sign          mime-mc-pgp-sign-region         "mime-mc")
154     (traditional-sign   mc-pgp-sign-region              "mc-pgp")
155     (encrypt            mime-mc-pgp-encrypt-region      "mime-mc")
156     (insert-key         mc-insert-public-key            "mc-toplev")
157     )
158   "Alist of service names vs. corresponding functions and its filenames.
159 Each element looks like (SERVICE FUNCTION FILE).
160
161 SERVICE is a symbol of PGP processing.  It allows `verify', `decrypt',
162 `fetch-key', `snarf-keys', `mime-sign', `traditional-sign', `encrypt'
163 or `insert-key'.
164
165 Function is a symbol of function to do specified SERVICE.
166
167 FILE is string of filename which has definition of corresponding
168 FUNCTION.")
169
170 (defmacro pgp-function (method)
171   "Return function to do service METHOD."
172   `(cadr (assq ,method (symbol-value 'pgp-function-alist))))
173
174 (mapcar (function
175          (lambda (method)
176            (autoload (cadr method)(nth 2 method))
177            ))
178         pgp-function-alist)
179
180
181 ;;; @ field
182 ;;;
183
184 (defun tm:set-fields (sym field-list &optional regexp-sym)
185   (or regexp-sym
186       (setq regexp-sym
187             (let ((name (symbol-name sym)))
188               (intern
189                (concat (if (string-match "\\(.*\\)-list" name)
190                            (substring name 0 (match-end 1))
191                          name)
192                        "-regexp")
193                )))
194       )
195   (set sym field-list)
196   (set regexp-sym
197        (concat "^" (apply (function regexp-or) field-list) ":"))
198   )
199
200 (defun tm:add-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   (let ((fields (eval sym)))
212     (mapcar (function
213              (lambda (field)
214                (or (member field fields)
215                    (setq fields (cons field fields))
216                    )
217                ))
218             (reverse field-list)
219             )
220     (set regexp-sym
221          (concat "^" (apply (function regexp-or) fields) ":"))
222     (set sym fields)
223     ))
224
225 (defun tm:delete-fields (sym field-list &optional regexp-sym)
226   (or regexp-sym
227       (setq regexp-sym
228             (let ((name (symbol-name sym)))
229               (intern
230                (concat (if (string-match "\\(.*\\)-list" name)
231                            (substring name 0 (match-end 1))
232                          name)
233                        "-regexp")
234                )))
235       )
236   (let ((fields (eval sym)))
237     (mapcar (function
238              (lambda (field)
239                (setq fields (delete field fields))
240                ))
241             field-list)
242     (set regexp-sym
243          (concat "^" (apply (function regexp-or) fields) ":"))
244     (set sym fields)
245     ))
246
247
248 ;;; @ Other Utility
249 ;;;
250
251 (defun call-after-loaded (module func &optional hook-name)
252   "If MODULE is provided, then FUNC is called.
253 Otherwise func is set to MODULE-load-hook.
254 If optional argument HOOK-NAME is specified,
255 it is used as hook to set."
256   (if (featurep module)
257       (funcall func)
258     (or hook-name
259         (setq hook-name (intern (concat (symbol-name module) "-load-hook")))
260         )
261     (add-hook hook-name func)
262     ))
263
264
265 (defvar mime-condition-type-alist
266   '((preview . mime-preview-condition)
267     (action . mime-acting-condition)))
268
269 (defvar mime-condition-mode-alist
270   '((with-default . ctree-set-calist-with-default)
271     (t . ctree-set-calist-strictly)))
272
273 (defun mime-add-condition (target-type condition &optional mode file)
274   "Add CONDITION to database specified by TARGET-TYPE.
275 TARGET-TYPE must be 'preview or 'action.  
276 If optional argument MODE is 'strict or nil (omitted), CONDITION is
277 added strictly.
278 If optional argument MODE is 'with-default, CONDITION is added with
279 default rule.
280 If optional argument FILE is specified, it is loaded when CONDITION is
281 activate."
282   (let ((sym (cdr (assq target-type mime-condition-type-alist))))
283     (if sym
284         (let ((func (cdr (or (assq mode mime-condition-mode-alist)
285                              (assq t mime-condition-mode-alist)))))
286           (if (fboundp func)
287               (progn
288                 (funcall func sym condition)
289                 (if file
290                     (let ((method (cdr (assq 'method condition))))
291                       (autoload method file)
292                       ))
293                 )
294             (error "Function for mode `%s' is not found." mode)
295             ))
296       (error "Variable for target-type `%s' is not found." target-type)
297       )))
298
299
300 ;;; @ end
301 ;;;
302
303 (provide 'semi-def)
304
305 ;;; semi-def.el ends here