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