I've modified calculate-lisp-indent
as a better solution to the indentation questions here, here and here. By "modified" I mean I overrode the function with advice. After doing this when I restarted emacs and I tried to indent a lisp form, I'd get the error void-variable calculate-lisp-indent-last-sexp
.
Here's where things get interesting. You may be inclined to think that's it's some problem with the code I added to calculate-lisp-indent
. But after failing to figuring out what was wrong with the code I added (which by the way didn't even mention calculate-lisp-indent
), I tried overriding calculate-lisp-indent
with a function that has the exact same body as calculate-lisp-indent
. What happened then? I got the same error. Wierd.
Ok, now I started looking at the code for lisp-indent-function
and I could not find where the variable calculate-lisp-indent-last-sexp
was set. It looked as if it was just unbound.
Check out the beginning of the lisp-indent-function
. calculate-lisp-indent-last-sexp
was used but not defined.
(let ((normal-indent (current-column))
(orig-point (point)))
(goto-char (1+ (elt state 1)))
(parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t)
...
A small digression: go to say I'm not sure why parse-partial-sexp
is doing here. Based on it's documentation I thought it just returned the list, but I don't see the value it's returning being stored anywhere. Does it have any (undocumented) side-effects?
So my first though was calculate-lisp-indent-last-sexp
must be a global variable. But when I tried to describe it with describe-variable
I couldn't find it.
Also super interesting. I noticed that after restarting emacs and getting the void variable error, if I re-evaluated the function I wrote to override calculate-lisp-indent
then I don't get errors and my changes to calculate-lisp-indent
take effect as I would expect.
;; If I re-evaluate this, then the indentation works and I get no errors.
(defun void~calculate-lisp-indent (&optional parse-start)
"Has same body as `calculate-lisp-indent`."
...)
What is going on here?
EDIT: After starting emacs with the advice I performed a few checks.
(foundp #'void~calculate-lisp-indent) ; => t
(advice-member-p #'void~calculate-lisp-indent #'calculate-lisp-indent) ; => a non-nil value
This confirms the advice is a defined function and that it is advising calculate-lisp-indent
. I don't understand why re-evaluating it makes a difference.
parse-partial-sexp
has the documented side-effect: "point is set to where parsing stops." (see the docstring)