0

I'm trying to define a buffer-local variable inside a function using setq-local in a function here is the function definition:

(defun set-buffer-local-variable ()
    (with-current-buffer (get-buffer-create "*my-buffer*")
        (erase-buffer)
        (setq-local m-buffer-local-variable 0)
        (print (format "buffer local value: %s" m-buffer-local-variable)) ;; prints 0
        (insert "some content")))

print in the function works as expected but when trying to access the variable from other functions (interactive or not ) or from eval-expression I get void variable error.

I don't know much about elisp but here are some relevant info that I think might help:

  1. emacs version: emacs29.0.92
  2. lexical-binding is set to nil
  3. enable-local-variables is set to t

I tried using make-local-variable with no success.

1
  • 1
    emacs version: emacs29.0.92 -- you should update to Emacs 29.1 rather than continuing to run the pretest version. (Not that it will have any bearing on this question, but you should still do this.)
    – phils
    Commented Aug 11, 2023 at 6:12

2 Answers 2

1

Using GNU Emacs 28.1 (which should not make any difference) started with emacs -Q I defined the following functions in the *scratch* buffer:

(defun set-buffer-local-variable ()
  (interactive)
  (with-current-buffer (get-buffer-create "*my-buffer*")
    (erase-buffer)
    (setq-local m-buffer-local-variable 0)
    (print (format "buffer local value: %s" m-buffer-local-variable)) ;; prints 0
    (insert "some content")))


(defun access-buffer-local-variable ()
  (interactive)
  (with-current-buffer (get-buffer "*my-buffer*")
    (print (format "buffer local value: %s" m-buffer-local-variable)) ;; prints 0
    (insert "some content")))

First I run M-x set-buffer-local-variable RET and afterwards I run M-x access-buffer-local-variable RET.

Both times I get the answer "buffer local value: 0". So everything is like expected.

Note also that enable-local-variable is about file-local variables. This is related to buffer-local variables but it is not the same. The Setting of enable-local-variable does not influence the behavior of buffer local variables.

1
  • Ok, thanks for your help. this works just fine but not my actual code, I might be to some mistake I made in code not to buffer-local-variables.
    – ntriisii
    Commented Aug 11, 2023 at 6:41
2

Presumably, since you setq the variable m-buffer-local-variable, that's the variable you want to print - instead you are trying to print a different (and undefined) variable mitmp-request-id.

Assuming you change the variable to print to m-buffer-local-variable instead, then when you call the function, e.g. with M-: (set-buffer-local-variable), it will create *my-buffer* if necessary and define m-buffer-local-variable as a buffer-local variable in that buffer. So if you switch to that buffer with C-x b *my-buffer*and ask for the value of the variable with e.g. C-h v m-buffer-local-variable, it will tell you its value is 0. But if you do the same thing in any other buffer, then it will be undefined. That's exactly how a buffer-local variable is supposed to behave.

12
  • sorry about, the variable I wanted to print is m-buffer-local-variable as it is the one defined locally.
    – ntriisii
    Commented Aug 11, 2023 at 5:52
  • as for the second part of the comment, I did exactly that and I still didn't have the buffer local to the buffer, I checked with (local-variable-p m-buffer-local-variable ) and it returns nil
    – ntriisii
    Commented Aug 11, 2023 at 5:54
  • 1
    @ntriisii: (local-variable-p 'm-buffer-local-variable) otherwise you're asking about a variable name stored in the value of m-buffer-local-variable. I ran your code and, in *my-buffer*, describe-variable tells me m-buffer-local-variable’s value is 0. Local in buffer *my-buffer*; globally void, which is what I'd expect.
    – phils
    Commented Aug 11, 2023 at 6:08
  • 1
    ok, I tried both sorry (local-variable-p 'm-buffer-local-variable) gave me nil and (local-variable-p m-buffer-local-variable ) gave me void-variable error.
    – ntriisii
    Commented Aug 11, 2023 at 6:44
  • 1
    Hi Nick, you actually answered the question. Nevertheless, I added a community answer which should be straight forward to reproduce (@ntriisii did it and accepted it). To compensate for your potential point losses I upvoted your answer.
    – Tobias
    Commented Aug 11, 2023 at 7:08

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.