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