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