I use pyenv to maintain my python versions and virtualenvs. I really like the way you can create a local .python-version
for each project, pointing to the version I need. I use pyenv-mode
which allows me to set the right pyenv
version pretty easily. The problem is that I tend to keep a single emacs server running, so when I switch between buffers in different projects I have to manually switch my pyenv, and if the python version is different I also have to restart the RPC server. I'm trying to automate this, and I found a great library to enable me to run hooks when I switch buffers (https://github.com/10sr/switch-buffer-functions-el).
Here's what I have so far, but it doesn't seem to be working at all (neither the if or then clauses execute):
(add-hook 'switch-buffer-functions
(lambda (prev cur)
(if (string-equal "Python" (format-mode-line mode-name nil nil (get-buffer cur)))
(lambda ()
(message "Switching python buffer")
(pyenv-use-corresponding)
(elpy-rpc-restart))
(lambda ()
(message "Switched to non-python buffer"))
)))
however if I do something like:
(add-hook 'switch-buffer-functions
(lambda (prev cur)
(message (format-mode-line mode-name nil nil (get-buffer cur)))))
it seems like it should work as expected as it does print the mode-name
. I also tested the if
comparison with M-:
and it seems to work.... anyone see what I'm doing wrong?