Function: setq

setq is a special form defined in eval.c.

Signature

(setq [SYM VAL]...)

Documentation

Set each SYM to the value of its VAL.

The symbols SYM are variables; they are literal (not evaluated). The values VAL are expressions; they are evaluated. Thus, (setq x (1+ y)) sets x to the value of (1+ y). The second VAL is not computed until after the first SYM is set, and so on; each VAL can use the new value of variables set earlier in the setq. The return value of the setq form is the value of the last VAL.

View in manual

Probably introduced at or before Emacs version 1.12.

Source Code

// Defined in /usr/src/emacs/src/eval.c
{
  Lisp_Object val = args, tail = args;

  for (EMACS_INT nargs = 0; CONSP (tail); nargs += 2)
    {
      Lisp_Object sym = XCAR (tail);
      tail = XCDR (tail);
      if (!CONSP (tail))
	xsignal2 (Qwrong_number_of_arguments, Qsetq, make_fixnum (nargs + 1));
      Lisp_Object arg = XCAR (tail);
      tail = XCDR (tail);
      val = eval_sub (arg);
      /* Like for eval_sub, we do not check declared_special here since
	 it's been done when let-binding.  */
      Lisp_Object lex_binding
	= (SYMBOLP (sym)
	   ? Fassq (sym, Vinternal_interpreter_environment)
	   : Qnil);
      if (!NILP (lex_binding))
	XSETCDR (lex_binding, val); /* SYM is lexically bound.  */
      else
	Fset (sym, val);	/* SYM is dynamically bound.  */
    }

  return val;
}