1 ;;; poe.el --- Portable Outfit for Emacsen
3 ;; Copyright (C) 1995,1996,1997,1998,1999 Free Software Foundation, Inc.
5 ;; Author: MORIOKA Tomohiko <tomo@m17n.org>
6 ;; Shuhei KOBAYASHI <shuhei@aqua.ocn.ne.jp>
7 ;; Keywords: emulation, compatibility, Nemacs, MULE, Emacs/mule, XEmacs
9 ;; This file is part of APEL (A Portable Emacs Library).
11 ;; This program is free software; you can redistribute it and/or
12 ;; modify it under the terms of the GNU General Public License as
13 ;; published by the Free Software Foundation; either version 2, or (at
14 ;; your option) any later version.
16 ;; This program is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ;; General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
30 (provide 'poe) ; beware of circular dependency.
31 ; localhook.el depends on poe.el.
35 ;;; @ Version information.
38 ;; v18 does not have many features we expect,
39 ;; notably `eval-when-compile' and `eval-and-compile'.
40 (static-when (string= (substring emacs-version 0 2) "18")
43 ;; Now we can use them!
45 ;; We must define these two constants at compile-time as well as
46 ;; load-time since they are used for compile-time version checking.
47 (defconst-maybe emacs-major-version
48 (progn (string-match "^[0-9]+" emacs-version)
49 (string-to-int (substring emacs-version
50 (match-beginning 0)(match-end 0))))
51 "Major version number of this version of Emacs.")
52 (defconst-maybe emacs-minor-version
53 (progn (string-match "^[0-9]+\\.\\([0-9]+\\)" emacs-version)
54 (string-to-int (substring emacs-version
55 (match-beginning 1)(match-end 1))))
56 "Minor version number of this version of Emacs."))
58 ;; Some ancient version of XEmacs did not provide 'xemacs.
59 (static-when (string-match "XEmacs" emacs-version)
62 ;; `file-coding' was appeared in the spring of 1998, just before XEmacs
63 ;; 21.0. Therefore it is not provided in XEmacs with MULE versions 20.4
65 (static-when (featurep 'xemacs)
66 ;; must be load-time check to share .elc between w/ MULE and w/o MULE.
67 (when (featurep 'mule)
68 (provide 'file-coding)))
70 (static-when (featurep 'xemacs)
71 (require 'poe-xemacs))
73 ;; must be load-time check to share .elc between different systems.
74 (or (fboundp 'open-network-stream)
78 ;;; @ C primitives emulation.
81 ;; Emacs 20.3 and earlier: (require FEATURE &optional FILENAME)
82 ;; Emacs 20.4 and later: (require FEATURE &optional FILENAME NOERROR)
83 (static-condition-case nil
84 ;; compile-time check.
86 (require 'nofeature "nofile" 'noerror)
87 (if (get 'require 'defun-maybe)
88 (error ""))) ; already redefined.
91 (or (fboundp 'si:require)
93 (fset 'si:require (symbol-function 'require))
94 (put 'require 'defun-maybe t)
95 (defun require (feature &optional filename noerror)
97 If feature FEATURE is not loaded, load it from FILENAME.
98 If FEATURE is not a member of the list `features', then the feature
99 is not loaded; so load the file FILENAME.
100 If FILENAME is omitted, the printname of FEATURE is used as the file name,
101 but in this case `load' insists on adding the suffix `.el' or `.elc'.
102 If the optional third argument NOERROR is non-nil,
103 then return nil if the file is not found.
104 Normally the return value is FEATURE."
107 (si:require feature filename)
109 (si:require feature filename)))))))
111 ;; Emacs 19.29 and later: (plist-get PLIST PROP)
112 ;; (defun-maybe plist-get (plist prop)
114 ;; (not (eq (car plist) prop)))
115 ;; (setq plist (cdr (cdr plist))))
116 ;; (car (cdr plist)))
117 (static-unless (and (fboundp 'plist-get)
118 (not (get 'plist-get 'defun-maybe)))
119 (or (fboundp 'plist-get)
121 (defvar plist-get-internal-symbol)
122 (defun plist-get (plist prop)
124 Extract a value from a property list.
125 PLIST is a property list, which is a list of the form
126 \(PROP1 VALUE1 PROP2 VALUE2...\). This function returns the value
127 corresponding to the given PROP, or nil if PROP is not
128 one of the properties on the list."
129 (setplist 'plist-get-internal-symbol plist)
130 (get 'plist-get-internal-symbol prop))
131 ;; for `load-history'.
132 (setq current-load-list (cons 'plist-get current-load-list))
133 (put 'plist-get 'defun-maybe t))))
135 ;; Emacs 19.29 and later: (plist-put PLIST PROP VAL)
136 ;; (defun-maybe plist-put (plist prop val)
138 ;; (let ((tail plist)
140 ;; (while (and tail (cdr tail))
141 ;; (if (eq (car tail) prop)
143 ;; (setcar (cdr tail) val)
144 ;; (throw 'found plist))
146 ;; tail (cdr (cdr tail)))))
149 ;; (setcdr (cdr prev) (list prop val))
151 ;; (list prop val)))))
152 (static-unless (and (fboundp 'plist-put)
153 (not (get 'plist-put 'defun-maybe)))
154 (or (fboundp 'plist-put)
156 (defvar plist-put-internal-symbol)
157 (defun plist-put (plist prop val)
159 Change value in PLIST of PROP to VAL.
160 PLIST is a property list, which is a list of the form
161 \(PROP1 VALUE1 PROP2 VALUE2 ...\). PROP is a symbol and VAL is any object.
162 If PROP is already a property on the list, its value is set to VAL,
163 otherwise the new PROP VAL pair is added. The new plist is returned;
164 use `\(setq x \(plist-put x prop val\)\)' to be sure to use the new value.
165 The PLIST is modified by side effects."
166 (setplist 'plist-put-internal-symbol plist)
167 (put 'plist-put-internal-symbol prop val)
168 (symbol-plist 'plist-put-internal-symbol))
169 ;; for `load-history'.
170 (setq current-load-list (cons 'plist-put current-load-list))
171 (put 'plist-put 'defun-maybe t))))
173 ;; Emacs 19.23 and later: (minibuffer-prompt-width)
174 (defun-maybe minibuffer-prompt-width ()
175 "Return the display width of the minibuffer prompt."
177 (set-buffer (window-buffer (minibuffer-window)))
180 ;; (read-string PROMPT &optional INITIAL-INPUT HISTORY)
181 ;; Emacs 19.29/XEmacs 19.14(?) and later takes optional 3rd arg HISTORY.
182 (static-unless (or (featurep 'xemacs)
183 (>= emacs-major-version 20)
184 (and (= emacs-major-version 19)
185 (>= emacs-minor-version 29)))
186 (or (fboundp 'si:read-string)
188 (fset 'si:read-string (symbol-function 'read-string))
189 (defun read-string (prompt &optional initial-input history)
191 Read a string from the minibuffer, prompting with string PROMPT.
192 If non-nil, second arg INITIAL-INPUT is a string to insert before reading.
193 The third arg HISTORY, is dummy for compatibility.
194 See `read-from-minibuffer' for details of HISTORY argument."
195 (si:read-string prompt initial-input)))))
197 ;; v18: (string-to-int STRING)
198 ;; v19: (string-to-number STRING)
199 ;; v20: (string-to-number STRING &optional BASE)
201 ;; XXX: `string-to-number' of Emacs 20.3 and earlier is broken.
202 ;; (string-to-number "1e1" 16) => 10.0, should be 481.
203 (static-condition-case nil
204 ;; compile-time check.
205 (if (= (string-to-number "1e1" 16) 481)
206 (if (get 'string-to-number 'defun-maybe)
207 (error "")) ; already redefined.
208 (error "")) ; Emacs 20.3 and ealier.
211 (or (fboundp 'si:string-to-number)
213 (if (fboundp 'string-to-number)
214 (fset 'si:string-to-number (symbol-function 'string-to-number))
215 (fset 'si:string-to-number (symbol-function 'string-to-int))
216 (defalias 'string-to-int 'string-to-number))
217 (put 'string-to-number 'defun-maybe t)
218 (defun string-to-number (string &optional base)
220 Convert STRING to a number by parsing it as a decimal number.
221 This parses both integers and floating point numbers.
222 It ignores leading spaces and tabs.
224 If BASE, interpret STRING as a number in that base. If BASE isn't
225 present, base 10 is used. BASE must be between 2 and 16 (inclusive).
226 If the base used is not 10, floating point is not recognized."
227 (if (or (null base) (= base 10))
228 (si:string-to-number string)
229 (if (or (< base 2)(> base 16))
230 (signal 'args-out-of-range (cons base nil)))
231 (let ((len (length string))
233 ;; skip leading whitespace.
234 (while (and (< pos len)
235 (memq (aref string pos) '(?\ ?\t)))
239 (let ((number 0)(negative 1)
241 (if (eq (aref string pos) ?-)
244 (if (eq (aref string pos) ?+)
245 (setq pos (1+ pos))))
246 (while (and (< pos len)
247 (setq chr (aref string pos)
249 ((and (<= ?0 chr)(<= chr ?9))
251 ((and (<= ?A chr)(<= chr ?F))
253 ((and (<= ?a chr)(<= chr ?f))
257 (setq number (+ (* number base) num)
259 (* negative number))))))))))
261 ;; Emacs 20.1 and 20.2: (concat-chars &rest CHARS)
262 ;; Emacs 20.3/XEmacs 21.0 and later: (string &rest CHARS)
264 ((and (fboundp 'string)
265 (subrp (symbol-function 'string)))
266 ;; Emacs 20.3/XEmacs 21.0 and later.
268 ((and (fboundp 'concat-chars)
269 (subrp (symbol-function 'concat-chars)))
270 ;; Emacs 20.1 and 20.2.
271 (defalias 'string 'concat-chars))
273 ;; Use `defun-maybe' to update `load-history'.
274 (defun-maybe string (&rest chars)
275 "Concatenate all the argument characters and make the result a string."
276 ;; We cannot use (apply 'concat chars) here because `concat' does not
277 ;; work with multibyte chars on Mule 1.* and 2.*.
278 (mapconcat (function char-to-string) chars ""))))
280 ;; Mule: (char-before POS)
281 ;; v20: (char-before &optional POS)
282 (static-condition-case nil
283 ;; compile-time check.
286 (if (get 'char-before 'defun-maybe)
287 (error ""))) ; already defined.
288 (wrong-number-of-arguments ; Mule.
290 (or (fboundp 'si:char-before)
292 (fset 'si:char-before (symbol-function 'char-before))
293 (put 'char-before 'defun-maybe t)
294 ;; takes IGNORED for backward compatibility.
295 (defun char-before (&optional pos ignored)
297 Return character in current buffer preceding position POS.
298 POS is an integer or a buffer pointer.
299 If POS is out of range, the value is nil."
300 (si:char-before (or pos (point)))))))
301 (void-function ; non-Mule.
303 (defun-maybe char-before (&optional pos)
305 Return character in current buffer preceding position POS.
306 POS is an integer or a buffer pointer.
307 If POS is out of range, the value is nil."
310 (and (= (goto-char pos) (point))
315 (error ; found our definition at compile-time.
319 (wrong-number-of-arguments ; Mule.
320 (or (fboundp 'si:char-before)
322 (fset 'si:char-before (symbol-function 'char-before))
323 (put 'char-before 'defun-maybe t)
324 ;; takes IGNORED for backward compatibility.
325 (defun char-before (&optional pos ignored)
327 Return character in current buffer preceding position POS.
328 POS is an integer or a buffer pointer.
329 If POS is out of range, the value is nil."
330 (si:char-before (or pos (point)))))))
331 (void-function ; non-Mule.
332 (defun-maybe char-before (&optional pos)
334 Return character in current buffer preceding position POS.
335 POS is an integer or a buffer pointer.
336 If POS is out of range, the value is nil."
339 (and (= (goto-char pos) (point))
343 (preceding-char))))))))
345 ;; v18, v19: (char-after POS)
346 ;; v20: (char-after &optional POS)
347 (static-condition-case nil
348 ;; compile-time check.
351 (if (get 'char-after 'defun-maybe)
352 (error ""))) ; already defined.
353 (wrong-number-of-arguments ; v18, v19
355 (or (fboundp 'si:char-after)
357 (fset 'si:char-after (symbol-function 'char-after))
358 (put 'char-after 'defun-maybe t)
359 (defun char-after (&optional pos)
361 Return character in current buffer at position POS.
362 POS is an integer or a buffer pointer.
363 If POS is out of range, the value is nil."
364 (si:char-after (or pos (point)))))))
365 (void-function ; NEVER happen?
367 (defun-maybe char-after (&optional pos)
369 Return character in current buffer at position POS.
370 POS is an integer or a buffer pointer.
371 If POS is out of range, the value is nil."
374 (and (= (goto-char pos) (point))
379 (error ; found our definition at compile-time.
383 (wrong-number-of-arguments ; v18, v19
384 (or (fboundp 'si:char-after)
386 (fset 'si:char-after (symbol-function 'char-after))
387 (put 'char-after 'defun-maybe t)
388 (defun char-after (&optional pos)
390 Return character in current buffer at position POS.
391 POS is an integer or a buffer pointer.
392 If POS is out of range, the value is nil."
393 (si:char-after (or pos (point)))))))
394 (void-function ; NEVER happen?
395 (defun-maybe char-after (&optional pos)
397 Return character in current buffer at position POS.
398 POS is an integer or a buffer pointer.
399 If POS is out of range, the value is nil."
402 (and (= (goto-char pos) (point))
406 (following-char))))))))
408 ;; Emacs 19.29 and later: (buffer-substring-no-properties START END)
409 (defun-maybe buffer-substring-no-properties (start end)
410 "Return the characters of part of the buffer, without the text properties.
411 The two arguments START and END are character positions;
412 they can be in either order."
413 (let ((string (buffer-substring start end)))
414 (set-text-properties 0 (length string) nil string)
417 ;; Emacs 19.31 and later: (buffer-live-p OBJECT)
418 (defun-maybe buffer-live-p (object)
419 "Return non-nil if OBJECT is a buffer which has not been killed.
420 Value is nil if OBJECT is not a buffer or if it has been killed."
423 (buffer-name (get-buffer object))
426 ;; Emacs 20: (line-beginning-position &optional N)
427 (defun-maybe line-beginning-position (&optional n)
428 "Return the character position of the first 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."
433 (forward-line (1- (or n 1)))
436 ;; Emacs 20: (line-end-position &optional N)
437 (defun-maybe line-end-position (&optional n)
438 "Return the character position of the last 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."
443 (end-of-line (or n 1))
447 ;;; @ Basic lisp subroutines emulation. (lisp/subr.el)
450 ;;; @@ Lisp language features.
452 (defmacro-maybe push (newelt listname)
453 "Add NEWELT to the list stored in the symbol LISTNAME.
454 This is equivalent to (setq LISTNAME (cons NEWELT LISTNAME)).
455 LISTNAME must be a symbol."
457 (list 'cons newelt listname)))
459 (defmacro-maybe pop (listname)
460 "Return the first element of LISTNAME's value, and remove it from the list.
461 LISTNAME must be a symbol whose value is a list.
462 If the value is nil, `pop' returns nil but does not actually
464 (list 'prog1 (list 'car listname)
465 (list 'setq listname (list 'cdr listname))))
467 (defmacro-maybe when (cond &rest body)
468 "If COND yields non-nil, do BODY, else return nil."
469 (list 'if cond (cons 'progn body)))
470 ;; (def-edebug-spec when (&rest form))
472 (defmacro-maybe unless (cond &rest body)
473 "If COND yields nil, do BODY, else return nil."
474 (cons 'if (cons cond (cons nil body))))
475 ;; (def-edebug-spec unless (&rest form))
477 (defsubst-maybe caar (x)
478 "Return the car of the car of X."
481 (defsubst-maybe cadr (x)
482 "Return the car of the cdr of X."
485 (defsubst-maybe cdar (x)
486 "Return the cdr of the car of X."
489 (defsubst-maybe cddr (x)
490 "Return the cdr of the cdr of X."
493 (defun-maybe last (x &optional n)
494 "Return the last link of the list X. Its car is the last element.
495 If X is nil, return nil.
496 If N is non-nil, return the Nth-to-last link of X.
497 If N is bigger than the length of X, return X."
501 (setq m (1+ m) p (cdr p)))
503 (if (< n m) (nthcdr (- m n) x) x)))
508 ;; Actually, `butlast' and `nbutlast' are defined in lisp/cl.el.
509 (defun-maybe butlast (x &optional n)
510 "Returns a copy of LIST with the last N elements removed."
511 (if (and n (<= n 0)) x
512 (nbutlast (copy-sequence x) n)))
514 (defun-maybe nbutlast (x &optional n)
515 "Modifies LIST to remove the last N elements."
516 (let ((m (length x)))
520 (if (> n 0) (setcdr (nthcdr (- (1- m) n) x) nil))
523 ;; Emacs 20.3 and later: (assoc-default KEY ALIST &optional TEST DEFAULT)
524 (defun-maybe assoc-default (key alist &optional test default)
525 "Find object KEY in a pseudo-alist ALIST.
526 ALIST is a list of conses or objects. Each element (or the element's car,
527 if it is a cons) is compared with KEY by evaluating (TEST (car elt) KEY).
528 If that is non-nil, the element matches;
529 then `assoc-default' returns the element's cdr, if it is a cons,
530 or DEFAULT if the element is not a cons.
532 If no element matches, the value is nil.
533 If TEST is omitted or nil, `equal' is used."
534 (let (found (tail alist) value)
535 (while (and tail (not found))
536 (let ((elt (car tail)))
537 (when (funcall (or test 'equal) (if (consp elt) (car elt) elt) key)
538 (setq found t value (if (consp elt) (cdr elt) default))))
539 (setq tail (cdr tail)))
542 ;; The following two function use `compare-strings', which we don't
544 ;; (defun assoc-ignore-case (key alist))
545 ;; (defun assoc-ignore-representation (key alist))
547 ;; Emacs 19.29/XEmacs 19.14(?) and later: (rassoc KEY LIST)
548 ;; Actually, `rassoc' is defined in src/fns.c.
549 (defun-maybe rassoc (key list)
550 "Return non-nil if KEY is `equal' to the cdr of an element of LIST.
551 The value is actually the element of LIST whose cdr equals KEY.
552 Elements of LIST that are not conses are ignored."
555 (cond ((not (consp (car list))))
556 ((equal (cdr (car list)) key)
557 (throw 'found (car list))))
558 (setq list (cdr list)))))
560 ;;; @@ Hook manipulation functions.
562 ;; "localhook" package is written for Emacs 19.28 and earlier.
563 ;; `run-hooks' was a lisp function in Emacs 19.29 and earlier.
564 ;; So, in Emacs 19.29, `run-hooks' and others will be overrided.
565 ;; But, who cares it?
566 (static-unless (subrp (symbol-function 'run-hooks))
567 (require 'localhook))
569 ;; Emacs 19.29/XEmacs 19.14(?) and later: (add-to-list LIST-VAR ELEMENT)
570 (defun-maybe add-to-list (list-var element)
571 "Add to the value of LIST-VAR the element ELEMENT if it isn't there yet.
572 The test for presence of ELEMENT is done with `equal'.
573 If you want to use `add-to-list' on a variable that is not defined
574 until a certain package is loaded, you should put the call to `add-to-list'
575 into a hook function that will be run only after loading the package.
576 `eval-after-load' provides one way to do this. In some cases
577 other hooks, such as major mode hooks, can do the job."
578 (or (member element (symbol-value list-var))
579 (set list-var (cons element (symbol-value list-var)))))
581 ;; (eval-after-load FILE FORM)
582 ;; Emacs 19.28 and earlier do not evaluate FORM if FILE is already loaded.
583 ;; XEmacs 20.2 and earlier have `after-load-alist', but refuse to support
584 ;; `eval-after-load'. (see comments in XEmacs/lisp/subr.el.)
587 ;; for XEmacs 20.2 and earlier.
588 (defun-maybe eval-after-load (file form)
589 "Arrange that, if FILE is ever loaded, FORM will be run at that time.
590 This makes or adds to an entry on `after-load-alist'.
591 If FILE is already loaded, evaluate FORM right now.
592 It does nothing if FORM is already on the list for FILE.
593 FILE should be the name of a library, with no directory name."
594 ;; Make sure there is an element for FILE.
595 (or (assoc file after-load-alist)
596 (setq after-load-alist (cons (list file) after-load-alist)))
597 ;; Add FORM to the element if it isn't there.
598 (let ((elt (assoc file after-load-alist)))
599 (or (member form (cdr elt))
601 (nconc elt (list form))
602 ;; If the file has been loaded already, run FORM right away.
603 (and (assoc file load-history)
606 ((>= emacs-major-version 20))
607 ((and (= emacs-major-version 19)
608 (< emacs-minor-version 29))
609 ;; for Emacs 19.28 and earlier.
610 (defun eval-after-load (file form)
611 "Arrange that, if FILE is ever loaded, FORM will be run at that time.
612 This makes or adds to an entry on `after-load-alist'.
613 If FILE is already loaded, evaluate FORM right now.
614 It does nothing if FORM is already on the list for FILE.
615 FILE should be the name of a library, with no directory name."
616 ;; Make sure there is an element for FILE.
617 (or (assoc file after-load-alist)
618 (setq after-load-alist (cons (list file) after-load-alist)))
619 ;; Add FORM to the element if it isn't there.
620 (let ((elt (assoc file after-load-alist)))
621 (or (member form (cdr elt))
623 (nconc elt (list form))
624 ;; If the file has been loaded already, run FORM right away.
625 (and (assoc file load-history)
629 ;; should emulate for v18?
632 (defun-maybe eval-next-after-load (file)
633 "Read the following input sexp, and run it whenever FILE is loaded.
634 This makes or adds to an entry on `after-load-alist'.
635 FILE should be the name of a library, with no directory name."
636 (eval-after-load file (read)))
638 ;;; @@ Input and display facilities.
640 ;; XXX: (defun read-passwd (prompt &optional confirm default))
644 ;; Avoid compiler warnings about this variable,
645 ;; which has a special meaning on certain system types.
646 (defvar-maybe buffer-file-type nil
647 "Non-nil if the visited file is a binary file.
648 This variable is meaningful on MS-DOG and Windows NT.
649 On those systems, it is automatically local in every buffer.
650 On other systems, this variable is normally always nil.")
652 ;; Emacs 20.1/XEmacs 20.3(?) and later: (save-current-buffer &rest BODY)
654 ;; v20 defines `save-current-buffer' as a C primitive (in src/editfns.c)
655 ;; and introduces a new bytecode Bsave_current_buffer(_1), replacing an
656 ;; obsolete bytecode Bread_char. To make things worse, Emacs 20.1 and
657 ;; 20.2 have a bug that it will restore the current buffer without
658 ;; confirming that it is alive.
660 ;; This is a source of incompatibility of .elc between v18/v19 and v20.
661 ;; (XEmacs compiler takes care of it if compatibility mode is enabled.)
662 (defmacro-maybe save-current-buffer (&rest body)
663 "Save the current buffer; execute BODY; restore the current buffer.
664 Executes BODY just like `progn'."
665 (` (let ((orig-buffer (current-buffer)))
668 (if (buffer-live-p orig-buffer)
669 (set-buffer orig-buffer))))))
671 ;; Emacs 20.1/XEmacs 20.3(?) and later: (with-current-buffer BUFFER &rest BODY)
672 (defmacro-maybe with-current-buffer (buffer &rest body)
673 "Execute the forms in BODY with BUFFER as the current buffer.
674 The value returned is the value of the last form in BODY.
675 See also `with-temp-buffer'."
676 (` (save-current-buffer
677 (set-buffer (, buffer))
680 ;; Emacs 20.1/XEmacs 20.3(?) and later: (with-temp-file FILE &rest FORMS)
681 (defmacro-maybe with-temp-file (file &rest forms)
682 "Create a new buffer, evaluate FORMS there, and write the buffer to FILE.
683 The value of the last form in FORMS is returned, like `progn'.
684 See also `with-temp-buffer'."
685 (let ((temp-file (make-symbol "temp-file"))
686 (temp-buffer (make-symbol "temp-buffer")))
687 (` (let (((, temp-file) (, file))
689 (get-buffer-create (generate-new-buffer-name " *temp file*"))))
692 (with-current-buffer (, temp-buffer)
694 (with-current-buffer (, temp-buffer)
696 (write-region (point-min) (point-max) (, temp-file) nil 0)))
697 (and (buffer-name (, temp-buffer))
698 (kill-buffer (, temp-buffer))))))))
700 ;; Emacs 20.4 and later: (with-temp-message MESSAGE &rest BODY)
701 ;; This macro uses `current-message', which appears in v20.
702 (static-when (and (fboundp 'current-message)
703 (subrp (symbol-function 'current-message)))
704 (defmacro-maybe with-temp-message (message &rest body)
706 Display MESSAGE temporarily if non-nil while BODY is evaluated.
707 The original message is restored to the echo area after BODY has finished.
708 The value returned is the value of the last form in BODY.
709 MESSAGE is written to the message log buffer if `message-log-max' is non-nil.
710 If MESSAGE is nil, the echo area and message log buffer are unchanged.
711 Use a MESSAGE of \"\" to temporarily clear the echo area."
712 (let ((current-message (make-symbol "current-message"))
713 (temp-message (make-symbol "with-temp-message")))
714 (` (let (((, temp-message) (, message))
715 ((, current-message)))
718 (when (, temp-message)
719 (setq (, current-message) (current-message))
720 (message "%s" (, temp-message))
722 (and (, temp-message) (, current-message)
723 (message "%s" (, current-message))))))))))
725 ;; Emacs 20.1/XEmacs 20.3(?) and later: (with-temp-buffer &rest FORMS)
726 (defmacro-maybe with-temp-buffer (&rest forms)
727 "Create a temporary buffer, and evaluate FORMS there like `progn'.
728 See also `with-temp-file' and `with-output-to-string'."
729 (let ((temp-buffer (make-symbol "temp-buffer")))
730 (` (let (((, temp-buffer)
731 (get-buffer-create (generate-new-buffer-name " *temp*"))))
733 (with-current-buffer (, temp-buffer)
735 (and (buffer-name (, temp-buffer))
736 (kill-buffer (, temp-buffer))))))))
738 ;; Emacs 20.1/XEmacs 20.3(?) and later: (with-output-to-string &rest BODY)
739 (defmacro-maybe with-output-to-string (&rest body)
740 "Execute BODY, return the text it sent to `standard-output', as a string."
741 (` (let ((standard-output
742 (get-buffer-create (generate-new-buffer-name " *string-output*"))))
743 (let ((standard-output standard-output))
745 (with-current-buffer standard-output
748 (kill-buffer nil))))))
750 ;; Emacs 20.1 and later: (combine-after-change-calls &rest BODY)
751 (defmacro-maybe combine-after-change-calls (&rest body)
752 "Execute BODY, but don't call the after-change functions till the end.
753 If BODY makes changes in the buffer, they are recorded
754 and the functions on `after-change-functions' are called several times
755 when BODY is finished.
756 The return value is the value of the last form in BODY.
758 If `before-change-functions' is non-nil, then calls to the after-change
759 functions can't be deferred, so in that case this macro has no effect.
761 Do not alter `after-change-functions' or `before-change-functions'
764 This emulating macro does not support after-change functions at all,
768 ;; Emacs 19.29/XEmacs 19.14(?) and later: (match-string NUM &optional STRING)
769 (defun-maybe match-string (num &optional string)
770 "Return string of text matched by last search.
771 NUM specifies which parenthesized expression in the last regexp.
772 Value is nil if NUMth pair didn't match, or there were less than NUM pairs.
773 Zero means the entire text matched by the whole regexp or whole string.
774 STRING should be given if the last search was by `string-match' on STRING."
775 (if (match-beginning num)
777 (substring string (match-beginning num) (match-end num))
778 (buffer-substring (match-beginning num) (match-end num)))))
780 ;; Emacs 20.3 and later: (match-string-no-properties NUM &optional STRING)
781 (defun-maybe match-string-no-properties (num &optional string)
782 "Return string of text matched by last search, without text properties.
783 NUM specifies which parenthesized expression in the last regexp.
784 Value is nil if NUMth pair didn't match, or there were less than NUM pairs.
785 Zero means the entire text matched by the whole regexp or whole string.
786 STRING should be given if the last search was by `string-match' on STRING."
787 (if (match-beginning num)
790 (substring string (match-beginning num) (match-end num))))
791 (set-text-properties 0 (length result) nil result)
793 (buffer-substring-no-properties (match-beginning num)
796 ;; Emacs 20.1/XEmacs 20.3(?) and later: (split-string STRING &optional PATTERN)
797 ;; Here is a XEmacs version.
798 (defun-maybe split-string (string &optional pattern)
799 "Return a list of substrings of STRING which are separated by PATTERN.
800 If PATTERN is omitted, it defaults to \"[ \\f\\t\\n\\r\\v]+\"."
802 (setq pattern "[ \f\t\n\r\v]+"))
803 ;; The FSF version of this function takes care not to cons in case
804 ;; of infloop. Maybe we should synch?
805 (let (parts (start 0))
806 (while (string-match pattern string start)
807 (setq parts (cons (substring string start (match-beginning 0)) parts)
808 start (match-end 0)))
809 (nreverse (cons (substring string start) parts))))
811 ;; Emacs 20.1/XEmacs 20.3 (but first appeared in Epoch?): (functionp OBJECT)
812 (defun-maybe functionp (object)
813 "Non-nil if OBJECT is a type of object that can be called as a function."
814 (or (subrp object) (byte-code-function-p object)
815 (eq (car-safe object) 'lambda)
816 (and (symbolp object) (fboundp object))))
819 ;;; @ Window commands emulation. (lisp/window.el)
822 (defmacro-maybe save-selected-window (&rest body)
823 "Execute BODY, then select the window that was selected before BODY."
825 '((save-selected-window-window (selected-window)))
826 (list 'unwind-protect
828 (list 'select-window 'save-selected-window-window))))
831 ;;; @ Basic editing commands emulation. (lisp/simple.el)
835 ;;; @ File input and output commands emulation. (lisp/files.el)
838 (defvar-maybe temporary-file-directory
839 (file-name-as-directory
840 (cond ((memq system-type '(ms-dos windows-nt))
841 (or (getenv "TEMP") (getenv "TMPDIR") (getenv "TMP") "c:/temp"))
842 ((memq system-type '(vax-vms axp-vms))
843 (or (getenv "TMPDIR") (getenv "TMP") (getenv "TEMP") "SYS$SCRATCH:"))
845 (or (getenv "TMPDIR") (getenv "TMP") (getenv "TEMP") "/tmp"))))
846 "The directory for writing temporary files.")
848 ;; Actually, `path-separator' is defined in src/emacs.c and overrided
850 (defvar-maybe path-separator ":"
851 "The directory separator in search paths, as a string.")
853 ;; `convert-standard-filename' is defined in lisp/files.el and overrided
854 ;; in lisp/dos-fns.el and lisp/w32-fns.el for each environment.
856 ;; must be load-time check to share .elc between different systems.
857 ((fboundp 'convert-standard-filename))
858 ((memq system-type '(windows-nt ms-dos))
859 ;; should we do (require 'filename) at load-time ?
860 ;; (require 'filename)
861 ;; filename.el requires many modules, so we do not want to load it
862 ;; at compile-time. Instead, suppress warnings by these autoloads.
864 (autoload 'filename-maybe-truncate-by-size "filename")
865 (autoload 'filename-special-filter "filename"))
866 (defun convert-standard-filename (filename)
867 "Convert a standard file's name to something suitable for the current OS.
868 This function's standard definition is trivial; it just returns the argument.
869 However, on some systems, the function is redefined
870 with a definition that really does change some file names.
871 Under `windows-nt' or `ms-dos', it refers `filename-replacement-alist' and
872 `filename-limit-length' for the basic filename and each parent directory name."
874 (let* ((names (split-string filename "/"))
875 (drive-name (car names))
878 (filename-maybe-truncate-by-size
879 (filename-special-filter string))))))
881 ((eq 1 (length names))
882 (funcall filter drive-name))
883 ((string-match "^[^/]:$" drive-name)
884 (concat drive-name "/" (mapconcat filter (cdr names) "/")))
886 (mapconcat filter names "/"))))))
888 (defun convert-standard-filename (filename)
889 "Convert a standard file's name to something suitable for the current OS.
890 This function's standard definition is trivial; it just returns the argument.
891 However, on some systems, the function is redefined
892 with a definition that really does change some file names.
893 Under `windows-nt' or `ms-dos', it refers `filename-replacement-alist' and
894 `filename-limit-length' for the basic filename and each parent directory name."
898 ((fboundp 'insert-file-contents-literally))
899 ((boundp 'file-name-handler-alist)
900 ;; Use `defun-maybe' to update `load-history'.
901 (defun-maybe insert-file-contents-literally (filename &optional visit
903 "Like `insert-file-contents', q.v., but only reads in the file.
904 A buffer may be modified in several ways after reading into the buffer due
905 to advanced Emacs features, such as file-name-handlers, format decoding,
906 find-file-hooks, etc.
907 This function ensures that none of these modifications will take place."
908 (let (file-name-handler-alist)
909 (insert-file-contents filename visit beg end replace))))
911 (defalias 'insert-file-contents-literally 'insert-file-contents)))
913 (defun-maybe file-name-sans-extension (filename)
914 "Return FILENAME sans final \"extension\".
915 The extension, in a file name, is the part that follows the last `.'."
917 (let ((file (file-name-sans-versions (file-name-nondirectory filename)))
919 (if (string-match "\\.[^.]*\\'" file)
920 (if (setq directory (file-name-directory filename))
921 (expand-file-name (substring file 0 (match-beginning 0))
923 (substring file 0 (match-beginning 0)))
927 ;;; @ XEmacs emulation.
930 (defun-maybe find-face (face-or-name)
931 "Retrieve the face of the given name.
932 If FACE-OR-NAME is a face object, it is simply returned.
933 Otherwise, FACE-OR-NAME should be a symbol. If there is no such face,
934 nil is returned. Otherwise the associated face object is returned."
935 (car (memq face-or-name (face-list))))
937 ;; Emacs 21.1 defines this as an alias for `line-beginning-position'.
938 ;; Therefore, optional 2nd arg BUFFER is not portable.
939 (defun-maybe point-at-bol (&optional n buffer)
940 "Return the character position of the first character on the current line.
941 With argument N not nil or 1, move forward N - 1 lines first.
942 If scan reaches end of buffer, return that position.
943 This function does not move point."
945 (if buffer (set-buffer buffer))
946 (forward-line (1- (or n 1)))
949 ;; Emacs 21.1 defines this as an alias for `line-end-position'.
950 ;; Therefore, optional 2nd arg BUFFER is not portable.
951 (defun-maybe point-at-eol (&optional n buffer)
952 "Return the character position of the last character on the current line.
953 With argument N not nil or 1, move forward N - 1 lines first.
954 If scan reaches end of buffer, return that position.
955 This function does not move point."
957 (if buffer (set-buffer buffer))
958 (end-of-line (or n 1))
961 (defsubst-maybe define-obsolete-function-alias (oldfun newfun)
962 "Define OLDFUN as an obsolete alias for function NEWFUN.
963 This makes calling OLDFUN equivalent to calling NEWFUN and marks OLDFUN
965 (defalias oldfun newfun)
966 (make-obsolete oldfun newfun))
968 ;; XEmacs 21: (character-to-event CH &optional EVENT DEVICE)
969 (defun-maybe character-to-event (ch)
970 "Convert keystroke CH into an event structure, replete with bucky bits.
971 Note that CH (the keystroke specifier) can be an integer, a character
972 or a symbol such as 'clear."
975 ;; XEmacs 21: (event-to-character EVENT
976 ;; &optional ALLOW-EXTRA-MODIFIERS ALLOW-META ALLOW-NON-ASCII)
977 (defun-maybe-cond event-to-character (event)
978 "Return the character approximation to the given event object.
979 If the event isn't a keypress, this returns nil."
980 ((and (fboundp 'read-event)
981 (subrp (symbol-function 'read-event)))
982 ;; Emacs 19 and later.
985 ;; mask is (BASE-TYPE MODIFIER-BITS) or nil.
986 (let ((mask (get event 'event-symbol-element-mask)))
988 (let ((base (get (car mask) 'ascii-character)))
990 (logior base (car (cdr mask))))))))
991 ((integerp event) event)))
993 ;; v18. Is this correct?
996 ;; v18: no event; (read-char)
997 ;; Emacs 19, 20.1 and 20.2: (read-event)
998 ;; Emacs 20.3: (read-event &optional PROMPT SUPPRESS-INPUT-METHOD)
999 ;; Emacs 20.4: (read-event &optional PROMPT INHERIT-INPUT-METHOD)
1000 ;; XEmacs: (next-event &optional EVENT PROMPT),
1001 ;; (next-command-event &optional EVENT PROMPT)
1002 (defun-maybe-cond next-command-event (&optional event prompt)
1003 "Read an event object from the input stream.
1004 If EVENT is non-nil, it should be an event object and will be filled
1005 in and returned; otherwise a new event object will be created and
1007 If PROMPT is non-nil, it should be a string and will be displayed in
1008 the echo area while this function is waiting for an event."
1009 ((and (>= emacs-major-version 20)
1010 (>= emacs-minor-version 4))
1011 ;; Emacs 20.4 and later.
1012 (read-event prompt)) ; should specify 2nd arg?
1013 ((and (= emacs-major-version 20)
1014 (= emacs-minor-version 3))
1016 (read-event prompt)) ; should specify 2nd arg?
1017 ((and (fboundp 'read-event)
1018 (subrp (symbol-function 'read-event)))
1019 ;; Emacs 19, 20.1 and 20.2.
1020 (if prompt (message prompt))
1023 (if prompt (message prompt))
1027 ;;; @ MULE 2 emulation.
1030 (defun-maybe-cond cancel-undo-boundary ()
1031 "Cancel undo boundary."
1032 ((boundp 'buffer-undo-list)
1033 ;; for Emacs 19 and later.
1034 (if (and (consp buffer-undo-list)
1035 (null (car buffer-undo-list)))
1036 (setq buffer-undo-list (cdr buffer-undo-list)))))
1042 ;;; poe.el ends here