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)
107 (setq everything-else (cons doc everything-else)
110 (or (and (fboundp name)
111 (not (get name 'defun-maybe)))
112 (` (or (fboundp (quote (, name)))
114 (cond (,@ (mapcar (function
118 (` (defun (, name) (, args)
121 (` (defun (, name) (, args)
125 (put (quote (, name)) 'defun-maybe t)
128 (defsubst subr-fboundp (symbol)
129 "Return t if SYMBOL's function definition is a built-in function."
130 (and (fboundp symbol)
131 (subrp (symbol-function symbol))))
133 (defconst-maybe emacs-major-version (string-to-int emacs-version))
134 (defconst-maybe emacs-minor-version
136 (substring emacs-version
137 (string-match (format "%d\\." emacs-major-version)
140 (cond ((featurep 'xemacs)
141 (require 'poe-xemacs)
143 ((string-match "XEmacs" emacs-version)
145 (require 'poe-xemacs)
147 ((> emacs-major-version 20))
148 ((= emacs-major-version 20)
149 (cond ((subr-fboundp 'string)
150 ;; Emacs 20.3 or later
152 ((subr-fboundp 'concat-chars)
153 ;; Emacs 20.1 or later
154 (defalias 'string 'concat-chars)
157 ((= emacs-major-version 19))
162 ;;; @ Emacs 19.23 emulation
165 (defun-maybe minibuffer-prompt-width ()
166 "Return the display width of the minibuffer prompt."
168 (set-buffer (window-buffer (minibuffer-window)))
171 ;;; @ Emacs 19.29 emulation
174 (eval-when-compile (require 'static))
176 ;; `add-hook' and `remove-hook' are imported from Emacs 19.28
177 ;; (with additional `local' argument).
178 (static-condition-case nil
180 (add-hook 'test-hook 'test 'append 'local)
181 (remove-hook 'test-hook 'test 'local))
183 ;; emulate add-hook/remove-hook for version 18.
184 (defun-maybe add-hook (hook function &optional append local)
185 "Add to the value of HOOK the function FUNCTION.
186 FUNCTION is not added if already present.
187 FUNCTION is added \(if necessary\) at the beginning of the hook list
188 unless the optional argument APPEND is non-nil, in which case
189 FUNCTION is added at the end.
191 The optional fourth argument, LOCAL, if non-nil, says to modify
192 the hook's buffer-local value rather than its default value
193 \(LOCAL is only for emulation\).
195 HOOK should be a symbol, and FUNCTION may be any valid function. If
196 HOOK is void, it is first set to nil. If HOOK's value is a single
197 function, it is changed to a list of functions.
198 \[Emacs 19.29 emulating function]"
201 ;; If the hook value is a single function, turn it into a list.
202 (let ((old (symbol-value hook)))
203 (if (or (not (listp old))
204 (eq (car old) 'lambda))
205 (set hook (list old))))
206 (or (if (consp function)
207 ;; Clever way to tell whether a given lambda-expression
208 ;; is equal to anything in the hook.
209 (let ((tail (assoc (cdr function) (symbol-value hook))))
210 (equal function tail))
211 (memq function (symbol-value hook)))
214 (nconc (symbol-value hook) (list function))
215 (cons function (symbol-value hook))))))
217 (defun-maybe remove-hook (hook function &optional local)
218 "Remove from the value of HOOK the function FUNCTION.
219 HOOK should be a symbol, and FUNCTION may be any valid function. If
220 FUNCTION isn't the value of HOOK, or, if FUNCTION doesn't appear in the
221 list of hooks to run in HOOK, then nothing is done. See `add-hook'.
223 The optional third argument, LOCAL, if non-nil, says to modify
224 the hook's buffer-local value rather than its default value
225 \(LOCAL is only for emulation\).
226 \[Emacs 19.29 emulating function]"
227 (if (or (not (boundp hook))
228 (null (symbol-value hook))
231 (let ((hook-value (symbol-value hook)))
232 (if (consp hook-value)
233 (setq hook-value (delete function hook-value))
234 (if (equal hook-value function)
235 (setq hook-value nil)))
236 (set hook hook-value))))
238 (defun-maybe make-local-hook (hook)
239 "Make the hook HOOK local to the current buffer.
240 This function is only for emulation.
241 \[Emacs 19.29 emulating function]"
244 (wrong-number-of-arguments
245 ;; emulate `local' arg for version 19.28 and earlier.
246 (or (fboundp 'si:add-hook)
248 (fset 'si:add-hook (symbol-function 'add-hook))
249 (defun add-hook (hook function &optional append local)
250 "Add to the value of HOOK the function FUNCTION.
251 FUNCTION is not added if already present.
252 FUNCTION is added \(if necessary\) at the beginning of the hook list
253 unless the optional argument APPEND is non-nil, in which case
254 FUNCTION is added at the end.
256 The optional fourth argument, LOCAL, if non-nil, says to modify
257 the hook's buffer-local value rather than its default value
258 \(LOCAL is only for emulation\).
260 HOOK should be a symbol, and FUNCTION may be any valid function. If
261 HOOK is void, it is first set to nil. If HOOK's value is a single
262 function, it is changed to a list of functions.
263 \[Emacs 19.29 emulating function]"
264 ;; the fourth argument LOCAL is simply ignored.
265 (si:add-hook hook function append))))
267 (or (fboundp 'si:remove-hook)
269 (fset 'si:remove-hook (symbol-function 'remove-hook))
270 (defun remove-hook (hook function &optional local)
271 "Remove from the value of HOOK the function FUNCTION.
272 HOOK should be a symbol, and FUNCTION may be any valid function. If
273 FUNCTION isn't the value of HOOK, or, if FUNCTION doesn't appear in the
274 list of hooks to run in HOOK, then nothing is done. See `add-hook'.
276 The optional third argument, LOCAL, if non-nil, says to modify
277 the hook's buffer-local value rather than its default value
278 \(LOCAL is only for emulation\).
279 \[Emacs 19.29 emulating function]"
280 ;; the third argument LOCAL is simply ignored.
281 (si:remove-hook hook function))))
283 (defun-maybe make-local-hook (hook)
284 "Make the hook HOOK local to the current buffer.
285 This function is only for emulation.
286 \[Emacs 19.29 emulating function]"
290 (defvar-maybe path-separator ":"
291 "Character used to separate concatenated paths.")
293 (defun-maybe buffer-substring-no-properties (start end)
294 "Return the characters of part of the buffer, without the text properties.
295 The two arguments START and END are character positions;
296 they can be in either order. [Emacs 19.29 emulating function]"
297 (let ((string (buffer-substring start end)))
298 (set-text-properties 0 (length string) nil string)
301 (defun-maybe match-string (num &optional string)
302 "Return string of text matched by last search.
303 NUM specifies which parenthesized expression in the last regexp.
304 Value is nil if NUMth pair didn't match, or there were less than NUM pairs.
305 Zero means the entire text matched by the whole regexp or whole string.
306 STRING should be given if the last search was by `string-match' on STRING.
307 \[Emacs 19.29 emulating function]"
308 (if (match-beginning num)
310 (substring string (match-beginning num) (match-end num))
311 (buffer-substring (match-beginning num) (match-end num)))))
313 (or (featurep 'xemacs)
314 (>= emacs-major-version 20)
315 (and (= emacs-major-version 19)
316 (>= emacs-minor-version 29))
317 ;; for Emacs 19.28 or earlier
318 (fboundp 'si:read-string)
320 (fset 'si:read-string (symbol-function 'read-string))
321 (defun read-string (prompt &optional initial-input history)
322 "Read a string from the minibuffer, prompting with string PROMPT.
323 If non-nil, second arg INITIAL-INPUT is a string to insert before reading.
324 The third arg HISTORY, is dummy for compatibility.
325 See `read-from-minibuffer' for details of HISTORY argument."
326 (si:read-string prompt initial-input))
329 (defun-maybe rassoc (key list)
330 "Return non-nil if KEY is `equal' to the cdr of an element of LIST.
331 The value is actually the element of LIST whose cdr equals KEY."
334 (if (equal (cdr (car list)) key)
335 (throw 'found (car list))
337 (setq list (cdr list)))
340 (defun-maybe file-name-sans-extension (filename)
341 "Return FILENAME sans final \"extension\".
342 The extension, in a file name, is the part that follows the last `.'."
344 (let ((file (file-name-sans-versions (file-name-nondirectory filename)))
346 (if (string-match "\\.[^.]*\\'" file)
347 (if (setq directory (file-name-directory filename))
348 (expand-file-name (substring file 0 (match-beginning 0))
350 (substring file 0 (match-beginning 0)))
353 ;;; @ Emacs 19.30 emulation
356 ;; imported from Emacs 19.30.
357 (defun-maybe add-to-list (list-var element)
358 "Add to the value of LIST-VAR the element ELEMENT if it isn't there yet.
359 If you want to use `add-to-list' on a variable that is not defined
360 until a certain package is loaded, you should put the call to `add-to-list'
361 into a hook function that will be run only after loading the package.
362 \[Emacs 19.30 emulating function]"
363 (or (member element (symbol-value list-var))
364 (set list-var (cons element (symbol-value list-var)))))
366 (cond ((fboundp 'insert-file-contents-literally))
367 ((boundp 'file-name-handler-alist)
368 (defun insert-file-contents-literally
369 (filename &optional visit beg end replace)
370 "Like `insert-file-contents', q.v., but only reads in the file.
371 A buffer may be modified in several ways after reading into the buffer due
372 to advanced Emacs features, such as file-name-handlers, format decoding,
373 find-file-hooks, etc.
374 This function ensures that none of these modifications will take place.
375 \[Emacs 19.30 emulating function]"
376 (let (file-name-handler-alist)
377 (insert-file-contents filename visit beg end replace)))
380 (defalias 'insert-file-contents-literally 'insert-file-contents)
384 ;;; @ Emacs 19.31 emulation
387 (defun-maybe buffer-live-p (object)
388 "Return non-nil if OBJECT is a buffer which has not been killed.
389 Value is nil if OBJECT is not a buffer or if it has been killed.
390 \[Emacs 19.31 emulating function]"
393 (buffer-name (get-buffer object))))
395 ;; imported from Emacs 19.33.
396 (defmacro-maybe save-selected-window (&rest body)
397 "Execute BODY, then select the window that was selected before BODY.
398 \[Emacs 19.31 emulating function]"
400 '((save-selected-window-window (selected-window)))
401 (list 'unwind-protect
403 (list 'select-window 'save-selected-window-window))))
406 ;;; @ Emacs 20.1 emulation
409 ;; imported from Emacs 20.2.
410 (defmacro-maybe when (cond &rest body)
411 "(when COND BODY...): if COND yields non-nil, do BODY, else return nil."
412 (list 'if cond (cons 'progn body)))
414 ;; imported from Emacs 20.3.
415 (defmacro-maybe unless (cond &rest body)
416 "(unless COND BODY...): if COND yields nil, do BODY, else return nil."
417 (cons 'if (cons cond (cons nil body))))
419 ;; imported from Emacs 20.3.
420 (defsubst-maybe caar (x)
421 "Return the car of the car of X."
424 ;; imported from Emacs 20.3.
425 (defun-maybe last (x &optional n)
426 "Return the last link of the list X. Its car is the last element.
427 If X is nil, return nil.
428 If N is non-nil, return the Nth-to-last link of X.
429 If N is bigger than the length of X, return X."
433 (setq m (1+ m) p (cdr p)))
435 (if (< n m) (nthcdr (- m n) x) x)))
440 (defmacro-maybe save-current-buffer (&rest body)
441 "Save the current buffer; execute BODY; restore the current buffer.
442 Executes BODY just like `progn'."
443 (` (let ((orig-buffer (current-buffer)))
446 (set-buffer orig-buffer)))))
448 ;; imported from Emacs 20.2.
449 (defmacro-maybe with-current-buffer (buffer &rest body)
450 "Execute the forms in BODY with BUFFER as the current buffer.
451 The value returned is the value of the last form in BODY.
452 See also `with-temp-buffer'."
453 (` (save-current-buffer
454 (set-buffer (, buffer))
457 ;; imported from Emacs 20.2.
458 (defmacro-maybe with-temp-file (file &rest forms)
459 "Create a new buffer, evaluate FORMS there, and write the buffer to FILE.
460 The value of the last form in FORMS is returned, like `progn'.
461 See also `with-temp-buffer'."
462 (let ((temp-file (make-symbol "temp-file"))
463 (temp-buffer (make-symbol "temp-buffer")))
464 (` (let (((, temp-file) (, file))
466 (get-buffer-create (generate-new-buffer-name " *temp file*"))))
469 (with-current-buffer (, temp-buffer)
471 (with-current-buffer (, temp-buffer)
473 (write-region (point-min) (point-max) (, temp-file) nil 0)))
474 (and (buffer-name (, temp-buffer))
475 (kill-buffer (, temp-buffer))))))))
477 ;; imported from Emacs 20.2.
478 (defmacro-maybe with-temp-buffer (&rest forms)
479 "Create a temporary buffer, and evaluate FORMS there like `progn'.
480 See also `with-temp-file' and `with-output-to-string'."
481 (let ((temp-buffer (make-symbol "temp-buffer")))
482 (` (let (((, temp-buffer)
483 (get-buffer-create (generate-new-buffer-name " *temp*"))))
485 (with-current-buffer (, temp-buffer)
487 (and (buffer-name (, temp-buffer))
488 (kill-buffer (, temp-buffer))))))))
490 (defmacro-maybe combine-after-change-calls (&rest body)
494 ;; imported from Emacs 20.3. (cl function)
495 (defun-maybe butlast (x &optional n)
496 "Returns a copy of LIST with the last N elements removed."
497 (if (and n (<= n 0)) x
498 (nbutlast (copy-sequence x) n)))
500 ;; imported from Emacs 20.3. (cl function)
501 (defun-maybe nbutlast (x &optional n)
502 "Modifies LIST to remove the last N elements."
503 (let ((m (length x)))
507 (if (> n 0) (setcdr (nthcdr (- (1- m) n) x) nil))
510 ;; imported from XEmacs 21.
511 (defun-maybe split-string (string &optional pattern)
512 "Return a list of substrings of STRING which are separated by PATTERN.
513 If PATTERN is omitted, it defaults to \"[ \\f\\t\\n\\r\\v]+\"."
515 (setq pattern "[ \f\t\n\r\v]+"))
516 ;; The FSF version of this function takes care not to cons in case
517 ;; of infloop. Maybe we should synch?
518 (let (parts (start 0))
519 (while (string-match pattern string start)
520 (setq parts (cons (substring string start (match-beginning 0)) parts)
521 start (match-end 0)))
522 (nreverse (cons (substring string start) parts))))
525 ;;; @ Emacs 20.3 emulation
528 ;; imported from Emacs 20.3.91.
529 (defvar-maybe temporary-file-directory
530 (file-name-as-directory
531 (cond ((memq system-type '(ms-dos windows-nt))
532 (or (getenv "TEMP") (getenv "TMPDIR") (getenv "TMP") "c:/temp"))
533 ((memq system-type '(vax-vms axp-vms))
534 (or (getenv "TMPDIR") (getenv "TMP") (getenv "TEMP") "SYS$SCRATCH:"))
536 (or (getenv "TMPDIR") (getenv "TMP") (getenv "TEMP") "/tmp"))))
537 "The directory for writing temporary files.")
539 (defun-maybe line-beginning-position (&optional n)
540 "Return the character position of the first character on the current line.
541 With argument N not nil or 1, move forward N - 1 lines first.
542 If scan reaches end of buffer, return that position.
543 This function does not move point."
546 (forward-line (1- n))
551 (defun-maybe line-end-position (&optional n)
552 "Return the character position of the last character on the current line.
553 With argument N not nil or 1, move forward N - 1 lines first.
554 If scan reaches end of buffer, return that position.
555 This function does not move point."
558 (forward-line (1- n))
563 (defun-maybe string (&rest chars)
564 "Concatenate all the argument characters and make the result a string."
565 (mapconcat (function char-to-string) chars "")
569 ;;; @ XEmacs emulation
572 (defun-maybe find-face (face-or-name)
573 "Retrieve the face of the given name.
574 If FACE-OR-NAME is a face object, it is simply returned.
575 Otherwise, FACE-OR-NAME should be a symbol. If there is no such face,
576 nil is returned. Otherwise the associated face object is returned.
577 \[XEmacs emulating function]"
578 (car (memq face-or-name (face-list)))
581 (defun-maybe point-at-bol (&optional n buffer)
582 "Return the character position of the first character on the current line.
583 With argument N not nil or 1, move forward N - 1 lines first.
584 If scan reaches end of buffer, return that position.
585 This function does not move point. [XEmacs emulating function]"
590 (line-beginning-position n)
593 (defun-maybe point-at-eol (&optional n buffer)
594 "Return the character position of the last character on the current line.
595 With argument N not nil or 1, move forward N - 1 lines first.
596 If scan reaches end of buffer, return that position.
597 This function does not move point. [XEmacs emulating function]"
602 (line-end-position n)
605 (defun-maybe functionp (obj)
606 "Returns t if OBJ is a function, nil otherwise.
607 \[XEmacs emulating function]"
609 (byte-code-function-p obj)
610 (and (symbolp obj)(fboundp obj))
611 (and (consp obj)(eq (car obj) 'lambda))
614 (defsubst-maybe define-obsolete-function-alias (oldfun newfun)
615 "Define OLDFUN as an obsolete alias for function NEWFUN.
616 This makes calling OLDFUN equivalent to calling NEWFUN and marks OLDFUN
617 as obsolete. [XEmacs emulating function]"
618 (defalias oldfun newfun)
619 (make-obsolete oldfun newfun)
622 (when (subr-fboundp 'read-event)
623 ;; for Emacs 19 or later
625 (defun-maybe-cond next-command-event (&optional event prompt)
626 "Read an event object from the input stream.
627 If EVENT is non-nil, it should be an event object and will be filled
628 in and returned; otherwise a new event object will be created and
630 If PROMPT is non-nil, it should be a string and will be displayed in
631 the echo area while this function is waiting for an event.
632 \[XEmacs emulating function]"
633 ((subr-fboundp 'string)
634 ;; for Emacs 20.3 or later
635 (read-event prompt t)
638 (if prompt (message prompt))
642 (defsubst-maybe character-to-event (ch)
643 "Convert keystroke CH into an event structure, replete with bucky bits.
644 Note that CH (the keystroke specifier) can be an integer, a character
645 or a symbol such as 'clear. [XEmacs emulating function]"
648 (defun-maybe event-to-character (event)
649 "Return the character approximation to the given event object.
650 If the event isn't a keypress, this returns nil.
651 \[XEmacs emulating function]"
652 (cond ((symbolp event)
653 ;; mask is (BASE-TYPE MODIFIER-BITS) or nil.
654 (let ((mask (get event 'event-symbol-element-mask)))
656 (let ((base (get (car mask) 'ascii-character)))
658 (logior base (car (cdr mask)))
660 ((integerp event) event)
665 ;;; @ MULE 2 emulation
668 (defun-maybe-cond cancel-undo-boundary ()
669 "Cancel undo boundary. [MULE 2.3 emulating function]"
670 ((boundp 'buffer-undo-list)
671 ;; for Emacs 19.7 or later
672 (if (and (consp buffer-undo-list)
674 (null (car buffer-undo-list)))
675 (setq buffer-undo-list (cdr buffer-undo-list))
678 ;; for anything older than Emacs 19.7.