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 (wrong-number-of-arguments
239 ;; emulate `local' arg for version 19.28 and earlier.
240 (or (fboundp 'si:add-hook)
242 (fset 'si:add-hook (symbol-function 'add-hook))
243 (defun add-hook (hook function &optional append local)
244 "Add to the value of HOOK the function FUNCTION.
245 FUNCTION is not added if already present.
246 FUNCTION is added \(if necessary\) at the beginning of the hook list
247 unless the optional argument APPEND is non-nil, in which case
248 FUNCTION is added at the end.
250 The optional fourth argument, LOCAL, if non-nil, says to modify
251 the hook's buffer-local value rather than its default value
252 \(LOCAL is only for emulation\).
254 HOOK should be a symbol, and FUNCTION may be any valid function. If
255 HOOK is void, it is first set to nil. If HOOK's value is a single
256 function, it is changed to a list of functions.
257 \[Emacs 19.29 emulating function]"
258 ;; the fourth argument LOCAL is simply ignored.
259 (si:add-hook hook function append))))
261 (or (fboundp 'si:remove-hook)
263 (fset 'si:remove-hook (symbol-function 'remove-hook))
264 (defun remove-hook (hook function &optional local)
265 "Remove from the value of HOOK the function FUNCTION.
266 HOOK should be a symbol, and FUNCTION may be any valid function. If
267 FUNCTION isn't the value of HOOK, or, if FUNCTION doesn't appear in the
268 list of hooks to run in HOOK, then nothing is done. See `add-hook'.
270 The optional third argument, LOCAL, if non-nil, says to modify
271 the hook's buffer-local value rather than its default value
272 \(LOCAL is only for emulation\).
273 \[Emacs 19.29 emulating function]"
274 ;; the third argument LOCAL is simply ignored.
275 (si:remove-hook hook function))))
278 (defun-maybe make-local-hook (hook)
279 "Make the hook HOOK local to the current buffer.
280 This function is only for emulation.
281 \[Emacs 19.29 emulating function]"
284 (defvar-maybe path-separator ":"
285 "Character used to separate concatenated paths.")
287 (defun-maybe buffer-substring-no-properties (start end)
288 "Return the characters of part of the buffer, without the text properties.
289 The two arguments START and END are character positions;
290 they can be in either order. [Emacs 19.29 emulating function]"
291 (let ((string (buffer-substring start end)))
292 (set-text-properties 0 (length string) nil string)
295 (defun-maybe match-string (num &optional string)
296 "Return string of text matched by last search.
297 NUM specifies which parenthesized expression in the last regexp.
298 Value is nil if NUMth pair didn't match, or there were less than NUM pairs.
299 Zero means the entire text matched by the whole regexp or whole string.
300 STRING should be given if the last search was by `string-match' on STRING.
301 \[Emacs 19.29 emulating function]"
302 (if (match-beginning num)
304 (substring string (match-beginning num) (match-end num))
305 (buffer-substring (match-beginning num) (match-end num)))))
307 (or (featurep 'xemacs)
308 (>= emacs-major-version 20)
309 (and (= emacs-major-version 19)
310 (>= emacs-minor-version 29))
311 ;; for Emacs 19.28 or earlier
312 (fboundp 'si:read-string)
314 (fset 'si:read-string (symbol-function 'read-string))
315 (defun read-string (prompt &optional initial-input history)
316 "Read a string from the minibuffer, prompting with string PROMPT.
317 If non-nil, second arg INITIAL-INPUT is a string to insert before reading.
318 The third arg HISTORY, is dummy for compatibility.
319 See `read-from-minibuffer' for details of HISTORY argument."
320 (si:read-string prompt initial-input))
323 (defun-maybe rassoc (key list)
324 "Return non-nil if KEY is `equal' to the cdr of an element of LIST.
325 The value is actually the element of LIST whose cdr equals KEY."
328 (if (equal (cdr (car list)) key)
329 (throw 'found (car list))
331 (setq list (cdr list)))
334 (defun-maybe file-name-sans-extension (filename)
335 "Return FILENAME sans final \"extension\".
336 The extension, in a file name, is the part that follows the last `.'."
338 (let ((file (file-name-sans-versions (file-name-nondirectory filename)))
340 (if (string-match "\\.[^.]*\\'" file)
341 (if (setq directory (file-name-directory filename))
342 (expand-file-name (substring file 0 (match-beginning 0))
344 (substring file 0 (match-beginning 0)))
347 ;;; @ Emacs 19.30 emulation
350 ;; imported from Emacs 19.30.
351 (defun-maybe add-to-list (list-var element)
352 "Add to the value of LIST-VAR the element ELEMENT if it isn't there yet.
353 If you want to use `add-to-list' on a variable that is not defined
354 until a certain package is loaded, you should put the call to `add-to-list'
355 into a hook function that will be run only after loading the package.
356 \[Emacs 19.30 emulating function]"
357 (or (member element (symbol-value list-var))
358 (set list-var (cons element (symbol-value list-var)))))
360 (cond ((fboundp 'insert-file-contents-literally))
361 ((boundp 'file-name-handler-alist)
362 (defun insert-file-contents-literally
363 (filename &optional visit beg end replace)
364 "Like `insert-file-contents', q.v., but only reads in the file.
365 A buffer may be modified in several ways after reading into the buffer due
366 to advanced Emacs features, such as file-name-handlers, format decoding,
367 find-file-hooks, etc.
368 This function ensures that none of these modifications will take place.
369 \[Emacs 19.30 emulating function]"
370 (let (file-name-handler-alist)
371 (insert-file-contents filename visit beg end replace)))
374 (defalias 'insert-file-contents-literally 'insert-file-contents)
378 ;;; @ Emacs 19.31 emulation
381 (defun-maybe buffer-live-p (object)
382 "Return non-nil if OBJECT is a buffer which has not been killed.
383 Value is nil if OBJECT is not a buffer or if it has been killed.
384 \[Emacs 19.31 emulating function]"
387 (buffer-name (get-buffer object))))
389 ;; imported from Emacs 19.33.
390 (defmacro-maybe save-selected-window (&rest body)
391 "Execute BODY, then select the window that was selected before BODY.
392 \[Emacs 19.31 emulating function]"
394 '((save-selected-window-window (selected-window)))
395 (list 'unwind-protect
397 (list 'select-window 'save-selected-window-window))))
400 ;;; @ Emacs 20.1 emulation
403 ;; imported from Emacs 20.2.
404 (defmacro-maybe when (cond &rest body)
405 "(when COND BODY...): if COND yields non-nil, do BODY, else return nil."
406 (list 'if cond (cons 'progn body)))
408 ;; imported from Emacs 20.3.
409 (defmacro-maybe unless (cond &rest body)
410 "(unless COND BODY...): if COND yields nil, do BODY, else return nil."
411 (cons 'if (cons cond (cons nil body))))
413 ;; imported from Emacs 20.3.
414 (defsubst-maybe caar (x)
415 "Return the car of the car of X."
418 ;; imported from Emacs 20.3.
419 (defun-maybe last (x &optional n)
420 "Return the last link of the list X. Its car is the last element.
421 If X is nil, return nil.
422 If N is non-nil, return the Nth-to-last link of X.
423 If N is bigger than the length of X, return X."
427 (setq m (1+ m) p (cdr p)))
429 (if (< n m) (nthcdr (- m n) x) x)))
434 (defmacro-maybe save-current-buffer (&rest body)
435 "Save the current buffer; execute BODY; restore the current buffer.
436 Executes BODY just like `progn'."
437 (` (let ((orig-buffer (current-buffer)))
440 (set-buffer orig-buffer)))))
442 ;; imported from Emacs 20.2.
443 (defmacro-maybe with-current-buffer (buffer &rest body)
444 "Execute the forms in BODY with BUFFER as the current buffer.
445 The value returned is the value of the last form in BODY.
446 See also `with-temp-buffer'."
447 (` (save-current-buffer
448 (set-buffer (, buffer))
451 ;; imported from Emacs 20.2.
452 (defmacro-maybe with-temp-file (file &rest forms)
453 "Create a new buffer, evaluate FORMS there, and write the buffer to FILE.
454 The value of the last form in FORMS is returned, like `progn'.
455 See also `with-temp-buffer'."
456 (let ((temp-file (make-symbol "temp-file"))
457 (temp-buffer (make-symbol "temp-buffer")))
458 (` (let (((, temp-file) (, file))
460 (get-buffer-create (generate-new-buffer-name " *temp file*"))))
463 (with-current-buffer (, temp-buffer)
465 (with-current-buffer (, temp-buffer)
467 (write-region (point-min) (point-max) (, temp-file) nil 0)))
468 (and (buffer-name (, temp-buffer))
469 (kill-buffer (, temp-buffer))))))))
471 ;; imported from Emacs 20.2.
472 (defmacro-maybe with-temp-buffer (&rest forms)
473 "Create a temporary buffer, and evaluate FORMS there like `progn'.
474 See also `with-temp-file' and `with-output-to-string'."
475 (let ((temp-buffer (make-symbol "temp-buffer")))
476 (` (let (((, temp-buffer)
477 (get-buffer-create (generate-new-buffer-name " *temp*"))))
479 (with-current-buffer (, temp-buffer)
481 (and (buffer-name (, temp-buffer))
482 (kill-buffer (, temp-buffer))))))))
484 (defmacro-maybe combine-after-change-calls (&rest body)
488 ;; imported from Emacs 20.3. (cl function)
489 (defun-maybe butlast (x &optional n)
490 "Returns a copy of LIST with the last N elements removed."
491 (if (and n (<= n 0)) x
492 (nbutlast (copy-sequence x) n)))
494 ;; imported from Emacs 20.3. (cl function)
495 (defun-maybe nbutlast (x &optional n)
496 "Modifies LIST to remove the last N elements."
497 (let ((m (length x)))
501 (if (> n 0) (setcdr (nthcdr (- (1- m) n) x) nil))
504 ;; imported from XEmacs 21.
505 (defun-maybe split-string (string &optional pattern)
506 "Return a list of substrings of STRING which are separated by PATTERN.
507 If PATTERN is omitted, it defaults to \"[ \\f\\t\\n\\r\\v]+\"."
509 (setq pattern "[ \f\t\n\r\v]+"))
510 ;; The FSF version of this function takes care not to cons in case
511 ;; of infloop. Maybe we should synch?
512 (let (parts (start 0))
513 (while (string-match pattern string start)
514 (setq parts (cons (substring string start (match-beginning 0)) parts)
515 start (match-end 0)))
516 (nreverse (cons (substring string start) parts))))
519 ;;; @ Emacs 20.3 emulation
522 ;; imported from Emacs 20.3.91.
523 (defvar-maybe temporary-file-directory
524 (file-name-as-directory
525 (cond ((memq system-type '(ms-dos windows-nt))
526 (or (getenv "TEMP") (getenv "TMPDIR") (getenv "TMP") "c:/temp"))
527 ((memq system-type '(vax-vms axp-vms))
528 (or (getenv "TMPDIR") (getenv "TMP") (getenv "TEMP") "SYS$SCRATCH:"))
530 (or (getenv "TMPDIR") (getenv "TMP") (getenv "TEMP") "/tmp"))))
531 "The directory for writing temporary files.")
533 (defun-maybe line-beginning-position (&optional n)
534 "Return the character position of the first character on the current line.
535 With argument N not nil or 1, move forward N - 1 lines first.
536 If scan reaches end of buffer, return that position.
537 This function does not move point."
540 (forward-line (1- n))
545 (defun-maybe line-end-position (&optional n)
546 "Return the character position of the last character on the current line.
547 With argument N not nil or 1, move forward N - 1 lines first.
548 If scan reaches end of buffer, return that position.
549 This function does not move point."
552 (forward-line (1- n))
557 (defun-maybe string (&rest chars)
558 "Concatenate all the argument characters and make the result a string."
559 (mapconcat (function char-to-string) chars "")
563 ;;; @ XEmacs emulation
566 (defun-maybe find-face (face-or-name)
567 "Retrieve the face of the given name.
568 If FACE-OR-NAME is a face object, it is simply returned.
569 Otherwise, FACE-OR-NAME should be a symbol. If there is no such face,
570 nil is returned. Otherwise the associated face object is returned.
571 \[XEmacs emulating function]"
572 (car (memq face-or-name (face-list)))
575 (defun-maybe point-at-bol (&optional n buffer)
576 "Return the character position of the first character on the current line.
577 With argument N not nil or 1, move forward N - 1 lines first.
578 If scan reaches end of buffer, return that position.
579 This function does not move point. [XEmacs emulating function]"
584 (line-beginning-position n)
587 (defun-maybe point-at-eol (&optional n buffer)
588 "Return the character position of the last character on the current line.
589 With argument N not nil or 1, move forward N - 1 lines first.
590 If scan reaches end of buffer, return that position.
591 This function does not move point. [XEmacs emulating function]"
596 (line-end-position n)
599 (defun-maybe functionp (obj)
600 "Returns t if OBJ is a function, nil otherwise.
601 \[XEmacs emulating function]"
603 (byte-code-function-p obj)
604 (and (symbolp obj)(fboundp obj))
605 (and (consp obj)(eq (car obj) 'lambda))
608 (defsubst-maybe define-obsolete-function-alias (oldfun newfun)
609 "Define OLDFUN as an obsolete alias for function NEWFUN.
610 This makes calling OLDFUN equivalent to calling NEWFUN and marks OLDFUN
611 as obsolete. [XEmacs emulating function]"
612 (defalias oldfun newfun)
613 (make-obsolete oldfun newfun)
616 (when (subr-fboundp 'read-event)
617 ;; for Emacs 19 or later
619 (defun-maybe-cond next-command-event (&optional event prompt)
620 "Read an event object from the input stream.
621 If EVENT is non-nil, it should be an event object and will be filled
622 in and returned; otherwise a new event object will be created and
624 If PROMPT is non-nil, it should be a string and will be displayed in
625 the echo area while this function is waiting for an event.
626 \[XEmacs emulating function]"
627 ((subr-fboundp 'string)
628 ;; for Emacs 20.3 or later
629 (read-event prompt t)
632 (if prompt (message prompt))
636 (defsubst-maybe character-to-event (ch)
637 "Convert keystroke CH into an event structure, replete with bucky bits.
638 Note that CH (the keystroke specifier) can be an integer, a character
639 or a symbol such as 'clear. [XEmacs emulating function]"
642 (defun-maybe event-to-character (event)
643 "Return the character approximation to the given event object.
644 If the event isn't a keypress, this returns nil.
645 \[XEmacs emulating function]"
646 (cond ((symbolp event)
647 ;; mask is (BASE-TYPE MODIFIER-BITS) or nil.
648 (let ((mask (get event 'event-symbol-element-mask)))
650 (let ((base (get (car mask) 'ascii-character)))
652 (logior base (car (cdr mask)))
654 ((integerp event) event)
659 ;;; @ MULE 2 emulation
662 (defun-maybe-cond cancel-undo-boundary ()
663 "Cancel undo boundary. [MULE 2.3 emulating function]"
664 ((boundp 'buffer-undo-list)
665 ;; for Emacs 19.7 or later
666 (if (and (consp buffer-undo-list)
668 (null (car buffer-undo-list)))
669 (setq buffer-undo-list (cdr buffer-undo-list))
672 ;; for anything older than Emacs 19.7.