summaryrefslogtreecommitdiff
path: root/lisp/init-dired.el
blob: 590536e6b99215c82f0186d11a0619226aa16216 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
(put 'dired-find-alternate-file 'disabled nil)

(require 'dired-x)

;; .git is present as part of `dired-omit-extensions', but this seems to
;; only be taken into account if a non-exension part exists.
(setq dired-omit-files
      (concat dired-omit-files
              "\\|^\\.git$\\|^\\.gitignore$"
              "\\|^__pycache__$\\|^\\.snakemake$"))

(defvar km/latex-omit-extensions '(".aux"
                                   ".fdb_latexmk"
                                   ".fls"
                                   ".log"
                                   ".nav"
                                   ".out"
                                   ".snm")
  "Intermediate LaTeX files")

(setq dired-omit-extensions
      (append dired-omit-extensions km/latex-omit-extensions))

(setq-default dired-omit-mode t)
(setq dired-listing-switches "-alh"
      dired-dwim-target t)

(setq dired-guess-shell-alist-user
        '(("\\.pdf\\'" "zathura")))

(setq dired-recursive-deletes t
      dired-recursive-copies t)

(defun km/dired-copy-project-filename-as-kill ()
  "Copy names of marked project files into kill ring.
This is similar to `dired-copy-filename-as-kill', but the leading
path is always relative to `projectile-project-root'."
  (interactive)
  (let ((project-dir (projectile-project-root)))
    (km/dired-copy-filename-relative-to-directory project-dir)))

(defun km/dired-copy-filename-relative-to-directory (directory)
  "Like `dired-copy-filename-as-kill', but the filename is always
relative to DIRECTORY."
  (let* ((string
          (mapconcat 'identity
                     (--map (file-relative-name it directory)
                            (dired-get-marked-files t))
                     " ")))
    (if (eq last-command 'kill-region)
        (kill-append string nil)
      (kill-new string))
    (message "%s" string)))

(defun km/dired-copy-relative-filename-as-kill ()
  "Copy names of marked files into kill ring.
This is similar to `dired-copy-filename-as-kill', but the leading
path is always relative to the `default-directory' of the other
window."
  (interactive)
  (let ((other-dir (km/other-default-directory)))
    (km/dired-copy-filename-relative-to-directory other-dir)))

(defun km/other-default-directory ()
  "Get `default-directory' for result of `(other-window 1)'."
  (save-window-excursion
    (other-window 1)
    default-directory))

(define-prefix-command 'km/dired-copy-filename-map)
(after 'projectile
  (define-key km/dired-copy-filename-map "o"
    'km/dired-copy-relative-filename-as-kill))
(define-key km/dired-copy-filename-map "w" 'dired-copy-filename-as-kill)
(define-key km/dired-copy-filename-map "p"
  'km/dired-copy-project-filename-as-kill)

;; This overrides the default binding for `dired-copy-filename-as-kill'.
(define-key dired-mode-map "w" 'km/dired-copy-filename-map)

(define-key dired-mode-map (kbd "C-c C-b") 'dired-up-directory)

(defun km/dired-switch-to-buffer ()
  (interactive)
  (let ((buffer-name (km/dired-completing-buffer)))
    (switch-to-buffer buffer-name)))

(defun km/dired-switch-to-buffer-other-window ()
  (interactive)
  (let ((buffer-name (km/dired-completing-buffer)))
    (pop-to-buffer buffer-name)))

(defun km/dired-completing-buffer ()
  (ido-completing-read "Dired buffer: "
                       (-map 'buffer-name (km/dired-buffer-list))))

(defun km/dired-buffer-list ()
  (--filter (with-current-buffer it
              (derived-mode-p 'dired-mode))
            (buffer-list)))

(define-key ctl-x-4-map "D" 'km/dired-switch-to-buffer-other-window)
;; This overrides the binding for `list-directory'.
(global-set-key (kbd "C-x C-d") 'km/dired-switch-to-buffer)

(defun km/org-open-dired-marked-files (&optional in-emacs)
  (interactive "P")
  (let* ((files (dired-get-marked-files))
         (num-files (length files)))
    (when (or (< num-files 5)
              (yes-or-no-p (format "Open %s files?" num-files)))
      (--each files (org-open-file it in-emacs)))))

(after 'org
  ;; This overrides `dired-find-file', which is also bound to "f".
  (define-key dired-mode-map "e" 'km/org-open-dired-marked-files))

(define-prefix-command 'km/dired-narrow-prefix-map)
(define-key km/dired-narrow-prefix-map "n" 'dired-narrow)
(define-key km/dired-narrow-prefix-map "f" 'dired-narrow-fuzzy)
(define-key km/dired-narrow-prefix-map "r" 'dired-narrow-regexp)

(define-key dired-mode-map "/" 'dired-narrow)

(define-prefix-command 'km/dired-subtree-prefix-map)
(define-key km/dired-subtree-prefix-map "i" 'dired-subtree-insert)
(define-key km/dired-subtree-prefix-map "r" 'dired-subtree-remove)
(define-key km/dired-subtree-prefix-map "g" 'dired-subtree-revert)
(define-key km/dired-subtree-prefix-map "s" 'dired-subtree-narrow)
(define-key km/dired-subtree-prefix-map "u" 'dired-subtree-up)
(define-key km/dired-subtree-prefix-map "d" 'dired-subtree-down)
(define-key km/dired-subtree-prefix-map "n" 'dired-subtree-next-sibling)
(define-key km/dired-subtree-prefix-map "p" 'dired-subtree-previous-sibling)
(define-key km/dired-subtree-prefix-map "<" 'dired-subtree-beginning)
(define-key km/dired-subtree-prefix-map ">" 'dired-subtree-end)
(define-key km/dired-subtree-prefix-map "@" 'dired-subtree-mark-subtree)
(define-key km/dired-subtree-prefix-map "." 'dired-subtree-unmark-subtree)

(define-prefix-command 'km/dired-prefix-map)

(define-key km/dired-prefix-map "n" 'km/dired-narrow-prefix-map)
(define-key km/dired-prefix-map "s" 'km/dired-subtree-prefix-map)

(define-key dired-mode-map (kbd "C-c m") 'km/dired-prefix-map)

(provide 'init-dired)