How to start emacs with ipython shell running in one side of a divided window?

1.1k views Asked by At

I´d like to have the following workflow using emacs 23.4 as a python (2.7) IDE on Debian:

  1. Emacs initiates with 2 windows side-by-side when opening a file like $ emacs file.py
  2. There´s already a shell in the left window and the buffer file.py in the right window.
  3. A shortcut executes the code (and another shortcut for parts of it) and the result can be seen in the left window (ipython shell). The focus remains at the right window and the buffers don´t change when the command is executed.
  4. A shortcut easily switches the focus from left to right and the other way around.

I could, so far, accomplish everything except the second item (I have to make the buffer visible manually), which seems simple. I´ve been reading the emacs lips reference manual, so that I can customize emacs myself, but I´m still a beginner in emacs. I also found some similar questions, but not fully helpful. Here are some relevant parts of my .emacs.

;; Initial frame size and position (1280x1024)
(setq default-frame-alist
  '((top . 45) (left . 45)
    (width . 142) (height . 54)))
(if (window-system) 
  (split-window-horizontally (floor (* 0.49 (window-width))))
)

; python-mode
(setq py-install-directory "~/.emacs.d/python-mode.el-6.1.3")
(add-to-list 'load-path py-install-directory)
(require 'python-mode)

; use IPython
(setq-default py-shell-name "ipython")
(setq-default py-which-bufname "IPython")
; use the wx backend, for both mayavi and matplotlib
(setq py-python-command-args
  '("--gui=wx" "--pylab=wx" "--colors=linux"))
(setq py-force-py-shell-name-p t)

; switch to the interpreter after executing code
(setq py-shell-switch-buffers-on-execute-p t)
 ;(setq py-switch-buffers-on-execute-p t)

; don't split windows
(setq py-split-windows-on-execute-p nil)
; try to automagically figure out indentation
(setq py-smart-indentation t)

(defun goto-python-shell () 
  "Go to the python command window (start it if needed)" 
  (interactive)
  (setq current-python-script-buffer (current-buffer))
  (py-shell)
  (end-of-buffer)
)
(goto-python-shell)

I believe the solution is simple and lies on the functions/variables: switch-to-buffer, initial-buffer-choice, other-window, py-shell-switch-buffers-on-execute-p, py-switch-buffers-on-execute-p.

However, I still couldn't find a solution that makes it all work.


EDIT:

I was able to have my desired behavior, substituting the last part for:

(switch-to-buffer (py-shell))
(end-of-buffer)
(other-window 3)
(switch-to-buffer (current-buffer))

, since I found out with get-buffer-window that the left window appears as 3 and right window as 6.

1

There are 1 answers

3
Andreas Röhler On

When py-split-windows-on-execute-p, py-switch-buffers-on-execute-p are set, it should work as expected - no need to hand-write splitting.

Remains the horizontal/vertical question. For this python-mode.el provides a customization of py-split-windows-on-execute-function in current trunk:

https://code.launchpad.net/python-mode