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.
This commit is contained in:
Henrik Lissner 2019-06-17 19:18:51 +02:00
parent 2835314022
commit 9c842bfad8
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395
3 changed files with 24 additions and 51 deletions

View file

@ -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)

View file

@ -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))))
(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)))))
(switch-to-buffer (save-window-excursion (multi-term))))

View file

@ -8,7 +8,8 @@ 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*"
(let ((buffer-name
(format "*doom:vterm-popup:%s*"
(if (bound-and-true-p persp-mode)
(safe-persp-name (get-current-persp))
"main")))
@ -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))