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