Emacs 26.1 user here.
I know some common-lisp but new to elisp, so I wonder if I'm missing something obvious. I'm trying a rather simple frame setup with a function I wrote, and there's this strange behaviour. My local variable is getting set to nil after switch-to-buffer call. Here's the simplified version that has the same issue.
(defun my-frame-test1 ()
(let ((frame (make-frame '((name . "test-frame")
(width . 300)
(visibility . nil)
(height . 100))))
left-window edit-window-left edit-window-right)
(set-frame-position frame 100 100)
(make-frame-visible frame)
(select-frame frame)
(setq-local left-window (selected-window))
(setq-local edit-window-left (split-window-right 30))
(select-window edit-window-left)
(setq-local edit-window-right (split-window-right))
;; ---- edit-window-right has the expected value at this point ----
(dolist (f '("/tmp/file1.lisp" "/tmp/file2.lisp"))
(find-file-noselect f t))
(switch-to-buffer (get-file-buffer "/tmp/file1.lisp")) ;; after this line, edit-window-right has changed to nil
(select-window edit-window-right) ;; <-- thus, this call doesn't work.
(switch-to-buffer (get-file-buffer "/tmp/file2.lisp"))
))
I tried using setq instead of a let block, using different variable name, for example 'amp-edit-window-right, calling (switch-to-buffer (get-file-buffer "/tmp/file1.lisp") nil t), and all behave the same.
When I do a line by line execution by using the new frame and M-: it is the same. And the window that edit-window-right is set to (#<window 18 on some-file.el>) still has the same print value after edit-window-right is set to nil (i.e. the same window still exists).
Why would this happen?
kill-all-local-variables. SeeC-h ffor that function.setq-localmeans, which is buffer-local. If you set a buffer-local variable and then switch to a different buffer; in the new buffer you naturally won't see the value which was local to the original buffer.