Skip to content

Commit

Permalink
refactor(core): add dirvish-quit command
Browse files Browse the repository at this point in the history
  • Loading branch information
alexluigit committed Jul 21, 2022
1 parent 9e77452 commit 5662f7f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 32 deletions.
8 changes: 4 additions & 4 deletions CUSTOMIZING.org
Original file line number Diff line number Diff line change
Expand Up @@ -284,11 +284,11 @@ interference, which means ugly hacks is no longer needed.

If you still want to use the =dired= module in Doom, here are some caveats.

*** Remap/disable ~+dired/quit-all~ command (required)
*** Remap ~+dired/quit-all~ command (required)

After you enable the =dired= module, Doom remaps =q= key to a custom command
~+dired/quit-all~, which doesn't work well with Dirvish. You can either override
this function with ~quit-window~ or bind ~quit-window~ to =q= directly.
~+dired/quit-all~, which doesn't work well with Dirvish. You should use
~dirvish-quit~ instead (which is bound to =q= by default).

*** Use ~vc-state~ attribute instead of ~diff-hl-dired-mode~

Expand All @@ -313,7 +313,7 @@ same remote host, see: https://github.com/stsquad/dired-rsync/issues/24.

#+begin_src emacs-lisp
;; This is *NECESSARY* for Doom users who enabled `dired' module
;; (map! :map dired-mode-map :ng "q" #'quit-window)
;; (map! :map dired-mode-map :ng "q" #'dirvish-quit)

(use-package dirvish
:init
Expand Down
59 changes: 31 additions & 28 deletions dirvish.el
Original file line number Diff line number Diff line change
Expand Up @@ -636,17 +636,17 @@ restore them after."
(defun dirvish-kill (dv &optional keep-current)
"Kill a dirvish instance DV and remove it from `dirvish--hash'.
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))
(remhash (dv-name dv) dirvish--hash))
(if keep-current
(progn (mapc #'dirvish--kill-buffer (seq-remove (lambda (i) (eq i (current-buffer)))
(mapcar #'cdr (dv-roots dv))))
(setf (dv-roots dv) (list (dv-index-dir dv)))
(unless (dv-layout dv) (let (quit-window-hook) (quit-window))))
(mapc #'dirvish--kill-buffer (mapcar #'cdr (dv-roots dv)))
(remhash (dv-name dv) dirvish--hash))
(cond ((dv-layout dv)
(mapc #'dirvish--kill-buffer (mapcar #'cdr (dv-roots dv)))
(set-window-configuration (dv-window-conf dv))
(goto-char (plist-get (dv-scopes dv) :point))
(remhash (dv-name dv) dirvish--hash))
(keep-current (mapc #'dirvish--kill-buffer
(seq-remove (lambda (i) (eq i (current-buffer)))
(mapcar #'cdr (dv-roots dv))))
(setf (dv-roots dv) (list (dv-index-dir dv))))
(t (mapc #'dirvish--kill-buffer (mapcar #'cdr (dv-roots dv)))
(remhash (dv-name dv) dirvish--hash)))
(dolist (type '(preview header footer))
(dirvish--kill-buffer (dirvish--util-buffer type dv)))
(mapc #'dirvish--kill-buffer (dv-preview-buffers dv))
Expand Down Expand Up @@ -868,17 +868,18 @@ FILENAME and WILDCARD are their args."
(let* ((ext (downcase (or (file-name-extension filename) "")))
(file (expand-file-name filename))
(process-connection-type nil)
(ex-cmd (cl-loop
for (exts . (cmd . args)) in dirvish-open-with-programs
thereis (and (not (dirvish-prop :tramp))
(executable-find cmd)
(member ext exts)
(append (list cmd) args)))))
(cond (ex-cmd
(and (bound-and-true-p recentf-mode) (add-to-list 'recentf-list file))
(apply #'start-process "" nil "nohup"
(cl-substitute file "%f" ex-cmd :test 'string=)))
(t (when-let ((dv (dirvish-prop :dv))) (funcall (dv-on-file-open dv) dv))
(ex (cl-loop
for (exts . (cmd . args)) in dirvish-open-with-programs
thereis (and (not (dirvish-prop :tramp))
(executable-find cmd)
(member ext exts)
(append (list cmd) args)))))
(cond (ex (and (bound-and-true-p recentf-mode)
(add-to-list 'recentf-list file))
(apply #'start-process "" nil "nohup"
(cl-substitute file "%f" ex :test 'string=)))
(t (when-let ((dv (dirvish-prop :dv)))
(funcall (dv-on-file-open dv) dv))
(funcall fn file wildcard)))))

(defun dirvish-ignore-ad (fn &rest args)
Expand Down Expand Up @@ -1146,11 +1147,6 @@ default implementation is `find-args' with simple formatting."
(unless (memq this-cmd dirvish--no-update-preview-cmds)
(dirvish-preview-update))))))))

(defun dirvish-quit-window-h ()
"Hook function added to `quit-window' locally."
(dirvish-kill (dirvish-prop :dv) dirvish-reuse-session)
(switch-to-buffer (dirvish--util-buffer)))

(defun dirvish-kill-buffer-h ()
"Hook function added to `kill-buffer' locally."
(let ((dv (dirvish-prop :dv)))
Expand Down Expand Up @@ -1195,7 +1191,6 @@ Dirvish sets `revert-buffer-function' to this function."
(t (dv-header-line-format dv)))))
(add-hook 'window-buffer-change-functions #'dirvish-reclaim nil t)
(add-hook 'post-command-hook #'dirvish-update-body-h nil t)
(add-hook 'quit-window-hook #'dirvish-quit-window-h nil t)
(add-hook 'kill-buffer-hook #'dirvish-kill-buffer-h nil t)
(run-hooks 'dirvish-mode-hook)
(set-buffer-modified-p nil))
Expand Down Expand Up @@ -1436,6 +1431,7 @@ If VEC, the attributes are retrieved by parsing the output of
(defvar dirvish-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "?") 'dirvish-dispatch)
(define-key map (kbd "q") 'dirvish-quit)
map)
"Keymap used in a dirvish buffer.")

Expand All @@ -1445,6 +1441,13 @@ If VEC, the attributes are retrieved by parsing the output of

;;;; Commands

(defun dirvish-quit ()
"Quit current Dirvish session."
(interactive)
(let ((dv (dirvish-prop :dv)))
(dirvish-kill dv dirvish-reuse-session)
(and dirvish-reuse-session (not (dv-layout dv)) (quit-window))))

(defun dirvish-toggle-fullscreen ()
"Toggle fullscreen of current Dirvish."
(interactive)
Expand Down

0 comments on commit 5662f7f

Please sign in to comment.