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