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