61ef4d7f77ce731d620572ee5a2698941afda97a
[elisp/semi.git] / semi-def.el
1 ;;; semi-def.el --- definition module for WEMI
2
3 ;; Copyright (C) 1995,1996,1997,1998,1999 Free Software Foundation, Inc.
4
5 ;; Author: MORIOKA Tomohiko <tomo@m17n.org>
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 'poe)
28
29 (eval-when-compile (require 'cl))
30
31 (require 'custom)
32
33 (require 'widget)
34
35 (defconst mime-user-interface-product ["WEMI" (1 13 5) "Fijieda"]
36   "Product name, version number and code name of MIME-kernel package.")
37
38 (autoload 'mule-caesar-region "mule-caesar"
39   "Caesar rotation of current region." t)
40
41 (autoload 'widget-convert-button "wid-edit")
42
43
44 ;;; @ constants
45 ;;;
46
47 (defconst mime-echo-buffer-name "*MIME-echo*"
48   "Name of buffer to display MIME-playing information.")
49
50 (defconst mime-temp-buffer-name " *MIME-temp*")
51
52
53 ;;; @ button
54 ;;;
55
56 (defcustom mime-button-face 'bold
57   "Face used for content-button or URL-button of MIME-Preview buffer."
58   :group 'mime
59   :type 'face)
60
61 (defcustom mime-button-mouse-face 'highlight
62   "Face used for MIME-preview buffer mouse highlighting."
63   :group 'mime
64   :type 'face)
65
66 (defsubst mime-insert-button (string function &optional data)
67   "Insert STRING as button with callback FUNCTION and DATA."
68   (save-restriction
69     (narrow-to-region (point)(point))
70     (mapcar #'(lambda (line)
71                 (let ((start (point))
72                       end extent)
73                   (widget-create
74                    'push-button
75                    :action `(lambda (widget &optional event)
76                               (,function)
77                               )
78                    :mouse-down-action `(lambda (widget event)
79                                          (let (buf point)
80                                            (save-window-excursion
81                                              (mouse-set-point event)
82                                              (setq buf (current-buffer)
83                                                    point (point)))
84                                            (save-excursion
85                                              (set-buffer buf)
86                                              (goto-char point)
87                                              (,function)
88                                              )))
89                    line)
90                   (if (featurep 'xemacs)
91                       (progn
92                         (setq end (point))
93                         (insert "[" line "]")
94                         (while (setq extent (extent-at start nil nil extent))
95                           (set-extent-endpoints extent end (point)))
96                         (delete-region start end)))
97                   (insert "\n")))
98             (split-string string "\n"))))
99
100 (defvar mime-button-mother-dispatcher nil)
101
102 (defun mime-button-dispatcher (event)
103   "Select the button under point."
104   (interactive "e")
105   (let (buf point func data)
106     (save-window-excursion
107       (mouse-set-point event)
108       (setq buf (current-buffer)
109             point (point)
110             func (get-text-property (point) 'mime-button-callback)
111             data (get-text-property (point) 'mime-button-data)
112             ))
113     (save-excursion
114       (set-buffer buf)
115       (goto-char point)
116       (if func
117           (apply func data)
118         (if (fboundp mime-button-mother-dispatcher)
119             (funcall mime-button-mother-dispatcher event)
120           )))))
121
122
123 ;;; @ for URL
124 ;;;
125
126 (defcustom mime-browse-url-regexp
127   (concat "\\(http\\|ftp\\|file\\|gopher\\|news\\|telnet\\|wais\\|mailto\\):"
128           "\\(//[-a-zA-Z0-9_.]+:[0-9]*\\)?"
129           "[-a-zA-Z0-9_=?#$@~`%&*+|\\/.,]*[-a-zA-Z0-9_=#$@~`%&*+|\\/]")
130   "*Regexp to match URL in text body."
131   :group 'mime
132   :type 'regexp)
133
134 (defcustom mime-browse-url-function (function browse-url)
135   "*Function to browse URL."
136   :group 'mime
137   :type 'function)
138
139 (defsubst mime-add-url-buttons ()
140   "Add URL-buttons for text body."
141   (goto-char (point-min))
142   (while (re-search-forward mime-browse-url-regexp nil t)
143     (let ((beg (match-beginning 0))
144           (end (match-end 0)))
145       (widget-convert-button 'mime-url-link beg end
146                              (buffer-substring beg end))
147       )))
148
149 (define-widget 'mime-url-link 'link
150   "A link to an www page."
151   :help-echo 'widget-url-link-help-echo
152   :action 'widget-mime-url-link-action)
153
154 (defun widget-mime-url-link-action (widget &optional event)
155   "Open the url specified by WIDGET."
156   (funcall mime-browse-url-function (widget-value widget)))
157
158
159 ;;; @ menu
160 ;;;
161
162 (if window-system
163     (if (featurep 'xemacs)
164         (defun select-menu-alist (title menu-alist)
165           (let (ret)
166             (popup-menu
167              (list* title
168                     "---"
169                     (mapcar (function
170                              (lambda (cell)
171                                (vector (car cell)
172                                        `(progn
173                                           (setq ret ',(cdr cell))
174                                           (throw 'exit nil)
175                                           )
176                                        t)
177                                ))
178                             menu-alist)
179                     ))
180             (recursive-edit)
181             ret))
182       (defun select-menu-alist (title menu-alist)
183         (x-popup-menu
184          (list '(1 1) (selected-window))
185          (list title (cons title menu-alist))
186          ))
187       )
188   (defun select-menu-alist (title menu-alist)
189     (cdr
190      (assoc (completing-read (concat title " : ") menu-alist)
191             menu-alist)
192      ))
193   )
194
195
196 ;;; @ PGP
197 ;;;
198
199 (defvar pgp-function-alist
200   '(
201     ;; for mime-pgp
202     (verify             mc-verify                       "mc-toplev")
203     (decrypt            mc-decrypt                      "mc-toplev")
204     (fetch-key          mc-pgp-fetch-key                "mc-pgp")
205     (snarf-keys         mc-snarf-keys                   "mc-toplev")
206     ;; for mime-edit
207     (mime-sign          mime-mc-pgp-sign-region         "mime-mc")
208     (traditional-sign   mc-pgp-sign-region              "mc-pgp")
209     (encrypt            mime-mc-pgp-encrypt-region      "mime-mc")
210     (insert-key         mc-insert-public-key            "mc-toplev")
211     )
212   "Alist of service names vs. corresponding functions and its filenames.
213 Each element looks like (SERVICE FUNCTION FILE).
214
215 SERVICE is a symbol of PGP processing.  It allows `verify', `decrypt',
216 `fetch-key', `snarf-keys', `mime-sign', `traditional-sign', `encrypt'
217 or `insert-key'.
218
219 Function is a symbol of function to do specified SERVICE.
220
221 FILE is string of filename which has definition of corresponding
222 FUNCTION.")
223
224 (defmacro pgp-function (method)
225   "Return function to do service METHOD."
226   `(cadr (assq ,method (symbol-value 'pgp-function-alist))))
227
228 (mapcar (function
229          (lambda (method)
230            (autoload (cadr method)(nth 2 method))
231            ))
232         pgp-function-alist)
233
234
235 ;;; @ Other Utility
236 ;;;
237
238 (defvar mime-condition-type-alist
239   '((preview . mime-preview-condition)
240     (action . mime-acting-condition)))
241
242 (defvar mime-condition-mode-alist
243   '((with-default . ctree-set-calist-with-default)
244     (t . ctree-set-calist-strictly)))
245
246 (defun mime-add-condition (target-type condition &optional mode file)
247   "Add CONDITION to database specified by TARGET-TYPE.
248 TARGET-TYPE must be 'preview or 'action.  
249 If optional argument MODE is 'strict or nil (omitted), CONDITION is
250 added strictly.
251 If optional argument MODE is 'with-default, CONDITION is added with
252 default rule.
253 If optional argument FILE is specified, it is loaded when CONDITION is
254 activate."
255   (let ((sym (cdr (assq target-type mime-condition-type-alist))))
256     (if sym
257         (let ((func (cdr (or (assq mode mime-condition-mode-alist)
258                              (assq t mime-condition-mode-alist)))))
259           (if (fboundp func)
260               (progn
261                 (funcall func sym condition)
262                 (if file
263                     (let ((method (cdr (assq 'method condition))))
264                       (autoload method file)
265                       ))
266                 )
267             (error "Function for mode `%s' is not found." mode)
268             ))
269       (error "Variable for target-type `%s' is not found." target-type)
270       )))
271
272
273 ;;; @ end
274 ;;;
275
276 (provide 'semi-def)
277
278 ;;; semi-def.el ends here