5

I have the following code in the file1.el:

(with-current-buffer (get-buffer-create "TestBuffer")
  (read-only-mode -1)
  (erase-buffer)
  (make-local-variable 'source)
  (setq source (get-source-language))
  (my-package-insert text))

In the file2.el:

(with-current-buffer "TestBuffer"
  (message "Source: %s" (buffer-local-value 'source (get-buffer "TestBuffer"))))

First executes the code from file1.el and then from file2.el.

I expect that the source will be retrieved from the buffer "TestBuffer" but I get the error:

 Symbol's value as variable is void: source

Why that happens so? How could I retrieve the variable from the buffer? I can declare that variable by means of defvar but I don't want to hold temporal variable just for such a little need. Is it possible to avoid additional variable declaration?

Andrii Tykhonov
  • 452
  • 2
  • 13
  • I didn't put here all lines from original code and after the `my-package-insert` there is a call which turns on major-mode. I didn't know that that kills local variables! After I placed variable assignment after the major mode activation I can get local variable in the file2.el. Thank you very much! – Andrii Tykhonov Jan 15 '15 at 10:51
  • OK, replaced comment by an answer. Glad it helped. The Elisp manual is your friend - e.g., wrt local variables. – Drew Jan 15 '15 at 14:20
  • Yes, I've investigated manual before I posted the question here. But for sorry didn't find the answer. Probably, more careful investigation was required :-) Anyway, thank you very much! :-) – Andrii Tykhonov Jan 15 '15 at 16:44
  • 1
    Try node [`Creating Buffer-Local`](https://www.gnu.org/software/emacs/manual/html_node/elisp/Creating-Buffer_002dLocal.html) in the Elisp manual. Look for `kill-all-local-variables` and this paragraph: "*Every major mode command begins by calling this function, which has the effect of switching to Fundamental mode and erasing most of the effects of the previous major mode. To ensure that this does its job, the variables that major modes set should not be marked permanent.*". – Drew Jan 15 '15 at 16:48
  • Thanks for the reference! Now I see everything in details. I was reading exactly that page before but I was interested only in `make-local-variable`, `make-variable-buffer-local` and `buffer-local-value` functions and didn't even imagine to look to other functions, didn't mind that there could be such important information which could be related to other functions. – Andrii Tykhonov Jan 15 '15 at 19:12
  • 1
    FYI - I filed Emacs (doc) [**bug #19608**](http://debbugs.gnu.org/cgi/bugreport.cgi?bug=19608) for this (add an index entry) today, and it has already been fixed (in dev snapshots). – Drew Jan 15 '15 at 19:43
  • Great! Hope new developers will be informed and will not get the same issue in the future. Also I would like to say that that situation for me is nice lesson what can be done (I mean file an bug) in the future if I'll meet similar case. Really appreciate! – Andrii Tykhonov Jan 15 '15 at 21:43
  • 1
    `M-x report-emacs-bug` - and it is for enhancement requests too. – Drew Jan 15 '15 at 21:48

1 Answers1

3

Sounds like my-package-insert turns on a major mode. Doing that calls kill-all-local-variables.

You can give the variable a non-nil permanent-local property to protect it. But we would need more info to be able to suggest whether that is a good idea in your context.

If you don't need the variable to be bound before my-package-insert then consider binding it afterward.

Drew
  • 75,699
  • 9
  • 109
  • 225