From e15dc23bbe3a4cd815e90a7df10fbaa7f35d34a3 Mon Sep 17 00:00:00 2001 From: Debanjum Singh Solanky Date: Thu, 13 Jun 2024 08:02:33 +0530 Subject: [PATCH] Improve logic to create vs reuse window for khoj side pane logic Khoj side pane occupies a vertically split bottom right side pane. If the bottom right window is not a vertical split, create a new vertical split pane for khoj, otherwise reuse the existing window --- src/interface/emacs/khoj.el | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/src/interface/emacs/khoj.el b/src/interface/emacs/khoj.el index f18680ef..7a68707a 100644 --- a/src/interface/emacs/khoj.el +++ b/src/interface/emacs/khoj.el @@ -736,20 +736,27 @@ Render results in BUFFER-NAME using search results, CONTENT-TYPE and (optional) (defun khoj--open-side-pane (buffer-name) "Open Khoj BUFFER-NAME in right side pane." (if (get-buffer-window-list buffer-name) - ; if window is already open, switch to it + ;; if window is already open, switch to it (progn (select-window (get-buffer-window buffer-name)) (switch-to-buffer buffer-name)) ;; else if window is not open, open it as a right-side window pane - (progn - ;; Select the right-most window - (select-window (some-window (lambda (window) (window-at-side-p window 'right)))) - ;; Split the window to the right and resize it to take up 1/3 of the frame width - (let ((new-window (split-window-right))) - (set-window-buffer new-window buffer-name) - (window-resize new-window - (- (truncate (* 0.33 (frame-width))) (window-width)) - t))))) + (let ((bottomright-window (some-window (lambda (window) (and (window-at-side-p window 'right) (window-at-side-p window 'bottom)))))) + (progn + ;; Select the right-most window + (select-window bottomright-window) + ;; if bottom-right window is not a vertical pane, split it vertically, else use the existing bottom-right vertical window + (let ((khoj-window (if (window-at-side-p bottomright-window 'left) + (split-window-right) + bottomright-window))) + ;; Set the buffer in the khoj window + (set-window-buffer khoj-window buffer-name) + ;; Switch to the khoj window + (select-window khoj-window) + ;; Resize the window to 1/3 of the frame width + (window-resize khoj-window + (- (truncate (* 0.33 (frame-width))) (window-width)) + t)))))) (defun khoj--load-chat-session (buffer-name &optional session-id) "Load Khoj Chat conversation history into BUFFER-NAME." @@ -781,6 +788,7 @@ Render results in BUFFER-NAME using search results, CONTENT-TYPE and (optional) ;; create khoj chat shortcut keybindings (use-local-map (copy-keymap org-mode-map)) + (local-set-key (kbd "q") #'khoj--close) (local-set-key (kbd "m") #'khoj--chat) (local-set-key (kbd "C-x m") #'khoj--chat) @@ -788,6 +796,13 @@ Render results in BUFFER-NAME using search results, CONTENT-TYPE and (optional) (visual-line-mode) (read-only-mode t))))) +(defun khoj--close () + "Kill Khoj buffer and window" + (interactive) + (progn + (kill-buffer (current-buffer)) + (delete-window))) + (defun khoj--add-hover-text-to-footnote-refs (start-pos) "Show footnote defs on mouse hover on footnote refs from START-POS." (org-with-wide-buffer