From 9c842bfad8f34edede4fdc536eea4aaf09569a78 Mon Sep 17 00:00:00 2001 From: Henrik Lissner Date: Mon, 17 Jun 2019 19:18:51 +0200 Subject: [PATCH] Open term popups in current dir, instead of project-root CDing into the project root can be accomplished with external tools, e.g. cd $(git rev-parse --show-toplevel) cd $(hg root) cd $(npm root) Any of which could be aliased. Also, +vterm/toggle and term/toggle define the PROOT environment variable, so `cd $PROOT` will work too. On the other hand, CDing to the current file/folder requires that the shell be made aware of the file/directory of some Emacs state, which is a little trickier to deal with, so I made that the default behavior for +term/toggle, +vterm/toggle and +eshell/toggle. --- modules/term/eshell/autoload/eshell.el | 30 ++++-------------------- modules/term/term/autoload.el | 32 ++++++++++---------------- modules/term/vterm/autoload.el | 13 ++++++----- 3 files changed, 24 insertions(+), 51 deletions(-) diff --git a/modules/term/eshell/autoload/eshell.el b/modules/term/eshell/autoload/eshell.el index 6f5198002..c0d1a18b6 100644 --- a/modules/term/eshell/autoload/eshell.el +++ b/modules/term/eshell/autoload/eshell.el @@ -79,10 +79,7 @@ ;;;###autoload (defun +eshell/toggle (arg &optional command) - "Toggle eshell popup window at project's root. - -Changes the PWD to the PWD of the buffer this command is executed from a new -project (or if prefix ARG was present)." + "Toggle eshell popup window." (interactive "P") (let ((eshell-buffer (get-buffer-create @@ -90,7 +87,6 @@ project (or if prefix ARG was present)." (if (bound-and-true-p persp-mode) (safe-persp-name (get-current-persp)) "main")))) - (target-project (or (doom-project-root) default-directory)) confirm-kill-processes current-prefix-arg) (when arg @@ -114,30 +110,16 @@ project (or if prefix ARG was present)." (if (eq major-mode 'eshell-mode) (run-hooks 'eshell-mode-hook) (eshell-mode)) - (let ((old-project (doom-project-root))) - (when (and old-project - target-project - (not (file-equal-p old-project target-project))) - (setq default-directory target-project) - (with-silent-modifications - (goto-char (point-max)) - (when (re-search-backward eshell-prompt-regexp nil t) - (delete-region (match-end 0) (point-max))) - (eshell-send-input)))) (when command (+eshell-run-command command eshell-buffer)))))) ;;;###autoload -(defun +eshell/here (arg &optional command) +(defun +eshell/here (&optional command) "Open eshell in the current buffer." (interactive "P") (when (eq major-mode 'eshell-mode) (user-error "Already in an eshell buffer")) - (let* ((default-directory - (if arg - default-directory - (or (doom-project-root) default-directory))) - (buf (+eshell--unused-buffer))) + (let ((buf (+eshell--unused-buffer))) (with-current-buffer (switch-to-buffer buf) (if (eq major-mode 'eshell-mode) (run-hooks 'eshell-mode-hook) @@ -147,14 +129,12 @@ project (or if prefix ARG was present)." buf)) ;;;###autoload -(defun +eshell/frame (arg &optional command) +(defun +eshell/frame (&optional command) "Open a frame dedicated to eshell. Once the eshell process is killed, the previous frame layout is restored." (interactive "P") - (let ((default-directory (or (if arg default-directory (doom-project-root)) - default-directory)) - (buf (+eshell--unused-buffer 'new))) + (let ((buf (+eshell--unused-buffer 'new))) (unless (frame-parameter nil 'saved-wconf) (set-frame-parameter nil 'saved-wconf (current-window-configuration))) (delete-other-windows) diff --git a/modules/term/term/autoload.el b/modules/term/term/autoload.el index 4b095b430..4d4ddb491 100644 --- a/modules/term/term/autoload.el +++ b/modules/term/term/autoload.el @@ -9,15 +9,13 @@ ;;;###autoload (defun +term/toggle (arg) - "Toggle a persistent terminal popup window at project's root. + "Toggle a persistent terminal popup window. If popup is visible but unselected, select it. - -If prefix ARG, recreate term buffer in the current project's root." +If prefix ARG, recreate the term buffer." (interactive "P") (require 'multi-term) - (let ((default-directory (or (doom-project-root) default-directory)) - (multi-term-dedicated-select-after-open-p t) + (let ((multi-term-dedicated-select-after-open-p t) (multi-term-dedicated-buffer-name (format "doom:term-popup:%s" (if (bound-and-true-p persp-mode) @@ -36,6 +34,7 @@ If prefix ARG, recreate term buffer in the current project's root." (when (bound-and-true-p evil-local-mode) (evil-change-to-initial-state)) (goto-char (point-max))) + (setenv "PROOT" (or (doom-project-root) default-directory)) (with-current-buffer buffer (doom|mark-buffer-as-real) (multi-term-internal)) @@ -46,18 +45,11 @@ If prefix ARG, recreate term buffer in the current project's root." (select-window window))))))) ;;;###autoload -(defun +term/here (arg) - "Open a terminal buffer in the current window at project's root. - -If prefix ARG is non-nil, cd into `default-directory' instead of the project -root." - (interactive "P") - (let ((default-directory - (if arg - default-directory - (or (doom-project-root) default-directory)))) - ;; Doom's switch-buffer hooks prevent themselves from triggering when - ;; switching from buffer A back to A. Because `multi-term' uses `set-buffer' - ;; before `switch-to-buffer', the hooks don't trigger, so we use this - ;; roundabout way to trigger them properly. - (switch-to-buffer (save-window-excursion (multi-term))))) +(defun +term/here () + "Open a terminal buffer in the current window." + (interactive) + ;; Doom's switch-buffer hooks prevent themselves from triggering when + ;; switching from buffer A back to A. Because `multi-term' uses `set-buffer' + ;; before `switch-to-buffer', the hooks don't trigger, so we use this + ;; roundabout way to trigger them properly. + (switch-to-buffer (save-window-excursion (multi-term)))) diff --git a/modules/term/vterm/autoload.el b/modules/term/vterm/autoload.el index bb3b8e645..5305493b4 100644 --- a/modules/term/vterm/autoload.el +++ b/modules/term/vterm/autoload.el @@ -8,10 +8,11 @@ If prefix ARG is non-nil, recreate vterm buffer in the current project's root." (interactive "P") (unless (fboundp 'module-load) (user-error "Your build of Emacs lacks dynamic modules support and cannot load vterm")) - (let ((buffer-name (format "*doom:vterm-popup:%s*" - (if (bound-and-true-p persp-mode) - (safe-persp-name (get-current-persp)) - "main"))) + (let ((buffer-name + (format "*doom:vterm-popup:%s*" + (if (bound-and-true-p persp-mode) + (safe-persp-name (get-current-persp)) + "main"))) confirm-kill-processes current-prefix-arg) (when arg @@ -29,8 +30,8 @@ If prefix ARG is non-nil, recreate vterm buffer in the current project's root." (evil-change-to-initial-state)) (goto-char (point-max))) (require 'vterm) - (let* ((default-directory (or (doom-project-root) default-directory)) - (buffer (get-buffer-create buffer-name))) + (setenv "PROOT" (or (doom-project-root) default-directory)) + (let ((buffer (get-buffer-create buffer-name))) (with-current-buffer buffer (doom|mark-buffer-as-real) (vterm-mode))