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