5

I'm using this code on Xubuntu (emacs 24.5.1) but it throw error on windows (emacs 25.0.50.1 64bit)

(defun ignore-error-wrapper (fn)
  "Funtion return new function that ignore errors.
   The function wraps a function with `ignore-errors' macro."
  (lexical-let ((fn fn))
    (lambda ()
      (interactive)
      (ignore-errors
        (funcall fn)))))

(global-set-key [s-left] (ignore-error-wrapper 'windmove-left))
(global-set-key [s-right] (ignore-error-wrapper 'windmove-right))
(global-set-key [s-up] (ignore-error-wrapper 'windmove-up))
(global-set-key [s-down] (ignore-error-wrapper 'windmove-down))
Drew
  • 75,699
  • 9
  • 109
  • 225
jcubic
  • 691
  • 1
  • 4
  • 16
  • Please include some description of the error that is signaled. My crystal ball says that the problem is that you use `lexical-let` without first requiring `cl` (which is the package that exports `lexical-let`). – Stefan Dec 09 '15 at 13:12
  • @Stefan FWIW the error (void-function lexical-let) was mentioned in the title of the question. (But I do think it is very confusing.) – YoungFrog Dec 10 '15 at 10:31

1 Answers1

5

lexical-let is defined in cl.el. You can (eval-when-compile (require 'cl)) to fix the problem. This is mostly equivalent to (require 'cl) but is more efficient when you byte-compile your code (see Drew's comment below).

YoungFrog
  • 3,496
  • 15
  • 27
  • 2
    `cl-lib` is preferred to `cl`. To quote the CL manual: "New code should use cl-lib rather than cl." Ref: http://www.gnu.org/software/emacs/manual/html_node/cl/Organization.html#Organization – bmag Dec 09 '15 at 11:37
  • 3
    But `cl-lib` doesn't define `lexical-let`, so it's a bit less interesting for the problem at hand. I think the preferred way would be to rewrite the code with lexical-binding turned on. – YoungFrog Dec 09 '15 at 11:40
  • You are correct. I assumed that `cl-lib` provided `lexical-let`, but I was wrong. – bmag Dec 09 '15 at 11:46
  • 3
    Nope. You do not need to (and should not, since you do not need to), require `cl.el` at *runtime*. `lexical-let` is a **macro** (defined in `cl.el`), so what you should do is **`(eval-when-compile (require 'cl))`**. – Drew Dec 09 '15 at 14:40