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