-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdiredc.el
4465 lines (4099 loc) · 184 KB
/
diredc.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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; diredc.el --- Midnight Commander features (plus) for dired -*- lexical-binding: t -*-
;; Copyright © 2020-2024, Boruch Baum <[email protected]>
;; Author/Maintainer: Boruch Baum <[email protected]>
;; Homepage: https://github.com/Boruch-Baum/emacs-diredc
;; SPDX-License-Identifier: GPL-3.0-or-later
;; Keywords: files
;; Package: diredc
;; Version: 1.6
;; Package-Requires: ((emacs "26.1") (key-assist "1.0"))
;;
;; (emacs "24.1") for: split-window-right
;; (emacs "24.3") for: lexical-binding, user-error, cl-lib, defvar-local
;; (emacs "24.4") for: advice-remove
;; (emacs "25.1") for: save-mark-and-excursion
;; (emacs "26.1") for: copied version of dired-internal-noselect
;; This file is NOT part of GNU Emacs.
;; This is free software: you can redistribute it and/or modify it under
;; the terms of the GNU General Public License as published by the Free
;; Software Foundation, either version 3 of the License, or (at your
;; option) any later version.
;; This software is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
;; Public License for more details.
;; You should have received a copy of the GNU General Public License along
;; with this software. If not, see <https://www.gnu.org/licenses/>.
;;
;;; Commentary:
;;
;; This package extends and configures `dired' with features found in
;; almost all 'file managers', and also some unique features:
;;
;; * Resilient dedicated dual-pane frame.
;; * similar look to 'midnight commander'.
;; * intelligent recovery of manually altered frame configuration
;; * exit diredc/dired cleanly and totally
;; * Navigable directory history
;; * backward, forward, or to a direct history entry
;; * File quick-preview mode
;; * inspired by, and similar to, midnight commander's "C-x q"
;; * customizable exclusion criteria to suppress undesirable files
;; (eg. binaries)
;; * optionally view magit status buffers for repository roots
;; * Current file's supplemental information in minibuffer (optional)
;; * eg. output from 'getfattr', 'getfacl', 'stat', 'exif'.
;; * Multiple panel views
;; * inspired by, and similar to, midnight commander's "M-t"
;; * superior configurability
;; * directly choose a specific panel view, or toggle to next
;; * Extensive and easy-to-use sort options
;; * including options not in 'ls': sort by chmod, owner, group
;; * Swap panels (use "M-u")
;; * inspired by, and similar to, midnight commander's "C-u"
;; * a TRUE and complete swap (including history entries)
;; * Trash management
;; * per xfreedesktop standard
;; * restore trashed files to their original locations
;; * empty the trash, along with its administrative overhead
;; * view trash summary information
;; * Navigate 'up' n parent directories
;; * Launch persistent asynchronous processes for files
;; * Processes will survive even after exiting Emacs.
;; * Quick shell window
;; * choose your default shell / terminal emulation mode
;; * choose your default shell program
;; * easily opt for pre-configured alternatives
;; * useful pre-defined shell variables
;; * $d1, $d2 dired-directory in this/other pane
;; * $f1, $f2 current dired file in this/other pane
;; * $t1, $t2 tagged elements in this other pane
;; * as a shell array variable, if supported by the shell
;; * $INSIDE_DIREDC value of variable 'diredc--version'
;; * Bookmark support
;; * Edit dired buffers (really `wdired-mode', not `diredc')
;; * Set both panels to same directory (use "=" or "C-u =")
;; * inspired by 'midnight commander's "M-i"
;; * Fontify filenames based upon their names or extensions
;; * fontify 'executable' suffix symbol
;; * Optional "drilled-down" view of "sparse" paths (use "}", "{")
;; * ie. ./paths/with/only/single/entries
;; * Uses a 'diredc'-patched version of external package
;; 'dired-collapse' (https://github.com/Fuco1/dired-hacks)
;; pending pull-request merges.
;;
;; Bonus customization features
;; * Customize colors for chmod bits (font-lock)
;; * toggle display of "hidden" or "undesirable" files (dired-omit mode)
;; * highlight current line (hl-line-mode)
;; * current buffer highlights with a unique face.
;; * don't wrap long lines (toggle-truncate-lines)
;; * to disable:
;; * option 1: M-x customize-variable diredc-bonus-configuration
;; * option 2: (setq diredc-bonus-configuration nil)
;;
;;; Dependencies (all are already part of Emacs):
;;
;; dired -- ... (doh) ...
;; dired-x -- for dired-guess-default, dired-omit-mode
;; dired-aux -- for dired-compress-files
;; help-mode -- for help button widget
;; hl-line -- for hl-line-mode
;; subr -- for string-match-p
;; term -- for term-line-mode, term-send-input
;; view -- for view-mode
;;
;; Suggested (not part of Emacs):
;;
;; popup -- for popup-menu*
;; key-assist -- for key-assist
;;
;;; Installation:
;;
;; 1) Evaluate or load this file.
;;
;; 2) I recommend defining a global keybinding for function `diredc',
;; with a preference for Shift-F11, as follows:
;;
;; (global-set-key (kbd "S-<f11>") 'diredc)
;;
;; An alternative or additional option would be:
;;
;; (global-set-key [remap dired-other-frame] 'diredc)
;;
;;; Operation:
;;
;; Running `diredc' an initial time creates and selects a frame named
;; `diredc' with two side-by-side `dired' windows / buffers. Repeating the
;; command will return you to your prior frame / window / buffer.
;; Subsequent use of the command continues to toggle back and forth to/from
;; the named `diredc' frame.
;;
;; In addition to the usual Emacs keybinding help, diredc provides two
;; combination keybinding cheat-sheets and command launchers, both using
;; optional dependency package `key-assist'. You can also interactively
;; call M-x `key-assist' <RET> <RET> to view an exhaustive `dired'
;; keybinding listing. A separate `key-assist' is provided for
;; trash-related functions because it also displays the current trash state
;; statistics.
;;
;; h `diredc-key-assist'
;; C-<delete> ? `diredc-trash-key-assist'
;; ? `diredc-summary'
;; C-h m `describe-mode'
;;
;; Navigation from one `dired' panel to another can be accomplished using
;; '<TAB>' or 'S-<TAB>'. As long as you are in `diredc' mode, navigating to
;; new directories should not accumulate additional `dired' buffers and
;; your directory navigation history for each panel should be remembered.
;; If ever you find that the frame configuration has become botched, or you
;; somehow accumulate or have lost `dired' buffers, Run M-x
;; `diredc-recover'. You can also cleanly kill all `dired' buffers and the
;; `diredc' frame using `C-q' (M-x `diredc-quit'). And, if you want to use
;; `dired' without the `diredc' features, run M-x `diredc-mode' to toggle
;; the mode off.
;;
;; As mentioned above, each `dired' panel now 'remembers' its navigation
;; history. The history can be traversed sequentially backward 'C-<left>'
;; or forward 'C-<right>' without losing elements, and can be viewed and
;; traversed non-sequentially using 'C-u /'. Use '/' to directly navigate
;; to a directory not 'nearby'.
;;
;; A 'file preview' mode can be entered or exited using 'C-x q' (M-x
;; `diredc-browse-mode'). In that mode, whenever a `dired' buffer's POINT
;; is on a file's line, that file will be opened on the other pane, in
;; read-only emacs `view-mode' (see there for the navigation and other
;; features of that mode). The `view-mode' buffer is deleted automatically
;; when you either disable the mode or you move point to another line in
;; the `dired' buffer. Use '<TAB>' or 'S-<TAB>' to navigate between the
;; `dired' buffer window and the file preview window. There are several
;; options for excluding undesirable files (eg. binaries) from preview; see
;; the mode's docstring for details.
;;
;; The 'file preview' mode can also be configured to display the
;; `magit-status' of a repository's root directory. See customization
;; variable `diredc-browse-magit'.
;;
;; The traditional `dired' operations that 'find' or 'open' a file should
;; do so to a separate frame, most likely the one from which you came to
;; the `diredc' frame.
;;
;; The traditional `dired' feature to perform arbitrary asynchronous
;; operations on a file or set of files has been enhanced to make those
;; processes persistent, to survive even after exiting Emacs. Thus, with
;; the default keybinding, you can press '&' <RET> and have the selected
;; file(s) launched in the system-default external application. Do be
;; advised, though, that this comes at the expense of losing the processes'
;; *Async Shell Command* buffer and its log of STDOUT / STDERR for the
;; processes. The former, non-persistent behavior can be opted for at
;; run-time by prefixing the command with a SPACE (eg. " foo") or for the
;; default command simply enter just a SPACE. The former, non-persistent
;; behavior can be made default by modifying variable
;; `diredc-async-processes-are-persistent'.
;;
;; Pressing RETURN on files that you don't want opened in Emacs,
;; doesn't. Pre-existing 'dired' variable
;; 'dired-guess-shell-alist-user' is used as reference, and pressing
;; RETURN runs on the selected file the first associated executable in
;; that list. If you really do want to find the find in Emacs, press
;; C-u RETURN instead.
;;
;; The display format of `dired' buffers can be "hot-swapped" using 'M-t'
;; (M-x `diredc-display-toggle'). Use 'C-u M't' to select from available
;; display formats, and customize the list using defcustom variable
;; `diredc-display--listing-switches-list'. Four views are provided by
;; default, all long-format but with different file block-sizes (byte, Kb,
;; Mb), and several other differences.
;;
;; The traditional `dired' sort feature has been greatly enhanced with a
;; clearer UI and many more sorting options.
;;
;; The `diredc' buffers themselves can also be "hot-swapped", using 'M-u'
;; (M-x `diredc-swap-windows').
;;
;; While emacs does have a native defcustom variable
;; `delete-by-moving-to-trash' to control whether to "really" delete files,
;; `diredc' allows one or more trashed items to be restored, allows the
;; trash to be emptied, and conveniently present trash statistics. Here are
;; the relevant commands and their default keybindings. See each's
;; docstring for more details:
;;
;; C-<delete> SPC `diredc-trash-toggle'
;; C-<delete> <insertchar> `diredc-trash-toggle'
;;
;; C-<delete> i `diredc-trash-info'
;;
;; C-<delete> j `diredc-trash-view'
;; C-<delete> v `diredc-trash-view'
;;
;; C-<delete> C-<delete> `diredc-trash-empty'
;; C-<delete> x `diredc-trash-empty'
;;
;; C-<delete> r `diredc-trash-restore'
;;
;; C-k `diredc-trash-quick-delete'
;;
;; C-<delete> ? `diredc-trash-key-assist'
;;
;; A limitation in `dired' is its inability to natively present a file's
;; supplemental information, such as its possible extended access control
;; list or extended file attributes. `diredc' allows this and more to be
;; presented in the minibuffer echo area as you navigate a `diredc' buffer.
;; Use M-x `diredc-show-more-file-info' to toggle through the default
;; possibilities, or customize the `diredc-show-more-file-info-list' to
;; present the metadata of your choice.
;;
;; C-c ? `diredc-show-more-file-info'
;;
;; `diredc' brings bookmarks to dired:
;;
;; C-c + a `diredc-bookmark-add'
;; C-c + j `diredc-bookmark-jump'
;; C-c + e `diredc-bookmark-edit'
;;
;; Emacs has a nifty mode to "edit" a `dired' buffer, bringing the power of
;; emacs to the application of renaming files. `diredc' just gives you the
;; little bit of extra help with convenient keybindings `E' and `e' to
;; enter the mode. If you're happy with your edits, you apply them and exit
;; the mode with `C-c C-c', or abort your editing session with `C-c C-k'.
;;
;; The `diredc-shell' command opens up any type of emacs shell or terminal
;; emulator window and pre-seeds it with useful `dired' values (see section
;; 'Extra Features', below).
;;
;; ' `diredc-shell'
;; C-c ! `diredc-shell'
;; C-c C-k `diredc-shell-kill'
;;
;; For further information, KRTM, the package's docstrings and the
;; package's defcustom group.
;;
;;; Configuration
;;
;; You can browse and edit this mode's list of `defcustom's using "M-x
;; `customize-group' diredc", but there isn't too much to be found there.
;; Separately, you might want to redefine the default keybindings, but
;; otherwise there is nothing really important about `diredc' itself to
;; configure.
;;
;; `dired' mode itself, however, is a complex and highly configurable
;; package that has been under development for over 25 years. That's a long
;; time for options to accumulate and for opinions to multiply. The
;; `diredc' developer (ahem: me) has considerately imposed his preferences
;; upon you by default, in a way trivial to over-ride. If you don't want
;; them, toggle the value of defcustom `diredc-bonus-configuration' to nil,
;; an the settings will revert upon selecting new buffers. The bonus
;; customization features are listed above, in the 'Commentary' section, or
;; you could peek at the source code of function
;; `diredc-bonus-configuration'.
;;
;; The colorization for each buffer's selected line is set as part of
;; `diredc-bonus-configuration', but because it's controlled by
;; `hl-line-mode'; you can independently toggle the feature per-buffer, and
;; you can change the highlighting colors using "M-x `customize-face'" for
;; faces `hl-line' and `diredc-hl-current-buffer'. The colorization of the
;; chmod bits are also set as part of `diredc-bonus-configuration'; you can
;; find their definitions and edit them using "M-x `customize-group'
;; diredc".
;;
;;; Extra Features:
;;
;; *] Navigating to a parent directory with `dired-up-directory' (default:
;; `^') can use the prefix-argument to navigate multiple levels in one
;; operation.
;;
;; *] Use `dired-hist-change-directory' (default: `/') to jump to a
;; location not nearby without losing the current dired buffer's
;; history.
;;
;; *] Use the `prefix-argument' with `diredc-hist-change-directory' to have
;; `diredc-hist-select' display all elements of the Dired buffer's
;; history and allow you to jump directly to any of them.
;;
;; *] Use `diredc-hist-duplicate' (default: `=') to either navigate to
;; another `dired' buffer to your current one's directory, or with the
;; `prefix-argument' to navigate your current `dired' buffer to
;; another's directory.
;;
;; *] Modify data structure `diredc-recover-schemes' to apply your own
;; custom recovery strategies. Share them for others' benefit!
;;
;; *] Use `diredc-trash-quick-delete' (default: `C-k') on a POINT or a
;; REGION to quick-delete the selected files. Use the prefix-arg to
;; toggle between "trashing" or deleting.
;;
;; *] When `diredc-hist-mode' is disabled, the following functions continue
;; to operate, but without updating the history records, so you can use
;; them as your default `dired' functions even if you don't always want
;; to use `diredc-hist-mode'.
;;
;; `diredc-hist-change-directory'
;; `diredc-hist-up-directory'
;; `diredc-hist-duplicate'
;; `diredc-hist-find-file'
;; `diredc-hist-find-file-other-window'
;; `diredc-hist-find-alternate-file'
;;
;; *] `diredc' passes to the shell/terminal-emulator instance the following
;; shell variables:
;;
;; $d1 - this `diredc' windows's directory name
;; $d2 - directory name of other visible `diredc' window
;; $f1 - this `diredc' window's file name at POINT
;; $f2 - file name at POINT of other visible `diredc' window
;; $t1 - this `diredc' window's list of tagged file names
;; $t2 - list of tagged file names of other visible `diredc' window
;;
;; If the selected shell supports array variables, then $t1 and $t2 will
;; be set as such; Otherwise, elements will be quoted and delimited with
;; a space.
;;
;; *) universal fallback guess shell command(s)
;;
;; *] Use `diredc-collapse-mode' (default: `{' or `}') to view that
;; single file at the bottom of a "sparse" path, ie.
;; ./path/with/only/single/entry. This feature respects
;; `dired-omit-mode'.
;;
;;; Feedback:
;;
;; It's best to contact me by opening an 'issue' on the program's github
;; repository (see above) or, distant second-best, by direct e-mail.
;;
;; Code contributions are welcome and github starring is appreciated.
;;
;;
;;; Compatibility
;;
;; This package has been tested under debian linux emacs version 26
;; and 27. The main compatibility issue to be aware of is that this
;; suite needs to modify[1] a single line in function
;; `dired-internal-no-select' of the standard emacs file `dired.el'
;; This was accomplished by advising a wrapper function
;; `diredc--advice--dired-internal-noselect' around the original. If
;; that function ever changes, that advice function and this suite
;; will need to account for that.
;;
;; [1] emacs bug #44023: https://debbugs.gnu.org/cgi/bugreport.cgi?bug=44023"
;;
;;; Code:
;;
;;; Dependencies
(require 'dired) ; ... (doh)
(require 'dired-aux) ; dired-compress-files-alist
(require 'dired-x) ; dired-guess-default, dired-omit-mode
(require 'term) ; term-line-mode, term-send-input
(require 'view) ; view-mode
(require 'help-mode) ; help-xref button
;;
;;; Suggested
(require 'popup nil t)
(declare-function popup-menu* "ext:popup.el")
(require 'key-assist nil t)
(declare-function key-assist "ext:key-assist.el")
(declare-function key-assist--get-keybinding "ext:key-assist.el")
;;
;;; Forked Code:
;; Upstream: https://github.com/Fuco1/dired-hacks/blob/master/dired-collapse.el
;; Author: Matúš Goljer <[email protected]>
;; Copyright: ©2017-2024, Matúš Goljer
;; License: GPL 3
;; Fork purpose:
;; * Apply mode globally
;; https://github.com/Fuco1/dired-hacks/pull/209
;; * Support `dired-omit-mode'
;; https://github.com/Fuco1/dired-hacks/pull/210
;; * Remove dependencies
;; https://github.com/Fuco1/dired-hacks/pull/211
;; * Avoid using function 'length' on lists
;; https://github.com/Fuco1/dired-hacks/pull/212
;; * Apply overlay optionally
;; https://github.com/Fuco1/dired-hacks/pull/213
;; * Use custom shadow face
;; For me, 'shadow alters the :foreground property, which conflicts
;; with other `diredc' font-lock features, so we guarantee a face
;; that only alters the :background property, and only subtly.
(defgroup diredc-collapse ()
"Collapse unique nested paths in Diredc listings."
:group 'diredc
:prefix "diredc-collapse-")
(defcustom diredc-collapse-remote nil
"If non-nil, enable `diredc-collapse' in remote (TRAMP) buffers."
:type 'boolean
:package-version '(diredc . "1.4")
:group 'diredc-collapse)
(defcustom diredc-collapse-fontify nil
"If non-nil, fontify with a shaded overlay."
:type 'boolean
:package-version '(diredc . "1.4")
:group 'diredc-collapse)
(defface diredc-collapse-shadow-face
;; ref: https://emacs.stackexchange.com/questions/69050
(list (list t (list :background
(let*
((fg (or (color-values (face-attribute 'default :foreground))
'(0 0 0))) ; (color-values "black")
(bg (or (color-values (face-attribute 'default :background))
'(58853 58853 58853))) ; (color-values "white")
(fg-factor 0.92)
(bg-factor (- 1.0 fg-factor)))
(apply 'format
(cons "#%02x%02x%02x"
(mapcar
(lambda (n)
(ash (truncate (+ (* (nth n fg) bg-factor)
(* (nth n bg) fg-factor)))
-8))
'(0 1 2))))))))
"Slightly adjust the background color of collapsed `diredc' entries.")
(defvar diredc-collapse-mode nil
"Whether `diredc' extensions to `dired' are globally enabled.
Don't ever set this variable directly! Instead, evaluate function
`diredc-mode'.")
(defun diredc-collapse-mode (&optional arg)
"Toggle collapsing of unique nested paths in Diredc buffers.
Interactively, toggle the mode. From Lisp, with ARG positive or NIL,
turn the mode on; Otherwise, turn it off."
(interactive)
(cond
((called-interactively-p 'interactive)
(setq diredc-collapse-mode (not diredc-collapse-mode)))
(arg
(setq diredc-collapse-mode (if (< 0 arg) t nil)))
(t
(setq diredc-collapse-mode t)))
(cond
(diredc-collapse-mode
(add-hook 'dired-after-readin-hook 'diredc-collapse 90)
(add-hook 'dired-subtree-after-insert-hook 'diredc-collapse 90)
(add-hook 'dired-omit-mode-hook 'diredc-collapse 90))
(t ; ie. not diredc-collapse-mode
(remove-hook 'dired-after-readin-hook 'diredc-collapse)
(remove-hook 'dired-subtree-after-insert-hook 'diredc-collapse)
(remove-hook 'dired-omit-mode-hook 'diredc-collapse)))
(diredc--revert-all)
(let (message-log-max)
(message "Diredc-collapse-mode %s." (if diredc-collapse-mode "enabled" "disabled"))))
(defun diredc-collapse--replace-file (file)
"Replace file on the current line with FILE."
(delete-region (line-beginning-position) (1+ (line-end-position)))
(insert " ")
(insert-directory file dired-listing-switches nil nil)
(forward-line -1)
(dired-align-file (line-beginning-position) (1+ (line-end-position)))
(when-let (replaced-file (dired-get-filename nil t))
(when (file-remote-p replaced-file)
(while (search-forward (dired-current-directory) (line-end-position) t)
(replace-match "")))))
(defun diredc-collapse--create-ov (&optional to-eol)
"Create the shadow overlay which marks the collapsed path.
If TO-EOL is non-nil, extend the overlay over the whole
filename (for example when the final directory is empty)."
(save-excursion
(dired-move-to-filename)
(let* ((beg (point))
(end (save-excursion
(dired-move-to-end-of-filename)
(if to-eol
(point)
(1+ (search-backward "/")))))
(ov (make-overlay beg end)))
(overlay-put ov 'face 'diredc-collapse-shadow-face)
ov)))
(defun diredc-collapse ()
"Collapse unique nested paths in diredc listings."
(when (or (not (file-remote-p default-directory)) diredc-collapse-remote)
(let (;; dired-hide-details-mode hides details by assigning a
;; special invisibility text property to them, while
;; diredc-collapse requires all the details. So we disable
;; invisibility here temporarily.
(buffer-invisibility-spec nil)
(inhibit-read-only t)
(rgx (and dired-omit-mode (dired-omit-regexp))))
(save-excursion
(goto-char (point-min))
(while (not (eobp))
(when-let ((filename-no-dir (dired-get-filename 'no-dir t)))
(when (and (looking-at-p dired-re-dir)
(not (member filename-no-dir (list "." "..")))
(not (eolp)))
(let ((path (dired-get-filename nil t))
entry-1 entry-2 files)
(while (and (file-directory-p path)
(file-accessible-directory-p path)
(setq files (directory-files path
'full
directory-files-no-dot-files-regexp))
(or (not dired-omit-mode)
(setq files (cl-remove-if
(lambda(f)
(string-match rgx (file-name-nondirectory f)))
files)))
(setq entry-1 (pop files))
(not (setq entry-2 (pop files))))
(setq path entry-1))
(if (and (not files)
(equal path (dired-get-filename nil t)))
(when diredc-collapse-fontify
(diredc-collapse--create-ov 'to-eol))
(setq path (s-chop-prefix (dired-current-directory) path))
(when (string-match-p "/" path)
(let ((default-directory (dired-current-directory)))
(diredc-collapse--replace-file path))
(dired-insert-set-properties (line-beginning-position) (line-end-position))
(when diredc-collapse-fontify
(diredc-collapse--create-ov (not entry-1))))))))
(forward-line 1))))))
;;
;;; Keymaps:
(defun diredc--create-keymap ()
"Internal function for `diredc'. Create a new symbol `diredc-mode' keymap.
Returns a keymap."
(let ((map (make-sparse-keymap)))
(set-keymap-parent map dired-mode-map)
(define-key map (kbd "C-q") 'diredc-exit) ; defalias diredc-quit
(define-key map (kbd "q") 'diredc-do-not-quit-window)
(define-key map (kbd "?") 'diredc-summary)
(define-key map (kbd "h") 'diredc-key-assist)
(define-key map "\t" 'diredc-other-window)
(define-key map (kbd "<backtab>") 'diredc-other-window) ; No directionality
(define-key map [remap dired-find-file]
'diredc-hist-find-file)
(define-key map [remap dired-find-alternate-file]
'diredc-hist-find-alternate-file)
(define-key map (kbd "<RET>") 'diredc-hist-find-alternate-file)
(define-key map (kbd "o") 'diredc-hist-find-file-other-window)
(define-key map (kbd "/") 'diredc-hist-change-directory)
(define-key map (kbd "^") 'diredc-hist-up-directory)
(define-key map (kbd "C-<up>") 'diredc-hist-up-directory)
(define-key map (kbd "C-<left>") 'diredc-hist-previous-directory)
(define-key map (kbd "C-<right>") 'diredc-hist-next-directory)
(define-key map (kbd "=") 'diredc-hist-duplicate)
(define-key map (kbd "M-u") 'diredc-swap-windows)
(define-key map (kbd "C-x q") 'diredc-browse-mode)
(define-key map (kbd "M-t") 'diredc-display-toggle)
(define-key map (kbd "C-c ?") 'diredc-show-more-file-info)
(define-key map (kbd "C-c + a") 'diredc-bookmark-add)
(define-key map (kbd "C-c + j") 'diredc-bookmark-jump)
(define-key map (kbd "C-c + e") 'diredc-bookmark-edit)
; (define-key map (kbd "C-c + v") 'diredc-vc-jump) ; TODO: Not certain I want this...
(define-key map (kbd "E") 'diredc-wdired)
(define-key map (kbd "e") 'diredc-wdired)
(define-key map (kbd "s") 'diredc-sort-or-edit)
(define-key map (kbd "'") 'diredc-shell)
(define-key map (kbd "&") 'diredc-do-async-shell-command)
(define-key map (kbd "C-c !") 'diredc-shell)
(define-key map (kbd "C-k") 'diredc-trash-quick-delete)
(define-key map (kbd "C-<delete> ?") 'diredc-trash-key-assist)
(define-key map (kbd "C-<delete> SPC") 'diredc-trash-toggle)
(define-key map (kbd "C-<delete> <insertchar>") 'diredc-trash-toggle)
(define-key map (kbd "C-<delete> j") 'diredc-trash-view) ; jump to files-dir
(define-key map (kbd "C-<delete> v") 'diredc-trash-view) ; jump to files-dir
(define-key map (kbd "C-<delete> i") 'diredc-trash-info) ; report trash size
(define-key map (kbd "C-<delete> C-<delete>") 'diredc-trash-empty)
(define-key map (kbd "C-<delete> x") 'diredc-trash-empty)
(define-key map (kbd "C-<delete> r") 'diredc-trash-restore)
(define-key map [remap beginning-of-buffer] 'diredc-beginning-of-buffer)
(define-key map [remap end-of-buffer] 'diredc-end-of-buffer)
(define-key map (kbd "\{") 'diredc-collapse-mode)
(define-key map (kbd "\}") 'diredc-collapse-mode)
map))
(defvar diredc-mode-map (diredc--create-keymap))
(defun diredc-browse--create-keymap ()
"Internal function for `diredc'.
Create a new symbol `diredc-browse-mode' keymap. Returns a
keymap."
(let ((map (make-sparse-keymap)))
(set-keymap-parent map view-mode-map)
(define-key map "\t" 'diredc-browse-tab)
(define-key map (kbd "<backtab>") 'diredc-browse-backtab)
(define-key map (kbd "M-u") 'diredc-swap-windows)
(define-key map (kbd "C-x q") 'diredc-browse-quit)
(define-key map [remap View-quit] 'diredc-browse-quit)
(define-key map [remap View-kill-and-leave] 'diredc-browse-quit)
(define-key map [remap View-leave] 'diredc-browse-quit)
(define-key map [remap View-quit-all] 'diredc-browse-quit)
(define-key map [remap View-exit] 'diredc-browse-find)
(define-key map [remap View-exit-and-edit] 'diredc-browse-find)
(define-key map (kbd "C-q") 'diredc-exit) ; defalias diredc-quit
map))
(defvar diredc-browse-mode-map (diredc-browse--create-keymap))
;;
;;; Constants:
(defconst diredc--version 1.6
"Current version of `diredc'.")
;; diredc--identify-trash-directory: This function is here, seemingly
;; out of place, because its only purpose is to define several
;; constants, below.
(defun diredc--identify-trash-directory (&optional subdir)
"Return the path to the \"Trash\" directory.
This is an internal `diredc' function. You should never need to
call this function. Instead, use the constants `diredc-trash-dir'
`diredc-trash-files-dir', `diredc-trash-info-dir', or
`diredc-trash-epunged-dir'.
Optional SUBDIR is a string of a subdirectory to return. Note the
behavior of this argument is subject to change as information
about more \"Trash\" standards, uh, accumulate. The idea would be
to generically distinguish between \"files\", \"info\", and
\"expunged\" content subdirectories (if they exist), wherever they
may be and whatever they may be called."
(file-name-as-directory
(cond (trash-directory
trash-directory)
((fboundp 'system-trash-directory)
(system-trash-directory))
(t ;; https://freedesktop.org/wiki/Specifications/trash-spec
(expand-file-name
(if (member subdir '("files" "info" "expunged"))
subdir
"")
(directory-file-name
(expand-file-name "Trash"
(or (getenv "XDG_DATA_HOME")
"~/.local/share"))))))))
(defconst diredc-trash-dir (diredc--identify-trash-directory)
"The \"Trash\" root directory.")
(defconst diredc-trash-files-dir (diredc--identify-trash-directory "files")
"The \"Trash\" directory with the actual trashed files.")
(defconst diredc-trash-info-dir (diredc--identify-trash-directory "info")
"The \"Trash\" directory with the trashed files' metadata.")
(defconst diredc-trash-expunged-dir (diredc--identify-trash-directory "expunged")
"The \"Trash\" directory with the \"expunged\" information.")
(defconst diredc-face-chmod-font-lock-dir 'diredc-face-chmod-font-lock-dir
"Face definition for how we apply `font-lock' keywords.
Applicable when variable `diredc-bonus-configuration' is non-nil.")
(defconst diredc-face-chmod-font-lock-read 'diredc-face-chmod-font-lock-read
"Face definition for how we apply `font-lock' keywords.
Applicable when variable `diredc-bonus-configuration' is non-nil.")
(defconst diredc-face-chmod-font-lock-write 'diredc-face-chmod-font-lock-write
"Face definition for how we apply `font-lock' keywords.
Applicable when variable `diredc-bonus-configuration' is non-nil.")
(defconst diredc-face-chmod-font-lock-exec 'diredc-face-chmod-font-lock-exec
"Face definition for how we apply `font-lock' keywords.
Applicable when variable `diredc-bonus-configuration' is non-nil.")
(defconst diredc--chmod-font-lock-regexp
" \\([dl-]\\)\\([r-]\\)\\([w-]\\)\\([xsgt-]\\)\\([r-]\\)\\([w-]\\)\\([xsgt-]\\)\\([r-]\\)\\([w-]\\)\\([xsgt-]\\)\\+? "
"Regexp to identify alphanumeric permission mode strings.
This constant is used to colorize the string, using `font-lock',
when variable `diredc-bonus-configuration' is non-nil.")
(defconst diredc--chmod-font-lock-keyword
(list
(list diredc--chmod-font-lock-regexp
'(1 diredc-face-chmod-font-lock-dir t t)
'(2 diredc-face-chmod-font-lock-read t t)
'(3 diredc-face-chmod-font-lock-write t t)
'(4 diredc-face-chmod-font-lock-exec t t)
'(5 diredc-face-chmod-font-lock-read t t)
'(6 diredc-face-chmod-font-lock-write t t)
'(7 diredc-face-chmod-font-lock-exec t t)
'(8 diredc-face-chmod-font-lock-read t t)
'(9 diredc-face-chmod-font-lock-write t t)
'(10 diredc-face-chmod-font-lock-exec t t)))
"Diredc `font-lock' keyword definition for chmod strings.
Applicable when variable `diredc-bonus-configuration' is non-nil.")
(defconst diredc--sort-options
;; prompt sort= reverse up LC_COLLATE column
'(("none" "none" nil nil nil nil)
("name (a-z)" nil nil t nil nil)
("name (z-a)" nil t nil nil nil)
("name (A-Za-z)" nil nil t t nil)
("name (z-aZ-A)" nil t nil t nil)
("size (decreasing)" "size" nil nil nil nil)
("size (increasing)" "size" t t nil nil)
("time (newest first)" "time" nil nil nil nil)
("time (oldest first)" "time" t t nil nil)
("version (decreasing)" "version" nil nil nil nil)
("version (increasing)" "version" t t nil nil)
("chmod (a-z)" "chmod" nil t nil "\\1")
("chmod (z-a)" "chmod" t t nil "\\1")
("owner (a-z)" "owner" nil t nil "\\3")
("owner (z-a)" "owner" t t nil "\\3")
("group (a-z)" "group" nil t nil "\\4")
("group (z-a)" "group" t t nil "\\4")
("extension (a-z)" "extension" nil t nil nil)
("extension (z-a)" "extension" t nil nil nil)
("extension (A-Za-z)" "extension" nil t t nil)
("extension (Z-Az-a)" "extension" t nil t nil))
"List of options for function `diredc-sort-or-edit'.
See there and your version of \"man(1) ls\".")
(defconst diredc--sort-columns-regexp
"^ \\([-ldrwxgst]\\{10\\}\\) +\\([^ ]+\\) +\\([^ ]+\\) +\\([^ ]+\\).*$"
"Description of `diredc' line that we will parse for sorting by
chmod, owner, or group. See function `diredc-sort-or-edit'.")
(defconst diredc--update-interval-default 0.5
"Default update interval, in seconds, for `diredc' buffers.")
;;
;;; Customization variables:
(defgroup diredc nil
"Settings for the dired-commander suite of packages."
:group 'dired
:prefix "diredc-")
(defcustom diredc-allow-duplicate-buffers t
"Allow multiple `dired' buffers to visit the same directory.
This must be set NON-NIL for `diredc' to work properly, and
evaluating \\<global-keymap> \\[diredc] sets this variable
non-nil for the session."
:type 'boolean
:package-version '(diredc . "1.0"))
(defcustom diredc-bookmarks '()
"List of bookmarked directories for `diredc'.
Each entry is a CONS, whose CAR is a directory, and whose CDR is
a short descriptive string.
See also functions `diredc-bookmark-add' and
`diredc-bookmark-jump'."
:type '(repeat (cons (directory :tag "Directory to bookmark")
(string :tag "Description / Annotation")))
:package-version '(diredc . "1.0"))
(defcustom diredc-hist-select-without-popup nil
"Function `dir-hist-select' should never use package `popup'."
:type 'boolean
:package-version '(diredc . "1.0"))
(defcustom diredc-display-listing-switches-list
'(("classic long" . "-aFlv --group-directories-first --time-style=long-iso")
("long, derefernce links, K sizes" . "-aFlv --block-size=K --group-directories-first")
("long, inode, uid/gid, M sizes" . "-aFinlv --block-size=M --group-directories-first")
("long, sort by extension" . "-aFlvX --group-directories-first"))
; ("classic ls-dired" . "-D")
; ("long, no group, K sizes" . "-oL")
; ("long, security context" . "-alZ")
"How to display a `dired' buffer.
This is merely a list of values suitable for defcustom
`dired-listing-switches', so see there for all the gory details.
Also see the `man' page for `ls'."
:type '(repeat (cons (string :tag "Description")
(string :tag "Listing switches")))
:package-version '(diredc . "1.0"))
(defcustom diredc-show-more-file-info-list
'(("Don't display any additional information" . "")
("Extended Access Control List" . "getfacl -s '%s'")
("Stat command details" . "stat '%s'")
("Extended file attributes" . "getfattr '%s'"))
;; ("Image metadata" . "exif '%s'")
;; ("Image metadata" . "exiftool '%s'")
"Shell commands to be used to display additional information for a file.
Each element of the list is a CONS whose CAR is a descriptive
string, and whose CDR is the text string of a shell command with
the file's name replaced with \"%s\". See command `diredc-show-more-file-info'."
:type '(repeat (cons (string :tag "Description")
(string :tag "Command")))
:package-version '(diredc . "1.0"))
(defcustom diredc-display-select-without-popup nil
"Function `diredc-display-select' should never use package `popup'."
:type 'boolean
:package-version '(diredc . "1.0"))
(defcustom diredc-async-processes-are-persistent t
"Whether spawned asynchronous processes out-live Emacs.
When non-NIL, asynchronous processes spawned via
`dired-do-async-shell-command' will survive even after exiting
Emacs. However, because *Async Shell Command* buffers will not be
spawned, STDOUT and STDERR for the process will be lost.
Even when this variable is non-NIL, the non-persistent behavior
can be chosen at run-time by prefixing the process command with a
SPACE, thus spawning an *Async Shell Command* buffer and logging
there STDOUT and STDERR for the process. See
`diredc-do-async-shell-command'."
:type 'boolean
:package-version '(diredc . "1.0"))
(defcustom diredc-bonus-configuration t
"Supplemental configuration for `diredc' buffers.
Dired was developed more than 25 years ago. Around it have
developed very many configuration options and also very many
opinions about those options. Setting this variable non-nil
enables those options that the `diredc' developer feels are sane
and desirable for a newcomer to `dired'. For exactly what it
does, see function `diredc-bonus-configuration'."
:type 'boolean
:package-version '(diredc . "1.0"))
(defcustom diredc-header-line t
"Whether to display a header line.
This will summarize the number and size of marked items."
:type 'boolean
:package-version '(diredc . "1.0"))
(defcustom diredc-thousands-separator ","
"How to divide long numbers for readability.
This is in lieu of getting the information from environment
variable LC_NUMERIC."
;; ref: https://lists.gnu.org/archive/html/emacs-devel/2021-06/msg00139.html
:type 'string
:package-version '(diredc . "1.0"))
(defcustom diredc-update nil
"Whether to use the `diredc' buffer update feature.
This feature currently defaults to OFF because of some unresolved
buggy behavior: 1) After the minibuffer is used, it moves POINT
of the unselected `diredc' buffer to POINT-MIN; 2) It unsets the
highlighting of `hl-line-mode' in the selected `diredc' buffer.
When this variable is non-NIL, `diredc' buffers will be
reverted every `diredc-update-interval' seconds. This mitigates a
deficiency of `Dired' in that it relies upon `file-notify' to
trigger buffer updates. However, `file-notify' does not trigger
upon events that change the size of a file that is listed in a
`Dired' buffer. Thus, for example, when a tar archive is created,
vanilla `Dired' records its size as zero, and it will remain so
until some other action triggers a `revert-buffer' event."
:type 'boolean
:package-version '(diredc . "1.6"))
(defcustom diredc-update-interval diredc--update-interval-default
"How often, in seconds, to update `diredc' buffers.
Positive integers or floating point numbers are acceptable.
See function `diredc--update-control' for details."
:type '(float
:validate
(lambda (w)
(when (> 0 (floor (widget-value w)))
(widget-put w :error "Positive number required.")
w)))
:set (lambda (sym val)
(set-default-toplevel-value sym val)
(when (bound-and-true-p diredc-mode)
(diredc--update-control 'start)))
:package-version '(diredc . "1.6"))
(defgroup diredc-frame nil
"GUI Emacs settings."
:group 'diredc)
(defcustom diredc-frame-parameters '()
"Desired frame parameters for the diredc frame.
An alist of cons (PARAMETER . VALUE).
See function `make-frame' and (info \"(elisp) Frame Parameters\")."
;; ie. eval (info "(elisp) Frame Parameters")
:type '(repeat (cons (symbol :tag "Frame parameter")
(sexp :tag "Value")))
:package-version '(diredc . "1.2")
:group 'diredc-frame)
(defcustom diredc-frame-inherited-parameters
'(left top width height)
"Features of parent frame to retain for `diredc' frame.
A list of symbols of parameters.
See (info \"(elisp) Frame Parameters\")."
;; ie. eval (info "(elisp) Frame Parameters")
:type '(repeat (symbol :tag "Frame parameter"))
:package-version '(diredc . "1.2")
:group 'diredc-frame)
(defgroup diredc-shell nil
"Pop-up shell settings."
:group 'diredc)
(defcustom diredc-shell-guess-fallback '("xdg-open")
"Universal fallback suggested command(s).
This offers a final option if no matching regexp is found in
either `dired-guess-shell-alist-default' or
`dired-guess-shell-alist-default'.
The value is a list of COMMAND where each COMMAND can either be a
string or a Lisp expression that evaluates to a string. If this
expression needs to consult the name of the file for which the
shell commands are being requested, it can access that file name
as the variable \"file\". If several COMMANDs are given, the first
one will be the default and the rest will be added temporarily to
the history and can be retrieved with \\<minibuffer-local-map>
\\[previous-history-element].
IMPORTANT: This feature requires function `dired-guess-default'
be advised by `diredc--advice--shell-guess-fallback', as follows:
(advice-add \\'dired-guess-default
:around #\\'diredc--advice--shell-guess-fallback)
It may have already been done for you. In order to undo it,
perform:
(advice-remove \\'dired-guess-default
#\\'diredc--advice--shell-guess-fallback)"
:type '(repeat sexp)
:package-version '(diredc . "1.0")
:group 'diredc-shell)
(defcustom diredc-shell-lines 15
"Number of lines for a `diredc' shell window."
:type 'integer
:package-version '(diredc . "1.0")
:group 'diredc-shell)
(defcustom diredc-shell-list
'(("POSIX shell" diredc-shell--launch-shell "/bin/sh")
("bash shell" diredc-shell--launch-shell "/bin/bash")
("zsh shell" diredc-shell--launch-shell "/bin/zsh")
("eshell" diredc-shell--launch-eshell "")