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