update.
[elisp/semi.git] / semi-def.el
1 ;;; semi-def.el --- definition module for SEMI
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 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 'poe)
28 (eval-when-compile (require 'cl))
29 (require 'custom)
30
31 (defconst mime-user-interface-product ["EMY" (1 13 6) "Life is balance"]
32   "Product name, version number and code name of MIME-kernel package.")
33
34 (autoload 'mule-caesar-region "mule-caesar"
35   "Caesar rotation of current region." t)
36
37 (autoload 'widget-convert-button "wid-edit")
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 Variable `widget-button-face' is equivalent for it if widget is used."
55   :group 'mime
56   :type 'face)
57
58 (defcustom mime-button-mouse-face 'highlight
59   "Face used for MIME-preview buffer mouse highlighting.
60 Variable `widget-mouse-face' is equivalent for it if widget is used."
61   :group 'mime
62   :type 'face)
63
64 (defcustom mime-use-widget nil
65   "If t, use widget to display buttons."
66   :group 'mime
67   :type 'boolean)
68
69 (defun mime-create-widget-button (string function)
70   "Display STRING as a widget button with the callback FUNCTION.
71 Under XEmacs, the function `mime-create-xpm-button' might be identical
72 to the function `mime-create-widget-button' if the feature `xpm' is not
73 provided or the TTY frame is used."
74   (let ((start (point)))
75     (widget-create
76      'push-button
77      :action `(lambda (widget &optional event) (,function))
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      string)
89     ;; There may be only one string "*" behind the widget button.  We
90     ;; should replace it with the string as it can be seen because it
91     ;; will be yanked into the reply messages.
92     (static-when (featurep 'xemacs)
93       (let ((end (point))
94             extent)
95         (insert "[" string "]")
96         (while (setq extent (extent-at start nil nil extent))
97           ;; Avoid removing extents of next part.
98           (if (eq (extent-end-position extent) end)
99               (set-extent-endpoints extent end (point))))
100         (delete-region start end)))
101     (add-text-properties start (point)
102                          (list 'start-open t
103                                'mime-button t)))
104   (insert "\n"))
105
106 (static-when (featurep 'xemacs)
107   (defcustom mime-xpm-button-shadow-thickness 3
108     "A number of pixels should be used for the shadows on the edges of
109 the buttons."
110     :group 'mime
111     :type 'integer)
112
113   (defcustom mime-xpm-button-foreground "Yellow"
114     "A color used to display the text."
115     :group 'mime
116     :type 'string)
117
118   (defcustom mime-xpm-button-background "#a0a0d0"
119     "A background color the text will be displayed upon."
120     :group 'mime
121     :type 'string)
122
123   (defvar mime-xpm-button-glyph-cache nil)
124
125   (if (featurep 'xpm)
126       (defun mime-create-xpm-button (string function)
127         "Display STRING as a XPM button with the callback FUNCTION.
128 It might be identical to the function `mime-create-widget-button'
129 if the TTY frame is used."
130         ;; `device-on-widow-system-p' must be checked at run-time.
131         (if (device-on-window-system-p)
132             (progn
133               (set-extent-properties (make-extent (point)
134                                                   (progn
135                                                     (insert "[" string "]")
136                                                     (point)))
137                                      '(invisible t intangible t))
138               (let* ((spec (list string
139                                  mime-xpm-button-shadow-thickness
140                                  mime-xpm-button-foreground
141                                  mime-xpm-button-background))
142                      (button (cdr (assoc spec mime-xpm-button-glyph-cache))))
143                 (or button
144                     (set-alist 'mime-xpm-button-glyph-cache spec
145                                (setq button (apply (function xpm-button-create)
146                                                    spec))))
147                 (let* ((extent (make-extent (point) (point)))
148                        (down-glyph (make-glyph (car (cdr button))))
149                        (up-glyph (make-glyph (car button)))
150                        (down-func `(lambda (event)
151                                      (interactive "e")
152                                      (set-extent-begin-glyph ,extent
153                                                              ,down-glyph)))
154                        (up-func `(lambda (event)
155                                    (interactive "e")
156                                    (mouse-set-point event)
157                                    (set-extent-begin-glyph ,extent ,up-glyph)
158                                    (,function)))
159                        (keymap (make-sparse-keymap)))
160                   (define-key keymap 'button1 down-func)
161                   (define-key keymap 'button2 down-func)
162                   (define-key keymap 'button1up up-func)
163                   (define-key keymap 'button2up up-func)
164                   (set-extent-begin-glyph extent up-glyph)
165                   (set-extent-property extent 'keymap keymap))
166                 (insert "\n")))
167           (mime-create-widget-button string function)))
168     (fset 'mime-create-xpm-button 'mime-create-widget-button)))
169
170 (defcustom mime-create-button-function 'mime-create-widget-button
171   "A function called to create the content button."
172   :group 'mime
173   :type (list
174          'cons
175          :convert-widget
176          (function
177           (lambda (widget)
178             (list
179              'radio
180              :args
181              (append
182               '((const :tag "Widget button" mime-create-widget-button))
183               (static-when (featurep 'xemacs)
184                 '((const :tag "Xpm button" mime-create-xpm-button)))
185               '((function :tag "Other"))))))))
186
187 (defsubst mime-add-button (from to function &optional data)
188   "Create a button between FROM and TO with callback FUNCTION and DATA."
189   (and mime-button-face
190        (put-text-property from to 'face mime-button-face))
191   (and mime-button-mouse-face
192        (put-text-property from to 'mouse-face mime-button-mouse-face))
193   (add-text-properties from to (list 'mime-button-callback function
194                                      'start-open t
195                                      'mime-button t))
196   (and data
197        (add-text-properties from to (list 'mime-button-data data))))
198
199 (defsubst mime-insert-button (string function &optional data)
200   "Insert STRING as button with callback FUNCTION and DATA."
201   (unless (bolp)
202     (insert "\n"))
203   (save-restriction
204     (narrow-to-region (point) (point))
205     (if mime-use-widget
206         (mapcar (function
207                  (lambda (line)
208                    (funcall mime-create-button-function line function)))
209                 (split-string string "\n"))
210       (progn
211         ;; One more newline to avoid concatenation of face property.
212         (insert (concat "[" string "]\n\n"))
213         (mime-add-button (point-min) (1- (point-max)) function data)
214         (delete-char -1)))))
215
216 (defvar mime-button-mother-dispatcher nil)
217
218 (defun mime-button-dispatcher (event)
219   "Select the button under point."
220   (interactive "e")
221   (let (buf point func data)
222     (save-window-excursion
223       (mouse-set-point event)
224       (setq buf (current-buffer)
225             point (point)
226             func (get-text-property (point) 'mime-button-callback)
227             data (get-text-property (point) 'mime-button-data)))
228     (save-excursion
229       (set-buffer buf)
230       (goto-char point)
231       (if func
232           (apply func data)
233         (if (fboundp mime-button-mother-dispatcher)
234             (funcall mime-button-mother-dispatcher event))))))
235
236
237 ;;; @ for URL
238 ;;;
239
240 (defcustom mime-browse-url-regexp
241   (concat "\\(http\\|ftp\\|file\\|gopher\\|news\\|telnet\\|wais\\|mailto\\):"
242           "\\(//[-a-zA-Z0-9_.]+:[0-9]*\\)?"
243           "[-a-zA-Z0-9_=?#$@~`%&*+|\\/.,]*[-a-zA-Z0-9_=#$@~`%&*+|\\/]")
244   "*Regexp to match URL in text body."
245   :group 'mime
246   :type 'regexp)
247
248 (defcustom mime-browse-url-function (function browse-url)
249   "*Function to browse URL."
250   :group 'mime
251   :type 'function)
252
253 (defsubst mime-add-url-buttons ()
254   "Add URL-buttons for text body."
255   (goto-char (point-min))
256   (while (re-search-forward mime-browse-url-regexp nil t)
257     (let ((beg (match-beginning 0))
258           (end (match-end 0)))
259       (if mime-use-widget
260           (progn
261             (widget-convert-button 'mime-url-link beg end
262                                    (buffer-substring beg end))
263             (static-unless (featurep 'xemacs)
264               (overlay-put (make-overlay beg end) 'local-map widget-keymap)))
265         (mime-add-button beg end mime-browse-url-function
266                          (list (buffer-substring beg end)))))))
267
268 (define-widget 'mime-url-link 'link
269   "A link to an www page."
270   :help-echo 'widget-url-link-help-echo
271   :action 'widget-mime-url-link-action)
272
273 (defun widget-mime-url-link-action (widget &optional event)
274   "Open the url specified by WIDGET."
275   (funcall mime-browse-url-function (widget-value widget)))
276
277
278 ;;; @ menu
279 ;;;
280
281 (if window-system
282     (if (featurep 'xemacs)
283         (defun select-menu-alist (title menu-alist)
284           (let (ret)
285             (popup-menu
286              (list* title
287                     "---"
288                     (mapcar (function
289                              (lambda (cell)
290                                (vector (car cell)
291                                        `(progn
292                                           (setq ret ',(cdr cell))
293                                           (throw 'exit nil))
294                                        t)))
295                             menu-alist)))
296             (recursive-edit)
297             ret))
298       (defun select-menu-alist (title menu-alist)
299         (x-popup-menu
300          (list '(1 1) (selected-window))
301          (list title (cons title menu-alist)))))
302   (defun select-menu-alist (title menu-alist)
303     (cdr
304      (assoc (completing-read (concat title " : ") menu-alist)
305             menu-alist))))
306
307
308 ;;; @ Other Utility
309 ;;;
310
311 (defvar mime-condition-type-alist
312   '((preview . mime-preview-condition)
313     (action . mime-acting-condition)))
314
315 (defvar mime-condition-mode-alist
316   '((with-default . ctree-set-calist-with-default)
317     (t . ctree-set-calist-strictly)))
318
319 (defun mime-add-condition (target-type condition &optional mode file)
320   "Add CONDITION to database specified by TARGET-TYPE.
321 TARGET-TYPE must be 'preview or 'action.  
322 If optional argument MODE is 'strict or nil (omitted), CONDITION is
323 added strictly.
324 If optional argument MODE is 'with-default, CONDITION is added with
325 default rule.
326 If optional argument FILE is specified, it is loaded when CONDITION is
327 activate."
328   (let ((sym (cdr (assq target-type mime-condition-type-alist))))
329     (if sym
330         (let ((func (cdr (or (assq mode mime-condition-mode-alist)
331                              (assq t mime-condition-mode-alist)))))
332           (if (fboundp func)
333               (progn
334                 (funcall func sym condition)
335                 (if file
336                     (let ((method (cdr (assq 'method condition))))
337                       (autoload method file))))
338             (error "Function for mode `%s' is not found." mode)))
339       (error "Variable for target-type `%s' is not found." target-type))))
340
341
342 ;;; @ end
343 ;;;
344
345 (provide 'semi-def)
346
347 ;;; semi-def.el ends here