init-dired.el 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. ;;; Dired
  2. ;;; WARNING: This file is loaded unconditionally on startup.
  3. ;;; We cannot assume that current buffer is in dired-mode.
  4. (ambrevar/define-keys dired-mode-map
  5. "C-c h" 'ambrevar/dired-toggle-humansize
  6. "<left>" 'dired-up-directory
  7. "<right>" 'dired-find-file
  8. "SPC" 'dired-mark
  9. "<backspace>" 'dired-up-directory
  10. "b" 'dired-up-directory)
  11. (when (require 'dired+ nil t)
  12. (toggle-diredp-find-file-reuse-dir 1))
  13. ;;; On a GNU system, ls has the option to sort folders first.
  14. (if (string-match "^gnu.*" (prin1-to-string system-type))
  15. (setq dired-listing-switches "--group-directories-first -lha")
  16. (setq dired-listing-switches "-lha"))
  17. ;;; Switches are set before the hook is called, so we need to reload dired. The
  18. ;;; dired-internal-noselect is a lower level function, so it is faster. WARNING:
  19. ;;; Not sure if it is equivalent though.
  20. ;; (dired dired-directory dired-listing-switches)
  21. (defun ambrevar/dired-set-listing-switches ()
  22. (dired-internal-noselect dired-directory dired-listing-switches))
  23. (setq wdired-allow-to-change-permissions t)
  24. ;;; omit-mode needs to be started _after_ omit-files redefinition.
  25. (require 'dired-x)
  26. (setq dired-omit-files "^\\.")
  27. (setq dired-guess-shell-alist-user
  28. (list
  29. '("\\.ogg$" "mpv")
  30. '("\\.\\(jpe?g\\|png\\|git\\)$" "sxiv")
  31. '("\\.\\(mkv\\|mpe?g\\|avi\\|mp4\\|ogm\\)$" "mpv")))
  32. (defvar ambrevar/dired-showing-humansize t "If dired is displaying humansize or not.")
  33. (defun ambrevar/dired-toggle-humansize ()
  34. "Toggle displaying humansize in dired."
  35. (interactive)
  36. (let ((switch-regexp "\\(\\`\\| \\)-\\([a-gi-zA-Z]*\\)\\(h\\)\\([^ ]*\\)")
  37. case-fold-search)
  38. (while (string-match switch-regexp dired-actual-switches)
  39. (if (and (equal (match-string 2 dired-actual-switches) "")
  40. (equal (match-string 4 dired-actual-switches) ""))
  41. (setq dired-actual-switches
  42. (replace-match "" t t dired-actual-switches))
  43. (setq dired-actual-switches
  44. (replace-match "" t t dired-actual-switches 3))))
  45. (if ambrevar/dired-showing-humansize
  46. (setq ambrevar/dired-showing-humansize nil)
  47. (progn
  48. (setq dired-actual-switches
  49. (concat dired-actual-switches
  50. (if (string-match-p "\\`-[[:alnum:]]+\\'"
  51. dired-actual-switches)
  52. "h" " -h")))
  53. (setq ambrevar/dired-showing-humansize t))))
  54. (revert-buffer))
  55. (dolist (fun '(dired-omit-mode ambrevar/dired-set-listing-switches))
  56. (add-hook 'dired-mode-hook fun))
  57. (when (require 'dired-du nil t)
  58. (setq dired-du-size-format t)
  59. ;; dired-du needs some adjustments with a custom TIME_STYLE.
  60. (when (getenv "TIME_STYLE")
  61. (let* ((yyyy "[0-9][0-9][0-9][0-9]")
  62. (HH:MM "[ 0-2][0-9][:.][0-5][0-9]")
  63. (seconds "[0-6][0-9]\\([.,][0-9]+\\)?")
  64. (iso-mm-dd "[01][0-9]-[0-3][0-9]")
  65. (zone "[-+][0-2][0-9][0-5][0-9]")
  66. (iso-time (concat HH:MM "\\(:" seconds "\\( ?" zone "\\)?\\)?"))
  67. (iso (concat "\\(\\(" yyyy "-\\)?" iso-mm-dd "[ T]" iso-time
  68. "\\|" yyyy "-" iso-mm-dd "\\)")))
  69. (setq directory-listing-before-filename-regexp
  70. (concat "\\([0-9][BkKMGTPEZY]? "
  71. "|" iso "|"
  72. "\\) +")))))
  73. (when (executable-find "sxiv")
  74. (setq image-dired-external-viewer "sxiv"))
  75. (defun ambrevar/image-dired-setup ()
  76. (add-hook 'window-configuration-change-hook 'image-dired-line-up-dynamic nil t))
  77. (add-hook 'image-dired-thumbnail-mode-hook 'ambrevar/image-dired-setup)
  78. (provide 'init-dired)