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