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.37 1997-03-01 04:12:37 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 charsets-to-mime-charset (charsets)
130 "Return MIME charset from list of charset CHARSETS.
131 This function refers variable `charsets-mime-charset-alist'
132 and `default-mime-charset'. [emu.el]"
135 (let ((rest charsets-mime-charset-alist)
137 (while (setq cell (car rest))
138 (if (catch 'not-subset
139 (let ((set1 charsets)
143 (setq obj (car set1))
145 (throw 'not-subset nil)
147 (setq set1 (cdr set1))
150 (throw 'tag (cdr cell))
152 (setq rest (cdr rest))
154 default-mime-charset)))
156 (defun detect-mime-charset-region (start end)
157 "Return MIME charset for region between START and END."
158 (charsets-to-mime-charset
159 (find-charset-string (buffer-substring start end))
162 (defun encode-mime-charset-region (start end charset)
163 "Encode the text between START and END as MIME CHARSET."
164 (let ((cs (mime-charset-to-coding-system charset)))
166 (encode-coding-region start end cs)
169 (defun decode-mime-charset-region (start end charset)
170 "Decode the text between START and END as MIME CHARSET."
171 (let ((cs (mime-charset-to-coding-system charset)))
173 (decode-coding-region start end cs)
176 (defun encode-mime-charset-string (string charset)
177 "Encode the STRING as MIME CHARSET."
178 (let ((cs (mime-charset-to-coding-system charset)))
180 (encode-coding-string string cs)
183 (defun decode-mime-charset-string (string charset)
184 "Decode the STRING as MIME CHARSET."
185 (let ((cs (mime-charset-to-coding-system charset)))
187 (decode-coding-string string cs)
194 (defvar running-xemacs (string-match "XEmacs" emacs-version))
200 (defvar mime-button-face 'bold
201 "Face used for content-button or URL-button of MIME-Preview buffer.")
203 (defvar mime-button-mouse-face 'highlight
204 "Face used for MIME-preview buffer mouse highlighting.")
206 (defun mime-add-button (from to func &optional data)
207 "Create a button between FROM and TO with callback FUNC and data DATA."
208 (and mime-button-face
209 (overlay-put (make-overlay from to) 'face mime-button-face))
210 (add-text-properties from to
212 (and mime-button-mouse-face
213 (list 'mouse-face mime-button-mouse-face))
214 (list 'mime-button-callback func)
215 (and data (list 'mime-button-data data))
219 (defvar mime-button-mother-dispatcher nil)
221 (defun mime-button-dispatcher (event)
222 "Select the button under point."
224 (let (buf point func data)
225 (save-window-excursion
226 (mouse-set-point event)
227 (setq buf (current-buffer)
229 func (get-text-property (point) 'mime-button-callback)
230 data (get-text-property (point) 'mime-button-data)
238 (if (fboundp mime-button-mother-dispatcher)
239 (funcall mime-button-mother-dispatcher event)
247 (defvar pgp-function-alist
250 (verify mc-verify "mc-toplev")
251 (decrypt mc-decrypt "mc-toplev")
252 (fetch-key mc-pgp-fetch-key "mc-pgp")
253 (snarf-keys mc-snarf-keys "mc-toplev")
255 (mime-sign tm:mc-pgp-sign-region "mime-mc")
256 (traditional-sign mc-pgp-sign-region "mc-pgp")
257 (encrypt tm:mc-pgp-encrypt-region "mime-mc")
258 (insert-key mc-insert-public-key "mc-toplev")
260 "Alist of service names vs. corresponding functions and its filenames.
261 Each element looks like (SERVICE FUNCTION FILE).
263 SERVICE is a symbol of PGP processing. It allows `verify', `decrypt',
264 `fetch-key', `snarf-keys', `mime-sign', `traditional-sign', `encrypt'
267 Function is a symbol of function to do specified SERVICE.
269 FILE is string of filename which has definition of corresponding
272 (defmacro pgp-function (method)
273 "Return function to do service METHOD."
274 (` (car (cdr (assq (, method) (symbol-value 'pgp-function-alist)))))
279 (autoload (second method)(third method))
284 ;;; @ method selector kernel
290 (defun field-unifier-for-default (a b)
292 (cond ((equal a b) a)
300 (defun field-unifier-for-mode (a b)
309 (defun field-unify (a b)
310 (let ((sym (intern (concat "field-unifier-for-" (symbol-name (car a))))))
312 (setq sym (function field-unifier-for-default))
321 (defun assoc-unify (class instance)
323 (let ((cla (copy-alist class))
324 (ins (copy-alist instance))
326 cell aret ret prev rest)
329 (setq aret (assoc (car cell) ins))
331 (if (setq ret (field-unify cell aret))
334 (setq prev (put-alist (car (car ret))
339 (setq rest (put-alist (car (nth 2 ret))
343 (setq cla (put-alist (car cell)(cdr (nth 1 ret)) cla))
344 (setq ins (del-alist (car cell) ins))
350 (setq r (copy-alist ins))
353 (setq aret (assoc (car cell) cla))
355 (if (setq ret (field-unify cell aret))
358 (setq prev (put-alist (car (car ret))
363 (setq rest (put-alist (car (nth 2 ret))
367 (setq cla (del-alist (car cell) cla))
368 (setq ins (put-alist (car cell)(cdr (nth 1 ret)) ins))
374 (list prev (append cla ins) rest)
377 (defun get-unified-alist (db al)
381 (if (setq ret (nth 1 (assoc-unify (car r) al)))
387 (defun delete-atype (atl al)
388 (let* ((r atl) ret oal)
392 (if (setq ret (nth 1 (assoc-unify (car r) al)))
400 (defun remove-atype (sym al)
402 (set sym (delete-atype (eval sym) al))
405 (defun replace-atype (atl old-al new-al)
406 (let* ((r atl) ret oal)
409 (if (setq ret (nth 1 (assoc-unify (car r) old-al)))
410 (throw 'tag (rplaca r new-al))
416 (defun set-atype (sym al &rest options)
417 (if (null (boundp sym))
419 (let* ((replacement (memq 'replacement options))
420 (ignore-fields (car (cdr (memq 'ignore options))))
421 (remove (or (car (cdr (memq 'remove options)))
422 (let ((ral (copy-alist al)))
425 (setq ral (del-alist type ral))
432 (replace-atype (eval sym) remove al)
435 (delete-atype (eval sym) remove)
442 ;; caesar-region written by phr@prep.ai.mit.edu Nov 86
443 ;; modified by tower@prep Nov 86
444 ;; gnus-caesar-region
445 ;; Modified by umerin@flab.flab.Fujitsu.JUNET for ROT47.
446 (defun tm:caesar-region (&optional n)
447 "Caesar rotation of region by N, default 13, for decrypting netnews.
448 ROT47 will be performed for Japanese text in any case."
449 (interactive (if current-prefix-arg ; Was there a prefix arg?
450 (list (prefix-numeric-value current-prefix-arg))
452 (cond ((not (numberp n)) (setq n 13))
453 (t (setq n (mod n 26)))) ;canonicalize N
454 (if (not (zerop n)) ; no action needed for a rot of 0
456 (if (or (not (boundp 'caesar-translate-table))
457 (/= (aref caesar-translate-table ?a) (+ ?a n)))
458 (let ((i 0) (lower "abcdefghijklmnopqrstuvwxyz") upper)
459 (message "Building caesar-translate-table...")
460 (setq caesar-translate-table (make-vector 256 0))
462 (aset caesar-translate-table i i)
464 (setq lower (concat lower lower) upper (upcase lower) i 0)
466 (aset caesar-translate-table (+ ?a i) (aref lower (+ i n)))
467 (aset caesar-translate-table (+ ?A i) (aref upper (+ i n)))
469 ;; ROT47 for Japanese text.
470 ;; Thanks to ichikawa@flab.fujitsu.junet.
472 (let ((t1 (logior ?O 128))
474 (t3 (logior ?~ 128)))
476 (aset caesar-translate-table i
477 (let ((v (aref caesar-translate-table i)))
478 (if (<= v t1) (if (< v t2) v (+ v 47))
479 (if (<= v t3) (- v 47) v))))
481 (message "Building caesar-translate-table...done")))
482 (let ((from (region-beginning))
485 (setq str (buffer-substring from to))
486 (setq len (length str))
488 (aset str i (aref caesar-translate-table (aref str i)))
491 (delete-region from to)
498 (defsubst regexp-or (&rest args)
499 (concat "\\(" (mapconcat (function identity) args "\\|") "\\)"))
501 (defun tm:set-fields (sym field-list &optional regexp-sym)
504 (let ((name (symbol-name sym)))
506 (concat (if (string-match "\\(.*\\)-list" name)
507 (substring name 0 (match-end 1))
514 (concat "^" (apply (function regexp-or) field-list) ":"))
517 (defun tm:add-fields (sym field-list &optional regexp-sym)
520 (let ((name (symbol-name sym)))
522 (concat (if (string-match "\\(.*\\)-list" name)
523 (substring name 0 (match-end 1))
528 (let ((fields (eval sym)))
531 (or (member field fields)
532 (setq fields (cons field fields))
538 (concat "^" (apply (function regexp-or) fields) ":"))
542 (defun tm:delete-fields (sym field-list &optional regexp-sym)
545 (let ((name (symbol-name sym)))
547 (concat (if (string-match "\\(.*\\)-list" name)
548 (substring name 0 (match-end 1))
553 (let ((fields (eval sym)))
556 (setq fields (delete field fields))
560 (concat "^" (apply (function regexp-or) fields) ":"))
568 (defsubst get-version-string (id)
569 "Return a version-string from RCS ID."
570 (and (string-match ",v \\([0-9][0-9.][0-9.]+\\)" id)
571 (substring id (match-beginning 1)(match-end 1))
578 (defsubst eliminate-top-spaces (string)
579 "Eliminate top sequence of space or tab in STRING."
580 (if (string-match "^[ \t]+" string)
581 (substring string (match-end 0))
584 (defun call-after-loaded (module func &optional hook-name)
585 "If MODULE is provided, then FUNC is called.
586 Otherwise func is set to MODULE-load-hook.
587 If optional argument HOOK-NAME is specified,
588 it is used as hook to set."
589 (if (featurep module)
592 (setq hook-name (intern (concat (symbol-name module) "-load-hook")))
594 (add-hook hook-name func)
597 (defmacro defun-maybe (name &rest everything-else)
598 (or (and (fboundp name)
599 (not (get name 'defun-maybe))
601 `(or (fboundp (quote ,name))
603 (defun ,name ,@everything-else)
604 (put (quote ,name) 'defun-maybe t)
608 (put 'defun-maybe 'lisp-indent-function 'defun)
610 (defun-maybe functionp (obj)
611 "Returns t if OBJ is a function, nil otherwise.
612 \[XEmacs emulating function]"
614 (byte-code-function-p obj)
615 (and (symbolp obj)(fboundp obj))
616 (and (consp obj)(eq (car obj) 'lambda))
625 ;;; mime-def.el ends here