aboutsummaryrefslogtreecommitdiffstats
path: root/src/eval.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/eval.c')
-rw-r--r--src/eval.c78
1 files changed, 53 insertions, 25 deletions
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
759static Lisp_Object
760defvar (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
759DEFUN ("defvar", Fdefvar, Sdefvar, 1, UNEVALLED, 0, 786DEFUN ("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.
761You are not required to define a variable in order to use it, but 788You 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;
770buffer-local values are not affected. If INITVALUE is missing, 797buffer-local values are not affected. If INITVALUE is missing,
771SYMBOL's value is not set. 798SYMBOL's value is not set.
772 799
773If SYMBOL has a local binding, then this form affects the local 800If SYMBOL is let-bound, then this form does not affect the local let
774binding. This is usually not what you want. Thus, if you need to 801binding but the toplevel default binding instead, like
775load 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.)
777for these variables. (`defconst' and `defcustom' behave similarly in
778this respect.)
779 804
780The optional argument DOCSTRING is a documentation string for the 805The optional argument DOCSTRING is a documentation string for the
781variable. 806variable.
@@ -786,7 +811,7 @@ To define a buffer-local variable, use `defvar-local'.
786usage: (defvar SYMBOL &optional INITVALUE DOCSTRING) */) 811usage: (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
846DEFUN ("defvar-1", Fdefvar_1, Sdefvar_1, 2, 3, 0,
847 doc: /* Like `defvar' but as a function.
848More 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
837DEFUN ("defconst", Fdefconst, Sdefconst, 2, UNEVALLED, 0, 854DEFUN ("defconst", Fdefconst, Sdefconst, 2, UNEVALLED, 0,
838 doc: /* Define SYMBOL as a constant variable. 855 doc: /* Define SYMBOL as a constant variable.
839This declares that neither programs nor users should ever change the 856This 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
887DEFUN ("defconst-1", Fdefconst_1, Sdefconst_1, 2, 3, 0,
888 doc: /* Like `defconst' but as a function.
889More 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);