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 ;; `add-hook' and `remove-hook' are imported from Emacs 19.28
175 ;; (with additional `local' argument).
178 (add-hook 'test-hook 'test 'append 'local)
179 (remove-hook 'test-hook 'test 'local))
181 ;; emulate add-hook/remove-hook for version 18.
182 (defun add-hook (hook function &optional append local)
183 "Add to the value of HOOK the function FUNCTION.
184 FUNCTION is not added if already present.
185 FUNCTION is added \(if necessary\) at the beginning of the hook list
186 unless the optional argument APPEND is non-nil, in which case
187 FUNCTION is added at the end.
189 The optional fourth argument, LOCAL, if non-nil, says to modify
190 the hook's buffer-local value rather than its default value
191 \(LOCAL is only for emulation\).
193 HOOK should be a symbol, and FUNCTION may be any valid function. If
194 HOOK is void, it is first set to nil. If HOOK's value is a single
195 function, it is changed to a list of functions.
196 \[Emacs 19.29 emulating function]"
199 ;; If the hook value is a single function, turn it into a list.
200 (let ((old (symbol-value hook)))
201 (if (or (not (listp old))
202 (eq (car old) 'lambda))
203 (set hook (list old))))
204 (or (if (consp function)
205 ;; Clever way to tell whether a given lambda-expression
206 ;; is equal to anything in the hook.
207 (let ((tail (assoc (cdr function) (symbol-value hook))))
208 (equal function tail))
209 (memq function (symbol-value hook)))
212 (nconc (symbol-value hook) (list function))
213 (cons function (symbol-value hook))))))
215 (defun remove-hook (hook function &optional local)
216 "Remove from the value of HOOK the function FUNCTION.
217 HOOK should be a symbol, and FUNCTION may be any valid function. If
218 FUNCTION isn't the value of HOOK, or, if FUNCTION doesn't appear in the
219 list of hooks to run in HOOK, then nothing is done. See `add-hook'.
221 The optional third argument, LOCAL, if non-nil, says to modify
222 the hook's buffer-local value rather than its default value
223 \(LOCAL is only for emulation\).
224 \[Emacs 19.29 emulating function]"
225 (if (or (not (boundp hook))
226 (null (symbol-value hook))
229 (let ((hook-value (symbol-value hook)))
230 (if (consp hook-value)
231 (setq hook-value (delete function hook-value))
232 (if (equal hook-value function)
233 (setq hook-value nil)))
234 (set hook hook-value))))
236 (defun-maybe make-local-hook (hook)
237 "Make the hook HOOK local to the current buffer.
238 This function is only for emulation.
239 \[Emacs 19.29 emulating function]"
242 (wrong-number-of-arguments
243 ;; emulate `local' arg for version 19.28 and earlier.
244 (or (fboundp 'si:add-hook)
246 (fset 'si:add-hook (symbol-function 'add-hook))
247 (defun add-hook (hook function &optional append local)
248 "Add to the value of HOOK the function FUNCTION.
249 FUNCTION is not added if already present.
250 FUNCTION is added \(if necessary\) at the beginning of the hook list
251 unless the optional argument APPEND is non-nil, in which case
252 FUNCTION is added at the end.
254 The optional fourth argument, LOCAL, if non-nil, says to modify
255 the hook's buffer-local value rather than its default value
256 \(LOCAL is only for emulation\).
258 HOOK should be a symbol, and FUNCTION may be any valid function. If
259 HOOK is void, it is first set to nil. If HOOK's value is a single
260 function, it is changed to a list of functions.
261 \[Emacs 19.29 emulating function]"
262 ;; the fourth argument LOCAL is simply ignored.
263 (si:add-hook hook function append))))
265 (or (fboundp 'si:remove-hook)
267 (fset 'si:remove-hook (symbol-function 'remove-hook))
268 (defun remove-hook (hook function &optional local)
269 "Remove from the value of HOOK the function FUNCTION.
270 HOOK should be a symbol, and FUNCTION may be any valid function. If
271 FUNCTION isn't the value of HOOK, or, if FUNCTION doesn't appear in the
272 list of hooks to run in HOOK, then nothing is done. See `add-hook'.
274 The optional third argument, LOCAL, if non-nil, says to modify
275 the hook's buffer-local value rather than its default value
276 \(LOCAL is only for emulation\).
277 \[Emacs 19.29 emulating function]"
278 ;; the third argument LOCAL is simply ignored.
279 (si:remove-hook hook function))))
281 (defun-maybe make-local-hook (hook)
282 "Make the hook HOOK local to the current buffer.
283 This function is only for emulation.
284 \[Emacs 19.29 emulating function]"
288 (defvar-maybe path-separator ":"
289 "Character used to separate concatenated paths.")
291 (defun-maybe buffer-substring-no-properties (start end)
292 "Return the characters of part of the buffer, without the text properties.
293 The two arguments START and END are character positions;
294 they can be in either order. [Emacs 19.29 emulating function]"
295 (let ((string (buffer-substring start end)))
296 (set-text-properties 0 (length string) nil string)
299 (defun-maybe match-string (num &optional string)
300 "Return string of text matched by last search.
301 NUM specifies which parenthesized expression in the last regexp.
302 Value is nil if NUMth pair didn't match, or there were less than NUM pairs.
303 Zero means the entire text matched by the whole regexp or whole string.
304 STRING should be given if the last search was by `string-match' on STRING.
305 \[Emacs 19.29 emulating function]"
306 (if (match-beginning num)
308 (substring string (match-beginning num) (match-end num))
309 (buffer-substring (match-beginning num) (match-end num)))))
311 (or (featurep 'xemacs)
312 (>= emacs-major-version 20)
313 (and (= emacs-major-version 19)
314 (>= emacs-minor-version 29))
315 ;; for Emacs 19.28 or earlier
316 (fboundp 'si:read-string)
318 (fset 'si:read-string (symbol-function 'read-string))
319 (defun read-string (prompt &optional initial-input history)
320 "Read a string from the minibuffer, prompting with string PROMPT.
321 If non-nil, second arg INITIAL-INPUT is a string to insert before reading.
322 The third arg HISTORY, is dummy for compatibility.
323 See `read-from-minibuffer' for details of HISTORY argument."
324 (si:read-string prompt initial-input))
327 (defun-maybe rassoc (key list)
328 "Return non-nil if KEY is `equal' to the cdr of an element of LIST.
329 The value is actually the element of LIST whose cdr equals KEY."
332 (if (equal (cdr (car list)) key)
333 (throw 'found (car list))
335 (setq list (cdr list)))
338 (defun-maybe file-name-sans-extension (filename)
339 "Return FILENAME sans final \"extension\".
340 The extension, in a file name, is the part that follows the last `.'."
342 (let ((file (file-name-sans-versions (file-name-nondirectory filename)))
344 (if (string-match "\\.[^.]*\\'" file)
345 (if (setq directory (file-name-directory filename))
346 (expand-file-name (substring file 0 (match-beginning 0))
348 (substring file 0 (match-beginning 0)))
351 ;;; @ Emacs 19.30 emulation
354 ;; imported from Emacs 19.30.
355 (defun-maybe add-to-list (list-var element)
356 "Add to the value of LIST-VAR the element ELEMENT if it isn't there yet.
357 If you want to use `add-to-list' on a variable that is not defined
358 until a certain package is loaded, you should put the call to `add-to-list'
359 into a hook function that will be run only after loading the package.
360 \[Emacs 19.30 emulating function]"
361 (or (member element (symbol-value list-var))
362 (set list-var (cons element (symbol-value list-var)))))
364 (cond ((fboundp 'insert-file-contents-literally))
365 ((boundp 'file-name-handler-alist)
366 (defun insert-file-contents-literally
367 (filename &optional visit beg end replace)
368 "Like `insert-file-contents', q.v., but only reads in the file.
369 A buffer may be modified in several ways after reading into the buffer due
370 to advanced Emacs features, such as file-name-handlers, format decoding,
371 find-file-hooks, etc.
372 This function ensures that none of these modifications will take place.
373 \[Emacs 19.30 emulating function]"
374 (let (file-name-handler-alist)
375 (insert-file-contents filename visit beg end replace)))
378 (defalias 'insert-file-contents-literally 'insert-file-contents)
382 ;;; @ Emacs 19.31 emulation
385 (defun-maybe buffer-live-p (object)
386 "Return non-nil if OBJECT is a buffer which has not been killed.
387 Value is nil if OBJECT is not a buffer or if it has been killed.
388 \[Emacs 19.31 emulating function]"
391 (buffer-name (get-buffer object))))
393 ;; imported from Emacs 19.33.
394 (defmacro-maybe save-selected-window (&rest body)
395 "Execute BODY, then select the window that was selected before BODY.
396 \[Emacs 19.31 emulating function]"
398 '((save-selected-window-window (selected-window)))
399 (list 'unwind-protect
401 (list 'select-window 'save-selected-window-window))))
404 ;;; @ Emacs 20.1 emulation
407 ;; imported from Emacs 20.2.
408 (defmacro-maybe when (cond &rest body)
409 "(when COND BODY...): if COND yields non-nil, do BODY, else return nil."
410 (list 'if cond (cons 'progn body)))
412 ;; imported from Emacs 20.3.
413 (defmacro-maybe unless (cond &rest body)
414 "(unless COND BODY...): if COND yields nil, do BODY, else return nil."
415 (cons 'if (cons cond (cons nil body))))
417 ;; imported from Emacs 20.3.
418 (defun-maybe last (x &optional n)
419 "Return the last link of the list X. Its car is the last element.
420 If X is nil, return nil.
421 If N is non-nil, return the Nth-to-last link of X.
422 If N is bigger than the length of X, return X."
426 (setq m (1+ m) p (cdr p)))
428 (if (< n m) (nthcdr (- m n) x) x)))
433 (defmacro-maybe save-current-buffer (&rest body)
434 "Save the current buffer; execute BODY; restore the current buffer.
435 Executes BODY just like `progn'."
436 (` (let ((orig-buffer (current-buffer)))
439 (set-buffer orig-buffer)))))
441 ;; imported from Emacs 20.2.
442 (defmacro-maybe with-current-buffer (buffer &rest body)
443 "Execute the forms in BODY with BUFFER as the current buffer.
444 The value returned is the value of the last form in BODY.
445 See also `with-temp-buffer'."
446 (` (save-current-buffer
447 (set-buffer (, buffer))
450 ;; imported from Emacs 20.2.
451 (defmacro-maybe with-temp-file (file &rest forms)
452 "Create a new buffer, evaluate FORMS there, and write the buffer to FILE.
453 The value of the last form in FORMS is returned, like `progn'.
454 See also `with-temp-buffer'."
455 (let ((temp-file (make-symbol "temp-file"))
456 (temp-buffer (make-symbol "temp-buffer")))
457 (` (let (((, temp-file) (, file))
459 (get-buffer-create (generate-new-buffer-name " *temp file*"))))
462 (with-current-buffer (, temp-buffer)
464 (with-current-buffer (, temp-buffer)
466 (write-region (point-min) (point-max) (, temp-file) nil 0)))
467 (and (buffer-name (, temp-buffer))
468 (kill-buffer (, temp-buffer))))))))
470 ;; imported from Emacs 20.2.
471 (defmacro-maybe with-temp-buffer (&rest forms)
472 "Create a temporary buffer, and evaluate FORMS there like `progn'.
473 See also `with-temp-file' and `with-output-to-string'."
474 (let ((temp-buffer (make-symbol "temp-buffer")))
475 (` (let (((, temp-buffer)
476 (get-buffer-create (generate-new-buffer-name " *temp*"))))
478 (with-current-buffer (, temp-buffer)
480 (and (buffer-name (, temp-buffer))
481 (kill-buffer (, temp-buffer))))))))
483 (defmacro-maybe combine-after-change-calls (&rest body)
487 ;; imported from Emacs 20.3. (cl function)
488 (defun-maybe butlast (x &optional n)
489 "Returns a copy of LIST with the last N elements removed."
490 (if (and n (<= n 0)) x
491 (nbutlast (copy-sequence x) n)))
493 ;; imported from Emacs 20.3. (cl function)
494 (defun-maybe nbutlast (x &optional n)
495 "Modifies LIST to remove the last N elements."
496 (let ((m (length x)))
500 (if (> n 0) (setcdr (nthcdr (- (1- m) n) x) nil))
503 ;; imported from XEmacs 21.
504 (defun-maybe split-string (string &optional pattern)
505 "Return a list of substrings of STRING which are separated by PATTERN.
506 If PATTERN is omitted, it defaults to \"[ \\f\\t\\n\\r\\v]+\"."
508 (setq pattern "[ \f\t\n\r\v]+"))
509 ;; The FSF version of this function takes care not to cons in case
510 ;; of infloop. Maybe we should synch?
511 (let (parts (start 0))
512 (while (string-match pattern string start)
513 (setq parts (cons (substring string start (match-beginning 0)) parts)
514 start (match-end 0)))
515 (nreverse (cons (substring string start) parts))))
518 ;;; @ Emacs 20.3 emulation
521 ;; imported from Emacs 20.3.91.
522 (defvar-maybe temporary-file-directory
523 (file-name-as-directory
524 (cond ((memq system-type '(ms-dos windows-nt))
525 (or (getenv "TEMP") (getenv "TMPDIR") (getenv "TMP") "c:/temp"))
526 ((memq system-type '(vax-vms axp-vms))
527 (or (getenv "TMPDIR") (getenv "TMP") (getenv "TEMP") "SYS$SCRATCH:"))
529 (or (getenv "TMPDIR") (getenv "TMP") (getenv "TEMP") "/tmp"))))
530 "The directory for writing temporary files.")
532 (defun-maybe line-beginning-position (&optional n)
533 "Return the character position of the first character on the current line.
534 With argument N not nil or 1, move forward N - 1 lines first.
535 If scan reaches end of buffer, return that position.
536 This function does not move point."
539 (forward-line (1- n))
544 (defun-maybe line-end-position (&optional n)
545 "Return the character position of the last character on the current line.
546 With argument N not nil or 1, move forward N - 1 lines first.
547 If scan reaches end of buffer, return that position.
548 This function does not move point."
551 (forward-line (1- n))
556 (defun-maybe string (&rest chars)
557 "Concatenate all the argument characters and make the result a string."
558 (mapconcat (function char-to-string) chars "")
562 ;;; @ XEmacs emulation
565 (defun-maybe find-face (face-or-name)
566 "Retrieve the face of the given name.
567 If FACE-OR-NAME is a face object, it is simply returned.
568 Otherwise, FACE-OR-NAME should be a symbol. If there is no such face,
569 nil is returned. Otherwise the associated face object is returned.
570 \[XEmacs emulating function]"
571 (car (memq face-or-name (face-list)))
574 (defun-maybe point-at-bol (&optional n buffer)
575 "Return the character position of the first character on the current line.
576 With argument N not nil or 1, move forward N - 1 lines first.
577 If scan reaches end of buffer, return that position.
578 This function does not move point. [XEmacs emulating function]"
583 (line-beginning-position n)
586 (defun-maybe point-at-eol (&optional n buffer)
587 "Return the character position of the last character on the current line.
588 With argument N not nil or 1, move forward N - 1 lines first.
589 If scan reaches end of buffer, return that position.
590 This function does not move point. [XEmacs emulating function]"
595 (line-end-position n)
598 (defun-maybe functionp (obj)
599 "Returns t if OBJ is a function, nil otherwise.
600 \[XEmacs emulating function]"
602 (byte-code-function-p obj)
603 (and (symbolp obj)(fboundp obj))
604 (and (consp obj)(eq (car obj) 'lambda))
607 (defsubst-maybe define-obsolete-function-alias (oldfun newfun)
608 "Define OLDFUN as an obsolete alias for function NEWFUN.
609 This makes calling OLDFUN equivalent to calling NEWFUN and marks OLDFUN
610 as obsolete. [XEmacs emulating function]"
611 (defalias oldfun newfun)
612 (make-obsolete oldfun newfun)
615 (when (subr-fboundp 'read-event)
616 ;; for Emacs 19 or later
618 (defun-maybe-cond next-command-event (&optional event prompt)
619 "Read an event object from the input stream.
620 If EVENT is non-nil, it should be an event object and will be filled
621 in and returned; otherwise a new event object will be created and
623 If PROMPT is non-nil, it should be a string and will be displayed in
624 the echo area while this function is waiting for an event.
625 \[XEmacs emulating function]"
626 ((subr-fboundp 'string)
627 ;; for Emacs 20.3 or later
628 (read-event prompt t)
631 (if prompt (message prompt))
635 (defsubst-maybe character-to-event (ch)
636 "Convert keystroke CH into an event structure, replete with bucky bits.
637 Note that CH (the keystroke specifier) can be an integer, a character
638 or a symbol such as 'clear. [XEmacs emulating function]"
641 (defun-maybe event-to-character (event)
642 "Return the character approximation to the given event object.
643 If the event isn't a keypress, this returns nil.
644 \[XEmacs emulating function]"
645 (cond ((symbolp event)
646 ;; mask is (BASE-TYPE MODIFIER-BITS) or nil.
647 (let ((mask (get event 'event-symbol-element-mask)))
649 (let ((base (get (car mask) 'ascii-character)))
651 (logior base (car (cdr mask)))
653 ((integerp event) event)
658 ;;; @ MULE 2 emulation
661 (defun-maybe-cond cancel-undo-boundary ()
662 "Cancel undo boundary. [MULE 2.3 emulating function]"
663 ((boundp 'buffer-undo-list)
664 ;; for Emacs 19.7 or later
665 (if (and (consp buffer-undo-list)
667 (null (car buffer-undo-list)))
668 (setq buffer-undo-list (cdr buffer-undo-list))
671 ;; for anything older than Emacs 19.7.