I know Emacs tries to be intellectual and opens its helper buffers depending on which dimension of the window is bigger, so it may appear in vertical split window if current width is bigger than height, and in horizontal split otherwise.
But I’d prefer it to open that list always in horizontal split, because there are long paths I can’t see when the buffer is placed in vertical split. How can I do this?
I believe you've got the horizontal/vertical split terminology back to front (I can never remember which is which either), but as your goal was to retain the original window's width, I'm forcing a vertical split.
See C-hf split-window-sensibly RET. It tells you what to do:
You can enforce this function to not split WINDOW horizontally,
by setting (or binding) the variable `split-width-threshold' to
nil. If, in addition, you set `split-height-threshold' to zero,
chances increase that this function does split WINDOW vertically.
So as a permanent setting:
(setq split-width-threshold nil)
(setq split-height-threshold 0)
For just a specific function, you can advise that function (but see Edit 2 below!):
(defadvice list-buffers (around list-buffers-split-vertically)
"Always split vertically when displaying the buffer list.
See `split-window-sensibly'."
(let ((split-width-threshold nil)
(split-height-threshold 0))
ad-do-it))
(ad-activate 'list-buffers)
Edit: Actually, in this instance I suspect you're only concerned with the interactive case, in which case it's preferable to define a function and remap the bindings:
(defun my-list-buffers-vertical-split ()
"`list-buffers', but forcing a vertical split.
See `split-window-sensibly'."
(interactive)
(let ((split-width-threshold nil)
(split-height-threshold 0))
(call-interactively 'list-buffers)))
(global-set-key [remap list-buffers] 'my-list-buffers-vertical-split)
Edit 2: And Stefan points out that display-buffer-alist facilitates such things without advising functions (and of course avoiding unnecessary advice is always a good thing). I believe we still need a custom action, so:
(defun my-display-buffer-pop-up-same-width-window (buffer alist)
"A `display-buffer' ACTION forcing a vertical window split.
See `split-window-sensibly' and `display-buffer-pop-up-window'."
(let ((split-width-threshold nil)
(split-height-threshold 0))
(display-buffer-pop-up-window buffer alist)))
(add-to-list 'display-buffer-alist
'("\\*Buffer List\\*" my-display-buffer-pop-up-same-width-window))
If horizontally or vertically, presently neither split-height-threshold nor split-width-threshold seem reliable WRT to expected kind of split. Which looks like a bug, resp. design issue.
As a work-around call M-x split-window-horizontally resp. -vertically before, or advise the function with it.
You can remove (5) if you prefer not to select the window after it is displayed -- i.e., remove (select-window (get-buffer-window (buffer-name buffer))). I like the bottom window to be reserved for a 3-month calendar, so that's why I have a condition to use the window above (if it exists) -- you can remove that condition if you are so inclined. Actually, it's your function so you can modify everything as you see fit now that you see how it works. The alist would be used like this: '((window-width . 33)) if you wanted to control certain aspects of the target window, etc. I find myself always going back to this document page because it is the only scanty formal example I've found . . . and, of course, the source itself window.el: http://www.gnu.org/software/emacs/manual/html_node/elisp/Display-Action-Functions.html
(defun lawlist-list-buffers-left (&optional arg)
"Display a list of existing buffers.
The list is displayed in a buffer named \"*Buffer List*\".
See `buffer-menu' for a description of the Buffer Menu.
By default, all buffers are listed except those whose names start
with a space (which are for internal use). With prefix argument
ARG, show only buffers that are visiting files."
(interactive "P")
(lawlist-display-buffer-left (list-buffers-noselect arg) nil))
(defun lawlist-list-buffers-right (&optional arg)
"Display a list of existing buffers.
The list is displayed in a buffer named \"*Buffer List*\".
See `buffer-menu' for a description of the Buffer Menu.
By default, all buffers are listed except those whose names start
with a space (which are for internal use). With prefix argument
ARG, show only buffers that are visiting files."
(interactive "P")
(lawlist-display-buffer-right (list-buffers-noselect arg) nil))
(defun lawlist-display-buffer-left (buffer alist)
"(1) If `buffer` is already displayed, then display it again in the same window.
(2) If `buffer` is not already displayed, and if there is a window to the left,
then display that `buffer` in said window. (3) If `buffer` is not already
displayed, and if there is a window to the right, then use the selected window.
(4) If all else fails, then create a new window to the left and display `buffer` there.
(5) Select the target window which displays `buffer`."
(let (
(window
(cond
((get-buffer-window buffer (selected-frame)))
((window-in-direction 'above))
((window-in-direction 'left))
((window-in-direction 'right)
(selected-window))
(t
(split-window (selected-window) nil 'left)))))
(window--display-buffer buffer window 'window alist display-buffer-mark-dedicated)
;; OPTIONAL -- uncomment to select the target window.
;; (select-window (get-buffer-window (buffer-name buffer)))
))
(defun lawlist-display-buffer-right (buffer alist)
"(1) If `buffer` is already displayed, then display it again in the same window.
(2) If `buffer` is not already displayed, and if there is a window to the right,
then display that `buffer` in said window. (3) If `buffer` is not already
displayed, and if there is a window to the left, then use the selected window.
(4) If all else fails, then create a new window to the right and display `buffer` there.
(5) Select the target window which displays `buffer`."
(let (
(window
(cond
((get-buffer-window buffer (selected-frame)))
((window-in-direction 'above))
((window-in-direction 'right))
((window-in-direction 'left)
(selected-window))
(t
(split-window (selected-window) nil 'right)))))
(window--display-buffer buffer window 'window alist display-buffer-mark-dedicated)
;; OPTIONAL -- uncomment to select the target window.
;; (select-window (get-buffer-window (buffer-name buffer)))
))
Related
I'd like to implement a function it shows relevant code paragraph in its right window when Emacs cursor is prompting on a certain word.
Let's assume that I have my own log format and I have corresponding database which I can find where a log message came from then I have the pointer on a certain log message. If so, then I like to let Emacs open the corresponding source file in a right side window of that log buffer.
Now I can query and get the location of source file thru my own db and emacs. But I still don't know how to control the right window.
If I opened a right window once, then Emacs would open an another one again, I don't want to let it do but want to let it use the previous, existing window.
How could I implement this? Please advise me, or share an example you might have.
Thanks.
In addition to the examples below using the custom function my-display-buffer, keep in mind that BUFFER can be obtained by means other than find-file-noselect; e.g., current-buffer if so desired. In terms of finding your location in the other window, you may find it helpful to select-window or with-selected-window or set-window-point, etc. If the window is selected with the target buffer visible in said window, then simple things like goto-char will suffice to go visually to a particular location. The example my-display-buffer function has a doc-string which describes generally what it was designed to do; i.e., "There are three possibilities ...".
Display buffer to the left:
(let ((buffer (find-file-noselect "~/foo.py")))
(with-current-buffer buffer
(message "major-mode: %s" major-mode))
(my-display-buffer buffer nil 'left))
Display buffer to the right:
(let ((buffer (find-file-noselect "~/foo.py")))
(with-current-buffer buffer
(message "major-mode: %s" major-mode))
(my-display-buffer buffer nil 'right))
Display buffer above:
(let ((buffer (find-file-noselect "~/foo.py")))
(with-current-buffer buffer
(message "major-mode: %s" major-mode))
(my-display-buffer buffer nil 'above))
Display buffer below:
(let ((buffer (find-file-noselect "~/foo.py")))
(with-current-buffer buffer
(message "major-mode: %s" major-mode))
(my-display-buffer buffer nil 'below))
(defun my-display-buffer (buffer-or-name 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* ((buffer
(if (bufferp buffer-or-name)
buffer-or-name
(get-buffer buffer-or-name)))
(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))
I have 2 windows, one is the buffer list, how do I show the buffer in the buffer list in the other windows when I use n and p to naviage in the buffer list?
Thanks a lot.
If I understand the question right, the answer is simply to use C-o in the buffer menu. that displays the buffer of the line you are on in another window, but it does not select that buffer. That is, it does not move the input focus to that buffer's window. The buffer list retains the input focus, so you can continue to use C-o on different lines, to display different buffers.
If you mean an Emacs window when you say "frame", then see above. The same is true even if the buffers are on different frames, with this caveat: Some window managers, including MS Windows, change the input focus to a new frame that is created. So if, for example, you have non-nil pop-up-frames (so that a separate frame is used to display a buffer), and if the buffer you choose to display (using C-o) is not already shown in some other frame, then displaying it not only creates a new frame for it but also shifts the focus to that new frame. If the buffer is already visible in another frame then C-o simply raises that frame.
There is currently no key bound in the buffer-list display, to both (a) move the cursor down or up to the next or previous buffer line and (b) invoke the C-o behavior of displaying the buffer named on the target buffer line. But you could easily define such a command and bind it to a key:
(defun show-next (arg)
"Show next line's buffer in another window."
(interactive "p")
(next-line arg)
(Buffer-menu-switch-other-window))
(defun show-previous (arg)
"Show previous line's buffer in another window."
(interactive "p")
(previous-line arg)
(Buffer-menu-switch-other-window))
(define-key Buffer-menu-mode-map "\M-n" 'show-next)
(define-key Buffer-menu-mode-map "\M-p" 'show-previous)
Here's my prototype for this feature in a dired buffer. It kills a buffer after it got visited.
This is a feature I like from the ranger file manager, it is handy when you explore a directory.
(setq show-next-current-buffer nil)
(defun show-next ()
(interactive)
(next-line 1)
(dired-find-file-other-window)
(if show-next-current-buffer (kill-buffer show-next-current-buffer))
(setq show-next-current-buffer (current-buffer))
(other-window 1)
)
(defun show-previous ()
(interactive)
(previous-line 1)
(dired-find-file-other-window)
(if show-next-current-buffer (kill-buffer show-next-current-buffer))
(setq show-next-current-buffer (current-buffer))
(other-window 1)
)
(define-key dired-mode-map "n" 'show-next)
(define-key dired-mode-map "p" 'show-previous)
edit: I've written a minor mode to enable/disable this feature easily. See https://gitlab.com/emacs-stuff/my-elisp/blob/master/dired-show.el and meld it to your needs.
I'm looking for some assistance, please, to programmatically scroll a particular window to the very beginning without incurring an error beginning of buffer (which necessarily halts the function). The current (point) is within the visible window, but near the bottom. The number of lines to scroll-down may vary, so I cannot say for sure exactly how many is needed -- but the very beginning of the buffer is what I'm looking for. I don't think (save-excursion (goto-char (point-min))) will do what I want.
scroll-down does not have an argument for a specific window, so if the solution to this question could also include an example to perform the action with a specific window (without switching to it), that would be greatly appreciated. In other words, I'd like to be in a different window while this function is going on in the target window.
(when
(not (pos-visible-in-window-p (point-min)
(get-buffer-window calendar-buffer (selected-frame))))
(scroll-down 20))
You can wrap scroll-down in a condition-case to regain control when an error is signaled:
(condition-case nil
(scroll-down 20)
(error nil))
I wasn't exactly sure if you wanted your last code snippet was part of the function you were referring to, but you can wrap it all in a save-selected-window:
(let ((cal (get-buffer-window calendar-buffer (selected-frame))))
(when cal
(save-selected-window
(select-window cal)
(condition-case nil
(scroll-down 20)
(error nil)))))
EDIT: jpkotta suggested ignore-errors in the comment, which I agree looks cleaner:
(ignore-errors
(scroll-down 20))
(let ((win (get-buffer-window calendar-buffer (selected-frame))))
(when (not (pos-visible-in-window-p (point-min) win))
(set-window-start win (point-min))))
`set-window-start` is a built-in function in `C source code'.
(set-window-start WINDOW POS &optional NOFORCE)
Make display in WINDOW start at position POS in WINDOW's buffer.
WINDOW must be a live window and defaults to the selected one. Return
POS. Optional third arg NOFORCE non-nil inhibits next redisplay from
overriding motion of point in order to display at this exact start.
See also http://www.gnu.org/software/emacs/manual/html_node/elisp/Window-Start-and-End.html
See also http://www.gnu.org/software/emacs/manual/html_node/elisp/Selecting-Windows.html
So, instead of creating a split window under (or to the right of) the currently active window, it would appear below all the existed ones with a half of the frame height? And, after closing, frame layout would be restored as it was before calling C-x C-b?
I’d like see the full paths to opened files.
See also my previous answer to your related issue: https://stackoverflow.com/a/21544307/2112489
See also a related answer by #phils, which includes a nice halve-other-window-height function: https://stackoverflow.com/a/4988206/2112489
See also the built-in stock functions display-buffer-below-selected or display-buffer-at-bottom, which are available in a recent version of Emacs Trunk -- I'm not sure when each function was first introduced. They are in window.el.
The doc-string of the function split-window states in relevant part: SIZE defaults to half of
WINDOW's size. That is the second optional argument -- i.e., split-window (&optional window size side pixelwise)
Don't be shy about modifying these things -- you can make it do whatever you want. If want to select the window automatically after it is displayed, then you can add this to the bottom of the lawlist-display-buffer-below function: (select-window (get-buffer-window (buffer-name buffer))) -- leaving, of course, two closing parenthesis to the right -- i.e., one closing parentheis for the let binding and one closing parenthesis for the defun.
(defun lawlist-list-buffers-below (&optional arg)
"Display a list of existing buffers.
The list is displayed in a buffer named \"*Buffer List*\".
See `buffer-menu' for a description of the Buffer Menu.
By default, all buffers are listed except those whose names start
with a space (which are for internal use). With prefix argument
ARG, show only buffers that are visiting files."
(interactive "P")
(lawlist-display-buffer-below (list-buffers-noselect arg) nil))
(defun lawlist-display-buffer-below (buffer alist)
(let (
(window
(cond
((get-buffer-window buffer (selected-frame)))
((window-in-direction 'below))
(t
(split-window (selected-window) nil 'below)))))
(window--display-buffer buffer window 'window alist display-buffer-mark-dedicated)))
In Emacs 21.x I don't know if via a specific customization of split-window or due to a different default behaviour by Emacs, invoking the split-window-below besides splitting the window, it switched the buffer in the non-focused window to the next buffer.
Currently (Emacs 24.x), the split-window and siblings split-window-below and split-window-right don't seem to allow such a customization. Is this true?
If so, how to tweak Emacs to have this behaviour? Redefining split-window or split-window-below and split-window-right to have an extra step of switching to the next on the non-focused window. This could be done with advices:
(defun split-window-and-next-buffer (new-window)
(let ((old-window (selected-window)))
(select-window new-window)
(next-buffer)
(select-window old-window)
new-window))
(defadvice split-window-right (after split-window-right-and-next-buffer
activate protect compile)
(split-window-and-next-buffer ad-return-value))
(defadvice split-window-below (after split-window-bellow-and-next-buffer
activate protect compile)
(split-window-and-next-buffer ad-return-value))
With the corrections indicated by lawlist which are already available above the advices already work and I get the intended behaviour, but it isn't customizable to have the old behaviour.
In response to the question, the original poster might want try changing the spelling of the word below within the code posted.
This function adds three lines of code (at the end) to the current version of Emacs Trunk split-window-below and renames the function to lawlist-split-window-below with a defalias. One closing parentheses was moved to the end of the function to permit using two of the let bindings defined farther up in the function. If the user wants focus in the new-window (after exiting the function) instead, then just remove the last line of code (select-window old-window).
(defun lawlist-split-window-below (&optional size)
"Split the selected window into two windows, one above the other.
The selected window is above. The newly split-off window is
below, and displays the 'next-buffer'. Return the new window.
If optional argument SIZE is omitted or nil, both windows get the
same height, or close to it. If SIZE is positive, the upper
\(selected) window gets SIZE lines. If SIZE is negative, the
lower (new) window gets -SIZE lines.
If the variable `split-window-keep-point' is non-nil, both
windows get the same value of point as the selected window.
Otherwise, the window starts are chosen so as to minimize the
amount of redisplay; this is convenient on slow terminals."
(interactive "P")
(let ((old-window (selected-window))
(old-point (window-point))
(size (and size (prefix-numeric-value size)))
moved-by-window-height moved new-window bottom)
(when (and size (< size 0) (< (- size) window-min-height))
;; `split-window' would not signal an error here.
(error "Size of new window too small"))
(setq new-window (split-window nil size))
(unless split-window-keep-point
(with-current-buffer (window-buffer)
;; Use `save-excursion' around vertical movements below
;; (Bug#10971). Note: When the selected window's buffer has a
;; header line, up to two lines of the buffer may not show up
;; in the resulting configuration.
(save-excursion
(goto-char (window-start))
(setq moved (vertical-motion (window-height)))
(set-window-start new-window (point))
(when (> (point) (window-point new-window))
(set-window-point new-window (point)))
(when (= moved (window-height))
(setq moved-by-window-height t)
(vertical-motion -1))
(setq bottom (point)))
(and moved-by-window-height
(<= bottom (point))
(set-window-point old-window (1- bottom)))
(and moved-by-window-height
(<= (window-start new-window) old-point)
(set-window-point new-window old-point)
(select-window new-window)))
;; Always copy quit-restore parameter in interactive use.
(let ((quit-restore (window-parameter old-window 'quit-restore)))
(when quit-restore
(set-window-parameter new-window 'quit-restore quit-restore)))
new-window)
(select-window new-window)
(next-buffer)
(select-window old-window)))
(defalias 'split-window-below 'lawlist-split-window-below)