-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathdirvish-extras.el
433 lines (400 loc) · 19.4 KB
/
dirvish-extras.el
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
;;; dirvish-extras.el --- Extra utilities and transient prefixes for dirvish -*- lexical-binding: t -*-
;; Copyright (C) 2021-2025 Alex Lu
;; Author : Alex Lu <https://github.com/alexluigit>
;; Version: 2.1.0
;; Keywords: files, convenience
;; Homepage: https://github.com/alexluigit/dirvish
;; SPDX-License-Identifier: GPL-3.0-or-later
;;; Commentary:
;; Extra utilities and transient prefixes for Dirvish.
;;
;; Commands included:
;; - `dirvish-find-file-true-path'
;; - `dirvish-copy-file-name' (autoload)
;; - `dirvish-copy-file-path' (autoload)
;; - `dirvish-copy-file-directory'
;; - `dirvish-total-file-size' (autoload)
;; - `dirvish-layout-toggle' (autoload)
;; - `dirvish-layout-switch' (autoload)
;; - `dirvish-rename-space-to-underscore'
;;
;; Transient prefixes included (all autoloaded):
;; - `dirvish-file-info-menu'
;; - `dirvish-renaming-menu'
;; - `dirvish-subdir-menu'
;; - `dirvish-chxxx-menu'
;; - `dirvish-mark-menu'
;; - `dirvish-epa-dired-menu'
;; - `dirvish-setup-menu'
;; - `dirvish-dired-cheatsheet'
;; - `dirvish-dispatch'
;;; Code:
(require 'dirvish)
(require 'transient)
(declare-function tramp-file-name-user "tramp")
(declare-function tramp-file-name-host "tramp")
(defcustom dirvish-layout-recipes
'((0 0 0.4) ; | CURRENT | preview
(0 0 0.8) ; | current | PREVIEW
(1 0.08 0.8) ; parent | current | PREVIEW
(1 0.11 0.55)) ; parent | current | preview
"Layout RECIPEs for `dirvish-layout-switch' command.
RECIPE has the same form as `dirvish-default-layout'."
:group 'dirvish
:type '(repeat (list (integer :tag "number of parent windows")
(float :tag "max width of parent windows")
(float :tag "width of preview window"))))
(defclass dirvish-attribute (transient-infix)
((variable :initarg :variable))
"Class for dirvish attributes.")
(cl-defmethod transient-format-description ((obj dirvish-attribute))
"Format description for DIRVISH-ATTRIBUTE instance OBJ."
(format "%s%s" (oref obj description)
(propertize " " 'display '(space :align-to (- right 5)))))
(cl-defmethod transient-format-value ((obj dirvish-attribute))
"Format value for DIRVISH-ATTRIBUTE instance OBJ."
(let* ((val (oref obj value))
(face (if (equal val "+") 'transient-argument 'transient-inactive-value)))
(propertize val 'face face)))
(cl-defmethod transient-init-value ((obj dirvish-attribute))
"Initialize value for DIRVISH-ATTRIBUTE instance OBJ."
(let ((sym (oref obj variable)))
(oset obj value (if (memq sym dirvish-attributes) "+" "-"))))
(cl-defmethod transient-infix-read ((obj dirvish-attribute))
"Read value from DIRVISH-ATTRIBUTE instance OBJ."
(oset obj value (if (equal (oref obj value) "+") "-" "+")))
(cl-defmethod transient-infix-set ((obj dirvish-attribute) value)
"Set relevant value in DIRVISH-ATTRIBUTE instance OBJ to VALUE."
(let* ((item (oref obj variable))
(old-val (purecopy dirvish-attributes))
(new-val (if (equal value "+") (cl-pushnew item old-val)
(remq item old-val))))
(mapc #'require '(dirvish-widgets dirvish-vc dirvish-collapse))
(dirvish--render-attrs 'clear)
(dirvish-prop :attrs (dirvish--attrs-expand new-val))
(dirvish--render-attrs)))
;;;###autoload (autoload 'dirvish-setup-menu "dirvish-extras" nil t)
(defcustom dirvish-ui-setup-items
'(("s" file-size "File size")
("t" file-time "File modification time")
("c" collapse "Collapse unique nested paths"
(not (dirvish-prop :remote)))
("v" vc-state "Version control state"
(and (display-graphic-p) (symbolp (dirvish-prop :vc-backend))))
("m" git-msg "Git commit messages"
(and (symbolp (dirvish-prop :vc-backend)) (not (dirvish-prop :remote))))
("1" '(0 nil 0.4) " - | current (60%) | preview (40%)")
("2" '(0 nil 0.8) " - | current (20%) | preview (80%)")
("3" '(1 0.08 0.8) "parent (8%) | current (12%) | preview (80%)")
("4" '(1 0.11 0.55) "parent (11%) | current (33%) | preview (55%)"))
"ITEMs for `dirvish-setup-menu'.
A ITEM is a list consists of (KEY VAR DESC PRED) where KEY is the
keybinding for the item, VAR can be a valid `dirvish-attributes'
or a layout recipe (see `dirvish-layout-recipes'), DESC is the
documentation for the VAR. The optional PRED is passed as the
predicate for that infix."
:group 'dirvish :type 'alist
:set
(lambda (key value)
(set key value)
(cl-loop
with (attrs . layouts) = ()
for (k v desc pred) in value
for name = (and (symbolp v) (intern (format "dirvish-%s-infix" v)))
do (if (not name)
(push (list k (propertize desc 'face 'font-lock-doc-face)
`(lambda () (interactive) (dirvish-layout-switch ,v)))
layouts)
(eval `(transient-define-infix ,name ()
:class 'dirvish-attribute :variable ',v
:description ,desc :if (lambda () ,(if pred `,@pred t))))
(push (list k name) attrs))
finally
(eval
`(transient-define-prefix dirvish-setup-menu ()
"Configure current Dirvish session."
[:description (lambda () (dirvish--format-menu-heading "Setup Dirvish UI"))
["Attributes:" ,@attrs]]
["Switch layouts:"
:if (lambda () (dv-curr-layout (dirvish-curr))) ,@layouts]
["Actions:"
("M-t" "Toggle fullscreen" dirvish-layout-toggle)
("RET" "Apply current settings to future sessions"
(lambda () (interactive)
(setq-default dirvish-attributes dirvish-attributes)
(setq dirvish-default-layout (dv-ff-layout (dirvish-curr)))
(dirvish--build-layout (dirvish-curr))
(revert-buffer)))])))))
(defun dirvish-find-file-true-path ()
"Open truename of (maybe) symlink file under the cursor."
(interactive)
(dired-jump nil (file-truename (dired-get-filename nil t))))
(defun dirvish--kill-and-echo (string)
"Echo last killed STRING."
(kill-new string)
(let ((hint (propertize
"Copied: " 'face 'font-lock-builtin-face)))
(message "%s" (format "%s%s" hint string))))
(defun dirvish-copy-file-true-path ()
"Copy truename of (maybe) symlink file under the cursor."
(interactive)
(dirvish--kill-and-echo
(file-truename (dired-get-filename nil t))))
;;;###autoload
(defun dirvish-copy-file-name (&optional multi-line)
"Copy filename of marked files.
If MULTI-LINE, make every name occupy a new line."
(interactive "P")
(let* ((files (dired-get-marked-files t))
(names (mapconcat #'concat files (if multi-line "\n" " "))))
(dirvish--kill-and-echo (if multi-line (concat "\n" names) names))))
;;;###autoload
(defun dirvish-copy-file-path (&optional multi-line)
"Copy filepath of marked files.
If MULTI-LINE, make every path occupy a new line."
(interactive "P")
(let* ((files (mapcar #'file-local-name (dired-get-marked-files)))
(names (mapconcat #'concat files (if multi-line "\n" " "))))
(dirvish--kill-and-echo (if multi-line (concat "\n" names) names))))
(defun dirvish-copy-remote-path (&optional multi-line)
"Copy remote path of marked files.
If MULTI-LINE, every file takes a whole line."
(interactive "P")
(let* ((tramp (or (dirvish-prop :tramp)
(user-error "Not a remote folder")))
(files (cl-loop for file in (dired-get-marked-files)
for user = (tramp-file-name-user tramp)
for host = (tramp-file-name-host tramp)
for localname = (file-local-name file)
collect (format "%s%s%s:%s" (or user "")
(if user "@" "") host localname)))
(names (mapconcat #'concat files (if multi-line "\n" " "))))
(dirvish--kill-and-echo (if multi-line (concat "\n" names) names))))
(defun dirvish-copy-file-directory ()
"Copy directory name of file under the cursor."
(interactive)
(dirvish--kill-and-echo
(expand-file-name default-directory)))
;;;###autoload
(defun dirvish-total-file-size (&optional fileset)
"Echo total file size of FILESET.
FILESET defaults to `dired-get-marked-files'."
(interactive)
(cl-labels ((f-name (f) (if (not (file-directory-p f)) f
(directory-files-recursively f ".*" nil t)))
(f-size (f) (condition-case nil
(file-attribute-size (file-attributes f))
(file-error 0))))
(let* ((fileset (or fileset (dired-get-marked-files)))
(count (propertize (number-to-string (length fileset))
'face 'font-lock-builtin-face))
(size (thread-last fileset (mapcar #'f-name) flatten-tree
(mapcar #'f-size) (cl-reduce #'+)
file-size-human-readable)))
(message "%s" (format "Total size of %s entries: %s" count size)))))
;;;###autoload
(defun dirvish-layout-switch (&optional recipe)
"Switch Dirvish layout according to RECIPE.
If RECIPE is not provided, switch to the recipe next to the
current layout defined in `dirvish-layout-recipes'."
(interactive)
(cl-loop
with dv = (let ((dv (dirvish-curr)))
(unless dv (user-error "Not in a Dirvish session"))
(unless (dv-curr-layout dv)
(dirvish-layout-toggle)
(user-error "Dirvish: entering fullscreen")) dv)
with old-recipe = (dv-curr-layout dv)
with recipes = (if recipe (list recipe) dirvish-layout-recipes)
with l-length = (length recipes)
for idx from 1
for recipe in recipes
when (or (eq idx l-length) (equal old-recipe recipe))
return
(let* ((new-idx (if (> idx (1- l-length)) 0 idx))
(new-recipe (nth new-idx recipes)))
(setf (dv-curr-layout dv) new-recipe)
(setf (dv-ff-layout dv) new-recipe)
(dirvish--build-layout dv))))
(defun dirvish-rename-space-to-underscore ()
"Rename marked files by replacing space to underscore."
(interactive)
(require 'dired-aux)
(if (derived-mode-p 'dired-mode)
(let ((markedFiles (dired-get-marked-files )))
(mapc (lambda (x)
(when (string-match " " x )
(dired-rename-file x (replace-regexp-in-string " " "_" x) nil)))
markedFiles)
(revert-buffer))
(user-error "Not in a Dired buffer")))
(defun dirvish--marked-files-as-info-string ()
"Return all marked files as a string."
(let* ((files (dired-get-marked-files t))
(count (length files)))
(cond ((<= count 1)
(format "current file: %s" (dired-get-filename t t)))
((<= count 10)
(format "marked files:\n %s" (mapconcat #'concat files "\n ")))
(t (format "marked files:\n %s\n ... and %s more (%s in total)"
(mapconcat #'concat (seq-take files 10) "\n ")
(- count 10) count)))))
;;;###autoload (autoload 'dirvish-file-info-menu "dirvish-extras" nil t)
(transient-define-prefix dirvish-file-info-menu ()
"Gather file information."
[:description
(lambda () (dirvish--format-menu-heading
"Get File Information"
(dirvish--marked-files-as-info-string)))
("n" "Copy file NAMEs in one line <n> / multiple lines <C-u n>"
dirvish-copy-file-name)
("p" "Copy file PATHs in one line <p> / multiple lines <C-u p>"
dirvish-copy-file-path)
("P" "Copy remote PATHs in one line <P> / multiple lines <C-u P>"
dirvish-copy-remote-path
:if (lambda () (dirvish-prop :remote)))
("d" "Copy file DIRECTORY" dirvish-copy-file-directory)
("l" "Copy symlink's truename" dirvish-copy-file-true-path
:if (lambda () (file-symlink-p (dired-get-filename nil t))))
("L" "Go to symlink's truename" dirvish-find-file-true-path
:if (lambda () (file-symlink-p (dired-get-filename nil t))))
("s" "Get total size of marked files" dirvish-total-file-size)
("t" "Show file TYPE" dired-show-file-type)])
(transient-define-prefix dirvish-subdir-menu ()
"Help Menu for Dired subdir management."
[:description
(lambda () (dirvish--format-menu-heading "Manage subdirs"))
("i" " Insert subdir" dired-maybe-insert-subdir :transient t)
("k" " Kill subdir" dired-kill-subdir :transient t)
("n" " Next subdir" dired-next-subdir :transient t)
("p" " Prev subdir" dired-prev-subdir :transient t)
("j" " Jump to subdir" dired-goto-subdir)
("$" " Hide subdir" dired-hide-subdir :transient t)
("M-$" "Hide all subdirs" dired-hide-all)])
;;;###autoload (autoload 'dirvish-chxxx-menu "dirvish-extras" nil t)
(transient-define-prefix dirvish-chxxx-menu ()
"Help Menu for file attribute modification commands."
[:description
(lambda () (dirvish--format-menu-heading "Modify file's attributes"))
("g" "Change file's GROUP" dired-do-chgrp)
("m" "Change file's MODE" dired-do-chmod)
("o" "Change file's OWNER" dired-do-chown)
("t" "Change file's TIMESTAMP" dired-do-touch)
("p" "Change file's PATH" dired-do-rename)])
;;;###autoload (autoload 'dirvish-mark-menu "dirvish-extras" nil t)
(transient-define-prefix dirvish-mark-menu ()
"Help Menu for `dired-mark/do-*' commands."
[["Mark or unmark files:"
("e" " by Extension" dired-mark-extension :transient t)
("*" " by Regexp (file name)" dired-mark-files-regexp :transient t)
("c" " by Regexp (file content)" dired-mark-files-containing-regexp :transient t)
("s" " by Subdir" dired-mark-subdir-files :transient t)
("x" " by Executable" dired-mark-executables :transient t)
("/" " by Directory" dired-mark-directories :transient t)
("@" " by Symlink" dired-mark-symlinks :transient t)
("&" " by Garbage" dired-flag-garbage-files :transient t)
("#" " by Auto-saved" dired-flag-auto-save-files :transient t)
("~" " by Backup" dired-flag-backup-files :transient t)
("." " by Numerical backup" dired-clean-directory :transient t)
("u" " Unmark this file" dired-unmark :transient t)
("DEL" "Unmark and move up line" dired-unmark-backward :transient t)
("U" " Unmark all files" dired-unmark-all-files :transient t)
("t" " Toggle marks" dired-toggle-marks :transient t)
("n" " Move to next marked file" dired-next-marked-file :transient t)
("p" " Move to prev marked file" dired-prev-marked-file :transient t)]
["Actions on marked files:"
("O" "Open" dired-do-find-marked-files)
("S" "Symlink" dired-do-symlink)
("H" "Hardlink" dired-do-hardlink)
("P" "Print" dired-do-print)
("X" "Delete flagged" dired-do-flagged-delete)
("r" "Search file contents" dired-do-find-regexp)
("R" "Replace file contents" dired-do-find-regexp-and-replace)
("B" "Byte compile elisp" dired-do-byte-compile)
("L" "Load elisp" dired-do-load)
("z" "Compress to" dired-do-compress-to)
("Z" "Compress" dired-do-compress)
("!" "Shell command" dired-do-shell-command)
("&" "Async shell command" dired-do-async-shell-command)
("N" "Echo number of marked files" dired-number-of-marked-files)
("A" "Modify file's attributes" dirvish-chxxx-menu)
("C" "Change mark type" dired-change-marks)
("k" "Kill lines" dired-do-kill-lines)]]
(interactive)
(require 'dired-x)
(require 'dired-aux)
(transient-setup 'dirvish-mark-menu))
;;;###autoload (autoload 'dirvish-renaming-menu "dirvish-extras" nil t)
(transient-define-prefix dirvish-renaming-menu ()
"Help Menu for file renaming in Dired."
[:description
(lambda () (dirvish--format-menu-heading "File renaming"))
("u" "Upper-case file name" dired-upcase)
("l" "Lower-case file name" dired-downcase)
("_" "Replace SPC with UNDERSCORE" dirvish-rename-space-to-underscore :if-derived 'dirvish-mode)
("w" "Enter wdired [writable dired]" wdired-change-to-wdired-mode :if-not-derived wdired-mode)])
(transient-define-prefix dirvish-epa-dired-menu ()
"Help menu for `epa-dired-do-*' commands."
[:description
(lambda () (dirvish--format-menu-heading "GNUpg assistant"))
("e" "Encrypt" epa-dired-do-encrypt)
("d" "Decrypt" epa-dired-do-decrypt)
("v" "Verify" epa-dired-do-verify)
("s" "Sign" epa-dired-do-sign)])
;;;###autoload (autoload 'dirvish-dired-cheatsheet "dirvish-extras" nil t)
(transient-define-prefix dirvish-dired-cheatsheet ()
"A collection of most frequently used Dired commands."
[:description
(lambda () (dirvish--format-menu-heading
"Dired cheatsheet"
"The keys listed here may be different from the actual bindings"))
("n" " Move to next line" dired-next-line :transient t)
("p" " Move to prev line" dired-previous-line :transient t)
(">" " Move to next dirline" dired-next-dirline :transient t)
("<" " Move to prev dirline" dired-prev-dirline :transient t)
("." " Add an empty file" dired-create-empty-file)
("+" " Add a directory" dired-create-directory)
("X" " Delete files" dired-do-delete)
("v" " View this file" dired-view-file)
("g" " Refresh buffer" revert-buffer)
("f" " Find file" dired-find-file)
("o" " Find file other window" dired-find-file-other-window)
("j" " Go to line for file" dired-goto-file)
("^" " Go to parent directory" dired-up-directory)
("=" " Compare files" dired-diff)
("(" " Toggle details" dired-hide-details-mode)
("d" " Display this file" dired-display-file)
("s" " Manage subdirs" dirvish-subdir-menu)
(":" " GnuPG helpers" dirvish-epa-dired-menu)
("h" " More info about Dired" describe-mode)])
;;;###autoload (autoload 'dirvish-dispatch "dirvish-extras" nil t)
(transient-define-prefix dirvish-dispatch ()
"Main menu for Dired/Dirvish."
[:description
(lambda () (dirvish--format-menu-heading
"Dirvish main menu"
"NOTICE: these commands require relevant Dirvish extensions"))
"" "Actions & Essential commands"
("u" "User interface setup" dirvish-setup-menu)
("c" "Dired cheatsheet" dirvish-dired-cheatsheet)
("/" "Perform fd search" dirvish-fd)
("@" "Find all dirs by fd" dirvish-fd-jump)
("R" "Rsync marked files" dirvish-rsync)
("n" "Live narrowing" dirvish-narrow)
"Transient commands"
("a" "Quick access" dirvish-quick-access)
("h" "Go to history entries" dirvish-history-menu)
("s" "Sort current buffer" dirvish-quicksort)
("l" "Setup listing switches" dirvish-ls-switches-menu)
("f" "Setup fd-find switches" dirvish-fd-switches-menu
:if (lambda () (dirvish-prop :fd-arglist)))
("S" "Setup rsync switches" dirvish-rsync-switches-menu)
("m" "Manage marks" dirvish-mark-menu)
("e" "Manage emerged groups" dirvish-emerge-menu)
("t" "Manage subtrees" dirvish-subtree-menu)
("r" "Rename files" dirvish-renaming-menu)
("v" "Version control system" dirvish-vc-menu)
("y" "Yank marked files" dirvish-yank-menu)
("i" "Get file information" dirvish-file-info-menu)])
(provide 'dirvish-extras)
;;; dirvish-extras.el ends here