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