Variable `mime/output-buffer-window-is-shared-with-bbdb' was moved to
[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.46 1997-03-27 20:46:40 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 'cl)
29 (require 'emu)
30
31
32 ;;; @ variables
33 ;;;
34
35 (defvar mime/use-multi-frame
36   (and (>= emacs-major-version 19) window-system))
37
38 (defvar mime/find-file-function
39   (if mime/use-multi-frame
40       (function find-file-other-frame)
41     (function find-file)
42     ))
43
44
45 ;;; @ constants
46 ;;;
47
48 (defconst mime-echo-buffer-name "*MIME-echo*"
49   "Name of buffer to display MIME-playing information.")
50
51 (defconst mime/temp-buffer-name " *MIME-temp*")
52
53
54 ;;; @ definitions about MIME
55 ;;;
56
57 (defconst mime/tspecials "][\000-\040()<>@,\;:\\\"/?.=")
58 (defconst mime/token-regexp (concat "[^" mime/tspecials "]+"))
59 (defconst mime-charset-regexp mime/token-regexp)
60
61 (defconst mime/content-type-subtype-regexp
62   (concat mime/token-regexp "/" mime/token-regexp))
63
64 (defconst mime/disposition-type-regexp mime/token-regexp)
65
66
67 ;;; @ button
68 ;;;
69
70 (if running-xemacs
71     (require 'overlay)
72   )
73
74 (defvar mime-button-face 'bold
75   "Face used for content-button or URL-button of MIME-Preview buffer.")
76
77 (defvar mime-button-mouse-face 'highlight
78   "Face used for MIME-preview buffer mouse highlighting.")
79
80 (defsubst mime-add-button (from to func &optional data)
81   "Create a button between FROM and TO with callback FUNC and data DATA."
82   (and mime-button-face
83        (overlay-put (make-overlay from to) 'face mime-button-face))
84   (let ((props (cons 'mime-button-callback
85                      (cons func
86                            (if data
87                                (list 'mime-button-data data)
88                              )))))
89     (if mime-button-mouse-face
90         (setq props (cons 'mouse-face (cons mime-button-mouse-face props)))
91       )
92     (add-text-properties from to props)
93     ))
94
95 (defvar mime-button-mother-dispatcher nil)
96
97 (defun mime-button-dispatcher (event)
98   "Select the button under point."
99   (interactive "e")
100   (let (buf point func data)
101     (save-window-excursion
102       (mouse-set-point event)
103       (setq buf (current-buffer)
104             point (point)
105             func (get-text-property (point) 'mime-button-callback)
106             data (get-text-property (point) 'mime-button-data)
107             )
108       )
109     (save-excursion
110       (set-buffer buf)
111       (goto-char point)
112       (if func
113           (apply func data)
114         (if (fboundp mime-button-mother-dispatcher)
115             (funcall mime-button-mother-dispatcher event)
116           )
117         ))))
118
119
120 ;;; @ PGP
121 ;;;
122
123 (defvar pgp-function-alist
124   '(
125     ;; for mime-pgp
126     (verify             mc-verify                       "mc-toplev")
127     (decrypt            mc-decrypt                      "mc-toplev")
128     (fetch-key          mc-pgp-fetch-key                "mc-pgp")
129     (snarf-keys         mc-snarf-keys                   "mc-toplev")
130     ;; for mime-edit
131     (mime-sign          mime-mc-pgp-sign-region         "mime-mc")
132     (traditional-sign   mc-pgp-sign-region              "mc-pgp")
133     (encrypt            mime-mc-pgp-encrypt-region      "mime-mc")
134     (insert-key         mc-insert-public-key            "mc-toplev")
135     )
136   "Alist of service names vs. corresponding functions and its filenames.
137 Each element looks like (SERVICE FUNCTION FILE).
138
139 SERVICE is a symbol of PGP processing.  It allows `verify', `decrypt',
140 `fetch-key', `snarf-keys', `mime-sign', `traditional-sign', `encrypt'
141 or `insert-key'.
142
143 Function is a symbol of function to do specified SERVICE.
144
145 FILE is string of filename which has definition of corresponding
146 FUNCTION.")
147
148 (defmacro pgp-function (method)
149   "Return function to do service METHOD."
150   (` (car (cdr (assq (, method) (symbol-value 'pgp-function-alist)))))
151   )
152
153 (mapcar (function
154          (lambda (method)
155            (autoload (second method)(third method))
156            ))
157         pgp-function-alist)
158
159
160 ;;; @ method selector kernel
161 ;;;
162
163 (require 'atype)
164
165 ;;; @@ field unifier
166 ;;;
167
168 (defun field-unifier-for-mode (a b)
169   (let ((va (cdr a)))
170     (if (if (consp va)
171             (member (cdr b) va)
172           (equal va (cdr b))
173           )
174         (list nil b nil)
175       )))
176
177
178 ;;; @ rot13-47
179 ;;;
180 ;; caesar-region written by phr@prep.ai.mit.edu  Nov 86
181 ;; modified by tower@prep Nov 86
182 ;; gnus-caesar-region
183 ;; Modified by umerin@flab.flab.Fujitsu.JUNET for ROT47.
184 (defun tm:caesar-region (&optional n)
185   "Caesar rotation of region by N, default 13, for decrypting netnews.
186 ROT47 will be performed for Japanese text in any case."
187   (interactive (if current-prefix-arg   ; Was there a prefix arg?
188                    (list (prefix-numeric-value current-prefix-arg))
189                  (list nil)))
190   (cond ((not (numberp n)) (setq n 13))
191         (t (setq n (mod n 26))))        ;canonicalize N
192   (if (not (zerop n))           ; no action needed for a rot of 0
193       (progn
194         (if (or (not (boundp 'caesar-translate-table))
195                 (/= (aref caesar-translate-table ?a) (+ ?a n)))
196             (let ((i 0) (lower "abcdefghijklmnopqrstuvwxyz") upper)
197               (message "Building caesar-translate-table...")
198               (setq caesar-translate-table (make-vector 256 0))
199               (while (< i 256)
200                 (aset caesar-translate-table i i)
201                 (setq i (1+ i)))
202               (setq lower (concat lower lower) upper (upcase lower) i 0)
203               (while (< i 26)
204                 (aset caesar-translate-table (+ ?a i) (aref lower (+ i n)))
205                 (aset caesar-translate-table (+ ?A i) (aref upper (+ i n)))
206                 (setq i (1+ i)))
207               ;; ROT47 for Japanese text.
208               ;; Thanks to ichikawa@flab.fujitsu.junet.
209               (setq i 161)
210               (let ((t1 (logior ?O 128))
211                     (t2 (logior ?! 128))
212                     (t3 (logior ?~ 128)))
213                 (while (< i 256)
214                   (aset caesar-translate-table i
215                         (let ((v (aref caesar-translate-table i)))
216                           (if (<= v t1) (if (< v t2) v (+ v 47))
217                             (if (<= v t3) (- v 47) v))))
218                   (setq i (1+ i))))
219               (message "Building caesar-translate-table...done")))
220         (let ((from (region-beginning))
221               (to (region-end))
222               (i 0) str len)
223           (setq str (buffer-substring from to))
224           (setq len (length str))
225           (while (< i len)
226             (aset str i (aref caesar-translate-table (aref str i)))
227             (setq i (1+ i)))
228           (goto-char from)
229           (delete-region from to)
230           (insert str)))))
231
232
233 ;;; @ field
234 ;;;
235
236 (defsubst regexp-or (&rest args)
237   (concat "\\(" (mapconcat (function identity) args "\\|") "\\)"))
238
239 (defun tm:set-fields (sym field-list &optional regexp-sym)
240   (or regexp-sym
241       (setq regexp-sym
242             (let ((name (symbol-name sym)))
243               (intern
244                (concat (if (string-match "\\(.*\\)-list" name)
245                            (substring name 0 (match-end 1))
246                          name)
247                        "-regexp")
248                )))
249       )
250   (set sym field-list)
251   (set regexp-sym
252        (concat "^" (apply (function regexp-or) field-list) ":"))
253   )
254
255 (defun tm:add-fields (sym field-list &optional regexp-sym)
256   (or regexp-sym
257       (setq regexp-sym
258             (let ((name (symbol-name sym)))
259               (intern
260                (concat (if (string-match "\\(.*\\)-list" name)
261                            (substring name 0 (match-end 1))
262                          name)
263                        "-regexp")
264                )))
265       )
266   (let ((fields (eval sym)))
267     (mapcar (function
268              (lambda (field)
269                (or (member field fields)
270                    (setq fields (cons field fields))
271                    )
272                ))
273             (reverse field-list)
274             )
275     (set regexp-sym
276          (concat "^" (apply (function regexp-or) fields) ":"))
277     (set sym fields)
278     ))
279
280 (defun tm:delete-fields (sym field-list &optional regexp-sym)
281   (or regexp-sym
282       (setq regexp-sym
283             (let ((name (symbol-name sym)))
284               (intern
285                (concat (if (string-match "\\(.*\\)-list" name)
286                            (substring name 0 (match-end 1))
287                          name)
288                        "-regexp")
289                )))
290       )
291   (let ((fields (eval sym)))
292     (mapcar (function
293              (lambda (field)
294                (setq fields (delete field fields))
295                ))
296             field-list)
297     (set regexp-sym
298          (concat "^" (apply (function regexp-or) fields) ":"))
299     (set sym fields)
300     ))
301
302
303 ;;; @ RCS version
304 ;;;
305
306 (defsubst get-version-string (id)
307   "Return a version-string from RCS ID."
308   (and (string-match ",v \\([0-9][0-9.][0-9.]+\\)" id)
309        (substring id (match-beginning 1)(match-end 1))
310        ))
311
312
313 ;;; @ Other Utility
314 ;;;
315
316 (defsubst eliminate-top-spaces (string)
317   "Eliminate top sequence of space or tab in STRING."
318   (if (string-match "^[ \t]+" string)
319       (substring string (match-end 0))
320     string))
321
322 (defun call-after-loaded (module func &optional hook-name)
323   "If MODULE is provided, then FUNC is called.
324 Otherwise func is set to MODULE-load-hook.
325 If optional argument HOOK-NAME is specified,
326 it is used as hook to set."
327   (if (featurep module)
328       (funcall func)
329     (or hook-name
330         (setq hook-name (intern (concat (symbol-name module) "-load-hook")))
331         )
332     (add-hook hook-name func)
333     ))
334
335
336 ;;; @ end
337 ;;;
338
339 (provide 'mime-def)
340
341 ;;; mime-def.el ends here