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.
31 (product-provide (provide 'poe) (require 'apel-ver))
36 ;;; @ Version information.
39 (static-when (= emacs-major-version 18)
42 ;; Some ancient version of XEmacs did not provide 'xemacs.
43 (static-when (string-match "XEmacs" emacs-version)
46 ;; `file-coding' was appeared in the spring of 1998, just before XEmacs
47 ;; 21.0. Therefore it is not provided in XEmacs with MULE versions 20.4
49 (static-when (featurep 'xemacs)
50 ;; must be load-time check to share .elc between w/ MULE and w/o MULE.
51 (when (featurep 'mule)
52 (provide 'file-coding)))
54 (static-when (featurep 'xemacs)
55 (require 'poe-xemacs))
57 ;; must be load-time check to share .elc between different systems.
58 (or (fboundp 'open-network-stream)
62 ;;; @ C primitives emulation.
65 ;; Emacs 20.3 and earlier: (require FEATURE &optional FILENAME)
66 ;; Emacs 20.4 and later: (require FEATURE &optional FILENAME NOERROR)
67 (static-condition-case nil
68 ;; compile-time check.
70 (require 'nofeature "nofile" 'noerror)
71 (if (get 'require 'defun-maybe)
72 (error "`require' is already redefined")))
75 (or (fboundp 'si:require)
77 (fset 'si:require (symbol-function 'require))
78 (put 'require 'defun-maybe t)
79 (defun require (feature &optional filename noerror)
81 If feature FEATURE is not loaded, load it from FILENAME.
82 If FEATURE is not a member of the list `features', then the feature
83 is not loaded; so load the file FILENAME.
84 If FILENAME is omitted, the printname of FEATURE is used as the file name,
85 but in this case `load' insists on adding the suffix `.el' or `.elc'.
86 If the optional third argument NOERROR is non-nil,
87 then return nil if the file is not found.
88 Normally the return value is FEATURE."
91 (si:require feature filename)
93 (si:require feature filename)))))))
95 ;; Emacs 19.29 and later: (plist-get PLIST PROP)
96 ;; (defun-maybe plist-get (plist prop)
98 ;; (not (eq (car plist) prop)))
99 ;; (setq plist (cdr (cdr plist))))
100 ;; (car (cdr plist)))
101 (static-unless (and (fboundp 'plist-get)
102 (not (get 'plist-get 'defun-maybe)))
103 (or (fboundp 'plist-get)
105 (defvar plist-get-internal-symbol)
106 (defun plist-get (plist prop)
108 Extract a value from a property list.
109 PLIST is a property list, which is a list of the form
110 \(PROP1 VALUE1 PROP2 VALUE2...\). This function returns the value
111 corresponding to the given PROP, or nil if PROP is not
112 one of the properties on the list."
113 (setplist 'plist-get-internal-symbol plist)
114 (get 'plist-get-internal-symbol prop))
115 ;; for `load-history'.
116 (setq current-load-list (cons 'plist-get current-load-list))
117 (put 'plist-get 'defun-maybe t))))
119 ;; Emacs 19.29 and later: (plist-put PLIST PROP VAL)
120 ;; (defun-maybe plist-put (plist prop val)
122 ;; (let ((tail plist)
124 ;; (while (and tail (cdr tail))
125 ;; (if (eq (car tail) prop)
127 ;; (setcar (cdr tail) val)
128 ;; (throw 'found plist))
130 ;; tail (cdr (cdr tail)))))
133 ;; (setcdr (cdr prev) (list prop val))
135 ;; (list prop val)))))
136 (static-unless (and (fboundp 'plist-put)
137 (not (get 'plist-put 'defun-maybe)))
138 (or (fboundp 'plist-put)
140 (defvar plist-put-internal-symbol)
141 (defun plist-put (plist prop val)
143 Change value in PLIST of PROP to VAL.
144 PLIST is a property list, which is a list of the form
145 \(PROP1 VALUE1 PROP2 VALUE2 ...\). PROP is a symbol and VAL is any object.
146 If PROP is already a property on the list, its value is set to VAL,
147 otherwise the new PROP VAL pair is added. The new plist is returned;
148 use `\(setq x \(plist-put x prop val\)\)' to be sure to use the new value.
149 The PLIST is modified by side effects."
150 (setplist 'plist-put-internal-symbol plist)
151 (put 'plist-put-internal-symbol prop val)
152 (symbol-plist 'plist-put-internal-symbol))
153 ;; for `load-history'.
154 (setq current-load-list (cons 'plist-put current-load-list))
155 (put 'plist-put 'defun-maybe t))))
157 ;; Emacs 19.23 and later: (minibuffer-prompt-width)
158 (defun-maybe minibuffer-prompt-width ()
159 "Return the display width of the minibuffer prompt."
161 (set-buffer (window-buffer (minibuffer-window)))
164 ;; (read-string PROMPT &optional INITIAL-INPUT HISTORY)
165 ;; Emacs 19.29/XEmacs 19.14(?) and later takes optional 3rd arg HISTORY.
166 (static-unless (or (featurep 'xemacs)
167 (>= emacs-major-version 20)
168 (and (= emacs-major-version 19)
169 (>= emacs-minor-version 29)))
170 (or (fboundp 'si:read-string)
172 (fset 'si:read-string (symbol-function 'read-string))
173 (defun read-string (prompt &optional initial-input history)
175 Read a string from the minibuffer, prompting with string PROMPT.
176 If non-nil, second arg INITIAL-INPUT is a string to insert before reading.
177 The third arg HISTORY, is dummy for compatibility.
178 See `read-from-minibuffer' for details of HISTORY argument."
179 (si:read-string prompt initial-input)))))
181 ;; v18: (string-to-int STRING)
182 ;; v19: (string-to-number STRING)
183 ;; v20: (string-to-number STRING &optional BASE)
185 ;; XXX: `string-to-number' of Emacs 20.3 and earlier is broken.
186 ;; (string-to-number "1e1" 16) => 10.0, should be 481.
187 (static-condition-case nil
188 ;; compile-time check.
189 (if (= (string-to-number "1e1" 16) 481)
190 (if (get 'string-to-number 'defun-maybe)
191 (error "`string-to-number' is already redefined"))
192 (error "`string-to-number' is broken"))
195 (or (fboundp 'si:string-to-number)
197 (if (fboundp 'string-to-number)
198 (fset 'si:string-to-number (symbol-function 'string-to-number))
199 (fset 'si:string-to-number (symbol-function 'string-to-int))
200 ;; XXX: In v18, this causes infinite loop while bytecompiling.
201 ;; (defalias 'string-to-int 'string-to-number)
203 (put 'string-to-number 'defun-maybe t)
204 (defun string-to-number (string &optional base)
206 Convert STRING to a number by parsing it as a decimal number.
207 This parses both integers and floating point numbers.
208 It ignores leading spaces and tabs.
210 If BASE, interpret STRING as a number in that base. If BASE isn't
211 present, base 10 is used. BASE must be between 2 and 16 (inclusive).
212 If the base used is not 10, floating point is not recognized."
213 (if (or (null base) (= base 10))
214 (si:string-to-number string)
215 (if (or (< base 2)(> base 16))
216 (signal 'args-out-of-range (cons base nil)))
217 (let ((len (length string))
219 ;; skip leading whitespace.
220 (while (and (< pos len)
221 (memq (aref string pos) '(?\ ?\t)))
225 (let ((number 0)(negative 1)
227 (if (eq (aref string pos) ?-)
230 (if (eq (aref string pos) ?+)
231 (setq pos (1+ pos))))
232 (while (and (< pos len)
233 (setq chr (aref string pos)
235 ((and (<= ?0 chr)(<= chr ?9))
237 ((and (<= ?A chr)(<= chr ?F))
239 ((and (<= ?a chr)(<= chr ?f))
243 (setq number (+ (* number base) num)
245 (* negative number))))))))))
247 ;; Emacs 20.1 and 20.2: (concat-chars &rest CHARS)
248 ;; Emacs 20.3/XEmacs 21.0 and later: (string &rest CHARS)
250 ((and (fboundp 'string)
251 (subrp (symbol-function 'string)))
252 ;; Emacs 20.3/XEmacs 21.0 and later.
254 ((and (fboundp 'concat-chars)
255 (subrp (symbol-function 'concat-chars)))
256 ;; Emacs 20.1 and 20.2.
257 (defalias 'string 'concat-chars))
259 ;; Use `defun-maybe' to update `load-history'.
260 (defun-maybe string (&rest chars)
261 "Concatenate all the argument characters and make the result a string."
262 ;; We cannot use (apply 'concat chars) here because `concat' does not
263 ;; work with multibyte chars on Mule 1.* and 2.*.
264 (mapconcat (function char-to-string) chars ""))))
266 ;; Mule: (char-before POS)
267 ;; v20: (char-before &optional POS)
268 (static-condition-case nil
269 ;; compile-time check.
272 (if (get 'char-before 'defun-maybe)
273 (error "`char-before' is already defined")))
274 (wrong-number-of-arguments ; Mule.
276 (or (fboundp 'si:char-before)
278 (fset 'si:char-before (symbol-function 'char-before))
279 (put 'char-before 'defun-maybe t)
280 ;; takes IGNORED for backward compatibility.
281 (defun char-before (&optional pos ignored)
283 Return character in current buffer preceding position POS.
284 POS is an integer or a buffer pointer.
285 If POS is out of range, the value is nil."
286 (si:char-before (or pos (point)))))))
287 (void-function ; non-Mule.
289 (defun-maybe char-before (&optional pos)
291 Return character in current buffer preceding position POS.
292 POS is an integer or a buffer pointer.
293 If POS is out of range, the value is nil."
296 (and (= (goto-char pos) (point))
301 (error ; found our definition at compile-time.
305 (wrong-number-of-arguments ; Mule.
306 (or (fboundp 'si:char-before)
308 (fset 'si:char-before (symbol-function 'char-before))
309 (put 'char-before 'defun-maybe t)
310 ;; takes IGNORED for backward compatibility.
311 (defun char-before (&optional pos ignored)
313 Return character in current buffer preceding position POS.
314 POS is an integer or a buffer pointer.
315 If POS is out of range, the value is nil."
316 (si:char-before (or pos (point)))))))
317 (void-function ; non-Mule.
318 (defun-maybe char-before (&optional pos)
320 Return character in current buffer preceding position POS.
321 POS is an integer or a buffer pointer.
322 If POS is out of range, the value is nil."
325 (and (= (goto-char pos) (point))
329 (preceding-char))))))))
331 ;; v18, v19: (char-after POS)
332 ;; v20: (char-after &optional POS)
333 (static-condition-case nil
334 ;; compile-time check.
337 (if (get 'char-after 'defun-maybe)
338 (error "`char-after' is already redefined")))
339 (wrong-number-of-arguments ; v18, v19
341 (or (fboundp 'si:char-after)
343 (fset 'si:char-after (symbol-function 'char-after))
344 (put 'char-after 'defun-maybe t)
345 (defun char-after (&optional pos)
347 Return character in current buffer at position POS.
348 POS is an integer or a buffer pointer.
349 If POS is out of range, the value is nil."
350 (si:char-after (or pos (point)))))))
351 (void-function ; NEVER happen?
353 (defun-maybe char-after (&optional pos)
355 Return character in current buffer at position POS.
356 POS is an integer or a buffer pointer.
357 If POS is out of range, the value is nil."
360 (and (= (goto-char pos) (point))
365 (error ; found our definition at compile-time.
369 (wrong-number-of-arguments ; v18, v19
370 (or (fboundp 'si:char-after)
372 (fset 'si:char-after (symbol-function 'char-after))
373 (put 'char-after 'defun-maybe t)
374 (defun char-after (&optional pos)
376 Return character in current buffer at position POS.
377 POS is an integer or a buffer pointer.
378 If POS is out of range, the value is nil."
379 (si:char-after (or pos (point)))))))
380 (void-function ; NEVER happen?
381 (defun-maybe char-after (&optional pos)
383 Return character in current buffer at position POS.
384 POS is an integer or a buffer pointer.
385 If POS is out of range, the value is nil."
388 (and (= (goto-char pos) (point))
392 (following-char))))))))
394 ;; Emacs 19.29 and later: (buffer-substring-no-properties START END)
395 (defun-maybe buffer-substring-no-properties (start end)
396 "Return the characters of part of the buffer, without the text properties.
397 The two arguments START and END are character positions;
398 they can be in either order."
399 (let ((string (buffer-substring start end)))
400 (set-text-properties 0 (length string) nil string)
403 ;; Emacs 19.31 and later: (buffer-live-p OBJECT)
404 (defun-maybe buffer-live-p (object)
405 "Return non-nil if OBJECT is a buffer which has not been killed.
406 Value is nil if OBJECT is not a buffer or if it has been killed."
409 (buffer-name (get-buffer object))
412 ;; Emacs 20: (line-beginning-position &optional N)
413 (defun-maybe line-beginning-position (&optional n)
414 "Return the character position of the first character on the current line.
415 With argument N not nil or 1, move forward N - 1 lines first.
416 If scan reaches end of buffer, return that position.
417 This function does not move point."
419 (forward-line (1- (or n 1)))
422 ;; Emacs 20: (line-end-position &optional N)
423 (defun-maybe line-end-position (&optional n)
424 "Return the character position of the last character on the current line.
425 With argument N not nil or 1, move forward N - 1 lines first.
426 If scan reaches end of buffer, return that position.
427 This function does not move point."
429 (end-of-line (or n 1))
433 ;;; @ Basic lisp subroutines emulation. (lisp/subr.el)
436 ;;; @@ Lisp language features.
438 (defmacro-maybe push (newelt listname)
439 "Add NEWELT to the list stored in the symbol LISTNAME.
440 This is equivalent to (setq LISTNAME (cons NEWELT LISTNAME)).
441 LISTNAME must be a symbol."
443 (list 'cons newelt listname)))
445 (defmacro-maybe pop (listname)
446 "Return the first element of LISTNAME's value, and remove it from the list.
447 LISTNAME must be a symbol whose value is a list.
448 If the value is nil, `pop' returns nil but does not actually
450 (list 'prog1 (list 'car listname)
451 (list 'setq listname (list 'cdr listname))))
453 (defmacro-maybe when (cond &rest body)
454 "If COND yields non-nil, do BODY, else return nil."
455 (list 'if cond (cons 'progn body)))
456 ;; (def-edebug-spec when (&rest form))
458 (defmacro-maybe unless (cond &rest body)
459 "If COND yields nil, do BODY, else return nil."
460 (cons 'if (cons cond (cons nil body))))
461 ;; (def-edebug-spec unless (&rest form))
463 (defsubst-maybe caar (x)
464 "Return the car of the car of X."
467 (defsubst-maybe cadr (x)
468 "Return the car of the cdr of X."
471 (defsubst-maybe cdar (x)
472 "Return the cdr of the car of X."
475 (defsubst-maybe cddr (x)
476 "Return the cdr of the cdr of X."
479 (defun-maybe last (x &optional n)
480 "Return the last link of the list X. Its car is the last element.
481 If X is nil, return nil.
482 If N is non-nil, return the Nth-to-last link of X.
483 If N is bigger than the length of X, return X."
487 (setq m (1+ m) p (cdr p)))
489 (if (< n m) (nthcdr (- m n) x) x)))
494 ;; Actually, `butlast' and `nbutlast' are defined in lisp/cl.el.
495 (defun-maybe butlast (x &optional n)
496 "Returns a copy of LIST with the last N elements removed."
497 (if (and n (<= n 0)) x
498 (nbutlast (copy-sequence x) n)))
500 (defun-maybe nbutlast (x &optional n)
501 "Modifies LIST to remove the last N elements."
502 (let ((m (length x)))
506 (if (> n 0) (setcdr (nthcdr (- (1- m) n) x) nil))
509 ;; Emacs 20.3 and later: (assoc-default KEY ALIST &optional TEST DEFAULT)
510 (defun-maybe assoc-default (key alist &optional test default)
511 "Find object KEY in a pseudo-alist ALIST.
512 ALIST is a list of conses or objects. Each element (or the element's car,
513 if it is a cons) is compared with KEY by evaluating (TEST (car elt) KEY).
514 If that is non-nil, the element matches;
515 then `assoc-default' returns the element's cdr, if it is a cons,
516 or DEFAULT if the element is not a cons.
518 If no element matches, the value is nil.
519 If TEST is omitted or nil, `equal' is used."
520 (let (found (tail alist) value)
521 (while (and tail (not found))
522 (let ((elt (car tail)))
523 (when (funcall (or test 'equal) (if (consp elt) (car elt) elt) key)
524 (setq found t value (if (consp elt) (cdr elt) default))))
525 (setq tail (cdr tail)))
528 ;; The following two function use `compare-strings', which we don't
530 ;; (defun assoc-ignore-case (key alist))
531 ;; (defun assoc-ignore-representation (key alist))
533 ;; Emacs 19.29/XEmacs 19.14(?) and later: (rassoc KEY LIST)
534 ;; Actually, `rassoc' is defined in src/fns.c.
535 (defun-maybe rassoc (key list)
536 "Return non-nil if KEY is `equal' to the cdr of an element of LIST.
537 The value is actually the element of LIST whose cdr equals KEY.
538 Elements of LIST that are not conses are ignored."
541 (cond ((not (consp (car list))))
542 ((equal (cdr (car list)) key)
543 (throw 'found (car list))))
544 (setq list (cdr list)))))
546 ;;; Define `functionp' here because "localhook" uses it.
548 ;; Emacs 20.1/XEmacs 20.3 (but first appeared in Epoch?): (functionp OBJECT)
549 (defun-maybe functionp (object)
550 "Non-nil if OBJECT is a type of object that can be called as a function."
551 (or (subrp object) (byte-code-function-p object)
552 (eq (car-safe object) 'lambda)
553 (and (symbolp object) (fboundp object))))
555 ;;; @@ Hook manipulation functions.
557 ;; "localhook" package is written for Emacs 19.28 and earlier.
558 ;; `run-hooks' was a lisp function in Emacs 19.29 and earlier.
559 ;; So, in Emacs 19.29, `run-hooks' and others will be overrided.
560 ;; But, who cares it?
561 (static-unless (subrp (symbol-function 'run-hooks))
562 (require 'localhook))
564 ;; Emacs 19.29/XEmacs 19.14(?) and later: (add-to-list LIST-VAR ELEMENT)
565 (defun-maybe add-to-list (list-var element)
566 "Add to the value of LIST-VAR the element ELEMENT if it isn't there yet.
567 The test for presence of ELEMENT is done with `equal'.
568 If you want to use `add-to-list' on a variable that is not defined
569 until a certain package is loaded, you should put the call to `add-to-list'
570 into a hook function that will be run only after loading the package.
571 `eval-after-load' provides one way to do this. In some cases
572 other hooks, such as major mode hooks, can do the job."
573 (or (member element (symbol-value list-var))
574 (set list-var (cons element (symbol-value list-var)))))
576 ;; (eval-after-load FILE FORM)
577 ;; Emacs 19.28 and earlier do not evaluate FORM if FILE is already loaded.
578 ;; XEmacs 20.2 and earlier have `after-load-alist', but refuse to support
579 ;; `eval-after-load'. (see comments in XEmacs/lisp/subr.el.)
582 ;; for XEmacs 20.2 and earlier.
583 (defun-maybe eval-after-load (file form)
584 "Arrange that, if FILE is ever loaded, FORM will be run at that time.
585 This makes or adds to an entry on `after-load-alist'.
586 If FILE is already loaded, evaluate FORM right now.
587 It does nothing if FORM is already on the list for FILE.
588 FILE should be the name of a library, with no directory name."
589 ;; Make sure there is an element for FILE.
590 (or (assoc file after-load-alist)
591 (setq after-load-alist (cons (list file) after-load-alist)))
592 ;; Add FORM to the element if it isn't there.
593 (let ((elt (assoc file after-load-alist)))
594 (or (member form (cdr elt))
596 (nconc elt (list form))
597 ;; If the file has been loaded already, run FORM right away.
598 (and (assoc file load-history)
601 ((>= emacs-major-version 20))
602 ((and (= emacs-major-version 19)
603 (< emacs-minor-version 29))
604 ;; for Emacs 19.28 and earlier.
605 (defun eval-after-load (file form)
606 "Arrange that, if FILE is ever loaded, FORM will be run at that time.
607 This makes or adds to an entry on `after-load-alist'.
608 If FILE is already loaded, evaluate FORM right now.
609 It does nothing if FORM is already on the list for FILE.
610 FILE should be the name of a library, with no directory name."
611 ;; Make sure there is an element for FILE.
612 (or (assoc file after-load-alist)
613 (setq after-load-alist (cons (list file) after-load-alist)))
614 ;; Add FORM to the element if it isn't there.
615 (let ((elt (assoc file after-load-alist)))
616 (or (member form (cdr elt))
618 (nconc elt (list form))
619 ;; If the file has been loaded already, run FORM right away.
620 (and (assoc file load-history)
624 ;; should emulate for v18?
627 (defun-maybe eval-next-after-load (file)
628 "Read the following input sexp, and run it whenever FILE is loaded.
629 This makes or adds to an entry on `after-load-alist'.
630 FILE should be the name of a library, with no directory name."
631 (eval-after-load file (read)))
633 ;;; @@ Input and display facilities.
635 ;; XXX: (defun read-passwd (prompt &optional confirm default))
639 ;; Avoid compiler warnings about this variable,
640 ;; which has a special meaning on certain system types.
641 (defvar-maybe buffer-file-type nil
642 "Non-nil if the visited file is a binary file.
643 This variable is meaningful on MS-DOG and Windows NT.
644 On those systems, it is automatically local in every buffer.
645 On other systems, this variable is normally always nil.")
647 ;; Emacs 20.1/XEmacs 20.3(?) and later: (save-current-buffer &rest BODY)
649 ;; v20 defines `save-current-buffer' as a C primitive (in src/editfns.c)
650 ;; and introduces a new bytecode Bsave_current_buffer(_1), replacing an
651 ;; obsolete bytecode Bread_char. To make things worse, Emacs 20.1 and
652 ;; 20.2 have a bug that it will restore the current buffer without
653 ;; confirming that it is alive.
655 ;; This is a source of incompatibility of .elc between v18/v19 and v20.
656 ;; (XEmacs compiler takes care of it if compatibility mode is enabled.)
657 (defmacro-maybe save-current-buffer (&rest body)
658 "Save the current buffer; execute BODY; restore the current buffer.
659 Executes BODY just like `progn'."
660 (` (let ((orig-buffer (current-buffer)))
663 (if (buffer-live-p orig-buffer)
664 (set-buffer orig-buffer))))))
666 ;; Emacs 20.1/XEmacs 20.3(?) and later: (with-current-buffer BUFFER &rest BODY)
667 (defmacro-maybe with-current-buffer (buffer &rest body)
668 "Execute the forms in BODY with BUFFER as the current buffer.
669 The value returned is the value of the last form in BODY.
670 See also `with-temp-buffer'."
671 (` (save-current-buffer
672 (set-buffer (, buffer))
675 ;; Emacs 20.1/XEmacs 20.3(?) and later: (with-temp-file FILE &rest FORMS)
676 (defmacro-maybe with-temp-file (file &rest forms)
677 "Create a new buffer, evaluate FORMS there, and write the buffer to FILE.
678 The value of the last form in FORMS is returned, like `progn'.
679 See also `with-temp-buffer'."
680 (let ((temp-file (make-symbol "temp-file"))
681 (temp-buffer (make-symbol "temp-buffer")))
682 (` (let (((, temp-file) (, file))
684 (get-buffer-create (generate-new-buffer-name " *temp file*"))))
687 (with-current-buffer (, temp-buffer)
689 (with-current-buffer (, temp-buffer)
691 (write-region (point-min) (point-max) (, temp-file) nil 0)))
692 (and (buffer-name (, temp-buffer))
693 (kill-buffer (, temp-buffer))))))))
695 ;; Emacs 20.4 and later: (with-temp-message MESSAGE &rest BODY)
696 ;; This macro uses `current-message', which appears in v20.
697 (static-when (and (fboundp 'current-message)
698 (subrp (symbol-function 'current-message)))
699 (defmacro-maybe with-temp-message (message &rest body)
701 Display MESSAGE temporarily if non-nil while BODY is evaluated.
702 The original message is restored to the echo area after BODY has finished.
703 The value returned is the value of the last form in BODY.
704 MESSAGE is written to the message log buffer if `message-log-max' is non-nil.
705 If MESSAGE is nil, the echo area and message log buffer are unchanged.
706 Use a MESSAGE of \"\" to temporarily clear the echo area."
707 (let ((current-message (make-symbol "current-message"))
708 (temp-message (make-symbol "with-temp-message")))
709 (` (let (((, temp-message) (, message))
710 ((, current-message)))
713 (when (, temp-message)
714 (setq (, current-message) (current-message))
715 (message "%s" (, temp-message))
717 (and (, temp-message) (, current-message)
718 (message "%s" (, current-message))))))))))
720 ;; Emacs 20.1/XEmacs 20.3(?) and later: (with-temp-buffer &rest FORMS)
721 (defmacro-maybe with-temp-buffer (&rest forms)
722 "Create a temporary buffer, and evaluate FORMS there like `progn'.
723 See also `with-temp-file' and `with-output-to-string'."
724 (let ((temp-buffer (make-symbol "temp-buffer")))
725 (` (let (((, temp-buffer)
726 (get-buffer-create (generate-new-buffer-name " *temp*"))))
728 (with-current-buffer (, temp-buffer)
730 (and (buffer-name (, temp-buffer))
731 (kill-buffer (, temp-buffer))))))))
733 ;; Emacs 20.1/XEmacs 20.3(?) and later: (with-output-to-string &rest BODY)
734 (defmacro-maybe with-output-to-string (&rest body)
735 "Execute BODY, return the text it sent to `standard-output', as a string."
736 (` (let ((standard-output
737 (get-buffer-create (generate-new-buffer-name " *string-output*"))))
738 (let ((standard-output standard-output))
740 (with-current-buffer standard-output
743 (kill-buffer nil))))))
745 ;; Emacs 20.1 and later: (combine-after-change-calls &rest BODY)
746 (defmacro-maybe combine-after-change-calls (&rest body)
747 "Execute BODY, but don't call the after-change functions till the end.
748 If BODY makes changes in the buffer, they are recorded
749 and the functions on `after-change-functions' are called several times
750 when BODY is finished.
751 The return value is the value of the last form in BODY.
753 If `before-change-functions' is non-nil, then calls to the after-change
754 functions can't be deferred, so in that case this macro has no effect.
756 Do not alter `after-change-functions' or `before-change-functions'
759 This emulating macro does not support after-change functions at all,
763 ;; Emacs 19.29/XEmacs 19.14(?) and later: (match-string NUM &optional STRING)
764 (defun-maybe match-string (num &optional string)
765 "Return string of text matched by last search.
766 NUM specifies which parenthesized expression in the last regexp.
767 Value is nil if NUMth pair didn't match, or there were less than NUM pairs.
768 Zero means the entire text matched by the whole regexp or whole string.
769 STRING should be given if the last search was by `string-match' on STRING."
770 (if (match-beginning num)
772 (substring string (match-beginning num) (match-end num))
773 (buffer-substring (match-beginning num) (match-end num)))))
775 ;; Emacs 20.3 and later: (match-string-no-properties NUM &optional STRING)
776 (defun-maybe match-string-no-properties (num &optional string)
777 "Return string of text matched by last search, without text properties.
778 NUM specifies which parenthesized expression in the last regexp.
779 Value is nil if NUMth pair didn't match, or there were less than NUM pairs.
780 Zero means the entire text matched by the whole regexp or whole string.
781 STRING should be given if the last search was by `string-match' on STRING."
782 (if (match-beginning num)
785 (substring string (match-beginning num) (match-end num))))
786 (set-text-properties 0 (length result) nil result)
788 (buffer-substring-no-properties (match-beginning num)
791 ;; Emacs 20.1/XEmacs 20.3(?) and later: (split-string STRING &optional PATTERN)
792 ;; Here is a XEmacs version.
793 (defun-maybe split-string (string &optional pattern)
794 "Return a list of substrings of STRING which are separated by PATTERN.
795 If PATTERN is omitted, it defaults to \"[ \\f\\t\\n\\r\\v]+\"."
797 (setq pattern "[ \f\t\n\r\v]+"))
798 ;; The FSF version of this function takes care not to cons in case
799 ;; of infloop. Maybe we should synch?
800 (let (parts (start 0))
801 (while (string-match pattern string start)
802 (setq parts (cons (substring string start (match-beginning 0)) parts)
803 start (match-end 0)))
804 (nreverse (cons (substring string start) parts))))
807 ;;; @ Window commands emulation. (lisp/window.el)
810 (defmacro-maybe save-selected-window (&rest body)
811 "Execute BODY, then select the window that was selected before BODY."
813 '((save-selected-window-window (selected-window)))
814 (list 'unwind-protect
816 (list 'select-window 'save-selected-window-window))))
819 ;;; @ Basic editing commands emulation. (lisp/simple.el)
823 ;;; @ File input and output commands emulation. (lisp/files.el)
826 (defvar-maybe temporary-file-directory
827 (file-name-as-directory
828 (cond ((memq system-type '(ms-dos windows-nt))
829 (or (getenv "TEMP") (getenv "TMPDIR") (getenv "TMP") "c:/temp"))
830 ((memq system-type '(vax-vms axp-vms))
831 (or (getenv "TMPDIR") (getenv "TMP") (getenv "TEMP") "SYS$SCRATCH:"))
833 (or (getenv "TMPDIR") (getenv "TMP") (getenv "TEMP") "/tmp"))))
834 "The directory for writing temporary files.")
836 ;; Actually, `path-separator' is defined in src/emacs.c and overrided
838 (defvar-maybe path-separator ":"
839 "The directory separator in search paths, as a string.")
841 ;; `convert-standard-filename' is defined in lisp/files.el and overrided
842 ;; in lisp/dos-fns.el and lisp/w32-fns.el for each environment.
844 ;; must be load-time check to share .elc between different systems.
845 ((fboundp 'convert-standard-filename))
846 ((memq system-type '(windows-nt ms-dos))
847 ;; should we do (require 'filename) at load-time ?
848 ;; (require 'filename)
849 ;; filename.el requires many modules, so we do not want to load it
850 ;; at compile-time. Instead, suppress warnings by these autoloads.
852 (autoload 'filename-maybe-truncate-by-size "filename")
853 (autoload 'filename-special-filter "filename"))
854 (defun convert-standard-filename (filename)
855 "Convert a standard file's name to something suitable for the current OS.
856 This function's standard definition is trivial; it just returns the argument.
857 However, on some systems, the function is redefined
858 with a definition that really does change some file names.
859 Under `windows-nt' or `ms-dos', it refers `filename-replacement-alist' and
860 `filename-limit-length' for the basic filename and each parent directory name."
862 (let* ((names (split-string filename "/"))
863 (drive-name (car names))
866 (filename-maybe-truncate-by-size
867 (filename-special-filter string))))))
869 ((eq 1 (length names))
870 (funcall filter drive-name))
871 ((string-match "^[^/]:$" drive-name)
872 (concat drive-name "/" (mapconcat filter (cdr names) "/")))
874 (mapconcat filter names "/"))))))
876 (defun convert-standard-filename (filename)
877 "Convert a standard file's name to something suitable for the current OS.
878 This function's standard definition is trivial; it just returns the argument.
879 However, on some systems, the function is redefined
880 with a definition that really does change some file names.
881 Under `windows-nt' or `ms-dos', it refers `filename-replacement-alist' and
882 `filename-limit-length' for the basic filename and each parent directory name."
886 ((fboundp 'insert-file-contents-literally))
887 ((boundp 'file-name-handler-alist)
888 ;; Use `defun-maybe' to update `load-history'.
889 (defun-maybe insert-file-contents-literally (filename &optional visit
891 "Like `insert-file-contents', q.v., but only reads in the file.
892 A buffer may be modified in several ways after reading into the buffer due
893 to advanced Emacs features, such as file-name-handlers, format decoding,
894 find-file-hooks, etc.
895 This function ensures that none of these modifications will take place."
896 (let (file-name-handler-alist)
897 (insert-file-contents filename visit beg end replace))))
899 (defalias 'insert-file-contents-literally 'insert-file-contents)))
901 (defun-maybe file-name-sans-extension (filename)
902 "Return FILENAME sans final \"extension\".
903 The extension, in a file name, is the part that follows the last `.'."
905 (let ((file (file-name-sans-versions (file-name-nondirectory filename)))
907 (if (string-match "\\.[^.]*\\'" file)
908 (if (setq directory (file-name-directory filename))
909 (expand-file-name (substring file 0 (match-beginning 0))
911 (substring file 0 (match-beginning 0)))
915 ;;; @ XEmacs emulation.
918 (defun-maybe find-face (face-or-name)
919 "Retrieve the face of the given name.
920 If FACE-OR-NAME is a face object, it is simply returned.
921 Otherwise, FACE-OR-NAME should be a symbol. If there is no such face,
922 nil is returned. Otherwise the associated face object is returned."
923 (car (memq face-or-name (face-list))))
925 ;; Emacs 21.1 defines this as an alias for `line-beginning-position'.
926 ;; Therefore, optional 2nd arg BUFFER is not portable.
927 (defun-maybe point-at-bol (&optional n buffer)
928 "Return the character position of the first character on the current line.
929 With argument N not nil or 1, move forward N - 1 lines first.
930 If scan reaches end of buffer, return that position.
931 This function does not move point."
933 (if buffer (set-buffer buffer))
934 (forward-line (1- (or n 1)))
937 ;; Emacs 21.1 defines this as an alias for `line-end-position'.
938 ;; Therefore, optional 2nd arg BUFFER is not portable.
939 (defun-maybe point-at-eol (&optional n buffer)
940 "Return the character position of the last 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 (end-of-line (or n 1))
949 (defsubst-maybe define-obsolete-function-alias (oldfun newfun)
950 "Define OLDFUN as an obsolete alias for function NEWFUN.
951 This makes calling OLDFUN equivalent to calling NEWFUN and marks OLDFUN
953 (defalias oldfun newfun)
954 (make-obsolete oldfun newfun))
956 ;; XEmacs 21: (character-to-event CH &optional EVENT DEVICE)
957 (defun-maybe character-to-event (ch)
958 "Convert keystroke CH into an event structure, replete with bucky bits.
959 Note that CH (the keystroke specifier) can be an integer, a character
960 or a symbol such as 'clear."
963 ;; XEmacs 21: (event-to-character EVENT
964 ;; &optional ALLOW-EXTRA-MODIFIERS ALLOW-META ALLOW-NON-ASCII)
965 (defun-maybe-cond event-to-character (event)
966 "Return the character approximation to the given event object.
967 If the event isn't a keypress, this returns nil."
968 ((and (fboundp 'read-event)
969 (subrp (symbol-function 'read-event)))
970 ;; Emacs 19 and later.
973 ;; mask is (BASE-TYPE MODIFIER-BITS) or nil.
974 (let ((mask (get event 'event-symbol-element-mask)))
976 (let ((base (get (car mask) 'ascii-character)))
978 (logior base (car (cdr mask))))))))
979 ((integerp event) event)))
981 ;; v18. Is this correct?
984 ;; v18: no event; (read-char)
985 ;; Emacs 19, 20.1 and 20.2: (read-event)
986 ;; Emacs 20.3: (read-event &optional PROMPT SUPPRESS-INPUT-METHOD)
987 ;; Emacs 20.4: (read-event &optional PROMPT INHERIT-INPUT-METHOD)
988 ;; XEmacs: (next-event &optional EVENT PROMPT),
989 ;; (next-command-event &optional EVENT PROMPT)
990 (defun-maybe-cond next-command-event (&optional event prompt)
991 "Read an event object from the input stream.
992 If EVENT is non-nil, it should be an event object and will be filled
993 in and returned; otherwise a new event object will be created and
995 If PROMPT is non-nil, it should be a string and will be displayed in
996 the echo area while this function is waiting for an event."
997 ((and (>= emacs-major-version 20)
998 (>= emacs-minor-version 4))
999 ;; Emacs 20.4 and later.
1000 (read-event prompt)) ; should specify 2nd arg?
1001 ((and (= emacs-major-version 20)
1002 (= emacs-minor-version 3))
1004 (read-event prompt)) ; should specify 2nd arg?
1005 ((and (fboundp 'read-event)
1006 (subrp (symbol-function 'read-event)))
1007 ;; Emacs 19, 20.1 and 20.2.
1008 (if prompt (message prompt))
1011 (if prompt (message prompt))
1015 ;;; @ MULE 2 emulation.
1018 (defun-maybe-cond cancel-undo-boundary ()
1019 "Cancel undo boundary."
1020 ((boundp 'buffer-undo-list)
1021 ;; for Emacs 19 and later.
1022 (if (and (consp buffer-undo-list)
1023 (null (car buffer-undo-list)))
1024 (setq buffer-undo-list (cdr buffer-undo-list)))))
1030 ;;; poe.el ends here