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 defsubst-maybe (name &rest everything-else)
43 (or (and (fboundp name)
44 (not (get name 'defsubst-maybe)))
45 (` (or (fboundp (quote (, name)))
47 (defsubst (, name) (,@ everything-else))
48 (put (quote (, name)) 'defsubst-maybe t)
52 (defmacro defmacro-maybe (name &rest everything-else)
53 (or (and (fboundp name)
54 (not (get name 'defmacro-maybe)))
55 (` (or (fboundp (quote (, name)))
57 (defmacro (, name) (,@ everything-else))
58 (put (quote (, name)) 'defmacro-maybe t)
62 (defmacro defalias-maybe (symbol definition)
63 (setq symbol (eval symbol))
64 (or (and (fboundp symbol)
65 (not (get symbol 'defalias-maybe)))
66 (` (or (fboundp (quote (, symbol)))
68 (defalias (quote (, symbol)) (, definition))
69 (put (quote (, symbol)) 'defalias-maybe t)
73 (put 'defun-maybe 'lisp-indent-function 'defun)
74 (put 'defsubst-maybe 'lisp-indent-function 'defun)
75 (put 'defmacro-maybe 'lisp-indent-function 'defun)
77 (defmacro defvar-maybe (name &rest everything-else)
78 (or (and (boundp name)
79 (not (get name 'defvar-maybe)))
80 (` (or (boundp (quote (, name)))
82 (defvar (, name) (,@ everything-else))
83 (put (quote (, name)) 'defvar-maybe t)
87 (defmacro defconst-maybe (name &rest everything-else)
88 (or (and (boundp name)
89 (not (get name 'defconst-maybe))
91 (` (or (boundp (quote (, name)))
93 (defconst (, name) (,@ everything-else))
94 (put (quote (, name)) 'defconst-maybe t)
98 (defmacro defun-maybe-cond (name args &optional doc &rest everything-else)
100 (setq everything-else (cons doc everything-else)
103 (or (and (fboundp name)
104 (not (get name 'defun-maybe)))
105 (` (unless (fboundp (quote (, name)))
106 (cond (,@ (mapcar (lambda (case)
109 (` (defun (, name) (, args)
112 (` (defun (, name) (, args)
116 (put (quote (, name)) 'defun-maybe t)
119 (defsubst subr-fboundp (symbol)
120 "Return t if SYMBOL's function definition is a built-in function."
121 (and (fboundp symbol)
122 (subrp (symbol-function symbol))))
124 (defconst-maybe emacs-major-version (string-to-int emacs-version))
125 (defconst-maybe emacs-minor-version
127 (substring emacs-version
128 (string-match (format "%d\\." emacs-major-version)
131 (cond ((featurep 'xemacs)
132 (require 'poe-xemacs)
134 ((string-match "XEmacs" emacs-version)
136 (require 'poe-xemacs)
138 ((> emacs-major-version 20))
139 ((= emacs-major-version 20)
140 (cond ((subr-fboundp 'string)
141 ;; Emacs 20.3 or later
143 ((subr-fboundp 'concat-chars)
144 ;; Emacs 20.1 or later
145 (defalias 'string 'concat-chars)
148 ((= emacs-major-version 19))
154 ;;; @ Emacs 19.23 emulation
157 (defun-maybe minibuffer-prompt-width ()
158 "Return the display width of the minibuffer prompt."
160 (set-buffer (window-buffer (minibuffer-window)))
164 ;;; @ Emacs 19.29 emulation
167 (defvar-maybe path-separator ":"
168 "Character used to separate concatenated paths.")
170 (defun-maybe buffer-substring-no-properties (start end)
171 "Return the characters of part of the buffer, without the text properties.
172 The two arguments START and END are character positions;
173 they can be in either order. [Emacs 19.29 emulating function]"
174 (let ((string (buffer-substring start end)))
175 (set-text-properties 0 (length string) nil string)
178 (defun-maybe match-string (num &optional string)
179 "Return string of text matched by last search.
180 NUM specifies which parenthesized expression in the last regexp.
181 Value is nil if NUMth pair didn't match, or there were less than NUM pairs.
182 Zero means the entire text matched by the whole regexp or whole string.
183 STRING should be given if the last search was by `string-match' on STRING.
184 \[Emacs 19.29 emulating function]"
185 (if (match-beginning num)
187 (substring string (match-beginning num) (match-end num))
188 (buffer-substring (match-beginning num) (match-end num)))))
190 (or (featurep 'xemacs)
191 (>= emacs-major-version 20)
192 (and (= emacs-major-version 19)
193 (>= emacs-minor-version 29))
194 ;; for Emacs 19.28 or earlier
195 (fboundp 'si:read-string)
197 (fset 'si:read-string (symbol-function 'read-string))
198 (defun read-string (prompt &optional initial-input history)
199 "Read a string from the minibuffer, prompting with string PROMPT.
200 If non-nil, second arg INITIAL-INPUT is a string to insert before reading.
201 The third arg HISTORY, is dummy for compatibility.
202 See `read-from-minibuffer' for details of HISTORY argument."
203 (si:read-string prompt initial-input))
206 (defun-maybe rassoc (key list)
207 "Return non-nil if KEY is `equal' to the cdr of an element of LIST.
208 The value is actually the element of LIST whose cdr equals KEY."
211 (if (equal (cdr (car list)) key)
212 (throw 'found (car list))
214 (setq list (cdr list)))
217 (defmacro-maybe make-local-hook (hook))
219 ;; They are not Emacs features
221 (defmacro-maybe add-local-hook (hook function &optional append)
222 (if (fboundp 'make-local-hook)
223 (list 'add-hook hook function append t)
224 (list 'add-hook hook function append)
227 (defmacro-maybe remove-local-hook (hook function)
228 (if (fboundp 'make-local-hook)
229 (list 'remove-hook hook function t)
230 (list 'remove-hook hook function)
234 ;;; @ Emacs 19.30 emulation
237 ;; imported from Emacs 19.30.
238 (defun-maybe add-to-list (list-var element)
239 "Add to the value of LIST-VAR the element ELEMENT if it isn't there yet.
240 If you want to use `add-to-list' on a variable that is not defined
241 until a certain package is loaded, you should put the call to `add-to-list'
242 into a hook function that will be run only after loading the package.
243 \[Emacs 19.30 emulating function]"
244 (or (member element (symbol-value list-var))
245 (set list-var (cons element (symbol-value list-var)))))
247 (cond ((fboundp 'insert-file-contents-literally))
248 ((boundp 'file-name-handler-alist)
249 (defun insert-file-contents-literally
250 (filename &optional visit beg end replace)
251 "Like `insert-file-contents', q.v., but only reads in the file.
252 A buffer may be modified in several ways after reading into the buffer due
253 to advanced Emacs features, such as file-name-handlers, format decoding,
254 find-file-hooks, etc.
255 This function ensures that none of these modifications will take place.
256 \[Emacs 19.30 emulating function]"
257 (let (file-name-handler-alist)
258 (insert-file-contents filename visit beg end replace)))
261 (defalias 'insert-file-contents-literally 'insert-file-contents)
265 ;;; @ Emacs 19.31 emulation
268 (defun-maybe buffer-live-p (object)
269 "Return non-nil if OBJECT is a buffer which has not been killed.
270 Value is nil if OBJECT is not a buffer or if it has been killed.
271 \[Emacs 19.31 emulating function]"
274 (buffer-name (get-buffer object))))
276 ;; imported from Emacs 19.33.
277 (defmacro-maybe save-selected-window (&rest body)
278 "Execute BODY, then select the window that was selected before BODY.
279 \[Emacs 19.31 emulating function]"
281 '((save-selected-window-window (selected-window)))
282 (list 'unwind-protect
284 (list 'select-window 'save-selected-window-window))))
287 ;;; @ Emacs 20.1 emulation
290 ;; imported from Emacs 20.2.
291 (defmacro-maybe when (cond &rest body)
292 "(when COND BODY...): if COND yields non-nil, do BODY, else return nil."
293 (list 'if cond (cons 'progn body)))
295 ;; imported from Emacs 20.3.
296 (defmacro-maybe unless (cond &rest body)
297 "(unless COND BODY...): if COND yields nil, do BODY, else return nil."
298 (cons 'if (cons cond (cons nil body))))
300 ;; imported from Emacs 20.3.
301 (defun-maybe last (x &optional n)
302 "Return the last link of the list X. Its car is the last element.
303 If X is nil, return nil.
304 If N is non-nil, return the Nth-to-last link of X.
305 If N is bigger than the length of X, return X."
309 (setq m (1+ m) p (cdr p)))
311 (if (< n m) (nthcdr (- m n) x) x)))
316 (defmacro-maybe save-current-buffer (&rest body)
317 "Save the current buffer; execute BODY; restore the current buffer.
318 Executes BODY just like `progn'."
319 (` (let ((orig-buffer (current-buffer)))
322 (set-buffer orig-buffer)))))
324 ;; imported from Emacs 20.2.
325 (defmacro-maybe with-current-buffer (buffer &rest body)
326 "Execute the forms in BODY with BUFFER as the current buffer.
327 The value returned is the value of the last form in BODY.
328 See also `with-temp-buffer'."
329 (` (save-current-buffer
330 (set-buffer (, buffer))
333 ;; imported from Emacs 20.2.
334 (defmacro-maybe with-temp-file (file &rest forms)
335 "Create a new buffer, evaluate FORMS there, and write the buffer to FILE.
336 The value of the last form in FORMS is returned, like `progn'.
337 See also `with-temp-buffer'."
338 (let ((temp-file (make-symbol "temp-file"))
339 (temp-buffer (make-symbol "temp-buffer")))
340 (` (let (((, temp-file) (, file))
342 (get-buffer-create (generate-new-buffer-name " *temp file*"))))
345 (with-current-buffer (, temp-buffer)
347 (with-current-buffer (, temp-buffer)
349 (write-region (point-min) (point-max) (, temp-file) nil 0)))
350 (and (buffer-name (, temp-buffer))
351 (kill-buffer (, temp-buffer))))))))
353 ;; imported from Emacs 20.2.
354 (defmacro-maybe with-temp-buffer (&rest forms)
355 "Create a temporary buffer, and evaluate FORMS there like `progn'.
356 See also `with-temp-file' and `with-output-to-string'."
357 (let ((temp-buffer (make-symbol "temp-buffer")))
358 (` (let (((, temp-buffer)
359 (get-buffer-create (generate-new-buffer-name " *temp*"))))
361 (with-current-buffer (, temp-buffer)
363 (and (buffer-name (, temp-buffer))
364 (kill-buffer (, temp-buffer))))))))
366 (defmacro-maybe combine-after-change-calls (&rest body)
370 ;; imported from Emacs 20.3. (cl function)
371 (defun-maybe butlast (x &optional n)
372 "Returns a copy of LIST with the last N elements removed."
373 (if (and n (<= n 0)) x
374 (nbutlast (copy-sequence x) n)))
376 ;; imported from Emacs 20.3. (cl function)
377 (defun-maybe nbutlast (x &optional n)
378 "Modifies LIST to remove the last N elements."
379 (let ((m (length x)))
383 (if (> n 0) (setcdr (nthcdr (- (1- m) n) x) nil))
386 ;; imported from XEmacs 21.
387 (defun-maybe split-string (string &optional pattern)
388 "Return a list of substrings of STRING which are separated by PATTERN.
389 If PATTERN is omitted, it defaults to \"[ \\f\\t\\n\\r\\v]+\"."
391 (setq pattern "[ \f\t\n\r\v]+"))
392 ;; The FSF version of this function takes care not to cons in case
393 ;; of infloop. Maybe we should synch?
394 (let (parts (start 0))
395 (while (string-match pattern string start)
396 (setq parts (cons (substring string start (match-beginning 0)) parts)
397 start (match-end 0)))
398 (nreverse (cons (substring string start) parts))))
401 ;;; @ Emacs 20.3 emulation
404 ;; imported from Emacs 20.3.91.
405 (defvar-maybe temporary-file-directory
406 (file-name-as-directory
407 (cond ((memq system-type '(ms-dos windows-nt))
408 (or (getenv "TEMP") (getenv "TMPDIR") (getenv "TMP") "c:/temp"))
409 ((memq system-type '(vax-vms axp-vms))
410 (or (getenv "TMPDIR") (getenv "TMP") (getenv "TEMP") "SYS$SCRATCH:"))
412 (or (getenv "TMPDIR") (getenv "TMP") (getenv "TEMP") "/tmp"))))
413 "The directory for writing temporary files.")
415 (defun-maybe line-beginning-position (&optional n)
416 "Return the character position of the first character on the current line.
417 With argument N not nil or 1, move forward N - 1 lines first.
418 If scan reaches end of buffer, return that position.
419 This function does not move point."
422 (forward-line (1- n))
427 (defun-maybe line-end-position (&optional n)
428 "Return the character position of the last character on the current line.
429 With argument N not nil or 1, move forward N - 1 lines first.
430 If scan reaches end of buffer, return that position.
431 This function does not move point."
434 (forward-line (1- n))
439 (defun-maybe string (&rest chars)
440 "Concatenate all the argument characters and make the result a string."
441 (mapconcat (function char-to-string) chars "")
445 ;;; @ XEmacs emulation
448 (defun-maybe find-face (face-or-name)
449 "Retrieve the face of the given name.
450 If FACE-OR-NAME is a face object, it is simply returned.
451 Otherwise, FACE-OR-NAME should be a symbol. If there is no such face,
452 nil is returned. Otherwise the associated face object is returned.
453 \[XEmacs emulating function]"
454 (car (memq face-or-name (face-list)))
457 (defun-maybe point-at-bol (&optional n buffer)
458 "Return the character position of the first character on the current line.
459 With argument N not nil or 1, move forward N - 1 lines first.
460 If scan reaches end of buffer, return that position.
461 This function does not move point. [XEmacs emulating function]"
466 (line-beginning-position n)
469 (defun-maybe point-at-eol (&optional n buffer)
470 "Return the character position of the last character on the current line.
471 With argument N not nil or 1, move forward N - 1 lines first.
472 If scan reaches end of buffer, return that position.
473 This function does not move point. [XEmacs emulating function]"
478 (line-end-position n)
481 (defun-maybe functionp (obj)
482 "Returns t if OBJ is a function, nil otherwise.
483 \[XEmacs emulating function]"
485 (byte-code-function-p obj)
486 (and (symbolp obj)(fboundp obj))
487 (and (consp obj)(eq (car obj) 'lambda))
490 (defsubst-maybe define-obsolete-function-alias (oldfun newfun)
491 "Define OLDFUN as an obsolete alias for function NEWFUN.
492 This makes calling OLDFUN equivalent to calling NEWFUN and marks OLDFUN
493 as obsolete. [XEmacs emulating function]"
494 (defalias oldfun newfun)
495 (make-obsolete oldfun newfun)
498 (when (subr-fboundp 'read-event)
499 ;; for Emacs 19 or later
501 (defun-maybe-cond next-command-event (&optional event prompt)
502 "Read an event object from the input stream.
503 If EVENT is non-nil, it should be an event object and will be filled
504 in and returned; otherwise a new event object will be created and
506 If PROMPT is non-nil, it should be a string and will be displayed in
507 the echo area while this function is waiting for an event.
508 \[XEmacs emulating function]"
509 ((subr-fboundp 'string)
510 ;; for Emacs 20.3 or later
511 (read-event prompt t)
514 (if prompt (message prompt))
518 (defsubst-maybe character-to-event (ch)
519 "Convert keystroke CH into an event structure, replete with bucky bits.
520 Note that CH (the keystroke specifier) can be an integer, a character
521 or a symbol such as 'clear. [XEmacs emulating function]"
524 (defun-maybe event-to-character (event)
525 "Return the character approximation to the given event object.
526 If the event isn't a keypress, this returns nil.
527 \[XEmacs emulating function]"
528 (cond ((symbolp event)
529 ;; mask is (BASE-TYPE MODIFIER-BITS) or nil.
530 (let ((mask (get event 'event-symbol-element-mask)))
532 (let ((base (get (car mask) 'ascii-character)))
534 (logior base (car (cdr mask)))
536 ((integerp event) event)
541 ;;; @ MULE 2 emulation
544 (defun-maybe-cond cancel-undo-boundary ()
545 "Cancel undo boundary. [MULE 2.3 emulating function]"
546 ((boundp 'buffer-undo-list)
547 ;; for Emacs 19.7 or later
548 (if (and (consp buffer-undo-list)
550 (null (car buffer-undo-list)))
551 (setq buffer-undo-list (cdr buffer-undo-list))
554 ;; for anything older than Emacs 19.7.