Suppose, we have a buffer with Python code in it. I want to evaluate it, switch to Python buffer and see the results. Then easily switch back to code buffer.
I tried the following:
(defun my-python-mode ()
(local-set-key (kbd "<kp-insert>")
(lambda () (interactive)
(elpy-shell-send-buffer-and-go)
(elpy-shell-switch-to-shell-in-current-window)
(let ((py-buffer (get-buffer "*Python*")))
(message (buffer-name py-buffer))
(if py-buffer
(progn
; (other-window 1)
(switch-to-buffer py-buffer)
(end-of-buffer))))))))
I added a callback to kp-insert
but my function doesn't switch to python buffer when I click kp-insert
. If there is no python buffer previously, it runs python interpreter, but doesn't switch to it.
(add-hook 'inferior-python-mode-hook
(lambda ()
(local-set-key (kbd "<kp-add>")
lambda () (interactive)
message "aaa")))
I tried this to switch back from interpreter, but it doesn't work.
How can I set up an ergonomic python environment in Emacs?
Update 1
I created a function that splits the window, runs the code and switches to Python buffer. Unfortunately, when there is no Python buffer, it just splits the window, but doesn't switch to buffer and doesn't send my code to Python. So I have to click the key twice. Is it possible to fix this?
(defun aaa ()
(interactive)
(if (= (length (window-list)) 1)
(split-window-right))
(elpy-shell-send-buffer-and-step)
(let ((py-buffer (get-buffer "Python")))
(other-window 1)
(if py-buffer
(progn
(switch-to-buffer py-buffer)
(end-of-buffer)
))))
elpy-shell-send-buffer-and-step
will create a*Python*
buffer, but there's no guarantee it will exist when you try to get it. I notice there existselpy-get-or-create-process
which takes a delay parameter to ensure startup. These are not thoroughly thought out suggestions, merely nudges. Hopefully they get you un-stuck :) – Lorem Ipsum Feb 02 '21 at 15:39