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