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