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'.
34 (defmacro defun-maybe (name &rest everything-else)
35 (or (and (fboundp name)
36 (not (get name 'defun-maybe)))
37 (` (or (fboundp (quote (, name)))
39 (defun (, name) (,@ everything-else))
40 (put (quote (, name)) 'defun-maybe t)
44 (defmacro defmacro-maybe (name &rest everything-else)
45 (or (and (fboundp name)
46 (not (get name 'defmacro-maybe)))
47 (` (or (fboundp (quote (, name)))
49 (defmacro (, name) (,@ everything-else))
50 (put (quote (, name)) 'defmacro-maybe t)
54 (defmacro-maybe defsubst (name arglist &rest body)
55 "Define an inline function. The syntax is just like that of `defun'."
56 (cons 'defun (cons name (cons arglist body)))
59 (defmacro defsubst-maybe (name &rest everything-else)
60 (or (and (fboundp name)
61 (not (get name 'defsubst-maybe)))
62 (` (or (fboundp (quote (, name)))
64 (defsubst (, name) (,@ everything-else))
65 (put (quote (, name)) 'defsubst-maybe t)
69 (defmacro defalias-maybe (symbol definition)
70 (setq symbol (eval symbol))
71 (or (and (fboundp symbol)
72 (not (get symbol 'defalias-maybe)))
73 (` (or (fboundp (quote (, symbol)))
75 (defalias (quote (, symbol)) (, definition))
76 (put (quote (, symbol)) 'defalias-maybe t)
80 (put 'defun-maybe 'lisp-indent-function 'defun)
81 (put 'defsubst-maybe 'lisp-indent-function 'defun)
82 (put 'defmacro-maybe 'lisp-indent-function 'defun)
84 (defmacro defvar-maybe (name &rest everything-else)
85 (or (and (boundp name)
86 (not (get name 'defvar-maybe)))
87 (` (or (boundp (quote (, name)))
89 (defvar (, name) (,@ everything-else))
90 (put (quote (, name)) 'defvar-maybe t)
94 (defmacro defconst-maybe (name &rest everything-else)
95 (or (and (boundp name)
96 (not (get name 'defconst-maybe))
98 (` (or (boundp (quote (, name)))
100 (defconst (, name) (,@ everything-else))
101 (put (quote (, name)) 'defconst-maybe t)
105 (defmacro defun-maybe-cond (name args &optional doc &rest everything-else)
106 (unless (stringp doc)
107 (setq everything-else (cons doc everything-else)
110 (or (and (fboundp name)
111 (not (get name 'defun-maybe)))
112 (` (unless (fboundp (quote (, name)))
113 (cond (,@ (mapcar (function
117 (` (defun (, name) (, args)
120 (` (defun (, name) (, args)
124 (put (quote (, name)) 'defun-maybe t)
127 (defsubst subr-fboundp (symbol)
128 "Return t if SYMBOL's function definition is a built-in function."
129 (and (fboundp symbol)
130 (subrp (symbol-function symbol))))
132 (defconst-maybe emacs-major-version (string-to-int emacs-version))
133 (defconst-maybe emacs-minor-version
135 (substring emacs-version
136 (string-match (format "%d\\." emacs-major-version)
139 (defmacro condition-case-if (cond var bodyform &rest handlers)
140 "If COND yields non-nil at run-time, do wrapped BODYFORM in
141 `condition-case' with VAR and HANDLERS, else do bare BODYFORM."
143 (condition-case (, var)
148 (put 'condition-case-if 'lisp-indent-function 2)
150 (cond ((featurep 'xemacs)
151 (require 'poe-xemacs)
153 ((string-match "XEmacs" emacs-version)
155 (require 'poe-xemacs)
157 ((> emacs-major-version 20))
158 ((= emacs-major-version 20)
159 (cond ((subr-fboundp 'string)
160 ;; Emacs 20.3 or later
162 ((subr-fboundp 'concat-chars)
163 ;; Emacs 20.1 or later
164 (defalias 'string 'concat-chars)
167 ((= emacs-major-version 19))
172 ;;; @ Emacs 19.23 emulation
175 (defun-maybe minibuffer-prompt-width ()
176 "Return the display width of the minibuffer prompt."
178 (set-buffer (window-buffer (minibuffer-window)))
182 ;;; @ Emacs 19.29 emulation
185 (defvar-maybe path-separator ":"
186 "Character used to separate concatenated paths.")
188 (defun-maybe buffer-substring-no-properties (start end)
189 "Return the characters of part of the buffer, without the text properties.
190 The two arguments START and END are character positions;
191 they can be in either order. [Emacs 19.29 emulating function]"
192 (let ((string (buffer-substring start end)))
193 (set-text-properties 0 (length string) nil string)
196 (defun-maybe match-string (num &optional string)
197 "Return string of text matched by last search.
198 NUM specifies which parenthesized expression in the last regexp.
199 Value is nil if NUMth pair didn't match, or there were less than NUM pairs.
200 Zero means the entire text matched by the whole regexp or whole string.
201 STRING should be given if the last search was by `string-match' on STRING.
202 \[Emacs 19.29 emulating function]"
203 (if (match-beginning num)
205 (substring string (match-beginning num) (match-end num))
206 (buffer-substring (match-beginning num) (match-end num)))))
208 (or (featurep 'xemacs)
209 (>= emacs-major-version 20)
210 (and (= emacs-major-version 19)
211 (>= emacs-minor-version 29))
212 ;; for Emacs 19.28 or earlier
213 (fboundp 'si:read-string)
215 (fset 'si:read-string (symbol-function 'read-string))
216 (defun read-string (prompt &optional initial-input history)
217 "Read a string from the minibuffer, prompting with string PROMPT.
218 If non-nil, second arg INITIAL-INPUT is a string to insert before reading.
219 The third arg HISTORY, is dummy for compatibility.
220 See `read-from-minibuffer' for details of HISTORY argument."
221 (si:read-string prompt initial-input))
224 (defun-maybe rassoc (key list)
225 "Return non-nil if KEY is `equal' to the cdr of an element of LIST.
226 The value is actually the element of LIST whose cdr equals KEY."
229 (if (equal (cdr (car list)) key)
230 (throw 'found (car list))
232 (setq list (cdr list)))
235 (defun-maybe file-name-sans-extension (filename)
236 "Return FILENAME sans final \"extension\".
237 The extension, in a file name, is the part that follows the last `.'."
239 (let ((file (file-name-sans-versions (file-name-nondirectory filename)))
241 (if (string-match "\\.[^.]*\\'" file)
242 (if (setq directory (file-name-directory filename))
243 (expand-file-name (substring file 0 (match-beginning 0))
245 (substring file 0 (match-beginning 0)))
248 (defmacro-maybe make-local-hook (hook))
250 ;; They are not Emacs features
252 (defmacro-maybe add-local-hook (hook function &optional append)
253 (if (fboundp 'make-local-hook)
254 (list 'add-hook hook function append t)
255 (list 'add-hook hook function append)
258 (defmacro-maybe remove-local-hook (hook function)
259 (if (fboundp 'make-local-hook)
260 (list 'remove-hook hook function t)
261 (list 'remove-hook hook function)
265 ;;; @ Emacs 19.30 emulation
268 ;; imported from Emacs 19.30.
269 (defun-maybe add-to-list (list-var element)
270 "Add to the value of LIST-VAR the element ELEMENT if it isn't there yet.
271 If you want to use `add-to-list' on a variable that is not defined
272 until a certain package is loaded, you should put the call to `add-to-list'
273 into a hook function that will be run only after loading the package.
274 \[Emacs 19.30 emulating function]"
275 (or (member element (symbol-value list-var))
276 (set list-var (cons element (symbol-value list-var)))))
278 (cond ((fboundp 'insert-file-contents-literally))
279 ((boundp 'file-name-handler-alist)
280 (defun insert-file-contents-literally
281 (filename &optional visit beg end replace)
282 "Like `insert-file-contents', q.v., but only reads in the file.
283 A buffer may be modified in several ways after reading into the buffer due
284 to advanced Emacs features, such as file-name-handlers, format decoding,
285 find-file-hooks, etc.
286 This function ensures that none of these modifications will take place.
287 \[Emacs 19.30 emulating function]"
288 (let (file-name-handler-alist)
289 (insert-file-contents filename visit beg end replace)))
292 (defalias 'insert-file-contents-literally 'insert-file-contents)
296 ;;; @ Emacs 19.31 emulation
299 (defun-maybe buffer-live-p (object)
300 "Return non-nil if OBJECT is a buffer which has not been killed.
301 Value is nil if OBJECT is not a buffer or if it has been killed.
302 \[Emacs 19.31 emulating function]"
305 (buffer-name (get-buffer object))))
307 ;; imported from Emacs 19.33.
308 (defmacro-maybe save-selected-window (&rest body)
309 "Execute BODY, then select the window that was selected before BODY.
310 \[Emacs 19.31 emulating function]"
312 '((save-selected-window-window (selected-window)))
313 (list 'unwind-protect
315 (list 'select-window 'save-selected-window-window))))
318 ;;; @ Emacs 20.1 emulation
321 ;; imported from Emacs 20.2.
322 (defmacro-maybe when (cond &rest body)
323 "(when COND BODY...): if COND yields non-nil, do BODY, else return nil."
324 (list 'if cond (cons 'progn body)))
326 ;; imported from Emacs 20.3.
327 (defmacro-maybe unless (cond &rest body)
328 "(unless COND BODY...): if COND yields nil, do BODY, else return nil."
329 (cons 'if (cons cond (cons nil body))))
331 ;; imported from Emacs 20.3.
332 (defun-maybe last (x &optional n)
333 "Return the last link of the list X. Its car is the last element.
334 If X is nil, return nil.
335 If N is non-nil, return the Nth-to-last link of X.
336 If N is bigger than the length of X, return X."
340 (setq m (1+ m) p (cdr p)))
342 (if (< n m) (nthcdr (- m n) x) x)))
347 (defmacro-maybe save-current-buffer (&rest body)
348 "Save the current buffer; execute BODY; restore the current buffer.
349 Executes BODY just like `progn'."
350 (` (let ((orig-buffer (current-buffer)))
353 (set-buffer orig-buffer)))))
355 ;; imported from Emacs 20.2.
356 (defmacro-maybe with-current-buffer (buffer &rest body)
357 "Execute the forms in BODY with BUFFER as the current buffer.
358 The value returned is the value of the last form in BODY.
359 See also `with-temp-buffer'."
360 (` (save-current-buffer
361 (set-buffer (, buffer))
364 ;; imported from Emacs 20.2.
365 (defmacro-maybe with-temp-file (file &rest forms)
366 "Create a new buffer, evaluate FORMS there, and write the buffer to FILE.
367 The value of the last form in FORMS is returned, like `progn'.
368 See also `with-temp-buffer'."
369 (let ((temp-file (make-symbol "temp-file"))
370 (temp-buffer (make-symbol "temp-buffer")))
371 (` (let (((, temp-file) (, file))
373 (get-buffer-create (generate-new-buffer-name " *temp file*"))))
376 (with-current-buffer (, temp-buffer)
378 (with-current-buffer (, temp-buffer)
380 (write-region (point-min) (point-max) (, temp-file) nil 0)))
381 (and (buffer-name (, temp-buffer))
382 (kill-buffer (, temp-buffer))))))))
384 ;; imported from Emacs 20.2.
385 (defmacro-maybe with-temp-buffer (&rest forms)
386 "Create a temporary buffer, and evaluate FORMS there like `progn'.
387 See also `with-temp-file' and `with-output-to-string'."
388 (let ((temp-buffer (make-symbol "temp-buffer")))
389 (` (let (((, temp-buffer)
390 (get-buffer-create (generate-new-buffer-name " *temp*"))))
392 (with-current-buffer (, temp-buffer)
394 (and (buffer-name (, temp-buffer))
395 (kill-buffer (, temp-buffer))))))))
397 (defmacro-maybe combine-after-change-calls (&rest body)
401 ;; imported from Emacs 20.3. (cl function)
402 (defun-maybe butlast (x &optional n)
403 "Returns a copy of LIST with the last N elements removed."
404 (if (and n (<= n 0)) x
405 (nbutlast (copy-sequence x) n)))
407 ;; imported from Emacs 20.3. (cl function)
408 (defun-maybe nbutlast (x &optional n)
409 "Modifies LIST to remove the last N elements."
410 (let ((m (length x)))
414 (if (> n 0) (setcdr (nthcdr (- (1- m) n) x) nil))
417 ;; imported from XEmacs 21.
418 (defun-maybe split-string (string &optional pattern)
419 "Return a list of substrings of STRING which are separated by PATTERN.
420 If PATTERN is omitted, it defaults to \"[ \\f\\t\\n\\r\\v]+\"."
422 (setq pattern "[ \f\t\n\r\v]+"))
423 ;; The FSF version of this function takes care not to cons in case
424 ;; of infloop. Maybe we should synch?
425 (let (parts (start 0))
426 (while (string-match pattern string start)
427 (setq parts (cons (substring string start (match-beginning 0)) parts)
428 start (match-end 0)))
429 (nreverse (cons (substring string start) parts))))
432 ;;; @ Emacs 20.3 emulation
435 ;; imported from Emacs 20.3.91.
436 (defvar-maybe temporary-file-directory
437 (file-name-as-directory
438 (cond ((memq system-type '(ms-dos windows-nt))
439 (or (getenv "TEMP") (getenv "TMPDIR") (getenv "TMP") "c:/temp"))
440 ((memq system-type '(vax-vms axp-vms))
441 (or (getenv "TMPDIR") (getenv "TMP") (getenv "TEMP") "SYS$SCRATCH:"))
443 (or (getenv "TMPDIR") (getenv "TMP") (getenv "TEMP") "/tmp"))))
444 "The directory for writing temporary files.")
446 (defun-maybe line-beginning-position (&optional n)
447 "Return the character position of the first character on the current line.
448 With argument N not nil or 1, move forward N - 1 lines first.
449 If scan reaches end of buffer, return that position.
450 This function does not move point."
453 (forward-line (1- n))
458 (defun-maybe line-end-position (&optional n)
459 "Return the character position of the last character on the current line.
460 With argument N not nil or 1, move forward N - 1 lines first.
461 If scan reaches end of buffer, return that position.
462 This function does not move point."
465 (forward-line (1- n))
470 (defun-maybe string (&rest chars)
471 "Concatenate all the argument characters and make the result a string."
472 (mapconcat (function char-to-string) chars "")
476 ;;; @ XEmacs emulation
479 (defun-maybe find-face (face-or-name)
480 "Retrieve the face of the given name.
481 If FACE-OR-NAME is a face object, it is simply returned.
482 Otherwise, FACE-OR-NAME should be a symbol. If there is no such face,
483 nil is returned. Otherwise the associated face object is returned.
484 \[XEmacs emulating function]"
485 (car (memq face-or-name (face-list)))
488 (defun-maybe point-at-bol (&optional n buffer)
489 "Return the character position of the first character on the current line.
490 With argument N not nil or 1, move forward N - 1 lines first.
491 If scan reaches end of buffer, return that position.
492 This function does not move point. [XEmacs emulating function]"
497 (line-beginning-position n)
500 (defun-maybe point-at-eol (&optional n buffer)
501 "Return the character position of the last character on the current line.
502 With argument N not nil or 1, move forward N - 1 lines first.
503 If scan reaches end of buffer, return that position.
504 This function does not move point. [XEmacs emulating function]"
509 (line-end-position n)
512 (defun-maybe functionp (obj)
513 "Returns t if OBJ is a function, nil otherwise.
514 \[XEmacs emulating function]"
516 (byte-code-function-p obj)
517 (and (symbolp obj)(fboundp obj))
518 (and (consp obj)(eq (car obj) 'lambda))
521 (defsubst-maybe define-obsolete-function-alias (oldfun newfun)
522 "Define OLDFUN as an obsolete alias for function NEWFUN.
523 This makes calling OLDFUN equivalent to calling NEWFUN and marks OLDFUN
524 as obsolete. [XEmacs emulating function]"
525 (defalias oldfun newfun)
526 (make-obsolete oldfun newfun)
529 (when (subr-fboundp 'read-event)
530 ;; for Emacs 19 or later
532 (defun-maybe-cond next-command-event (&optional event prompt)
533 "Read an event object from the input stream.
534 If EVENT is non-nil, it should be an event object and will be filled
535 in and returned; otherwise a new event object will be created and
537 If PROMPT is non-nil, it should be a string and will be displayed in
538 the echo area while this function is waiting for an event.
539 \[XEmacs emulating function]"
540 ((subr-fboundp 'string)
541 ;; for Emacs 20.3 or later
542 (read-event prompt t)
545 (if prompt (message prompt))
549 (defsubst-maybe character-to-event (ch)
550 "Convert keystroke CH into an event structure, replete with bucky bits.
551 Note that CH (the keystroke specifier) can be an integer, a character
552 or a symbol such as 'clear. [XEmacs emulating function]"
555 (defun-maybe event-to-character (event)
556 "Return the character approximation to the given event object.
557 If the event isn't a keypress, this returns nil.
558 \[XEmacs emulating function]"
559 (cond ((symbolp event)
560 ;; mask is (BASE-TYPE MODIFIER-BITS) or nil.
561 (let ((mask (get event 'event-symbol-element-mask)))
563 (let ((base (get (car mask) 'ascii-character)))
565 (logior base (car (cdr mask)))
567 ((integerp event) event)
572 ;;; @ MULE 2 emulation
575 (defun-maybe-cond cancel-undo-boundary ()
576 "Cancel undo boundary. [MULE 2.3 emulating function]"
577 ((boundp 'buffer-undo-list)
578 ;; for Emacs 19.7 or later
579 (if (and (consp buffer-undo-list)
581 (null (car buffer-undo-list)))
582 (setq buffer-undo-list (cdr buffer-undo-list))
585 ;; for anything older than Emacs 19.7.