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