Add dirvish configuration

Signed-off-by: Magic_RB <magic_rb@redalder.org>
This commit is contained in:
Magic_RB 2023-09-16 19:30:27 +02:00
parent 640de3fb86
commit 2ed6b1fc6e

View file

@ -8,8 +8,139 @@
#
# SPDX-License-Identifier: LGPL-3.0-or-later
#+begin_src emacs-lisp
First install [[https://github.com/alexluigit/dirvish][dirvish]], a improved version of dired.
#+begin_src emacs-lisp :tangle no
(use-package dirvish
:straight t)
#+end_src
#+begin_src emacs-lisp :noweb yes :exports none
(use-package dirvish
:straight t
:init
(dirvish-override-dired-mode)
:custom
<<dirvish-quick-access-entries>>
:config
;; (dirvish-peek-mode) ; Preview files in minibuffer
;; (dirvish-side-follow-mode) ; similar to `treemacs-follow-mode'
(setq dirvish-mode-line-format
'(:left (sort symlink) :right (omit yank index)))
<<dirvish-attributes>>
(setq delete-by-moving-to-trash t)
(setq dired-listing-switches
"-l --all --human-readable --group-directories-first --no-group")
<<dirvish-side-obey-display-actions-fix>>
:bind ; Bind `dirvish|dirvish-side|dirvish-dwim' as you see fit
<<dirvish-bindings>>)
#+end_src
#+RESULTS:
: dirvish-fd-jump
Add frequently visited diretories, they're accessible from the ~dirvish-mode-map~ under the prefix ~a~.
#+name: dirvish-quick-access-entries
#+begin_src emacs-lisp :tangle no
(dirvish-quick-access-entries ; It's a custom option, `setq' won't work
'(("h" "~/" "Home")
("d" "~/Downloads/" "Downloads")
("m" "/mnt/" "Drives")
("t" "~/.local/share/Trash/files/" "TrashCan")
("r" "~/roam/")
("s" "~/sync")))
#+end_src
Enable more attributes in dirvish buffers.
#+name: dirvish-attributes
#+begin_src emacs-lisp :tangle no
(setq dirvish-attributes
'(all-the-icons file-time file-size collapse subtree-state vc-state git-msg))
#+end_src
#+begin_src emacs-lisp
(defun dirvish-fd-projectile ()
(interactive)
(funcall-interactively #'dirvish-fd (projectile-project-root) nil))
#+end_src
Next fill up the ~dirvish-mode-map~ and make dirvish reachable under the ~C-c d~ prefix.
#+name: dirvish-bindings
#+begin_src emacs-lisp :tangle no
(("C-c d f" . dirvish-fd-projectile)
("C-c d s" . dirvish-side)
:map dirvish-mode-map ; Dirvish inherits `dired-mode-map'
("a" . dirvish-quick-access)
("f" . dirvish-file-info-menu)
("y" . dirvish-yank-menu)
("N" . dirvish-narrow)
("^" . dirvish-history-last)
("h" . dirvish-history-jump) ; remapped `describe-mode'
("s" . dirvish-quicksort) ; remapped `dired-sort-toggle-or-edit'
("v" . dirvish-vc-menu) ; remapped `dired-view-file'
("w" . dirvish-apply-wallpaper)
("TAB" . dirvish-subtree-toggle)
("M-f" . dirvish-history-go-forward)
("M-b" . dirvish-history-go-backward)
("M-l" . dirvish-ls-switches-menu)
("M-m" . dirvish-mark-menu)
("M-t" . dirvish-layout-toggle)
("M-s" . dirvish-setup-menu)
("M-e" . dirvish-emerge-menu)
("M-j" . dirvish-fd-jump))
#+end_src
Dirvish has an issue with ~switch-to-buffer-obey-display-actions~, so advise ~switch-to-buffer~ and force override ~switch-to-buffer-obey-display-actions~ to ~nil~ when dealing with a Dirvish side buffer.
#+name: dirvish-side-obey-display-actions-fix
#+begin_src emacs-lisp :tangle no
(defun dirvish-side-buffer-p (buffer-or-name)
"Return t if BUFFER-OR-NAME is a dirvish buffer and a side buffer."
(with-current-buffer buffer-or-name
(let ((dv (dirvish-curr)))
(if (and dv (member 'side (dv-type dv)))
t
nil))))
(defun dirvish-ignore-display-buffer (original-function buffer-or-name &rest original-arguments)
(let ((switch-to-buffer-obey-display-actions (not (dirvish-side-buffer-p buffer-or-name))))
(apply original-function buffer-or-name original-arguments)))
(advice-add 'switch-to-buffer :around #'dirvish-ignore-display-buffer)
#+end_src
In sideview Dirvish buffers, I want the ~listing-switches~ to exclude ~--all~ and instead use ~--almost-all~, that causes ~..~ and ~.~ to be omitted.
#+begin_src emacs-lisp
(defun dirvish-side-disable-listing-all (&rest args)
(with-selected-window (dv-root-window dirvish--this)
(setq dired-actual-switches (string-replace "--all" "--almost-all" dired-actual-switches))
(revert-buffer)))
(advice-add 'dirvish-side--new :after #'dirvish-side-disable-listing-all)
#+end_src
In sideview Dirvish buffers, I don't want ~RET~ to narrow to a directory, it makes no sense in my opinion.
#+begin_src emacs-lisp
(defun dirvish-side-disable-open-folder (original-function &optional entry)
(let ((entry (or entry (dired-get-filename nil t))))
(if (dirvish-side-buffer-p (current-buffer))
(unless (and entry (file-directory-p entry))
(funcall original-function entry))
(funcall original-function entry))))
(advice-add 'dirvish-find-entry-a :around #'dirvish-side-disable-open-folder)
#+end_src
#+begin_src emacs-lisp
(defun dirvish-apply-wallpaper ()
(interactive)
(let ((filename (or (dired-get-filename) (completing-read))))
(async-shell-command (format "feh %s" filename))))
(defun dirvish-side-obey-display-actions (original-function &rest original-arguments)
(let ((switch-to-buffer-obey-display-actions nil))
(apply original-function original-arguments)))
(advice-add 'dirvish-side :around #'dirvish-side-obey-display-actions)
#+end_src