(eval-after-load 'lisp-mode
  (define-key emacs-lisp-mode-map (kbd "C-c r") 'raise-sexp))

When above lisp is evaluated, it shows following error message.

up-list: Scan error: "Unbalanced parentheses", 183, 1

What's strange to me is that following lisp is evaluated without any error:

(eval-after-load 'lisp-mode
  (define-key emacs-lisp-mode-map (kbd "C-c r") 'forward-sexp))

Could someone help me figure out what's going on here?

1 Answer 1

(with-eval-after-load 'lisp-mode
  (define-key emacs-lisp-mode-map (kbd "C-c r") 'raise-sexp))

Alternatively - note the quote mark before the argument:

(eval-after-load 'lisp-mode
  '(define-key emacs-lisp-mode-map (kbd "C-c r") 'raise-sexp))

eval-after-load is a function, which means that it evaluates all of its arguments. So you want to quote the form you pass to it, so it is evaluated after loading, not when eval-after-load itself is evaluated.

with-eval-after-load is a macro, which means that it does not necessarily evaluate any of its arguments (and a macro often doesn't evaluate any of them). Emacs added with-eval-after-load specifically because with eval-after-load some users have fallen into the same gotcha that you did: forgetting a quote mark or not realizing that they wanted one.

  • 3
    eval-after-load is a function so you should quote the form you pass to it; with-eval-after-load is a macro.
    – npostavs
    Commented Dec 27, 2015 at 21:15
  • if its required, then why the code for forward-sexp worked fine, now interested
    – Yuki Inoue
    Commented Dec 27, 2015 at 21:22
  • 1
    Because it's actually the same as doing (progn (define-key emacs-lisp-mode-map (kbd "C-c r") 'forward-sexp) (eval-after-load 'lisp-mode 'forward-sexp)), i.e. it performs the binding before loading lisp-mode and actually runs forward-sexp after lisp-mode is loaded (which is apparently harmless, but running raise-sexp causes problems).
    – npostavs
    Commented Dec 27, 2015 at 22:38
  • @npostavs Now I get it. What I evaluated in the question resulted in keybinding and executing the command itself, then.
    – Yuki Inoue
    Commented Dec 28, 2015 at 3:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.