(frame-background-mode): New variable.
[elisp/apel.git] / poe.el
1 ;;; poe.el --- Portable Outfit for Emacsen; -*-byte-compile-dynamic: t;-*-
2
3 ;; Copyright (C) 1995,1996,1997,1998,1999 Free Software Foundation, Inc.
4
5 ;; Author: MORIOKA Tomohiko <tomo@m17n.org>
6 ;; Keywords: emulation, compatibility, NEmacs, MULE, Emacs/mule, XEmacs
7
8 ;; This file is part of APEL (A Portable Emacs Library).
9
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.
14
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.
19
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.
24
25 ;;; Commentary:
26
27 ;; This modules does not includes MULE related features.
28 ;; MULE related features are supported by `poem'.
29
30 ;;; Code:
31
32 (provide 'poe)
33
34 (or (boundp 'current-load-list) (setq current-load-list nil))
35
36 (put 'defun-maybe 'lisp-indent-function 'defun)
37 (defmacro defun-maybe (name &rest everything-else)
38   "Define NAME as a function if NAME is not defined.
39 See also the function `defun'."
40   (or (and (fboundp name)
41            (not (get name 'defun-maybe)))
42       (` (or (fboundp (quote (, name)))
43              (prog1
44                  (defun (, name) (,@ everything-else))
45                ;; This `defun' will be compiled to `fset', which does
46                ;; not update `load-history'.
47                (setq current-load-list
48                      (cons (quote (, name)) current-load-list))
49                (put (quote (, name)) 'defun-maybe t))))))
50
51 (put 'defmacro-maybe 'lisp-indent-function 'defun)
52 (defmacro defmacro-maybe (name &rest everything-else)
53   "Define NAME as a macro if NAME is not defined.
54 See also the function `defmacro'."
55   (or (and (fboundp name)
56            (not (get name 'defmacro-maybe)))
57       (` (or (fboundp (quote (, name)))
58              (prog1
59                  (defmacro (, name) (,@ everything-else))
60                (setq current-load-list
61                      (cons (quote (, name)) current-load-list))
62                (put (quote (, name)) 'defmacro-maybe t))))))
63
64 (put 'defsubst-maybe 'lisp-indent-function 'defun)
65 (defmacro defsubst-maybe (name &rest everything-else)
66   "Define NAME as an inline function if NAME is not defined.
67 See also the macro `defsubst'."
68   (or (and (fboundp name)
69            (not (get name 'defsubst-maybe)))
70       (` (or (fboundp (quote (, name)))
71              (prog1
72                  (defsubst (, name) (,@ everything-else))
73                (setq current-load-list
74                      (cons (quote (, name)) current-load-list))
75                (put (quote (, name)) 'defsubst-maybe t))))))
76
77 (defmacro defalias-maybe (symbol definition)
78   "Define SYMBOL as an alias for DEFINITION if SYMBOL is not defined.
79 See also the function `defalias'."
80   (setq symbol (eval symbol))
81   (or (and (fboundp symbol)
82            (not (get symbol 'defalias-maybe)))
83       (` (or (fboundp (quote (, symbol)))
84              (prog1
85                  (defalias (quote (, symbol)) (, definition))
86                (setq current-load-list
87                      (cons (quote (, symbol)) current-load-list))
88                (put (quote (, symbol)) 'defalias-maybe t))))))
89
90 (defmacro defvar-maybe (name &rest everything-else)
91   "Define NAME as a variable if NAME is not defined.
92 See also the function `defvar'."
93   (or (and (boundp name)
94            (not (get name 'defvar-maybe)))
95       (` (or (boundp (quote (, name)))
96              (prog1
97                  (defvar (, name) (,@ everything-else))
98                ;; byte-compiler will generate code to update
99                ;; `load-history'.
100                (put (quote (, name)) 'defvar-maybe t))))))
101
102 (defmacro defconst-maybe (name &rest everything-else)
103   "Define NAME as a constant variable if NAME is not defined.
104 See also the function `defconst'."
105   (or (and (boundp name)
106            (not (get name 'defconst-maybe)))
107       (` (or (boundp (quote (, name)))
108              (prog1
109                  (defconst (, name) (,@ everything-else))
110                ;; byte-compiler will generate code to update
111                ;; `load-history'.
112                (put (quote (, name)) 'defconst-maybe t))))))
113
114 (defmacro defun-maybe-cond (name args &optional doc &rest everything-else)
115   (or (stringp doc)
116       (setq everything-else (cons doc everything-else)
117             doc nil))
118   (or (and (fboundp name)
119            (not (get name 'defun-maybe)))
120       (` (or (fboundp (quote (, name)))
121              (prog1
122                  (cond
123                   (,@ (mapcar
124                        (function
125                         (lambda (case)
126                           (list (car case)
127                                 (if doc
128                                     (` (defun (, name) (, args)
129                                          (, doc)
130                                          (,@ (cdr case))))
131                                   (` (defun (, name) (, args)
132                                        (,@ (cdr case))))))))
133                        everything-else)))
134                (setq current-load-list
135                      (cons (quote (, name)) current-load-list))
136                (put (quote (, name)) 'defun-maybe t))))))
137
138 (defmacro defmacro-maybe-cond (name args &optional doc &rest everything-else)
139   (or (stringp doc)
140       (setq everything-else (cons doc everything-else)
141             doc nil))
142   (or (and (fboundp name)
143            (not (get name 'defmacro-maybe)))
144       (` (or (fboundp (quote (, name)))
145              (prog1
146                  (cond
147                   (,@ (mapcar
148                        (function
149                         (lambda (case)
150                           (list (car case)
151                                 (if doc
152                                     (` (defmacro (, name) (, args)
153                                          (, doc)
154                                          (,@ (cdr case))))
155                                   (` (defmacro (, name) (, args)
156                                        (,@ (cdr case))))))))
157                        everything-else)))
158                (setq current-load-list
159                      (cons (quote (, name)) current-load-list))
160                (put (quote (, name)) 'defmacro-maybe t))))))
161
162 (defun subr-fboundp (symbol)
163   "Return t if SYMBOL's function definition is a built-in function."
164   (and (fboundp symbol)
165        (subrp (symbol-function symbol))))
166
167 (defconst-maybe emacs-major-version (string-to-int emacs-version))
168 (defconst-maybe emacs-minor-version
169   (string-to-int
170    (substring emacs-version
171               (string-match (format "%d\\." emacs-major-version)
172                             emacs-version))))
173
174 (cond ((featurep 'xemacs)
175        (require 'poe-xemacs)
176        )
177       ((string-match "XEmacs" emacs-version)
178        (provide 'xemacs)
179        (require 'poe-xemacs)
180        )
181       ((> emacs-major-version 20))
182       ((= emacs-major-version 20)
183        (cond ((subr-fboundp 'string)
184               ;; Emacs 20.3 or later
185               )
186              ((subr-fboundp 'concat-chars)
187               ;; Emacs 20.1 or later
188               (defalias 'string 'concat-chars)
189               ))
190        )
191       ((= emacs-major-version 19)
192        ;; XXX: should do compile-time and load-time check before loading
193        ;;      "localhook".  But, it is difficult since "localhook" is
194        ;;      already loaded via "install" at compile-time.  any idea?
195        (if (< emacs-minor-version 29)
196            (require 'localhook)))
197       (t
198        (require 'poe-18)
199        ;; XXX: should do compile-time and load-time check before loading
200        ;;      "localhook".  But, it is difficult since "localhook" is
201        ;;      already loaded via "install" at compile-time.  any idea?
202        (require 'localhook)))
203
204 ;;; `eval-when-compile' is defined in "poe-18" under v18 with old compiler.
205 (eval-when-compile (require 'static))
206
207 ;; imported from emacs-20.3/lisp/emacs-lisp/edebug.el.
208 ;; `def-edebug-spec' is an autoloaded macro in v19 and later.
209 (defmacro-maybe def-edebug-spec (symbol spec)
210   "Set the edebug-form-spec property of SYMBOL according to SPEC.
211 Both SYMBOL and SPEC are unevaluated. The SPEC can be 0, t, a symbol
212 \(naming a function\), or a list."
213   (` (put (quote (, symbol)) 'edebug-form-spec (quote (, spec)))))
214
215 (def-edebug-spec defun-maybe defun)
216 (def-edebug-spec defmacro-maybe defmacro)
217 (def-edebug-spec defsubst-maybe defun)
218
219 ;;; Emacs 20.1 emulation
220
221 ;; imported from emacs-20.3/lisp/subr.el.
222 (defmacro-maybe when (cond &rest body)
223   "If COND yields non-nil, do BODY, else return nil."
224   (list 'if cond (cons 'progn body)))
225 ;; (def-edebug-spec when (&rest form))
226
227 ;; imported from emacs-20.3/lisp/subr.el.
228 (defmacro-maybe unless (cond &rest body)
229   "If COND yields nil, do BODY, else return nil."
230   (cons 'if (cons cond (cons nil body))))
231 ;; (def-edebug-spec unless (&rest form))
232
233 (defvar-maybe frame-background-mode nil
234   "*The brightness of the background.
235 Set this to the symbol dark if your background color is dark, light if
236 your background is light, or nil (default) if you want Emacs to
237 examine the brightness for you.  However, the old Emacsen might not
238 examine the brightness, so you should set this value definitely.")
239
240
241 ;;; @ Emacs 19.23 emulation
242 ;;;
243
244 (defun-maybe minibuffer-prompt-width ()
245   "Return the display width of the minibuffer prompt."
246   (save-excursion
247     (set-buffer (window-buffer (minibuffer-window)))
248     (current-column)))
249
250
251 ;;; @ Emacs 19.29 emulation
252 ;;;
253
254 (defvar-maybe path-separator ":"
255   "The directory separator in search paths, as a string.")
256
257 (defun-maybe buffer-substring-no-properties (start end)
258   "Return the characters of part of the buffer, without the text properties.
259 The two arguments START and END are character positions;
260 they can be in either order.
261 \[Emacs 19.29 emulating function]"
262   (let ((string (buffer-substring start end)))
263     (set-text-properties 0 (length string) nil string)
264     string))
265
266 ;; imported from emacs-19.34/lisp/subr.el.
267 (defun-maybe match-string (num &optional string)
268   "Return string of text matched by last search.
269 NUM specifies which parenthesized expression in the last regexp.
270  Value is nil if NUMth pair didn't match, or there were less than NUM pairs.
271 Zero means the entire text matched by the whole regexp or whole string.
272 STRING should be given if the last search was by `string-match' on STRING.
273 \[Emacs 19.29 emulating function]"
274   (if (match-beginning num)
275       (if string
276           (substring string (match-beginning num) (match-end num))
277         (buffer-substring (match-beginning num) (match-end num)))))
278
279 (static-unless (or (featurep 'xemacs)
280                    (>= emacs-major-version 20)
281                    (and (= emacs-major-version 19)
282                         (>= emacs-minor-version 29)))
283   ;; for Emacs 19.28 or earlier
284   (unless (fboundp 'si:read-string)
285     (fset 'si:read-string (symbol-function 'read-string))
286     (defun read-string (prompt &optional initial-input history)
287       "Read a string from the minibuffer, prompting with string PROMPT.
288 If non-nil, second arg INITIAL-INPUT is a string to insert before reading.
289 The third arg HISTORY, is dummy for compatibility.
290 See `read-from-minibuffer' for details of HISTORY argument."
291       (si:read-string prompt initial-input))
292     ))
293
294 (defun-maybe rassoc (key list)
295   "Return non-nil if KEY is `equal' to the cdr of an element of LIST.
296 The value is actually the element of LIST whose cdr equals KEY.
297 Elements of LIST that are not conses are ignored.
298 \[Emacs 19.29 emulating function]"
299   (catch 'found
300     (while list
301       (cond ((not (consp (car list))))
302             ((equal (cdr (car list)) key)
303              (throw 'found (car list)) ))
304       (setq list (cdr list)) )))
305
306 ;; imported from emacs-19.34/lisp/files.el.
307 (defun-maybe file-name-sans-extension (filename)
308   "Return FILENAME sans final \"extension\".
309 The extension, in a file name, is the part that follows the last `.'.
310 \[Emacs 19.29 emulating function]"
311   (save-match-data
312     (let ((file (file-name-sans-versions (file-name-nondirectory filename)))
313           directory)
314       (if (string-match "\\.[^.]*\\'" file)
315           (if (setq directory (file-name-directory filename))
316               (expand-file-name (substring file 0 (match-beginning 0))
317                                 directory)
318             (substring file 0 (match-beginning 0)))
319         filename))))
320
321
322 ;;; @ Emacs 19.30 emulation
323 ;;;
324
325 ;; imported from emacs-19.34/lisp/subr.el.
326 (defun-maybe add-to-list (list-var element)
327   "Add to the value of LIST-VAR the element ELEMENT if it isn't there yet.
328 The test for presence of ELEMENT is done with `equal'.
329 If you want to use `add-to-list' on a variable that is not defined
330 until a certain package is loaded, you should put the call to `add-to-list'
331 into a hook function that will be run only after loading the package.
332 `eval-after-load' provides one way to do this.  In some cases
333 other hooks, such as major mode hooks, can do the job.
334 \[Emacs 19.30 emulating function]"
335   (or (member element (symbol-value list-var))
336       (set list-var (cons element (symbol-value list-var)))))
337
338 (cond ((fboundp 'insert-file-contents-literally))
339       ((boundp 'file-name-handler-alist)
340        (defun insert-file-contents-literally
341          (filename &optional visit beg end replace)
342          "Like `insert-file-contents', q.v., but only reads in the file.
343 A buffer may be modified in several ways after reading into the buffer due
344 to advanced Emacs features, such as file-name-handlers, format decoding,
345 find-file-hooks, etc.
346   This function ensures that none of these modifications will take place.
347 \[Emacs 19.30 emulating function]"
348          (let (file-name-handler-alist)
349            (insert-file-contents filename visit beg end replace)))
350        )
351       (t
352        (defalias 'insert-file-contents-literally 'insert-file-contents)
353        ))
354
355
356 ;;; @ Emacs 19.31 emulation
357 ;;;
358
359 (defun-maybe buffer-live-p (object)
360   "Return non-nil if OBJECT is a buffer which has not been killed.
361 Value is nil if OBJECT is not a buffer or if it has been killed.
362 \[Emacs 19.31 emulating function]"
363   (and object
364        (get-buffer object)
365        (buffer-name (get-buffer object))
366        t))
367
368 ;; imported from emacs-19.34/lisp/window.el.
369 (defmacro-maybe save-selected-window (&rest body)
370   "Execute BODY, then select the window that was selected before BODY.
371 \[Emacs 19.31 emulating function]"
372   (list 'let
373         '((save-selected-window-window (selected-window)))
374         (list 'unwind-protect
375               (cons 'progn body)
376               (list 'select-window 'save-selected-window-window))))
377
378 (defun-maybe-cond convert-standard-filename (filename)
379   "Convert a standard file's name to something suitable for the current OS.
380 This function's standard definition is trivial; it just returns the argument.
381 However, on some systems, the function is redefined
382 with a definition that really does change some file names.
383 Under `windows-nt' or `ms-dos', it refers `filename-replacement-alist' and
384 `filename-limit-length' for the basic filename and each parent directory name.
385 \[Emacs 19.31 emulating function]"
386   ((memq system-type '(windows-nt ms-dos))
387    (require 'filename)
388    (let* ((names (split-string filename "/"))
389           (drive-name (car names))
390           (filter (function (lambda (string)
391                               (filename-maybe-truncate-by-size
392                                (filename-special-filter string))))))
393      (cond ((eq 1 (length names))
394             (funcall filter drive-name))
395            ((string-match "^[^/]:$" drive-name)
396             (concat drive-name "/" (mapconcat filter (cdr names) "/")))
397            (t (mapconcat filter names "/")))))
398   (t filename))
399
400
401 ;;; @ Emacs 20.1 emulation
402 ;;;
403
404 ;; imported from emacs-20.3/lisp/subr.el.
405 (defsubst-maybe caar (x)
406   "Return the car of the car of X."
407   (car (car x)))
408
409 ;; imported from emacs-20.3/lisp/subr.el.
410 (defsubst-maybe cadr (x)
411   "Return the car of the cdr of X."
412   (car (cdr x)))
413
414 ;; imported from emacs-20.3/lisp/subr.el.
415 (defsubst-maybe cdar (x)
416   "Return the cdr of the car of X."
417   (cdr (car x)))
418
419 ;; imported from emacs-20.3/lisp/subr.el.
420 (defsubst-maybe cddr (x)
421   "Return the cdr of the cdr of X."
422   (cdr (cdr x)))
423
424 ;; imported from emacs-20.3/lisp/subr.el.
425 (defun-maybe last (x &optional n)
426   "Return the last link of the list X.  Its car is the last element.
427 If X is nil, return nil.
428 If N is non-nil, return the Nth-to-last link of X.
429 If N is bigger than the length of X, return X."
430   (if n
431       (let ((m 0) (p x))
432         (while (consp p)
433           (setq m (1+ m) p (cdr p)))
434         (if (<= n 0) p
435           (if (< n m) (nthcdr (- m n) x) x)))
436     (while (cdr x)
437       (setq x (cdr x)))
438     x))
439
440 ;; In Emacs 20.3, save-current-buffer is defined in src/editfns.c.
441 (defmacro-maybe save-current-buffer (&rest body)
442   "Save the current buffer; execute BODY; restore the current buffer.
443 Executes BODY just like `progn'."
444   (` (let ((orig-buffer (current-buffer)))
445        (unwind-protect
446            (progn (,@ body))
447          (if (buffer-live-p orig-buffer)
448              (set-buffer orig-buffer))))))
449
450 ;; imported from emacs-20.3/lisp/subr.el. (with macro style change)
451 (defmacro-maybe with-current-buffer (buffer &rest body)
452   "Execute the forms in BODY with BUFFER as the current buffer.
453 The value returned is the value of the last form in BODY.
454 See also `with-temp-buffer'."
455   (` (save-current-buffer
456        (set-buffer (, buffer))
457        (,@ body))))
458
459 ;; imported from emacs-20.3/lisp/subr.el. (with macro style change)
460 (defmacro-maybe with-temp-file (file &rest forms)
461   "Create a new buffer, evaluate FORMS there, and write the buffer to FILE.
462 The value of the last form in FORMS is returned, like `progn'.
463 See also `with-temp-buffer'."
464   (let ((temp-file (make-symbol "temp-file"))
465         (temp-buffer (make-symbol "temp-buffer")))
466     (` (let (((, temp-file) (, file))
467              ((, temp-buffer)
468               (get-buffer-create (generate-new-buffer-name " *temp file*"))))
469          (unwind-protect
470              (prog1
471                  (with-current-buffer (, temp-buffer)
472                    (,@ forms))
473                (with-current-buffer (, temp-buffer)
474                  (widen)
475                  (write-region (point-min) (point-max) (, temp-file) nil 0)))
476            (and (buffer-name (, temp-buffer))
477                 (kill-buffer (, temp-buffer))))))))
478
479 ;; imported from emacs-20.3/lisp/subr.el. (with macro style change)
480 (defmacro-maybe with-temp-buffer (&rest forms)
481   "Create a temporary buffer, and evaluate FORMS there like `progn'.
482 See also `with-temp-file' and `with-output-to-string'."
483   (let ((temp-buffer (make-symbol "temp-buffer")))
484     (` (let (((, temp-buffer)
485               (get-buffer-create (generate-new-buffer-name " *temp*"))))
486          (unwind-protect
487              (with-current-buffer (, temp-buffer)
488                (,@ forms))
489            (and (buffer-name (, temp-buffer))
490                 (kill-buffer (, temp-buffer))))))))
491
492 (defmacro-maybe combine-after-change-calls (&rest body)
493   "Execute BODY."
494   (cons 'progn body))
495
496 ;; imported from emacs-20.3/lisp/subr.el.
497 (defun-maybe functionp (object)
498   "Non-nil if OBJECT is a type of object that can be called as a function."
499   (or (subrp object) (byte-code-function-p object)
500       (eq (car-safe object) 'lambda)
501       (and (symbolp object) (fboundp object))))
502
503 ;; imported from emacs-20.3/lisp/emacs-lisp/cl.el.
504 (defun-maybe butlast (x &optional n)
505   "Returns a copy of LIST with the last N elements removed."
506   (if (and n (<= n 0)) x
507     (nbutlast (copy-sequence x) n)))
508
509 ;; imported from emacs-20.3/lisp/emacs-lisp/cl.el.
510 (defun-maybe nbutlast (x &optional n)
511   "Modifies LIST to remove the last N elements."
512   (let ((m (length x)))
513     (or n (setq n 1))
514     (and (< n m)
515          (progn
516            (if (> n 0) (setcdr (nthcdr (- (1- m) n) x) nil))
517            x))))
518
519 ;; imported from XEmacs 21.
520 (defun-maybe split-string (string &optional pattern)
521   "Return a list of substrings of STRING which are separated by PATTERN.
522 If PATTERN is omitted, it defaults to \"[ \\f\\t\\n\\r\\v]+\"."
523   (or pattern
524       (setq pattern "[ \f\t\n\r\v]+"))
525   ;; The FSF version of this function takes care not to cons in case
526   ;; of infloop.  Maybe we should synch?
527   (let (parts (start 0))
528     (while (string-match pattern string start)
529       (setq parts (cons (substring string start (match-beginning 0)) parts)
530             start (match-end 0)))
531     (nreverse (cons (substring string start) parts))))
532
533 ;; emulating char-before of Emacs 20.
534 (static-condition-case nil
535     ;; compile-time check.
536     (progn
537       ;; XXX: this file is already loaded at compile-time,
538       ;; so this test will always success.
539       (char-before)
540       ;; If our definition is found at compile-time, signal an error.
541       ;; XXX: should signal more specific error. 
542       (if (get 'char-before 'defun-maybe)
543           (error "")))
544   (wrong-number-of-arguments            ; Mule 1.*, 2.*.
545    ;; load-time check.
546    (or (fboundp 'si:char-before)
547        (progn
548          (fset 'si:char-before (symbol-function 'char-before))
549          (put 'char-before 'defun-maybe t)
550          ;; takes IGNORED for backward compatibility.
551          (defun char-before (&optional pos ignored)
552            "\
553 Return character in current buffer preceding position POS.
554 POS is an integer or a buffer pointer.
555 If POS is out of range, the value is nil."
556            (si:char-before (or pos (point)))))))
557   (void-function                        ; non-Mule.
558    ;; load-time check.
559    (defun-maybe char-before (&optional pos)
560      "\
561 Return character in current buffer preceding position POS.
562 POS is an integer or a buffer pointer.
563 If POS is out of range, the value is nil."
564      (if pos
565          (save-excursion
566            (and (= (goto-char pos) (point))
567                 (not (bobp))
568                 (preceding-char)))
569        (and (not (bobp))
570             (preceding-char)))))
571   (error                                ; found our definition at compile-time.
572    ;; load-time check.
573    (condition-case nil
574        (char-before)
575      (wrong-number-of-arguments         ; Mule 1.*, 2.*.
576       (or (fboundp 'si:char-before)
577           (progn
578             (fset 'si:char-before (symbol-function 'char-before))
579             (put 'char-before 'defun-maybe t)
580             ;; takes IGNORED for backward compatibility.
581             (defun char-before (&optional pos ignored)
582               "\
583 Return character in current buffer preceding position POS.
584 POS is an integer or a buffer pointer.
585 If POS is out of range, the value is nil."
586               (si:char-before (or pos (point)))))))
587      (void-function                     ; non-Mule.
588       (defun-maybe char-before (&optional pos)
589         "\
590 Return character in current buffer preceding position POS.
591 POS is an integer or a buffer pointer.
592 If POS is out of range, the value is nil."
593         (if pos
594             (save-excursion
595               (and (= (goto-char pos) (point))
596                    (not (bobp))
597                    (preceding-char)))
598           (and (not (bobp))
599                (preceding-char))))))))
600
601 ;; emulating char-after of Emacs 20.
602 (static-condition-case nil
603     ;; compile-time check.
604     (progn
605       ;; XXX: this file is already loaded at compile-time,
606       ;; so this test will always success.
607       (char-after)
608       ;; If our definition is found at compile-time, signal an error.
609       ;; XXX: should signal more specific error. 
610       (if (get 'char-after 'defun-maybe)
611           (error "")))
612   (wrong-number-of-arguments            ; v18, v19
613    ;; load-time check.
614    (or (fboundp 'si:char-after)
615        (progn
616          (fset 'si:char-after (symbol-function 'char-after))
617          (put 'char-after 'defun-maybe t)
618          (defun char-after (&optional pos)
619            "\
620 Return character in current buffer at position POS.
621 POS is an integer or a buffer pointer.
622 If POS is out of range, the value is nil."
623            (si:char-after (or pos (point)))))))
624   (void-function                        ; NEVER happen?
625    ;; load-time check.
626    (defun-maybe char-after (&optional pos)
627      "\
628 Return character in current buffer at position POS.
629 POS is an integer or a buffer pointer.
630 If POS is out of range, the value is nil."
631      (if pos
632          (save-excursion
633            (and (= (goto-char pos) (point))
634                 (not (eobp))
635                 (following-char)))
636        (and (not (eobp))
637             (following-char)))))
638   (error                                ; found our definition at compile-time.
639    ;; load-time check.
640    (condition-case nil
641        (char-after)
642      (wrong-number-of-arguments         ; v18, v19
643       (or (fboundp 'si:char-after)
644           (progn
645             (fset 'si:char-after (symbol-function 'char-after))
646             (put 'char-after 'defun-maybe t)
647             (defun char-after (&optional pos)
648               "\
649 Return character in current buffer at position POS.
650 POS is an integer or a buffer pointer.
651 If POS is out of range, the value is nil."
652               (si:char-after (or pos (point)))))))
653      (void-function                     ; NEVER happen?
654       (defun-maybe char-after (&optional pos)
655         "\
656 Return character in current buffer at position POS.
657 POS is an integer or a buffer pointer.
658 If POS is out of range, the value is nil."
659         (if pos
660             (save-excursion
661               (and (= (goto-char pos) (point))
662                    (not (eobp))
663                    (following-char)))
664           (and (not (eobp))
665                (following-char))))))))
666
667
668 ;;; @ Emacs 20.3 emulation
669 ;;;
670
671 ;; imported from emacs-20.3/lisp/files.el.
672 (defvar-maybe temporary-file-directory
673   (file-name-as-directory
674    (cond ((memq system-type '(ms-dos windows-nt))
675           (or (getenv "TEMP") (getenv "TMPDIR") (getenv "TMP") "c:/temp"))
676          ((memq system-type '(vax-vms axp-vms))
677           (or (getenv "TMPDIR") (getenv "TMP") (getenv "TEMP") "SYS$SCRATCH:"))
678          (t
679           (or (getenv "TMPDIR") (getenv "TMP") (getenv "TEMP") "/tmp"))))
680   "The directory for writing temporary files.")
681
682 (defun-maybe line-beginning-position (&optional n)
683   "Return the character position of the first character on the current line.
684 With argument N not nil or 1, move forward N - 1 lines first.
685 If scan reaches end of buffer, return that position.
686 This function does not move point."
687   (save-excursion
688     (forward-line (1- (or n 1)))
689     (point)))
690
691 (defun-maybe line-end-position (&optional n)
692   "Return the character position of the last character on the current line.
693 With argument N not nil or 1, move forward N - 1 lines first.
694 If scan reaches end of buffer, return that position.
695 This function does not move point."
696   (save-excursion
697     (end-of-line (or n 1))
698     (point)))
699
700 (defun-maybe string (&rest chars)
701   "Concatenate all the argument characters and make the result a string."
702   (mapconcat (function char-to-string) chars ""))
703
704     
705 ;;; @ XEmacs emulation
706 ;;;
707
708 (defun-maybe find-face (face-or-name)
709   "Retrieve the face of the given name.
710 If FACE-OR-NAME is a face object, it is simply returned.
711 Otherwise, FACE-OR-NAME should be a symbol.  If there is no such face,
712 nil is returned.  Otherwise the associated face object is returned.
713 \[XEmacs emulating function]"
714   (car (memq face-or-name (face-list))))
715
716 (defun-maybe point-at-bol (&optional n buffer)
717   "Return the character position of the first character on the current line.
718 With argument N not nil or 1, move forward N - 1 lines first.
719 If scan reaches end of buffer, return that position.
720 This function does not move point.
721 \[XEmacs emulating function]"
722   (save-excursion
723     (if buffer (set-buffer buffer))
724     (forward-line (1- (or n 1)))
725     (point)))
726
727 (defun-maybe point-at-eol (&optional n buffer)
728   "Return the character position of the last character on the current line.
729 With argument N not nil or 1, move forward N - 1 lines first.
730 If scan reaches end of buffer, return that position.
731 This function does not move point.
732 \[XEmacs emulating function]"
733   (save-excursion
734     (if buffer (set-buffer buffer))
735     (end-of-line (or n 1))
736     (point)))
737
738 (defsubst-maybe define-obsolete-function-alias (oldfun newfun)
739   "Define OLDFUN as an obsolete alias for function NEWFUN.
740 This makes calling OLDFUN equivalent to calling NEWFUN and marks OLDFUN
741 as obsolete.
742 \[XEmacs emulating function]"
743   (defalias oldfun newfun)
744   (make-obsolete oldfun newfun))
745
746 (when (subr-fboundp 'read-event)
747   ;; for Emacs 19 or later
748
749   (defun-maybe-cond next-command-event (&optional event prompt)
750     "Read an event object from the input stream.
751 If EVENT is non-nil, it should be an event object and will be filled
752 in and returned; otherwise a new event object will be created and
753 returned.
754 If PROMPT is non-nil, it should be a string and will be displayed in
755 the echo area while this function is waiting for an event.
756 \[XEmacs emulating function]"
757     ((subr-fboundp 'string)
758      ;; for Emacs 20.3 or later
759      (read-event prompt t)
760      )
761     (t
762      (if prompt (message prompt))
763      (read-event)
764      ))
765
766   (defsubst-maybe character-to-event (ch)
767     "Convert keystroke CH into an event structure, replete with bucky bits.
768 Note that CH (the keystroke specifier) can be an integer, a character
769 or a symbol such as 'clear. [XEmacs emulating function]"
770     ch)
771
772   (defsubst-maybe event-to-character (event)
773     "Return the character approximation to the given event object.
774 If the event isn't a keypress, this returns nil.
775 \[XEmacs emulating function]"
776     (cond ((symbolp event)
777            ;; mask is (BASE-TYPE MODIFIER-BITS) or nil.
778            (let ((mask (get event 'event-symbol-element-mask)))
779              (if mask
780                  (let ((base (get (car mask) 'ascii-character)))
781                    (if base
782                        (logior base (car (cdr mask)))
783                      )))))
784           ((integerp event) event)))
785   )
786
787
788 ;;; @ MULE 2 emulation
789 ;;;
790
791 (defun-maybe-cond cancel-undo-boundary ()
792   "Cancel undo boundary. [MULE 2.3 emulating function]"
793   ((boundp 'buffer-undo-list)
794    ;; for Emacs 19.7 or later
795    (if (and (consp buffer-undo-list)
796             ;; if car is nil.
797             (null (car buffer-undo-list)))
798        (setq buffer-undo-list (cdr buffer-undo-list))
799      ))
800   (t
801    ;; for anything older than Emacs 19.7.    
802    ))
803
804
805 ;;; @ end
806 ;;;
807
808 ;;; poe.el ends here