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'.
32 (defmacro defun-maybe (name &rest everything-else)
33 (or (and (fboundp name)
34 (not (get name 'defun-maybe)))
35 (` (or (fboundp (quote (, name)))
37 (defun (, name) (,@ everything-else))
38 (put (quote (, name)) 'defun-maybe t)
42 (defmacro defmacro-maybe (name &rest everything-else)
43 (or (and (fboundp name)
44 (not (get name 'defmacro-maybe)))
45 (` (or (fboundp (quote (, name)))
47 (defmacro (, name) (,@ everything-else))
48 (put (quote (, name)) 'defmacro-maybe t)
52 (defmacro-maybe defsubst (name arglist &rest body)
53 "Define an inline function. The syntax is just like that of `defun'."
54 (cons 'defun (cons name (cons arglist body)))
57 (defmacro defsubst-maybe (name &rest everything-else)
58 (or (and (fboundp name)
59 (not (get name 'defsubst-maybe)))
60 (` (or (fboundp (quote (, name)))
62 (defsubst (, name) (,@ everything-else))
63 (put (quote (, name)) 'defsubst-maybe t)
67 (defmacro defalias-maybe (symbol definition)
68 (setq symbol (eval symbol))
69 (or (and (fboundp symbol)
70 (not (get symbol 'defalias-maybe)))
71 (` (or (fboundp (quote (, symbol)))
73 (defalias (quote (, symbol)) (, definition))
74 (put (quote (, symbol)) 'defalias-maybe t)
78 (put 'defun-maybe 'lisp-indent-function 'defun)
79 (put 'defsubst-maybe 'lisp-indent-function 'defun)
80 (put 'defmacro-maybe 'lisp-indent-function 'defun)
82 (defmacro defvar-maybe (name &rest everything-else)
83 (or (and (boundp name)
84 (not (get name 'defvar-maybe)))
85 (` (or (boundp (quote (, name)))
87 (defvar (, name) (,@ everything-else))
88 (put (quote (, name)) 'defvar-maybe t)
92 (defmacro defconst-maybe (name &rest everything-else)
93 (or (and (boundp name)
94 (not (get name 'defconst-maybe))
96 (` (or (boundp (quote (, name)))
98 (defconst (, name) (,@ everything-else))
99 (put (quote (, name)) 'defconst-maybe t)
103 (defmacro defun-maybe-cond (name args &optional doc &rest everything-else)
104 (unless (stringp doc)
105 (setq everything-else (cons doc everything-else)
108 (or (and (fboundp name)
109 (not (get name 'defun-maybe)))
110 (` (unless (fboundp (quote (, name)))
111 (cond (,@ (mapcar (function
115 (` (defun (, name) (, args)
118 (` (defun (, name) (, args)
122 (put (quote (, name)) 'defun-maybe t)
125 (defsubst subr-fboundp (symbol)
126 "Return t if SYMBOL's function definition is a built-in function."
127 (and (fboundp symbol)
128 (subrp (symbol-function symbol))))
130 (defconst-maybe emacs-major-version (string-to-int emacs-version))
131 (defconst-maybe emacs-minor-version
133 (substring emacs-version
134 (string-match (format "%d\\." emacs-major-version)
137 (cond ((featurep 'xemacs)
138 (require 'poe-xemacs)
140 ((string-match "XEmacs" emacs-version)
142 (require 'poe-xemacs)
144 ((> emacs-major-version 20))
145 ((= emacs-major-version 20)
146 (cond ((subr-fboundp 'string)
147 ;; Emacs 20.3 or later
149 ((subr-fboundp 'concat-chars)
150 ;; Emacs 20.1 or later
151 (defalias 'string 'concat-chars)
154 ((= emacs-major-version 19))
160 ;;; @ Emacs 19.23 emulation
163 (defun-maybe minibuffer-prompt-width ()
164 "Return the display width of the minibuffer prompt."
166 (set-buffer (window-buffer (minibuffer-window)))
170 ;;; @ Emacs 19.29 emulation
173 (defvar-maybe path-separator ":"
174 "Character used to separate concatenated paths.")
176 (defun-maybe buffer-substring-no-properties (start end)
177 "Return the characters of part of the buffer, without the text properties.
178 The two arguments START and END are character positions;
179 they can be in either order. [Emacs 19.29 emulating function]"
180 (let ((string (buffer-substring start end)))
181 (set-text-properties 0 (length string) nil string)
184 (defun-maybe match-string (num &optional string)
185 "Return string of text matched by last search.
186 NUM specifies which parenthesized expression in the last regexp.
187 Value is nil if NUMth pair didn't match, or there were less than NUM pairs.
188 Zero means the entire text matched by the whole regexp or whole string.
189 STRING should be given if the last search was by `string-match' on STRING.
190 \[Emacs 19.29 emulating function]"
191 (if (match-beginning num)
193 (substring string (match-beginning num) (match-end num))
194 (buffer-substring (match-beginning num) (match-end num)))))
196 (or (featurep 'xemacs)
197 (>= emacs-major-version 20)
198 (and (= emacs-major-version 19)
199 (>= emacs-minor-version 29))
200 ;; for Emacs 19.28 or earlier
201 (fboundp 'si:read-string)
203 (fset 'si:read-string (symbol-function 'read-string))
204 (defun read-string (prompt &optional initial-input history)
205 "Read a string from the minibuffer, prompting with string PROMPT.
206 If non-nil, second arg INITIAL-INPUT is a string to insert before reading.
207 The third arg HISTORY, is dummy for compatibility.
208 See `read-from-minibuffer' for details of HISTORY argument."
209 (si:read-string prompt initial-input))
212 (defun-maybe rassoc (key list)
213 "Return non-nil if KEY is `equal' to the cdr of an element of LIST.
214 The value is actually the element of LIST whose cdr equals KEY."
217 (if (equal (cdr (car list)) key)
218 (throw 'found (car list))
220 (setq list (cdr list)))
223 (defun-maybe file-name-sans-extension (filename)
224 "Return FILENAME sans final \"extension\".
225 The extension, in a file name, is the part that follows the last `.'."
227 (let ((file (file-name-sans-versions (file-name-nondirectory filename)))
229 (if (string-match "\\.[^.]*\\'" file)
230 (if (setq directory (file-name-directory filename))
231 (expand-file-name (substring file 0 (match-beginning 0))
233 (substring file 0 (match-beginning 0)))
236 (defmacro-maybe make-local-hook (hook))
238 ;; They are not Emacs features
240 (defmacro-maybe add-local-hook (hook function &optional append)
241 (if (fboundp 'make-local-hook)
242 (list 'add-hook hook function append t)
243 (list 'add-hook hook function append)
246 (defmacro-maybe remove-local-hook (hook function)
247 (if (fboundp 'make-local-hook)
248 (list 'remove-hook hook function t)
249 (list 'remove-hook hook function)
253 ;;; @ Emacs 19.30 emulation
256 ;; imported from Emacs 19.30.
257 (defun-maybe add-to-list (list-var element)
258 "Add to the value of LIST-VAR the element ELEMENT if it isn't there yet.
259 If you want to use `add-to-list' on a variable that is not defined
260 until a certain package is loaded, you should put the call to `add-to-list'
261 into a hook function that will be run only after loading the package.
262 \[Emacs 19.30 emulating function]"
263 (or (member element (symbol-value list-var))
264 (set list-var (cons element (symbol-value list-var)))))
266 (cond ((fboundp 'insert-file-contents-literally))
267 ((boundp 'file-name-handler-alist)
268 (defun insert-file-contents-literally
269 (filename &optional visit beg end replace)
270 "Like `insert-file-contents', q.v., but only reads in the file.
271 A buffer may be modified in several ways after reading into the buffer due
272 to advanced Emacs features, such as file-name-handlers, format decoding,
273 find-file-hooks, etc.
274 This function ensures that none of these modifications will take place.
275 \[Emacs 19.30 emulating function]"
276 (let (file-name-handler-alist)
277 (insert-file-contents filename visit beg end replace)))
280 (defalias 'insert-file-contents-literally 'insert-file-contents)
284 ;;; @ Emacs 19.31 emulation
287 (defun-maybe buffer-live-p (object)
288 "Return non-nil if OBJECT is a buffer which has not been killed.
289 Value is nil if OBJECT is not a buffer or if it has been killed.
290 \[Emacs 19.31 emulating function]"
293 (buffer-name (get-buffer object))))
295 ;; imported from Emacs 19.33.
296 (defmacro-maybe save-selected-window (&rest body)
297 "Execute BODY, then select the window that was selected before BODY.
298 \[Emacs 19.31 emulating function]"
300 '((save-selected-window-window (selected-window)))
301 (list 'unwind-protect
303 (list 'select-window 'save-selected-window-window))))
306 ;;; @ Emacs 20.1 emulation
309 ;; imported from Emacs 20.2.
310 (defmacro-maybe when (cond &rest body)
311 "(when COND BODY...): if COND yields non-nil, do BODY, else return nil."
312 (list 'if cond (cons 'progn body)))
314 ;; imported from Emacs 20.3.
315 (defmacro-maybe unless (cond &rest body)
316 "(unless COND BODY...): if COND yields nil, do BODY, else return nil."
317 (cons 'if (cons cond (cons nil body))))
319 ;; imported from Emacs 20.3.
320 (defun-maybe last (x &optional n)
321 "Return the last link of the list X. Its car is the last element.
322 If X is nil, return nil.
323 If N is non-nil, return the Nth-to-last link of X.
324 If N is bigger than the length of X, return X."
328 (setq m (1+ m) p (cdr p)))
330 (if (< n m) (nthcdr (- m n) x) x)))
335 (defmacro-maybe save-current-buffer (&rest body)
336 "Save the current buffer; execute BODY; restore the current buffer.
337 Executes BODY just like `progn'."
338 (` (let ((orig-buffer (current-buffer)))
341 (set-buffer orig-buffer)))))
343 ;; imported from Emacs 20.2.
344 (defmacro-maybe with-current-buffer (buffer &rest body)
345 "Execute the forms in BODY with BUFFER as the current buffer.
346 The value returned is the value of the last form in BODY.
347 See also `with-temp-buffer'."
348 (` (save-current-buffer
349 (set-buffer (, buffer))
352 ;; imported from Emacs 20.2.
353 (defmacro-maybe with-temp-file (file &rest forms)
354 "Create a new buffer, evaluate FORMS there, and write the buffer to FILE.
355 The value of the last form in FORMS is returned, like `progn'.
356 See also `with-temp-buffer'."
357 (let ((temp-file (make-symbol "temp-file"))
358 (temp-buffer (make-symbol "temp-buffer")))
359 (` (let (((, temp-file) (, file))
361 (get-buffer-create (generate-new-buffer-name " *temp file*"))))
364 (with-current-buffer (, temp-buffer)
366 (with-current-buffer (, temp-buffer)
368 (write-region (point-min) (point-max) (, temp-file) nil 0)))
369 (and (buffer-name (, temp-buffer))
370 (kill-buffer (, temp-buffer))))))))
372 ;; imported from Emacs 20.2.
373 (defmacro-maybe with-temp-buffer (&rest forms)
374 "Create a temporary buffer, and evaluate FORMS there like `progn'.
375 See also `with-temp-file' and `with-output-to-string'."
376 (let ((temp-buffer (make-symbol "temp-buffer")))
377 (` (let (((, temp-buffer)
378 (get-buffer-create (generate-new-buffer-name " *temp*"))))
380 (with-current-buffer (, temp-buffer)
382 (and (buffer-name (, temp-buffer))
383 (kill-buffer (, temp-buffer))))))))
385 (defmacro-maybe combine-after-change-calls (&rest body)
389 ;; imported from Emacs 20.3. (cl function)
390 (defun-maybe butlast (x &optional n)
391 "Returns a copy of LIST with the last N elements removed."
392 (if (and n (<= n 0)) x
393 (nbutlast (copy-sequence x) n)))
395 ;; imported from Emacs 20.3. (cl function)
396 (defun-maybe nbutlast (x &optional n)
397 "Modifies LIST to remove the last N elements."
398 (let ((m (length x)))
402 (if (> n 0) (setcdr (nthcdr (- (1- m) n) x) nil))
405 ;; imported from XEmacs 21.
406 (defun-maybe split-string (string &optional pattern)
407 "Return a list of substrings of STRING which are separated by PATTERN.
408 If PATTERN is omitted, it defaults to \"[ \\f\\t\\n\\r\\v]+\"."
410 (setq pattern "[ \f\t\n\r\v]+"))
411 ;; The FSF version of this function takes care not to cons in case
412 ;; of infloop. Maybe we should synch?
413 (let (parts (start 0))
414 (while (string-match pattern string start)
415 (setq parts (cons (substring string start (match-beginning 0)) parts)
416 start (match-end 0)))
417 (nreverse (cons (substring string start) parts))))
420 ;;; @ Emacs 20.3 emulation
423 ;; imported from Emacs 20.3.91.
424 (defvar-maybe temporary-file-directory
425 (file-name-as-directory
426 (cond ((memq system-type '(ms-dos windows-nt))
427 (or (getenv "TEMP") (getenv "TMPDIR") (getenv "TMP") "c:/temp"))
428 ((memq system-type '(vax-vms axp-vms))
429 (or (getenv "TMPDIR") (getenv "TMP") (getenv "TEMP") "SYS$SCRATCH:"))
431 (or (getenv "TMPDIR") (getenv "TMP") (getenv "TEMP") "/tmp"))))
432 "The directory for writing temporary files.")
434 (defun-maybe line-beginning-position (&optional n)
435 "Return the character position of the first character on the current line.
436 With argument N not nil or 1, move forward N - 1 lines first.
437 If scan reaches end of buffer, return that position.
438 This function does not move point."
441 (forward-line (1- n))
446 (defun-maybe line-end-position (&optional n)
447 "Return the character position of the last 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 string (&rest chars)
459 "Concatenate all the argument characters and make the result a string."
460 (mapconcat (function char-to-string) chars "")
464 ;;; @ XEmacs emulation
467 (defun-maybe find-face (face-or-name)
468 "Retrieve the face of the given name.
469 If FACE-OR-NAME is a face object, it is simply returned.
470 Otherwise, FACE-OR-NAME should be a symbol. If there is no such face,
471 nil is returned. Otherwise the associated face object is returned.
472 \[XEmacs emulating function]"
473 (car (memq face-or-name (face-list)))
476 (defun-maybe point-at-bol (&optional n buffer)
477 "Return the character position of the first character on the current line.
478 With argument N not nil or 1, move forward N - 1 lines first.
479 If scan reaches end of buffer, return that position.
480 This function does not move point. [XEmacs emulating function]"
485 (line-beginning-position n)
488 (defun-maybe point-at-eol (&optional n buffer)
489 "Return the character position of the last 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-end-position n)
500 (defun-maybe functionp (obj)
501 "Returns t if OBJ is a function, nil otherwise.
502 \[XEmacs emulating function]"
504 (byte-code-function-p obj)
505 (and (symbolp obj)(fboundp obj))
506 (and (consp obj)(eq (car obj) 'lambda))
509 (defsubst-maybe define-obsolete-function-alias (oldfun newfun)
510 "Define OLDFUN as an obsolete alias for function NEWFUN.
511 This makes calling OLDFUN equivalent to calling NEWFUN and marks OLDFUN
512 as obsolete. [XEmacs emulating function]"
513 (defalias oldfun newfun)
514 (make-obsolete oldfun newfun)
517 (when (subr-fboundp 'read-event)
518 ;; for Emacs 19 or later
520 (defun-maybe-cond next-command-event (&optional event prompt)
521 "Read an event object from the input stream.
522 If EVENT is non-nil, it should be an event object and will be filled
523 in and returned; otherwise a new event object will be created and
525 If PROMPT is non-nil, it should be a string and will be displayed in
526 the echo area while this function is waiting for an event.
527 \[XEmacs emulating function]"
528 ((subr-fboundp 'string)
529 ;; for Emacs 20.3 or later
530 (read-event prompt t)
533 (if prompt (message prompt))
537 (defsubst-maybe character-to-event (ch)
538 "Convert keystroke CH into an event structure, replete with bucky bits.
539 Note that CH (the keystroke specifier) can be an integer, a character
540 or a symbol such as 'clear. [XEmacs emulating function]"
543 (defun-maybe event-to-character (event)
544 "Return the character approximation to the given event object.
545 If the event isn't a keypress, this returns nil.
546 \[XEmacs emulating function]"
547 (cond ((symbolp event)
548 ;; mask is (BASE-TYPE MODIFIER-BITS) or nil.
549 (let ((mask (get event 'event-symbol-element-mask)))
551 (let ((base (get (car mask) 'ascii-character)))
553 (logior base (car (cdr mask)))
555 ((integerp event) event)
560 ;;; @ MULE 2 emulation
563 (defun-maybe-cond cancel-undo-boundary ()
564 "Cancel undo boundary. [MULE 2.3 emulating function]"
565 ((boundp 'buffer-undo-list)
566 ;; for Emacs 19.7 or later
567 (if (and (consp buffer-undo-list)
569 (null (car buffer-undo-list)))
570 (setq buffer-undo-list (cdr buffer-undo-list))
573 ;; for anything older than Emacs 19.7.