update.
[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 (defmacro mime-popup-menu-bogus-filter-constructor (menu)
109   `(let (x y)
110      (setq x (x-popup-menu t ,menu)
111            y (and x (lookup-key ,menu (apply #'vector x))))
112      (if (and x y)
113          (funcall y))))
114
115 ;;; While XEmacs can have both X and tty frames at the same time with
116 ;;; gnuclient, we shouldn't emulate in text-mode here.
117
118 (static-if (featurep 'xemacs)
119     (defalias 'mime-popup-menu-popup 'popup-menu)
120   (defun mime-popup-menu-popup (menu &optional event)
121     (let (bogus-menu)
122       ;; #### Kludge for FSF Emacs-style menu.
123       (easy-menu-define bogus-menu nil nil menu)
124       (mime-popup-menu-bogus-filter-constructor bogus-menu))))
125
126 (static-if (featurep 'xemacs)
127     (defun mime-popup-menu-select (menu &optional event)
128       (let ((selection (get-popup-menu-response menu event)))
129         (event-object selection)))
130   (defun mime-popup-menu-select (menu &optional event)
131     (let (bogus-menu)
132       ;; #### Kludge for FSF Emacs-style menu.
133       (easy-menu-define bogus-menu nil nil menu)
134       (x-popup-menu t bogus-menu))))
135
136
137 ;;; @ Other Utility
138 ;;;
139
140 (defvar mime-condition-type-alist
141   '((preview . mime-preview-condition)
142     (action . mime-acting-condition)))
143
144 (defvar mime-condition-mode-alist
145   '((with-default . ctree-set-calist-with-default)
146     (t . ctree-set-calist-strictly)))
147
148 (defun mime-add-condition (target-type condition &optional mode file)
149   "Add CONDITION to database specified by TARGET-TYPE.
150 TARGET-TYPE must be 'preview or 'action.  
151 If optional argument MODE is 'strict or nil (omitted), CONDITION is
152 added strictly.
153 If optional argument MODE is 'with-default, CONDITION is added with
154 default rule.
155 If optional argument FILE is specified, it is loaded when CONDITION is
156 activate."
157   (let ((sym (cdr (assq target-type mime-condition-type-alist))))
158     (if sym
159         (let ((func (cdr (or (assq mode mime-condition-mode-alist)
160                              (assq t mime-condition-mode-alist)))))
161           (if (fboundp func)
162               (progn
163                 (funcall func sym condition)
164                 (if file
165                     (let ((method (cdr (assq 'method condition))))
166                       (autoload method file))))
167             (error "Function for mode `%s' is not found." mode)))
168       (error "Variable for target-type `%s' is not found." target-type))))
169
170
171 ;;; @ end
172 ;;;
173
174 (provide 'semi-def)
175
176 ;;; semi-def.el ends here