From 5662f7ffad011144d5db2f9cfeb1cc24b1ff1548 Mon Sep 17 00:00:00 2001 From: Alex Date: Thu, 21 Jul 2022 14:36:52 +0800 Subject: [PATCH] refactor(core): add `dirvish-quit` command --- CUSTOMIZING.org | 8 +++---- dirvish.el | 59 ++++++++++++++++++++++++++----------------------- 2 files changed, 35 insertions(+), 32 deletions(-) diff --git a/CUSTOMIZING.org b/CUSTOMIZING.org index 69e36af4..cb7be4b8 100644 --- a/CUSTOMIZING.org +++ b/CUSTOMIZING.org @@ -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~ @@ -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 diff --git a/dirvish.el b/dirvish.el index 9c607437..3eba74d3 100644 --- a/dirvish.el +++ b/dirvish.el @@ -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)) @@ -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) @@ -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))) @@ -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)) @@ -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.") @@ -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)