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