dotfiles/emacs-lisp/dirvish.org
magic_rb e01811711e
Update dirvish to git main, due to nerd-icons support
Signed-off-by: magic_rb <magic_rb@redalder.org>
2023-12-14 15:06:31 +01:00

5.6 KiB

Dirvish

#

First install dirvish, a improved version of dired.

  (use-package dirvish
    :straight t)

Add frequently visited diretories, they're accessible from the dirvish-mode-map under the prefix a.

  (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")))

Enable more attributes in dirvish buffers.

  (setq dirvish-attributes
        '(nerd-icons file-time file-size collapse subtree-state vc-state git-msg))
  (defun dirvish-fd-projectile ()
    (interactive)
    (funcall-interactively #'dirvish-fd (projectile-project-root) nil))

Next fill up the dirvish-mode-map and make dirvish reachable under the C-c d prefix.

  (("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))

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.

  (defun dirvish-side-buffer-p (buffer-or-name)
    "Return t if BUFFER-OR-NAME is a dirvish buffer and a side buffer."
    (when (get-buffer buffer-or-name)
      (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)

In sideview Dirvish buffers, I want the listing-switches to exclude --all and instead use --almost-all, that causes .. and . to be omitted.

  (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)

In sideview Dirvish buffers, I don't want RET to narrow to a directory, it makes no sense in my opinion.

  (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)
  (defun dirvish-apply-wallpaper ()
    (interactive)
    (let ((filename (or (dired-get-filename) (completing-read))))
      (async-shell-command (format "ln -sf %s /nix/persist/home/main/.config/wallpaper && systemctl restart --user wallpaper" 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)