atype functions were moved from mime-play.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.31 1997-02-28 04:56: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 ;;; @ 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 ;;; @ method selector kernel
255 ;;;
256
257 ;;; @@ field
258 ;;;
259
260 (defun put-fields (tp c)
261   (catch 'tag
262     (let ((r tp) f ret)
263       (while r
264         (setq f (car r))
265         (if (not (if (setq ret (assoc (car f) c))
266                      (equal (cdr ret)(cdr f))
267                    (setq c (cons f c))
268                    ))
269             (throw 'tag 'error))
270         (setq r (cdr r))
271         ))
272     c))
273
274
275 ;;; @@ field unifier
276 ;;;
277
278 (defun field-unifier-for-default (a b)
279   (let ((ret
280          (cond ((equal a b)    a)
281                ((null (cdr b)) a)
282                ((null (cdr a)) b)
283                )))
284     (if ret
285         (list nil ret nil)
286       )))
287
288 (defun field-unifier-for-mode (a b)
289   (let ((va (cdr a)))
290     (if (if (consp va)
291             (member (cdr b) va)
292           (equal va (cdr b))
293           )
294         (list nil b nil)
295       )))
296
297 (defun field-unify (a b)
298   (let ((sym (intern (concat "field-unifier-for-" (symbol-value (car a))))))
299     (or (fboundp sym)
300         (setq sym (function field-unifier-for-default))
301         )
302     (funcall sym a b)
303     ))
304
305
306 ;;; @@ type unifier
307 ;;;
308
309 (defun assoc-unify (class instance)
310   (catch 'tag
311     (let ((cla (copy-alist class))
312           (ins (copy-alist instance))
313           (r class)
314           cell aret ret prev rest)
315       (while r
316         (setq cell (car r))
317         (setq aret (assoc (car cell) ins))
318         (if aret
319             (if (setq ret (field-unify cell aret))
320                 (progn
321                   (if (car ret)
322                       (setq prev (put-alist (car (car ret))
323                                             (cdr (car ret))
324                                             prev))
325                     )
326                   (if (nth 2 ret)
327                       (setq rest (put-alist (car (nth 2 ret))
328                                             (cdr (nth 2 ret))
329                                             rest))
330                     )
331                   (setq cla (put-alist (car cell)(cdr (nth 1 ret)) cla))
332                   (setq ins (del-alist (car cell) ins))
333                   )
334               (throw 'tag nil)
335               ))
336         (setq r (cdr r))
337         )
338       (setq r (copy-alist ins))
339       (while r
340         (setq cell (car r))
341         (setq aret (assoc (car cell) cla))
342         (if aret
343             (if (setq ret (field-unify cell aret))
344                 (progn
345                   (if (car ret)
346                       (setq prev (put-alist (car (car ret))
347                                             (cdr (car ret))
348                                             prev))
349                     )
350                   (if (nth 2 ret)
351                       (setq rest (put-alist (car (nth 2 ret))
352                                             (cdr (nth 2 ret))
353                                             rest))
354                     )
355                   (setq cla (del-alist (car cell) cla))
356                   (setq ins (put-alist (car cell)(cdr (nth 1 ret)) ins))
357                   )
358               (throw 'tag nil)
359               ))
360         (setq r (cdr r))
361         )
362       (list prev (append cla ins) rest)
363       )))
364
365 (defun get-unified-alist (db al)
366   (let ((r db) ret)
367     (catch 'tag
368       (while r
369         (if (setq ret (nth 1 (assoc-unify (car r) al)))
370             (throw 'tag ret)
371           )
372         (setq r (cdr r))
373         ))))
374
375 (defun delete-atype (atl al)
376   (let* ((r atl) ret oal)
377     (setq oal
378           (catch 'tag
379             (while r
380               (if (setq ret (nth 1 (assoc-unify (car r) al)))
381                   (throw 'tag (car r))
382                 )
383               (setq r (cdr r))
384               )))
385     (delete oal atl)
386     ))
387
388 (defun remove-atype (sym al)
389   (and (boundp sym)
390        (set sym (delete-atype (eval sym) al))
391        ))
392
393 (defun replace-atype (atl old-al new-al)
394   (let* ((r atl) ret oal)
395     (if (catch 'tag
396           (while r
397             (if (setq ret (nth 1 (assoc-unify (car r) old-al)))
398                 (throw 'tag (rplaca r new-al))
399               )
400             (setq r (cdr r))
401             ))
402         atl)))
403
404 (defun set-atype (sym al &rest options)
405   (if (null (boundp sym))
406       (set sym al)
407     (let* ((replacement (memq 'replacement options))
408            (ignore-fields (car (cdr (memq 'ignore options))))
409            (remove (or (car (cdr (memq 'remove options)))
410                        (let ((ral (copy-alist al)))
411                          (mapcar (function
412                                   (lambda (type)
413                                     (setq ral (del-alist type ral))
414                                     ))
415                                  ignore-fields)
416                          ral)))
417            )
418       (set sym
419            (or (if replacement
420                    (replace-atype (eval sym) remove al)
421                  )
422                (cons al
423                      (delete-atype (eval sym) remove)
424                      )
425                )))))
426
427
428 ;;; @ rot13-47
429 ;;;
430 ;; caesar-region written by phr@prep.ai.mit.edu  Nov 86
431 ;; modified by tower@prep Nov 86
432 ;; gnus-caesar-region
433 ;; Modified by umerin@flab.flab.Fujitsu.JUNET for ROT47.
434 (defun tm:caesar-region (&optional n)
435   "Caesar rotation of region by N, default 13, for decrypting netnews.
436 ROT47 will be performed for Japanese text in any case."
437   (interactive (if current-prefix-arg   ; Was there a prefix arg?
438                    (list (prefix-numeric-value current-prefix-arg))
439                  (list nil)))
440   (cond ((not (numberp n)) (setq n 13))
441         (t (setq n (mod n 26))))        ;canonicalize N
442   (if (not (zerop n))           ; no action needed for a rot of 0
443       (progn
444         (if (or (not (boundp 'caesar-translate-table))
445                 (/= (aref caesar-translate-table ?a) (+ ?a n)))
446             (let ((i 0) (lower "abcdefghijklmnopqrstuvwxyz") upper)
447               (message "Building caesar-translate-table...")
448               (setq caesar-translate-table (make-vector 256 0))
449               (while (< i 256)
450                 (aset caesar-translate-table i i)
451                 (setq i (1+ i)))
452               (setq lower (concat lower lower) upper (upcase lower) i 0)
453               (while (< i 26)
454                 (aset caesar-translate-table (+ ?a i) (aref lower (+ i n)))
455                 (aset caesar-translate-table (+ ?A i) (aref upper (+ i n)))
456                 (setq i (1+ i)))
457               ;; ROT47 for Japanese text.
458               ;; Thanks to ichikawa@flab.fujitsu.junet.
459               (setq i 161)
460               (let ((t1 (logior ?O 128))
461                     (t2 (logior ?! 128))
462                     (t3 (logior ?~ 128)))
463                 (while (< i 256)
464                   (aset caesar-translate-table i
465                         (let ((v (aref caesar-translate-table i)))
466                           (if (<= v t1) (if (< v t2) v (+ v 47))
467                             (if (<= v t3) (- v 47) v))))
468                   (setq i (1+ i))))
469               (message "Building caesar-translate-table...done")))
470         (let ((from (region-beginning))
471               (to (region-end))
472               (i 0) str len)
473           (setq str (buffer-substring from to))
474           (setq len (length str))
475           (while (< i len)
476             (aset str i (aref caesar-translate-table (aref str i)))
477             (setq i (1+ i)))
478           (goto-char from)
479           (delete-region from to)
480           (insert str)))))
481
482
483 ;;; @ field
484 ;;;
485
486 (defsubst regexp-or (&rest args)
487   (concat "\\(" (mapconcat (function identity) args "\\|") "\\)"))
488
489 (defun tm:set-fields (sym field-list &optional regexp-sym)
490   (or regexp-sym
491       (setq regexp-sym
492             (let ((name (symbol-name sym)))
493               (intern
494                (concat (if (string-match "\\(.*\\)-list" name)
495                            (substring name 0 (match-end 1))
496                          name)
497                        "-regexp")
498                )))
499       )
500   (set sym field-list)
501   (set regexp-sym
502        (concat "^" (apply (function regexp-or) field-list) ":"))
503   )
504
505 (defun tm:add-fields (sym field-list &optional regexp-sym)
506   (or regexp-sym
507       (setq regexp-sym
508             (let ((name (symbol-name sym)))
509               (intern
510                (concat (if (string-match "\\(.*\\)-list" name)
511                            (substring name 0 (match-end 1))
512                          name)
513                        "-regexp")
514                )))
515       )
516   (let ((fields (eval sym)))
517     (mapcar (function
518              (lambda (field)
519                (or (member field fields)
520                    (setq fields (cons field fields))
521                    )
522                ))
523             (reverse field-list)
524             )
525     (set regexp-sym
526          (concat "^" (apply (function regexp-or) fields) ":"))
527     (set sym fields)
528     ))
529
530 (defun tm:delete-fields (sym field-list &optional regexp-sym)
531   (or regexp-sym
532       (setq regexp-sym
533             (let ((name (symbol-name sym)))
534               (intern
535                (concat (if (string-match "\\(.*\\)-list" name)
536                            (substring name 0 (match-end 1))
537                          name)
538                        "-regexp")
539                )))
540       )
541   (let ((fields (eval sym)))
542     (mapcar (function
543              (lambda (field)
544                (setq fields (delete field fields))
545                ))
546             field-list)
547     (set regexp-sym
548          (concat "^" (apply (function regexp-or) fields) ":"))
549     (set sym fields)
550     ))
551
552
553 ;;; @ RCS version
554 ;;;
555
556 (defsubst get-version-string (id)
557   "Return a version-string from RCS ID."
558   (and (string-match ",v \\([0-9][0-9.][0-9.]+\\)" id)
559        (substring id (match-beginning 1)(match-end 1))
560        ))
561
562
563 ;;; @ Other Utility
564 ;;;
565
566 (defun call-after-loaded (module func &optional hook-name)
567   "If MODULE is provided, then FUNC is called.
568 Otherwise func is set to MODULE-load-hook.
569 If optional argument HOOK-NAME is specified,
570 it is used as hook to set."
571   (if (featurep module)
572       (funcall func)
573     (or hook-name
574         (setq hook-name (intern (concat (symbol-name module) "-load-hook")))
575         )
576     (add-hook hook-name func)
577     ))
578
579
580 ;;; @ end
581 ;;;
582
583 (provide 'mime-def)
584
585 ;;; mime-def.el ends here