* mime-view.el (mime-view-xemacs-popup-menu): Abolish.
[elisp/semi.git] / semi-def.el
1 ;;; semi-def.el --- definition module for SEMI -*- coding: iso-8859-4; -*-
2
3 ;; Copyright (C) 1995,96,97,98,99,2000 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
29 (eval-when-compile (require 'cl))
30
31 (require 'custom)
32
33 (defconst mime-user-interface-product ["EMIKO" (1 14 0) "Zoomastigophora"]
34   "Product name, version number and code name of MIME-kernel package.")
35
36 (autoload 'mule-caesar-region "mule-caesar"
37   "Caesar rotation of current region." t)
38
39 (autoload 'widget-convert-button "wid-edit")
40
41 ;;; @ constants
42 ;;;
43
44 (defconst mime-echo-buffer-name "*MIME-echo*"
45   "Name of buffer to display MIME-playing information.")
46
47 (defconst mime-temp-buffer-name " *MIME-temp*")
48
49
50 ;;; @ button
51 ;;;
52
53 (define-widget 'mime-button 'link
54   "Widget for MIME button."
55   :action 'mime-button-action)
56
57 (defun mime-button-action (widget &optional event)
58   (let ((function (widget-get widget :mime-button-callback))
59         (data (widget-get widget :mime-button-data)))
60     (when function
61       (funcall function data))))
62     
63 (defsubst mime-insert-button (string function &optional data)
64   "Insert STRING as button with callback FUNCTION and DATA."
65   (save-restriction
66     (narrow-to-region (point)(point))
67     ;; Maybe we should introduce button formatter such as
68     ;; `gnus-mime-button-line-format'.
69     (insert "[" string "]")
70     ;; XEmacs -- when `widget-glyph-enable' is non nil, widget values are not
71     ;; guaranteed to be underlain.
72     (widget-convert-button 'mime-button (point-min)(point-max)
73                            :mime-button-callback function
74                            :mime-button-data data)
75     (insert "\n")))
76
77
78 ;;; @ for URL
79 ;;;
80
81 (defcustom mime-browse-url-regexp
82   (concat "\\(http\\|ftp\\|file\\|gopher\\|news\\|telnet\\|wais\\|mailto\\):"
83           "\\(//[-a-zA-Z0-9_.]+:[0-9]*\\)?"
84           "[-a-zA-Z0-9_=?#$@~`%&*+|\\/.,]*[-a-zA-Z0-9_=#$@~`%&*+|\\/]")
85   "Regexp to match URL in text body."
86   :group 'mime
87   :type 'regexp)
88
89 (defcustom mime-browse-url-function (function browse-url)
90   "Function to browse URL."
91   :group 'mime
92   :type 'function)
93
94 (define-widget 'mime-url-link 'url-link
95   "A link to an www page.")
96
97 (defsubst mime-add-url-buttons ()
98   "Add URL-buttons for text body."
99   (goto-char (point-min))
100   (while (re-search-forward mime-browse-url-regexp nil t)
101     (widget-convert-button 'mime-url-link (match-beginning 0)(match-end 0)
102                            (match-string-no-properties 0))))
103
104
105 ;;; @ menu
106 ;;;
107
108 (defun-maybe-cond select-menu-alist (title menu-alist)
109   ((fboundp 'popup-menu)
110    ;; While XEmacs can have both X and tty frames at the same time with
111    ;; gnuclient, we shouldn't emulate in text-mode here.
112    (let (ret)
113      (popup-menu
114       ;; list* is CL function, but CL is a part of XEmacs.
115       (list* title
116              "---"
117              (mapcar
118               (lambda (cell)
119                 (vector (car cell)
120                         `(progn
121                            (setq ret ',(cdr cell))
122                            (throw 'exit nil))
123                         t)))
124              menu-alist))
125      (recursive-edit)
126      ret))
127   (window-system
128    (x-popup-menu t (list title (cons title menu-alist)))))
129
130 (defmacro mime-menu-bogus-filter-constructor (name menu)
131   `(let (x y)
132      (setq x (x-popup-menu t ,menu)
133            y (and x (lookup-key ,menu (apply #'vector x))))
134      (if (and x y)
135          (funcall y))))
136
137 (defmacro mime-menu-popup (event menu)
138   (if (fboundp 'popup-menu)
139       `(popup-menu ,menu)
140     ;; #### Kludge for GNU Emacs 20.7 or earlier.
141     `(let (bogus-menu)
142        (easy-menu-define bogus-menu nil nil ,menu)
143        (mime-menu-bogus-filter-constructor "Popup" bogus-menu))))
144
145
146 ;;; @ Other Utility
147 ;;;
148
149 (defvar mime-condition-type-alist
150   '((preview . mime-preview-condition)
151     (action . mime-acting-condition)))
152
153 (defvar mime-condition-mode-alist
154   '((with-default . ctree-set-calist-with-default)
155     (t . ctree-set-calist-strictly)))
156
157 (defun mime-add-condition (target-type condition &optional mode file)
158   "Add CONDITION to database specified by TARGET-TYPE.
159 TARGET-TYPE must be 'preview or 'action.  
160 If optional argument MODE is 'strict or nil (omitted), CONDITION is
161 added strictly.
162 If optional argument MODE is 'with-default, CONDITION is added with
163 default rule.
164 If optional argument FILE is specified, it is loaded when CONDITION is
165 activate."
166   (let ((sym (cdr (assq target-type mime-condition-type-alist))))
167     (if sym
168         (let ((func (cdr (or (assq mode mime-condition-mode-alist)
169                              (assq t mime-condition-mode-alist)))))
170           (if (fboundp func)
171               (progn
172                 (funcall func sym condition)
173                 (if file
174                     (let ((method (cdr (assq 'method condition))))
175                       (autoload method file))))
176             (error "Function for mode `%s' is not found." mode)))
177       (error "Variable for target-type `%s' is not found." target-type))))
178
179
180 ;;; @ end
181 ;;;
182
183 (provide 'semi-def)
184
185 ;;; semi-def.el ends here