APEL 3.4.
[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.44 1997/06/28 10:21:24 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 emulation
138 ;;;
139
140 (defun-maybe minibuffer-prompt-width ()
141   "Return the display width of the minibuffer prompt."
142   (save-excursion
143     (set-buffer (window-buffer (minibuffer-window)))
144     (current-column)
145     ))
146
147
148 ;;; @ Emacs 19.29 emulation
149 ;;;
150
151 (defvar path-separator ":"
152   "Character used to separate concatenated paths.")
153
154 (defun-maybe buffer-substring-no-properties (start end)
155   "Return the characters of part of the buffer, without the text properties.
156 The two arguments START and END are character positions;
157 they can be in either order. [Emacs 19.29 emulating function]"
158   (let ((string (buffer-substring start end)))
159     (set-text-properties 0 (length string) nil string)
160     string))
161
162 (defun-maybe match-string (num &optional string)
163   "Return string of text matched by last search.
164 NUM specifies which parenthesized expression in the last regexp.
165  Value is nil if NUMth pair didn't match, or there were less than NUM pairs.
166 Zero means the entire text matched by the whole regexp or whole string.
167 STRING should be given if the last search was by `string-match' on STRING.
168 \[Emacs 19.29 emulating function]"
169   (if (match-beginning num)
170       (if string
171           (substring string (match-beginning num) (match-end num))
172         (buffer-substring (match-beginning num) (match-end num)))))
173
174 (or running-emacs-19_29-or-later
175     running-xemacs
176     ;; for Emacs 19.28 or earlier
177     (fboundp 'si:read-string)
178     (progn
179       (fset 'si:read-string (symbol-function 'read-string))
180       
181       (defun read-string (prompt &optional initial-input history)
182         "Read a string from the minibuffer, prompting with string PROMPT.
183 If non-nil, second arg INITIAL-INPUT is a string to insert before reading.
184 The third arg HISTORY, is dummy for compatibility. [emu.el]
185 See `read-from-minibuffer' for details of HISTORY argument."
186         (si:read-string prompt initial-input)
187         )
188       ))
189
190
191 ;;; @ Emacs 19.30 emulation
192 ;;;
193
194 ;; This function was imported Emacs 19.30.
195 (defun-maybe add-to-list (list-var element)
196   "Add to the value of LIST-VAR the element ELEMENT if it isn't there yet.
197 If you want to use `add-to-list' on a variable that is not defined
198 until a certain package is loaded, you should put the call to `add-to-list'
199 into a hook function that will be run only after loading the package.
200 \[Emacs 19.30 emulating function]"
201   (or (member element (symbol-value list-var))
202       (set list-var (cons element (symbol-value list-var)))
203       ))
204
205 (cond ((fboundp 'insert-file-contents-literally)
206        )
207       ((boundp 'file-name-handler-alist)
208        (defun insert-file-contents-literally
209          (filename &optional visit beg end replace)
210          "Like `insert-file-contents', q.v., but only reads in the file.
211 A buffer may be modified in several ways after reading into the buffer due
212 to advanced Emacs features, such as file-name-handlers, format decoding,
213 find-file-hooks, etc.
214   This function ensures that none of these modifications will take place.
215 \[Emacs 19.30 emulating function]"
216          (let (file-name-handler-alist)
217            (insert-file-contents filename visit beg end replace)
218            ))
219        )
220       (t
221        (defalias 'insert-file-contents-literally 'insert-file-contents)
222        ))
223
224
225 ;;; @ Emacs 19.31 emulation
226 ;;;
227
228 (defun-maybe buffer-live-p (object)
229   "Return non-nil if OBJECT is a buffer which has not been killed.
230 Value is nil if OBJECT is not a buffer or if it has been killed.
231 \[Emacs 19.31 emulating function]"
232   (and object
233        (get-buffer object)
234        (buffer-name (get-buffer object))
235        ))
236
237 ;; This macro was imported Emacs 19.33.
238 (defmacro-maybe save-selected-window (&rest body)
239   "Execute BODY, then select the window that was selected before BODY.
240 \[Emacs 19.31 emulating function]"
241   (list 'let
242         '((save-selected-window-window (selected-window)))
243         (list 'unwind-protect
244               (cons 'progn body)
245               (list 'select-window 'save-selected-window-window))))
246
247
248 ;;; @ XEmacs emulation
249 ;;;
250
251 (defun-maybe functionp (obj)
252   "Returns t if OBJ is a function, nil otherwise.
253 \[XEmacs emulating function]"
254   (or (subrp obj)
255       (byte-code-function-p obj)
256       (and (symbolp obj)(fboundp obj))
257       (and (consp obj)(eq (car obj) 'lambda))
258       ))
259
260 (defun-maybe point-at-eol (&optional arg buffer)
261   "Return the character position of the last character on the current line.
262 With argument N not nil or 1, move forward N - 1 lines first.
263 If scan reaches end of buffer, return that position.
264 This function does not move point. [XEmacs emulating function]"
265   (save-excursion
266     (if buffer
267         (set-buffer buffer)
268       )
269     (if arg
270         (forward-line (1- arg))
271       )
272     (end-of-line)
273     (point)
274     ))
275
276
277 ;;; @ for XEmacs 20
278 ;;;
279
280 (or (fboundp 'char-int)
281     (fset 'char-int (symbol-function 'identity))
282     )
283 (or (fboundp 'int-char)
284     (fset 'int-char (symbol-function 'identity))
285     )
286 (or (fboundp 'char-or-char-int-p)
287     (fset 'char-or-char-int-p (symbol-function 'integerp))
288     )
289
290
291 ;;; @ for text/richtext and text/enriched
292 ;;;
293
294 (cond ((fboundp 'richtext-decode)
295        ;; have richtext.el
296        )
297       ((or running-emacs-19_29-or-later running-xemacs-19_14-or-later)
298        ;; have enriched.el
299        (autoload 'richtext-decode "richtext")
300        (or (assq 'text/richtext format-alist)
301            (setq format-alist
302                  (cons
303                   (cons 'text/richtext
304                         '("Extended MIME text/richtext format."
305                           "Content-[Tt]ype:[ \t]*text/richtext"
306                           richtext-decode richtext-encode t enriched-mode))
307                   format-alist)))
308        )
309       (t
310        ;; don't have enriched.el
311        (autoload 'richtext-decode "tinyrich")
312        (autoload 'enriched-decode "tinyrich")
313        ))
314
315
316 ;;; @ end
317 ;;;
318
319 (provide 'emu)
320
321 ;;; emu.el ends here