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