How do I open a file foo, associated with a major mode (in this case python, so it's a foo.py), on the right side of a split window (and the window is split only for this major mode)? The following code doesn't work at all (it displays the scratch buffer on both sides).
(defun my-eval-after-load-python()
(split-window-horizontally)
)
(eval-after-load "python" '(my-eval-after-load-python))
For setting up a particular major-mode
that matches a file name or file extension, see the variable auto-mode-alist
. Emacs 25 supports python-mode
for .py
file extensions out-of-the-box.
Caveat re Startup: The built-in (hard-coded) library startup.el
contains some selection of buffers/windows that are displayed on startup. Customizing a particular window layout upon starting Emacs is always somewhat of a challenge, and the method will most likely be custom to a particular user. For better results, the user may wish to place the window organization functions at the very bottom of the .emacs
or init.el
or use the emacs-startup-hook
(which runs towards the end of the startup process). Certain libraries such as desktop.el
(desktop restore) will complicate the selection of which buffer should have focus when startup finishes. Each user will need to invest some time organizing the startup in a way that suits his/her needs. For example, there may be two windows and the user may want to have focus in the other -- something lo-tech like (other-window 1)
at the bottom of the customization file might be all that is needed. In the example below with my-display-buffer
, the value returned is a window -- the user may wish to wrap the last line window
like this (select-window window)
so that it gets selected; or, instead of modifying my-display-buffer
, the user could add (select-window (my-display-buffer BUFFER ALIST DIRECTION))
when using the function without modifying it internally. The original poster may also be interested in using find-file
(to target the current window), or find-file-other-window
(to create/target another window) -- e.g., (find-file-other-window "~/foo.py")
.
If focus is already in the desired window (e.g., the window on the right is already selected), then just use something like set-window-buffer
or switch-to-buffer
.
To control display of a buffer above, below, left or right, see the following example that uses a custom function called my-display-buffer
hereinbelow:
Sample Usage: The function definition of my-display-buffer
would need to appear in the .emacs
or init.el
file prior to using any of these four sample snippets.
(let ((buffer (find-file-noselect "~/foo.py")))
(with-current-buffer buffer
(message "major-mode: %s" major-mode))
(my-display-buffer buffer nil 'left))
or
(let ((buffer (find-file-noselect "~/foo.py")))
(with-current-buffer buffer
(message "major-mode: %s" major-mode))
(my-display-buffer buffer nil 'right))
or
(let ((buffer (find-file-noselect "~/foo.py")))
(with-current-buffer buffer
(message "major-mode: %s" major-mode))
(my-display-buffer buffer nil 'above))
or
(let ((buffer (find-file-noselect "~/foo.py")))
(with-current-buffer buffer
(message "major-mode: %s" major-mode))
(my-display-buffer buffer nil 'below))
Example Function:
(defun my-display-buffer (buffer alist direction &optional size pixelwise)
"BUFFER: The buffer that will be displayed.
ALIST: See the doc-string of `display-buffer' for more information.
DIRECTION: Must use one of these symbols: 'left 'right 'below 'above
SIZE: See the doc-string for `split-window'.
PIXELWISE: See the doc-string for `split-window'.
There are three possibilities:
- (1) If a window on the frame already displays the target buffer,
then just reuse the same window.
- (2) If there is already a window in the specified direction in relation
to the selected window, then display the target buffer in said window.
- (3) If there is no window in the specified direction, then create one
in that direction and display the target buffer in said window."
(let ((window
(cond
((get-buffer-window buffer (selected-frame)))
((window-in-direction direction))
(t
(split-window (selected-window) size direction pixelwise)))))
(window--display-buffer buffer window 'window alist display-buffer-mark-dedicated)
window))