From b522e1aea3f6a34afc5ef0e4bdebc1d73f706b07 Mon Sep 17 00:00:00 2001 From: David Morgan Date: Mon, 20 Sep 2021 16:05:22 +0100 Subject: [PATCH] Kill empty perspective created by aborted project switching --- .emacs.d/lisp/init-project.el | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/.emacs.d/lisp/init-project.el b/.emacs.d/lisp/init-project.el index 71c1694..a8dd2eb 100644 --- a/.emacs.d/lisp/init-project.el +++ b/.emacs.d/lisp/init-project.el @@ -90,11 +90,20 @@ (let* ((persp-name (file-name-nondirectory (directory-file-name proj))) (persp (gethash persp-name (perspectives-hash)))) (unless (equal persp (persp-curr)) - ;; Create or switch to a perspective named after the project - (persp-switch persp-name) - ;; If the perspective did not exist, switch to the project - (when (not persp) - (project-switch-project proj))))) + (unwind-protect + (progn + ;; Create or switch to a perspective named after the project + (persp-switch persp-name) + ;; If the perspective did not exist, switch to the project + (when (not persp) + (project-switch-project proj))) + ;; If the only buffer is the persp scratch buffer, it's safe to kill the perspective if switching project was cancelled + (when (seq-empty-p + (seq-filter + (lambda (b) (not (equal (buffer-name b) (persp-scratch-buffer)))) + (persp-buffers (persp-curr)))) + (persp-kill persp-name)))))) + :bind ("C-x p p" . switch-project) ("C-x C-b" . persp-previous-buffer-same-mode)