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