jixiuf / vterm-toggle

toggles between the vterm buffer and whatever buffer you are editing.
GNU General Public License v3.0
188 stars 12 forks source link

Consider projectile integration? #11

Closed ianyepan closed 4 years ago

ianyepan commented 4 years ago

Thanks for the awesome package! I've been really enjoying using it. I also appreciate the intelligence of vterm-toggle-cd and vterm-toggle-cd-auto-create-buffer.

I have a proposal that I think you might be highly interested in:

Proposal

Oftentimes Emacs users have separated "projects" open at the same time. (Using projectile to manage each project is really a breeze, especially commands like projectile-find-file can limit the scope and fuzzy search files only in the current project). I think it'd be brilliant if we can somehow get vterm-toggle to "recognize" the current project we're in, and decide "whether or not" to create a new vterm instance, or instead re-use an existing vterm buffer that's within the project scope.

Let me know if something is unclear. I wish my Emacs Lisp skills are good enough that I could provide a PR, but truth is at this stage I can only raise suggestions.

ianyepan commented 4 years ago

Good news! There's the command projectile-run-vterm, but it currently has no integration with the toggle functionality, and it always takes up the whole buffer view (can't specify height). I feel like it's not hard to implement given that projectile-run-vterm is already written.

ianyepan commented 4 years ago

I hacked on this for a bit and here's what I got:

(defun vterm-toggle-projectile (&optional arg)
  (interactive "P")
  (if (derived-mode-p 'vterm-mode)
      (delete-window)
    (let* ((project (projectile-ensure-project (projectile-project-root)))
           (buffer (projectile-generate-process-name "vterm" arg)))
      (unless (buffer-live-p (get-buffer buffer))
        (projectile-with-default-dir project
          (vterm buffer)))
      (progn
        (vterm-toggle)
        (switch-to-buffer buffer)))))

It works 95% of the time. But sometimes upon spawning a new terminal instance for a newly-switched project, the vterm buffer appears to be full screen, despite vterm-toggle-fullscreen-p being set to nil.

Here's my full vterm-toggle config at the moment:

(use-package vterm-toggle
  :after (projectile vterm evil)
  :preface
  (defun vterm-toggle-projectile (&optional arg)
    (interactive "P")
    (if (derived-mode-p 'vterm-mode)
        (delete-window)
      (let* ((project (projectile-ensure-project (projectile-project-root)))
             (buffer (projectile-generate-process-name "vterm" arg)))
        (unless (buffer-live-p (get-buffer buffer))
          (projectile-with-default-dir project
            (vterm buffer)))
        (progn
          (vterm-toggle)
          (switch-to-buffer buffer)))))
  :config
  (setq vterm-toggle-fullscreen-p nil)
  (with-eval-after-load 'evil
    (evil-set-initial-state 'vterm-mode 'emacs))
  ;; (global-set-key (kbd "C-`") #'vterm-toggle-cd)
  (global-set-key (kbd "C-`") #'vterm-toggle-projectile)
  (add-to-list 'display-buffer-alist
               '("^.?v?term.*"
                 (display-buffer-reuse-window display-buffer-at-bottom)
                 (reusable-frames . visible)
                 (window-height . 0.5))))
jixiuf commented 4 years ago

try

(setq vterm-toggle-scope 'projectile)

and just use vterm-toggle or vterm-toggle-cd

ianyepan commented 4 years ago

Thanks for getting back. However, this does not solve my issue. I wanted to achieve having a dedicated vterm buffer for each projectile project by invoking vterm-toggle. Reuse the current one if one exists, or spawn a new one for this projectile project.

Edit: Ah I noticed you pushed a new commit, I'll wait for MELPA to update and test it out.

ianyepan commented 4 years ago

@jixiuf First of all thanks for pushing that commit!

I just tested it out and I found one inconsistency: although vterm-toggle now intelligently reuses the vterm buffer in the current projectile scope (or creates a new one if it doesn't exist), the initial root is sometimes not the projectile root directory, instead, it's the subdirectory of whichever the current buffer is in. Suppose I have a file buffer open ~/Projects/CoolProject1/src/main.js where ~/Projects/CoolProject1 is the root directory, I expect the vterm instance I spawn from main.js to be starting at ~/Projects/CoolProject1, but it starts at ~/Projects/CoolProject1/src/, which is where main.js resides. This behaviour is similar to that of vterm-toggle-cd, but I wish to have a behaviour of something like "vterm-toggle-projectile-root".

One workaround that I found is to create the first-ever vterm instance in each project using projectile-run-vterm, then afterwards always use vterm-toggle to correctly identify which vterm buffer it should use depending on the project scope. But I wonder if we can achieve this behaviour automatically without users having to run projectile-run-vterm.

jixiuf commented 4 years ago

new option vterm-toggle-projectile-root is added.

ianyepan commented 4 years ago

Thanks a ton @jixiuf!! I just tested it, and your latest commit fixes my exact problem! I'll close this issue for now, thanks again for the patience.