Require `emu-20' in emu-x20.el.
[elisp/apel.git] / emu.el
1 ;;; emu.el --- Emulation module for each Emacs variants
2
3 ;; Copyright (C) 1995,1996,1997,1998 Free Software Foundation, Inc.
4
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; Keywords: emulation, compatibility, NEmacs, MULE, Emacs/mule, XEmacs
7
8 ;; This file is part of emu.
9
10 ;; This program is free software; you can redistribute it and/or
11 ;; modify it under the terms of the GNU General Public License as
12 ;; published by the Free Software Foundation; either version 2, or (at
13 ;; your option) any later version.
14
15 ;; This program is distributed in the hope that it will be useful, but
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 ;; General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Code:
26
27 (defmacro defun-maybe (name &rest everything-else)
28   (or (and (fboundp name)
29            (not (get name 'defun-maybe))
30            )
31       (` (or (fboundp (quote (, name)))
32              (progn
33                (defun (, name) (,@ everything-else))
34                (put (quote (, name)) 'defun-maybe t)
35                ))
36          )))
37
38 (defmacro defsubst-maybe (name &rest everything-else)
39   (or (and (fboundp name)
40            (not (get name 'defsubst-maybe))
41            )
42       (` (or (fboundp (quote (, name)))
43              (progn
44                (defsubst (, name) (,@ everything-else))
45                (put (quote (, name)) 'defsubst-maybe t)
46                ))
47          )))
48
49 (defmacro defmacro-maybe (name &rest everything-else)
50   (or (and (fboundp name)
51            (not (get name 'defmacro-maybe))
52            )
53       (` (or (fboundp (quote (, name)))
54              (progn
55                (defmacro (, name) (,@ everything-else))
56                (put (quote (, name)) 'defmacro-maybe t)
57                ))
58          )))
59
60 (put 'defun-maybe 'lisp-indent-function 'defun)
61 (put 'defsubst-maybe 'lisp-indent-function 'defun)
62 (put 'defmacro-maybe 'lisp-indent-function 'defun)
63
64 (defmacro defconst-maybe (name &rest everything-else)
65   (or (and (boundp name)
66            (not (get name 'defconst-maybe))
67            )
68       (` (or (boundp (quote (, name)))
69              (progn
70                (defconst (, name) (,@ everything-else))
71                (put (quote (, name)) 'defconst-maybe t)
72                ))
73          )))
74
75
76 (defconst-maybe emacs-major-version (string-to-int emacs-version))
77 (defconst-maybe emacs-minor-version
78   (string-to-int
79    (substring emacs-version
80               (string-match (format "%d\\." emacs-major-version)
81                             emacs-version))))
82
83 (defvar running-emacs-18 (<= emacs-major-version 18))
84 (defvar running-xemacs (string-match "XEmacs" emacs-version))
85
86 (defvar running-mule-merged-emacs (and (not (boundp 'MULE))
87                                        (not running-xemacs) (featurep 'mule)))
88 (defvar running-xemacs-with-mule (and running-xemacs (featurep 'mule)))
89
90 (defvar running-emacs-19 (and (not running-xemacs) (= emacs-major-version 19)))
91 (defvar running-emacs-19_29-or-later
92   (or (and running-emacs-19 (>= emacs-minor-version 29))
93       (and (not running-xemacs)(>= emacs-major-version 20))))
94
95 (defvar running-xemacs-19 (and running-xemacs
96                                (= emacs-major-version 19)))
97 (defvar running-xemacs-20-or-later (and running-xemacs
98                                         (>= emacs-major-version 20)))
99 (defvar running-xemacs-19_14-or-later
100   (or (and running-xemacs-19 (>= emacs-minor-version 14))
101       running-xemacs-20-or-later))
102
103 (cond (running-xemacs
104        ;; for XEmacs
105        (require 'emu-xemacs)
106        (if (featurep 'mule)
107            ;; for XEmacs with MULE
108            (require 'emu-x20)
109          ;; for XEmacs without MULE
110          (require 'emu-latin1)
111          ))
112       (running-mule-merged-emacs
113        ;; for Emacs 20.1 and 20.2
114        (require 'emu-e20)
115        )
116       ((boundp 'MULE)
117        ;; for MULE 1.* and 2.*
118        (require 'emu-mule)
119        )
120       ((boundp 'NEMACS)
121        ;; for NEmacs and NEpoch
122        (require 'emu-nemacs)
123        )
124       (t
125        ;; for Emacs 19
126        (require 'emu-e19)
127        (require 'emu-latin1)
128        ))
129
130
131 ;;; @ MIME charset
132 ;;;
133
134 (defun charsets-to-mime-charset (charsets)
135   "Return MIME charset from list of charset CHARSETS.
136 This function refers variable `charsets-mime-charset-alist'
137 and `default-mime-charset'."
138   (if charsets
139       (or (catch 'tag
140             (let ((rest charsets-mime-charset-alist)
141                   cell)
142               (while (setq cell (car rest))
143                 (if (catch 'not-subset
144                       (let ((set1 charsets)
145                             (set2 (car cell))
146                             obj)
147                         (while set1
148                           (setq obj (car set1))
149                           (or (memq obj set2)
150                               (throw 'not-subset nil)
151                               )
152                           (setq set1 (cdr set1))
153                           )
154                         t))
155                     (throw 'tag (cdr cell))
156                   )
157                 (setq rest (cdr rest))
158                 )))
159           default-mime-charset)))
160
161
162 ;;; @ Emacs 19 emulation
163 ;;;
164
165 (defun-maybe minibuffer-prompt-width ()
166   "Return the display width of the minibuffer prompt."
167   (save-excursion
168     (set-buffer (window-buffer (minibuffer-window)))
169     (current-column)
170     ))
171
172
173 ;;; @ Emacs 19.29 emulation
174 ;;;
175
176 (defvar path-separator ":"
177   "Character used to separate concatenated paths.")
178
179 (defun-maybe buffer-substring-no-properties (start end)
180   "Return the characters of part of the buffer, without the text properties.
181 The two arguments START and END are character positions;
182 they can be in either order. [Emacs 19.29 emulating function]"
183   (let ((string (buffer-substring start end)))
184     (set-text-properties 0 (length string) nil string)
185     string))
186
187 (defun-maybe match-string (num &optional string)
188   "Return string of text matched by last search.
189 NUM specifies which parenthesized expression in the last regexp.
190  Value is nil if NUMth pair didn't match, or there were less than NUM pairs.
191 Zero means the entire text matched by the whole regexp or whole string.
192 STRING should be given if the last search was by `string-match' on STRING.
193 \[Emacs 19.29 emulating function]"
194   (if (match-beginning num)
195       (if string
196           (substring string (match-beginning num) (match-end num))
197         (buffer-substring (match-beginning num) (match-end num)))))
198
199 (or running-emacs-19_29-or-later
200     running-xemacs
201     ;; for Emacs 19.28 or earlier
202     (fboundp 'si:read-string)
203     (progn
204       (fset 'si:read-string (symbol-function 'read-string))
205       
206       (defun read-string (prompt &optional initial-input history)
207         "Read a string from the minibuffer, prompting with string PROMPT.
208 If non-nil, second arg INITIAL-INPUT is a string to insert before reading.
209 The third arg HISTORY, is dummy for compatibility. [emu.el]
210 See `read-from-minibuffer' for details of HISTORY argument."
211         (si:read-string prompt initial-input)
212         )
213       ))
214
215
216 ;;; @ Emacs 19.30 emulation
217 ;;;
218
219 ;; This function was imported Emacs 19.30.
220 (defun-maybe add-to-list (list-var element)
221   "Add to the value of LIST-VAR the element ELEMENT if it isn't there yet.
222 If you want to use `add-to-list' on a variable that is not defined
223 until a certain package is loaded, you should put the call to `add-to-list'
224 into a hook function that will be run only after loading the package.
225 \[Emacs 19.30 emulating function]"
226   (or (member element (symbol-value list-var))
227       (set list-var (cons element (symbol-value list-var)))
228       ))
229
230 (cond ((fboundp 'insert-file-contents-literally)
231        )
232       ((boundp 'file-name-handler-alist)
233        (defun insert-file-contents-literally
234          (filename &optional visit beg end replace)
235          "Like `insert-file-contents', q.v., but only reads in the file.
236 A buffer may be modified in several ways after reading into the buffer due
237 to advanced Emacs features, such as file-name-handlers, format decoding,
238 find-file-hooks, etc.
239   This function ensures that none of these modifications will take place.
240 \[Emacs 19.30 emulating function]"
241          (let (file-name-handler-alist)
242            (insert-file-contents filename visit beg end replace)
243            ))
244        )
245       (t
246        (defalias 'insert-file-contents-literally 'insert-file-contents)
247        ))
248
249
250 ;;; @ Emacs 19.31 emulation
251 ;;;
252
253 (defun-maybe buffer-live-p (object)
254   "Return non-nil if OBJECT is a buffer which has not been killed.
255 Value is nil if OBJECT is not a buffer or if it has been killed.
256 \[Emacs 19.31 emulating function]"
257   (and object
258        (get-buffer object)
259        (buffer-name (get-buffer object))
260        ))
261
262 ;; This macro was imported Emacs 19.33.
263 (defmacro-maybe save-selected-window (&rest body)
264   "Execute BODY, then select the window that was selected before BODY.
265 \[Emacs 19.31 emulating function]"
266   (list 'let
267         '((save-selected-window-window (selected-window)))
268         (list 'unwind-protect
269               (cons 'progn body)
270               (list 'select-window 'save-selected-window-window))))
271
272
273 ;;; @ Emacs 20.1 emulation
274 ;;;
275
276 ;; This macro was imported Emacs 20.2.
277 (defmacro-maybe when (cond &rest body)
278   "(when COND BODY...): if COND yields non-nil, do BODY, else return nil."
279   (list 'if cond (cons 'progn body)))
280
281 (defmacro-maybe save-current-buffer (&rest body)
282   "Save the current buffer; execute BODY; restore the current buffer.
283 Executes BODY just like `progn'."
284   (` (let ((orig-buffer (current-buffer)))
285        (unwind-protect
286            (progn (,@ body))
287          (set-buffer orig-buffer)))))
288
289 ;; This macro was imported Emacs 20.2.
290 (defmacro-maybe with-current-buffer (buffer &rest body)
291   "Execute the forms in BODY with BUFFER as the current buffer.
292 The value returned is the value of the last form in BODY.
293 See also `with-temp-buffer'."
294   (` (save-current-buffer
295        (set-buffer (, buffer))
296        (,@ body))))
297
298 ;; This macro was imported Emacs 20.2.
299 (defmacro-maybe with-temp-buffer (&rest forms)
300   "Create a temporary buffer, and evaluate FORMS there like `progn'.
301 See also `with-temp-file' and `with-output-to-string'."
302   (let ((temp-buffer (make-symbol "temp-buffer")))
303     (` (let (((, temp-buffer)
304               (get-buffer-create (generate-new-buffer-name " *temp*"))))
305          (unwind-protect
306              (with-current-buffer (, temp-buffer)
307                (,@ forms))
308            (and (buffer-name (, temp-buffer))
309                 (kill-buffer (, temp-buffer))))))))
310
311 ;; This function was imported from XEmacs 21.
312 (defun-maybe split-string (string &optional pattern)
313   "Return a list of substrings of STRING which are separated by PATTERN.
314 If PATTERN is omitted, it defaults to \"[ \\f\\t\\n\\r\\v]+\"."
315   (or pattern
316       (setq pattern "[ \f\t\n\r\v]+"))
317   ;; The FSF version of this function takes care not to cons in case
318   ;; of infloop.  Maybe we should synch?
319   (let (parts (start 0))
320     (while (string-match pattern string start)
321       (setq parts (cons (substring string start (match-beginning 0)) parts)
322             start (match-end 0)))
323     (nreverse (cons (substring string start) parts))))
324
325
326 ;;; @ Emacs 20.3 emulation
327 ;;;
328
329 (defmacro-maybe string-as-unibyte (string)
330   "Return a unibyte string with the same individual bytes as STRING.
331 If STRING is unibyte, the result is STRING itself.
332 \[Emacs 20.3 emulating macro]"
333   string)
334
335 (defmacro-maybe string-as-multibyte (string)
336   "Return a multibyte string with the same individual bytes as STRING.
337 If STRING is multibyte, the result is STRING itself.
338 \[Emacs 20.3 emulating macro]"
339   string)
340
341
342 ;;; @ XEmacs emulation
343 ;;;
344
345 (defun-maybe functionp (obj)
346   "Returns t if OBJ is a function, nil otherwise.
347 \[XEmacs emulating function]"
348   (or (subrp obj)
349       (byte-code-function-p obj)
350       (and (symbolp obj)(fboundp obj))
351       (and (consp obj)(eq (car obj) 'lambda))
352       ))
353
354 (defun-maybe point-at-eol (&optional arg buffer)
355   "Return the character position of the last character on the current line.
356 With argument N not nil or 1, move forward N - 1 lines first.
357 If scan reaches end of buffer, return that position.
358 This function does not move point. [XEmacs emulating function]"
359   (save-excursion
360     (if buffer
361         (set-buffer buffer)
362       )
363     (if arg
364         (forward-line (1- arg))
365       )
366     (end-of-line)
367     (point)
368     ))
369
370
371 ;;; @ for XEmacs 20
372 ;;;
373
374 (or (fboundp 'char-int)
375     (fset 'char-int (symbol-function 'identity))
376     )
377 (or (fboundp 'int-char)
378     (fset 'int-char (symbol-function 'identity))
379     )
380 (or (fboundp 'char-or-char-int-p)
381     (fset 'char-or-char-int-p (symbol-function 'integerp))
382     )
383
384
385 ;;; @ for text/richtext and text/enriched
386 ;;;
387
388 (cond ((fboundp 'richtext-decode)
389        ;; have richtext.el
390        )
391       ((or running-emacs-19_29-or-later running-xemacs-19_14-or-later)
392        ;; have enriched.el
393        (autoload 'richtext-decode "richtext")
394        (or (assq 'text/richtext format-alist)
395            (setq format-alist
396                  (cons
397                   (cons 'text/richtext
398                         '("Extended MIME text/richtext format."
399                           "Content-[Tt]ype:[ \t]*text/richtext"
400                           richtext-decode richtext-encode t enriched-mode))
401                   format-alist)))
402        )
403       (t
404        ;; don't have enriched.el
405        (autoload 'richtext-decode "tinyrich")
406        (autoload 'enriched-decode "tinyrich")
407        ))
408
409
410 ;;; @ end
411 ;;;
412
413 (provide 'emu)
414
415 ;;; emu.el ends here