1 ;;; poe.el --- Portable Outfit for Emacsen; -*-byte-compile-dynamic: t;-*-
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 ;; This modules does not includes MULE related features. MULE related
28 ;; features are supported by `poem'.
32 (defmacro defun-maybe (name &rest everything-else)
33 (or (and (fboundp name)
34 (not (get name 'defun-maybe)))
35 (` (or (fboundp (quote (, name)))
37 (defun (, name) (,@ everything-else))
38 (put (quote (, name)) 'defun-maybe t)
42 (defmacro defsubst-maybe (name &rest everything-else)
43 (or (and (fboundp name)
44 (not (get name 'defsubst-maybe)))
45 (` (or (fboundp (quote (, name)))
47 (defsubst (, name) (,@ everything-else))
48 (put (quote (, name)) 'defsubst-maybe t)
52 (defmacro defmacro-maybe (name &rest everything-else)
53 (or (and (fboundp name)
54 (not (get name 'defmacro-maybe)))
55 (` (or (fboundp (quote (, name)))
57 (defmacro (, name) (,@ everything-else))
58 (put (quote (, name)) 'defmacro-maybe t)
62 (defmacro defalias-maybe (symbol definition)
63 (setq symbol (eval symbol))
64 (or (and (fboundp symbol)
65 (not (get symbol 'defalias-maybe)))
66 (` (or (fboundp (quote (, symbol)))
68 (defalias (quote (, symbol)) (, definition))
69 (put (quote (, symbol)) 'defalias-maybe t)
73 (put 'defun-maybe 'lisp-indent-function 'defun)
74 (put 'defsubst-maybe 'lisp-indent-function 'defun)
75 (put 'defmacro-maybe 'lisp-indent-function 'defun)
77 (defmacro defvar-maybe (name &rest everything-else)
78 (or (and (boundp name)
79 (not (get name 'defvar-maybe)))
80 (` (or (boundp (quote (, name)))
82 (defvar (, name) (,@ everything-else))
83 (put (quote (, name)) 'defvar-maybe t)
87 (defmacro defconst-maybe (name &rest everything-else)
88 (or (and (boundp name)
89 (not (get name 'defconst-maybe))
91 (` (or (boundp (quote (, name)))
93 (defconst (, name) (,@ everything-else))
94 (put (quote (, name)) 'defconst-maybe t)
98 (defconst-maybe emacs-major-version (string-to-int emacs-version))
99 (defconst-maybe emacs-minor-version
101 (substring emacs-version
102 (string-match (format "%d\\." emacs-major-version)
105 (cond ((featurep 'xemacs)
106 (require 'poe-xemacs)
108 ((string-match "XEmacs" emacs-version)
110 (require 'poe-xemacs)
112 ((> emacs-major-version 20))
113 ((= emacs-major-version 20)
114 (cond ((fboundp 'string)
115 ;; Emacs 20.3 or later
117 ((fboundp 'concat-chars)
118 ;; Emacs 20.1 or later
119 (defalias 'string 'concat-chars)
122 ((= emacs-major-version 19))
128 ;;; @ Emacs 19 emulation
131 (defmacro-maybe eval-and-compile (&rest body)
132 "Like `progn', but evaluates the body at compile time and at load time."
133 ;; Remember, it's magic.
136 (defun-maybe minibuffer-prompt-width ()
137 "Return the display width of the minibuffer prompt."
139 (set-buffer (window-buffer (minibuffer-window)))
143 ;;; @ Emacs 19.29 emulation
146 (defvar-maybe path-separator ":"
147 "Character used to separate concatenated paths.")
149 (defun-maybe buffer-substring-no-properties (start end)
150 "Return the characters of part of the buffer, without the text properties.
151 The two arguments START and END are character positions;
152 they can be in either order. [Emacs 19.29 emulating function]"
153 (let ((string (buffer-substring start end)))
154 (set-text-properties 0 (length string) nil string)
157 (defun-maybe match-string (num &optional string)
158 "Return string of text matched by last search.
159 NUM specifies which parenthesized expression in the last regexp.
160 Value is nil if NUMth pair didn't match, or there were less than NUM pairs.
161 Zero means the entire text matched by the whole regexp or whole string.
162 STRING should be given if the last search was by `string-match' on STRING.
163 \[Emacs 19.29 emulating function]"
164 (if (match-beginning num)
166 (substring string (match-beginning num) (match-end num))
167 (buffer-substring (match-beginning num) (match-end num)))))
169 (or (featurep 'xemacs)
170 (>= emacs-major-version 20)
171 (and (= emacs-major-version 19)
172 (>= emacs-minor-version 29))
173 ;; for Emacs 19.28 or earlier
174 (fboundp 'si:read-string)
176 (fset 'si:read-string (symbol-function 'read-string))
177 (defun read-string (prompt &optional initial-input history)
178 "Read a string from the minibuffer, prompting with string PROMPT.
179 If non-nil, second arg INITIAL-INPUT is a string to insert before reading.
180 The third arg HISTORY, is dummy for compatibility.
181 See `read-from-minibuffer' for details of HISTORY argument."
182 (si:read-string prompt initial-input))
185 (defun-maybe rassoc (key list)
186 "Return non-nil if KEY is `equal' to the cdr of an element of LIST.
187 The value is actually the element of LIST whose cdr equals KEY."
190 (if (equal (cdr (car list)) key)
191 (throw 'found (car list))
193 (setq list (cdr list)))
196 (defmacro-maybe make-local-hook (hook))
198 ;; They are not Emacs features
200 (defmacro-maybe add-local-hook (hook function &optional append)
201 (if (fboundp 'make-local-hook)
202 (list 'add-hook hook function append t)
203 (list 'add-hook hook function append)
206 (defmacro-maybe remove-local-hook (hook function)
207 (if (fboundp 'make-local-hook)
208 (list 'remove-hook hook function t)
209 (list 'remove-hook hook function)
213 ;;; @ Emacs 19.30 emulation
216 ;; imported from Emacs 19.30.
217 (defun-maybe add-to-list (list-var element)
218 "Add to the value of LIST-VAR the element ELEMENT if it isn't there yet.
219 If you want to use `add-to-list' on a variable that is not defined
220 until a certain package is loaded, you should put the call to `add-to-list'
221 into a hook function that will be run only after loading the package.
222 \[Emacs 19.30 emulating function]"
223 (or (member element (symbol-value list-var))
224 (set list-var (cons element (symbol-value list-var)))))
226 (cond ((fboundp 'insert-file-contents-literally))
227 ((boundp 'file-name-handler-alist)
228 (defun insert-file-contents-literally
229 (filename &optional visit beg end replace)
230 "Like `insert-file-contents', q.v., but only reads in the file.
231 A buffer may be modified in several ways after reading into the buffer due
232 to advanced Emacs features, such as file-name-handlers, format decoding,
233 find-file-hooks, etc.
234 This function ensures that none of these modifications will take place.
235 \[Emacs 19.30 emulating function]"
236 (let (file-name-handler-alist)
237 (insert-file-contents filename visit beg end replace)))
240 (defalias 'insert-file-contents-literally 'insert-file-contents)
244 ;;; @ Emacs 19.31 emulation
247 (defun-maybe buffer-live-p (object)
248 "Return non-nil if OBJECT is a buffer which has not been killed.
249 Value is nil if OBJECT is not a buffer or if it has been killed.
250 \[Emacs 19.31 emulating function]"
253 (buffer-name (get-buffer object))))
255 ;; imported from Emacs 19.33.
256 (defmacro-maybe save-selected-window (&rest body)
257 "Execute BODY, then select the window that was selected before BODY.
258 \[Emacs 19.31 emulating function]"
260 '((save-selected-window-window (selected-window)))
261 (list 'unwind-protect
263 (list 'select-window 'save-selected-window-window))))
266 ;;; @ Emacs 20.1 emulation
269 ;; imported from Emacs 20.2.
270 (defmacro-maybe when (cond &rest body)
271 "(when COND BODY...): if COND yields non-nil, do BODY, else return nil."
272 (list 'if cond (cons 'progn body)))
274 ;; imported from Emacs 20.3.
275 (defmacro-maybe unless (cond &rest body)
276 "(unless COND BODY...): if COND yields nil, do BODY, else return nil."
277 (cons 'if (cons cond (cons nil body))))
279 (defmacro-maybe save-current-buffer (&rest body)
280 "Save the current buffer; execute BODY; restore the current buffer.
281 Executes BODY just like `progn'."
282 (` (let ((orig-buffer (current-buffer)))
285 (set-buffer orig-buffer)))))
287 ;; imported from Emacs 20.2.
288 (defmacro-maybe with-current-buffer (buffer &rest body)
289 "Execute the forms in BODY with BUFFER as the current buffer.
290 The value returned is the value of the last form in BODY.
291 See also `with-temp-buffer'."
292 (` (save-current-buffer
293 (set-buffer (, buffer))
296 ;; imported from Emacs 20.2.
297 (defmacro-maybe with-temp-file (file &rest forms)
298 "Create a new buffer, evaluate FORMS there, and write the buffer to FILE.
299 The value of the last form in FORMS is returned, like `progn'.
300 See also `with-temp-buffer'."
301 (let ((temp-file (make-symbol "temp-file"))
302 (temp-buffer (make-symbol "temp-buffer")))
303 (` (let (((, temp-file) (, file))
305 (get-buffer-create (generate-new-buffer-name " *temp file*"))))
308 (with-current-buffer (, temp-buffer)
310 (with-current-buffer (, temp-buffer)
312 (write-region (point-min) (point-max) (, temp-file) nil 0)))
313 (and (buffer-name (, temp-buffer))
314 (kill-buffer (, temp-buffer))))))))
316 ;; imported from Emacs 20.2.
317 (defmacro-maybe with-temp-buffer (&rest forms)
318 "Create a temporary buffer, and evaluate FORMS there like `progn'.
319 See also `with-temp-file' and `with-output-to-string'."
320 (let ((temp-buffer (make-symbol "temp-buffer")))
321 (` (let (((, temp-buffer)
322 (get-buffer-create (generate-new-buffer-name " *temp*"))))
324 (with-current-buffer (, temp-buffer)
326 (and (buffer-name (, temp-buffer))
327 (kill-buffer (, temp-buffer))))))))
329 ;; imported from Emacs 20.3.
330 (defun-maybe last (x &optional n)
331 "Return the last link of the list X. Its car is the last element.
332 If X is nil, return nil.
333 If N is non-nil, return the Nth-to-last link of X.
334 If N is bigger than the length of X, return X."
338 (setq m (1+ m) p (cdr p)))
340 (if (< n m) (nthcdr (- m n) x) x)))
345 ;; imported from Emacs 20.3. (cl function)
346 (defun-maybe butlast (x &optional n)
347 "Returns a copy of LIST with the last N elements removed."
348 (if (and n (<= n 0)) x
349 (nbutlast (copy-sequence x) n)))
351 ;; imported from Emacs 20.3. (cl function)
352 (defun-maybe nbutlast (x &optional n)
353 "Modifies LIST to remove the last N elements."
354 (let ((m (length x)))
358 (if (> n 0) (setcdr (nthcdr (- (1- m) n) x) nil))
361 ;; imported from XEmacs 21.
362 (defun-maybe split-string (string &optional pattern)
363 "Return a list of substrings of STRING which are separated by PATTERN.
364 If PATTERN is omitted, it defaults to \"[ \\f\\t\\n\\r\\v]+\"."
366 (setq pattern "[ \f\t\n\r\v]+"))
367 ;; The FSF version of this function takes care not to cons in case
368 ;; of infloop. Maybe we should synch?
369 (let (parts (start 0))
370 (while (string-match pattern string start)
371 (setq parts (cons (substring string start (match-beginning 0)) parts)
372 start (match-end 0)))
373 (nreverse (cons (substring string start) parts))))
376 ;;; @ Emacs 20.3 emulation
379 ;; imported from Emacs 20.3.91.
380 (defvar-maybe temporary-file-directory
381 (file-name-as-directory
382 (cond ((memq system-type '(ms-dos windows-nt))
383 (or (getenv "TEMP") (getenv "TMPDIR") (getenv "TMP") "c:/temp"))
384 ((memq system-type '(vax-vms axp-vms))
385 (or (getenv "TMPDIR") (getenv "TMP") (getenv "TEMP") "SYS$SCRATCH:"))
387 (or (getenv "TMPDIR") (getenv "TMP") (getenv "TEMP") "/tmp"))))
388 "The directory for writing temporary files.")
390 (defun-maybe line-beginning-position (&optional n)
391 "Return the character position of the first character on the current line.
392 With argument N not nil or 1, move forward N - 1 lines first.
393 If scan reaches end of buffer, return that position.
394 This function does not move point."
397 (forward-line (1- n))
402 (defun-maybe line-end-position (&optional n)
403 "Return the character position of the last character on the current line.
404 With argument N not nil or 1, move forward N - 1 lines first.
405 If scan reaches end of buffer, return that position.
406 This function does not move point."
409 (forward-line (1- n))
414 (defun-maybe string (&rest chars)
415 "Concatenate all the argument characters and make the result a string."
416 (mapconcat (function char-to-string) chars "")
420 ;;; @ XEmacs emulation
423 (defun-maybe find-face (face-or-name)
424 "Retrieve the face of the given name.
425 If FACE-OR-NAME is a face object, it is simply returned.
426 Otherwise, FACE-OR-NAME should be a symbol. If there is no such face,
427 nil is returned. Otherwise the associated face object is returned.
428 \[XEmacs emulating function]"
429 (car (memq face-or-name (face-list)))
432 (defun-maybe point-at-bol (&optional n buffer)
433 "Return the character position of the first character on the current line.
434 With argument N not nil or 1, move forward N - 1 lines first.
435 If scan reaches end of buffer, return that position.
436 This function does not move point. [XEmacs emulating function]"
441 (line-beginning-position n)
444 (defun-maybe point-at-eol (&optional n buffer)
445 "Return the character position of the last character on the current line.
446 With argument N not nil or 1, move forward N - 1 lines first.
447 If scan reaches end of buffer, return that position.
448 This function does not move point. [XEmacs emulating function]"
453 (line-end-position n)
456 (defun-maybe functionp (obj)
457 "Returns t if OBJ is a function, nil otherwise.
458 \[XEmacs emulating function]"
460 (byte-code-function-p obj)
461 (and (symbolp obj)(fboundp obj))
462 (and (consp obj)(eq (car obj) 'lambda))
465 (defsubst-maybe define-obsolete-function-alias (oldfun newfun)
466 "Define OLDFUN as an obsolete alias for function NEWFUN.
467 This makes calling OLDFUN equivalent to calling NEWFUN and marks OLDFUN
468 as obsolete. [XEmacs emulating function]"
469 (defalias oldfun newfun)
470 (make-obsolete oldfun newfun)