1 ;;; emu.el --- Emulation module for each Emacs variants
3 ;; Copyright (C) 1995,1996,1997,1998 Free Software Foundation, Inc.
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; Keywords: emulation, compatibility, NEmacs, MULE, Emacs/mule, XEmacs
8 ;; This file is part of emu.
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.
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.
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.
27 (defmacro defun-maybe (name &rest everything-else)
28 (or (and (fboundp name)
29 (not (get name 'defun-maybe))
31 (` (or (fboundp (quote (, name)))
33 (defun (, name) (,@ everything-else))
34 (put (quote (, name)) 'defun-maybe t)
38 (defmacro defsubst-maybe (name &rest everything-else)
39 (or (and (fboundp name)
40 (not (get name 'defsubst-maybe))
42 (` (or (fboundp (quote (, name)))
44 (defsubst (, name) (,@ everything-else))
45 (put (quote (, name)) 'defsubst-maybe t)
49 (defmacro defmacro-maybe (name &rest everything-else)
50 (or (and (fboundp name)
51 (not (get name 'defmacro-maybe))
53 (` (or (fboundp (quote (, name)))
55 (defmacro (, name) (,@ everything-else))
56 (put (quote (, name)) 'defmacro-maybe t)
60 (put 'defun-maybe 'lisp-indent-function 'defun)
61 (put 'defsubst-maybe 'lisp-indent-function 'defun)
62 (put 'defmacro-maybe 'lisp-indent-function 'defun)
64 (defmacro defconst-maybe (name &rest everything-else)
65 (or (and (boundp name)
66 (not (get name 'defconst-maybe))
68 (` (or (boundp (quote (, name)))
70 (defconst (, name) (,@ everything-else))
71 (put (quote (, name)) 'defconst-maybe t)
76 (defconst-maybe emacs-major-version (string-to-int emacs-version))
77 (defconst-maybe emacs-minor-version
79 (substring emacs-version
80 (string-match (format "%d\\." emacs-major-version)
83 (defvar running-emacs-18 (<= emacs-major-version 18))
84 (defvar running-xemacs (string-match "XEmacs" emacs-version))
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)))
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))))
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))
103 (cond (running-xemacs
105 (require 'emu-xemacs)
107 ;; for XEmacs with MULE
109 ;; for XEmacs without MULE
110 (require 'emu-latin1)
112 (running-mule-merged-emacs
113 ;; for Emacs 20.1 and 20.2
117 ;; for MULE 1.* and 2.*
121 ;; for NEmacs and NEpoch
122 (require 'emu-nemacs)
127 (require 'emu-latin1)
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'."
140 (let ((rest charsets-mime-charset-alist)
142 (while (setq cell (car rest))
143 (if (catch 'not-subset
144 (let ((set1 charsets)
148 (setq obj (car set1))
150 (throw 'not-subset nil))
151 (setq set1 (cdr set1)))
153 (throw 'tag (cdr cell)))
154 (setq rest (cdr rest)))))
155 default-mime-charset)))
158 ;;; @ Emacs 19 emulation
161 (defun-maybe minibuffer-prompt-width ()
162 "Return the display width of the minibuffer prompt."
164 (set-buffer (window-buffer (minibuffer-window)))
168 ;;; @ Emacs 19.29 emulation
171 (defvar path-separator ":"
172 "Character used to separate concatenated paths.")
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)
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)
191 (substring string (match-beginning num) (match-end num))
192 (buffer-substring (match-beginning num) (match-end num)))))
194 (or running-emacs-19_29-or-later
196 ;; for Emacs 19.28 or earlier
197 (fboundp 'si:read-string)
199 (fset 'si:read-string (symbol-function 'read-string))
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))
210 ;;; @ Emacs 19.30 emulation
213 ;; This function was imported Emacs 19.30.
214 (defun-maybe add-to-list (list-var element)
215 "Add to the value of LIST-VAR the element ELEMENT if it isn't there yet.
216 If you want to use `add-to-list' on a variable that is not defined
217 until a certain package is loaded, you should put the call to `add-to-list'
218 into a hook function that will be run only after loading the package.
219 \[Emacs 19.30 emulating function]"
220 (or (member element (symbol-value list-var))
221 (set list-var (cons element (symbol-value list-var)))))
223 (cond ((fboundp 'insert-file-contents-literally)
225 ((boundp 'file-name-handler-alist)
226 (defun insert-file-contents-literally
227 (filename &optional visit beg end replace)
228 "Like `insert-file-contents', q.v., but only reads in the file.
229 A buffer may be modified in several ways after reading into the buffer due
230 to advanced Emacs features, such as file-name-handlers, format decoding,
231 find-file-hooks, etc.
232 This function ensures that none of these modifications will take place.
233 \[Emacs 19.30 emulating function]"
234 (let (file-name-handler-alist)
235 (insert-file-contents filename visit beg end replace)))
238 (defalias 'insert-file-contents-literally 'insert-file-contents)
242 ;;; @ Emacs 19.31 emulation
245 (defun-maybe buffer-live-p (object)
246 "Return non-nil if OBJECT is a buffer which has not been killed.
247 Value is nil if OBJECT is not a buffer or if it has been killed.
248 \[Emacs 19.31 emulating function]"
251 (buffer-name (get-buffer object))))
253 ;; This macro was imported Emacs 19.33.
254 (defmacro-maybe save-selected-window (&rest body)
255 "Execute BODY, then select the window that was selected before BODY.
256 \[Emacs 19.31 emulating function]"
258 '((save-selected-window-window (selected-window)))
259 (list 'unwind-protect
261 (list 'select-window 'save-selected-window-window))))
264 ;;; @ Emacs 20.1 emulation
267 ;; This macro was imported Emacs 20.2.
268 (defmacro-maybe when (cond &rest body)
269 "(when COND BODY...): if COND yields non-nil, do BODY, else return nil."
270 (list 'if cond (cons 'progn body)))
272 (defmacro-maybe save-current-buffer (&rest body)
273 "Save the current buffer; execute BODY; restore the current buffer.
274 Executes BODY just like `progn'."
275 (` (let ((orig-buffer (current-buffer)))
278 (set-buffer orig-buffer)))))
280 ;; This macro was imported Emacs 20.2.
281 (defmacro-maybe with-current-buffer (buffer &rest body)
282 "Execute the forms in BODY with BUFFER as the current buffer.
283 The value returned is the value of the last form in BODY.
284 See also `with-temp-buffer'."
285 (` (save-current-buffer
286 (set-buffer (, buffer))
289 ;; This macro was imported Emacs 20.2.
290 (defmacro-maybe with-temp-buffer (&rest forms)
291 "Create a temporary buffer, and evaluate FORMS there like `progn'.
292 See also `with-temp-file' and `with-output-to-string'."
293 (let ((temp-buffer (make-symbol "temp-buffer")))
294 (` (let (((, temp-buffer)
295 (get-buffer-create (generate-new-buffer-name " *temp*"))))
297 (with-current-buffer (, temp-buffer)
299 (and (buffer-name (, temp-buffer))
300 (kill-buffer (, temp-buffer))))))))
302 ;; This function was imported Emacs 20.3.
303 (defun-maybe last (x &optional n)
304 "Return the last link of the list X. Its car is the last element.
305 If X is nil, return nil.
306 If N is non-nil, return the Nth-to-last link of X.
307 If N is bigger than the length of X, return X."
311 (setq m (1+ m) p (cdr p)))
313 (if (< n m) (nthcdr (- m n) x) x)))
318 ;; This function was imported Emacs 20.3. (cl function)
319 (defun-maybe butlast (x &optional n)
320 "Returns a copy of LIST with the last N elements removed."
321 (if (and n (<= n 0)) x
322 (nbutlast (copy-sequence x) n)))
324 ;; This function was imported Emacs 20.3. (cl function)
325 (defun-maybe nbutlast (x &optional n)
326 "Modifies LIST to remove the last N elements."
327 (let ((m (length x)))
331 (if (> n 0) (setcdr (nthcdr (- (1- m) n) x) nil))
334 ;; This function was imported from XEmacs 21.
335 (defun-maybe split-string (string &optional pattern)
336 "Return a list of substrings of STRING which are separated by PATTERN.
337 If PATTERN is omitted, it defaults to \"[ \\f\\t\\n\\r\\v]+\"."
339 (setq pattern "[ \f\t\n\r\v]+"))
340 ;; The FSF version of this function takes care not to cons in case
341 ;; of infloop. Maybe we should synch?
342 (let (parts (start 0))
343 (while (string-match pattern string start)
344 (setq parts (cons (substring string start (match-beginning 0)) parts)
345 start (match-end 0)))
346 (nreverse (cons (substring string start) parts))))
349 ;;; @ Emacs 20.3 emulation
352 (defmacro-maybe string-as-unibyte (string)
353 "Return a unibyte string with the same individual bytes as STRING.
354 If STRING is unibyte, the result is STRING itself.
355 \[Emacs 20.3 emulating macro]"
358 (defmacro-maybe string-as-multibyte (string)
359 "Return a multibyte string with the same individual bytes as STRING.
360 If STRING is multibyte, the result is STRING itself.
361 \[Emacs 20.3 emulating macro]"
365 ;;; @ XEmacs emulation
368 (defun-maybe functionp (obj)
369 "Returns t if OBJ is a function, nil otherwise.
370 \[XEmacs emulating function]"
372 (byte-code-function-p obj)
373 (and (symbolp obj)(fboundp obj))
374 (and (consp obj)(eq (car obj) 'lambda))
377 (defun-maybe point-at-eol (&optional arg buffer)
378 "Return the character position of the last character on the current line.
379 With argument N not nil or 1, move forward N - 1 lines first.
380 If scan reaches end of buffer, return that position.
381 This function does not move point. [XEmacs emulating function]"
387 (forward-line (1- arg))
396 (or (fboundp 'char-int)
397 (fset 'char-int (symbol-function 'identity))
399 (or (fboundp 'int-char)
400 (fset 'int-char (symbol-function 'identity))
402 (or (fboundp 'char-or-char-int-p)
403 (fset 'char-or-char-int-p (symbol-function 'integerp))
407 ;;; @ for text/richtext and text/enriched
410 (cond ((fboundp 'richtext-decode)
413 ((or running-emacs-19_29-or-later running-xemacs-19_14-or-later)
415 (autoload 'richtext-decode "richtext")
416 (or (assq 'text/richtext format-alist)
420 '("Extended MIME text/richtext format."
421 "Content-[Tt]ype:[ \t]*text/richtext"
422 richtext-decode richtext-encode t enriched-mode))
426 ;; don't have enriched.el
427 (autoload 'richtext-decode "tinyrich")
428 (autoload 'enriched-decode "tinyrich")