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 (defsubst subr-fboundp (symbol)
99 "Return t if SYMBOL's function definition is a built-in function."
100 (and (fboundp symbol)
101 (subrp (symbol-function symbol))))
103 (defconst-maybe emacs-major-version (string-to-int emacs-version))
104 (defconst-maybe emacs-minor-version
106 (substring emacs-version
107 (string-match (format "%d\\." emacs-major-version)
110 (cond ((featurep 'xemacs)
111 (require 'poe-xemacs)
113 ((string-match "XEmacs" emacs-version)
115 (require 'poe-xemacs)
117 ((> emacs-major-version 20))
118 ((= emacs-major-version 20)
119 (cond ((subr-fboundp 'string)
120 ;; Emacs 20.3 or later
122 ((subr-fboundp 'concat-chars)
123 ;; Emacs 20.1 or later
124 (defalias 'string 'concat-chars)
127 ((= emacs-major-version 19))
133 ;;; @ Emacs 19 emulation
136 (defmacro-maybe eval-and-compile (&rest body)
137 "Like `progn', but evaluates the body at compile time and at load time."
138 ;; Remember, it's magic.
141 (defun-maybe minibuffer-prompt-width ()
142 "Return the display width of the minibuffer prompt."
144 (set-buffer (window-buffer (minibuffer-window)))
148 ;;; @ Emacs 19.29 emulation
151 (defvar-maybe path-separator ":"
152 "Character used to separate concatenated paths.")
154 (defun-maybe buffer-substring-no-properties (start end)
155 "Return the characters of part of the buffer, without the text properties.
156 The two arguments START and END are character positions;
157 they can be in either order. [Emacs 19.29 emulating function]"
158 (let ((string (buffer-substring start end)))
159 (set-text-properties 0 (length string) nil string)
162 (defun-maybe match-string (num &optional string)
163 "Return string of text matched by last search.
164 NUM specifies which parenthesized expression in the last regexp.
165 Value is nil if NUMth pair didn't match, or there were less than NUM pairs.
166 Zero means the entire text matched by the whole regexp or whole string.
167 STRING should be given if the last search was by `string-match' on STRING.
168 \[Emacs 19.29 emulating function]"
169 (if (match-beginning num)
171 (substring string (match-beginning num) (match-end num))
172 (buffer-substring (match-beginning num) (match-end num)))))
174 (or (featurep 'xemacs)
175 (>= emacs-major-version 20)
176 (and (= emacs-major-version 19)
177 (>= emacs-minor-version 29))
178 ;; for Emacs 19.28 or earlier
179 (fboundp 'si:read-string)
181 (fset 'si:read-string (symbol-function 'read-string))
182 (defun read-string (prompt &optional initial-input history)
183 "Read a string from the minibuffer, prompting with string PROMPT.
184 If non-nil, second arg INITIAL-INPUT is a string to insert before reading.
185 The third arg HISTORY, is dummy for compatibility.
186 See `read-from-minibuffer' for details of HISTORY argument."
187 (si:read-string prompt initial-input))
190 (defun-maybe rassoc (key list)
191 "Return non-nil if KEY is `equal' to the cdr of an element of LIST.
192 The value is actually the element of LIST whose cdr equals KEY."
195 (if (equal (cdr (car list)) key)
196 (throw 'found (car list))
198 (setq list (cdr list)))
201 (defmacro-maybe make-local-hook (hook))
203 ;; They are not Emacs features
205 (defmacro-maybe add-local-hook (hook function &optional append)
206 (if (fboundp 'make-local-hook)
207 (list 'add-hook hook function append t)
208 (list 'add-hook hook function append)
211 (defmacro-maybe remove-local-hook (hook function)
212 (if (fboundp 'make-local-hook)
213 (list 'remove-hook hook function t)
214 (list 'remove-hook hook function)
218 ;;; @ Emacs 19.30 emulation
221 ;; imported from Emacs 19.30.
222 (defun-maybe add-to-list (list-var element)
223 "Add to the value of LIST-VAR the element ELEMENT if it isn't there yet.
224 If you want to use `add-to-list' on a variable that is not defined
225 until a certain package is loaded, you should put the call to `add-to-list'
226 into a hook function that will be run only after loading the package.
227 \[Emacs 19.30 emulating function]"
228 (or (member element (symbol-value list-var))
229 (set list-var (cons element (symbol-value list-var)))))
231 (cond ((fboundp 'insert-file-contents-literally))
232 ((boundp 'file-name-handler-alist)
233 (defun insert-file-contents-literally
234 (filename &optional visit beg end replace)
235 "Like `insert-file-contents', q.v., but only reads in the file.
236 A buffer may be modified in several ways after reading into the buffer due
237 to advanced Emacs features, such as file-name-handlers, format decoding,
238 find-file-hooks, etc.
239 This function ensures that none of these modifications will take place.
240 \[Emacs 19.30 emulating function]"
241 (let (file-name-handler-alist)
242 (insert-file-contents filename visit beg end replace)))
245 (defalias 'insert-file-contents-literally 'insert-file-contents)
249 ;;; @ Emacs 19.31 emulation
252 (defun-maybe buffer-live-p (object)
253 "Return non-nil if OBJECT is a buffer which has not been killed.
254 Value is nil if OBJECT is not a buffer or if it has been killed.
255 \[Emacs 19.31 emulating function]"
258 (buffer-name (get-buffer object))))
260 ;; imported from Emacs 19.33.
261 (defmacro-maybe save-selected-window (&rest body)
262 "Execute BODY, then select the window that was selected before BODY.
263 \[Emacs 19.31 emulating function]"
265 '((save-selected-window-window (selected-window)))
266 (list 'unwind-protect
268 (list 'select-window 'save-selected-window-window))))
271 ;;; @ Emacs 20.1 emulation
274 ;; imported from Emacs 20.2.
275 (defmacro-maybe when (cond &rest body)
276 "(when COND BODY...): if COND yields non-nil, do BODY, else return nil."
277 (list 'if cond (cons 'progn body)))
279 ;; imported from Emacs 20.3.
280 (defmacro-maybe unless (cond &rest body)
281 "(unless COND BODY...): if COND yields nil, do BODY, else return nil."
282 (cons 'if (cons cond (cons nil body))))
284 (defmacro-maybe save-current-buffer (&rest body)
285 "Save the current buffer; execute BODY; restore the current buffer.
286 Executes BODY just like `progn'."
287 (` (let ((orig-buffer (current-buffer)))
290 (set-buffer orig-buffer)))))
292 ;; imported from Emacs 20.2.
293 (defmacro-maybe with-current-buffer (buffer &rest body)
294 "Execute the forms in BODY with BUFFER as the current buffer.
295 The value returned is the value of the last form in BODY.
296 See also `with-temp-buffer'."
297 (` (save-current-buffer
298 (set-buffer (, buffer))
301 ;; imported from Emacs 20.2.
302 (defmacro-maybe with-temp-file (file &rest forms)
303 "Create a new buffer, evaluate FORMS there, and write the buffer to FILE.
304 The value of the last form in FORMS is returned, like `progn'.
305 See also `with-temp-buffer'."
306 (let ((temp-file (make-symbol "temp-file"))
307 (temp-buffer (make-symbol "temp-buffer")))
308 (` (let (((, temp-file) (, file))
310 (get-buffer-create (generate-new-buffer-name " *temp file*"))))
313 (with-current-buffer (, temp-buffer)
315 (with-current-buffer (, temp-buffer)
317 (write-region (point-min) (point-max) (, temp-file) nil 0)))
318 (and (buffer-name (, temp-buffer))
319 (kill-buffer (, temp-buffer))))))))
321 ;; imported from Emacs 20.2.
322 (defmacro-maybe with-temp-buffer (&rest forms)
323 "Create a temporary buffer, and evaluate FORMS there like `progn'.
324 See also `with-temp-file' and `with-output-to-string'."
325 (let ((temp-buffer (make-symbol "temp-buffer")))
326 (` (let (((, temp-buffer)
327 (get-buffer-create (generate-new-buffer-name " *temp*"))))
329 (with-current-buffer (, temp-buffer)
331 (and (buffer-name (, temp-buffer))
332 (kill-buffer (, temp-buffer))))))))
334 ;; imported from Emacs 20.3.
335 (defun-maybe last (x &optional n)
336 "Return the last link of the list X. Its car is the last element.
337 If X is nil, return nil.
338 If N is non-nil, return the Nth-to-last link of X.
339 If N is bigger than the length of X, return X."
343 (setq m (1+ m) p (cdr p)))
345 (if (< n m) (nthcdr (- m n) x) x)))
350 ;; imported from Emacs 20.3. (cl function)
351 (defun-maybe butlast (x &optional n)
352 "Returns a copy of LIST with the last N elements removed."
353 (if (and n (<= n 0)) x
354 (nbutlast (copy-sequence x) n)))
356 ;; imported from Emacs 20.3. (cl function)
357 (defun-maybe nbutlast (x &optional n)
358 "Modifies LIST to remove the last N elements."
359 (let ((m (length x)))
363 (if (> n 0) (setcdr (nthcdr (- (1- m) n) x) nil))
366 ;; imported from XEmacs 21.
367 (defun-maybe split-string (string &optional pattern)
368 "Return a list of substrings of STRING which are separated by PATTERN.
369 If PATTERN is omitted, it defaults to \"[ \\f\\t\\n\\r\\v]+\"."
371 (setq pattern "[ \f\t\n\r\v]+"))
372 ;; The FSF version of this function takes care not to cons in case
373 ;; of infloop. Maybe we should synch?
374 (let (parts (start 0))
375 (while (string-match pattern string start)
376 (setq parts (cons (substring string start (match-beginning 0)) parts)
377 start (match-end 0)))
378 (nreverse (cons (substring string start) parts))))
381 ;;; @ Emacs 20.3 emulation
384 ;; imported from Emacs 20.3.91.
385 (defvar-maybe temporary-file-directory
386 (file-name-as-directory
387 (cond ((memq system-type '(ms-dos windows-nt))
388 (or (getenv "TEMP") (getenv "TMPDIR") (getenv "TMP") "c:/temp"))
389 ((memq system-type '(vax-vms axp-vms))
390 (or (getenv "TMPDIR") (getenv "TMP") (getenv "TEMP") "SYS$SCRATCH:"))
392 (or (getenv "TMPDIR") (getenv "TMP") (getenv "TEMP") "/tmp"))))
393 "The directory for writing temporary files.")
395 (defun-maybe line-beginning-position (&optional n)
396 "Return the character position of the first character on the current line.
397 With argument N not nil or 1, move forward N - 1 lines first.
398 If scan reaches end of buffer, return that position.
399 This function does not move point."
402 (forward-line (1- n))
407 (defun-maybe line-end-position (&optional n)
408 "Return the character position of the last character on the current line.
409 With argument N not nil or 1, move forward N - 1 lines first.
410 If scan reaches end of buffer, return that position.
411 This function does not move point."
414 (forward-line (1- n))
419 (defun-maybe string (&rest chars)
420 "Concatenate all the argument characters and make the result a string."
421 (mapconcat (function char-to-string) chars "")
425 ;;; @ XEmacs emulation
428 (defun-maybe find-face (face-or-name)
429 "Retrieve the face of the given name.
430 If FACE-OR-NAME is a face object, it is simply returned.
431 Otherwise, FACE-OR-NAME should be a symbol. If there is no such face,
432 nil is returned. Otherwise the associated face object is returned.
433 \[XEmacs emulating function]"
434 (car (memq face-or-name (face-list)))
437 (defun-maybe point-at-bol (&optional n buffer)
438 "Return the character position of the first character on the current line.
439 With argument N not nil or 1, move forward N - 1 lines first.
440 If scan reaches end of buffer, return that position.
441 This function does not move point. [XEmacs emulating function]"
446 (line-beginning-position n)
449 (defun-maybe point-at-eol (&optional n buffer)
450 "Return the character position of the last character on the current line.
451 With argument N not nil or 1, move forward N - 1 lines first.
452 If scan reaches end of buffer, return that position.
453 This function does not move point. [XEmacs emulating function]"
458 (line-end-position n)
461 (defun-maybe functionp (obj)
462 "Returns t if OBJ is a function, nil otherwise.
463 \[XEmacs emulating function]"
465 (byte-code-function-p obj)
466 (and (symbolp obj)(fboundp obj))
467 (and (consp obj)(eq (car obj) 'lambda))
470 (defsubst-maybe define-obsolete-function-alias (oldfun newfun)
471 "Define OLDFUN as an obsolete alias for function NEWFUN.
472 This makes calling OLDFUN equivalent to calling NEWFUN and marks OLDFUN
473 as obsolete. [XEmacs emulating function]"
474 (defalias oldfun newfun)
475 (make-obsolete oldfun newfun)
478 (when (subr-fboundp 'read-event)
479 ;; for Emacs 19 or later
481 ((subr-fboundp 'string)
482 ;; for Emacs 20.3 or later
483 (defun-maybe next-command-event (&optional event prompt)
484 "Read an event object from the input stream.
485 If EVENT is non-nil, it should be an event object and will be filled
486 in and returned; otherwise a new event object will be created and
488 If PROMPT is non-nil, it should be a string and will be displayed in
489 the echo area while this function is waiting for an event.
490 \[XEmacs emulating function]"
491 (read-event prompt t)
495 (defun-maybe next-command-event (&optional event prompt)
496 "Read an event object from the input stream.
497 If EVENT is non-nil, it should be an event object and will be filled
498 in and returned; otherwise a new event object will be created and
500 If PROMPT is non-nil, it should be a string and will be displayed in
501 the echo area while this function is waiting for an event.
502 \[XEmacs emulating function]"
508 (defsubst-maybe character-to-event (ch)
509 "Convert keystroke CH into an event structure, replete with bucky bits.
510 Note that CH (the keystroke specifier) can be an integer, a character
511 or a symbol such as 'clear. [XEmacs emulating function]"
514 (defun-maybe event-to-character (event)
515 "Return the character approximation to the given event object.
516 If the event isn't a keypress, this returns nil.
517 \[XEmacs emulating function]"
518 (cond ((symbolp event)
519 ;; mask is (BASE-TYPE MODIFIER-BITS) or nil.
520 (let ((mask (get event 'event-symbol-element-mask)))
522 (let ((base (get (car mask) 'ascii-character)))
524 (logior base (car (cdr mask)))
526 ((integerp event) event)
531 ;;; @ MULE 2 emulation
534 (if (boundp 'buffer-undo-list)
535 ;; for Emacs 19.7 or later
536 (defun-maybe cancel-undo-boundary ()
537 "Cancel undo boundary. [MULE 2.3 emulating function]"
538 (if (and (consp buffer-undo-list)
540 (null (car buffer-undo-list)))
541 (setq buffer-undo-list (cdr buffer-undo-list))
543 ;; for anything older than Emacs 19.7.
544 (defun-maybe cancel-undo-boundary ()
545 "Cancel undo boundary. [MULE 2.3 emulating function]")