-
Notifications
You must be signed in to change notification settings - Fork 12
/
sidebar.el
1934 lines (1735 loc) · 80.1 KB
/
sidebar.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
;;; sidebar.el --- Sidebar major mode -*- lexical-binding: t; -*-
;; Copyright (C) 2017 Sebastien Chapuis
;; Author: Sebastien Chapuis <[email protected]>
;; URL: https://github.com/sebastiencs/sidebar.el
;; Keywords: files, convenience, frames
;; Version: 0.0.1
;; Package-Requires: ((emacs "25") (dash "2.11.0") (projectile "0.10.0") (s "1.10.0") (dash-functional "1.2.0") (ov "1.0.6") (frame-local "0.0.1"))
;;; License
;;
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth
;; Floor, Boston, MA 02110-1301, USA.
;;; Commentary:
;;
;; Librairies that are required by this project:
;;
;; `projectile'
;; `s'
;; `dash'
;; `icons-in-terminal'
;; `ov'
;;; Code:
(require 'projectile)
(require 's)
(require 'dash)
(require 'dash-functional)
(require 'ov)
(require 'sidebar-filemapping)
(require 'sidebar-select)
(require 'sidebar-utils)
(require 'sidebar-face)
(require 'sidebar-mu4e)
(require 'sidebar-buffers)
(defvar sidebar-insert-fileicon-function 'sidebar-insert-fileicon)
(defmacro sidebar--dir-p (file)
"Return non-nil if FILE is a directory."
`(alist-get 'dir ,file))
(defmacro sidebar--open-p (file)
"Return non-nil if FILE is opened. Open means extended (only directory can be open)."
`(alist-get 'opened ,file))
(when (null (require 'icons-in-terminal nil t))
(defun icons-in-terminal (&rest _)
"")
(defun sidebar-insert-fileicon-rescue (file _1 _2 _3 face)
"FILE FACE."
(when (sidebar--dir-p file)
(insert (propertize (if (sidebar--open-p file) "-" "+") 'face face))))
(setq sidebar-insert-fileicon-function 'sidebar-insert-fileicon-rescue))
(eval-after-load 'dash '(dash-enable-font-lock))
(defconst sidebar-version "0.5.0"
"Sidebar's version.")
(defgroup sidebar nil
"Customizable file explorer with git integration."
:group 'tools
:group 'convenience
:link '(custom-manual "(sidebar) Top")
:link '(info-link "(sidebar) Customizing"))
(defmacro sidebar--getpath (file)
"Return the path from FILE."
`(alist-get 'path ,file))
(defmacro sidebar--getline (file)
"Return the line where is FILE."
`(alist-get 'line ,file))
(defmacro sidebar-writable (&rest body)
"Set temporarily the buffer to writable while executing BODY."
`(-let [buffer-read-only nil]
,@body))
(defcustom sidebar-adjust-auto-window-width nil
"If activated, the sidebar's window will automatically be resize if the..
filename on the current line is longer than the window.
This can be done manually by calling the function `sidebar-adjust-window-width'
or typing `<right>'."
:type 'boolean
:group 'sidebar)
(defcustom sidebar-message-current nil
"If activated, print the full path of the current file in the echo area."
:type 'boolean
:group 'sidebar)
(defcustom sidebar-show-hidden-files t
"If non-nil, hidden files are listed."
:type 'boolean
:group 'sidebar)
(defcustom sidebar-width 40
"Default window width of `sidebar'."
:type 'integer
:group 'sidebar)
(defcustom sidebar-icon-dir-closed 'myicons_0013
"Icon to use before a closed directory.
To get a list of the icons names, you can run:
`~/.local/share/icons-in-terminal/print_icons.sh --names'
More info at URL `https://github.com/sebastiencs/icons-in-terminal'."
:type 'symbol
:group 'sidebar)
(defcustom sidebar-icon-dir-opened 'myicons_0014
"Icon to use before an opened directory.
To get a list of the icons names, you can run:
`~/.local/share/icons-in-terminal/print_icons.sh --names'
More info at URL `https://github.com/sebastiencs/icons-in-terminal'."
:type 'symbol
:group 'sidebar)
(defcustom sidebar-icon-git-not-updated 'oct_flame
"Icon to use with a non updated file (with git).
To get a list of the icons names, you can run:
`~/.local/share/icons-in-terminal/print_icons.sh --names'
More info at URL `https://github.com/sebastiencs/icons-in-terminal'."
:type 'symbol
:group 'sidebar)
(defcustom sidebar-icon-git-updated 'oct_git_commit
"Icon to use with an updated file (with git).
To get a list of the icons names, you can run:
`~/.local/share/icons-in-terminal/print_icons.sh --names'
More info at URL `https://github.com/sebastiencs/icons-in-terminal'."
:type 'symbol
:group 'sidebar)
(defcustom sidebar-icon-git-changed 'oct_beaker
"Icon to use with file that has changed since index (with git).
To get a list of the icons names, you can run:
`~/.local/share/icons-in-terminal/print_icons.sh --names'
More info at URL `https://github.com/sebastiencs/icons-in-terminal'."
:type 'symbol
:group 'sidebar)
(defcustom sidebar-icon-git-added 'oct_pulse
"Icon to use with an added file (with git).
To get a list of the icons names, you can run:
`~/.local/share/icons-in-terminal/print_icons.sh --names'
More info at URL `https://github.com/sebastiencs/icons-in-terminal'."
:type 'symbol
:group 'sidebar)
(defcustom sidebar-icon-git-renamed 'oct_git_renamed
"Icon to use with a renamed file (with git).
To get a list of the icons names, you can run:
`~/.local/share/icons-in-terminal/print_icons.sh --names'
More info at URL `https://github.com/sebastiencs/icons-in-terminal'."
:type 'symbol
:group 'sidebar)
(defcustom sidebar-icon-git-match 'oct_git_commit
"Icon to use with a file that matches the index (with git).
To get a list of the icons names, you can run:
`~/.local/share/icons-in-terminal/print_icons.sh --names'
More info at URL `https://github.com/sebastiencs/icons-in-terminal'."
:type 'symbol
:group 'sidebar)
(defcustom sidebar-icon-powerline '(powerline_left_hard_divider 0 -0.05 1.0)
"Icon to insert at the end of the current line.
To get a list of the icons names, you can run:
`~/.local/share/icons-in-terminal/print_icons.sh --names'
More info at URL `https://github.com/sebastiencs/icons-in-terminal'."
:type `(radio
(const :tag ,(format "Triangle up %s" (icons-in-terminal 'myicons_0002))
(myicons_0002 0 -0.01 1.11))
(const :tag ,(format "Triangle bottom %s" (icons-in-terminal 'myicons_0003))
(myicons_0003 0 -0.05 1.2))
(const :tag ,(format "Wave-Bottom %s" (icons-in-terminal 'myicons_0009))
(myicons_0009 0 -0.05 1.0))
(const :tag ,(format "Wave-Up %s" (icons-in-terminal 'myicons_0008))
(myicons_0008 0 -0.05 1.0))
(const :tag ,(format "Powerline %s" (icons-in-terminal 'powerline_left_hard_divider))
(powerline_left_hard_divider 0 -0.05 1.0))
(const :tag ,(format "Circle %s" (icons-in-terminal 'powerline_extra_right_half_circle_thick))
(powerline_extra_right_half_circle_thick 0 -0.05 1.0))
(const :tag ,(format "Flame %s" (icons-in-terminal 'powerline_extra_flame_thick))
(powerline_extra_flame_thick 1 -0.05 1.0))
(const :tag ,(format "Squares small %s" (icons-in-terminal 'powerline_extra_pixelated_squares_small))
(powerline_extra_pixelated_squares_small 1 -0.05 1.0))
(const :tag ,(format "Squares big %s" (icons-in-terminal 'powerline_extra_pixelated_squares_big))
(powerline_extra_pixelated_squares_big 1 -0.05 1.0))
(const :tag ,(format "Ice %s" (icons-in-terminal 'powerline_extra_ice_waveform))
(powerline_extra_ice_waveform 1 -0.05 1.0))
(const :tag ,(format "Lego %s" (icons-in-terminal 'myicons_0010))
(myicons_0010 0 0.0 1.0))
)
:group 'sidebar)
(defcustom sidebar-icons-modeline '(myicons_0009 myicons_0007 0)
"Icons to use in the modeline with branch and remote branch."
:group 'sidebar
:type `(radio
(const :tag ,(format "Triangle up left / bottom right Left %s %s Right"
(icons-in-terminal 'myicons_0002)
(icons-in-terminal 'myicons_0004))
(myicons_0002 myicons_0004 0))
(const :tag ,(format "Triangle bottom left / up right Left %s %s Right"
(icons-in-terminal 'myicons_0003)
(icons-in-terminal 'myicons_0005))
(myicons_0003 myicons_0005 0))
(const :tag ,(format "Triangle bottom Left %s %s Right"
(icons-in-terminal 'myicons_0003)
(icons-in-terminal 'myicons_0004))
(myicons_0003 myicons_0004 0))
(const :tag ,(format "Triangle up Left %s %s Right"
(icons-in-terminal 'myicons_0002)
(icons-in-terminal 'myicons_0005))
(myicons_0002 myicons_0005 0))
(const :tag ,(format "Wave-Bottom Left %s %s Right"
(icons-in-terminal 'myicons_0009)
(icons-in-terminal 'myicons_0007))
(myicons_0009 myicons_0007 0))
(const :tag ,(format "Wave-Up Left %s %s Right"
(icons-in-terminal 'myicons_0008)
(icons-in-terminal 'myicons_0006))
(myicons_0008 myicons_0006 0))
(const :tag ,(format "Wave up left / bottom right Left %s %s Right"
(icons-in-terminal 'myicons_0008)
(icons-in-terminal 'myicons_0007))
(myicons_0008 myicons_0007 0))
(const :tag ,(format "Wave bottom left / up right Left %s %s Right"
(icons-in-terminal 'myicons_0009)
(icons-in-terminal 'myicons_0006))
(myicons_0009 myicons_0006 0))
(const :tag ,(format "Powerline Left %s %s Right"
(icons-in-terminal 'powerline_left_hard_divider)
(icons-in-terminal 'powerline_right_hard_divider))
(powerline_left_hard_divider powerline_right_hard_divider 0))
(const :tag ,(format "Circle Left %s %s Right"
(icons-in-terminal 'powerline_extra_right_half_circle_thick)
(icons-in-terminal 'powerline_extra_left_half_circle_thick))
(powerline_extra_right_half_circle_thick powerline_extra_left_half_circle_thick 2))
(const :tag ,(format "Flame Left %s %s Right"
(icons-in-terminal 'powerline_extra_flame_thick)
(icons-in-terminal 'powerline_extra_flame_thick_mirrored))
(powerline_extra_flame_thick powerline_extra_flame_thick_mirrored 2))
(const :tag ,(format "Squares small Left %s %s Right"
(icons-in-terminal 'powerline_extra_pixelated_squares_small)
(icons-in-terminal 'powerline_extra_pixelated_squares_small_mirrored))
(powerline_extra_pixelated_squares_small powerline_extra_pixelated_squares_small_mirrored 2))
(const :tag ,(format "Squares big Left %s %s Right"
(icons-in-terminal 'powerline_extra_pixelated_squares_big)
(icons-in-terminal 'powerline_extra_pixelated_squares_big_mirrored))
(powerline_extra_pixelated_squares_big powerline_extra_pixelated_squares_big_mirrored 2))
(const :tag ,(format "Ice Left %s %s Right"
(icons-in-terminal 'powerline_extra_ice_waveform)
(icons-in-terminal 'powerline_extra_ice_waveform_mirrored))
(powerline_extra_ice_waveform powerline_extra_ice_waveform_mirrored 2))
))
(defcustom sidebar-icon-header-end '(myicons_0008 0)
"Icon to insert at the end of the header line.
To get a list of the icons names, you can run:
`~/.local/share/icons-in-terminal/print_icons.sh --names'
More info at URL `https://github.com/sebastiencs/icons-in-terminal'."
:type `(radio
(const :tag ,(format "Triangle up %s" (icons-in-terminal 'myicons_0002))
(myicons_0002 1))
(const :tag ,(format "Triangle bottom %s" (icons-in-terminal 'myicons_0003))
(myicons_0003 1))
(const :tag ,(format "Wave-Bottom %s" (icons-in-terminal 'myicons_0009))
(myicons_0009 0))
(const :tag ,(format "Wave-Up %s" (icons-in-terminal 'myicons_0008))
(myicons_0008 0))
(const :tag ,(format "Powerline %s" (icons-in-terminal 'powerline_left_hard_divider))
(powerline_left_hard_divider 1))
(const :tag ,(format "Circle %s" (icons-in-terminal 'powerline_extra_right_half_circle_thick))
(powerline_extra_right_half_circle_thick 1))
(const :tag ,(format "Flame %s" (icons-in-terminal 'powerline_extra_flame_thick))
(powerline_extra_flame_thick 2))
(const :tag ,(format "Squares small %s" (icons-in-terminal 'powerline_extra_pixelated_squares_small))
(powerline_extra_pixelated_squares_small 2))
(const :tag ,(format "Squares big %s" (icons-in-terminal 'powerline_extra_pixelated_squares_big))
(powerline_extra_pixelated_squares_big 2))
(const :tag ,(format "Ice %s" (icons-in-terminal 'powerline_extra_ice_waveform))
(powerline_extra_ice_waveform 2))
)
:group 'sidebar)
(defcustom sidebar-icon-header-project 'oct_repo
"Icon to insert before a project name.
To get a list of the icons names, you can run:
`~/.local/share/icons-in-terminal/print_icons.sh --names'
More info at URL `https://github.com/sebastiencs/icons-in-terminal'."
:type 'symbol
:group 'sidebar)
(defcustom sidebar-icon-header-directory 'oct_file_directory
"Icon to insert before a directory name.
To get a list of the icons names, you can run:
`~/.local/share/icons-in-terminal/print_icons.sh --names'
More info at URL `https://github.com/sebastiencs/icons-in-terminal'."
:type 'symbol
:group 'sidebar)
(defcustom sidebar-icon-branch 'oct_git_branch
"Icon to insert before the name of the current branch.
To get a list of the icons names, you can run:
`~/.local/share/icons-in-terminal/print_icons.sh --names'
More info at URL `https://github.com/sebastiencs/icons-in-terminal'."
:type 'symbol
:group 'sidebar)
(defcustom sidebar-icon-remotebranch 'oct_git_branch
"Icon to insert after the name of the remote branch.
To get a list of the icons names, you can run:
`~/.local/share/icons-in-terminal/print_icons.sh --names'
More info at URL `https://github.com/sebastiencs/icons-in-terminal'."
:type 'symbol
:group 'sidebar)
(defcustom sidebar-mode-line-height 1.5
"(GUI) Height of the mode line."
:type 'float
:group 'sidebar)
(defcustom sidebar-header-line-height 1.5
"(GUI) Height of the header line."
:type 'float
:group 'sidebar)
(defcustom sidebar-status-on-file t
"Insert icon after the filename according to its git status.
Default: t."
:type 'boolean
:group 'sidebar)
(defcustom sidebar-status-on-directory 'on-closed
"Control when to place icon of git status on directories.
The icons represent the git status of all the subfiles of the directory.
Each icon is followed by a number: The number of times the status is present.
The following values are possible:
- `never' Never insert icon after directories;
- `on-closed' Insert icons on closed directories only.
- `on-opened' Insert icons on opened directories only.
' `always' Always insert icon.
Default: `on-closed'."
:type '(choice (const :tag "Never" never)
(const :tag "On closed directories only" on-closed)
(const :tag "On opened directories only" on-opened)
(const :tag "Always" 'always))
:group 'sidebar)
(defcustom sidebar-filename-colored nil
"The filename will be colored according to its git status.
Untracked and ignored files will always be colored.
You can change their colors if you don't want them to be
colored.
Default: nil."
:type 'boolean
:group 'sidebar)
(defcustom sidebar-check-update t
"If non nil, sidebar checks if we're using the last version.
Default: non-nil."
:type 'boolean
:group 'sidebar)
;;(ignore-errors (kill-buffer (sidebar-cons-buffer-name)))
(defvar-local sidebar-files-number 0
"Number of files listed in the sidebar.")
(defvar-local sidebar-directories-number 0
"Number of directories listed in the sidebar.")
(defvar sidebar-button-keymap
(let ((map (make-sparse-keymap)))
(define-key map [mouse-1] 'sidebar-open-line)
(define-key map [mouse-2] 'sidebar-open-line)
map)
"Keymap for file button.")
(defvar sidebar--streched-spaces-p (intern-soft ":re-align")
"Non-nil if streched spaces are supported.
Apply this patch to support it:
https://gist.github.com/sebastiencs/2f066f8d12b71f40fda9bdb979fe971d")
(defsubst sidebar-get-root-project ()
"Return the project directory, nil if there is no project."
(ignore-errors (projectile-project-root)))
(defsubst sidebar-get-lsp-root ()
"Return the lsp root directory, or nil."
(and (bound-and-true-p lsp--cur-workspace)
(fboundp 'lsp--workspace-root)
(lsp--workspace-root lsp--cur-workspace)))
(defsubst sidebar-project-root ()
"Return the project root using projectile.
If it's not a project, return the file's directory.
If it's not a file, return the home directory."
(or (sidebar-get-lsp-root)
(sidebar-get-root-project)
(when buffer-file-name (file-name-directory buffer-file-name))
(file-name-as-directory (expand-file-name "~"))))
(defsubst sidebar-get-curl-buffer ()
"Return the buffer associated to the curl buffer for the current frame."
(get-buffer-create (sidebar-cons-buffer-name "-CURL")))
(defsubst sidebar-get-git-buffer ()
"Return the existing/created sidebar git buffer for the current frame."
(get-buffer-create (sidebar-cons-buffer-name "-GIT")))
(defsubst sidebar-exists-p ()
"Check if a sidebar for the frame exists."
(get-buffer (sidebar-cons-buffer-name)))
(defun sidebar-count-char (char s)
"Count the occurence of CHAR in string S."
(let ((n 0))
(dotimes (it (length s) n)
(when (eq (elt s it) char)
(setq n (1+ n))))))
(defun sidebar-calc-depth (path status)
"Calcul the depth of FILE from the current directory point of view.
This is used to count the number of space to insert before the filename.
STATUS is the status of the FILE."
(let* ((path-from-current (s-chop-prefix default-directory path))
(depth (sidebar-count-char ?\/ path-from-current))) ; TODO: Support Windows (replace '/')
(if (> depth 0)
(1+ (* depth 2))
(1+ depth))))
(defmacro measure-time (&rest body)
"Measure the time it takes to evaluate BODY."
`(let ((time (current-time)))
,@body
;;(message "%.06f" (float-time (time-since time)))
))
(defvar-local sidebar--root-project nil)
(defvar-local sidebar--git-hashtable nil)
(defvar-local sidebar--git-directories nil)
(defun sidebar-status-from-parent (file-path)
"If FILE-PATH is in a untracked or ignored directory, return its status."
(when sidebar--git-directories
(let ((list sidebar--git-directories)
found)
(while (and list (not found))
(when (string-prefix-p (caar list) file-path)
(setq found (cdar list)))
(setq list (cdr list)))
found)))
(defsubst sidebar-color-from-status (status &optional default)
"Return the face associated to the git STATUS.
If there is no status, return DEFAULT."
(pcase status
('not-updated 'sidebar-not-updated)
('updated 'sidebar-updated)
('changed 'sidebar-changed)
('added 'sidebar-added)
('renamed 'sidebar-renamed)
('match 'sidebar-match)
(_ default)))
(defun sidebar-insert-status-subfiles-helper (status number file path)
"Insert the icon and numbers after a directory.
STATUS is use to know which icon to insert
NUMBER is the number to insert
FILE PATH"
(concat
(sidebar-insert-status file path status t)
;; https://gist.github.com/sebastiencs/2f066f8d12b71f40fda9bdb979fe971d
(propertize " " 'display '(space :re-align t))
(number-to-string number)))
(defun sidebar-status-subfiles (path)
"Return the numbers of git status in the subfiles of directory PATH."
(let ((not-updated 0) (updated 0) (untracked 0) (changed 0) (added 0) (renamed 0) (match 0))
(-when-let (hashtable (sidebar-get git-hashtable))
(maphash (lambda (key value)
(when (s-starts-with? path key)
(pcase value
('not-updated (setq not-updated (1+ not-updated)))
('updated (setq updated (1+ updated)))
('changed (setq changed (1+ changed)))
('added (setq added (1+ added)))
('match (setq match (1+ match))))))
hashtable))
`((not-updated . ,not-updated) (updated . ,updated) (untracked . ,untracked)
(changed . ,changed) (added . ,added) (renamed . ,renamed) (match . ,match))))
(defun sidebar-insert-status-subfiles-p (file)
"Return non-nil if we have to insert the icons for the subfiles of FILE."
(when (sidebar--dir-p file)
(cond ((equal sidebar-status-on-directory 'always) t)
((and (equal sidebar-status-on-directory 'on-closed) (not (sidebar--open-p file))) t)
((and (equal sidebar-status-on-directory 'on-opened) (sidebar--open-p file)) t))))
(defun sidebar-insert-status-subfiles (file path)
"Insert the subfiles's status of the directory FILE.
PATH is the path of FILE."
(when (sidebar-insert-status-subfiles-p file)
(-let (((&alist 'not-updated not-updated
'updated updated
'changed changed
'added added
'match match)
(sidebar-status-subfiles path))
(func 'sidebar-insert-status-subfiles-helper))
(concat
(when (> not-updated 0)
(funcall func 'not-updated not-updated file path))
(when (> updated 0)
(funcall func 'updated updated file path))
(when (> changed 0)
(funcall func 'changed changed file path))
(when (> added 0)
(funcall func 'added added file path))
(when (> match 0)
(funcall func 'match match file path))))))
(defsubst sidebar-insert-filename (str face)
"Small function to insert STR with FACE if non-nil."
(propertize str 'face face 'mouse-face face 'keymap sidebar-button-keymap))
(defun sidebar-insert-icon (icon face)
"Insert ICON with FACE if non-nil."
(icons-in-terminal icon :face face :height 1.12))
(defun sidebar-insert-fileicon (file filename status path face)
"Insert the icon associated to FILE.
It should insert the directories icons for directories and icons
associated to their extensions for files.
FILENAME is the filename of FILE.
STATUS is its git status.
PATH its path.
FACE is the face to use for the icons."
(if (sidebar--dir-p file)
(let ((icon (if (sidebar--open-p file) sidebar-icon-dir-opened sidebar-icon-dir-closed)))
(sidebar-insert-icon icon face))
(-let* (((icon . color) (sidebar-filemapping-lookup filename))
(partial (-partial 'icons-in-terminal icon :height 1.12)))
(cond (face (funcall partial :face face))
(color (funcall partial :foreground color))
(t (funcall partial))))))
(defsubst sidebar-window-width ()
"Return the sidebar's window width."
(window-width (sidebar-get-window)))
(defun sidebar-insert-icon-filename (file filename status path)
"Insert the icon associated to FILE followed by its FILENAME.
STATUS is its git status.
PATH is the file path."
(let* ((status (or (sidebar-status-from-parent path) status))
(dir-p (sidebar--dir-p file))
(icon-color (or (and (eq 'ignored status)
(if dir-p 'sidebar-ignored-dir 'sidebar-ignored-file))
(and dir-p 'sidebar-dir)))
(file-color (or (and (eq 'ignored status)
(if dir-p 'sidebar-ignored-dir 'sidebar-ignored-file))
(and (eq 'untracked status) 'sidebar-untracked)
(and dir-p 'sidebar-dir)
(and sidebar-filename-colored
(sidebar-color-from-status status 'sidebar-file))
'sidebar-file)))
(concat
(funcall sidebar-insert-fileicon-function file filename status path icon-color)
;; Use a streched space, so filenames are always aligned no matter the icon's width
;; Apply this patch to emacs source:
;; https://gist.github.com/sebastiencs/2f066f8d12b71f40fda9bdb979fe971d
(when sidebar--streched-spaces-p
(propertize " " 'display '(space :re-align t)))
(propertize " " 'display `(space :width 0.6))
(sidebar-insert-filename filename file-color))))
(defun sidebar-insert-status (file path status &optional dir)
"Insert the icons associated to the git status of FILE.
PATH is its path.
STATUS is the git status of that file.
DIR is non-nil if it's a directory."
(when (or sidebar-status-on-file dir)
(let* ((face (sidebar-color-from-status status nil))
(func (lambda (name)
(concat
;; https://gist.github.com/sebastiencs/2f066f8d12b71f40fda9bdb979fe971d
(when sidebar--streched-spaces-p
(propertize " " 'display '(space :re-align t)))
(propertize " " 'display '(space :width 0.5))
(funcall 'sidebar-insert-icon name face)))))
(pcase status
('not-updated (funcall func sidebar-icon-git-not-updated))
('updated (funcall func sidebar-icon-git-updated))
('changed (funcall func sidebar-icon-git-changed))
('added (funcall func sidebar-icon-git-added))
('renamed (funcall func sidebar-icon-git-renamed))
('match (funcall func sidebar-icon-git-match))))))
(sidebar-print-function file (file &optional no-newline)
"Insert FILE's filename on the current LINE.
First, it inserts ' ' x times, depending on the file depth (relative to
the current directory).
This is followed by the icon of that file. The icon is selected according to
its extension (or if it's a directory).
Then it inserts the filename.
If FILE is a directory and closed (not expanded), it inserts the icons of
all the files it contains just after its name, still on the same line.
FILE is an alist created from `sidebar-file-struct'."
(let* ((path (sidebar--getpath file))
(filename (and path (file-name-nondirectory path)))
(path-in-project (s-chop-prefix sidebar--root-project path))
(path-fixed-dirname (if (sidebar--dir-p file) (file-name-as-directory path-in-project) path-in-project))
(status (and sidebar--git-hashtable
(gethash path-fixed-dirname sidebar--git-hashtable)))
(depth (sidebar-calc-depth path status)))
(concat
(make-string depth ?\s)
(sidebar-insert-icon-filename file filename status path-fixed-dirname)
(sidebar-insert-status file path-fixed-dirname status)
(sidebar-insert-status-subfiles file path-fixed-dirname)
(unless no-newline "\n"))))
(defun sidebar-print-listfiles (list)
"Insert all files in LIST from the current line.
It updates the associated value `line' for each file. This is used to
keep track of which file is on which line."
(sidebar-writable
(let ((fn (sidebar-get print-item))
(sidebar--root-project (sidebar-get root-project))
(sidebar--git-hashtable (sidebar-get git-hashtable))
(default-directory (sidebar-get current-path)))
(mapc fn list))))
(defun sidebar-toggle-hidden-files ()
"Toggle listing of hidden files."
(interactive)
(setq sidebar-show-hidden-files (not sidebar-show-hidden-files))
(sidebar-refresh))
(defun sidebar-dots-file (file)
"Return t if FILE is '.' or '..'."
(let ((file (file-name-nondirectory file)))
(or (string= "." file)
(string= ".." file)
(and (not sidebar-show-hidden-files)
(string= "." (substring file 0 1))))))
(sidebar-content-provider files (path)
"Return a list of files/directories in PATH.
It removes '.' and '..'
Sort the directories first
The list returned is a list of association list for each file created
with `\\[sidebar-file-struct]'"
(let* ((files-and-dirs (--> path (directory-files it t) (-remove 'sidebar-dots-file it)))
(dirs-sorted (-filter 'file-directory-p files-and-dirs))
(files-sorted (--filter (not (file-directory-p it)) files-and-dirs)))
(-concat dirs-sorted files-sorted)))
(defsubst sidebar-load-content (path)
"Function that call 'load-content-function'.
It varies for files, buffers or mu4e.
PATH is used only for files, it represents the path where to load new files."
(funcall (sidebar-get load-content-function) path))
(defun sidebar-goto-buffername (buffer-name)
"This function jump the cursor to BUFFER-NAME (string) in the sidebar.
If there is no filename equal to the BUFFER-NAME, it put the cursor
on the frame parameter `sidebar-line-to-start'.
This is use when the sidebar is created."
(let* ((file (and buffer-name (sidebar--find-file buffer-name)))
(line (or (sidebar-get save-line) (sidebar--getline file) (sidebar-get line-to-start))))
(sidebar-goto-line line)
(sidebar-show-current)
(when (and (sidebar-get restore-function)
(sidebar-get window-start))
(set-window-start (sidebar-get-window) (sidebar-get window-start)))
(sidebar-set save-line nil)))
(defun sidebar-expand-path (project-path-root file-path)
"Return a list of files and dirs to open from PROJECT-PATH-ROOT to FILE-PATH."
(-when-let* ((base project-path-root)
(dirs-to-open (-some->> file-path
(s-chop-prefix (-> base directory-file-name))
(s-chop-suffix (-> file-path file-name-nondirectory))
directory-file-name
sidebar-split-path)))
(-map 'sidebar-file-struct
(--map (setq base (concat (file-name-as-directory base) it))
dirs-to-open))))
(defun sidebar-split-path (path)
"Return a list of directory names from PATH.
Example:
path = \"/var/dir/other\"
return = (\"var\" \"dir\" \"other\")"
(sidebar-split-path-helper path ()))
(defun sidebar-split-path-helper (path accum)
"Help function for `sidebar-split-path'.
PATH ACCUM."
(let ((dir (-> path file-name-directory directory-file-name))
(name (file-name-nondirectory path)))
(if (equal dir path)
accum
(sidebar-split-path-helper dir (cons name accum)))))
(defun sidebar-check-setup ()
"Check if the font ‘icons-in-terminal’ is installed.
TODO: Check with terminals too (now it checks only with GUI), `font-info'
returns an error on terminals."
(when (and (sidebar-gui-p) (not (font-info "icons-in-terminal")))
;; (ignore-errors (kill-buffer (sidebar-cons-buffer-name)))
(message "The font icons-in-terminal is not installed: see https://github.com/sebastiencs/sidebar.el")))
(defvar sidebar-mode-association
'(:sidebar-mode
((sidebar-mode-to-use . sidebar-mode)
(sidebar-load-content-function . sidebar-content-files)
(sidebar-make-header-function . sidebar-make-header)
(sidebar-make-modeline-left-function . sidebar-make-modeline-left)
(sidebar-make-modeline-right-function . sidebar-make-modeline-right)
(sidebar-item-builder-function . sidebar-file-struct)
(sidebar-restore-function . sidebar-restore-state)
(sidebar-print-item . sidebar-print-file)
(sidebar-line-to-start . 1))
:sidebar-mu4e-mode
((sidebar-load-content-function . sidebar-content-mu4e)
(sidebar-mode-to-use . sidebar-mu4e-mode)
(sidebar-make-header-function . sidebar-mu4e-make-header)
(sidebar-make-modeline-left-function . sidebar-mu4e-make-modeline-left)
(sidebar-make-modeline-right-function . sidebar-mu4e-make-modeline-right)
(sidebar-item-builder-function . sidebar-mu4e-item-builder)
(sidebar-restore-function . nil)
(sidebar-print-item . sidebar-print-mu4e)
(sidebar-line-to-start . 1))
:sidebar-buffers-mode
((sidebar-load-content-function . sidebar-content-buffers)
(sidebar-mode-to-use . sidebar-buffers-mode)
(sidebar-make-header-function . sidebar-buffers-make-header)
(sidebar-make-modeline-left-function . sidebar-buffers-make-modeline-left)
(sidebar-make-modeline-right-function . sidebar-buffers-make-modeline-right)
(sidebar-item-builder-function . sidebar-buffers-item-builder)
(sidebar-restore-function . nil)
(sidebar-print-item . sidebar-print-buffers)
(sidebar-line-to-start . 1))
)
"Variable storing the functions to call according to the mode used.")
(defun sidebar-init-vars (project-path-root)
"Initialize the variables on sidebar startup.
PROJECT-PATH-ROOT."
(let ((mode (cond ((sidebar-mu4e?) :sidebar-mu4e-mode)
((sidebar-buffers?) :sidebar-buffers-mode)
(t :sidebar-mode))))
(--each (plist-get sidebar-mode-association mode)
(sidebar-set1 (car it) (cdr it))))
(if (and (sidebar-get restore-function) (sidebar-get saved-state-files))
(funcall (sidebar-get restore-function))
(sidebar-set root-project (sidebar-project-root))
(sidebar-set history (list project-path-root))
(sidebar-set current-path project-path-root)
(sidebar-set window-start nil)
(setq default-directory project-path-root)
(sidebar-set default-width sidebar-width)
;; (sidebar-set files (sidebar-load-content project-path-root))
(unless (sidebar-get overlay)
(sidebar-set overlay (ov (point-min) (point-min) 'face 'sidebar-powerline)))))
(defun sidebar-open ()
"Open or create a sidebar for the current frame."
(interactive)
(sidebar-set window-origin (get-buffer-window))
(when (or (sidebar-get mu4e-force) (sidebar-mu4e?))
(sidebar-kill))
(if (and (eq (sidebar-get mode-to-use) 'sidebar-buffers-mode)
(not (eq this-command 'sidebar-buffers-open)))
(sidebar-buffers-switch-to-files t)
(let ((sidebar-exists (sidebar-exists-p))
(sidebar-buffer (sidebar-get-buffer))
(sidebar-window (sidebar-get-window))
(project-path-root (sidebar-project-root))
(buffer-name-current (buffer-file-name)))
(set-window-buffer sidebar-window sidebar-buffer)
(set-buffer sidebar-buffer)
(select-window sidebar-window)
(internal-show-cursor (sidebar-get-window) nil)
(unless sidebar-exists
(sidebar-check-setup)
(sidebar-init-vars project-path-root)
(funcall (sidebar-get mode-to-use))
(sidebar-refresh (sidebar-expand-path project-path-root buffer-name-current))
(sidebar-goto-buffername (and (eq (sidebar-get mode-to-use) 'sidebar-mode)
buffer-name-current))
(sidebar-curl-run)
(unless (sidebar-get saved-state-files)
(sidebar-git-run nil t))))))
(defun sidebar-close ()
"Close the sidebar for the current frame.
If you want to kill the sidebar and its buffer, use `sidebar-kill'"
(interactive)
(-when-let (sidebar-window (sidebar-get-window t))
(delete-window sidebar-window)))
;;(kill-buffer (sidebar-cons-buffer-name))
(defun sidebar-adjust-window-width (&optional n-chars-on-line sidebar-window)
"Resize the sidebar window if the filename on the current line is longer...
than the window's width.
N-CHARS-ON-LINE is the number of characters on the current line.
SIDEBAR-WINDOW is sidebar's window."
(interactive)
(let ((window (or sidebar-window (sidebar-get-window t)))
(chars (or n-chars-on-line (- (line-end-position) (line-beginning-position)))))
(when (> chars (window-total-width window))
(sidebar-set-window (+ 3 chars)))))
(defun sidebar-reset-window-width ()
"Reset the sidebar window to the default value `sidebar-width'."
(interactive)
(sidebar-set-window))
(defun sidebar-make-powerline ()
"Return a string to make a poweline effect.
The effect is made by inserting space with a colored background
and an icon at the end.
See `sidebar-move-overlay'."
(let* ((icon sidebar-icon-powerline)
(face 'sidebar-powerline))
(concat
(propertize " " 'face face 'display `(space :align-to (- right-fringe ,(if (sidebar-gui-p) 2 3))))
(icons-in-terminal (car icon)
:raise (car (cddr icon))
:height (cadr (cddr icon))
:foreground (face-background face nil t)))))
(defun sidebar-move-overlay (beg end window)
"Move the overlay that make the powerline effect.
The overlay is just a colored background (more precisely a face).
The overlay is only inserted from the beginning of the line to the end
of the filename. The remaining space and the icon at the end is a string
that we insert with the 'after-string overlay property.
This string is generated for each line, to make it fit for the filename
width and the window.
BEG is the position at the beginning of the line.
END is the position at the end of the line.
WINDOW is the sidebar's window."
(let* ((ov (sidebar-get overlay)))
(ov-move ov beg end)
(unless (ov-val ov 'after-string)
(ov-set ov 'after-string (sidebar-make-powerline)))))
(defun sidebar-update-path-on-header ()
"Function that update the suffix on the header.
The suffix represents the path of the file pointed by the
cursor (current line).
The header is asked to be update only when the suffix change."
(-when-let* ((path-file (-some-> (sidebar--getpath (sidebar-find-file-from-line)) file-name-directory))
(current-path (sidebar-get current-path))
(suffix-path (when (>= (length path-file) (length current-path))
(substring path-file (1- (length current-path))))))
(-let [suffix-header (sidebar-get suffix-header)]
(when (or (null suffix-header)
(not (s-equals? suffix-path suffix-header)))
(sidebar-set suffix-header suffix-path)
(force-mode-line-update)))))
(defun sidebar-show-current ()
"Show the current line with a background color and powerline effect.
Resize the window if necessary (customizable)."
(save-excursion
(-when-let* ((sidebar-window (sidebar-get-window t))
(line-begin (line-beginning-position))
(line-end (line-end-position)))
(sidebar-move-overlay line-begin line-end sidebar-window)
(set-window-fringes sidebar-window nil nil nil)
(set-window-margins sidebar-window 0 0)
(sidebar-update-path-on-header)
(when sidebar-adjust-auto-window-width
(sidebar-adjust-window-width (- line-end line-begin) sidebar-window))
(-when-let* ((_ sidebar-message-current)
(path (sidebar--getpath (sidebar-find-file-from-line))))
(message (abbreviate-file-name path))))))
;;(ignore-errors (kill-buffer (sidebar-cons-buffer-name)))
(defun sidebar-create-directory ()
"Create a directory and its parents if non existing."
(interactive)
(let* ((new-dir (-some->> (sidebar-find-file-from-line)
sidebar--getpath
file-name-directory
(read-string "[sidebar] Create directory: "))))
(if (file-exists-p new-dir)
(error "[sidebar] Cannot create directory %s, it already exists" new-dir)
(condition-case err
(progn
(make-directory new-dir t)
(message "[sidebar] directory created")
(sidebar-git-run t))
(error "[sidebar] Error: %s" (error-message-string err))))))
(defun sidebar-create-file ()
"Create a new file.
The file should be in an existing directory.
To create directories, see `sidebar-create-directory'"
(interactive)
(let* ((new-file (-some--> (sidebar-find-file-from-line)
sidebar--getpath
(or it (sidebar-get current-path))
file-name-directory
(read-string "[sidebar] Create file: " it))))
(cond
((file-exists-p new-file)
(error "[sidebar] Cannot create file %s, it already exists" new-file))
((-> new-file file-writable-p not)
(error "[sidebar] Cannot create file %s, directory not writable" new-file))
(t (-when-let (sidebar-window (sidebar-get window-origin))
(select-window sidebar-window)
(find-file new-file)
(save-buffer)
(message "[sidebar] File %s created" new-file)
(sidebar-refresh))))))
(defun sidebar-delete-selected ()
"Delete the file/directory on the current line.
If it's a directory, it removes recursively its subfiles."
(interactive)
(let* ((file-at-line (sidebar-find-file-from-line))
(path (sidebar--getpath file-at-line))
(str-prompt (format "[sidebar] Delete the %s %s ? ('yes' or anything else to cancel): "
(if (sidebar--dir-p file-at-line) "directory" "file")
path))
(confirm (read-string str-prompt)))
(when (s-equals? "yes" confirm)
(if (sidebar--dir-p file-at-line) (delete-directory path t) (delete-file path))
(sidebar-refresh))))
(defun sidebar-rename-buffer-name (buffers name new-name)
"Rename a buffer.
BUFFERS is a list of buffers.
NAME is the old name.
NEW-NAME is the new name."
(while buffers
(when (equal (buffer-file-name (car buffers)) name)
(when (y-or-n-p (format "Rename the buffer %s ? " (buffer-name (car buffers))))
(with-current-buffer (car buffers)
(set-visited-file-name new-name t t)
(save-buffer))))
(setq buffers (cdr buffers))))
(defun sidebar-rename-selected ()
"Rename the file/directory on the current line.
If there are buffers visiting this file, you'll be ask to rename them too."
(interactive)
(let* ((file-at-line (sidebar-find-file-from-line))
(path (sidebar--getpath file-at-line))
(directory (file-name-directory path))
(new-name (read-string (format "[sidebar] Rename %s to: " (file-name-nondirectory path)))))
(if (file-exists-p new-name)
(error "[sidebar] Cannot rename to %s, it already exists" new-name)
(rename-file path (concat directory new-name))
(sidebar-rename-buffer-name (buffer-list) path new-name)
(sidebar-refresh))))
(defvar-local sidebar-file-to-copy
'(:file nil :method 'copy)
"Variable that holds the file to copy.