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