Here are four (4) sample custom display-buffer
family of functions that can be custom tailored to suit a user's particular needs -- above; below; left; right -- and here are four (4) interactive
functions to display the file or directory of the current line of a dired-mode
buffer. There are only three conditions: (a) if there is already a window displaying the target buffer, then choose it; (b) if there is a window in the direction desired that is available, then use it; (c) the catch-all is to create a new window in the desired direction if the other conditions are not met.
Usage:
M-x dired-display-above
M-x dired-display-below
M-x dired-display-left
M-x dired-display-right
There so many key-bindings already built-in to dired-mode
and dired+
, that I dare not attempt to make up my own. The user is free choose his/her own keyboard shortcuts, which is beyond the scope of this limited example.
The user is free to add additional conditions to the sample display-buffer
family of functions to handle more situations -- e.g., more windows than just a couple.
(defun my-display-buffer-below (buffer alist)
"Doc-string."
(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)
window))
(defun my-display-buffer-above (buffer alist)
"Doc-string."
(let (
(window
(cond
((get-buffer-window buffer (selected-frame)))
((window-in-direction 'above))
(t
(split-window (selected-window) nil 'above)))))
(window--display-buffer buffer window 'window alist display-buffer-mark-dedicated)
window))
(defun my-display-buffer-left (buffer alist)
"Doc-string."
(let (
(window
(cond
((get-buffer-window buffer (selected-frame)))
((window-in-direction 'left))
(t
(split-window (selected-window) nil 'left)))))
(window--display-buffer buffer window 'window alist display-buffer-mark-dedicated)
window))
(defun my-display-buffer-right (buffer alist)
"Doc-string."
(let (
(window
(cond
((get-buffer-window buffer (selected-frame)))
((window-in-direction 'right))
(t
(split-window (selected-window) nil 'right)))))
(window--display-buffer buffer window 'window alist display-buffer-mark-dedicated)
window))
(defun dired-display-above ()
"Doc-string."
(interactive)
(let* (
(file-or-dir (dired-get-file-for-visit))
(buffer (find-file-noselect file-or-dir)))
(my-display-buffer-above buffer nil)))
(defun dired-display-below ()
"Doc-string."
(interactive)
(let* (
(file-or-dir (dired-get-file-for-visit))
(buffer (find-file-noselect file-or-dir)))
(my-display-buffer-below buffer nil)))
(defun dired-display-left ()
"Doc-string."
(interactive)
(let* (
(file-or-dir (dired-get-file-for-visit))
(buffer (find-file-noselect file-or-dir)))
(my-display-buffer-left buffer nil)))
(defun dired-display-right ()
"Doc-string."
(interactive)
(let* (
(file-or-dir (dired-get-file-for-visit))
(buffer (find-file-noselect file-or-dir)))
(my-display-buffer-right buffer nil)))
EDIT: Here is a slightly more sophisticated / fun implementation of the above-concept, which gives the user the ability to use this non-interactively or interactively; e.g., M-x dired-display-buffer
-- where the user will be prompted to choose a directory if not hovering over a file in dired-mode
, and to choose a display direction (left, right, above, 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))
(defun dired-display-buffer (&optional direction alist)
"Display a dired-mode buffer or a file underneath point in a dired-mode buffer."
(interactive)
(let* ((file-or-dir (or (and (eq major-mode 'dired-mode) (dired-get-file-for-visit))
(read-directory-name "Directory: ")))
(buffer (find-file-noselect file-or-dir))
(direction
(if direction
direction
(let ((char (read-char-exclusive (concat
"["
(propertize "l" 'face '(:foreground "red"))
"]"
(propertize "eft" 'face '(:foreground "blue"))
" | ["
(propertize "r" 'face '(:foreground "red"))
"]"
(propertize "ight" 'face '(:foreground "blue"))
" | ["
(propertize "a" 'face '(:foreground "red"))
"]"
(propertize "bove" 'face '(:foreground "blue"))
" | ["
(propertize "b" 'face '(:foreground "red"))
"]"
(propertize "elow" 'face '(:foreground "blue"))))))
(cond
((eq char ?l)
'left)
((eq char ?r)
'right)
((eq char ?a)
'above)
((eq char ?b)
'below)
;;; FIXME: @lawlist may add a loop similar to `org-capture'
;;; whereby a new `read-char-exclusive' will be initiated if
;;; a user did not initially choose a valid option (l/r/a/b).
(t
(let ((debug-on-quit nil)
(msg (concat "dired-display-buffer: "
"You did not select l/r/a/b "
"-- exiting.")))
(signal 'quit `(,msg)))))))))
(my-display-buffer buffer alist direction)))
window
to refer to a buffer quadrant within the sameframe
. Aframe
is considered to be the whole kitten-kaboodle, which can have many windows within it. Emacs can generate multiple frames, with each frame containing multiple windows.