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