-
Notifications
You must be signed in to change notification settings - Fork 47
/
dirvish.el
1313 lines (1191 loc) · 61.6 KB
/
dirvish.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
;;; dirvish.el --- A modern file manager based on dired mode -*- lexical-binding: t -*-
;; Copyright (C) 2021-2022 Alex Lu
;; Author : Alex Lu <https://github.com/alexluigit>
;; Version: 2.0.53
;; Keywords: files, convenience
;; Homepage: https://github.com/alexluigit/dirvish
;; SPDX-License-Identifier: GPL-3.0-or-later
;; Package-Requires: ((emacs "27.1") (transient "0.3.7"))
;; This file is not part of GNU Emacs.
;;; Commentary:
;; A minimalistic yet versatile file manager based on Dired.
;; This package gives Dired the following features:
;;
;; - Multiple window layouts
;; - Always available file preview
;; - Isolated sessions
;; - A modern and composable user interface
;;; Code:
(require 'dired)
(require 'transient)
(declare-function ansi-color-apply-on-region "ansi-color")
(declare-function dirvish-fd-find "dirvish-fd")
(declare-function dirvish-noselect-tramp "dirvish-extras")
;;;; User Options
(defgroup dirvish nil "A better Dired." :group 'dired)
(defcustom dirvish-attributes '(file-size)
"File attributes showing in file lines.
Dirvish ships with these attributes:
- `subtree-state': A indicator for directory expanding state.
- `all-the-icons': File icons provided by `all-the-icons.el'.
- `nerd-icons': File icons provided by `nerd-icons.el'.
- `vscode-icon': File icons provided by `vscode-icon.el'.
- `collapse': Collapse unique nested paths.
- `git-msg': Append git commit message to filename.
- `vc-state': The version control state at left fringe.
- `file-size': file size or directories file count at right fringe.
- `file-time': Show file modification time before the `file-size'."
:group 'dirvish :type '(repeat (symbol :tag "Dirvish attribute")))
(defcustom dirvish-preview-dispatchers '(image gif video audio epub archive pdf)
"List of preview dispatchers.
Each dispatcher in this list handles the validation and preview
content generation for the corresponding filetype.
The default value contains:
- image: preview image files, requires `imagemagick'.
- gif: preview GIF image files with animation.
- video: preview videos files with thumbnail, requires `ffmpegthumbnailer'.
- audio: preview audio files with metadata, requires `mediainfo'.
- epub: preview epub documents, requires `epub-thumbnailer'.
- pdf: preview pdf documents via `pdf-tools'.
- archive: preview archive files such as .tar, .zip, requires `tar' / `unzip'."
:group 'dirvish :type '(repeat (symbol :tag "Dirvish preview methods")))
(defcustom dirvish-preview-disabled-exts '("iso" "bin" "exe" "gpg" "elc" "eln")
"Do not preview files end with these extensions."
:group 'dirvish :type '(repeat (string :tag "File name extension")))
(defcustom dirvish-preview-environment
'((inhibit-message . t) (non-essential . t) (delay-mode-hooks . t)
(enable-dir-local-variables . nil) (enable-local-variables . :safe))
"Variables which are bound for default file preview dispatcher.
Credit: copied from `consult-preview-variables' in `consult.el'."
:group 'dirvish :type 'alist)
(defcustom dirvish-cache-dir
(expand-file-name "dirvish/" user-emacs-directory)
"Preview / thumbnail cache directory for dirvish."
:group 'dirvish :type 'string)
(defcustom dirvish-default-layout '(1 0.11 0.55)
"Default layout recipe for fullscreen Dirvish sessions.
The value has the form (DEPTH MAX-PARENT-WIDTH PREVIEW-WIDTH).
DEPTH controls the number of windows displaying parent
directories. It can be 0 if you don't need the parent
directories. MAX-PARENT-WIDTH controls the max width allocated
to each parent windows. PREVIEW-WIDTH controls the width
allocated to preview window. The default value provides a
1:3:5 (approximately) pane ratio. Also see
`dirvish-layout-recipes' in `dirvish-extras.el'."
:group 'dirvish :type '(list (integer :tag "number of parent windows")
(float :tag "max width of parent windows")
(float :tag "width of preview windows")))
(defface dirvish-hl-line
'((t :inherit highlight :extend t))
"Face for Dirvish line highlighting."
:group 'dirvish)
(define-obsolete-variable-alias 'dirvish-mode-line-position 'dirvish-use-mode-line "Aug 5, 2022")
(defcustom dirvish-use-mode-line t
"Whether to display mode line in dirvish buffers.
The valid value are:
- nil: hide mode line in dirvish sessions
- global: display the mode line across all panes
- t (and others): Display the mode line across directory panes"
:group 'dirvish
:type '(choice (const :tag "Do not show the mode line" nil)
(const :tag "Display the mode line across directory panes" t)
(const :tag "Make the mode line span all panes" global)))
(define-obsolete-variable-alias 'dirvish-header-line-position 'dirvish-use-header-line "Aug 5, 2022")
(defcustom dirvish-use-header-line t
"Like `dirvish-use-mode-line', but for header line."
:group 'dirvish :type 'symbol)
(defcustom dirvish-mode-line-height 30
"Height of Dirvish's mode line.
The value should be a cons cell (H-WIN . H-FRAME), where H-WIN
and H-FRAME represent the height of mode line in single window
state and fullframe state respectively. If this value is a
integer INT, it is seen as a shorthand for (INT . INT)."
:group 'dirvish
:type '(choice interger (cons integer integer)))
(defcustom dirvish-header-line-height 30
"Like `dirvish-mode-line-height', but for header line."
:type '(choice interger (cons integer integer)))
(defcustom dirvish-mode-line-format
'(:left (sort omit symlink) :right (index))
"Mode line SEGMENTs aligned to left/right respectively.
Here are all the predefined segments you can choose from:
* Basics (from `dirvish-extras')
`path': directory path under the cursor.
`symlink': target of symlink under the cursor.
`sort': sort criteria applied in current buffer.
`omit': a `dired-omit-mode' indicator.
`index': line number / total line count.
`free-space': amount of free space on `default-directory''s file system.
Others are self-explanatory:
`file-size', `file-modes', `file-link-number', `file-user',
`file-group',`file-time',`file-inode-number',`file-device-number'.
* Miscs
`vc-info': version control information (from `dirvish-vc').
`yank': file transfer progress (from `dirvish-yank').
Set it to nil to use the default `mode-line-format'."
:group 'dirvish :type 'plist)
(defcustom dirvish-header-line-format
'(:left (path) :right ())
"Like `dirvish-mode-line-format', but for header line ."
:group 'dirvish :type 'plist)
(defcustom dirvish-hide-details t
"Whether to hide detailed information on session startup."
:group 'dirvish :type 'boolean)
(defcustom dirvish-hide-cursor t
"Whether to hide cursor in dirvish buffers."
:group 'dirvish :type 'boolean)
(defconst dirvish-emacs-bin
(cond
((and invocation-directory invocation-name)
(expand-file-name (concat (file-name-as-directory invocation-directory) invocation-name)))
((eq system-type 'darwin)
"/Applications/Emacs.app/Contents/MacOS/Emacs")
(t "emacs")))
(defconst dirvish-image-exts '("webp" "wmf" "pcx" "xif" "wbmp" "vtf" "tap" "s1j" "sjp" "sjpg" "s1g" "sgi" "sgif" "s1n" "spn" "spng" "xyze" "rgbe" "hdr" "b16" "mdi" "apng" "ico" "pgb" "rlc" "mmr" "fst" "fpx" "fbs" "dxf" "dwg" "djv" "uvvg" "uvg" "uvvi" "uvi" "azv" "psd" "tfx" "t38" "svgz" "svg" "pti" "btf" "btif" "ktx2" "ktx" "jxss" "jxsi" "jxsc" "jxs" "jxrs" "jxra" "jxr" "jxl" "jpf" "jpx" "jpgm" "jpm" "jfif" "jhc" "jph" "jpg2" "jp2" "jls" "hsj2" "hej2" "heifs" "heif" "heics" "heic" "fts" "fit" "fits" "emf" "drle" "cgm" "dib" "bmp" "hif" "avif" "avcs" "avci" "exr" "fax" "icon" "ief" "jpg" "macp" "pbm" "pgm" "pict" "png" "pnm" "ppm" "ras" "rgb" "tga" "tif" "tiff" "xbm" "xpm" "xwd" "jpe" "jpeg" "cr2" "arw"))
(defconst dirvish-audio-exts '("ape" "stm" "s3m" "ra" "rm" "ram" "wma" "wax" "m3u" "med" "669" "mtm" "m15" "uni" "ult" "mka" "flac" "axa" "kar" "midi" "mid" "s1m" "smp" "smp3" "rip" "multitrack" "ecelp9600" "ecelp7470" "ecelp4800" "vbk" "pya" "lvp" "plj" "dtshd" "dts" "mlp" "eol" "uvva" "uva" "koz" "xhe" "loas" "sofa" "smv" "qcp" "psid" "sid" "spx" "opus" "ogg" "oga" "mp1" "mpga" "m4a" "mxmf" "mhas" "l16" "lbc" "evw" "enw" "evb" "evc" "dls" "omg" "aa3" "at3" "atx" "aal" "acn" "awb" "amr" "ac3" "ass" "aac" "adts" "726" "abs" "aif" "aifc" "aiff" "au" "mp2" "mp3" "mp2a" "mpa" "mpa2" "mpega" "snd" "vox" "wav"))
(defconst dirvish-video-exts '("f4v" "rmvb" "wvx" "wmx" "wmv" "wm" "asx" "mk3d" "mkv" "fxm" "flv" "axv" "webm" "viv" "yt" "s1q" "smo" "smov" "ssw" "sswf" "s14" "s11" "smpg" "smk" "bk2" "bik" "nim" "pyv" "m4u" "mxu" "fvt" "dvb" "uvvv" "uvv" "uvvs" "uvs" "uvvp" "uvp" "uvvu" "uvu" "uvvm" "uvm" "uvvh" "uvh" "ogv" "m2v" "m1v" "m4v" "mpg4" "mp4" "mjp2" "mj2" "m4s" "3gpp2" "3g2" "3gpp" "3gp" "avi" "mov" "movie" "mpe" "mpeg" "mpegv" "mpg" "mpv" "qt" "vbs"))
(defconst dirvish-media-exts (append dirvish-image-exts dirvish-video-exts dirvish-audio-exts '("pdf" "epub" "gif")))
(defcustom dirvish-open-with-programs
(let ((mpv (or (executable-find "mpv") "mpv")))
`((,dirvish-audio-exts . (,mpv "--profile=builtin-pseudo-gui" "%f"))
(,dirvish-video-exts . (,mpv "%f"))))
"Open certain file types using external programs.
The value should be an association list where each element is of
the form (EXTS . (CMD . ARGS)). EXTS is a list of file name
extensions. When opening a file whose filename ends with one of
the EXTS using `dired-find-file', a subprocess according to CMD
and its ARGS is issued to open the file externally. The special
placeholder \"%f\" in the ARGS is replaced by the FILENAME at
runtime. Set it to nil disables this feature."
:group 'dirvish
:type '(alist :key-type ((repeat string) :tag "File extensions")
:value-type ((repeat string) :tag "External command and args")))
(defcustom dirvish-reuse-session t
"Whether to reuse the hidden sessions.
If non-nil, Dirvish keeps the session's last buffer alive on
exit. The hidden session can be reused in the future by command
`dirvish' and friends. If the value is \\='resume, dirvish
exhibits the last entry of the hidden session unless the PATH
argument is specified via prompt."
:group 'dirvish :type '(choice (const :tag "Do not reuse the session, quit it completely" nil)
(const :tag "Reuse the session and open new path when reusing" t)
(const :tag "Reuse the session and resume its last entry when reusing" resume)))
(defcustom dirvish-redisplay-debounce 0.02
"Input debounce for dirvish UI redisplay.
The UI of dirvish is refreshed only when there has not been new
input for `dirvish-redisplay-debounce' seconds."
:group 'dirvish :type 'float)
(cl-defgeneric dirvish-clean-cache () "Clean cache for selected files." nil)
(cl-defgeneric dirvish-build-cache () "Build cache for current directory." nil)
(defcustom dirvish-after-revert-hook '(dirvish-clean-cache)
"Executed after `revert-buffer'."
:group 'dirvish :type 'hook)
(defcustom dirvish-setup-hook '(dirvish-build-cache)
"Executed after the Dired buffer is showed up."
:group 'dirvish :type 'hook)
(defcustom dirvish-find-entry-hook '(dirvish-insert-entry-h)
"Executed after finding a entry."
:group 'dirvish :type 'hook)
;;;; Internal variables
(defvar dirvish-scopes '(:frame selected-frame :tab tab-bar--current-tab-index
:persp get-current-persp :perspective persp-curr))
(defvar dirvish-libraries
'((dirvish-widgets path symlink sort omit index free-space file-link-number
file-user file-group file-time file-size file-modes
file-inode-number file-device-number
audio image gif video epub pdf pdf-preface archive)
(dirvish-vc vc-state git-msg vc-diff vc-blame vc-log vc-info)
(dirvish-icons all-the-icons nerd-icons vscode-icon)
(dirvish-collapse collapse)
(dirvish-subtree subtree-state)
(dirvish-yank yank)))
(defvar dirvish-mode-map
(let ((map (make-sparse-keymap)))
(set-keymap-parent map dired-mode-map)
(define-key map (kbd "?") 'dirvish-dispatch)
(define-key map (kbd "q") 'dirvish-quit) map)
"Keymap used in dirvish buffers.")
(defvar dirvish-redisplay-debounce-timer nil)
(defvar dirvish--history nil)
(defvar dirvish--reset-keywords '(:free-space :content-begin))
(defvar dirvish--selected-window nil)
(defvar dirvish--mode-line-fmt nil)
(defvar dirvish--header-line-fmt nil)
(defvar dirvish--session-hash (make-hash-table))
(defvar dirvish--parent-hash (make-hash-table :test #'equal))
(defvar dirvish--this nil)
(defvar dirvish--available-attrs '())
(defvar dirvish--available-preview-dispatchers '())
(defvar dirvish--working-attrs '())
(defvar dirvish--working-preview-dispathchers '())
(defvar image-dired-thumbnail-buffer)
(defvar-local dirvish--props '())
(defvar-local dirvish--attrs-hash nil)
;;;; Macros
(defmacro dirvish-prop (prop &rest body)
"Retrive PROP from `dirvish--props'.
Set the PROP with BODY if given."
(declare (indent defun))
`(let* ((pair (assq ,prop dirvish--props)) (val (cdr pair)))
,(if body `(prog1 (setq val ,@body)
(if pair (setcdr (assq ,prop dirvish--props) val)
(push (cons ,prop val) dirvish--props)))
`val)))
(defmacro dirvish-debounce (label &rest body)
"Debouncing the execution of BODY under LABEL.
The BODY runs only when there has not been new input for DEBOUNCE
seconds. DEBOUNCE defaults to `dirvish-redisplay-debounce'."
(declare (indent defun))
(setq label (or label "redisplay"))
(let* ((debounce (intern (format "dirvish-%s-debounce" label)))
(timer (intern (format "dirvish-%s-debounce-timer" label)))
(fn `(lambda () (ignore-errors ,@body))))
`(progn
(and (timerp ,timer) (cancel-timer ,timer))
(setq ,timer (run-with-idle-timer ,debounce nil ,fn)))))
(defmacro dirvish-define-attribute (name docstring &rest body)
"Define a Dirvish attribute NAME.
An attribute contains a pair of predicate/rendering functions
that are being called on `post-command-hook'. The predicate fn
takes current DV as argument and is executed once. When it
evaluates to t, the rendering fn runs BODY for every line with
following arguments:
- `f-beg' from `dired-move-to-filename'
- `f-end' from `dired-move-to-end-of-filename'
- `f-str' from (`buffer-substring' F-BEG F-END)
- `f-name' from `dired-get-filename'
- `f-attrs' from `file-attributes'
- `f-type' from `file-directory-p' along with `file-symlink-p'
- `l-beg' from `line-beginning-position'
- `l-end' from `line-end-position'
- `hl-face' a face that is only passed in on current line
DOCSTRING is the docstring for the attribute. An optional
`:width' keyword is used to declare the length of the attribute."
(declare (indent defun) (doc-string 2))
(let ((ov (intern (format "dirvish-%s-ov" name)))
(render (intern (format "dirvish-attribute-%s-rd" name)))
(args '(f-beg f-end f-str f-name f-attrs f-type l-beg l-end hl-face))
options)
(while (keywordp (car body)) (dotimes (_ 2) (push (pop body) options)))
(setq options (reverse options))
`(progn
(add-to-list
'dirvish--available-attrs
(cons ',name '(,(or (plist-get options :index) 0)
,(or (plist-get options :width) 0)
,(or (plist-get options :when) t)
,render ,ov ,docstring)))
(defun ,render ,args (ignore ,@args) ,@body))))
(defmacro dirvish-attribute-cache (file attribute &rest body)
"Get FILE's ATTRIBUTE from `dirvish--attrs-hash'.
When the attribute does not exist, set it with BODY."
(declare (indent defun))
`(let* ((md5 (intern (secure-hash 'md5 ,file)))
(hash (gethash md5 dirvish--attrs-hash))
(cached (plist-get hash ,attribute))
(attr (or cached ,@body)))
(unless cached
(puthash md5 (append hash (list ,attribute attr)) dirvish--attrs-hash))
attr))
(cl-defmacro dirvish-define-preview (name &optional arglist docstring &rest body)
"Define a Dirvish preview dispatcher NAME.
A dirvish preview dispatcher is a function consumed by
`dirvish-preview-dispatch' which takes `file' (filename under
the cursor) and `preview-window' as ARGLIST. DOCSTRING and BODY
is the docstring and body for this function."
(declare (indent defun) (doc-string 3))
(let* ((dp-name (intern (format "dirvish-%s-dp" name)))
(default-arglist '(file ext preview-window dv))
(ignore-list (cl-set-difference default-arglist arglist))
(keywords `(:doc ,docstring)))
(while (keywordp (car body)) (dotimes (_ 2) (push (pop body) keywords)))
`(progn
(add-to-list
'dirvish--available-preview-dispatchers (cons ',name ',keywords))
(defun ,dp-name ,default-arglist (ignore ,@ignore-list) ,@body))))
(defmacro dirvish-define-mode-line (name &optional docstring &rest body)
"Define a mode line segment NAME with BODY and DOCSTRING."
(declare (indent defun) (doc-string 2))
(let ((ml-name (intern (format "dirvish-%s-ml" name))))
`(defun ,ml-name (dv) ,docstring (ignore dv) ,@body)))
;;;; Helpers
(defun dirvish--hide-dired-header ()
"Hide the Dired header."
(remove-overlays (point-min) (point) 'dired-header t)
(save-excursion
(let* ((beg (goto-char (point-min)))
(next-file (next-single-property-change beg 'dired-filename))
(end (or (dirvish-prop :content-begin)
(and (not next-file) (point-max))
(progn (goto-char next-file) (line-beginning-position))))
(o (make-overlay beg end)))
(dirvish-prop :content-begin end)
(overlay-put o 'dired-header t)
(overlay-put o 'invisible
(cond ((cdr dired-subdir-alist) nil)
(dirvish-use-header-line t))))))
(defun dirvish--display-buffer (buffer alist)
"Try displaying BUFFER with ALIST.
This splits the window at the designated side of the frame.
ALIST is window arguments passed to `window--display-buffer'."
(let* ((side (cdr (assq 'side alist)))
(window-configuration-change-hook nil)
(width (or (cdr (assq 'window-width alist)) 0.5))
(height (cdr (assq 'window-height alist)))
(size (or height (ceiling (* (frame-width) width))))
(split-width-threshold 0)
(ignore-window-parameters t)
(new-window (split-window-no-error nil size side)))
(window--display-buffer buffer new-window 'window alist)))
(defun dirvish--kill-buffer (buffer)
"Kill BUFFER without side effects."
(and (buffer-live-p buffer)
(cl-letf (((symbol-function 'undo-tree-save-history-from-hook) #'ignore)
((symbol-function 'recentf-track-closed-file) #'ignore))
(let (kill-buffer-query-functions) (kill-buffer buffer)))))
(defun dirvish--get-project-root (&optional directory)
"Get project root path of DIRECTORY."
(when-let* ((pj (project-current nil directory))
(pj-root (car (with-no-warnings (project-roots pj)))))
(expand-file-name pj-root)))
(defun dirvish--get-parent-path (path)
"Get parent directory of PATH."
(file-name-directory (directory-file-name (expand-file-name path))))
(defun dirvish--append-metadata (metadata completions)
"Append METADATA for minibuffer COMPLETIONS."
(let ((entry (if (functionp metadata)
`(metadata (annotation-function . ,metadata))
`(metadata (category . ,metadata)))))
(lambda (string pred action)
(if (eq action 'metadata)
entry
(complete-with-action action completions string pred)))))
(defun dirvish--window-selected-p (dv)
"Return t if session DV is selected."
(eq (if (car (dv-layout dv)) (dv-root-window dv) (frame-selected-window))
dirvish--selected-window))
(defun dirvish--scopes ()
"Return computed scopes according to `dirvish-scopes'."
(cl-loop for (k v) on dirvish-scopes by 'cddr
append (list k (and (functionp v) (funcall v)))))
(defun dirvish--format-menu-heading (title &optional note)
"Format TITLE as a menu heading.
When NOTE is non-nil, append it the next line."
(let ((no-wb (= (frame-bottom-divider-width) 0)))
(format "%s%s%s"
(propertize title 'face `(:inherit dired-mark :overline ,no-wb)
'display '((height 1.1)))
(propertize " " 'face `(:inherit dired-mark :overline ,no-wb)
'display '(space :align-to right))
(propertize (if note (concat "\n" note) "") 'face 'font-lock-doc-face))))
;;;; Core
(defsubst dirvish-curr ()
"Get selected Dirvish session."
(gethash (dirvish-prop :dv) dirvish--session-hash))
(defun dirvish--util-buffer (type &optional dv no-create inhibit-hiding)
"Return session DV's utility buffer of TYPE (defaults to `temp').
If NO-CREATE is non-nil, do not create the buffer.
If INHIBIT-HIDING is non-nil, do not hide the buffer."
(let* ((id (if dv (format "-%s*" (dv-name dv)) "*"))
(name (format "%s*Dirvish-%s%s" (if inhibit-hiding "" " ") type id)))
(if no-create (get-buffer name) (get-buffer-create name))))
(cl-defstruct (dirvish (:conc-name dv-))
"Define dirvish data type."
(type () :documentation "is the (TYPE FIXED-WIDTH DEDICATED ROOT-WIN-FN FILE-OPEN-FN) struct.")
(layout (cons nil dirvish-default-layout) :documentation "is the working layouts.")
(ls-switches dired-listing-switches :documentation "is the listing switches.")
(root-window nil :documentation "is the main window created by ROOT-WINDOW-FN.")
(scopes () :documentation "are the \"environments\" such as init frame of this session.")
(preview-buffers () :documentation "holds all file preview buffers in this session.")
(preview-window nil :documentation "is the window to display preview buffer.")
(name (cl-gensym) :documentation "is an unique symbol for every session.")
(winconf (current-window-configuration) :documentation "is the saved window configuration.")
(index () :documentation "is a (DIR . CORRESPONDING-BUFFER) cons of ROOT-WINDOW.")
(roots () :documentation "is the list of all INDEXs."))
(defun dirvish--find-reusable (&optional type)
"Return the first matched reusable session with TYPE."
(when dirvish-reuse-session
(cl-loop with scopes = (dirvish--scopes)
for dv in (hash-table-values dirvish--session-hash)
when (and (eq type (car (dv-type dv)))
(equal (dv-scopes dv) scopes))
collect dv)))
(defun dirvish-new (&rest args)
"Create and save a new dirvish struct to `dirvish--session-hash'.
ARGS is a list of keyword arguments for `dirvish' struct."
(let (slots new)
(while (keywordp (car args)) (dotimes (_ 2) (push (pop args) slots)))
(setq new (apply #'make-dirvish (reverse slots)) dirvish--this new)
(puthash (dv-name new) new dirvish--session-hash)
(dirvish--check-deps)
(dirvish--create-root-window new) new))
(defun dirvish-kill (dv)
"Kill the dirvish instance DV."
(let ((index (cdr (dv-index dv))))
(if (not (car (dv-layout dv)))
(cl-loop for (_d . b) in (dv-roots dv) when
(not (get-buffer-window b)) do (kill-buffer b)
finally (setf (dv-index dv) (car (dv-roots dv))))
(when dirvish-use-header-line
(with-current-buffer index
(setq header-line-format dirvish--header-line-fmt)))
(cl-loop for (_d . b) in (dv-roots dv)
when (not (eq b index)) do (kill-buffer b))
(when-let ((wconf (dv-winconf dv))) (set-window-configuration wconf)))
(mapc #'dirvish--kill-buffer (dv-preview-buffers dv))
(cl-loop for b in (buffer-list) for bn = (buffer-name b) when
(string-match-p (format " ?\\*Dirvish-.*-%s\\*" (dv-name dv)) bn)
do (dirvish--kill-buffer b))
(setq dirvish--parent-hash (make-hash-table :test #'equal))
(cond (dirvish-reuse-session (setf (dv-winconf dv) nil))
(t (mapc (pcase-lambda (`(,_ . ,b)) (kill-buffer b)) (dv-roots dv))))
(setq dirvish--this nil)))
(defun dirvish--create-root-window (dv)
"Create root window of DV."
(let* ((fn (or (nth 3 (dv-type dv)) 'frame-selected-window)) (w (funcall fn)))
(setf (dv-root-window dv) w) w))
(defun dirvish--preview-dps-validate (&optional dps)
"Check if the requirements of dispatchers DPS are met."
(cl-loop with dps = (or dps dirvish-preview-dispatchers)
with res = (prog1 '() (require 'recentf) (require 'ansi-color))
with fmt = "[Dirvish]: install '%s' executable to preview %s files."
for dp in (append '(disable) dps '(default))
for info = (alist-get dp dirvish--available-preview-dispatchers)
for requirements = (plist-get info :require)
for met = t
do (progn (dolist (pkg requirements)
(unless (executable-find pkg)
(message fmt pkg dp) (setq met nil)))
(when met (push (intern (format "dirvish-%s-dp" dp)) res)))
finally return (reverse res)))
(defun dirvish--attrs-expand (attrs)
"Expand ATTRS to `dirvish--working-attrs'."
(sort (cl-loop for attr in attrs
for lst = (alist-get attr dirvish--available-attrs)
for (idx width pred render ov _) = lst
collect (list idx (eval width) pred render ov))
(lambda (a b) (< (car a) (car b)))))
(defun dirvish--check-deps ()
"Remove invalid widgets, raise warnings for missing dependencies."
(cl-loop
with (m . h) = (cons dirvish-mode-line-format dirvish-header-line-format)
with (ml-l . ml-r) = (cons (plist-get m :left) (plist-get m :right))
with (hl-l . hl-r) = (cons (plist-get h :left) (plist-get h :right))
with feat-reqs = (append dirvish-preview-dispatchers ml-l ml-r hl-l hl-r)
with attrs = '(hl-line symlink-target)
for (lib . feat) in dirvish-libraries do
(let ((m-attr (cl-intersection feat dirvish-attributes))
(feat-in-lib (cl-intersection feat feat-reqs)))
(when (or m-attr feat-in-lib) (require lib))
(and m-attr (setq attrs (append attrs m-attr))))
finally
(setf dirvish--mode-line-fmt (dirvish--mode-line-fmt-setter ml-l ml-r))
(setf dirvish--header-line-fmt (dirvish--mode-line-fmt-setter hl-l hl-r t))
(setf dirvish--working-preview-dispathchers (dirvish--preview-dps-validate))
(setf dirvish--working-attrs (dirvish--attrs-expand attrs))))
(defun dirvish--render-attrs-1 (height width pos remote fns ov align-to)
"HEIGHT WIDTH POS REMOTE FNS OV ALIGN-TO."
(forward-line (- 0 height))
(cl-dotimes (_ (* (if (eq major-mode 'dired-mode) 2 5) height))
(when (eobp) (cl-return))
(let ((f-beg (dired-move-to-filename))
(f-end (dired-move-to-end-of-filename t))
(l-beg (line-beginning-position)) (l-end (line-end-position))
(f-wid 0) f-str f-name f-attrs f-type hl-face left right)
(setq hl-face
(and (eq (or f-beg l-beg) pos) dirvish-hide-cursor 'dirvish-hl-line))
(when f-beg
(setq f-str (buffer-substring f-beg f-end)
f-wid (string-width f-str)
f-name (concat (dired-current-directory) f-str)
f-attrs (dirvish-attribute-cache f-name :builtin
(unless remote (file-attributes f-name)))
f-type (dirvish-attribute-cache f-name :type
(let ((ch (progn (back-to-indentation) (char-after))))
`(,(if (eq ch 100) 'dir 'file) . nil))))
(unless (get-text-property f-beg 'mouse-face)
(dired-insert-set-properties l-beg l-end)))
(cl-loop
for fn in (if f-beg fns '(dirvish-attribute-hl-line-rd))
for (k . v) = (funcall fn f-beg f-end f-str f-name
f-attrs f-type l-beg l-end hl-face)
do (pcase k ('ov (overlay-put v ov t))
('left (setq left (concat v left)))
('right (setq right (concat v right))))
finally
(prog1 (unless (or left right) (cl-return))
(let* ((len1 (length right))
(remain (- width len1
(or (get-text-property l-beg 'line-prefix) 0)))
(len2 (min (length left) (max 0 (- remain f-wid 1))))
(ovl (make-overlay f-end f-end))
(r-pos (if (> remain f-wid) l-end
(let ((end (+ f-beg remain))
(offset (- f-wid (length f-str))))
(- end offset))))
(spec `(space :align-to (- right-fringe ,len1 ,align-to)))
(spc (propertize " " 'display spec 'face hl-face))
(ovr (make-overlay r-pos r-pos)))
(overlay-put ovl 'dirvish-l-end-ov t)
(overlay-put ovl 'after-string (substring (or left "") 0 len2))
(overlay-put ovr 'dirvish-r-end-ov t)
(overlay-put ovr 'after-string (concat spc right))))))
(forward-line 1)))
(defun dirvish--render-attrs (&optional clear)
"Render or CLEAR attributes in DV's dirvish buffer."
(cl-loop with remote = (dirvish-prop :remote) with gui = (dirvish-prop :gui)
with fns = () with height = (frame-height)
with remain = (- (window-width) (if gui 1 2))
for (_ width pred render ov) in dirvish--working-attrs
do (remove-overlays (point-min) (point-max) ov t)
when (eval pred `((win-width . ,remain)))
do (setq remain (- remain width)) (push render fns)
initially
(remove-overlays (point-min) (point-max) 'dirvish-l-end-ov t)
(remove-overlays (point-min) (point-max) 'dirvish-r-end-ov t)
finally
(with-silent-modifications
(unless clear
(save-excursion
(dirvish--render-attrs-1 height remain (point)
remote fns ov (if gui 0 2)))))))
;;;; Advices
(defun dirvish-find-entry-a (&optional entry)
"Find ENTRY in current dirvish session.
ENTRY can be a filename or a string with format of
`dirvish-fd-bufname' used to query or create a `fd' result
buffer, it defaults to filename under the cursor when it is nil."
(let* ((entry (or entry (dired-get-filename nil t)))
(buffer (cond ((string-prefix-p "🔍" entry) (dirvish-fd-find entry))
((file-directory-p entry) (dired-noselect entry))
((string-suffix-p "/" entry)
(user-error
(concat entry " is not a valid directory"))))))
(if buffer (switch-to-buffer buffer)
(let* ((ext (downcase (or (file-name-extension entry) "")))
(file (expand-file-name entry))
(process-connection-type nil)
(ex (cl-loop
for (exts . (cmd . args)) in dirvish-open-with-programs
thereis (and (not (dirvish-prop :remote))
(executable-find cmd)
(member ext exts)
(append (list cmd) args)))))
(if ex (apply #'start-process "" nil "nohup"
(cl-substitute file "%f" ex :test 'string=))
(let* ((dv (dirvish-curr)) (fn (nth 4 (dv-type dv))))
(if fn (funcall fn) (dirvish-kill dv)))
(find-file file))))))
(defun dirvish-insert-subdir-a (dirname &rest _)
"Setup newly inserted subdir DIRNAME for this Dirvish buffer."
(dirvish--hide-dired-header)
(dirvish-data-for-dir dirname (current-buffer) nil))
(defun dirvish-wdired-enter-a (&rest _)
"Advice for `wdired-change-to-wdired-mode'."
(when dirvish-hide-cursor (dired-move-to-end-of-filename t))
(setq-local cursor-type 'hollow)
(when (boundp 'evil-normal-state-cursor)
(setq-local evil-normal-state-cursor 'hollow))
(dirvish--render-attrs 'clear)
(remove-hook 'window-configuration-change-hook #'dirvish-winconf-change-h t)
(remove-hook 'post-command-hook #'dirvish-update-body-h t))
(defun dirvish-thumb-buf-a (fn)
"Advice for FN `image-dired-create-thumbnail-buffer'."
(when-let ((dv dirvish--this) ((dv-preview-window dv)))
(dirvish--init-session dv)
(with-selected-window (dv-preview-window dv)
(switch-to-buffer image-dired-thumbnail-buffer)))
(let ((buf (funcall fn))
(fun (lambda () (let ((buf (get-text-property
(point) 'associated-dired-buffer)))
(and (buffer-live-p buf)
(with-current-buffer buf (dirvish--render-attrs)))))))
(with-current-buffer buf (add-hook 'post-command-hook fun nil t)) buf))
(defun dirvish-dired-noselect-a (fn dir &optional flags)
"Return buffer for DIR with FLAGS, FN is `dired-noselect'."
(let* ((key (file-name-as-directory (expand-file-name dir)))
(this dirvish--this)
(dv (if (and this (eq this-command 'dired-other-frame)) (dirvish-new)
(or this (car (dirvish--find-reusable)) (dirvish-new))))
(bname buffer-file-name)
(remote (file-remote-p dir))
(flags (or flags (dv-ls-switches dv)))
(buffer (alist-get key (dv-roots dv) nil nil #'equal))
(new-buffer-p (not buffer)))
(if this (set-window-dedicated-p nil nil) (setcar (dv-layout dv) nil))
(when new-buffer-p
(if (not remote)
(let ((dired-buffers nil)) ; disable reuse from dired
(setq buffer (apply fn (list dir flags))))
(require 'dirvish-extras)
(setq buffer (dirvish-noselect-tramp fn dir flags remote)))
(with-current-buffer buffer (dirvish-init-dired-buffer))
(push (cons key buffer) (dv-roots dv))
(push (cons key buffer) dired-buffers))
(with-current-buffer buffer
(cond (new-buffer-p nil)
((and (not remote) (not (equal flags dired-actual-switches)))
(dired-sort-other flags))
((eq dired-auto-revert-buffer t) (revert-buffer))
((functionp dired-auto-revert-buffer)
(when (funcall dired-auto-revert-buffer dir) (revert-buffer))))
(dirvish-prop :dv (dv-name dv))
(dirvish-prop :gui (display-graphic-p))
(dirvish-prop :remote remote)
(dirvish-prop :root key)
(when bname (dired-goto-file bname))
(setf (dv-index dv) (cons key buffer))
(run-hook-with-args 'dirvish-find-entry-hook key buffer)
buffer)))
;;;; Hooks
(defun dirvish-apply-ansicolor-h (_win pos)
"Update dirvish ansicolor in preview window from POS."
(ansi-color-apply-on-region
(goto-char pos) (progn (forward-line (frame-height)) (point))))
(defun dirvish-update-body-h (&optional force)
"Update UI of current Dirvish.
When FORCE, ensure the preview get refreshed."
(when-let ((dv (dirvish-curr)))
(cond ((not dirvish-hide-cursor))
((eobp) (forward-line -1))
((cdr dired-subdir-alist))
((and (bobp) dirvish-use-header-line)
(goto-char (dirvish-prop :content-begin))))
(when dirvish-hide-cursor (dired-move-to-filename))
(dirvish--render-attrs)
(when-let ((filename (dired-get-filename nil t)))
(dirvish-prop :index filename)
(let ((h-buf (dirvish--util-buffer 'header dv t))
(f-buf (dirvish--util-buffer 'footer dv t))
(last-index (dirvish-prop :last-index)))
(dirvish-prop :last-index filename)
(dirvish-debounce nil
(if (not (car (dv-layout dv)))
(and (< emacs-major-version 29) (force-mode-line-update))
(when (and dirvish-use-mode-line (buffer-live-p f-buf))
(with-current-buffer f-buf (force-mode-line-update)))
(when (and dirvish-use-header-line (buffer-live-p h-buf))
(with-current-buffer h-buf (force-mode-line-update)))
(when (or force (not (equal last-index filename)))
(dirvish--preview-update dv filename))))))))
(defun dirvish-insert-entry-h (entry buffer)
"Add ENTRY or BUFFER name to `dirvish--history'."
(let ((entry (if (string-prefix-p "🔍" entry)
(buffer-name buffer) entry)))
(setq dirvish--history (seq-take (push entry dirvish--history) 200))))
(defun dirvish-kill-buffer-h ()
"Remove buffer from session's buffer list."
(when-let ((dv (dirvish-curr)) (buf (current-buffer)))
(let ((win (get-buffer-window buf)))
(when (window-live-p win) (set-window-dedicated-p win nil)))
(setf (dv-roots dv) (cl-remove-if (lambda (i) (eq (cdr i) buf)) (dv-roots dv)))
(unless (dv-roots dv)
(when-let ((layout (car (dv-layout dv)))
(wconf (dv-winconf dv))
((eq buf (window-buffer (selected-window)))))
(set-window-configuration wconf))
(remhash (dv-name dv) dirvish--session-hash)
(cl-loop for b in (buffer-list) for bn = (buffer-name b) when
(string-match-p (format " ?\\*Dirvish-.*-%s\\*" (dv-name dv)) bn)
do (dirvish--kill-buffer b))
(setq dirvish--this nil))))
(defun dirvish-selection-change-h (&optional _frame-or-window)
"Save current session to frame parameters."
(let* ((w (frame-selected-window)) (b (window-buffer w)) (dv (dirvish-curr)))
(cond ((and dv (minibufferp (window-buffer dirvish--selected-window)))
(with-selected-window (dirvish--create-root-window dv)
(switch-to-buffer b)
(dirvish--init-session dv)))
((active-minibuffer-window))
(t (setq dirvish--this dv)))
(setq dirvish--selected-window w)))
(defun dirvish-winconf-change-h ()
"Restore hidden sessions on buffer switching."
(let ((dv (dirvish-curr)))
(setf (dv-root-window dv) (get-buffer-window (cdr (dv-index dv))))
(dirvish-update-body-h 'force-preview-update)))
(defun dirvish-winbuf-change-h (frame-or-window)
"Rebuild layout once buffer in FRAME-OR-WINDOW changed."
(let ((win (frame-selected-window frame-or-window)))
(with-current-buffer (window-buffer win)
(when-let ((dv (dirvish-curr))) (dirvish--init-session dv)))))
(defun dirvish-tab-new-post-h (_tab)
"Do not reuse sessions from other tabs."
(setq dirvish--this nil))
;;;; Preview
(dirvish-define-preview disable (file ext)
"Disable preview in some cases."
(cond
((not (file-exists-p file))
`(info . ,(format "%s does not exist" file)))
((not (file-readable-p file))
`(info . ,(format "%s is not readable" file)))
((member ext dirvish-preview-disabled-exts)
`(info . ,(format "Preview for %s has been disabled" file)))))
(defun dirvish--find-file-temporarily (name)
"Open file NAME temporarily for preview."
(cl-letf (((symbol-function 'recentf-track-opened-file) #'ignore)
((symbol-function 'undo-tree-save-history-from-hook) #'ignore)
((symbol-function 'flycheck-mode-on-safe) #'ignore))
(let* ((vc-follow-symlinks t)
(vars (mapcar (pcase-lambda (`(,k . ,v))
(list k v (default-value k) (symbol-value k)))
dirvish-preview-environment))
(buf (unwind-protect (progn (pcase-dolist (`(,k ,v . ,_) vars)
(set-default k v) (set k v))
(find-file-noselect name 'nowarn))
(pcase-dolist (`(,k ,_ ,d ,v) vars)
(set-default k d) (set k v)))))
(cond ((ignore-errors (buffer-local-value 'so-long-detected-p buf))
(kill-buffer buf)
`(info . ,(format "File `%s' with long lines not previewed" name)))
(t `(buffer . ,buf))))))
(dirvish-define-preview default (file ext)
"Default preview dispatcher for FILE."
(when-let ((attrs (ignore-errors (file-attributes file)))
(size (file-attribute-size attrs)))
(cond ((file-directory-p file) ; default directory previewer
(let* ((script `(with-current-buffer
(progn (setq insert-directory-program
,insert-directory-program)
(dired-noselect ,file "-AlGh"))
(buffer-string)))
(cmd (format "%S" `(message "\n%s" ,script))))
`(dired . (,dirvish-emacs-bin "-Q" "-batch" "--eval" ,cmd))))
((> size (or large-file-warning-threshold 10000000))
`(info . ,(format "File %s is too big for literal preview." file)))
((member ext dirvish-media-exts)
`(info . "Preview disabled for media files"))
(t (dirvish--find-file-temporarily file)))))
(cl-defgeneric dirvish-preview-dispatch (recipe dv)
"Return preview buffer generated according to RECIPE in session DV.")
(cl-defmethod dirvish-preview-dispatch ((recipe (head info)) dv)
"Insert info string from RECIPE into DV's preview buffer."
(let ((buf (dirvish--util-buffer 'preview dv nil t)))
(with-current-buffer buf
(erase-buffer) (remove-overlays) (font-lock-mode -1)
(insert (cdr recipe)) buf)))
(cl-defmethod dirvish-preview-dispatch ((recipe (head buffer)) dv)
"Use payload of RECIPE as preview buffer of DV directly."
(let ((p-buf (dirvish--util-buffer 'preview dv nil t)))
(with-current-buffer p-buf (erase-buffer) (remove-overlays) (cdr recipe))))
(defun dirvish-shell-preview-proc-s (proc _exitcode)
"A sentinel for dirvish preview process.
When PROC finishes, fill preview buffer with process result."
(when-let ((dv (or (dirvish-curr) dirvish--this)))
(with-current-buffer (dirvish--util-buffer 'preview dv nil t)
(erase-buffer) (remove-overlays)
(insert (with-current-buffer (process-buffer proc) (buffer-string)))
(pcase (process-get proc 'cmd-info)
('shell (font-lock-mode -1) (dirvish-apply-ansicolor-h nil (point-min)))
('dired
(setq-local dired-subdir-alist
(list (cons (car (dv-index dv)) (point-min-marker)))
font-lock-defaults
'(dired-font-lock-keywords t nil nil beginning-of-line))
(font-lock-mode 1)
(run-hooks 'dirvish-directory-view-mode-hook)))))
(kill-buffer (process-buffer proc)))
(defun dirvish--run-shell-for-preview (dv recipe)
"Dispatch shell cmd with RECIPE for session DV."
(when-let ((proc (get-buffer-process (get-buffer " *Dirvish-temp*"))))
(delete-process proc))
(let ((buf (dirvish--util-buffer 'preview dv nil t))
(proc (make-process :name "sh-out" :connection-type nil
:buffer " *Dirvish-temp*" :command (cdr recipe)
:sentinel 'dirvish-shell-preview-proc-s)))
(process-put proc 'cmd-info (car recipe))
(with-current-buffer buf (erase-buffer) (remove-overlays) buf)))
(cl-defmethod dirvish-preview-dispatch ((recipe (head shell)) dv)
"Fill DV's preview buffer with output of sh command from RECIPE."
(dirvish--run-shell-for-preview dv recipe))
(cl-defmethod dirvish-preview-dispatch ((recipe (head dired)) dv)
"Fill DV's preview buffer with output of sh command from RECIPE."
(dirvish--run-shell-for-preview dv recipe))
(defun dirvish--preview-update (dv index)
"Update preview content of INDEX for DV."
(when-let* ((window (dv-preview-window dv))
((window-live-p window))
(orig-bufs (buffer-list))
(ext (downcase (or (file-name-extension index) "")))
(buf (cl-loop for fn in dirvish--working-preview-dispathchers
for rcp = (funcall fn index ext window dv) thereis
(and rcp (dirvish-preview-dispatch rcp dv)))))
(setq-local other-window-scroll-buffer buf)
(set-window-buffer window buf)
(unless (memq buf orig-bufs) (push buf (dv-preview-buffers dv)))))
;;;; Builder
(dirvish-define-attribute hl-line
"Highlight current line.
This attribute is enabled when `dirvish-hide-cursor' is non-nil."
(when hl-face
(let ((ov (make-overlay l-beg (1+ l-end))))
(overlay-put ov 'face hl-face) `(ov . ,ov))))
(dirvish-define-attribute symlink-target
"Hide symlink target."
:when (or (eq major-mode 'dirvish-directory-view-mode)
(and dired-hide-details-mode
(default-value 'dired-hide-details-hide-symlink-targets)))
(when (< (+ f-end 4) l-end)
(let ((ov (make-overlay f-end l-end)))
(overlay-put ov 'invisible t) `(ov . ,ov))))
(defun dirvish--mode-line-fmt-setter (left right &optional header)
"Set the `dirvish--mode-line-fmt'.
LEFT and RIGHT are segments aligned to left/right respectively.
If HEADER, set the `dirvish--header-line-fmt' instead."
(cl-labels ((expand (segments)
(cl-loop for s in segments collect
(if (stringp s) s
`(:eval (,(intern (format "dirvish-%s-ml" s)) (dirvish-curr))))))
(get-font-scale ()
(let* ((face (if header 'header-line 'mode-line-inactive))
(defualt (face-attribute 'default :height))
(ml-height (face-attribute face :height)))
(cond ((floatp ml-height) ml-height)
((integerp ml-height) (/ (float ml-height) defualt))
(t 1)))))
`((:eval
(let* ((dv (dirvish-curr))
(buf (and (car (dv-layout dv)) (cdr (dv-index dv))))
(scale ,(get-font-scale))
(win-width (floor (/ (window-width) scale)))
(str-l (format-mode-line
',(or (expand left) mode-line-format) nil nil buf))
(str-r (format-mode-line ',(expand right) nil nil buf))
(len-r (string-width str-r)))
(concat
(dirvish--bar-image (car (dv-layout dv)) ,header)
(if (< (+ (string-width str-l) len-r) win-width)
str-l
(let ((trim (1- (- win-width len-r))))
(if (>= trim 0)
(substring str-l 0 (min trim (1- (length str-l))))
"")))
(propertize
" " 'display
`((space :align-to (- (+ right right-fringe right-margin)
,(ceiling (* scale (string-width str-r)))))))
str-r))))))
;; Thanks to `doom-modeline'.
(defun dirvish--bar-image (fullscreenp header)
"Create a bar image with height of `dirvish-mode-line-height'.
If FULLSCREENP, use the `cdr' of the value as height, otherwise
use `car'. If HEADER, use `dirvish-header-line-height' instead."
(when (and (display-graphic-p) (image-type-available-p 'pbm))
(let* ((hv (if header dirvish-header-line-height dirvish-mode-line-height))
(ht (cond ((numberp hv) hv) (fullscreenp (cdr hv)) (t (car hv)))))
(propertize
" " 'display
(ignore-errors
(create-image
(concat (format "P1\n%i %i\n" 2 ht) (make-string (* 2 ht) ?1) "\n")
'pbm t :foreground "None" :ascent 'center))))))
(defun dirvish--hide-cursor ()
"Hide cursor in dirvish buffer."
(when dirvish-hide-cursor
(setq-local cursor-type nil)
(cond ((and (boundp 'evil-normal-state-cursor) (featurep 'evil))
(setq-local evil-normal-state-cursor '(bar . 0)))
((and (boundp 'meow-cursor-type-default) (featurep 'meow))
(setq-local meow-cursor-type-motion nil
meow-cursor-type-default nil)))))
(defun dirvish--setup-mode-line (layout)
"Setup the mode/header line according to LAYOUT."
(setq mode-line-format
(unless (or layout (not dirvish-use-mode-line))
dirvish--mode-line-fmt)
header-line-format