dotfiles/emacs-lisp/dirvish.org
MagicRB c21340b577
Fix dirvish wallpaper apply command
Signed-off-by: MagicRB <richard@brezak.sk>
2023-10-03 23:48:27 +02:00

150 lines
5.5 KiB
Org Mode

:PROPERTIES:
:ID: 67919dd9-2aa0-4b89-8c96-0441a54e7b03
:END:
#+title: Dirvish
#+filetags: emacs-load
# SPDX-FileCopyrightText: 2022 Richard Brežák <richard@brezak.sk>
#
# SPDX-License-Identifier: LGPL-3.0-or-later
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."
(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)
#+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 "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)
#+end_src
#+RESULTS: