Skip to content

Commit

Permalink
refactor(core): reuse hidden session
Browse files Browse the repository at this point in the history
  • Loading branch information
alexluigit committed Jun 13, 2022
1 parent 553cab6 commit cc0c04f
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/melpazoid.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ jobs:
"extensions/dirvish-subtree.el"
"extensions/dirvish-icons.el"))
# set this to false (or remove it) if the package isn't on MELPA:
EXIST_OK: false
# EXIST_OK: false
run: echo $GITHUB_REF && make -C ~/melpazoid
25 changes: 20 additions & 5 deletions dirvish.el
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,19 @@ ALIST is window arguments passed to `window--display-buffer'."
(and (tramp-get-method-parameter vec 'tramp-direct-async)
(tramp-get-connection-property vec "direct-async-process" nil)))))

(defun dirvish--reuse-session (file)
"Reuse the first hidden Dirvish session and find FILE in it."
(when dirvish-keep-alive-on-quit
(cl-loop for dv-name in (dirvish-get-all 'name nil t)
for dv = (gethash dv-name dirvish--hash)
for index-dir = (dv-index-dir dv)
for index-buf = (alist-get index-dir (dv-roots dv) nil nil #'equal)
thereis (and (not (get-buffer-window index-buf))
(eq (dv-quit-window-fn dv) #'ignore)
(prog1 (switch-to-buffer index-buf)
(dirvish-reclaim)
(dirvish-find-file file))))))

;;;; Core

(defun dirvish-curr (&optional frame)
Expand Down Expand Up @@ -649,7 +662,6 @@ If KEEP-CURRENT, do not kill the current directory buffer."
(when (dv-layout dv)
(set-window-configuration (dv-window-conf dv))
(goto-char (plist-get (dv-scopes dv) :point))
(mapc #'kill-live (mapcar #'cdr (dv-roots dv)))
(remhash (dv-name dv) dirvish--hash))
(if keep-current
(progn (mapc #'kill-live (seq-remove (lambda (i) (eq i (current-buffer)))
Expand Down Expand Up @@ -799,11 +811,14 @@ DIRNAME and SWITCHES are the same args in `dired'."
"Override `dired-jump' command.
OTHER-WINDOW and FILE-NAME are the same args in `dired-jump'."
(interactive
(list nil (and current-prefix-arg (read-file-name "Dirvish jump to: "))))
(if (and (dirvish-curr) (not other-window))
(dirvish-find-file file-name)
(list nil (and current-prefix-arg (read-directory-name "Dirvish jump to: "))))
(let ((file-name (expand-file-name (or file-name default-directory))))
(and other-window (switch-to-buffer-other-window (dirvish--util-buffer)))
(dirvish-new t :path (or file-name default-directory))))
(if (dirvish-curr)
(dirvish-find-file file-name)
(dirvish--reuse-session file-name)
(unless (dirvish-prop :dv)
(dirvish-new t :path file-name)))))

(defun dirvish-find-file-other-win-ad (&rest _)
"Override `dired-find-file-other-window' command."
Expand Down
10 changes: 7 additions & 3 deletions extensions/dirvish-extras.el
Original file line number Diff line number Diff line change
Expand Up @@ -366,9 +366,13 @@ current layout defined in `dirvish-layout-recipes'."
(defun dirvish-dwim (&optional path)
"Start a Dirvish session with optional PATH.
The session takes the whole frame when `one-window-p'."
(interactive (list (and current-prefix-arg (read-file-name "Dirvish: "))))
(dirvish-new t :path (or path default-directory)
:layout (and (one-window-p) dirvish-default-layout)))
(interactive (list (and current-prefix-arg (read-directory-name "Dirvish: "))))
(let ((path (expand-file-name (or path default-directory))))
(dirvish--reuse-session path)
(if (dirvish-prop :dv)
(and (one-window-p) (dirvish-toggle-fullscreen))
(dirvish-new t :path path
:layout (and (one-window-p) dirvish-default-layout)))))

(provide 'dirvish-extras)
;;; dirvish-extras.el ends here
7 changes: 5 additions & 2 deletions extensions/dirvish-side.el
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,11 @@ will visit the latest `project-root' after executing
(dirvish-reclaim)
(when-let ((dv (dirvish-curr))) (dirvish-kill dv)))

(defun dirvish-side-quit-window-fn (_dv)
"Quit window action for `dirvish-side'."
(defun dirvish-side-quit-window-fn (dv)
"Quit window action for `dirvish-side' session DV."
(let (kill-buffer-hook)
(mapc #'kill-buffer (mapcar #'cdr (dv-roots dv))))
(remhash (dv-name dv) dirvish--hash)
(dirvish-side--set-state nil 'uninitialized))

(defun dirvish-side-root-window-fn (dv)
Expand Down

0 comments on commit cc0c04f

Please sign in to comment.