(mime-view-define-keymap): `tm:button-dispatcher' ->
[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.22 1997-02-26 04:31:06 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 (require 'emu)
29
30
31 ;;; @ variables
32 ;;;
33
34 (defvar mime/tmp-dir (or (getenv "TM_TMP_DIR") "/tmp/"))
35
36 (defvar mime/use-multi-frame
37   (and (>= emacs-major-version 19) window-system))
38
39 (defvar mime/find-file-function
40   (if mime/use-multi-frame
41       (function find-file-other-frame)
42     (function find-file)
43     ))
44
45 (defvar mime/output-buffer-window-is-shared-with-bbdb t
46   "*If t, mime/output-buffer window is shared with BBDB window.")
47
48
49 ;;; @ constants
50 ;;;
51
52 (defconst mime/output-buffer-name "*MIME-out*")
53 (defconst mime/temp-buffer-name " *MIME-temp*")
54
55
56 ;;; @ button
57 ;;;
58
59 (defvar mime-button-face 'bold
60   "Face used for content-button or URL-button of MIME-Preview buffer.")
61
62 (defvar mime-button-mouse-face 'highlight
63   "Face used for MIME-preview buffer mouse highlighting.")
64
65 (defun tm:add-button (from to func &optional data)
66   "Create a button between FROM and TO with callback FUNC and data DATA."
67   (and mime-button-face
68        (overlay-put (make-overlay from to) 'face mime-button-face))
69   (tl:add-text-properties from to
70                           (nconc
71                            (and mime-button-mouse-face
72                                 (list 'mouse-face mime-button-mouse-face))
73                            (list 'mime-button-callback func)
74                            (and data (list 'mime-button-data data))
75                            ))
76   )
77
78 (defvar mime-button-mother-dispatcher nil)
79
80 (defun mime-button-dispatcher (event)
81   "Select the button under point."
82   (interactive "e")
83   (let (buf point func data)
84     (save-window-excursion
85       (mouse-set-point event)
86       (setq buf (current-buffer)
87             point (point)
88             func (get-text-property (point) 'mime-button-callback)
89             data (get-text-property (point) 'mime-button-data)
90             )
91       )
92     (save-excursion
93       (set-buffer buf)
94       (goto-char point)
95       (if func
96           (apply func data)
97         (if (fboundp mime-button-mother-dispatcher)
98             (funcall mime-button-mother-dispatcher event)
99           )
100         ))))
101
102
103 ;;; @ PGP
104 ;;;
105
106 (defvar pgp-function-alist
107   '(
108     ;; for mime-pgp
109     (verify             mc-verify                       "mc-toplev")
110     (decrypt            mc-decrypt                      "mc-toplev")
111     (fetch-key          mc-pgp-fetch-key                "mc-pgp")
112     (snarf-keys         mc-snarf-keys                   "mc-toplev")
113     ;; for mime-edit
114     (mime-sign          tm:mc-pgp-sign-region           "mime-edit-mc")
115     (traditional-sign   mc-pgp-sign-region              "mc-pgp")
116     (encrypt            tm:mc-pgp-encrypt-region        "mime-edit-mc")
117     (insert-key         mc-insert-public-key            "mc-toplev")
118     )
119   "Alist of service names vs. corresponding functions and its filenames.
120 Each element looks like (SERVICE FUNCTION FILE).
121
122 SERVICE is a symbol of PGP processing.  It allows `verify', `decrypt',
123 `fetch-key', `snarf-keys', `mime-sign', `traditional-sign', `encrypt'
124 or `insert-key'.
125
126 Function is a symbol of function to do specified SERVICE.
127
128 FILE is string of filename which has definition of corresponding
129 FUNCTION.")
130
131 (defmacro pgp-function (method)
132   "Return function to do service METHOD."
133   (` (car (cdr (assq (, method) (symbol-value 'pgp-function-alist)))))
134   )
135
136 (mapcar (function
137          (lambda (method)
138            (autoload (second method)(third method))
139            ))
140         pgp-function-alist)
141
142
143 ;;; @ definitions about MIME
144 ;;;
145
146 (defconst mime/tspecials "][\000-\040()<>@,\;:\\\"/?.=")
147 (defconst mime/token-regexp (concat "[^" mime/tspecials "]+"))
148 (defconst mime-charset-regexp mime/token-regexp)
149
150 (defconst mime/content-type-subtype-regexp
151   (concat mime/token-regexp "/" mime/token-regexp))
152
153 (defconst mime/disposition-type-regexp mime/token-regexp)
154
155
156 ;;; @ rot13-47
157 ;;;
158 ;; caesar-region written by phr@prep.ai.mit.edu  Nov 86
159 ;; modified by tower@prep Nov 86
160 ;; gnus-caesar-region
161 ;; Modified by umerin@flab.flab.Fujitsu.JUNET for ROT47.
162 (defun tm:caesar-region (&optional n)
163   "Caesar rotation of region by N, default 13, for decrypting netnews.
164 ROT47 will be performed for Japanese text in any case."
165   (interactive (if current-prefix-arg   ; Was there a prefix arg?
166                    (list (prefix-numeric-value current-prefix-arg))
167                  (list nil)))
168   (cond ((not (numberp n)) (setq n 13))
169         (t (setq n (mod n 26))))        ;canonicalize N
170   (if (not (zerop n))           ; no action needed for a rot of 0
171       (progn
172         (if (or (not (boundp 'caesar-translate-table))
173                 (/= (aref caesar-translate-table ?a) (+ ?a n)))
174             (let ((i 0) (lower "abcdefghijklmnopqrstuvwxyz") upper)
175               (message "Building caesar-translate-table...")
176               (setq caesar-translate-table (make-vector 256 0))
177               (while (< i 256)
178                 (aset caesar-translate-table i i)
179                 (setq i (1+ i)))
180               (setq lower (concat lower lower) upper (upcase lower) i 0)
181               (while (< i 26)
182                 (aset caesar-translate-table (+ ?a i) (aref lower (+ i n)))
183                 (aset caesar-translate-table (+ ?A i) (aref upper (+ i n)))
184                 (setq i (1+ i)))
185               ;; ROT47 for Japanese text.
186               ;; Thanks to ichikawa@flab.fujitsu.junet.
187               (setq i 161)
188               (let ((t1 (logior ?O 128))
189                     (t2 (logior ?! 128))
190                     (t3 (logior ?~ 128)))
191                 (while (< i 256)
192                   (aset caesar-translate-table i
193                         (let ((v (aref caesar-translate-table i)))
194                           (if (<= v t1) (if (< v t2) v (+ v 47))
195                             (if (<= v t3) (- v 47) v))))
196                   (setq i (1+ i))))
197               (message "Building caesar-translate-table...done")))
198         (let ((from (region-beginning))
199               (to (region-end))
200               (i 0) str len)
201           (setq str (buffer-substring from to))
202           (setq len (length str))
203           (while (< i len)
204             (aset str i (aref caesar-translate-table (aref str i)))
205             (setq i (1+ i)))
206           (goto-char from)
207           (delete-region from to)
208           (insert str)))))
209
210
211 ;;; @ field
212 ;;;
213
214 (defun tm:set-fields (sym field-list &optional regexp-sym)
215   (or regexp-sym
216       (setq regexp-sym
217             (let ((name (symbol-name sym)))
218               (intern
219                (concat (if (string-match "\\(.*\\)-list" name)
220                            (substring name 0 (match-end 1))
221                          name)
222                        "-regexp")
223                )))
224       )
225   (set sym field-list)
226   (set regexp-sym
227        (concat "^" (apply (function regexp-or) field-list) ":"))
228   )
229
230 (defun tm:add-fields (sym field-list &optional regexp-sym)
231   (or regexp-sym
232       (setq regexp-sym
233             (let ((name (symbol-name sym)))
234               (intern
235                (concat (if (string-match "\\(.*\\)-list" name)
236                            (substring name 0 (match-end 1))
237                          name)
238                        "-regexp")
239                )))
240       )
241   (let ((fields (eval sym)))
242     (mapcar (function
243              (lambda (field)
244                (or (member field fields)
245                    (setq fields (cons field fields))
246                    )
247                ))
248             (reverse field-list)
249             )
250     (set regexp-sym
251          (concat "^" (apply (function regexp-or) fields) ":"))
252     (set sym fields)
253     ))
254
255 (defun tm:delete-fields (sym field-list &optional regexp-sym)
256   (or regexp-sym
257       (setq regexp-sym
258             (let ((name (symbol-name sym)))
259               (intern
260                (concat (if (string-match "\\(.*\\)-list" name)
261                            (substring name 0 (match-end 1))
262                          name)
263                        "-regexp")
264                )))
265       )
266   (let ((fields (eval sym)))
267     (mapcar (function
268              (lambda (field)
269                (setq fields (delete field fields))
270                ))
271             field-list)
272     (set regexp-sym
273          (concat "^" (apply (function regexp-or) fields) ":"))
274     (set sym fields)
275     ))
276
277
278 ;;; @ RCS version
279 ;;;
280
281 (defsubst get-version-string (id)
282   "Return a version-string from RCS ID."
283   (and (string-match ",v \\([0-9][0-9.][0-9.]+\\)" id)
284        (substring id (match-beginning 1)(match-end 1))
285        ))
286
287
288 ;;; @ end
289 ;;;
290
291 (provide 'mime-def)
292
293 ;;; mime-def.el ends here