diff options
| -rw-r--r-- | doc/lispref/variables.texi | 9 | ||||
| -rw-r--r-- | lisp/emacs-lisp/bytecomp.el | 23 | ||||
| -rw-r--r-- | src/eval.c | 78 |
3 files changed, 68 insertions, 42 deletions
diff --git a/doc/lispref/variables.texi b/doc/lispref/variables.texi index f0e3f337a69..c29547d00db 100644 --- a/doc/lispref/variables.texi +++ b/doc/lispref/variables.texi | |||
| @@ -527,10 +527,11 @@ If @var{symbol} has a buffer-local binding in the current buffer, | |||
| 527 | rather than the buffer-local binding. It sets the default value if | 527 | rather than the buffer-local binding. It sets the default value if |
| 528 | the default value is void. @xref{Buffer-Local Variables}. | 528 | the default value is void. @xref{Buffer-Local Variables}. |
| 529 | 529 | ||
| 530 | If @var{symbol} is already lexically bound (e.g., if the @code{defvar} | 530 | If @var{symbol} is already let bound (e.g., if the @code{defvar} |
| 531 | form occurs in a @code{let} form with lexical binding enabled), then | 531 | form occurs in a @code{let} form), then @code{defvar} sets the toplevel |
| 532 | @code{defvar} sets the dynamic value. The lexical binding remains in | 532 | default value, like @code{set-default-toplevel-value}. |
| 533 | effect until its binding construct exits. @xref{Variable Scoping}. | 533 | The let binding remains in effect until its binding construct exits. |
| 534 | @xref{Variable Scoping}. | ||
| 534 | 535 | ||
| 535 | @cindex @code{eval-defun}, and @code{defvar} forms | 536 | @cindex @code{eval-defun}, and @code{defvar} forms |
| 536 | @cindex @code{eval-last-sexp}, and @code{defvar} forms | 537 | @cindex @code{eval-last-sexp}, and @code{defvar} forms |
diff --git a/lisp/emacs-lisp/bytecomp.el b/lisp/emacs-lisp/bytecomp.el index d7140ad9e63..ee530f95d09 100644 --- a/lisp/emacs-lisp/bytecomp.el +++ b/lisp/emacs-lisp/bytecomp.el | |||
| @@ -4943,8 +4943,6 @@ binding slots have been popped." | |||
| 4943 | (push (nth 1 (nth 1 form)) byte-compile-global-not-obsolete-vars)) | 4943 | (push (nth 1 (nth 1 form)) byte-compile-global-not-obsolete-vars)) |
| 4944 | (byte-compile-normal-call form)) | 4944 | (byte-compile-normal-call form)) |
| 4945 | 4945 | ||
| 4946 | (defconst byte-compile-tmp-var (make-symbol "def-tmp-var")) | ||
| 4947 | |||
| 4948 | (defun byte-compile-defvar (form) | 4946 | (defun byte-compile-defvar (form) |
| 4949 | ;; This is not used for file-level defvar/consts. | 4947 | ;; This is not used for file-level defvar/consts. |
| 4950 | (when (and (symbolp (nth 1 form)) | 4948 | (when (and (symbolp (nth 1 form)) |
| @@ -4977,18 +4975,17 @@ binding slots have been popped." | |||
| 4977 | string | 4975 | string |
| 4978 | "third arg to `%s %s' is not a string: %s" | 4976 | "third arg to `%s %s' is not a string: %s" |
| 4979 | fun var string)) | 4977 | fun var string)) |
| 4978 | ;; Delegate the actual work to the function version of the | ||
| 4979 | ;; special form, named with a "-1" suffix. | ||
| 4980 | (byte-compile-form-do-effect | 4980 | (byte-compile-form-do-effect |
| 4981 | (if (cddr form) ; `value' provided | 4981 | (cond |
| 4982 | ;; Quote with `quote' to prevent byte-compiling the body, | 4982 | ((eq fun 'defconst) `(defconst-1 ',var ,@(nthcdr 2 form))) |
| 4983 | ;; which would lead to an inf-loop. | 4983 | ((not (cddr form)) `',var) ; A simple (defvar foo) just returns foo. |
| 4984 | `(funcall '(lambda (,byte-compile-tmp-var) | 4984 | (t `(defvar-1 ',var |
| 4985 | (,fun ,var ,byte-compile-tmp-var ,@(nthcdr 3 form))) | 4985 | ;; Don't eval `value' if `defvar' wouldn't eval it either. |
| 4986 | ,value) | 4986 | ,(if (macroexp-const-p value) value |
| 4987 | (if (eq fun 'defconst) | 4987 | `(if (boundp ',var) nil ,value)) |
| 4988 | ;; This will signal an appropriate error at runtime. | 4988 | ,@(nthcdr 3 form))))))) |
| 4989 | `(eval ',form) | ||
| 4990 | ;; A simple (defvar foo) just returns foo. | ||
| 4991 | `',var))))) | ||
| 4992 | 4989 | ||
| 4993 | (defun byte-compile-autoload (form) | 4990 | (defun byte-compile-autoload (form) |
| 4994 | (and (macroexp-const-p (nth 1 form)) | 4991 | (and (macroexp-const-p (nth 1 form)) |
diff --git a/src/eval.c b/src/eval.c index 08e2dce61e4..c3be1dc12c8 100644 --- a/src/eval.c +++ b/src/eval.c | |||
| @@ -756,6 +756,33 @@ value. */) | |||
| 756 | return Qnil; | 756 | return Qnil; |
| 757 | } | 757 | } |
| 758 | 758 | ||
| 759 | static Lisp_Object | ||
| 760 | defvar (Lisp_Object sym, Lisp_Object initvalue, Lisp_Object docstring, bool eval) | ||
| 761 | { | ||
| 762 | Lisp_Object tem; | ||
| 763 | |||
| 764 | CHECK_SYMBOL (sym); | ||
| 765 | |||
| 766 | tem = Fdefault_boundp (sym); | ||
| 767 | |||
| 768 | /* Do it before evaluating the initial value, for self-references. */ | ||
| 769 | Finternal__define_uninitialized_variable (sym, docstring); | ||
| 770 | |||
| 771 | if (NILP (tem)) | ||
| 772 | Fset_default (sym, eval ? eval_sub (initvalue) : initvalue); | ||
| 773 | else | ||
| 774 | { /* Check if there is really a global binding rather than just a let | ||
| 775 | binding that shadows the global unboundness of the var. */ | ||
| 776 | union specbinding *binding = default_toplevel_binding (sym); | ||
| 777 | if (binding && EQ (specpdl_old_value (binding), Qunbound)) | ||
| 778 | { | ||
| 779 | set_specpdl_old_value (binding, | ||
| 780 | eval ? eval_sub (initvalue) : initvalue); | ||
| 781 | } | ||
| 782 | } | ||
| 783 | return sym; | ||
| 784 | } | ||
| 785 | |||
| 759 | DEFUN ("defvar", Fdefvar, Sdefvar, 1, UNEVALLED, 0, | 786 | DEFUN ("defvar", Fdefvar, Sdefvar, 1, UNEVALLED, 0, |
| 760 | doc: /* Define SYMBOL as a variable, and return SYMBOL. | 787 | doc: /* Define SYMBOL as a variable, and return SYMBOL. |
| 761 | You are not required to define a variable in order to use it, but | 788 | You are not required to define a variable in order to use it, but |
| @@ -770,12 +797,10 @@ value. If SYMBOL is buffer-local, its default value is what is set; | |||
| 770 | buffer-local values are not affected. If INITVALUE is missing, | 797 | buffer-local values are not affected. If INITVALUE is missing, |
| 771 | SYMBOL's value is not set. | 798 | SYMBOL's value is not set. |
| 772 | 799 | ||
| 773 | If SYMBOL has a local binding, then this form affects the local | 800 | If SYMBOL is let-bound, then this form does not affect the local let |
| 774 | binding. This is usually not what you want. Thus, if you need to | 801 | binding but the toplevel default binding instead, like |
| 775 | load a file defining variables, with this form or with `defconst' or | 802 | `set-toplevel-default-binding`. |
| 776 | `defcustom', you should always load that file _outside_ any bindings | 803 | (`defcustom' behaves similarly in this respect.) |
| 777 | for these variables. (`defconst' and `defcustom' behave similarly in | ||
| 778 | this respect.) | ||
| 779 | 804 | ||
| 780 | The optional argument DOCSTRING is a documentation string for the | 805 | The optional argument DOCSTRING is a documentation string for the |
| 781 | variable. | 806 | variable. |
| @@ -786,7 +811,7 @@ To define a buffer-local variable, use `defvar-local'. | |||
| 786 | usage: (defvar SYMBOL &optional INITVALUE DOCSTRING) */) | 811 | usage: (defvar SYMBOL &optional INITVALUE DOCSTRING) */) |
| 787 | (Lisp_Object args) | 812 | (Lisp_Object args) |
| 788 | { | 813 | { |
| 789 | Lisp_Object sym, tem, tail; | 814 | Lisp_Object sym, tail; |
| 790 | 815 | ||
| 791 | sym = XCAR (args); | 816 | sym = XCAR (args); |
| 792 | tail = XCDR (args); | 817 | tail = XCDR (args); |
| @@ -798,24 +823,8 @@ usage: (defvar SYMBOL &optional INITVALUE DOCSTRING) */) | |||
| 798 | if (!NILP (XCDR (tail)) && !NILP (XCDR (XCDR (tail)))) | 823 | if (!NILP (XCDR (tail)) && !NILP (XCDR (XCDR (tail)))) |
| 799 | error ("Too many arguments"); | 824 | error ("Too many arguments"); |
| 800 | Lisp_Object exp = XCAR (tail); | 825 | Lisp_Object exp = XCAR (tail); |
| 801 | |||
| 802 | tem = Fdefault_boundp (sym); | ||
| 803 | tail = XCDR (tail); | 826 | tail = XCDR (tail); |
| 804 | 827 | return defvar (sym, exp, CAR (tail), true); | |
| 805 | /* Do it before evaluating the initial value, for self-references. */ | ||
| 806 | Finternal__define_uninitialized_variable (sym, CAR (tail)); | ||
| 807 | |||
| 808 | if (NILP (tem)) | ||
| 809 | Fset_default (sym, eval_sub (exp)); | ||
| 810 | else | ||
| 811 | { /* Check if there is really a global binding rather than just a let | ||
| 812 | binding that shadows the global unboundness of the var. */ | ||
| 813 | union specbinding *binding = default_toplevel_binding (sym); | ||
| 814 | if (binding && EQ (specpdl_old_value (binding), Qunbound)) | ||
| 815 | { | ||
| 816 | set_specpdl_old_value (binding, eval_sub (exp)); | ||
| 817 | } | ||
| 818 | } | ||
| 819 | } | 828 | } |
| 820 | else if (!NILP (Vinternal_interpreter_environment) | 829 | else if (!NILP (Vinternal_interpreter_environment) |
| 821 | && (SYMBOLP (sym) && !XSYMBOL (sym)->u.s.declared_special)) | 830 | && (SYMBOLP (sym) && !XSYMBOL (sym)->u.s.declared_special)) |
| @@ -834,6 +843,14 @@ usage: (defvar SYMBOL &optional INITVALUE DOCSTRING) */) | |||
| 834 | return sym; | 843 | return sym; |
| 835 | } | 844 | } |
| 836 | 845 | ||
| 846 | DEFUN ("defvar-1", Fdefvar_1, Sdefvar_1, 2, 3, 0, | ||
| 847 | doc: /* Like `defvar' but as a function. | ||
| 848 | More specifically behaves like (defvar SYM 'INITVALUE DOCSTRING). */) | ||
| 849 | (Lisp_Object sym, Lisp_Object initvalue, Lisp_Object docstring) | ||
| 850 | { | ||
| 851 | return defvar (sym, initvalue, docstring, false); | ||
| 852 | } | ||
| 853 | |||
| 837 | DEFUN ("defconst", Fdefconst, Sdefconst, 2, UNEVALLED, 0, | 854 | DEFUN ("defconst", Fdefconst, Sdefconst, 2, UNEVALLED, 0, |
| 838 | doc: /* Define SYMBOL as a constant variable. | 855 | doc: /* Define SYMBOL as a constant variable. |
| 839 | This declares that neither programs nor users should ever change the | 856 | This declares that neither programs nor users should ever change the |
| @@ -863,9 +880,18 @@ usage: (defconst SYMBOL INITVALUE [DOCSTRING]) */) | |||
| 863 | error ("Too many arguments"); | 880 | error ("Too many arguments"); |
| 864 | docstring = XCAR (XCDR (XCDR (args))); | 881 | docstring = XCAR (XCDR (XCDR (args))); |
| 865 | } | 882 | } |
| 883 | tem = eval_sub (XCAR (XCDR (args))); | ||
| 884 | return Fdefconst_1 (sym, tem, docstring); | ||
| 885 | } | ||
| 866 | 886 | ||
| 887 | DEFUN ("defconst-1", Fdefconst_1, Sdefconst_1, 2, 3, 0, | ||
| 888 | doc: /* Like `defconst' but as a function. | ||
| 889 | More specifically, behaves like (defconst SYM 'INITVALUE DOCSTRING). */) | ||
| 890 | (Lisp_Object sym, Lisp_Object initvalue, Lisp_Object docstring) | ||
| 891 | { | ||
| 892 | CHECK_SYMBOL (sym); | ||
| 893 | Lisp_Object tem = initvalue; | ||
| 867 | Finternal__define_uninitialized_variable (sym, docstring); | 894 | Finternal__define_uninitialized_variable (sym, docstring); |
| 868 | tem = eval_sub (XCAR (XCDR (args))); | ||
| 869 | if (!NILP (Vpurify_flag)) | 895 | if (!NILP (Vpurify_flag)) |
| 870 | tem = Fpurecopy (tem); | 896 | tem = Fpurecopy (tem); |
| 871 | Fset_default (sym, tem); /* FIXME: set-default-toplevel-value? */ | 897 | Fset_default (sym, tem); /* FIXME: set-default-toplevel-value? */ |
| @@ -4333,9 +4359,11 @@ alist of active lexical bindings. */); | |||
| 4333 | defsubr (&Sdefault_toplevel_value); | 4359 | defsubr (&Sdefault_toplevel_value); |
| 4334 | defsubr (&Sset_default_toplevel_value); | 4360 | defsubr (&Sset_default_toplevel_value); |
| 4335 | defsubr (&Sdefvar); | 4361 | defsubr (&Sdefvar); |
| 4362 | defsubr (&Sdefvar_1); | ||
| 4336 | defsubr (&Sdefvaralias); | 4363 | defsubr (&Sdefvaralias); |
| 4337 | DEFSYM (Qdefvaralias, "defvaralias"); | 4364 | DEFSYM (Qdefvaralias, "defvaralias"); |
| 4338 | defsubr (&Sdefconst); | 4365 | defsubr (&Sdefconst); |
| 4366 | defsubr (&Sdefconst_1); | ||
| 4339 | defsubr (&Sinternal__define_uninitialized_variable); | 4367 | defsubr (&Sinternal__define_uninitialized_variable); |
| 4340 | defsubr (&Smake_var_non_special); | 4368 | defsubr (&Smake_var_non_special); |
| 4341 | defsubr (&Slet); | 4369 | defsubr (&Slet); |