Rorschach
Rorschach

Reputation: 32426

Use value of buffer-local variable in another buffer

How can you process a buffer local variable in another buffer? I thought I could just bind it with let, but am having trouble passing the variable to another function that uses symbol-value. Here is a small example,

(defvar-local local-var nil)
(setq local-var "a")

(defun fun ()
  (let ((local-var local-var))
    (with-temp-buffer
      (format-fun 'local-var)
      (message (buffer-string)))))

(defun format-fun (name)
  (insert (symbol-value name)))

How can I bind local-var in fun so format-fun can process it in another buffer?

Upvotes: 1

Views: 794

Answers (2)

Jürgen Hötzel
Jürgen Hötzel

Reputation: 19727

There is an elisp function to get a buffer-local variable value from another buffer:

(buffer-local-value 'var (get-buffer  "your-buffer-name"))

Upvotes: 5

Barmar
Barmar

Reputation: 781088

Binding the variable with let doesn't stop it from being reassigned when switching buffers.

Use a different variable to avoid this.

(defun fun ()
  (let ((new-var local-var))
    (with-temp-buffer
      (format-fun 'new-var)
      (message (buffer-string)))))

Upvotes: 2

Related Questions