1 ;;; mime-def.el --- definition module for SEMI
3 ;; Copyright (C) 1995,1996,1997 Free Software Foundation, Inc.
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; Version: $Id: mime-def.el,v 0.35 1997-02-28 06:46:48 tmorioka Exp $
7 ;; Keywords: definition, MIME, multimedia, mail, news
9 ;; This file is part of SEMI (SEMI is Emacs MIME Interfaces).
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.
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.
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.
34 (defvar mime/tmp-dir (or (getenv "TM_TMP_DIR") "/tmp/"))
36 (defvar mime/use-multi-frame
37 (and (>= emacs-major-version 19) window-system))
39 (defvar mime/find-file-function
40 (if mime/use-multi-frame
41 (function find-file-other-frame)
45 (defvar mime/output-buffer-window-is-shared-with-bbdb t
46 "*If t, mime/output-buffer window is shared with BBDB window.")
52 (defconst mime/output-buffer-name "*MIME-out*")
53 (defconst mime/temp-buffer-name " *MIME-temp*")
56 ;;; @ definitions about MIME
59 (defconst mime/tspecials "][\000-\040()<>@,\;:\\\"/?.=")
60 (defconst mime/token-regexp (concat "[^" mime/tspecials "]+"))
61 (defconst mime-charset-regexp mime/token-regexp)
63 (defconst mime/content-type-subtype-regexp
64 (concat mime/token-regexp "/" mime/token-regexp))
66 (defconst mime/disposition-type-regexp mime/token-regexp)
72 (defvar charsets-mime-charset-alist
73 '(((ascii) . us-ascii)
74 ((ascii latin-iso8859-1) . iso-8859-1)
75 ((ascii latin-iso8859-2) . iso-8859-2)
76 ((ascii latin-iso8859-3) . iso-8859-3)
77 ((ascii latin-iso8859-4) . iso-8859-4)
78 ;;; ((ascii cyrillic-iso8859-5) . iso-8859-5)
79 ((ascii cyrillic-iso8859-5) . koi8-r)
80 ((ascii arabic-iso8859-6) . iso-8859-6)
81 ((ascii greek-iso8859-7) . iso-8859-7)
82 ((ascii hebrew-iso8859-8) . iso-8859-8)
83 ((ascii latin-iso8859-9) . iso-8859-9)
84 ((ascii latin-jisx0201
85 japanese-jisx0208-1978 japanese-jisx0208) . iso-2022-jp)
86 ((ascii korean-ksc5601) . euc-kr)
87 ((ascii chinese-gb2312) . cn-gb-2312)
88 ((ascii chinese-big5-1 chinese-big5-2) . cn-big5)
89 ((ascii latin-iso8859-1 greek-iso8859-7
90 latin-jisx0201 japanese-jisx0208-1978
91 chinese-gb2312 japanese-jisx0208
92 korean-ksc5601 japanese-jisx0212) . iso-2022-jp-2)
93 ((ascii latin-iso8859-1 greek-iso8859-7
94 latin-jisx0201 japanese-jisx0208-1978
95 chinese-gb2312 japanese-jisx0208
96 korean-ksc5601 japanese-jisx0212
97 chinese-cns11643-1 chinese-cns11643-2) . iso-2022-int-1)
98 ((ascii latin-iso8859-1 latin-iso8859-2
99 cyrillic-iso8859-5 greek-iso8859-7
100 latin-jisx0201 japanese-jisx0208-1978
101 chinese-gb2312 japanese-jisx0208
102 korean-ksc5601 japanese-jisx0212
103 chinese-cns11643-1 chinese-cns11643-2
104 chinese-cns11643-3 chinese-cns11643-4
105 chinese-cns11643-5 chinese-cns11643-6
106 chinese-cns11643-7) . iso-2022-cjk)
109 (defvar default-mime-charset 'x-ctext)
111 (defvar mime-charset-coding-system-alist
113 (gb2312 . cn-gb-2312)
114 (iso-2022-jp-2 . iso-2022-ss2-7)
117 (defun mime-charset-to-coding-system (charset &optional lbt)
118 (if (stringp charset)
119 (setq charset (intern (downcase charset)))
122 (or (cdr (assq charset mime-charset-coding-system-alist))
123 (and (coding-system-p charset) charset)
126 (intern (concat (symbol-name cs) "-" (symbol-name lbt)))
129 (defun detect-mime-charset-region (start end)
130 "Return MIME charset for region between START and END."
131 (charsets-to-mime-charset
132 (find-charset-string (buffer-substring start end))
135 (defun encode-mime-charset-region (start end charset)
136 "Encode the text between START and END as MIME CHARSET."
137 (let ((cs (mime-charset-to-coding-system charset)))
139 (encode-coding-region start end cs)
142 (defun decode-mime-charset-region (start end charset)
143 "Decode the text between START and END as MIME CHARSET."
144 (let ((cs (mime-charset-to-coding-system charset)))
146 (decode-coding-region start end cs)
149 (defun encode-mime-charset-string (string charset)
150 "Encode the STRING as MIME CHARSET."
151 (let ((cs (mime-charset-to-coding-system charset)))
153 (encode-coding-string string cs)
156 (defun decode-mime-charset-string (string charset)
157 "Decode the STRING as MIME CHARSET."
158 (let ((cs (mime-charset-to-coding-system charset)))
160 (decode-coding-string string cs)
167 (defvar running-xemacs (string-match "XEmacs" emacs-version))
173 (defvar mime-button-face 'bold
174 "Face used for content-button or URL-button of MIME-Preview buffer.")
176 (defvar mime-button-mouse-face 'highlight
177 "Face used for MIME-preview buffer mouse highlighting.")
179 (defun mime-add-button (from to func &optional data)
180 "Create a button between FROM and TO with callback FUNC and data DATA."
181 (and mime-button-face
182 (overlay-put (make-overlay from to) 'face mime-button-face))
183 (add-text-properties from to
185 (and mime-button-mouse-face
186 (list 'mouse-face mime-button-mouse-face))
187 (list 'mime-button-callback func)
188 (and data (list 'mime-button-data data))
192 (defvar mime-button-mother-dispatcher nil)
194 (defun mime-button-dispatcher (event)
195 "Select the button under point."
197 (let (buf point func data)
198 (save-window-excursion
199 (mouse-set-point event)
200 (setq buf (current-buffer)
202 func (get-text-property (point) 'mime-button-callback)
203 data (get-text-property (point) 'mime-button-data)
211 (if (fboundp mime-button-mother-dispatcher)
212 (funcall mime-button-mother-dispatcher event)
220 (defvar pgp-function-alist
223 (verify mc-verify "mc-toplev")
224 (decrypt mc-decrypt "mc-toplev")
225 (fetch-key mc-pgp-fetch-key "mc-pgp")
226 (snarf-keys mc-snarf-keys "mc-toplev")
228 (mime-sign tm:mc-pgp-sign-region "mime-mc")
229 (traditional-sign mc-pgp-sign-region "mc-pgp")
230 (encrypt tm:mc-pgp-encrypt-region "mime-mc")
231 (insert-key mc-insert-public-key "mc-toplev")
233 "Alist of service names vs. corresponding functions and its filenames.
234 Each element looks like (SERVICE FUNCTION FILE).
236 SERVICE is a symbol of PGP processing. It allows `verify', `decrypt',
237 `fetch-key', `snarf-keys', `mime-sign', `traditional-sign', `encrypt'
240 Function is a symbol of function to do specified SERVICE.
242 FILE is string of filename which has definition of corresponding
245 (defmacro pgp-function (method)
246 "Return function to do service METHOD."
247 (` (car (cdr (assq (, method) (symbol-value 'pgp-function-alist)))))
252 (autoload (second method)(third method))
257 ;;; @ method selector kernel
263 (defun field-unifier-for-default (a b)
265 (cond ((equal a b) a)
273 (defun field-unifier-for-mode (a b)
282 (defun field-unify (a b)
283 (let ((sym (intern (concat "field-unifier-for-" (symbol-name (car a))))))
285 (setq sym (function field-unifier-for-default))
294 (defun assoc-unify (class instance)
296 (let ((cla (copy-alist class))
297 (ins (copy-alist instance))
299 cell aret ret prev rest)
302 (setq aret (assoc (car cell) ins))
304 (if (setq ret (field-unify cell aret))
307 (setq prev (put-alist (car (car ret))
312 (setq rest (put-alist (car (nth 2 ret))
316 (setq cla (put-alist (car cell)(cdr (nth 1 ret)) cla))
317 (setq ins (del-alist (car cell) ins))
323 (setq r (copy-alist ins))
326 (setq aret (assoc (car cell) cla))
328 (if (setq ret (field-unify cell aret))
331 (setq prev (put-alist (car (car ret))
336 (setq rest (put-alist (car (nth 2 ret))
340 (setq cla (del-alist (car cell) cla))
341 (setq ins (put-alist (car cell)(cdr (nth 1 ret)) ins))
347 (list prev (append cla ins) rest)
350 (defun get-unified-alist (db al)
354 (if (setq ret (nth 1 (assoc-unify (car r) al)))
360 (defun delete-atype (atl al)
361 (let* ((r atl) ret oal)
365 (if (setq ret (nth 1 (assoc-unify (car r) al)))
373 (defun remove-atype (sym al)
375 (set sym (delete-atype (eval sym) al))
378 (defun replace-atype (atl old-al new-al)
379 (let* ((r atl) ret oal)
382 (if (setq ret (nth 1 (assoc-unify (car r) old-al)))
383 (throw 'tag (rplaca r new-al))
389 (defun set-atype (sym al &rest options)
390 (if (null (boundp sym))
392 (let* ((replacement (memq 'replacement options))
393 (ignore-fields (car (cdr (memq 'ignore options))))
394 (remove (or (car (cdr (memq 'remove options)))
395 (let ((ral (copy-alist al)))
398 (setq ral (del-alist type ral))
405 (replace-atype (eval sym) remove al)
408 (delete-atype (eval sym) remove)
415 ;; caesar-region written by phr@prep.ai.mit.edu Nov 86
416 ;; modified by tower@prep Nov 86
417 ;; gnus-caesar-region
418 ;; Modified by umerin@flab.flab.Fujitsu.JUNET for ROT47.
419 (defun tm:caesar-region (&optional n)
420 "Caesar rotation of region by N, default 13, for decrypting netnews.
421 ROT47 will be performed for Japanese text in any case."
422 (interactive (if current-prefix-arg ; Was there a prefix arg?
423 (list (prefix-numeric-value current-prefix-arg))
425 (cond ((not (numberp n)) (setq n 13))
426 (t (setq n (mod n 26)))) ;canonicalize N
427 (if (not (zerop n)) ; no action needed for a rot of 0
429 (if (or (not (boundp 'caesar-translate-table))
430 (/= (aref caesar-translate-table ?a) (+ ?a n)))
431 (let ((i 0) (lower "abcdefghijklmnopqrstuvwxyz") upper)
432 (message "Building caesar-translate-table...")
433 (setq caesar-translate-table (make-vector 256 0))
435 (aset caesar-translate-table i i)
437 (setq lower (concat lower lower) upper (upcase lower) i 0)
439 (aset caesar-translate-table (+ ?a i) (aref lower (+ i n)))
440 (aset caesar-translate-table (+ ?A i) (aref upper (+ i n)))
442 ;; ROT47 for Japanese text.
443 ;; Thanks to ichikawa@flab.fujitsu.junet.
445 (let ((t1 (logior ?O 128))
447 (t3 (logior ?~ 128)))
449 (aset caesar-translate-table i
450 (let ((v (aref caesar-translate-table i)))
451 (if (<= v t1) (if (< v t2) v (+ v 47))
452 (if (<= v t3) (- v 47) v))))
454 (message "Building caesar-translate-table...done")))
455 (let ((from (region-beginning))
458 (setq str (buffer-substring from to))
459 (setq len (length str))
461 (aset str i (aref caesar-translate-table (aref str i)))
464 (delete-region from to)
471 (defsubst regexp-or (&rest args)
472 (concat "\\(" (mapconcat (function identity) args "\\|") "\\)"))
474 (defun tm:set-fields (sym field-list &optional regexp-sym)
477 (let ((name (symbol-name sym)))
479 (concat (if (string-match "\\(.*\\)-list" name)
480 (substring name 0 (match-end 1))
487 (concat "^" (apply (function regexp-or) field-list) ":"))
490 (defun tm:add-fields (sym field-list &optional regexp-sym)
493 (let ((name (symbol-name sym)))
495 (concat (if (string-match "\\(.*\\)-list" name)
496 (substring name 0 (match-end 1))
501 (let ((fields (eval sym)))
504 (or (member field fields)
505 (setq fields (cons field fields))
511 (concat "^" (apply (function regexp-or) fields) ":"))
515 (defun tm:delete-fields (sym field-list &optional regexp-sym)
518 (let ((name (symbol-name sym)))
520 (concat (if (string-match "\\(.*\\)-list" name)
521 (substring name 0 (match-end 1))
526 (let ((fields (eval sym)))
529 (setq fields (delete field fields))
533 (concat "^" (apply (function regexp-or) fields) ":"))
541 (defsubst get-version-string (id)
542 "Return a version-string from RCS ID."
543 (and (string-match ",v \\([0-9][0-9.][0-9.]+\\)" id)
544 (substring id (match-beginning 1)(match-end 1))
551 (defun call-after-loaded (module func &optional hook-name)
552 "If MODULE is provided, then FUNC is called.
553 Otherwise func is set to MODULE-load-hook.
554 If optional argument HOOK-NAME is specified,
555 it is used as hook to set."
556 (if (featurep module)
559 (setq hook-name (intern (concat (symbol-name module) "-load-hook")))
561 (add-hook hook-name func)
564 (defmacro defun-maybe (name &rest everything-else)
565 (or (and (fboundp name)
566 (not (get name 'defun-maybe))
568 `(or (fboundp (quote ,name))
570 (defun ,name ,@everything-else)
571 (put (quote ,name) 'defun-maybe t)
575 (put 'defun-maybe 'lisp-indent-function 'defun)
577 (defun-maybe functionp (obj)
578 "Returns t if OBJ is a function, nil otherwise.
579 \[XEmacs emulating function]"
581 (byte-code-function-p obj)
582 (and (symbolp obj)(fboundp obj))
583 (and (consp obj)(eq (car obj) 'lambda))
592 ;;; mime-def.el ends here