Search code examples
emacsscopeelisp

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?


Solution

  • 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)))))