tm 7.105.2.
[elisp/apel.git] / emu.el
1 ;;; emu.el --- Emulation module for each Emacs variants
2
3 ;; Copyright (C) 1995,1996,1997 Free Software Foundation, Inc.
4
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; Version: $Id: emu.el,v 7.40 1997/03/06 21:35:37 morioka Exp $
7 ;; Keywords: emulation, compatibility, NEmacs, MULE, Emacs/mule, XEmacs
8
9 ;; This file is part of emu.
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 (defmacro defun-maybe (name &rest everything-else)
29   (or (and (fboundp name)
30            (not (get name 'defun-maybe))
31            )
32       (` (or (fboundp (quote (, name)))
33              (progn
34                (defun (, name) (,@ everything-else))
35                (put (quote (, name)) 'defun-maybe t)
36                ))
37          )))
38
39 (defmacro defmacro-maybe (name &rest everything-else)
40   (or (and (fboundp name)
41            (not (get name 'defmacro-maybe))
42            )
43       (` (or (fboundp (quote (, name)))
44              (progn
45                (defmacro (, name) (,@ everything-else))
46                (put (quote (, name)) 'defmacro-maybe t)
47                ))
48          )))
49
50 (put 'defun-maybe 'lisp-indent-function 'defun)
51 (put 'defmacro-maybe 'lisp-indent-function 'defun)
52
53
54 (or (boundp 'emacs-major-version)
55     (defconst emacs-major-version (string-to-int emacs-version)))
56 (or (boundp 'emacs-minor-version)
57     (defconst emacs-minor-version
58       (string-to-int
59        (substring
60         emacs-version
61         (string-match (format "%d\\." emacs-major-version) emacs-version)
62         ))))
63
64 (defvar running-emacs-18 (<= emacs-major-version 18))
65 (defvar running-xemacs (string-match "XEmacs" emacs-version))
66
67 (defvar running-mule-merged-emacs (and (not (boundp 'MULE))
68                                        (not running-xemacs) (featurep 'mule)))
69 (defvar running-xemacs-with-mule (and running-xemacs (featurep 'mule)))
70
71 (defvar running-emacs-19 (and (not running-xemacs) (= emacs-major-version 19)))
72 (defvar running-emacs-19_29-or-later
73   (or (and running-emacs-19 (>= emacs-minor-version 29))
74       (and (not running-xemacs)(>= emacs-major-version 20))))
75
76 (defvar running-xemacs-19 (and running-xemacs
77                                (= emacs-major-version 19)))
78 (defvar running-xemacs-20-or-later (and running-xemacs
79                                         (>= emacs-major-version 20)))
80 (defvar running-xemacs-19_14-or-later
81   (or (and running-xemacs-19 (>= emacs-minor-version 14))
82       running-xemacs-20-or-later))
83
84 (cond (running-mule-merged-emacs
85        ;; for mule merged EMACS
86        (require 'emu-e20)
87        )
88       (running-xemacs-with-mule
89        ;; for XEmacs/mule
90        (require 'emu-x20)
91        )
92       ((boundp 'MULE)
93        ;; for MULE 1.* and 2.*
94        (require 'emu-mule)
95        )
96       ((boundp 'NEMACS)
97        ;; for NEmacs and NEpoch
98        (require 'emu-nemacs)
99        )
100       (t
101        ;; for EMACS 19 and XEmacs 19 (without mule)
102        (require 'emu-e19)
103        ))
104
105
106 ;;; @ MIME charset
107 ;;;
108
109 (defun charsets-to-mime-charset (charsets)
110   "Return MIME charset from list of charset CHARSETS.
111 This function refers variable `charsets-mime-charset-alist'
112 and `default-mime-charset'. [emu.el]"
113   (if charsets
114       (or (catch 'tag
115             (let ((rest charsets-mime-charset-alist)
116                   cell csl)
117               (while (setq cell (car rest))
118                 (if (catch 'not-subset
119                       (let ((set1 charsets)
120                             (set2 (car cell))
121                             obj)
122                         (while set1
123                           (setq obj (car set1))
124                           (or (memq obj set2)
125                               (throw 'not-subset nil)
126                               )
127                           (setq set1 (cdr set1))
128                           )
129                         t))
130                     (throw 'tag (cdr cell))
131                   )
132                 (setq rest (cdr rest))
133                 )))
134           default-mime-charset)))
135
136
137 ;;; @ Emacs 19.29 emulation
138 ;;;
139
140 (defvar path-separator ":"
141   "Character used to separate concatenated paths.")
142
143 (defun-maybe buffer-substring-no-properties (start end)
144   "Return the characters of part of the buffer, without the text properties.
145 The two arguments START and END are character positions;
146 they can be in either order. [Emacs 19.29 emulating function]"
147   (let ((string (buffer-substring start end)))
148     (set-text-properties 0 (length string) nil string)
149     string))
150
151 (defun-maybe match-string (num &optional string)
152   "Return string of text matched by last search.
153 NUM specifies which parenthesized expression in the last regexp.
154  Value is nil if NUMth pair didn't match, or there were less than NUM pairs.
155 Zero means the entire text matched by the whole regexp or whole string.
156 STRING should be given if the last search was by `string-match' on STRING.
157 \[Emacs 19.29 emulating function]"
158   (if (match-beginning num)
159       (if string
160           (substring string (match-beginning num) (match-end num))
161         (buffer-substring (match-beginning num) (match-end num)))))
162
163 (or running-emacs-19_29-or-later
164     running-xemacs
165     ;; for Emacs 19.28 or earlier
166     (fboundp 'si:read-string)
167     (progn
168       (fset 'si:read-string (symbol-function 'read-string))
169       
170       (defun read-string (prompt &optional initial-input history)
171         "Read a string from the minibuffer, prompting with string PROMPT.
172 If non-nil, second arg INITIAL-INPUT is a string to insert before reading.
173 The third arg HISTORY, is dummy for compatibility. [emu.el]
174 See `read-from-minibuffer' for details of HISTORY argument."
175         (si:read-string prompt initial-input)
176         )
177       ))
178
179
180 ;;; @ Emacs 19.30 emulation
181 ;;;
182
183 ;; This function was imported Emacs 19.30.
184 (defun-maybe add-to-list (list-var element)
185   "Add to the value of LIST-VAR the element ELEMENT if it isn't there yet.
186 If you want to use `add-to-list' on a variable that is not defined
187 until a certain package is loaded, you should put the call to `add-to-list'
188 into a hook function that will be run only after loading the package.
189 \[Emacs 19.30 emulating function]"
190   (or (member element (symbol-value list-var))
191       (set list-var (cons element (symbol-value list-var)))
192       ))
193
194 (cond ((fboundp 'insert-file-contents-literally)
195        )
196       ((boundp 'file-name-handler-alist)
197        (defun insert-file-contents-literally
198          (filename &optional visit beg end replace)
199          "Like `insert-file-contents', q.v., but only reads in the file.
200 A buffer may be modified in several ways after reading into the buffer due
201 to advanced Emacs features, such as file-name-handlers, format decoding,
202 find-file-hooks, etc.
203   This function ensures that none of these modifications will take place.
204 \[Emacs 19.30 emulating function]"
205          (let (file-name-handler-alist)
206            (insert-file-contents filename visit beg end replace)
207            ))
208        )
209       (t
210        (defalias 'insert-file-contents-literally 'insert-file-contents)
211        ))
212
213
214 ;;; @ Emacs 19.31 emulation
215 ;;;
216
217 (defun-maybe buffer-live-p (object)
218   "Return non-nil if OBJECT is a buffer which has not been killed.
219 Value is nil if OBJECT is not a buffer or if it has been killed.
220 \[Emacs 19.31 emulating function]"
221   (and object
222        (get-buffer object)
223        (buffer-name (get-buffer object))
224        ))
225
226 ;; This macro was imported Emacs 19.33.
227 (defmacro-maybe save-selected-window (&rest body)
228   "Execute BODY, then select the window that was selected before BODY.
229 \[Emacs 19.31 emulating function]"
230   (list 'let
231         '((save-selected-window-window (selected-window)))
232         (list 'unwind-protect
233               (cons 'progn body)
234               (list 'select-window 'save-selected-window-window))))
235
236
237 ;;; @ XEmacs emulation
238 ;;;
239
240 (defun-maybe functionp (obj)
241   "Returns t if OBJ is a function, nil otherwise.
242 \[XEmacs emulating function]"
243   (or (subrp obj)
244       (byte-code-function-p obj)
245       (and (symbolp obj)(fboundp obj))
246       (and (consp obj)(eq (car obj) 'lambda))
247       ))
248
249
250 ;;; @ for XEmacs 20
251 ;;;
252
253 (or (fboundp 'char-int)
254     (fset 'char-int (symbol-function 'identity))
255     )
256 (or (fboundp 'int-char)
257     (fset 'int-char (symbol-function 'identity))
258     )
259
260
261 ;;; @ for text/richtext and text/enriched
262 ;;;
263
264 (cond ((or running-emacs-19_29-or-later running-xemacs-19_14-or-later)
265        ;; have enriched.el
266        (autoload 'richtext-decode "richtext")
267        (or (assq 'text/richtext format-alist)
268            (setq format-alist
269                  (cons
270                   (cons 'text/richtext
271                         '("Extended MIME text/richtext format."
272                           "Content-[Tt]ype:[ \t]*text/richtext"
273                           richtext-decode richtext-encode t enriched-mode))
274                   format-alist)))
275        )
276       (t
277        ;; don't have enriched.el
278        (autoload 'richtext-decode "tinyrich")
279        (autoload 'enriched-decode "tinyrich")
280        ))
281
282
283 ;;; @ end
284 ;;;
285
286 (provide 'emu)
287
288 ;;; emu.el ends here