Function `put-fields' were abolished.
[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.32 1997-02-28 04:57:30 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 unifier
258 ;;;
259
260 (defun field-unifier-for-default (a b)
261   (let ((ret
262          (cond ((equal a b)    a)
263                ((null (cdr b)) a)
264                ((null (cdr a)) b)
265                )))
266     (if ret
267         (list nil ret nil)
268       )))
269
270 (defun field-unifier-for-mode (a b)
271   (let ((va (cdr a)))
272     (if (if (consp va)
273             (member (cdr b) va)
274           (equal va (cdr b))
275           )
276         (list nil b nil)
277       )))
278
279 (defun field-unify (a b)
280   (let ((sym (intern (concat "field-unifier-for-" (symbol-value (car a))))))
281     (or (fboundp sym)
282         (setq sym (function field-unifier-for-default))
283         )
284     (funcall sym a b)
285     ))
286
287
288 ;;; @@ type unifier
289 ;;;
290
291 (defun assoc-unify (class instance)
292   (catch 'tag
293     (let ((cla (copy-alist class))
294           (ins (copy-alist instance))
295           (r class)
296           cell aret ret prev rest)
297       (while r
298         (setq cell (car r))
299         (setq aret (assoc (car cell) ins))
300         (if aret
301             (if (setq ret (field-unify cell aret))
302                 (progn
303                   (if (car ret)
304                       (setq prev (put-alist (car (car ret))
305                                             (cdr (car ret))
306                                             prev))
307                     )
308                   (if (nth 2 ret)
309                       (setq rest (put-alist (car (nth 2 ret))
310                                             (cdr (nth 2 ret))
311                                             rest))
312                     )
313                   (setq cla (put-alist (car cell)(cdr (nth 1 ret)) cla))
314                   (setq ins (del-alist (car cell) ins))
315                   )
316               (throw 'tag nil)
317               ))
318         (setq r (cdr r))
319         )
320       (setq r (copy-alist ins))
321       (while r
322         (setq cell (car r))
323         (setq aret (assoc (car cell) cla))
324         (if aret
325             (if (setq ret (field-unify cell aret))
326                 (progn
327                   (if (car ret)
328                       (setq prev (put-alist (car (car ret))
329                                             (cdr (car ret))
330                                             prev))
331                     )
332                   (if (nth 2 ret)
333                       (setq rest (put-alist (car (nth 2 ret))
334                                             (cdr (nth 2 ret))
335                                             rest))
336                     )
337                   (setq cla (del-alist (car cell) cla))
338                   (setq ins (put-alist (car cell)(cdr (nth 1 ret)) ins))
339                   )
340               (throw 'tag nil)
341               ))
342         (setq r (cdr r))
343         )
344       (list prev (append cla ins) rest)
345       )))
346
347 (defun get-unified-alist (db al)
348   (let ((r db) ret)
349     (catch 'tag
350       (while r
351         (if (setq ret (nth 1 (assoc-unify (car r) al)))
352             (throw 'tag ret)
353           )
354         (setq r (cdr r))
355         ))))
356
357 (defun delete-atype (atl al)
358   (let* ((r atl) ret oal)
359     (setq oal
360           (catch 'tag
361             (while r
362               (if (setq ret (nth 1 (assoc-unify (car r) al)))
363                   (throw 'tag (car r))
364                 )
365               (setq r (cdr r))
366               )))
367     (delete oal atl)
368     ))
369
370 (defun remove-atype (sym al)
371   (and (boundp sym)
372        (set sym (delete-atype (eval sym) al))
373        ))
374
375 (defun replace-atype (atl old-al new-al)
376   (let* ((r atl) ret oal)
377     (if (catch 'tag
378           (while r
379             (if (setq ret (nth 1 (assoc-unify (car r) old-al)))
380                 (throw 'tag (rplaca r new-al))
381               )
382             (setq r (cdr r))
383             ))
384         atl)))
385
386 (defun set-atype (sym al &rest options)
387   (if (null (boundp sym))
388       (set sym al)
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)))
393                          (mapcar (function
394                                   (lambda (type)
395                                     (setq ral (del-alist type ral))
396                                     ))
397                                  ignore-fields)
398                          ral)))
399            )
400       (set sym
401            (or (if replacement
402                    (replace-atype (eval sym) remove al)
403                  )
404                (cons al
405                      (delete-atype (eval sym) remove)
406                      )
407                )))))
408
409
410 ;;; @ rot13-47
411 ;;;
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))
421                  (list nil)))
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
425       (progn
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))
431               (while (< i 256)
432                 (aset caesar-translate-table i i)
433                 (setq i (1+ i)))
434               (setq lower (concat lower lower) upper (upcase lower) i 0)
435               (while (< i 26)
436                 (aset caesar-translate-table (+ ?a i) (aref lower (+ i n)))
437                 (aset caesar-translate-table (+ ?A i) (aref upper (+ i n)))
438                 (setq i (1+ i)))
439               ;; ROT47 for Japanese text.
440               ;; Thanks to ichikawa@flab.fujitsu.junet.
441               (setq i 161)
442               (let ((t1 (logior ?O 128))
443                     (t2 (logior ?! 128))
444                     (t3 (logior ?~ 128)))
445                 (while (< i 256)
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))))
450                   (setq i (1+ i))))
451               (message "Building caesar-translate-table...done")))
452         (let ((from (region-beginning))
453               (to (region-end))
454               (i 0) str len)
455           (setq str (buffer-substring from to))
456           (setq len (length str))
457           (while (< i len)
458             (aset str i (aref caesar-translate-table (aref str i)))
459             (setq i (1+ i)))
460           (goto-char from)
461           (delete-region from to)
462           (insert str)))))
463
464
465 ;;; @ field
466 ;;;
467
468 (defsubst regexp-or (&rest args)
469   (concat "\\(" (mapconcat (function identity) args "\\|") "\\)"))
470
471 (defun tm:set-fields (sym field-list &optional regexp-sym)
472   (or regexp-sym
473       (setq regexp-sym
474             (let ((name (symbol-name sym)))
475               (intern
476                (concat (if (string-match "\\(.*\\)-list" name)
477                            (substring name 0 (match-end 1))
478                          name)
479                        "-regexp")
480                )))
481       )
482   (set sym field-list)
483   (set regexp-sym
484        (concat "^" (apply (function regexp-or) field-list) ":"))
485   )
486
487 (defun tm:add-fields (sym field-list &optional regexp-sym)
488   (or regexp-sym
489       (setq regexp-sym
490             (let ((name (symbol-name sym)))
491               (intern
492                (concat (if (string-match "\\(.*\\)-list" name)
493                            (substring name 0 (match-end 1))
494                          name)
495                        "-regexp")
496                )))
497       )
498   (let ((fields (eval sym)))
499     (mapcar (function
500              (lambda (field)
501                (or (member field fields)
502                    (setq fields (cons field fields))
503                    )
504                ))
505             (reverse field-list)
506             )
507     (set regexp-sym
508          (concat "^" (apply (function regexp-or) fields) ":"))
509     (set sym fields)
510     ))
511
512 (defun tm:delete-fields (sym field-list &optional regexp-sym)
513   (or regexp-sym
514       (setq regexp-sym
515             (let ((name (symbol-name sym)))
516               (intern
517                (concat (if (string-match "\\(.*\\)-list" name)
518                            (substring name 0 (match-end 1))
519                          name)
520                        "-regexp")
521                )))
522       )
523   (let ((fields (eval sym)))
524     (mapcar (function
525              (lambda (field)
526                (setq fields (delete field fields))
527                ))
528             field-list)
529     (set regexp-sym
530          (concat "^" (apply (function regexp-or) fields) ":"))
531     (set sym fields)
532     ))
533
534
535 ;;; @ RCS version
536 ;;;
537
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))
542        ))
543
544
545 ;;; @ Other Utility
546 ;;;
547
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)
554       (funcall func)
555     (or hook-name
556         (setq hook-name (intern (concat (symbol-name module) "-load-hook")))
557         )
558     (add-hook hook-name func)
559     ))
560
561
562 ;;; @ end
563 ;;;
564
565 (provide 'mime-def)
566
567 ;;; mime-def.el ends here