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