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