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