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