prev UP NEXT GNU Emacs Lisp Reference Manual

10.8.1: Scope

Emacs Lisp uses indefinite scope for local variable bindings. This means that any function anywhere in the program text might access a given binding of a variable. Consider the following function definitions:

(defun binder (x)   ; x is bound in binder.
   (foo 5))         ; foo is some other function.
(defun user ()      ; x is used in user.
  (list x))

In a lexically scoped language, the binding of x in binder would never be accessible in user, because user is not textually contained within the function binder. However, in dynamically scoped Emacs Lisp, user may or may not refer to the binding of x established in binder, depending on circumstances:

  • If we call user directly without calling binder at all, then whatever binding of x is found, it cannot come from binder.
  • If we define foo as follows and call binder, then the binding made in binder will be seen in user:
    (defun foo (lose)
      (user))
    
  • If we define foo as follows and call binder, then the binding made in binder will not be seen in user:
    (defun foo (x)
      (user))
    

    Here, when foo is called by binder, it binds x. (The binding in foo is said to shadow the one made in binder.) Therefore, user will access the x bound by foo instead of the one bound by binder.