Synch with the semi-1_14 branch.
[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 ["REMI" (1 14 3) "Matsudai"]
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
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   :group 'mime
55   :type 'face)
56
57 (defcustom mime-button-mouse-face 'highlight
58   "Face used for MIME-preview buffer mouse highlighting."
59   :group 'mime
60   :type 'face)
61
62 (defsubst mime-add-button (from to function &optional data)
63   "Create a button between FROM and TO with callback FUNCTION and DATA."
64   (and mime-button-face
65        (put-text-property from to 'face mime-button-face))
66   (and mime-button-mouse-face
67        (put-text-property from to 'mouse-face mime-button-mouse-face))
68   (put-text-property from to 'mime-button-callback function)
69   (and data
70        (put-text-property from to 'mime-button-data data))
71   )
72
73 (defsubst mime-insert-button (string function &optional data)
74   "Insert STRING as button with callback FUNCTION and DATA."
75   (save-restriction
76     (narrow-to-region (point)(point))
77     (insert (concat "[" string "]\n"))
78     (mime-add-button (point-min)(point-max) function data)
79     ))
80
81 (defvar mime-button-mother-dispatcher nil)
82
83 (defun mime-button-dispatcher (event)
84   "Select the button under point."
85   (interactive "e")
86   (let (buf point func data)
87     (save-window-excursion
88       (mouse-set-point event)
89       (setq buf (current-buffer)
90             point (point)
91             func (get-text-property (point) 'mime-button-callback)
92             data (get-text-property (point) 'mime-button-data)
93             ))
94     (save-excursion
95       (set-buffer buf)
96       (goto-char point)
97       (if func
98           (apply func data)
99         (if (fboundp mime-button-mother-dispatcher)
100             (funcall mime-button-mother-dispatcher event)
101           )))))
102
103
104 ;;; @ for URL
105 ;;;
106
107 (defcustom mime-browse-url-regexp
108   (concat "\\(http\\|ftp\\|file\\|gopher\\|news\\|telnet\\|wais\\|mailto\\):"
109           "\\(//[-a-zA-Z0-9_.]+:[0-9]*\\)?"
110           "[-a-zA-Z0-9_=?#$@~`%&*+|\\/.,]*[-a-zA-Z0-9_=#$@~`%&*+|\\/]")
111   "*Regexp to match URL in text body."
112   :group 'mime
113   :type 'regexp)
114
115 (defcustom mime-browse-url-function (function browse-url)
116   "*Function to browse URL."
117   :group 'mime
118   :type 'function)
119
120 (defsubst mime-add-url-buttons ()
121   "Add URL-buttons for text body."
122   (goto-char (point-min))
123   (while (re-search-forward mime-browse-url-regexp nil t)
124     (let ((beg (match-beginning 0))
125           (end (match-end 0)))
126       (mime-add-button beg end mime-browse-url-function
127                        (list (buffer-substring beg end))))))
128
129
130 ;;; @ menu
131 ;;;
132
133 (if window-system
134     (if (featurep 'xemacs)
135         (defun select-menu-alist (title menu-alist)
136           (let (ret)
137             (popup-menu
138              (list* title
139                     "---"
140                     (mapcar (function
141                              (lambda (cell)
142                                (vector (car cell)
143                                        `(progn
144                                           (setq ret ',(cdr cell))
145                                           (throw 'exit nil)
146                                           )
147                                        t)
148                                ))
149                             menu-alist)
150                     ))
151             (recursive-edit)
152             ret))
153       (defun select-menu-alist (title menu-alist)
154         (x-popup-menu
155          (list '(1 1) (selected-window))
156          (list title (cons title menu-alist))
157          ))
158       )
159   (defun select-menu-alist (title menu-alist)
160     (cdr
161      (assoc (completing-read (concat title " : ") menu-alist)
162             menu-alist)
163      ))
164   )
165
166
167 ;;; @ Other Utility
168 ;;;
169
170 (defvar mime-condition-type-alist
171   '((preview . mime-preview-condition)
172     (action . mime-acting-condition)))
173
174 (defvar mime-condition-mode-alist
175   '((with-default . ctree-set-calist-with-default)
176     (t . ctree-set-calist-strictly)))
177
178 (defun mime-add-condition (target-type condition &optional mode file)
179   "Add CONDITION to database specified by TARGET-TYPE.
180 TARGET-TYPE must be 'preview or 'action.  
181 If optional argument MODE is 'strict or nil (omitted), CONDITION is
182 added strictly.
183 If optional argument MODE is 'with-default, CONDITION is added with
184 default rule.
185 If optional argument FILE is specified, it is loaded when CONDITION is
186 activate."
187   (let ((sym (cdr (assq target-type mime-condition-type-alist))))
188     (if sym
189         (let ((func (cdr (or (assq mode mime-condition-mode-alist)
190                              (assq t mime-condition-mode-alist)))))
191           (if (fboundp func)
192               (progn
193                 (funcall func sym condition)
194                 (if file
195                     (let ((method (cdr (assq 'method condition))))
196                       (autoload method file)
197                       ))
198                 )
199             (error "Function for mode `%s' is not found." mode)
200             ))
201       (error "Variable for target-type `%s' is not found." target-type)
202       )))
203
204
205 ;;; @ end
206 ;;;
207
208 (provide 'semi-def)
209
210 ;;; semi-def.el ends here