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))
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)
75 (defconst-maybe emacs-major-version (string-to-int emacs-version))
76 (defconst-maybe emacs-minor-version
78 (substring emacs-version
79 (string-match (format "%d\\." emacs-major-version)
82 (cond ((featurep 'xemacs)
85 ((string-match "XEmacs" emacs-version)
89 ((> emacs-major-version 20))
90 ((= emacs-major-version 20)
91 (cond ((fboundp 'string)
92 ;; Emacs 20.3 or later
94 ((fboundp 'concat-chars)
95 ;; Emacs 20.1 or later
96 (defalias 'string 'concat-chars)
99 ((= emacs-major-version 19))
105 ;;; @ Emacs 19 emulation
108 (defmacro-maybe eval-and-compile (&rest body)
109 "Like `progn', but evaluates the body at compile time and at load time."
110 ;; Remember, it's magic.
113 (defun-maybe minibuffer-prompt-width ()
114 "Return the display width of the minibuffer prompt."
116 (set-buffer (window-buffer (minibuffer-window)))
120 ;;; @ Emacs 19.29 emulation
123 (defvar path-separator ":"
124 "Character used to separate concatenated paths.")
126 (defun-maybe buffer-substring-no-properties (start end)
127 "Return the characters of part of the buffer, without the text properties.
128 The two arguments START and END are character positions;
129 they can be in either order. [Emacs 19.29 emulating function]"
130 (let ((string (buffer-substring start end)))
131 (set-text-properties 0 (length string) nil string)
134 (defun-maybe match-string (num &optional string)
135 "Return string of text matched by last search.
136 NUM specifies which parenthesized expression in the last regexp.
137 Value is nil if NUMth pair didn't match, or there were less than NUM pairs.
138 Zero means the entire text matched by the whole regexp or whole string.
139 STRING should be given if the last search was by `string-match' on STRING.
140 \[Emacs 19.29 emulating function]"
141 (if (match-beginning num)
143 (substring string (match-beginning num) (match-end num))
144 (buffer-substring (match-beginning num) (match-end num)))))
146 (or (featurep 'xemacs)
147 (>= emacs-major-version 20)
148 (and (= emacs-major-version 19)
149 (>= emacs-minor-version 29))
150 ;; for Emacs 19.28 or earlier
151 (fboundp 'si:read-string)
153 (fset 'si:read-string (symbol-function 'read-string))
154 (defun read-string (prompt &optional initial-input history)
155 "Read a string from the minibuffer, prompting with string PROMPT.
156 If non-nil, second arg INITIAL-INPUT is a string to insert before reading.
157 The third arg HISTORY, is dummy for compatibility. [emu.el]
158 See `read-from-minibuffer' for details of HISTORY argument."
159 (si:read-string prompt initial-input))
163 ;;; @ Emacs 19.30 emulation
166 ;; This function was imported Emacs 19.30.
167 (defun-maybe add-to-list (list-var element)
168 "Add to the value of LIST-VAR the element ELEMENT if it isn't there yet.
169 If you want to use `add-to-list' on a variable that is not defined
170 until a certain package is loaded, you should put the call to `add-to-list'
171 into a hook function that will be run only after loading the package.
172 \[Emacs 19.30 emulating function]"
173 (or (member element (symbol-value list-var))
174 (set list-var (cons element (symbol-value list-var)))))
176 (cond ((fboundp 'insert-file-contents-literally))
177 ((boundp 'file-name-handler-alist)
178 (defun insert-file-contents-literally
179 (filename &optional visit beg end replace)
180 "Like `insert-file-contents', q.v., but only reads in the file.
181 A buffer may be modified in several ways after reading into the buffer due
182 to advanced Emacs features, such as file-name-handlers, format decoding,
183 find-file-hooks, etc.
184 This function ensures that none of these modifications will take place.
185 \[Emacs 19.30 emulating function]"
186 (let (file-name-handler-alist)
187 (insert-file-contents filename visit beg end replace)))
190 (defalias 'insert-file-contents-literally 'insert-file-contents)
194 ;;; @ Emacs 19.31 emulation
197 (defun-maybe buffer-live-p (object)
198 "Return non-nil if OBJECT is a buffer which has not been killed.
199 Value is nil if OBJECT is not a buffer or if it has been killed.
200 \[Emacs 19.31 emulating function]"
203 (buffer-name (get-buffer object))))
205 ;; This macro was imported Emacs 19.33.
206 (defmacro-maybe save-selected-window (&rest body)
207 "Execute BODY, then select the window that was selected before BODY.
208 \[Emacs 19.31 emulating function]"
210 '((save-selected-window-window (selected-window)))
211 (list 'unwind-protect
213 (list 'select-window 'save-selected-window-window))))
216 ;;; @ Emacs 20.1 emulation
219 ;; This macro was imported Emacs 20.2.
220 (defmacro-maybe when (cond &rest body)
221 "(when COND BODY...): if COND yields non-nil, do BODY, else return nil."
222 (list 'if cond (cons 'progn body)))
224 ;; This macro was imported Emacs 20.3.
225 (defmacro-maybe unless (cond &rest body)
226 "(unless COND BODY...): if COND yields nil, do BODY, else return nil."
227 (cons 'if (cons cond (cons nil body))))
229 (defmacro-maybe save-current-buffer (&rest body)
230 "Save the current buffer; execute BODY; restore the current buffer.
231 Executes BODY just like `progn'."
232 (` (let ((orig-buffer (current-buffer)))
235 (set-buffer orig-buffer)))))
237 ;; This macro was imported Emacs 20.2.
238 (defmacro-maybe with-current-buffer (buffer &rest body)
239 "Execute the forms in BODY with BUFFER as the current buffer.
240 The value returned is the value of the last form in BODY.
241 See also `with-temp-buffer'."
242 (` (save-current-buffer
243 (set-buffer (, buffer))
246 ;; This macro was imported Emacs 20.2.
247 (defmacro-maybe with-temp-file (file &rest forms)
248 "Create a new buffer, evaluate FORMS there, and write the buffer to FILE.
249 The value of the last form in FORMS is returned, like `progn'.
250 See also `with-temp-buffer'."
251 (let ((temp-file (make-symbol "temp-file"))
252 (temp-buffer (make-symbol "temp-buffer")))
253 (` (let (((, temp-file) (, file))
255 (get-buffer-create (generate-new-buffer-name " *temp file*"))))
258 (with-current-buffer (, temp-buffer)
260 (with-current-buffer (, temp-buffer)
262 (write-region (point-min) (point-max) (, temp-file) nil 0)))
263 (and (buffer-name (, temp-buffer))
264 (kill-buffer (, temp-buffer))))))))
266 ;; This macro was imported Emacs 20.2.
267 (defmacro-maybe with-temp-buffer (&rest forms)
268 "Create a temporary buffer, and evaluate FORMS there like `progn'.
269 See also `with-temp-file' and `with-output-to-string'."
270 (let ((temp-buffer (make-symbol "temp-buffer")))
271 (` (let (((, temp-buffer)
272 (get-buffer-create (generate-new-buffer-name " *temp*"))))
274 (with-current-buffer (, temp-buffer)
276 (and (buffer-name (, temp-buffer))
277 (kill-buffer (, temp-buffer))))))))
279 ;; This function was imported Emacs 20.3.
280 (defun-maybe last (x &optional n)
281 "Return the last link of the list X. Its car is the last element.
282 If X is nil, return nil.
283 If N is non-nil, return the Nth-to-last link of X.
284 If N is bigger than the length of X, return X."
288 (setq m (1+ m) p (cdr p)))
290 (if (< n m) (nthcdr (- m n) x) x)))
295 ;; This function was imported Emacs 20.3. (cl function)
296 (defun-maybe butlast (x &optional n)
297 "Returns a copy of LIST with the last N elements removed."
298 (if (and n (<= n 0)) x
299 (nbutlast (copy-sequence x) n)))
301 ;; This function was imported Emacs 20.3. (cl function)
302 (defun-maybe nbutlast (x &optional n)
303 "Modifies LIST to remove the last N elements."
304 (let ((m (length x)))
308 (if (> n 0) (setcdr (nthcdr (- (1- m) n) x) nil))
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]+\"."
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))))
326 ;;; @ Emacs 20.3 emulation
329 (defun-maybe line-beginning-position (&optional n)
330 "Return the character position of the first character on the current line.
331 With argument N not nil or 1, move forward N - 1 lines first.
332 If scan reaches end of buffer, return that position.
333 This function does not move point."
336 (forward-line (1- n))
341 (defun-maybe line-end-position (&optional n)
342 "Return the character position of the last character on the current line.
343 With argument N not nil or 1, move forward N - 1 lines first.
344 If scan reaches end of buffer, return that position.
345 This function does not move point."
348 (forward-line (1- n))
353 (defun-maybe string (&rest chars)
354 "Concatenate all the argument characters and make the result a string."
355 (mapconcat (function char-to-string) chars "")
359 ;;; @ XEmacs emulation
362 (defun-maybe find-face (face-or-name)
363 "Retrieve the face of the given name.
364 If FACE-OR-NAME is a face object, it is simply returned.
365 Otherwise, FACE-OR-NAME should be a symbol. If there is no such face,
366 nil is returned. Otherwise the associated face object is returned.
367 \[XEmacs emulating function]"
368 (car (memq face-or-name (face-list)))
371 (defun-maybe point-at-bol (&optional n buffer)
372 "Return the character position of the first character on the current line.
373 With argument N not nil or 1, move forward N - 1 lines first.
374 If scan reaches end of buffer, return that position.
375 This function does not move point. [XEmacs emulating function]"
380 (line-beginning-position n)
383 (defun-maybe point-at-eol (&optional n buffer)
384 "Return the character position of the last character on the current line.
385 With argument N not nil or 1, move forward N - 1 lines first.
386 If scan reaches end of buffer, return that position.
387 This function does not move point. [XEmacs emulating function]"
392 (line-end-position n)
395 (defun-maybe functionp (obj)
396 "Returns t if OBJ is a function, nil otherwise.
397 \[XEmacs emulating function]"
399 (byte-code-function-p obj)
400 (and (symbolp obj)(fboundp obj))
401 (and (consp obj)(eq (car obj) 'lambda))
404 (defsubst-maybe define-obsolete-function-alias (oldfun newfun)
405 "Define OLDFUN as an obsolete alias for function NEWFUN.
406 This makes calling OLDFUN equivalent to calling NEWFUN and marks OLDFUN
407 as obsolete. [XEmacs emulating function]"
408 (defalias oldfun newfun)
409 (make-obsolete oldfun newfun)