From a16c40d493c470c9d62e79224b00731ae4354c54 Mon Sep 17 00:00:00 2001 From: Henrik Lissner Date: Wed, 5 Aug 2020 21:13:42 -0400 Subject: [PATCH] tools/magit: split sub-windows to the side When a log or diff buffer is opened from the magit status buffer, it would display them in a random window (or split, if none were available). This changes forces it to predictably open to the right of the magit status window (using the next window over, if available). --- modules/tools/magit/autoload.el | 24 +++++++++++++++++++++++- modules/tools/magit/config.el | 8 ++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/modules/tools/magit/autoload.el b/modules/tools/magit/autoload.el index 03682c185..e23b88829 100644 --- a/modules/tools/magit/autoload.el +++ b/modules/tools/magit/autoload.el @@ -53,7 +53,29 @@ magit-diff-mode magit-stash-mode magit-status-mode)))) - '(display-buffer-same-window)))))) + '(display-buffer-same-window)) + + ('(+magit--display-buffer-in-direction)))))) + +(defun +magit--display-buffer-in-direction (buffer alist) + "`display-buffer-alist' handler that opens BUFFER in a direction. + +This differs from `display-buffer-in-direction' in one way: it will try to use a +window that already exists in that direction. It will split otherwise." + (let ((direction (or (alist-get 'direction alist) + +magit-open-windows-in-direction))) + (if-let (window (window-in-direction direction)) + (select-window window) + (if-let (window (window-in-direction + (pcase direction + (`right 'left) + (`left 'right) + ((or `up `above) 'down) + ((or `down `below) 'up)))) + (select-window window) + (split-window-horizontally))) + (switch-to-buffer buffer) + (selected-window))) ;; diff --git a/modules/tools/magit/config.el b/modules/tools/magit/config.el index 1ff772114..d5eb0ecba 100644 --- a/modules/tools/magit/config.el +++ b/modules/tools/magit/config.el @@ -1,5 +1,13 @@ ;;; tools/magit/config.el -*- lexical-binding: t; -*- +(defvar +magit-open-windows-in-direction 'right + "What direction to open new windows from the status buffer. +For example, diffs and log buffers. Accepts `left', `right', `up', and `down'.") + + +;; +;;; Packages + (use-package! magit :commands magit-file-delete :defer-incrementally (dash f s with-editor git-commit package eieio lv transient)