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