1 ;;; poe.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 APEL (A Portable Emacs Library).
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)))
30 (` (or (fboundp (quote (, name)))
32 (defun (, name) (,@ everything-else))
33 (put (quote (, name)) 'defun-maybe t)
37 (defmacro defsubst-maybe (name &rest everything-else)
38 (or (and (fboundp name)
39 (not (get name 'defsubst-maybe)))
40 (` (or (fboundp (quote (, name)))
42 (defsubst (, name) (,@ everything-else))
43 (put (quote (, name)) 'defsubst-maybe t)
47 (defmacro defmacro-maybe (name &rest everything-else)
48 (or (and (fboundp name)
49 (not (get name 'defmacro-maybe)))
50 (` (or (fboundp (quote (, name)))
52 (defmacro (, name) (,@ everything-else))
53 (put (quote (, name)) 'defmacro-maybe t)
57 (defmacro defalias-maybe (symbol definition)
58 (setq symbol (eval symbol))
59 (or (and (fboundp symbol)
60 (not (get symbol 'defalias-maybe)))
61 (` (or (fboundp (quote (, symbol)))
63 (defalias (quote (, symbol)) (, definition))
64 (put (quote (, symbol)) 'defalias-maybe t)
68 (put 'defun-maybe 'lisp-indent-function 'defun)
69 (put 'defsubst-maybe 'lisp-indent-function 'defun)
70 (put 'defmacro-maybe 'lisp-indent-function 'defun)
72 (defmacro defvar-maybe (name &rest everything-else)
73 (or (and (boundp name)
74 (not (get name 'defvar-maybe)))
75 (` (or (boundp (quote (, name)))
77 (defvar (, name) (,@ everything-else))
78 (put (quote (, name)) 'defvar-maybe t)
82 (defmacro defconst-maybe (name &rest everything-else)
83 (or (and (boundp name)
84 (not (get name 'defconst-maybe))
86 (` (or (boundp (quote (, name)))
88 (defconst (, name) (,@ everything-else))
89 (put (quote (, name)) 'defconst-maybe t)
93 (defconst-maybe emacs-major-version (string-to-int emacs-version))
94 (defconst-maybe emacs-minor-version
96 (substring emacs-version
97 (string-match (format "%d\\." emacs-major-version)
100 (cond ((featurep 'xemacs)
101 (require 'poe-xemacs)
103 ((string-match "XEmacs" emacs-version)
105 (require 'poe-xemacs)
107 ((> emacs-major-version 20))
108 ((= emacs-major-version 20)
109 (cond ((fboundp 'string)
110 ;; Emacs 20.3 or later
112 ((fboundp 'concat-chars)
113 ;; Emacs 20.1 or later
114 (defalias 'string 'concat-chars)
117 ((= emacs-major-version 19))
123 ;;; @ Emacs 19 emulation
126 (defmacro-maybe eval-and-compile (&rest body)
127 "Like `progn', but evaluates the body at compile time and at load time."
128 ;; Remember, it's magic.
131 (defun-maybe minibuffer-prompt-width ()
132 "Return the display width of the minibuffer prompt."
134 (set-buffer (window-buffer (minibuffer-window)))
138 ;;; @ Emacs 19.29 emulation
141 (defvar path-separator ":"
142 "Character used to separate concatenated paths.")
144 (defun-maybe buffer-substring-no-properties (start end)
145 "Return the characters of part of the buffer, without the text properties.
146 The two arguments START and END are character positions;
147 they can be in either order. [Emacs 19.29 emulating function]"
148 (let ((string (buffer-substring start end)))
149 (set-text-properties 0 (length string) nil string)
152 (defun-maybe match-string (num &optional string)
153 "Return string of text matched by last search.
154 NUM specifies which parenthesized expression in the last regexp.
155 Value is nil if NUMth pair didn't match, or there were less than NUM pairs.
156 Zero means the entire text matched by the whole regexp or whole string.
157 STRING should be given if the last search was by `string-match' on STRING.
158 \[Emacs 19.29 emulating function]"
159 (if (match-beginning num)
161 (substring string (match-beginning num) (match-end num))
162 (buffer-substring (match-beginning num) (match-end num)))))
164 (or (featurep 'xemacs)
165 (>= emacs-major-version 20)
166 (and (= emacs-major-version 19)
167 (>= emacs-minor-version 29))
168 ;; for Emacs 19.28 or earlier
169 (fboundp 'si:read-string)
171 (fset 'si:read-string (symbol-function 'read-string))
172 (defun read-string (prompt &optional initial-input history)
173 "Read a string from the minibuffer, prompting with string PROMPT.
174 If non-nil, second arg INITIAL-INPUT is a string to insert before reading.
175 The third arg HISTORY, is dummy for compatibility. [emu.el]
176 See `read-from-minibuffer' for details of HISTORY argument."
177 (si:read-string prompt initial-input))
181 ;;; @ Emacs 19.30 emulation
184 ;; imported from Emacs 19.30.
185 (defun-maybe add-to-list (list-var element)
186 "Add to the value of LIST-VAR the element ELEMENT if it isn't there yet.
187 If you want to use `add-to-list' on a variable that is not defined
188 until a certain package is loaded, you should put the call to `add-to-list'
189 into a hook function that will be run only after loading the package.
190 \[Emacs 19.30 emulating function]"
191 (or (member element (symbol-value list-var))
192 (set list-var (cons element (symbol-value list-var)))))
194 (cond ((fboundp 'insert-file-contents-literally))
195 ((boundp 'file-name-handler-alist)
196 (defun insert-file-contents-literally
197 (filename &optional visit beg end replace)
198 "Like `insert-file-contents', q.v., but only reads in the file.
199 A buffer may be modified in several ways after reading into the buffer due
200 to advanced Emacs features, such as file-name-handlers, format decoding,
201 find-file-hooks, etc.
202 This function ensures that none of these modifications will take place.
203 \[Emacs 19.30 emulating function]"
204 (let (file-name-handler-alist)
205 (insert-file-contents filename visit beg end replace)))
208 (defalias 'insert-file-contents-literally 'insert-file-contents)
212 ;;; @ Emacs 19.31 emulation
215 (defun-maybe buffer-live-p (object)
216 "Return non-nil if OBJECT is a buffer which has not been killed.
217 Value is nil if OBJECT is not a buffer or if it has been killed.
218 \[Emacs 19.31 emulating function]"
221 (buffer-name (get-buffer object))))
223 ;; imported from Emacs 19.33.
224 (defmacro-maybe save-selected-window (&rest body)
225 "Execute BODY, then select the window that was selected before BODY.
226 \[Emacs 19.31 emulating function]"
228 '((save-selected-window-window (selected-window)))
229 (list 'unwind-protect
231 (list 'select-window 'save-selected-window-window))))
234 ;;; @ Emacs 20.1 emulation
237 ;; imported from Emacs 20.2.
238 (defmacro-maybe when (cond &rest body)
239 "(when COND BODY...): if COND yields non-nil, do BODY, else return nil."
240 (list 'if cond (cons 'progn body)))
242 ;; imported from Emacs 20.3.
243 (defmacro-maybe unless (cond &rest body)
244 "(unless COND BODY...): if COND yields nil, do BODY, else return nil."
245 (cons 'if (cons cond (cons nil body))))
247 (defmacro-maybe save-current-buffer (&rest body)
248 "Save the current buffer; execute BODY; restore the current buffer.
249 Executes BODY just like `progn'."
250 (` (let ((orig-buffer (current-buffer)))
253 (set-buffer orig-buffer)))))
255 ;; imported from Emacs 20.2.
256 (defmacro-maybe with-current-buffer (buffer &rest body)
257 "Execute the forms in BODY with BUFFER as the current buffer.
258 The value returned is the value of the last form in BODY.
259 See also `with-temp-buffer'."
260 (` (save-current-buffer
261 (set-buffer (, buffer))
264 ;; imported from Emacs 20.2.
265 (defmacro-maybe with-temp-file (file &rest forms)
266 "Create a new buffer, evaluate FORMS there, and write the buffer to FILE.
267 The value of the last form in FORMS is returned, like `progn'.
268 See also `with-temp-buffer'."
269 (let ((temp-file (make-symbol "temp-file"))
270 (temp-buffer (make-symbol "temp-buffer")))
271 (` (let (((, temp-file) (, file))
273 (get-buffer-create (generate-new-buffer-name " *temp file*"))))
276 (with-current-buffer (, temp-buffer)
278 (with-current-buffer (, temp-buffer)
280 (write-region (point-min) (point-max) (, temp-file) nil 0)))
281 (and (buffer-name (, temp-buffer))
282 (kill-buffer (, temp-buffer))))))))
284 ;; imported from Emacs 20.2.
285 (defmacro-maybe with-temp-buffer (&rest forms)
286 "Create a temporary buffer, and evaluate FORMS there like `progn'.
287 See also `with-temp-file' and `with-output-to-string'."
288 (let ((temp-buffer (make-symbol "temp-buffer")))
289 (` (let (((, temp-buffer)
290 (get-buffer-create (generate-new-buffer-name " *temp*"))))
292 (with-current-buffer (, temp-buffer)
294 (and (buffer-name (, temp-buffer))
295 (kill-buffer (, temp-buffer))))))))
297 ;; imported from Emacs 20.3.
298 (defun-maybe last (x &optional n)
299 "Return the last link of the list X. Its car is the last element.
300 If X is nil, return nil.
301 If N is non-nil, return the Nth-to-last link of X.
302 If N is bigger than the length of X, return X."
306 (setq m (1+ m) p (cdr p)))
308 (if (< n m) (nthcdr (- m n) x) x)))
313 ;; imported from Emacs 20.3. (cl function)
314 (defun-maybe butlast (x &optional n)
315 "Returns a copy of LIST with the last N elements removed."
316 (if (and n (<= n 0)) x
317 (nbutlast (copy-sequence x) n)))
319 ;; imported from Emacs 20.3. (cl function)
320 (defun-maybe nbutlast (x &optional n)
321 "Modifies LIST to remove the last N elements."
322 (let ((m (length x)))
326 (if (> n 0) (setcdr (nthcdr (- (1- m) n) x) nil))
329 ;; imported from XEmacs 21.
330 (defun-maybe split-string (string &optional pattern)
331 "Return a list of substrings of STRING which are separated by PATTERN.
332 If PATTERN is omitted, it defaults to \"[ \\f\\t\\n\\r\\v]+\"."
334 (setq pattern "[ \f\t\n\r\v]+"))
335 ;; The FSF version of this function takes care not to cons in case
336 ;; of infloop. Maybe we should synch?
337 (let (parts (start 0))
338 (while (string-match pattern string start)
339 (setq parts (cons (substring string start (match-beginning 0)) parts)
340 start (match-end 0)))
341 (nreverse (cons (substring string start) parts))))
344 ;;; @ Emacs 20.3 emulation
347 ;; imported from Emacs 20.3.91.
348 (defvar-maybe temporary-file-directory
349 (file-name-as-directory
350 (cond ((memq system-type '(ms-dos windows-nt))
351 (or (getenv "TEMP") (getenv "TMPDIR") (getenv "TMP") "c:/temp"))
352 ((memq system-type '(vax-vms axp-vms))
353 (or (getenv "TMPDIR") (getenv "TMP") (getenv "TEMP") "SYS$SCRATCH:"))
355 (or (getenv "TMPDIR") (getenv "TMP") (getenv "TEMP") "/tmp"))))
356 "The directory for writing temporary files.")
358 (defun-maybe line-beginning-position (&optional n)
359 "Return the character position of the first character on the current line.
360 With argument N not nil or 1, move forward N - 1 lines first.
361 If scan reaches end of buffer, return that position.
362 This function does not move point."
365 (forward-line (1- n))
370 (defun-maybe line-end-position (&optional n)
371 "Return the character position of the last character on the current line.
372 With argument N not nil or 1, move forward N - 1 lines first.
373 If scan reaches end of buffer, return that position.
374 This function does not move point."
377 (forward-line (1- n))
382 (defun-maybe string (&rest chars)
383 "Concatenate all the argument characters and make the result a string."
384 (mapconcat (function char-to-string) chars "")
388 ;;; @ XEmacs emulation
391 (defun-maybe find-face (face-or-name)
392 "Retrieve the face of the given name.
393 If FACE-OR-NAME is a face object, it is simply returned.
394 Otherwise, FACE-OR-NAME should be a symbol. If there is no such face,
395 nil is returned. Otherwise the associated face object is returned.
396 \[XEmacs emulating function]"
397 (car (memq face-or-name (face-list)))
400 (defun-maybe point-at-bol (&optional n buffer)
401 "Return the character position of the first character on the current line.
402 With argument N not nil or 1, move forward N - 1 lines first.
403 If scan reaches end of buffer, return that position.
404 This function does not move point. [XEmacs emulating function]"
409 (line-beginning-position n)
412 (defun-maybe point-at-eol (&optional n buffer)
413 "Return the character position of the last character on the current line.
414 With argument N not nil or 1, move forward N - 1 lines first.
415 If scan reaches end of buffer, return that position.
416 This function does not move point. [XEmacs emulating function]"
421 (line-end-position n)
424 (defun-maybe functionp (obj)
425 "Returns t if OBJ is a function, nil otherwise.
426 \[XEmacs emulating function]"
428 (byte-code-function-p obj)
429 (and (symbolp obj)(fboundp obj))
430 (and (consp obj)(eq (car obj) 'lambda))
433 (defsubst-maybe define-obsolete-function-alias (oldfun newfun)
434 "Define OLDFUN as an obsolete alias for function NEWFUN.
435 This makes calling OLDFUN equivalent to calling NEWFUN and marks OLDFUN
436 as obsolete. [XEmacs emulating function]"
437 (defalias oldfun newfun)
438 (make-obsolete oldfun newfun)