-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathdirvish.el
1480 lines (1341 loc) · 70.8 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-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
;; Package-Requires: ((emacs "28.1"))
;; 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 'cl-lib)
(eval-when-compile (require 'project))
(declare-function ansi-color-apply-on-region "ansi-color")
(declare-function dirvish-fd-find "dirvish-fd")
(declare-function dirvish-tramp-noselect "dirvish-tramp")
;;;; 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
`(,(if (memq system-type '(ms-dos windows-nt)) 'video-mtn 'video)
image gif audio epub archive pdf dired)
"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' on Linux/macOS | `mtn' on Windows
- 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'.
- dired: preview directories using `emacs --batch'."
: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 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'.
Alternatively, set this to nil to hide both the parent and preview
windows. In this case, \\='(1 0.11 0.55) will be used as the fallback
full-frame recipe. This is useful when you want to use `dirvish'
exclusively for directory entries without `dired' or similar commands,
and want to prevent the preview from appearing at startup. You can
still use `dirvish-layout-toggle' or `dirvish-layout-switch' to display
the full-frame layout when file previews are needed."
:group 'dirvish
:type '(choice (const :tag "no default layout" nil)
(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 used for Dirvish line highlighting."
:group 'dirvish)
(defface dirvish-inactive
'((t :inherit shadow))
"Face used for inactive Dirvish windows."
:group 'dirvish)
(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)))
(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 integer (cons integer integer)))
(defcustom dirvish-header-line-height 30
"Like `dirvish-mode-line-height', but for header line."
:type '(choice integer (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 enable `dired-hide-details-mode' in Dirvish buffers.
When sets to t, it is enabled for all Dirvish buffers.
Alternatively, the value can be a list of symbols to instruct Dirvish in
what contexts it should be enabled. The accepted values are:
- `dired': when opening a directory using `dired-*' commands.
- `dirvish': when opening full-frame Dirvish.
- `dirvish-fd': when the buffer is create by `dirvish-fd*' commands.
- `dirvish-side': when opening Dirvish in the sidebar."
:group 'dirvish
:type '(choice (boolean :tag "Apply to all Dirvish buffers")
(repeat :tag "Apply to a list of buffer types: 'dired, 'dirvish, 'dirvish-fd or 'dirvish-side" symbol)))
(defcustom dirvish-hide-cursor t
"Whether to hide cursor in dirvish buffers.
Works all the same as `dirvish-hide-details' but for cursor."
:group 'dirvish
:type '(choice (boolean :tag "Apply to all Dirvish buffers")
(repeat :tag "Apply to a list of buffer types: 'dired, 'dirvish, 'dirvish-fd or 'dirvish-side" symbol)))
(defcustom dirvish-preview-dired-sync-omit nil
"If non-nil, `dired' preview buffers sync `dired-omit-mode' from root window.
Notice that it only take effects on the built-in `dired' preview dispatcher."
:group 'dirvish :type 'boolean)
(defcustom dirvish-preview-large-file-threshold 1048576 ; 1mb
"Text files larger than this byte limit are previewed partially."
:group 'dirvish :type '(natnum :tag "File size in bytes"))
(defcustom dirvish-preview-buffers-max-count 5
"Number of file buffers to keep open temporarily during preview."
:group 'dirvish :type '(natnum :tag "Number of buffers"))
(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 :tag "File extensions" string)
:value-type (repeat :tag "External command and args" string)))
(defcustom dirvish-reuse-session t
"Whether to keep the latest session index buffer for later reuse.
The valid values are:
- t: keep index buffer on both `dirvish-quit' and file open
- `resume': keep and resume to the index when using `dirvish' w/o specify a path
- `quit': only keep index after `dirvish-quit'
- `open': only keep index after open a file
- nil: never keep any index buffers on `dirvish-quit' or open files"
:group 'dirvish :type '(choice (const :tag "keep index buffer on both `dirvish-quit' and file open" t)
(const :tag "keep and resume to the index when using `dirvish' w/o specify a path" resume)
(const :tag "only keep index after `dirvish-quit'" quit)
(const :tag "only keep index after open a file" open)
(const :tag "never keep any index buffer" nil)))
(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)
(defcustom dirvish-preview-setup-hook nil
"Functions called after a preview buffer gets placed in the preview window.
The value should be a list of functions take no argument, and they are
called with the preview window temporarily selected."
:group 'dirvish :type 'hook)
(defcustom dirvish-util-buffer-init-hook nil
"Functions called in the mode-line / header-line buffer upon initialization."
:group 'dirvish :type 'hook)
;;;; Internal variables
(defvar dirvish-scopes
'(:frame selected-frame :tab tab-bar--current-tab-index :persp persp-curr))
(defvar dirvish-mode-map
(let ((map (make-sparse-keymap)))
(set-keymap-parent map dired-mode-map)
(define-key map (kbd "q") 'dirvish-quit) map)
"Keymap used in dirvish buffers.")
(defvar dirvish--libraries
'((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-redisplay-debounce-timer nil)
(defvar dirvish--history nil)
(defvar dirvish--reset-keywords '(:free-space :content-begin))
(defvar dirvish--selected-window nil)
(defvar dirvish--session-hash (make-hash-table :test #'equal))
(defvar dirvish--available-attrs '())
(defvar dirvish--available-preview-dispatchers '())
(defvar server-buffer-clients)
(defvar-local dirvish--props '())
(defvar-local dirvish--attrs-hash nil)
;;;; Macros
(defmacro dirvish-prop (prop &rest body)
"Retrieve 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-save-dedication (&rest body)
"Run BODY after undedicating window, restore dedication afterwards."
(declare (debug (&rest form)))
`(progn
(let ((dedicated (window-dedicated-p)))
(set-window-dedicated-p nil nil)
(prog1 ,@body (set-window-dedicated-p nil dedicated)))))
(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 (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 () ,docstring ,@body)))
;;;; Helpers
(defsubst dirvish-curr ()
"Return Dirvish session attached to current buffer, if there is any."
(gethash (dirvish-prop :dv) dirvish--session-hash))
(defun dirvish--ht ()
"Return a new hash-table with `equal' as its test function."
(make-hash-table :test #'equal))
(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 (project-root 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--change-selected (&rest _)
"Record `dirvish--selected-window'."
(setq dirvish--selected-window (frame-selected-window)))
(defun dirvish--selected-p (&optional dv)
"Return t if session DV (defaults to `dirvish-curr') is selected."
(when-let* ((dv (or dv (dirvish-curr))))
(if (dv-curr-layout dv) (eq (dv-root-window dv) dirvish--selected-window)
(eq (frame-selected-window) dirvish--selected-window))))
(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))))
(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-id dv)) "*"))
(name (format "%s*Dirvish-%s%s" (if inhibit-hiding "" " ") type id)))
(if no-create (get-buffer name) (get-buffer-create name))))
(defun dirvish--make-proc (form sentinel buffer-or-name &rest puts)
"Make process for shell or batch FORM in BUFFER-OR-NAME.
Set process's SENTINEL and PUTS accordingly."
(let* ((buf (or buffer-or-name (make-temp-name "*dirvish-batch*")))
(print-length nil) (print-level nil)
(cmd (if (stringp (car form)) form
(list dirvish-emacs-bin
"-Q" "-batch" "--eval" (prin1-to-string form))))
(proc (make-process :name "dirvish" :connection-type nil :buffer buf
:command cmd :sentinel sentinel)))
(while-let ((k (pop puts)) (v (pop puts))) (process-put proc k v))))
;;;; Session Struct
(cl-defstruct (dirvish (:conc-name dv-))
"Define dirvish session (`DV' for short) struct."
(id (format-time-string "%D|%T") :documentation "is the created time stamp of DV, used as its unique id.")
(type 'default :documentation "is the type of DV.")
(root-window () :documentation "is the root/main window of DV.")
(dedicated () :documentation "passes to `set-window-dedicated-p' for ROOT-WINDOW.")
(size-fixed () :documentation "passes to `window-size-fixed' for ROOT-WINDOW.")
(root-window-fn () :documentation "is a function used to create the ROOT-WINDOW for DV.")
(open-file-fn () :documentation "is a function used to open file under the cursor.")
(curr-layout () :documentation "is the working layout recipe of DV.")
(ff-layout dirvish-default-layout :documentation "is a full-frame layout recipe.")
(reuse () :documentation "indicates if DV has been reused.")
(ls-switches dired-listing-switches :documentation "is the directory listing switches.")
(mode-line () :documentation "is the `mode-line-format' used by this DV.")
(header-line () :documentation "is the `header-line-format' used by DV.")
(preview-dispatchers () :documentation "is the working preview methods of DV.")
(preview-hash (dirvish--ht) :documentation "is a hash-table to record content of preview files.")
(parent-hash (dirvish--ht) :documentation "is a hash-table to record content of parent directories.")
(attributes () :documentation "is the working attributes of DV.")
(preview-buffers () :documentation "holds all file preview buffers of DV.")
(preview-window () :documentation "is the window to display preview buffer.")
(winconf () :documentation "is a saved window configuration.")
(index () :documentation "is the (cwd-str . buf-obj) cons within ROOT-WINDOW.")
(roots () :documentation "is all the history INDEX entries in 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)))
;; ensure we have a fallback fullframe layout
(unless dirvish-default-layout (setf (dv-ff-layout new) '(1 0.11 0.55)))
(puthash (dv-id new) new dirvish--session-hash)
(dirvish--check-dependencies new)
(dirvish--create-root-window new) new))
(defun dirvish--get-session (&optional key val)
"Return the first matched session has KEY of VAL."
(setq key (or key 'type) val (or val 'default))
(cl-loop for dv being the hash-values of dirvish--session-hash
for b = (cdr (dv-index dv))
with (fr tab psp) = (cl-loop for (_ v) on dirvish-scopes by 'cddr
collect (and (functionp v) (funcall v)))
if (or (null b) ; newly created session
(and (buffer-live-p b)
(eq (with-current-buffer b (dirvish-prop :tab)) tab)
(eq (with-current-buffer b (dirvish-prop :frame)) fr)
(eq (with-current-buffer b (dirvish-prop :persp)) psp)))
if (let ((res (funcall (intern (format "dv-%s" key)) dv)))
(cond ((eq val 'any) res)
((eq key 'roots) (memq val (mapcar #'cdr res)))
(t (equal val res))))
return dv))
(defun dirvish--clear-session (dv &optional from-quit)
"Reset DV's slot and kill its buffers.
FROM-QUIT is used to signify the calling command."
(let ((index (cdr (dv-index dv))) (frg (dirvish-prop :fringe)))
(when frg (set-window-fringes nil (frame-parameter nil 'left-fringe)))
(if (not (dv-curr-layout dv))
(cl-loop for (_d . b) in (dv-roots dv)
when (and (not (get-buffer-window b))
(not (with-current-buffer b server-buffer-clients)))
do (kill-buffer b)
finally (setf (dv-index dv) (car (dv-roots dv))))
(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-id dv)) bn)
do (dirvish--kill-buffer b))
(setf (dv-preview-hash dv) (dirvish--ht) (dv-parent-hash dv) (dirvish--ht)
(dv-preview-buffers dv) nil (dv-winconf dv) nil)
(when (or (null dirvish-reuse-session)
(eq dirvish-reuse-session (if from-quit 'open 'quit)))
(mapc (pcase-lambda (`(,_ . ,b)) (kill-buffer b)) (dv-roots dv)))))
(defun dirvish--create-root-window (dv)
"Create root window of DV."
(let* ((fn (or (dv-root-window-fn 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 '(fallback))
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 from `dirvish--available-attrs'."
(sort (cl-loop for attr in (append '(hl-line symlink-target) 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-dependencies (dv)
"Require necessary extensions for DV, raise warnings for missing executables."
(cl-loop
with (dps . attrs) = (cons dirvish-preview-dispatchers dirvish-attributes)
with (m . h) = (cons dirvish-mode-line-format dirvish-header-line-format)
with (ml . mr) = (cons (plist-get m :left) (plist-get m :right))
with (hl . hr) = (cons (plist-get h :left) (plist-get h :right))
with feat-reqs = (append dps attrs ml mr hl hr)
when feat-reqs do (require 'dirvish-widgets)
for (lib . feat) in dirvish--libraries do
(when (cl-intersection feat feat-reqs) (require lib))
finally (setf (dv-mode-line dv) (dirvish--mode-line-composer ml mr)
(dv-header-line dv) (dirvish--mode-line-composer hl hr t)
(dv-preview-dispatchers dv) (dirvish--preview-dps-validate)
(dv-attributes dv) (dirvish--attrs-expand attrs))))
(defun dirvish--only-index ()
"If `dired-kill-when-opening-new-dired-buffer', only keep session index."
(when-let* (((default-value 'dired-kill-when-opening-new-dired-buffer))
(dv (dirvish-curr)) (index-buf (cdr (dv-index dv))))
(cl-loop for (_d . b) in (dv-roots dv)
unless (eq index-buf b) do (kill-buffer b))))
;;;; Advices
(defun dirvish-up-dir-a (fn args)
"Ensure FN and ARGS applied with window undedicated."
(dirvish-save-dedication (apply fn args))
(dirvish--only-index))
(cl-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)))
(buf (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 directory")))))
(dv (dirvish-curr)) process-connection-type file)
(when buf (dirvish-save-dedication (switch-to-buffer buf))
(cl-return-from dirvish-find-entry-a (dirvish--only-index)))
(setq file (expand-file-name entry))
(cl-loop with e = (downcase (or (file-name-extension entry) ""))
for (es . (c . a)) in dirvish-open-with-programs
when (and (member e es) (executable-find c)) do
(cl-return-from dirvish-find-entry-a
(let ((a (cl-substitute file "%f" a :test #'string=)))
(apply #'start-process "" nil "nohup" (append (list c) a)))))
(unless dv (cl-return-from dirvish-find-entry-a (find-file file)))
(if-let* ((fn (dv-open-file-fn dv))) (funcall fn)
(dirvish--clear-session 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--dir-data-async dirname (current-buffer) t))
(defun dirvish-wdired-enter-a (&rest _)
"Advice for `wdired-change-to-wdired-mode'."
(let (dirvish-hide-cursor) (dirvish--maybe-toggle-cursor 'hollow))
(dirvish--render-attrs 'clear))
(defun dirvish-dired-noselect-a (fn dir-or-list &optional flags)
"Return buffer for DIR-OR-LIST with FLAGS, FN is `dired-noselect'."
(let* ((dir (if (consp dir-or-list) (car dir-or-list) dir-or-list))
(key (file-name-as-directory (expand-file-name dir)))
(reuse? (or (dirvish-curr) (dirvish--get-session)))
(dv (or reuse? (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 (null buffer)))
(when reuse? (setf (dv-reuse dv) t))
(when new-buffer-p
(if (not remote)
(let ((dired-buffers nil)) ; disable reuse from dired
(setq buffer (apply fn (list dir-or-list flags))))
(require 'dirvish-tramp)
(setq buffer (dirvish-tramp-noselect fn dir-or-list 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-id dv))
(dirvish-prop :gui (display-graphic-p))
(dirvish-prop :remote remote)
(dirvish-prop :root key)
(dirvish-prop :preview-dps
(if remote '(dirvish-tramp-dp) (dv-preview-dispatchers dv)))
(dirvish-prop :attrs (dv-attributes dv))
(cl-loop for (k v) on dirvish-scopes by 'cddr
do (dirvish-prop k (and (functionp v) (funcall v))))
(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."
(let (buffer-read-only)
(ansi-color-apply-on-region
(goto-char pos) (progn (forward-line (frame-height)) (point)))))
(defun dirvish-update-body-h ()
"Update UI of Dirvish."
(when-let* ((dv (dirvish-curr)) ((null (derived-mode-p 'wdired-mode))))
(cond ((not (dirvish--apply-hiding-p 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--apply-hiding-p 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)))
(dirvish-debounce nil
(when (dv-curr-layout dv)
(when (buffer-live-p f-buf)
(with-current-buffer f-buf (force-mode-line-update)))
(when (buffer-live-p h-buf)
(with-current-buffer h-buf (force-mode-line-update)))
;; don't grab focus when peeking or preview window is selected
(when (and (dirvish--selected-p dv)
(not (dirvish--get-session 'type 'peek)))
(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)))
(setf (dv-roots dv) (cl-remove-if (lambda (i) (eq (cdr i) buf)) (dv-roots dv)))
(if (dv-roots dv) ; it might be killed by user in a fullframe session
(when-let* (((eq (cdr (dv-index dv)) buf))
((dv-curr-layout dv))
(win (dv-root-window dv))
((window-live-p win)))
(setf (dv-index dv) (car (dv-roots dv)))
(with-selected-window win ; we have to prevend this window get deleted
(dirvish-save-dedication (switch-to-buffer (cdr (dv-index dv))))))
(when-let* ((layout (dv-curr-layout dv))
(wconf (dv-winconf dv))
((eq buf (window-buffer (selected-window)))))
(set-window-configuration wconf))
(remhash (dv-id dv) dirvish--session-hash)
(cl-loop for b in (buffer-list) for bn = (buffer-name b) when
(string-match-p (format " ?\\*Dirvish-.*-%s\\*" (dv-id dv)) bn)
do (dirvish--kill-buffer b)))))
(defun dirvish-winbuf-change-h (window)
"Rebuild layout once buffer in WINDOW changed."
(when-let* ((dv (with-selected-window window (dirvish-curr)))
(dir (car (dv-index dv))) (buf (cdr (dv-index dv)))
(winconf t) (layout t)
(old-tab (with-selected-window window (dirvish-prop :tab)))
(old-frame (with-selected-window window (dirvish-prop :frame)))
(sc (cl-loop for (k v) on dirvish-scopes by 'cddr
append (list k (and (functionp v) (funcall v)))))
(frame t) (tab t))
(setq winconf (dv-winconf dv) layout (dv-curr-layout dv)
frame (plist-get sc :frame) tab (plist-get sc :tab))
(when (and (dv-reuse dv) (not (equal old-frame frame)))
(with-selected-window (frame-selected-window old-frame)
(when (dirvish-curr) (let (dirvish-reuse-session) (dirvish-quit)))
(setq dv (dirvish--new))))
(when (and (dv-reuse dv) (not (equal old-tab tab)))
;; TODO: maybe clear dirvish sessions in all tabs except the current TAB?
(setq dv (dirvish--new)))
(cond
;; by `*-other-tab|frame'
((or (null (equal old-frame frame)) (null (equal old-tab tab)))
(with-selected-window (dirvish--create-root-window dv)
(dirvish-save-dedication
(switch-to-buffer (get-buffer-create "*scratch*")))
(setf (dv-winconf dv) (current-window-configuration))
(dirvish-save-dedication (switch-to-buffer (dired-noselect dir)))
(cl-loop for (k v) on sc by 'cddr do (dirvish-prop k v))
(dirvish--build-layout dv)))
(t (with-selected-window window (dirvish--build-layout dv))))))
;;;; Preview
(defun dirvish--preview-file-maybe-truncate (dv file size)
"Return preview buffer of FILE with SIZE in DV."
(when (>= (length (dv-preview-buffers dv)) dirvish-preview-buffers-max-count)
(dirvish--kill-buffer (frame-parameter nil 'dv-preview-last)))
(with-current-buffer (dirvish--util-buffer "temp")
(let ((text (gethash file (dv-preview-hash dv))) info)
(with-silent-modifications
(if text (insert text)
(insert-file-contents
file nil 0 dirvish-preview-large-file-threshold)
(when (>= size dirvish-preview-large-file-threshold)
(goto-char (point-max))
(insert "\n\nFile truncated. End of partial preview.\n")))
(setq buffer-file-name file)
(goto-char (point-min))
(rename-buffer (format "Preview: %s @DIRVISH-%s"
(file-name-nondirectory file) (dv-id dv))))
(condition-case err
(eval `(let ,(mapcar (lambda (env) `(,(car env) ,(cdr env)))
(remove '(delay-mode-hooks . t)
dirvish-preview-environment))
(setq-local delay-mode-hooks t)
(set-auto-mode) (font-lock-mode 1)
(and (bound-and-true-p so-long-detected-p)
(error "No preview of file with long lines"))))
(error (setq info (error-message-string err))))
(set-frame-parameter nil 'dv-preview-last (current-buffer))
(if info (prog1 `(info . ,info) (dirvish--kill-buffer (current-buffer)))
(unless text (puthash file (buffer-string) (dv-preview-hash dv)))
`(buffer . ,(current-buffer))))))
(defun dirvish--find-file-temporarily (name)
"Open file NAME temporarily for preview."
`(buffer . ,(eval `(let ,(mapcar (lambda (env) `(,(car env) ,(cdr env)))
(append '((vc-follow-symlinks . t)
(find-file-hook . nil))
dirvish-preview-environment))
(find-file-noselect ,name 'nowarn)))))
(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 filetype [ %s ] has been disabled" ext)))))
(dirvish-define-preview dired (file)
"Preview dispatcher for directory FILE."
(when (file-directory-p file)
`(dired . (let ,(mapcar (lambda (env) `(,(car env) ,(cdr env)))
(remove (cons 'inhibit-message t)
dirvish-preview-environment))
(setq insert-directory-program ,insert-directory-program)
(setq dired-listing-switches ,dired-listing-switches)
(setq dired-omit-verbose ,(bound-and-true-p dired-omit-verbose))
(setq dired-omit-files ,(bound-and-true-p dired-omit-files))
(with-current-buffer (dired-noselect ,file)
,(and dirvish-preview-dired-sync-omit
(bound-and-true-p dired-omit-mode)
`(dired-omit-mode))
(message "\n%s" (buffer-string)))))))
(dirvish-define-preview fallback (file ext dv)
"Fallback preview dispatcher for FILE."
(let* ((attrs (ignore-errors (file-attributes file)))
(size (file-attribute-size attrs)) buf)
(cond ((setq buf (get-buffer
(format "Preview: %s @DIRVISH-%s"
(file-name-nondirectory file) (dv-id dv))))
`(buffer . ,buf))
((not attrs)
`(info . ,(format "Can not get attributes of [ %s ]." file)))
((not size)
`(info . ,(format "Can not get file size of [ %s ]." file)))
((> 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--preview-file-maybe-truncate dv file size)))))
(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
(let (buffer-read-only)
(erase-buffer) (remove-overlays) (insert "\n\n " (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
(let (buffer-read-only) (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 (dirvish-curr)) (cmd-type (process-get proc 'cmd-info))
(str (with-current-buffer (process-buffer proc) (buffer-string))))
(if (eq cmd-type 'shell)
(with-current-buffer (dirvish--util-buffer 'shell dv nil t)
(fundamental-mode) (setq mode-line-format nil header-line-format nil)
(let (buffer-read-only) (erase-buffer) (remove-overlays) (insert str))
(add-hook 'window-scroll-functions #'dirvish-apply-ansicolor-h nil t)
(dirvish-apply-ansicolor-h nil (point-min)))
(with-current-buffer (dirvish--util-buffer 'dired dv nil t)
(dirvish-directory-view-mode)
(setq mode-line-format nil header-line-format nil) (font-lock-mode 1)
(let (buffer-read-only) (erase-buffer) (remove-overlays) (insert str))
(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))))
(kill-buffer (process-buffer proc))))
(defun dirvish--run-shell-for-preview (dv recipe)
"Dispatch shell cmd with RECIPE for session DV."
(let ((proc (get-buffer-process (get-buffer " *dirvish-sh*")))
(buf (dirvish--util-buffer (car recipe) dv nil t)))
(when proc (delete-process proc))
(dirvish--make-proc
(cdr recipe) 'dirvish-shell-preview-proc-s " *dirvish-sh*"
'cmd-info (car recipe))
(with-current-buffer buf
(let (buffer-read-only) (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) "")))
(fns (with-current-buffer (window-buffer (dv-root-window dv))
(dirvish-prop :preview-dps)))
(buf (cl-loop for fn in fns
for rcp = (funcall fn index ext window dv) thereis
(and rcp (dirvish-preview-dispatch rcp dv)))))
(with-current-buffer buf (setq buffer-read-only t))
(setq-local other-window-scroll-buffer buf)
(set-window-buffer window buf)
(with-selected-window window (run-hooks 'dirvish-preview-setup-hook))
(unless (memq buf orig-bufs) (push buf (dv-preview-buffers dv)))))
;;;; Attributes
(defun dirvish--render-attrs-1 (height width pos remote fns ov align-to no-hl)
"HEIGHT WIDTH POS REMOTE FNS OV ALIGN-TO NO-HL."
(forward-line (- 0 height))
(cl-dotimes (_ (* 2 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)