1 ;;; pym.el --- Macros for Your Poe.
3 ;; Copyright (C) 1995,1996,1997,1998,1999 Free Software Foundation, Inc.
5 ;; Author: MORIOKA Tomohiko <tomo@m17n.org>
6 ;; Shuhei KOBAYASHI <shuhei@aqua.ocn.ne.jp>
7 ;; Keywords: byte-compile, evaluation, edebug, internal
9 ;; This file is part of APEL (A Portable Emacs Library).
11 ;; This program is free software; you can redistribute it and/or
12 ;; modify it under the terms of the GNU General Public License as
13 ;; published by the Free Software Foundation; either version 2, or (at
14 ;; your option) any later version.
16 ;; This program is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ;; General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
28 ;; This module provides `def*-maybe' macros for conditional definition.
30 ;; Many APEL modules use these macros to provide emulation version of
31 ;; Emacs builtins (both C primitives and lisp subroutines) for backward
32 ;; compatibility. While compilation time, if `def*-maybe' find that
33 ;; functions/variables being defined is already provided by Emacs used
34 ;; for compilation, it does not leave the definitions in compiled code
35 ;; and resulting .elc will be highly specialized for your environment.
37 ;; For `find-function' lovers, the following definitions may work with
40 ;; (setq find-function-regexp
41 ;; "^\\s-*(def[^cgvW]\\(\\w\\|-\\)+\\*?\\s-+'?%s\\(\\s-\\|$\\)")
42 ;; (setq find-variable-regexp
43 ;; "^\\s-*(def[^umaW]\\(\\w\\|-\\)+\\*?\\s-+%s\\(\\s-\\|$\\)")
45 ;; I'm too lazy to write better regexps, sorry. -- shuhei
49 ;; for `load-history'.
50 (or (boundp 'current-load-list) (setq current-load-list nil))
55 ;;; Conditional define.
57 (put 'defun-maybe 'lisp-indent-function 'defun)
58 (defmacro defun-maybe (name &rest everything-else)
59 "Define NAME as a function if NAME is not defined.
60 See also the function `defun'."
61 (or (and (fboundp name)
62 (not (get name 'defun-maybe)))
63 (` (or (fboundp (quote (, name)))
65 (defun (, name) (,@ everything-else))
66 ;; This `defun' will be compiled to `fset',
67 ;; which does not update `load-history'.
68 ;; We must update `current-load-list' explicitly.
69 (setq current-load-list
70 (cons (quote (, name)) current-load-list))
71 (put (quote (, name)) 'defun-maybe t))))))
73 (put 'defmacro-maybe 'lisp-indent-function 'defun)
74 (defmacro defmacro-maybe (name &rest everything-else)
75 "Define NAME as a macro if NAME is not defined.
76 See also the function `defmacro'."
77 (or (and (fboundp name)
78 (not (get name 'defmacro-maybe)))
79 (` (or (fboundp (quote (, name)))
81 (defmacro (, name) (,@ everything-else))
82 ;; This `defmacro' will be compiled to `fset',
83 ;; which does not update `load-history'.
84 ;; We must update `current-load-list' explicitly.
85 (setq current-load-list
86 (cons (quote (, name)) current-load-list))
87 (put (quote (, name)) 'defmacro-maybe t))))))
89 (put 'defsubst-maybe 'lisp-indent-function 'defun)
90 (defmacro defsubst-maybe (name &rest everything-else)
91 "Define NAME as an inline function if NAME is not defined.
92 See also the macro `defsubst'."
93 (or (and (fboundp name)
94 (not (get name 'defsubst-maybe)))
95 (` (or (fboundp (quote (, name)))
97 (defsubst (, name) (,@ everything-else))
98 ;; This `defsubst' will be compiled to `fset',
99 ;; which does not update `load-history'.
100 ;; We must update `current-load-list' explicitly.
101 (setq current-load-list
102 (cons (quote (, name)) current-load-list))
103 (put (quote (, name)) 'defsubst-maybe t))))))
105 (defmacro defalias-maybe (symbol definition)
106 "Define SYMBOL as an alias for DEFINITION if SYMBOL is not defined.
107 See also the function `defalias'."
108 (setq symbol (eval symbol))
109 (or (and (fboundp symbol)
110 (not (get symbol 'defalias-maybe)))
111 (` (or (fboundp (quote (, symbol)))
113 (defalias (quote (, symbol)) (, definition))
114 ;; `defalias' updates `load-history' internally.
115 (put (quote (, symbol)) 'defalias-maybe t))))))
117 (defmacro defvar-maybe (name &rest everything-else)
118 "Define NAME as a variable if NAME is not defined.
119 See also the function `defvar'."
120 (or (and (boundp name)
121 (not (get name 'defvar-maybe)))
122 (` (or (boundp (quote (, name)))
124 (defvar (, name) (,@ everything-else))
125 ;; byte-compiler will generate code to update
127 (put (quote (, name)) 'defvar-maybe t))))))
129 (defmacro defconst-maybe (name &rest everything-else)
130 "Define NAME as a constant variable if NAME is not defined.
131 See also the function `defconst'."
132 (or (and (boundp name)
133 (not (get name 'defconst-maybe)))
134 (` (or (boundp (quote (, name)))
136 (defconst (, name) (,@ everything-else))
137 ;; byte-compiler will generate code to update
139 (put (quote (, name)) 'defconst-maybe t))))))
141 (defmacro defun-maybe-cond (name args &optional doc &rest clauses)
142 "Define NAME as a function if NAME is not defined.
143 CLAUSES are like those of `cond' expression, but each condition is evaluated
144 at compile-time and, if the value is non-nil, the body of the clause is used
145 for function definition of NAME.
146 See also the function `defun'."
148 (setq clauses (cons doc clauses)
150 (or (and (fboundp name)
151 (not (get name 'defun-maybe)))
152 (` (or (fboundp (quote (, name)))
160 (` (defun (, name) (, args)
163 (` (defun (, name) (, args)
164 (,@ (cdr case))))))))
166 ;; This `defun' will be compiled to `fset',
167 ;; which does not update `load-history'.
168 ;; We must update `current-load-list' explicitly.
169 (setq current-load-list
170 (cons (quote (, name)) current-load-list))
171 (put (quote (, name)) 'defun-maybe t))))))
173 (defmacro defmacro-maybe-cond (name args &optional doc &rest clauses)
174 "Define NAME as a macro if NAME is not defined.
175 CLAUSES are like those of `cond' expression, but each condition is evaluated
176 at compile-time and, if the value is non-nil, the body of the clause is used
177 for macro definition of NAME.
178 See also the function `defmacro'."
180 (setq clauses (cons doc clauses)
182 (or (and (fboundp name)
183 (not (get name 'defmacro-maybe)))
184 (` (or (fboundp (quote (, name)))
192 (` (defmacro (, name) (, args)
195 (` (defmacro (, name) (, args)
196 (,@ (cdr case))))))))
198 ;; This `defmacro' will be compiled to `fset',
199 ;; which does not update `load-history'.
200 ;; We must update `current-load-list' explicitly.
201 (setq current-load-list
202 (cons (quote (, name)) current-load-list))
203 (put (quote (, name)) 'defmacro-maybe t))))))
205 (defmacro defsubst-maybe-cond (name args &optional doc &rest clauses)
206 "Define NAME as an inline function if NAME is not defined.
207 CLAUSES are like those of `cond' expression, but each condition is evaluated
208 at compile-time and, if the value is non-nil, the body of the clause is used
209 for function definition of NAME.
210 See also the macro `defsubst'."
212 (setq clauses (cons doc clauses)
214 (or (and (fboundp name)
215 (not (get name 'defsubst-maybe)))
216 (` (or (fboundp (quote (, name)))
224 (` (defsubst (, name) (, args)
227 (` (defsubst (, name) (, args)
228 (,@ (cdr case))))))))
230 ;; This `defsubst' will be compiled to `fset',
231 ;; which does not update `load-history'.
232 ;; We must update `current-load-list' explicitly.
233 (setq current-load-list
234 (cons (quote (, name)) current-load-list))
235 (put (quote (, name)) 'defsubst-maybe t))))))
240 ;; `def-edebug-spec' is an autoloaded macro in v19 and later.
241 ;; (Note that recent XEmacs provides "edebug" as a separate package.)
242 (defmacro-maybe def-edebug-spec (symbol spec)
243 "Set the edebug-form-spec property of SYMBOL according to SPEC.
244 Both SYMBOL and SPEC are unevaluated. The SPEC can be 0, t, a symbol
245 \(naming a function\), or a list."
246 (` (put (quote (, symbol)) 'edebug-form-spec (quote (, spec)))))
248 ;; edebug-spec for `def*-maybe' macros.
249 (def-edebug-spec defun-maybe defun)
250 (def-edebug-spec defmacro-maybe defmacro)
251 (def-edebug-spec defsubst-maybe defun)
252 (def-edebug-spec defun-maybe-cond
253 (&define name lambda-list
255 [&rest ([¬ eval] [&rest sexp])]
256 [&optional (eval [&optional ("interactive" interactive)] def-body)]
258 (def-edebug-spec defmacro-maybe-cond
259 (&define name lambda-list
260 [&rest ([¬ eval] [&rest sexp])]
261 [&optional (eval def-body)]
263 (def-edebug-spec defsubst-maybe-cond
264 (&define name lambda-list
266 [&rest ([¬ eval] [&rest sexp])]
267 [&optional (eval [&optional ("interactive" interactive)] def-body)]
270 ;; edebug-spec for `static-*' macros are also defined here.
271 (def-edebug-spec static-if t)
272 (def-edebug-spec static-when when)
273 (def-edebug-spec static-unless unless)
274 (def-edebug-spec static-condition-case condition-case)
275 (def-edebug-spec static-defconst defconst)
276 (def-edebug-spec static-cond cond)
279 ;;; for backward compatibility.
281 (defun subr-fboundp (symbol)
282 "Return t if SYMBOL's function definition is a built-in function."
283 (and (fboundp symbol)
284 (subrp (symbol-function symbol))))
285 ;; (make-obsolete 'subr-fboundp "don't use it.")
291 (product-provide (provide 'pym) (require 'apel-ver))