c852586f4a4321a172ca4c69c4bd8173d44bcff9
[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 (defconst mime-module-version '("WEMI" "Shinagawa" 1 2 2)
30   "Implementation name, version name and numbers of MIME-kernel package.")
31
32 (autoload 'mule-caesar-region "mule-caesar"
33   "Caesar rotation of current region." t)
34
35
36 ;;; @ variables
37 ;;;
38
39 (defvar mime/use-multi-frame
40   (and (>= emacs-major-version 19) window-system))
41
42 (defvar mime/find-file-function
43   (if mime/use-multi-frame
44       (function find-file-other-frame)
45     (function find-file)
46     ))
47
48
49 ;;; @ constants
50 ;;;
51
52 (defconst mime-echo-buffer-name "*MIME-echo*"
53   "Name of buffer to display MIME-playing information.")
54
55 (defconst mime-temp-buffer-name " *MIME-temp*")
56
57
58 ;;; @ button
59 ;;;
60
61 (defcustom mime-button-face 'bold
62   "Face used for content-button or URL-button of MIME-Preview buffer."
63   :group 'mime
64   :type 'face)
65
66 (defcustom mime-button-mouse-face 'highlight
67   "Face used for MIME-preview buffer mouse highlighting."
68   :group 'mime
69   :type 'face)
70
71 (defsubst mime-insert-button (string function &optional data)
72   "Insert STRING as button with callback FUNCTION and DATA."
73   (save-restriction
74     (narrow-to-region (point)(point))
75     (widget-create 'push-button
76                    :action `(lambda (widget &optional event)
77                               (,function)
78                               )
79                    :mouse-down-action `(lambda (widget event)
80                                          (let (buf point)
81                                            (save-window-excursion
82                                              (mouse-set-point event)
83                                              (setq buf (current-buffer)
84                                                    point (point)))
85                                            (save-excursion
86                                              (set-buffer buf)
87                                              (goto-char point)
88                                              (,function)
89                                              )))
90                    string)
91     (insert "\n")
92     ;;(mime-add-button (point-min)(point-max) function data)
93     ))
94
95 (defvar mime-button-mother-dispatcher nil)
96
97 (defun mime-button-dispatcher (event)
98   "Select the button under point."
99   (interactive "e")
100   (let (buf point func data)
101     (save-window-excursion
102       (mouse-set-point event)
103       (setq buf (current-buffer)
104             point (point)
105             func (get-text-property (point) 'mime-button-callback)
106             data (get-text-property (point) 'mime-button-data)
107             )
108       )
109     (save-excursion
110       (set-buffer buf)
111       (goto-char point)
112       (if func
113           (apply func data)
114         (if (fboundp mime-button-mother-dispatcher)
115             (funcall mime-button-mother-dispatcher event)
116           )
117         ))))
118
119
120 ;;; @ PGP
121 ;;;
122
123 (defvar pgp-function-alist
124   '(
125     ;; for mime-pgp
126     (verify             mc-verify                       "mc-toplev")
127     (decrypt            mc-decrypt                      "mc-toplev")
128     (fetch-key          mc-pgp-fetch-key                "mc-pgp")
129     (snarf-keys         mc-snarf-keys                   "mc-toplev")
130     ;; for mime-edit
131     (mime-sign          mime-mc-pgp-sign-region         "mime-mc")
132     (traditional-sign   mc-pgp-sign-region              "mc-pgp")
133     (encrypt            mime-mc-pgp-encrypt-region      "mime-mc")
134     (insert-key         mc-insert-public-key            "mc-toplev")
135     )
136   "Alist of service names vs. corresponding functions and its filenames.
137 Each element looks like (SERVICE FUNCTION FILE).
138
139 SERVICE is a symbol of PGP processing.  It allows `verify', `decrypt',
140 `fetch-key', `snarf-keys', `mime-sign', `traditional-sign', `encrypt'
141 or `insert-key'.
142
143 Function is a symbol of function to do specified SERVICE.
144
145 FILE is string of filename which has definition of corresponding
146 FUNCTION.")
147
148 (defmacro pgp-function (method)
149   "Return function to do service METHOD."
150   `(cadr (assq ,method (symbol-value 'pgp-function-alist)))
151   )
152
153 (mapcar (function
154          (lambda (method)
155            (autoload (cadr method)(nth 2 method))
156            ))
157         pgp-function-alist)
158
159
160 ;;; @ method selector kernel
161 ;;;
162
163 (require 'atype)
164
165 ;;; @@ field unifier
166 ;;;
167
168 (defun field-unifier-for-mode (a b)
169   (let ((va (cdr a)))
170     (if (if (consp va)
171             (member (cdr b) va)
172           (equal va (cdr b))
173           )
174         (list nil b nil)
175       )))
176
177
178 ;;; @ field
179 ;;;
180
181 (defun tm:set-fields (sym field-list &optional regexp-sym)
182   (or regexp-sym
183       (setq regexp-sym
184             (let ((name (symbol-name sym)))
185               (intern
186                (concat (if (string-match "\\(.*\\)-list" name)
187                            (substring name 0 (match-end 1))
188                          name)
189                        "-regexp")
190                )))
191       )
192   (set sym field-list)
193   (set regexp-sym
194        (concat "^" (apply (function regexp-or) field-list) ":"))
195   )
196
197 (defun tm:add-fields (sym field-list &optional regexp-sym)
198   (or regexp-sym
199       (setq regexp-sym
200             (let ((name (symbol-name sym)))
201               (intern
202                (concat (if (string-match "\\(.*\\)-list" name)
203                            (substring name 0 (match-end 1))
204                          name)
205                        "-regexp")
206                )))
207       )
208   (let ((fields (eval sym)))
209     (mapcar (function
210              (lambda (field)
211                (or (member field fields)
212                    (setq fields (cons field fields))
213                    )
214                ))
215             (reverse field-list)
216             )
217     (set regexp-sym
218          (concat "^" (apply (function regexp-or) fields) ":"))
219     (set sym fields)
220     ))
221
222 (defun tm:delete-fields (sym field-list &optional regexp-sym)
223   (or regexp-sym
224       (setq regexp-sym
225             (let ((name (symbol-name sym)))
226               (intern
227                (concat (if (string-match "\\(.*\\)-list" name)
228                            (substring name 0 (match-end 1))
229                          name)
230                        "-regexp")
231                )))
232       )
233   (let ((fields (eval sym)))
234     (mapcar (function
235              (lambda (field)
236                (setq fields (delete field fields))
237                ))
238             field-list)
239     (set regexp-sym
240          (concat "^" (apply (function regexp-or) fields) ":"))
241     (set sym fields)
242     ))
243
244
245 ;;; @ RCS version
246 ;;;
247
248 (defsubst get-version-string (id)
249   "Return a version-string from RCS ID."
250   (and (string-match ",v \\([0-9][0-9.][0-9.]+\\)" id)
251        (substring id (match-beginning 1)(match-end 1))
252        ))
253
254
255 ;;; @ Other Utility
256 ;;;
257
258 (defun call-after-loaded (module func &optional hook-name)
259   "If MODULE is provided, then FUNC is called.
260 Otherwise func is set to MODULE-load-hook.
261 If optional argument HOOK-NAME is specified,
262 it is used as hook to set."
263   (if (featurep module)
264       (funcall func)
265     (or hook-name
266         (setq hook-name (intern (concat (symbol-name module) "-load-hook")))
267         )
268     (add-hook hook-name func)
269     ))
270
271
272 ;;; @ end
273 ;;;
274
275 (provide 'semi-def)
276
277 ;;; semi-def.el ends here