Skip to content

Commit

Permalink
Merge pull request doomemacs#5037 from sggutier/vterm-project-root
Browse files Browse the repository at this point in the history
Fix +vterm-toggle not changing directory to project root
  • Loading branch information
hlissner authored May 24, 2021
2 parents bba8196 + 582759e commit 6424b14
Showing 1 changed file with 46 additions and 37 deletions.
83 changes: 46 additions & 37 deletions modules/term/vterm/autoload.el
Original file line number Diff line number Diff line change
Expand Up @@ -6,57 +6,66 @@
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")))
confirm-kill-processes
current-prefix-arg)
(when arg
(let ((buffer (get-buffer buffer-name))
(window (get-buffer-window buffer-name)))
(when (buffer-live-p buffer)
(kill-buffer buffer))
(when (window-live-p window)
(delete-window window))))
(if-let (win (get-buffer-window buffer-name))
(if (eq (selected-window) win)
(delete-window win)
(select-window win)
(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))
(let ((buffer (get-buffer-create buffer-name)))
(with-current-buffer buffer
(unless (eq major-mode 'vterm-mode)
(vterm-mode))
(+vterm--change-directory-if-remote))
(pop-to-buffer buffer)))))
(+vterm--configure-project-root-and-display
arg
(lambda()
(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
(let ((buffer (get-buffer buffer-name))
(window (get-buffer-window buffer-name)))
(when (buffer-live-p buffer)
(kill-buffer buffer))
(when (window-live-p window)
(delete-window window))))
(if-let (win (get-buffer-window buffer-name))
(if (eq (selected-window) win)
(delete-window win)
(select-window win)
(when (bound-and-true-p evil-local-mode)
(evil-change-to-initial-state))
(goto-char (point-max)))
(let ((buffer (get-buffer-create buffer-name)))
(with-current-buffer buffer
(unless (eq major-mode 'vterm-mode)
(vterm-mode))
(+vterm--change-directory-if-remote))
(pop-to-buffer buffer)))))))

;;;###autoload
(defun +vterm/here (arg)
"Open a terminal buffer in the current window at project root.
If prefix ARG is non-nil, cd into `default-directory' instead of project root."
(interactive "P")
(+vterm--configure-project-root-and-display
arg
(lambda()
(require 'vterm)
;; This hack forces vterm to redraw, fixing strange artefacting in the tty.
(save-window-excursion
(pop-to-buffer "*scratch*"))
(let (display-buffer-alist)
(vterm vterm-buffer-name)))))

(defun +vterm--configure-project-root-and-display (arg display-fn)
"Sets the environment variable PROOT and displays a terminal using `display-fn`.
If prefix ARG is non-nil, cd into `default-directory' instead of project root."
(unless (fboundp 'module-load)
(user-error "Your build of Emacs lacks dynamic modules support and cannot load vterm"))
(require 'vterm)
;; This hack forces vterm to redraw, fixing strange artefacting in the tty.
(save-window-excursion
(pop-to-buffer "*scratch*"))
(let* ((project-root (or (doom-project-root) default-directory))
(default-directory
(if arg
default-directory
project-root))
display-buffer-alist)
project-root)))
(setenv "PROOT" project-root)
(vterm vterm-buffer-name)
(funcall display-fn)
(+vterm--change-directory-if-remote)))

(defun +vterm--change-directory-if-remote ()
Expand Down

0 comments on commit 6424b14

Please sign in to comment.