fixed DOC string.
[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.29 1997-02-27 08:58:13 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 ;;; @ variables
29 ;;;
30
31 (defvar mime/tmp-dir (or (getenv "TM_TMP_DIR") "/tmp/"))
32
33 (defvar mime/use-multi-frame
34   (and (>= emacs-major-version 19) window-system))
35
36 (defvar mime/find-file-function
37   (if mime/use-multi-frame
38       (function find-file-other-frame)
39     (function find-file)
40     ))
41
42 (defvar mime/output-buffer-window-is-shared-with-bbdb t
43   "*If t, mime/output-buffer window is shared with BBDB window.")
44
45
46 ;;; @ constants
47 ;;;
48
49 (defconst mime/output-buffer-name "*MIME-out*")
50 (defconst mime/temp-buffer-name " *MIME-temp*")
51
52
53 ;;; @ definitions about MIME
54 ;;;
55
56 (defconst mime/tspecials "][\000-\040()<>@,\;:\\\"/?.=")
57 (defconst mime/token-regexp (concat "[^" mime/tspecials "]+"))
58 (defconst mime-charset-regexp mime/token-regexp)
59
60 (defconst mime/content-type-subtype-regexp
61   (concat mime/token-regexp "/" mime/token-regexp))
62
63 (defconst mime/disposition-type-regexp mime/token-regexp)
64
65
66 ;;; @ MIME charset
67 ;;;
68
69 (defvar charsets-mime-charset-alist
70   '(((ascii)                                            . us-ascii)
71     ((ascii latin-iso8859-1)                            . iso-8859-1)
72     ((ascii latin-iso8859-2)                            . iso-8859-2)
73     ((ascii latin-iso8859-3)                            . iso-8859-3)
74     ((ascii latin-iso8859-4)                            . iso-8859-4)
75 ;;; ((ascii cyrillic-iso8859-5)                         . iso-8859-5)
76     ((ascii cyrillic-iso8859-5)                         . koi8-r)
77     ((ascii arabic-iso8859-6)                           . iso-8859-6)
78     ((ascii greek-iso8859-7)                            . iso-8859-7)
79     ((ascii hebrew-iso8859-8)                           . iso-8859-8)
80     ((ascii latin-iso8859-9)                            . iso-8859-9)
81     ((ascii latin-jisx0201
82             japanese-jisx0208-1978 japanese-jisx0208)   . iso-2022-jp)
83     ((ascii korean-ksc5601)                             . euc-kr)
84     ((ascii chinese-gb2312)                             . cn-gb-2312)
85     ((ascii chinese-big5-1 chinese-big5-2)              . cn-big5)
86     ((ascii latin-iso8859-1 greek-iso8859-7
87             latin-jisx0201 japanese-jisx0208-1978
88             chinese-gb2312 japanese-jisx0208
89             korean-ksc5601 japanese-jisx0212)           . iso-2022-jp-2)
90     ((ascii latin-iso8859-1 greek-iso8859-7
91             latin-jisx0201 japanese-jisx0208-1978
92             chinese-gb2312 japanese-jisx0208
93             korean-ksc5601 japanese-jisx0212
94             chinese-cns11643-1 chinese-cns11643-2)      . iso-2022-int-1)
95     ((ascii latin-iso8859-1 latin-iso8859-2
96             cyrillic-iso8859-5 greek-iso8859-7
97             latin-jisx0201 japanese-jisx0208-1978
98             chinese-gb2312 japanese-jisx0208
99             korean-ksc5601 japanese-jisx0212
100             chinese-cns11643-1 chinese-cns11643-2
101             chinese-cns11643-3 chinese-cns11643-4
102             chinese-cns11643-5 chinese-cns11643-6
103             chinese-cns11643-7)                         . iso-2022-cjk)
104     ))
105
106 (defvar default-mime-charset 'x-ctext)
107
108 (defvar mime-charset-coding-system-alist
109   '((x-ctext            . ctext)
110     (gb2312             . cn-gb-2312)
111     (iso-2022-jp-2      . iso-2022-ss2-7)
112     ))
113
114 (defun mime-charset-to-coding-system (charset &optional lbt)
115   (if (stringp charset)
116       (setq charset (intern (downcase charset)))
117     )
118   (let ((cs
119          (or (cdr (assq charset mime-charset-coding-system-alist))
120              (and (coding-system-p charset) charset)
121              )))
122     (if lbt
123         (intern (concat (symbol-name cs) "-" (symbol-name lbt)))
124       cs)))
125
126 (defun detect-mime-charset-region (start end)
127   "Return MIME charset for region between START and END."
128   (charsets-to-mime-charset
129    (find-charset-string (buffer-substring start end))
130    ))
131
132 (defun encode-mime-charset-region (start end charset)
133   "Encode the text between START and END as MIME CHARSET."
134   (let ((cs (mime-charset-to-coding-system charset)))
135     (if cs
136         (encode-coding-region start end cs)
137       )))
138
139 (defun decode-mime-charset-region (start end charset)
140   "Decode the text between START and END as MIME CHARSET."
141   (let ((cs (mime-charset-to-coding-system charset)))
142     (if cs
143         (decode-coding-region start end cs)
144       )))
145
146 (defun encode-mime-charset-string (string charset)
147   "Encode the STRING as MIME CHARSET."
148   (let ((cs (mime-charset-to-coding-system charset)))
149     (if cs
150         (encode-coding-string string cs)
151       string)))
152
153 (defun decode-mime-charset-string (string charset)
154   "Decode the STRING as MIME CHARSET."
155   (let ((cs (mime-charset-to-coding-system charset)))
156     (if cs
157         (decode-coding-string string cs)
158       string)))
159
160
161 ;;; @ button
162 ;;;
163
164 (defvar running-xemacs (string-match "XEmacs" emacs-version))
165
166 (if running-xemacs
167     (require 'overlay)
168   )
169
170 (defvar mime-button-face 'bold
171   "Face used for content-button or URL-button of MIME-Preview buffer.")
172
173 (defvar mime-button-mouse-face 'highlight
174   "Face used for MIME-preview buffer mouse highlighting.")
175
176 (defun mime-add-button (from to func &optional data)
177   "Create a button between FROM and TO with callback FUNC and data DATA."
178   (and mime-button-face
179        (overlay-put (make-overlay from to) 'face mime-button-face))
180   (add-text-properties from to
181                        (nconc
182                         (and mime-button-mouse-face
183                              (list 'mouse-face mime-button-mouse-face))
184                         (list 'mime-button-callback func)
185                         (and data (list 'mime-button-data data))
186                         ))
187   )
188
189 (defvar mime-button-mother-dispatcher nil)
190
191 (defun mime-button-dispatcher (event)
192   "Select the button under point."
193   (interactive "e")
194   (let (buf point func data)
195     (save-window-excursion
196       (mouse-set-point event)
197       (setq buf (current-buffer)
198             point (point)
199             func (get-text-property (point) 'mime-button-callback)
200             data (get-text-property (point) 'mime-button-data)
201             )
202       )
203     (save-excursion
204       (set-buffer buf)
205       (goto-char point)
206       (if func
207           (apply func data)
208         (if (fboundp mime-button-mother-dispatcher)
209             (funcall mime-button-mother-dispatcher event)
210           )
211         ))))
212
213
214 ;;; @ PGP
215 ;;;
216
217 (defvar pgp-function-alist
218   '(
219     ;; for mime-pgp
220     (verify             mc-verify                       "mc-toplev")
221     (decrypt            mc-decrypt                      "mc-toplev")
222     (fetch-key          mc-pgp-fetch-key                "mc-pgp")
223     (snarf-keys         mc-snarf-keys                   "mc-toplev")
224     ;; for mime-edit
225     (mime-sign          tm:mc-pgp-sign-region           "mime-mc")
226     (traditional-sign   mc-pgp-sign-region              "mc-pgp")
227     (encrypt            tm:mc-pgp-encrypt-region        "mime-mc")
228     (insert-key         mc-insert-public-key            "mc-toplev")
229     )
230   "Alist of service names vs. corresponding functions and its filenames.
231 Each element looks like (SERVICE FUNCTION FILE).
232
233 SERVICE is a symbol of PGP processing.  It allows `verify', `decrypt',
234 `fetch-key', `snarf-keys', `mime-sign', `traditional-sign', `encrypt'
235 or `insert-key'.
236
237 Function is a symbol of function to do specified SERVICE.
238
239 FILE is string of filename which has definition of corresponding
240 FUNCTION.")
241
242 (defmacro pgp-function (method)
243   "Return function to do service METHOD."
244   (` (car (cdr (assq (, method) (symbol-value 'pgp-function-alist)))))
245   )
246
247 (mapcar (function
248          (lambda (method)
249            (autoload (second method)(third method))
250            ))
251         pgp-function-alist)
252
253
254 ;;; @ rot13-47
255 ;;;
256 ;; caesar-region written by phr@prep.ai.mit.edu  Nov 86
257 ;; modified by tower@prep Nov 86
258 ;; gnus-caesar-region
259 ;; Modified by umerin@flab.flab.Fujitsu.JUNET for ROT47.
260 (defun tm:caesar-region (&optional n)
261   "Caesar rotation of region by N, default 13, for decrypting netnews.
262 ROT47 will be performed for Japanese text in any case."
263   (interactive (if current-prefix-arg   ; Was there a prefix arg?
264                    (list (prefix-numeric-value current-prefix-arg))
265                  (list nil)))
266   (cond ((not (numberp n)) (setq n 13))
267         (t (setq n (mod n 26))))        ;canonicalize N
268   (if (not (zerop n))           ; no action needed for a rot of 0
269       (progn
270         (if (or (not (boundp 'caesar-translate-table))
271                 (/= (aref caesar-translate-table ?a) (+ ?a n)))
272             (let ((i 0) (lower "abcdefghijklmnopqrstuvwxyz") upper)
273               (message "Building caesar-translate-table...")
274               (setq caesar-translate-table (make-vector 256 0))
275               (while (< i 256)
276                 (aset caesar-translate-table i i)
277                 (setq i (1+ i)))
278               (setq lower (concat lower lower) upper (upcase lower) i 0)
279               (while (< i 26)
280                 (aset caesar-translate-table (+ ?a i) (aref lower (+ i n)))
281                 (aset caesar-translate-table (+ ?A i) (aref upper (+ i n)))
282                 (setq i (1+ i)))
283               ;; ROT47 for Japanese text.
284               ;; Thanks to ichikawa@flab.fujitsu.junet.
285               (setq i 161)
286               (let ((t1 (logior ?O 128))
287                     (t2 (logior ?! 128))
288                     (t3 (logior ?~ 128)))
289                 (while (< i 256)
290                   (aset caesar-translate-table i
291                         (let ((v (aref caesar-translate-table i)))
292                           (if (<= v t1) (if (< v t2) v (+ v 47))
293                             (if (<= v t3) (- v 47) v))))
294                   (setq i (1+ i))))
295               (message "Building caesar-translate-table...done")))
296         (let ((from (region-beginning))
297               (to (region-end))
298               (i 0) str len)
299           (setq str (buffer-substring from to))
300           (setq len (length str))
301           (while (< i len)
302             (aset str i (aref caesar-translate-table (aref str i)))
303             (setq i (1+ i)))
304           (goto-char from)
305           (delete-region from to)
306           (insert str)))))
307
308
309 ;;; @ field
310 ;;;
311
312 (defsubst regexp-or (&rest args)
313   (concat "\\(" (mapconcat (function identity) args "\\|") "\\)"))
314
315 (defun tm:set-fields (sym field-list &optional regexp-sym)
316   (or regexp-sym
317       (setq regexp-sym
318             (let ((name (symbol-name sym)))
319               (intern
320                (concat (if (string-match "\\(.*\\)-list" name)
321                            (substring name 0 (match-end 1))
322                          name)
323                        "-regexp")
324                )))
325       )
326   (set sym field-list)
327   (set regexp-sym
328        (concat "^" (apply (function regexp-or) field-list) ":"))
329   )
330
331 (defun tm:add-fields (sym field-list &optional regexp-sym)
332   (or regexp-sym
333       (setq regexp-sym
334             (let ((name (symbol-name sym)))
335               (intern
336                (concat (if (string-match "\\(.*\\)-list" name)
337                            (substring name 0 (match-end 1))
338                          name)
339                        "-regexp")
340                )))
341       )
342   (let ((fields (eval sym)))
343     (mapcar (function
344              (lambda (field)
345                (or (member field fields)
346                    (setq fields (cons field fields))
347                    )
348                ))
349             (reverse field-list)
350             )
351     (set regexp-sym
352          (concat "^" (apply (function regexp-or) fields) ":"))
353     (set sym fields)
354     ))
355
356 (defun tm:delete-fields (sym field-list &optional regexp-sym)
357   (or regexp-sym
358       (setq regexp-sym
359             (let ((name (symbol-name sym)))
360               (intern
361                (concat (if (string-match "\\(.*\\)-list" name)
362                            (substring name 0 (match-end 1))
363                          name)
364                        "-regexp")
365                )))
366       )
367   (let ((fields (eval sym)))
368     (mapcar (function
369              (lambda (field)
370                (setq fields (delete field fields))
371                ))
372             field-list)
373     (set regexp-sym
374          (concat "^" (apply (function regexp-or) fields) ":"))
375     (set sym fields)
376     ))
377
378
379 ;;; @ RCS version
380 ;;;
381
382 (defsubst get-version-string (id)
383   "Return a version-string from RCS ID."
384   (and (string-match ",v \\([0-9][0-9.][0-9.]+\\)" id)
385        (substring id (match-beginning 1)(match-end 1))
386        ))
387
388
389 ;;; @ end
390 ;;;
391
392 (provide 'mime-def)
393
394 ;;; mime-def.el ends here