Question

I try to evaluate simple exression (/ 72 24) using C-x C-e when it's inserted inside javascript code (in javascript mode), but it throw an error

Debugger entered--Lisp error: (invalid-read-syntax "] in a list")
  read(#<buffer SomeFile.js>)
  preceding-sexp()
  eval-last-sexp-1(nil)
  eval-last-sexp(nil)
  call-interactively(eval-last-sexp nil nil)

It work when I insert it into empty buffer. Why eval-last-sexp don't work in this way?

Was it helpful?

Solution

You need to position the point exactly after the closing parenthesis. Then it should work.

UPD The / messes up the syntax

I've checked again and the fault is the js-mode redefining the syntax table for its regex literal.

Here's the fix:

(defun eval-last-sexp-js (eval-last-sexp-arg-internal)
  (interactive "P")
  (save-excursion
    (while (re-search-backward "(/" (line-beginning-position) t)
      (remove-text-properties 
       (match-beginning 0)
       (1+ (match-end 0))
       '(syntax-table))))
  (eval-last-sexp eval-last-sexp-arg-internal))

eval-last-sexp-js will work the same as eval-last-sexp. You can rebind it specifically for js-mode if you want.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top