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.33 1997-02-28 05:01:58 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.
31 (defvar mime/tmp-dir (or (getenv "TM_TMP_DIR") "/tmp/"))
33 (defvar mime/use-multi-frame
34 (and (>= emacs-major-version 19) window-system))
36 (defvar mime/find-file-function
37 (if mime/use-multi-frame
38 (function find-file-other-frame)
42 (defvar mime/output-buffer-window-is-shared-with-bbdb t
43 "*If t, mime/output-buffer window is shared with BBDB window.")
49 (defconst mime/output-buffer-name "*MIME-out*")
50 (defconst mime/temp-buffer-name " *MIME-temp*")
53 ;;; @ definitions about MIME
56 (defconst mime/tspecials "][\000-\040()<>@,\;:\\\"/?.=")
57 (defconst mime/token-regexp (concat "[^" mime/tspecials "]+"))
58 (defconst mime-charset-regexp mime/token-regexp)
60 (defconst mime/content-type-subtype-regexp
61 (concat mime/token-regexp "/" mime/token-regexp))
63 (defconst mime/disposition-type-regexp mime/token-regexp)
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)
106 (defvar default-mime-charset 'x-ctext)
108 (defvar mime-charset-coding-system-alist
110 (gb2312 . cn-gb-2312)
111 (iso-2022-jp-2 . iso-2022-ss2-7)
114 (defun mime-charset-to-coding-system (charset &optional lbt)
115 (if (stringp charset)
116 (setq charset (intern (downcase charset)))
119 (or (cdr (assq charset mime-charset-coding-system-alist))
120 (and (coding-system-p charset) charset)
123 (intern (concat (symbol-name cs) "-" (symbol-name lbt)))
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))
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)))
136 (encode-coding-region start end cs)
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)))
143 (decode-coding-region start end cs)
146 (defun encode-mime-charset-string (string charset)
147 "Encode the STRING as MIME CHARSET."
148 (let ((cs (mime-charset-to-coding-system charset)))
150 (encode-coding-string string cs)
153 (defun decode-mime-charset-string (string charset)
154 "Decode the STRING as MIME CHARSET."
155 (let ((cs (mime-charset-to-coding-system charset)))
157 (decode-coding-string string cs)
164 (defvar running-xemacs (string-match "XEmacs" emacs-version))
170 (defvar mime-button-face 'bold
171 "Face used for content-button or URL-button of MIME-Preview buffer.")
173 (defvar mime-button-mouse-face 'highlight
174 "Face used for MIME-preview buffer mouse highlighting.")
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
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))
189 (defvar mime-button-mother-dispatcher nil)
191 (defun mime-button-dispatcher (event)
192 "Select the button under point."
194 (let (buf point func data)
195 (save-window-excursion
196 (mouse-set-point event)
197 (setq buf (current-buffer)
199 func (get-text-property (point) 'mime-button-callback)
200 data (get-text-property (point) 'mime-button-data)
208 (if (fboundp mime-button-mother-dispatcher)
209 (funcall mime-button-mother-dispatcher event)
217 (defvar pgp-function-alist
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")
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")
230 "Alist of service names vs. corresponding functions and its filenames.
231 Each element looks like (SERVICE FUNCTION FILE).
233 SERVICE is a symbol of PGP processing. It allows `verify', `decrypt',
234 `fetch-key', `snarf-keys', `mime-sign', `traditional-sign', `encrypt'
237 Function is a symbol of function to do specified SERVICE.
239 FILE is string of filename which has definition of corresponding
242 (defmacro pgp-function (method)
243 "Return function to do service METHOD."
244 (` (car (cdr (assq (, method) (symbol-value 'pgp-function-alist)))))
249 (autoload (second method)(third method))
254 ;;; @ method selector kernel
260 (defun field-unifier-for-default (a b)
262 (cond ((equal a b) a)
270 (defun field-unifier-for-mode (a b)
279 (defun field-unify (a b)
280 (let ((sym (intern (concat "field-unifier-for-" (symbol-name (car a))))))
282 (setq sym (function field-unifier-for-default))
291 (defun assoc-unify (class instance)
293 (let ((cla (copy-alist class))
294 (ins (copy-alist instance))
296 cell aret ret prev rest)
299 (setq aret (assoc (car cell) ins))
301 (if (setq ret (field-unify cell aret))
304 (setq prev (put-alist (car (car ret))
309 (setq rest (put-alist (car (nth 2 ret))
313 (setq cla (put-alist (car cell)(cdr (nth 1 ret)) cla))
314 (setq ins (del-alist (car cell) ins))
320 (setq r (copy-alist ins))
323 (setq aret (assoc (car cell) cla))
325 (if (setq ret (field-unify cell aret))
328 (setq prev (put-alist (car (car ret))
333 (setq rest (put-alist (car (nth 2 ret))
337 (setq cla (del-alist (car cell) cla))
338 (setq ins (put-alist (car cell)(cdr (nth 1 ret)) ins))
344 (list prev (append cla ins) rest)
347 (defun get-unified-alist (db al)
351 (if (setq ret (nth 1 (assoc-unify (car r) al)))
357 (defun delete-atype (atl al)
358 (let* ((r atl) ret oal)
362 (if (setq ret (nth 1 (assoc-unify (car r) al)))
370 (defun remove-atype (sym al)
372 (set sym (delete-atype (eval sym) al))
375 (defun replace-atype (atl old-al new-al)
376 (let* ((r atl) ret oal)
379 (if (setq ret (nth 1 (assoc-unify (car r) old-al)))
380 (throw 'tag (rplaca r new-al))
386 (defun set-atype (sym al &rest options)
387 (if (null (boundp sym))
389 (let* ((replacement (memq 'replacement options))
390 (ignore-fields (car (cdr (memq 'ignore options))))
391 (remove (or (car (cdr (memq 'remove options)))
392 (let ((ral (copy-alist al)))
395 (setq ral (del-alist type ral))
402 (replace-atype (eval sym) remove al)
405 (delete-atype (eval sym) remove)
412 ;; caesar-region written by phr@prep.ai.mit.edu Nov 86
413 ;; modified by tower@prep Nov 86
414 ;; gnus-caesar-region
415 ;; Modified by umerin@flab.flab.Fujitsu.JUNET for ROT47.
416 (defun tm:caesar-region (&optional n)
417 "Caesar rotation of region by N, default 13, for decrypting netnews.
418 ROT47 will be performed for Japanese text in any case."
419 (interactive (if current-prefix-arg ; Was there a prefix arg?
420 (list (prefix-numeric-value current-prefix-arg))
422 (cond ((not (numberp n)) (setq n 13))
423 (t (setq n (mod n 26)))) ;canonicalize N
424 (if (not (zerop n)) ; no action needed for a rot of 0
426 (if (or (not (boundp 'caesar-translate-table))
427 (/= (aref caesar-translate-table ?a) (+ ?a n)))
428 (let ((i 0) (lower "abcdefghijklmnopqrstuvwxyz") upper)
429 (message "Building caesar-translate-table...")
430 (setq caesar-translate-table (make-vector 256 0))
432 (aset caesar-translate-table i i)
434 (setq lower (concat lower lower) upper (upcase lower) i 0)
436 (aset caesar-translate-table (+ ?a i) (aref lower (+ i n)))
437 (aset caesar-translate-table (+ ?A i) (aref upper (+ i n)))
439 ;; ROT47 for Japanese text.
440 ;; Thanks to ichikawa@flab.fujitsu.junet.
442 (let ((t1 (logior ?O 128))
444 (t3 (logior ?~ 128)))
446 (aset caesar-translate-table i
447 (let ((v (aref caesar-translate-table i)))
448 (if (<= v t1) (if (< v t2) v (+ v 47))
449 (if (<= v t3) (- v 47) v))))
451 (message "Building caesar-translate-table...done")))
452 (let ((from (region-beginning))
455 (setq str (buffer-substring from to))
456 (setq len (length str))
458 (aset str i (aref caesar-translate-table (aref str i)))
461 (delete-region from to)
468 (defsubst regexp-or (&rest args)
469 (concat "\\(" (mapconcat (function identity) args "\\|") "\\)"))
471 (defun tm:set-fields (sym field-list &optional regexp-sym)
474 (let ((name (symbol-name sym)))
476 (concat (if (string-match "\\(.*\\)-list" name)
477 (substring name 0 (match-end 1))
484 (concat "^" (apply (function regexp-or) field-list) ":"))
487 (defun tm:add-fields (sym field-list &optional regexp-sym)
490 (let ((name (symbol-name sym)))
492 (concat (if (string-match "\\(.*\\)-list" name)
493 (substring name 0 (match-end 1))
498 (let ((fields (eval sym)))
501 (or (member field fields)
502 (setq fields (cons field fields))
508 (concat "^" (apply (function regexp-or) fields) ":"))
512 (defun tm:delete-fields (sym field-list &optional regexp-sym)
515 (let ((name (symbol-name sym)))
517 (concat (if (string-match "\\(.*\\)-list" name)
518 (substring name 0 (match-end 1))
523 (let ((fields (eval sym)))
526 (setq fields (delete field fields))
530 (concat "^" (apply (function regexp-or) fields) ":"))
538 (defsubst get-version-string (id)
539 "Return a version-string from RCS ID."
540 (and (string-match ",v \\([0-9][0-9.][0-9.]+\\)" id)
541 (substring id (match-beginning 1)(match-end 1))
548 (defun call-after-loaded (module func &optional hook-name)
549 "If MODULE is provided, then FUNC is called.
550 Otherwise func is set to MODULE-load-hook.
551 If optional argument HOOK-NAME is specified,
552 it is used as hook to set."
553 (if (featurep module)
556 (setq hook-name (intern (concat (symbol-name module) "-load-hook")))
558 (add-hook hook-name func)
567 ;;; mime-def.el ends here