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