1 ;;; poe.el --- Portable Outfit for Emacsen; -*-byte-compile-dynamic: t;-*-
3 ;; Copyright (C) 1995,1996,1997,1998,1999 Free Software Foundation, Inc.
5 ;; Author: MORIOKA Tomohiko <tomo@m17n.org>
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.
28 ;; MULE related features are supported by `poem'.
30 ;; Note to developers:
32 ;; In this file, `eval-when-compile' and `eval-and-compile' does not work
33 ;; for v18. If you really need them, require 'poe-18 at outermost level.
39 (put 'defun-maybe 'lisp-indent-function 'defun)
40 (put 'defun-maybe 'edebug-form-spec 'defun)
41 (defmacro defun-maybe (name &rest everything-else)
42 "Define NAME as a function if NAME is not defined.
43 See also the function `defun'."
44 (or (and (fboundp name)
45 (not (get name 'defun-maybe)))
46 (` (or (fboundp (quote (, name)))
48 (defun (, name) (,@ everything-else))
49 (put (quote (, name)) 'defun-maybe t))))))
51 (put 'defmacro-maybe 'lisp-indent-function 'defun)
52 (put 'defmacro-maybe 'edebug-form-spec '(&define name lambda-list def-body))
53 (defmacro defmacro-maybe (name &rest everything-else)
54 "Define NAME as a macro if NAME is not defined.
55 See also the function `defmacro'."
56 (or (and (fboundp name)
57 (not (get name 'defmacro-maybe)))
58 (` (or (fboundp (quote (, name)))
60 (defmacro (, name) (,@ everything-else))
61 (put (quote (, name)) 'defmacro-maybe t))))))
63 (put 'defsubst-maybe 'lisp-indent-function 'defun)
64 (put 'defsubst-maybe 'edebug-form-spec 'defun)
65 (defmacro defsubst-maybe (name &rest everything-else)
66 "Define NAME as an inline function if NAME is not defined.
67 See also the macro `defsubst'."
68 (or (and (fboundp name)
69 (not (get name 'defsubst-maybe)))
70 (` (or (fboundp (quote (, name)))
72 (defsubst (, name) (,@ everything-else))
73 (put (quote (, name)) 'defsubst-maybe t))))))
75 (defmacro defalias-maybe (symbol definition)
76 "Define SYMBOL as an alias for DEFINITION if SYMBOL is not defined.
77 See also the function `defalias'."
78 (setq symbol (eval symbol))
79 (or (and (fboundp symbol)
80 (not (get symbol 'defalias-maybe)))
81 (` (or (fboundp (quote (, symbol)))
83 (defalias (quote (, symbol)) (, definition))
84 (put (quote (, symbol)) 'defalias-maybe t))))))
86 (defmacro defvar-maybe (name &rest everything-else)
87 "Define NAME as a variable if NAME is not defined.
88 See also the function `defvar'."
89 (or (and (boundp name)
90 (not (get name 'defvar-maybe)))
91 (` (or (boundp (quote (, name)))
93 (defvar (, name) (,@ everything-else))
94 (put (quote (, name)) 'defvar-maybe t))))))
96 (defmacro defconst-maybe (name &rest everything-else)
97 "Define NAME as a constant variable if NAME is not defined.
98 See also the function `defconst'."
99 (or (and (boundp name)
100 (not (get name 'defconst-maybe)))
101 (` (or (boundp (quote (, name)))
103 (defconst (, name) (,@ everything-else))
104 (put (quote (, name)) 'defconst-maybe t))))))
106 (defmacro defun-maybe-cond (name args &optional doc &rest everything-else)
108 (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)
122 (,@ (cdr case))))))))
124 (put (quote (, name)) 'defun-maybe t))))))
126 (defmacro defmacro-maybe-cond (name args &optional doc &rest everything-else)
128 (setq everything-else (cons doc everything-else)
130 (or (and (fboundp name)
131 (not (get name 'defmacro-maybe)))
132 (` (or (fboundp (quote (, name)))
134 (cond (,@ (mapcar (function
138 (` (defmacro (, name) (, args)
141 (` (defmacro (, name) (, args)
142 (,@ (cdr case))))))))
144 (put (quote (, name)) 'defmacro-maybe t))))))
146 (defun subr-fboundp (symbol)
147 "Return t if SYMBOL's function definition is a built-in function."
148 (and (fboundp symbol)
149 (subrp (symbol-function symbol))))
151 (defconst-maybe emacs-major-version (string-to-int emacs-version))
152 (defconst-maybe emacs-minor-version
154 (substring emacs-version
155 (string-match (format "%d\\." emacs-major-version)
158 (cond ((featurep 'xemacs)
159 (require 'poe-xemacs)
161 ((string-match "XEmacs" emacs-version)
163 (require 'poe-xemacs)
165 ((> emacs-major-version 20))
166 ((= emacs-major-version 20)
167 (cond ((subr-fboundp 'string)
168 ;; Emacs 20.3 or later
170 ((subr-fboundp 'concat-chars)
171 ;; Emacs 20.1 or later
172 (defalias 'string 'concat-chars)
175 ((= emacs-major-version 19)
176 ;; XXX: should do compile-time and load-time check before loading
177 ;; "localhook". But, it is difficult since "localhook" is
178 ;; already loaded via "install" at compile-time. any idea?
179 (if (< emacs-minor-version 29)
180 (require 'localhook)))
183 ;; XXX: should do compile-time and load-time check before loading
184 ;; "localhook". But, it is difficult since "localhook" is
185 ;; already loaded via "install" at compile-time. any idea?
189 ;;; @ Emacs 19.23 emulation
192 (defun-maybe minibuffer-prompt-width ()
193 "Return the display width of the minibuffer prompt."
195 (set-buffer (window-buffer (minibuffer-window)))
198 ;;; @ Emacs 19.29 emulation
201 (defvar-maybe path-separator ":"
202 "The directory separator in search paths, as a string.")
204 (defun-maybe buffer-substring-no-properties (start end)
205 "Return the characters of part of the buffer, without the text properties.
206 The two arguments START and END are character positions;
207 they can be in either order.
208 \[Emacs 19.29 emulating function]"
209 (let ((string (buffer-substring start end)))
210 (set-text-properties 0 (length string) nil string)
213 ;; imported from emacs-19.34/lisp/subr.el.
214 (defun-maybe match-string (num &optional string)
215 "Return string of text matched by last search.
216 NUM specifies which parenthesized expression in the last regexp.
217 Value is nil if NUMth pair didn't match, or there were less than NUM pairs.
218 Zero means the entire text matched by the whole regexp or whole string.
219 STRING should be given if the last search was by `string-match' on STRING.
220 \[Emacs 19.29 emulating function]"
221 (if (match-beginning num)
223 (substring string (match-beginning num) (match-end num))
224 (buffer-substring (match-beginning num) (match-end num)))))
226 (or (featurep 'xemacs)
227 (>= emacs-major-version 20)
228 (and (= emacs-major-version 19)
229 (>= emacs-minor-version 29))
230 ;; for Emacs 19.28 or earlier
231 (fboundp 'si:read-string)
233 (fset 'si:read-string (symbol-function 'read-string))
234 (defun read-string (prompt &optional initial-input history)
235 "Read a string from the minibuffer, prompting with string PROMPT.
236 If non-nil, second arg INITIAL-INPUT is a string to insert before reading.
237 The third arg HISTORY, is dummy for compatibility.
238 See `read-from-minibuffer' for details of HISTORY argument."
239 (si:read-string prompt initial-input))
242 (defun-maybe rassoc (key list)
243 "Return non-nil if KEY is `equal' to the cdr of an element of LIST.
244 The value is actually the element of LIST whose cdr equals KEY.
245 \[Emacs 19.29 emulating function]"
248 (if (equal (cdr (car list)) key)
249 (throw 'found (car list)))
250 (setq list (cdr list)))))
252 ;; imported from emacs-19.34/lisp/files.el.
253 (defun-maybe file-name-sans-extension (filename)
254 "Return FILENAME sans final \"extension\".
255 The extension, in a file name, is the part that follows the last `.'.
256 \[Emacs 19.29 emulating function]"
258 (let ((file (file-name-sans-versions (file-name-nondirectory filename)))
260 (if (string-match "\\.[^.]*\\'" file)
261 (if (setq directory (file-name-directory filename))
262 (expand-file-name (substring file 0 (match-beginning 0))
264 (substring file 0 (match-beginning 0)))
267 ;;; @ Emacs 19.30 emulation
270 ;; imported from emacs-19.34/lisp/subr.el.
271 (defun-maybe add-to-list (list-var element)
272 "Add to the value of LIST-VAR the element ELEMENT if it isn't there yet.
273 The test for presence of ELEMENT is done with `equal'.
274 If you want to use `add-to-list' on a variable that is not defined
275 until a certain package is loaded, you should put the call to `add-to-list'
276 into a hook function that will be run only after loading the package.
277 `eval-after-load' provides one way to do this. In some cases
278 other hooks, such as major mode hooks, can do the job.
279 \[Emacs 19.30 emulating function]"
280 (or (member element (symbol-value list-var))
281 (set list-var (cons element (symbol-value list-var)))))
283 (cond ((fboundp 'insert-file-contents-literally))
284 ((boundp 'file-name-handler-alist)
285 (defun insert-file-contents-literally
286 (filename &optional visit beg end replace)
287 "Like `insert-file-contents', q.v., but only reads in the file.
288 A buffer may be modified in several ways after reading into the buffer due
289 to advanced Emacs features, such as file-name-handlers, format decoding,
290 find-file-hooks, etc.
291 This function ensures that none of these modifications will take place.
292 \[Emacs 19.30 emulating function]"
293 (let (file-name-handler-alist)
294 (insert-file-contents filename visit beg end replace)))
297 (defalias 'insert-file-contents-literally 'insert-file-contents)
301 ;;; @ Emacs 19.31 emulation
304 (defun-maybe buffer-live-p (object)
305 "Return non-nil if OBJECT is a buffer which has not been killed.
306 Value is nil if OBJECT is not a buffer or if it has been killed.
307 \[Emacs 19.31 emulating function]"
310 (buffer-name (get-buffer object))
313 ;; imported from emacs-19.34/lisp/window.el.
314 (defmacro-maybe save-selected-window (&rest body)
315 "Execute BODY, then select the window that was selected before BODY.
316 \[Emacs 19.31 emulating function]"
318 '((save-selected-window-window (selected-window)))
319 (list 'unwind-protect
321 (list 'select-window 'save-selected-window-window))))
324 ;;; @ Emacs 20.1 emulation
327 ;; imported from emacs-20.3/lisp/subr.el.
328 (defmacro-maybe when (cond &rest body)
329 "If COND yields non-nil, do BODY, else return nil."
330 (list 'if cond (cons 'progn body)))
332 ;; imported from emacs-20.3/lisp/subr.el.
333 (defmacro-maybe unless (cond &rest body)
334 "If COND yields nil, do BODY, else return nil."
335 (cons 'if (cons cond (cons nil body))))
337 ;; imported from emacs-20.3/lisp/subr.el.
338 (defsubst-maybe caar (x)
339 "Return the car of the car of X."
342 ;; imported from emacs-20.3/lisp/subr.el.
343 (defsubst-maybe cadr (x)
344 "Return the car of the cdr of X."
347 ;; imported from emacs-20.3/lisp/subr.el.
348 (defsubst-maybe cdar (x)
349 "Return the cdr of the car of X."
352 ;; imported from emacs-20.3/lisp/subr.el.
353 (defsubst-maybe cddr (x)
354 "Return the cdr of the cdr of X."
357 ;; imported from emacs-20.3/lisp/subr.el.
358 (defun-maybe last (x &optional n)
359 "Return the last link of the list X. Its car is the last element.
360 If X is nil, return nil.
361 If N is non-nil, return the Nth-to-last link of X.
362 If N is bigger than the length of X, return X."
366 (setq m (1+ m) p (cdr p)))
368 (if (< n m) (nthcdr (- m n) x) x)))
373 ;; In Emacs 20.3, save-current-buffer is defined in src/editfns.c.
374 (defmacro-maybe save-current-buffer (&rest body)
375 "Save the current buffer; execute BODY; restore the current buffer.
376 Executes BODY just like `progn'."
377 (` (let ((orig-buffer (current-buffer)))
380 (if (buffer-live-p orig-buffer)
381 (set-buffer orig-buffer))))))
383 ;; imported from emacs-20.3/lisp/subr.el. (with macro style change)
384 (defmacro-maybe with-current-buffer (buffer &rest body)
385 "Execute the forms in BODY with BUFFER as the current buffer.
386 The value returned is the value of the last form in BODY.
387 See also `with-temp-buffer'."
388 (` (save-current-buffer
389 (set-buffer (, buffer))
392 ;; imported from emacs-20.3/lisp/subr.el. (with macro style change)
393 (defmacro-maybe with-temp-file (file &rest forms)
394 "Create a new buffer, evaluate FORMS there, and write the buffer to FILE.
395 The value of the last form in FORMS is returned, like `progn'.
396 See also `with-temp-buffer'."
397 (let ((temp-file (make-symbol "temp-file"))
398 (temp-buffer (make-symbol "temp-buffer")))
399 (` (let (((, temp-file) (, file))
401 (get-buffer-create (generate-new-buffer-name " *temp file*"))))
404 (with-current-buffer (, temp-buffer)
406 (with-current-buffer (, temp-buffer)
408 (write-region (point-min) (point-max) (, temp-file) nil 0)))
409 (and (buffer-name (, temp-buffer))
410 (kill-buffer (, temp-buffer))))))))
412 ;; imported from emacs-20.3/lisp/subr.el. (with macro style change)
413 (defmacro-maybe with-temp-buffer (&rest forms)
414 "Create a temporary buffer, and evaluate FORMS there like `progn'.
415 See also `with-temp-file' and `with-output-to-string'."
416 (let ((temp-buffer (make-symbol "temp-buffer")))
417 (` (let (((, temp-buffer)
418 (get-buffer-create (generate-new-buffer-name " *temp*"))))
420 (with-current-buffer (, temp-buffer)
422 (and (buffer-name (, temp-buffer))
423 (kill-buffer (, temp-buffer))))))))
425 (defmacro-maybe combine-after-change-calls (&rest body)
429 ;; imported from emacs-20.3/lisp/subr.el.
430 (defun functionp (object)
431 "Non-nil if OBJECT is a type of object that can be called as a function."
432 (or (subrp object) (byte-code-function-p object)
433 (eq (car-safe object) 'lambda)
434 (and (symbolp object) (fboundp object))))
436 ;; imported from emacs-20.3/lisp/emacs-lisp/cl.el.
437 (defun-maybe butlast (x &optional n)
438 "Returns a copy of LIST with the last N elements removed."
439 (if (and n (<= n 0)) x
440 (nbutlast (copy-sequence x) n)))
442 ;; imported from emacs-20.3/lisp/emacs-lisp/cl.el.
443 (defun-maybe nbutlast (x &optional n)
444 "Modifies LIST to remove the last N elements."
445 (let ((m (length x)))
449 (if (> n 0) (setcdr (nthcdr (- (1- m) n) x) nil))
452 ;; imported from XEmacs 21.
453 (defun-maybe split-string (string &optional pattern)
454 "Return a list of substrings of STRING which are separated by PATTERN.
455 If PATTERN is omitted, it defaults to \"[ \\f\\t\\n\\r\\v]+\"."
457 (setq pattern "[ \f\t\n\r\v]+"))
458 ;; The FSF version of this function takes care not to cons in case
459 ;; of infloop. Maybe we should synch?
460 (let (parts (start 0))
461 (while (string-match pattern string start)
462 (setq parts (cons (substring string start (match-beginning 0)) parts)
463 start (match-end 0)))
464 (nreverse (cons (substring string start) parts))))
467 ;;; @ Emacs 20.3 emulation
470 ;; imported from emacs-20.3/lisp/files.el.
471 (defvar-maybe temporary-file-directory
472 (file-name-as-directory
473 (cond ((memq system-type '(ms-dos windows-nt))
474 (or (getenv "TEMP") (getenv "TMPDIR") (getenv "TMP") "c:/temp"))
475 ((memq system-type '(vax-vms axp-vms))
476 (or (getenv "TMPDIR") (getenv "TMP") (getenv "TEMP") "SYS$SCRATCH:"))
478 (or (getenv "TMPDIR") (getenv "TMP") (getenv "TEMP") "/tmp"))))
479 "The directory for writing temporary files.")
481 (defun-maybe line-beginning-position (&optional n)
482 "Return the character position of the first character on the current line.
483 With argument N not nil or 1, move forward N - 1 lines first.
484 If scan reaches end of buffer, return that position.
485 This function does not move point."
487 (forward-line (1- (or n 1)))
490 (defun-maybe line-end-position (&optional n)
491 "Return the character position of the last character on the current line.
492 With argument N not nil or 1, move forward N - 1 lines first.
493 If scan reaches end of buffer, return that position.
494 This function does not move point."
496 (end-of-line (or n 1))
499 (defun-maybe string (&rest chars)
500 "Concatenate all the argument characters and make the result a string."
501 (mapconcat (function char-to-string) chars ""))
504 ;;; @ XEmacs emulation
507 (defun-maybe find-face (face-or-name)
508 "Retrieve the face of the given name.
509 If FACE-OR-NAME is a face object, it is simply returned.
510 Otherwise, FACE-OR-NAME should be a symbol. If there is no such face,
511 nil is returned. Otherwise the associated face object is returned.
512 \[XEmacs emulating function]"
513 (car (memq face-or-name (face-list))))
515 (defun-maybe point-at-bol (&optional n buffer)
516 "Return the character position of the first character on the current line.
517 With argument N not nil or 1, move forward N - 1 lines first.
518 If scan reaches end of buffer, return that position.
519 This function does not move point.
520 \[XEmacs emulating function]"
522 (if buffer (set-buffer buffer))
523 (forward-line (1- (or n 1)))
526 (defun-maybe point-at-eol (&optional n buffer)
527 "Return the character position of the last character on the current line.
528 With argument N not nil or 1, move forward N - 1 lines first.
529 If scan reaches end of buffer, return that position.
530 This function does not move point.
531 \[XEmacs emulating function]"
533 (if buffer (set-buffer buffer))
534 (end-of-line (or n 1))
537 (defsubst-maybe define-obsolete-function-alias (oldfun newfun)
538 "Define OLDFUN as an obsolete alias for function NEWFUN.
539 This makes calling OLDFUN equivalent to calling NEWFUN and marks OLDFUN
541 \[XEmacs emulating function]"
542 (defalias oldfun newfun)
543 (make-obsolete oldfun newfun))
545 (when (subr-fboundp 'read-event)
546 ;; for Emacs 19 or later
548 (defun-maybe-cond next-command-event (&optional event prompt)
549 "Read an event object from the input stream.
550 If EVENT is non-nil, it should be an event object and will be filled
551 in and returned; otherwise a new event object will be created and
553 If PROMPT is non-nil, it should be a string and will be displayed in
554 the echo area while this function is waiting for an event.
555 \[XEmacs emulating function]"
556 ((subr-fboundp 'string)
557 ;; for Emacs 20.3 or later
558 (read-event prompt t)
561 (if prompt (message prompt))
565 (defsubst-maybe character-to-event (ch)
566 "Convert keystroke CH into an event structure, replete with bucky bits.
567 Note that CH (the keystroke specifier) can be an integer, a character
568 or a symbol such as 'clear. [XEmacs emulating function]"
571 (defun-maybe event-to-character (event)
572 "Return the character approximation to the given event object.
573 If the event isn't a keypress, this returns nil.
574 \[XEmacs emulating function]"
575 (cond ((symbolp event)
576 ;; mask is (BASE-TYPE MODIFIER-BITS) or nil.
577 (let ((mask (get event 'event-symbol-element-mask)))
579 (let ((base (get (car mask) 'ascii-character)))
581 (logior base (car (cdr mask)))
583 ((integerp event) event)
588 ;;; @ MULE 2 emulation
591 (defun-maybe-cond cancel-undo-boundary ()
592 "Cancel undo boundary. [MULE 2.3 emulating function]"
593 ((boundp 'buffer-undo-list)
594 ;; for Emacs 19.7 or later
595 (if (and (consp buffer-undo-list)
597 (null (car buffer-undo-list)))
598 (setq buffer-undo-list (cdr buffer-undo-list))
601 ;; for anything older than Emacs 19.7.