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 19)
97 ;;; @ Emacs 19 emulation
100 (defun-maybe minibuffer-prompt-width ()
101 "Return the display width of the minibuffer prompt."
103 (set-buffer (window-buffer (minibuffer-window)))
107 ;;; @ Emacs 19.29 emulation
110 (defvar path-separator ":"
111 "Character used to separate concatenated paths.")
113 (defun-maybe buffer-substring-no-properties (start end)
114 "Return the characters of part of the buffer, without the text properties.
115 The two arguments START and END are character positions;
116 they can be in either order. [Emacs 19.29 emulating function]"
117 (let ((string (buffer-substring start end)))
118 (set-text-properties 0 (length string) nil string)
121 (defun-maybe match-string (num &optional string)
122 "Return string of text matched by last search.
123 NUM specifies which parenthesized expression in the last regexp.
124 Value is nil if NUMth pair didn't match, or there were less than NUM pairs.
125 Zero means the entire text matched by the whole regexp or whole string.
126 STRING should be given if the last search was by `string-match' on STRING.
127 \[Emacs 19.29 emulating function]"
128 (if (match-beginning num)
130 (substring string (match-beginning num) (match-end num))
131 (buffer-substring (match-beginning num) (match-end num)))))
133 (or (featurep 'xemacs)
134 (>= emacs-major-version 20)
135 (and (= emacs-major-version 19)
136 (>= emacs-minor-version 29))
137 ;; for Emacs 19.28 or earlier
138 (fboundp 'si:read-string)
140 (fset 'si:read-string (symbol-function 'read-string))
142 (defun read-string (prompt &optional initial-input history)
143 "Read a string from the minibuffer, prompting with string PROMPT.
144 If non-nil, second arg INITIAL-INPUT is a string to insert before reading.
145 The third arg HISTORY, is dummy for compatibility. [emu.el]
146 See `read-from-minibuffer' for details of HISTORY argument."
147 (si:read-string prompt initial-input))
151 ;;; @ Emacs 19.30 emulation
154 ;; This function was imported Emacs 19.30.
155 (defun-maybe add-to-list (list-var element)
156 "Add to the value of LIST-VAR the element ELEMENT if it isn't there yet.
157 If you want to use `add-to-list' on a variable that is not defined
158 until a certain package is loaded, you should put the call to `add-to-list'
159 into a hook function that will be run only after loading the package.
160 \[Emacs 19.30 emulating function]"
161 (or (member element (symbol-value list-var))
162 (set list-var (cons element (symbol-value list-var)))))
164 (cond ((fboundp 'insert-file-contents-literally))
165 ((boundp 'file-name-handler-alist)
166 (defun insert-file-contents-literally
167 (filename &optional visit beg end replace)
168 "Like `insert-file-contents', q.v., but only reads in the file.
169 A buffer may be modified in several ways after reading into the buffer due
170 to advanced Emacs features, such as file-name-handlers, format decoding,
171 find-file-hooks, etc.
172 This function ensures that none of these modifications will take place.
173 \[Emacs 19.30 emulating function]"
174 (let (file-name-handler-alist)
175 (insert-file-contents filename visit beg end replace)))
178 (defalias 'insert-file-contents-literally 'insert-file-contents)
182 ;;; @ Emacs 19.31 emulation
185 (defun-maybe buffer-live-p (object)
186 "Return non-nil if OBJECT is a buffer which has not been killed.
187 Value is nil if OBJECT is not a buffer or if it has been killed.
188 \[Emacs 19.31 emulating function]"
191 (buffer-name (get-buffer object))))
193 ;; This macro was imported Emacs 19.33.
194 (defmacro-maybe save-selected-window (&rest body)
195 "Execute BODY, then select the window that was selected before BODY.
196 \[Emacs 19.31 emulating function]"
198 '((save-selected-window-window (selected-window)))
199 (list 'unwind-protect
201 (list 'select-window 'save-selected-window-window))))
204 ;;; @ Emacs 20.1 emulation
207 ;; This macro was imported Emacs 20.2.
208 (defmacro-maybe when (cond &rest body)
209 "(when COND BODY...): if COND yields non-nil, do BODY, else return nil."
210 (list 'if cond (cons 'progn body)))
212 ;; This macro was imported Emacs 20.3.
213 (defmacro-maybe unless (cond &rest body)
214 "(unless COND BODY...): if COND yields nil, do BODY, else return nil."
215 (cons 'if (cons cond (cons nil body))))
217 (defmacro-maybe save-current-buffer (&rest body)
218 "Save the current buffer; execute BODY; restore the current buffer.
219 Executes BODY just like `progn'."
220 (` (let ((orig-buffer (current-buffer)))
223 (set-buffer orig-buffer)))))
225 ;; This macro was imported Emacs 20.2.
226 (defmacro-maybe with-current-buffer (buffer &rest body)
227 "Execute the forms in BODY with BUFFER as the current buffer.
228 The value returned is the value of the last form in BODY.
229 See also `with-temp-buffer'."
230 (` (save-current-buffer
231 (set-buffer (, buffer))
234 ;; This macro was imported Emacs 20.2.
235 (defmacro-maybe with-temp-file (file &rest forms)
236 "Create a new buffer, evaluate FORMS there, and write the buffer to FILE.
237 The value of the last form in FORMS is returned, like `progn'.
238 See also `with-temp-buffer'."
239 (let ((temp-file (make-symbol "temp-file"))
240 (temp-buffer (make-symbol "temp-buffer")))
241 (` (let (((, temp-file) (, file))
243 (get-buffer-create (generate-new-buffer-name " *temp file*"))))
246 (with-current-buffer (, temp-buffer)
248 (with-current-buffer (, temp-buffer)
250 (write-region (point-min) (point-max) (, temp-file) nil 0)))
251 (and (buffer-name (, temp-buffer))
252 (kill-buffer (, temp-buffer))))))))
254 ;; This macro was imported Emacs 20.2.
255 (defmacro-maybe with-temp-buffer (&rest forms)
256 "Create a temporary buffer, and evaluate FORMS there like `progn'.
257 See also `with-temp-file' and `with-output-to-string'."
258 (let ((temp-buffer (make-symbol "temp-buffer")))
259 (` (let (((, temp-buffer)
260 (get-buffer-create (generate-new-buffer-name " *temp*"))))
262 (with-current-buffer (, temp-buffer)
264 (and (buffer-name (, temp-buffer))
265 (kill-buffer (, temp-buffer))))))))
267 ;; This function was imported Emacs 20.3.
268 (defun-maybe last (x &optional n)
269 "Return the last link of the list X. Its car is the last element.
270 If X is nil, return nil.
271 If N is non-nil, return the Nth-to-last link of X.
272 If N is bigger than the length of X, return X."
276 (setq m (1+ m) p (cdr p)))
278 (if (< n m) (nthcdr (- m n) x) x)))
283 ;; This function was imported Emacs 20.3. (cl function)
284 (defun-maybe butlast (x &optional n)
285 "Returns a copy of LIST with the last N elements removed."
286 (if (and n (<= n 0)) x
287 (nbutlast (copy-sequence x) n)))
289 ;; This function was imported Emacs 20.3. (cl function)
290 (defun-maybe nbutlast (x &optional n)
291 "Modifies LIST to remove the last N elements."
292 (let ((m (length x)))
296 (if (> n 0) (setcdr (nthcdr (- (1- m) n) x) nil))
299 ;; This function was imported from XEmacs 21.
300 (defun-maybe split-string (string &optional pattern)
301 "Return a list of substrings of STRING which are separated by PATTERN.
302 If PATTERN is omitted, it defaults to \"[ \\f\\t\\n\\r\\v]+\"."
304 (setq pattern "[ \f\t\n\r\v]+"))
305 ;; The FSF version of this function takes care not to cons in case
306 ;; of infloop. Maybe we should synch?
307 (let (parts (start 0))
308 (while (string-match pattern string start)
309 (setq parts (cons (substring string start (match-beginning 0)) parts)
310 start (match-end 0)))
311 (nreverse (cons (substring string start) parts))))
314 ;;; @ Emacs 20.3 emulation
317 (defun-maybe line-beginning-position (&optional n)
318 "Return the character position of the first character on the current line.
319 With argument N not nil or 1, move forward N - 1 lines first.
320 If scan reaches end of buffer, return that position.
321 This function does not move point."
324 (forward-line (1- n))
329 (defun-maybe line-end-position (&optional n)
330 "Return the character position of the last 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))
342 ;;; @ XEmacs emulation
345 (defun-maybe point-at-bol (&optional n buffer)
346 "Return the character position of the first character on the current line.
347 With argument N not nil or 1, move forward N - 1 lines first.
348 If scan reaches end of buffer, return that position.
349 This function does not move point. [XEmacs emulating function]"
354 (line-beginning-position n)
357 (defun-maybe point-at-eol (&optional n buffer)
358 "Return the character position of the last character on the current line.
359 With argument N not nil or 1, move forward N - 1 lines first.
360 If scan reaches end of buffer, return that position.
361 This function does not move point. [XEmacs emulating function]"
366 (line-end-position n)
369 (defun-maybe functionp (obj)
370 "Returns t if OBJ is a function, nil otherwise.
371 \[XEmacs emulating function]"
373 (byte-code-function-p obj)
374 (and (symbolp obj)(fboundp obj))
375 (and (consp obj)(eq (car obj) 'lambda))
378 (defsubst-maybe define-obsolete-function-alias (oldfun newfun)
379 "Define OLDFUN as an obsolete alias for function NEWFUN.
380 This makes calling OLDFUN equivalent to calling NEWFUN and marks OLDFUN
381 as obsolete. [XEmacs emulating function]"
382 (defalias oldfun newfun)
383 (make-obsolete oldfun newfun)