Search code examples
closureselispdynamic-scope

How to work without closures in Emacs lisp dynamic scoping


I know Emacs 24 introduced lexical scoping; that is great but I work with lexical scoping all the time, and I'm trying to see from a different point of view with dynamic scoping.

Originally I just did what I would always do, rely on a closure:

(add-hook 'cider-mode-hook 
  (lambda () 
    (dolist (p '(("M-l" . cider-load-current-buffer)
         ("M-e" . cider-eval-last-expression)))
      (local-set-key 
       (kbd (car p)) 
       (lambda () (interactive) (save-buffer) (cdr p))))))

After finally understanding why p is undefined when the lambda runs, I came up with this to force the evaluation of p in the context of the dolist rather than when the lambda runs.

(add-hook 'cider-mode-hook 
  (lambda () 
    (dolist (p '(("M-l" . cider-load-current-buffer)
         ("M-e" . cider-eval-last-expression)))
      (local-set-key 
       (kbd (car p)) 
       (cons 'lambda `(() (interactive) (save-buffer) (funcall (quote ,(cdr p)))))))))

Is this the classical solution for solving the problem without closures and lexical scoping?


Solution

  • I would do this like this if I wanted to keep bindings in a list like you. I actually prefer to spell out local-set-key for each command.

    (defun save-before-call (f)
      `(lambda()
         (interactive)
         (save-buffer)
         (funcall #',f)))
    
    (add-hook 'cider-mode-hook
      (lambda ()
        (mapc (lambda(x)(local-set-key
                         (kbd (car x))
                         (save-before-call (cdr x))))
              '(("M-l" . cider-load-current-buffer)
                ("M-e" . cider-eval-last-expression)))))