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 defmacro-maybe (name &rest everything-else)
43 (or (and (fboundp name)
44 (not (get name 'defmacro-maybe)))
45 (` (or (fboundp (quote (, name)))
47 (defmacro (, name) (,@ everything-else))
48 (put (quote (, name)) 'defmacro-maybe t)
52 (defmacro-maybe defsubst (name arglist &rest body)
53 "Define an inline function. The syntax is just like that of `defun'."
54 (cons 'defun (cons name (cons arglist body)))
57 (defmacro defsubst-maybe (name &rest everything-else)
58 (or (and (fboundp name)
59 (not (get name 'defsubst-maybe)))
60 (` (or (fboundp (quote (, name)))
62 (defsubst (, name) (,@ everything-else))
63 (put (quote (, name)) 'defsubst-maybe t)
67 (defmacro defalias-maybe (symbol definition)
68 (setq symbol (eval symbol))
69 (or (and (fboundp symbol)
70 (not (get symbol 'defalias-maybe)))
71 (` (or (fboundp (quote (, symbol)))
73 (defalias (quote (, symbol)) (, definition))
74 (put (quote (, symbol)) 'defalias-maybe t)
78 (put 'defun-maybe 'lisp-indent-function 'defun)
79 (put 'defsubst-maybe 'lisp-indent-function 'defun)
80 (put 'defmacro-maybe 'lisp-indent-function 'defun)
82 (defmacro defvar-maybe (name &rest everything-else)
83 (or (and (boundp name)
84 (not (get name 'defvar-maybe)))
85 (` (or (boundp (quote (, name)))
87 (defvar (, name) (,@ everything-else))
88 (put (quote (, name)) 'defvar-maybe t)
92 (defmacro defconst-maybe (name &rest everything-else)
93 (or (and (boundp name)
94 (not (get name 'defconst-maybe))
96 (` (or (boundp (quote (, name)))
98 (defconst (, name) (,@ everything-else))
99 (put (quote (, name)) 'defconst-maybe t)
103 (defmacro defun-maybe-cond (name args &optional doc &rest everything-else)
104 (unless (stringp doc)
105 (setq everything-else (cons doc everything-else)
108 (or (and (fboundp name)
109 (not (get name 'defun-maybe)))
110 (` (unless (fboundp (quote (, name)))
111 (cond (,@ (mapcar (function
115 (` (defun (, name) (, args)
118 (` (defun (, name) (, args)
122 (put (quote (, name)) 'defun-maybe t)
125 (defsubst subr-fboundp (symbol)
126 "Return t if SYMBOL's function definition is a built-in function."
127 (and (fboundp symbol)
128 (subrp (symbol-function symbol))))
130 (defconst-maybe emacs-major-version (string-to-int emacs-version))
131 (defconst-maybe emacs-minor-version
133 (substring emacs-version
134 (string-match (format "%d\\." emacs-major-version)
137 (cond ((featurep 'xemacs)
138 (require 'poe-xemacs)
140 ((string-match "XEmacs" emacs-version)
142 (require 'poe-xemacs)
144 ((> emacs-major-version 20))
145 ((= emacs-major-version 20)
146 (cond ((subr-fboundp 'string)
147 ;; Emacs 20.3 or later
149 ((subr-fboundp 'concat-chars)
150 ;; Emacs 20.1 or later
151 (defalias 'string 'concat-chars)
154 ((= emacs-major-version 19))
160 ;;; @ Emacs 19.23 emulation
163 (defun-maybe minibuffer-prompt-width ()
164 "Return the display width of the minibuffer prompt."
166 (set-buffer (window-buffer (minibuffer-window)))
170 ;;; @ Emacs 19.29 emulation
173 (defvar-maybe path-separator ":"
174 "Character used to separate concatenated paths.")
176 (defun-maybe buffer-substring-no-properties (start end)
177 "Return the characters of part of the buffer, without the text properties.
178 The two arguments START and END are character positions;
179 they can be in either order. [Emacs 19.29 emulating function]"
180 (let ((string (buffer-substring start end)))
181 (set-text-properties 0 (length string) nil string)
184 (defun-maybe match-string (num &optional string)
185 "Return string of text matched by last search.
186 NUM specifies which parenthesized expression in the last regexp.
187 Value is nil if NUMth pair didn't match, or there were less than NUM pairs.
188 Zero means the entire text matched by the whole regexp or whole string.
189 STRING should be given if the last search was by `string-match' on STRING.
190 \[Emacs 19.29 emulating function]"
191 (if (match-beginning num)
193 (substring string (match-beginning num) (match-end num))
194 (buffer-substring (match-beginning num) (match-end num)))))
196 (or (featurep 'xemacs)
197 (>= emacs-major-version 20)
198 (and (= emacs-major-version 19)
199 (>= emacs-minor-version 29))
200 ;; for Emacs 19.28 or earlier
201 (fboundp 'si:read-string)
203 (fset 'si:read-string (symbol-function 'read-string))
204 (defun read-string (prompt &optional initial-input history)
205 "Read a string from the minibuffer, prompting with string PROMPT.
206 If non-nil, second arg INITIAL-INPUT is a string to insert before reading.
207 The third arg HISTORY, is dummy for compatibility.
208 See `read-from-minibuffer' for details of HISTORY argument."
209 (si:read-string prompt initial-input))
212 (defun-maybe rassoc (key list)
213 "Return non-nil if KEY is `equal' to the cdr of an element of LIST.
214 The value is actually the element of LIST whose cdr equals KEY."
217 (if (equal (cdr (car list)) key)
218 (throw 'found (car list))
220 (setq list (cdr list)))
223 (defmacro-maybe make-local-hook (hook))
225 ;; They are not Emacs features
227 (defmacro-maybe add-local-hook (hook function &optional append)
228 (if (fboundp 'make-local-hook)
229 (list 'add-hook hook function append t)
230 (list 'add-hook hook function append)
233 (defmacro-maybe remove-local-hook (hook function)
234 (if (fboundp 'make-local-hook)
235 (list 'remove-hook hook function t)
236 (list 'remove-hook hook function)
240 ;;; @ Emacs 19.30 emulation
243 ;; imported from Emacs 19.30.
244 (defun-maybe add-to-list (list-var element)
245 "Add to the value of LIST-VAR the element ELEMENT if it isn't there yet.
246 If you want to use `add-to-list' on a variable that is not defined
247 until a certain package is loaded, you should put the call to `add-to-list'
248 into a hook function that will be run only after loading the package.
249 \[Emacs 19.30 emulating function]"
250 (or (member element (symbol-value list-var))
251 (set list-var (cons element (symbol-value list-var)))))
253 (cond ((fboundp 'insert-file-contents-literally))
254 ((boundp 'file-name-handler-alist)
255 (defun insert-file-contents-literally
256 (filename &optional visit beg end replace)
257 "Like `insert-file-contents', q.v., but only reads in the file.
258 A buffer may be modified in several ways after reading into the buffer due
259 to advanced Emacs features, such as file-name-handlers, format decoding,
260 find-file-hooks, etc.
261 This function ensures that none of these modifications will take place.
262 \[Emacs 19.30 emulating function]"
263 (let (file-name-handler-alist)
264 (insert-file-contents filename visit beg end replace)))
267 (defalias 'insert-file-contents-literally 'insert-file-contents)
271 ;;; @ Emacs 19.31 emulation
274 (defun-maybe buffer-live-p (object)
275 "Return non-nil if OBJECT is a buffer which has not been killed.
276 Value is nil if OBJECT is not a buffer or if it has been killed.
277 \[Emacs 19.31 emulating function]"
280 (buffer-name (get-buffer object))))
282 ;; imported from Emacs 19.33.
283 (defmacro-maybe save-selected-window (&rest body)
284 "Execute BODY, then select the window that was selected before BODY.
285 \[Emacs 19.31 emulating function]"
287 '((save-selected-window-window (selected-window)))
288 (list 'unwind-protect
290 (list 'select-window 'save-selected-window-window))))
293 ;;; @ Emacs 20.1 emulation
296 ;; imported from Emacs 20.2.
297 (defmacro-maybe when (cond &rest body)
298 "(when COND BODY...): if COND yields non-nil, do BODY, else return nil."
299 (list 'if cond (cons 'progn body)))
301 ;; imported from Emacs 20.3.
302 (defmacro-maybe unless (cond &rest body)
303 "(unless COND BODY...): if COND yields nil, do BODY, else return nil."
304 (cons 'if (cons cond (cons nil body))))
306 ;; imported from Emacs 20.3.
307 (defun-maybe last (x &optional n)
308 "Return the last link of the list X. Its car is the last element.
309 If X is nil, return nil.
310 If N is non-nil, return the Nth-to-last link of X.
311 If N is bigger than the length of X, return X."
315 (setq m (1+ m) p (cdr p)))
317 (if (< n m) (nthcdr (- m n) x) x)))
322 (defmacro-maybe save-current-buffer (&rest body)
323 "Save the current buffer; execute BODY; restore the current buffer.
324 Executes BODY just like `progn'."
325 (` (let ((orig-buffer (current-buffer)))
328 (set-buffer orig-buffer)))))
330 ;; imported from Emacs 20.2.
331 (defmacro-maybe with-current-buffer (buffer &rest body)
332 "Execute the forms in BODY with BUFFER as the current buffer.
333 The value returned is the value of the last form in BODY.
334 See also `with-temp-buffer'."
335 (` (save-current-buffer
336 (set-buffer (, buffer))
339 ;; imported from Emacs 20.2.
340 (defmacro-maybe with-temp-file (file &rest forms)
341 "Create a new buffer, evaluate FORMS there, and write the buffer to FILE.
342 The value of the last form in FORMS is returned, like `progn'.
343 See also `with-temp-buffer'."
344 (let ((temp-file (make-symbol "temp-file"))
345 (temp-buffer (make-symbol "temp-buffer")))
346 (` (let (((, temp-file) (, file))
348 (get-buffer-create (generate-new-buffer-name " *temp file*"))))
351 (with-current-buffer (, temp-buffer)
353 (with-current-buffer (, temp-buffer)
355 (write-region (point-min) (point-max) (, temp-file) nil 0)))
356 (and (buffer-name (, temp-buffer))
357 (kill-buffer (, temp-buffer))))))))
359 ;; imported from Emacs 20.2.
360 (defmacro-maybe with-temp-buffer (&rest forms)
361 "Create a temporary buffer, and evaluate FORMS there like `progn'.
362 See also `with-temp-file' and `with-output-to-string'."
363 (let ((temp-buffer (make-symbol "temp-buffer")))
364 (` (let (((, temp-buffer)
365 (get-buffer-create (generate-new-buffer-name " *temp*"))))
367 (with-current-buffer (, temp-buffer)
369 (and (buffer-name (, temp-buffer))
370 (kill-buffer (, temp-buffer))))))))
372 (defmacro-maybe combine-after-change-calls (&rest body)
376 ;; imported from Emacs 20.3. (cl function)
377 (defun-maybe butlast (x &optional n)
378 "Returns a copy of LIST with the last N elements removed."
379 (if (and n (<= n 0)) x
380 (nbutlast (copy-sequence x) n)))
382 ;; imported from Emacs 20.3. (cl function)
383 (defun-maybe nbutlast (x &optional n)
384 "Modifies LIST to remove the last N elements."
385 (let ((m (length x)))
389 (if (> n 0) (setcdr (nthcdr (- (1- m) n) x) nil))
392 ;; imported from XEmacs 21.
393 (defun-maybe split-string (string &optional pattern)
394 "Return a list of substrings of STRING which are separated by PATTERN.
395 If PATTERN is omitted, it defaults to \"[ \\f\\t\\n\\r\\v]+\"."
397 (setq pattern "[ \f\t\n\r\v]+"))
398 ;; The FSF version of this function takes care not to cons in case
399 ;; of infloop. Maybe we should synch?
400 (let (parts (start 0))
401 (while (string-match pattern string start)
402 (setq parts (cons (substring string start (match-beginning 0)) parts)
403 start (match-end 0)))
404 (nreverse (cons (substring string start) parts))))
407 ;;; @ Emacs 20.3 emulation
410 ;; imported from Emacs 20.3.91.
411 (defvar-maybe temporary-file-directory
412 (file-name-as-directory
413 (cond ((memq system-type '(ms-dos windows-nt))
414 (or (getenv "TEMP") (getenv "TMPDIR") (getenv "TMP") "c:/temp"))
415 ((memq system-type '(vax-vms axp-vms))
416 (or (getenv "TMPDIR") (getenv "TMP") (getenv "TEMP") "SYS$SCRATCH:"))
418 (or (getenv "TMPDIR") (getenv "TMP") (getenv "TEMP") "/tmp"))))
419 "The directory for writing temporary files.")
421 (defun-maybe line-beginning-position (&optional n)
422 "Return the character position of the first character on the current line.
423 With argument N not nil or 1, move forward N - 1 lines first.
424 If scan reaches end of buffer, return that position.
425 This function does not move point."
428 (forward-line (1- n))
433 (defun-maybe line-end-position (&optional n)
434 "Return the character position of the last character on the current line.
435 With argument N not nil or 1, move forward N - 1 lines first.
436 If scan reaches end of buffer, return that position.
437 This function does not move point."
440 (forward-line (1- n))
445 (defun-maybe string (&rest chars)
446 "Concatenate all the argument characters and make the result a string."
447 (mapconcat (function char-to-string) chars "")
451 ;;; @ XEmacs emulation
454 (defun-maybe find-face (face-or-name)
455 "Retrieve the face of the given name.
456 If FACE-OR-NAME is a face object, it is simply returned.
457 Otherwise, FACE-OR-NAME should be a symbol. If there is no such face,
458 nil is returned. Otherwise the associated face object is returned.
459 \[XEmacs emulating function]"
460 (car (memq face-or-name (face-list)))
463 (defun-maybe point-at-bol (&optional n buffer)
464 "Return the character position of the first character on the current line.
465 With argument N not nil or 1, move forward N - 1 lines first.
466 If scan reaches end of buffer, return that position.
467 This function does not move point. [XEmacs emulating function]"
472 (line-beginning-position n)
475 (defun-maybe point-at-eol (&optional n buffer)
476 "Return the character position of the last character on the current line.
477 With argument N not nil or 1, move forward N - 1 lines first.
478 If scan reaches end of buffer, return that position.
479 This function does not move point. [XEmacs emulating function]"
484 (line-end-position n)
487 (defun-maybe functionp (obj)
488 "Returns t if OBJ is a function, nil otherwise.
489 \[XEmacs emulating function]"
491 (byte-code-function-p obj)
492 (and (symbolp obj)(fboundp obj))
493 (and (consp obj)(eq (car obj) 'lambda))
496 (defsubst-maybe define-obsolete-function-alias (oldfun newfun)
497 "Define OLDFUN as an obsolete alias for function NEWFUN.
498 This makes calling OLDFUN equivalent to calling NEWFUN and marks OLDFUN
499 as obsolete. [XEmacs emulating function]"
500 (defalias oldfun newfun)
501 (make-obsolete oldfun newfun)
504 (when (subr-fboundp 'read-event)
505 ;; for Emacs 19 or later
507 (defun-maybe-cond next-command-event (&optional event prompt)
508 "Read an event object from the input stream.
509 If EVENT is non-nil, it should be an event object and will be filled
510 in and returned; otherwise a new event object will be created and
512 If PROMPT is non-nil, it should be a string and will be displayed in
513 the echo area while this function is waiting for an event.
514 \[XEmacs emulating function]"
515 ((subr-fboundp 'string)
516 ;; for Emacs 20.3 or later
517 (read-event prompt t)
520 (if prompt (message prompt))
524 (defsubst-maybe character-to-event (ch)
525 "Convert keystroke CH into an event structure, replete with bucky bits.
526 Note that CH (the keystroke specifier) can be an integer, a character
527 or a symbol such as 'clear. [XEmacs emulating function]"
530 (defun-maybe event-to-character (event)
531 "Return the character approximation to the given event object.
532 If the event isn't a keypress, this returns nil.
533 \[XEmacs emulating function]"
534 (cond ((symbolp event)
535 ;; mask is (BASE-TYPE MODIFIER-BITS) or nil.
536 (let ((mask (get event 'event-symbol-element-mask)))
538 (let ((base (get (car mask) 'ascii-character)))
540 (logior base (car (cdr mask)))
542 ((integerp event) event)
547 ;;; @ MULE 2 emulation
550 (defun-maybe-cond cancel-undo-boundary ()
551 "Cancel undo boundary. [MULE 2.3 emulating function]"
552 ((boundp 'buffer-undo-list)
553 ;; for Emacs 19.7 or later
554 (if (and (consp buffer-undo-list)
556 (null (car buffer-undo-list)))
557 (setq buffer-undo-list (cdr buffer-undo-list))
560 ;; for anything older than Emacs 19.7.