2

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?

  • 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! – Andriy 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
  • 1
    Try node Creating Buffer-Local 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
  • 1
    FYI - I filed Emacs (doc) 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
  • 1
    M-x report-emacs-bug - and it is for enhancement requests too. – Drew Jan 15 '15 at 21:48
2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.