(butlast): New function; imported from cl.el.
[elisp/semi.git] / mime-def.el
1 ;;; mime-def.el --- definition module for SEMI
2
3 ;; Copyright (C) 1995,1996,1997 Free Software Foundation, Inc.
4
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; Version: $Id: mime-def.el,v 0.65 1997-09-25 16:36:10 morioka Exp $
7 ;; Keywords: definition, MIME, multimedia, mail, news
8
9 ;; This file is part of SEMI (SEMI is Emacs MIME Interfaces).
10
11 ;; This program is free software; you can redistribute it and/or
12 ;; modify it under the terms of the GNU General Public License as
13 ;; published by the Free Software Foundation; either version 2, or (at
14 ;; your option) any later version.
15
16 ;; This program is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19 ;; General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Code:
27
28 (require 'emu)
29
30 (defun-maybe butlast (x &optional n)
31   "Returns a copy of LIST with the last N elements removed."
32   (if (and n (<= n 0)) x
33     (nbutlast (copy-sequence x) n)))
34
35 (defun-maybe nbutlast (x &optional n)
36   "Modifies LIST to remove the last N elements."
37   (let ((m (length x)))
38     (or n (setq n 1))
39     (and (< n m)
40          (progn
41            (if (> n 0) (setcdr (nthcdr (- (1- m) n) x) nil))
42            x))))
43
44 (defconst semi-version-name "Hinomiko")
45
46 (autoload 'mule-caesar-region "mule-caesar"
47   "Caesar rotation of current region." t)
48
49
50 ;;; @ variables
51 ;;;
52
53 (defvar mime/use-multi-frame
54   (and (>= emacs-major-version 19) window-system))
55
56 (defvar mime/find-file-function
57   (if mime/use-multi-frame
58       (function find-file-other-frame)
59     (function find-file)
60     ))
61
62
63 ;;; @ constants
64 ;;;
65
66 (defconst mime-echo-buffer-name "*MIME-echo*"
67   "Name of buffer to display MIME-playing information.")
68
69 (defconst mime-temp-buffer-name " *MIME-temp*")
70
71
72 ;;; @ definitions about MIME
73 ;;;
74
75 (defconst mime-tspecials "][()<>@,\;:\\\"/?=")
76 (defconst mime-token-regexp (concat "[^" mime-tspecials "\000-\040]+"))
77 (defconst mime-charset-regexp mime-token-regexp)
78
79 (defconst mime-media-type/subtype-regexp
80   (concat mime-token-regexp "/" mime-token-regexp))
81
82
83 ;;; @ button
84 ;;;
85
86 (defvar mime-button-face 'bold
87   "Face used for content-button or URL-button of MIME-Preview buffer.")
88
89 (defvar mime-button-mouse-face 'highlight
90   "Face used for MIME-preview buffer mouse highlighting.")
91
92 (defsubst mime-add-button (from to function &optional data)
93   "Create a button between FROM and TO with callback FUNCTION and DATA."
94   (let ((overlay (make-overlay from to)))
95     (and mime-button-face
96          (overlay-put overlay 'face mime-button-face))
97     (and mime-button-mouse-face
98          (overlay-put overlay 'mouse-face mime-button-mouse-face))
99     (add-text-properties from to (list 'mime-button-callback function))
100     (and data
101          (add-text-properties from to (list 'mime-button-data data)))
102     ;;(add-text-properties from to (list 'keymap widget-keymap))
103     ))
104
105 (defsubst mime-insert-button (string function &optional data)
106   "Insert STRING as button with callback FUNCTION and DATA."
107   (save-restriction
108     (narrow-to-region (point)(point))
109     (insert (concat "[" string "]"))
110     ;; (widget-push-button-value-create
111     ;;  (widget-convert 'push-button
112     ;;                  :notify (lambda (&rest ignore)
113     ;;                            (mime-view-play-current-entity)
114     ;;                            )
115     ;;                  string))
116     (insert "\n")
117     (mime-add-button (point-min)(point-max) function data)
118     ))
119
120 (defvar mime-button-mother-dispatcher nil)
121
122 (defun mime-button-dispatcher (event)
123   "Select the button under point."
124   (interactive "e")
125   (let (buf point func data)
126     (save-window-excursion
127       (mouse-set-point event)
128       (setq buf (current-buffer)
129             point (point)
130             func (get-text-property (point) 'mime-button-callback)
131             data (get-text-property (point) 'mime-button-data)
132             )
133       )
134     (save-excursion
135       (set-buffer buf)
136       (goto-char point)
137       (if func
138           (apply func data)
139         (if (fboundp mime-button-mother-dispatcher)
140             (funcall mime-button-mother-dispatcher event)
141           )
142         ))))
143
144
145 ;;; @ PGP
146 ;;;
147
148 (defvar pgp-function-alist
149   '(
150     ;; for mime-pgp
151     (verify             mc-verify                       "mc-toplev")
152     (decrypt            mc-decrypt                      "mc-toplev")
153     (fetch-key          mc-pgp-fetch-key                "mc-pgp")
154     (snarf-keys         mc-snarf-keys                   "mc-toplev")
155     ;; for mime-edit
156     (mime-sign          mime-mc-pgp-sign-region         "mime-mc")
157     (traditional-sign   mc-pgp-sign-region              "mc-pgp")
158     (encrypt            mime-mc-pgp-encrypt-region      "mime-mc")
159     (insert-key         mc-insert-public-key            "mc-toplev")
160     )
161   "Alist of service names vs. corresponding functions and its filenames.
162 Each element looks like (SERVICE FUNCTION FILE).
163
164 SERVICE is a symbol of PGP processing.  It allows `verify', `decrypt',
165 `fetch-key', `snarf-keys', `mime-sign', `traditional-sign', `encrypt'
166 or `insert-key'.
167
168 Function is a symbol of function to do specified SERVICE.
169
170 FILE is string of filename which has definition of corresponding
171 FUNCTION.")
172
173 (defmacro pgp-function (method)
174   "Return function to do service METHOD."
175   (` (car (cdr (assq (, method) (symbol-value 'pgp-function-alist)))))
176   )
177
178 (mapcar (function
179          (lambda (method)
180            (autoload (second method)(third method))
181            ))
182         pgp-function-alist)
183
184
185 ;;; @ method selector kernel
186 ;;;
187
188 (require 'atype)
189
190 ;;; @@ field unifier
191 ;;;
192
193 (defun field-unifier-for-mode (a b)
194   (let ((va (cdr a)))
195     (if (if (consp va)
196             (member (cdr b) va)
197           (equal va (cdr b))
198           )
199         (list nil b nil)
200       )))
201
202
203 ;;; @ field
204 ;;;
205
206 (defsubst regexp-or (&rest args)
207   (concat "\\(" (mapconcat (function identity) args "\\|") "\\)"))
208
209 (defun tm:set-fields (sym field-list &optional regexp-sym)
210   (or regexp-sym
211       (setq regexp-sym
212             (let ((name (symbol-name sym)))
213               (intern
214                (concat (if (string-match "\\(.*\\)-list" name)
215                            (substring name 0 (match-end 1))
216                          name)
217                        "-regexp")
218                )))
219       )
220   (set sym field-list)
221   (set regexp-sym
222        (concat "^" (apply (function regexp-or) field-list) ":"))
223   )
224
225 (defun tm:add-fields (sym field-list &optional regexp-sym)
226   (or regexp-sym
227       (setq regexp-sym
228             (let ((name (symbol-name sym)))
229               (intern
230                (concat (if (string-match "\\(.*\\)-list" name)
231                            (substring name 0 (match-end 1))
232                          name)
233                        "-regexp")
234                )))
235       )
236   (let ((fields (eval sym)))
237     (mapcar (function
238              (lambda (field)
239                (or (member field fields)
240                    (setq fields (cons field fields))
241                    )
242                ))
243             (reverse field-list)
244             )
245     (set regexp-sym
246          (concat "^" (apply (function regexp-or) fields) ":"))
247     (set sym fields)
248     ))
249
250 (defun tm:delete-fields (sym field-list &optional regexp-sym)
251   (or regexp-sym
252       (setq regexp-sym
253             (let ((name (symbol-name sym)))
254               (intern
255                (concat (if (string-match "\\(.*\\)-list" name)
256                            (substring name 0 (match-end 1))
257                          name)
258                        "-regexp")
259                )))
260       )
261   (let ((fields (eval sym)))
262     (mapcar (function
263              (lambda (field)
264                (setq fields (delete field fields))
265                ))
266             field-list)
267     (set regexp-sym
268          (concat "^" (apply (function regexp-or) fields) ":"))
269     (set sym fields)
270     ))
271
272
273 ;;; @ RCS version
274 ;;;
275
276 (defsubst get-version-string (id)
277   "Return a version-string from RCS ID."
278   (and (string-match ",v \\([0-9][0-9.][0-9.]+\\)" id)
279        (substring id (match-beginning 1)(match-end 1))
280        ))
281
282
283 ;;; @ Other Utility
284 ;;;
285
286 (defsubst eliminate-top-spaces (string)
287   "Eliminate top sequence of space or tab in STRING."
288   (if (string-match "^[ \t]+" string)
289       (substring string (match-end 0))
290     string))
291
292 (defun call-after-loaded (module func &optional hook-name)
293   "If MODULE is provided, then FUNC is called.
294 Otherwise func is set to MODULE-load-hook.
295 If optional argument HOOK-NAME is specified,
296 it is used as hook to set."
297   (if (featurep module)
298       (funcall func)
299     (or hook-name
300         (setq hook-name (intern (concat (symbol-name module) "-load-hook")))
301         )
302     (add-hook hook-name func)
303     ))
304
305
306 ;;; @ end
307 ;;;
308
309 (provide 'mime-def)
310
311 ;;; mime-def.el ends here