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