-
Notifications
You must be signed in to change notification settings - Fork 148
/
neotree.el
2244 lines (1978 loc) · 80 KB
/
neotree.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
;;; neotree.el --- A tree plugin like NerdTree for Vim
;; Copyright (C) 2014 jaypei
;; Author: jaypei <[email protected]>
;; URL: https://github.com/jaypei/emacs-neotree
;; Version: 0.6.0
;; Package-Requires: ((cl-lib "0.5"))
;; 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 of the License, 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. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; To use this file, put something like the following in your
;; ~/.emacs:
;;
;; (add-to-list 'load-path "/directory/containing/neotree/")
;; (require 'neotree)
;;
;; Type M-x neotree to start.
;;
;; To set options for NeoTree, type M-x customize, then select
;; Applications, NeoTree.
;;
;;; Code:
(require 'cl-lib)
;;
;; Constants
;;
(defconst neo-buffer-name " *NeoTree*"
"Name of the buffer where neotree shows directory contents.")
(defconst neo-dir
(expand-file-name (if load-file-name
(file-name-directory load-file-name)
default-directory)))
(defconst neo-header-height 5)
(eval-and-compile
;; Added in Emacs 24.3
(unless (fboundp 'user-error)
(defalias 'user-error 'error))
;; Added in Emacs 24.3 (mirrors/emacs@b335efc3).
(unless (fboundp 'setq-local)
(defmacro setq-local (var val)
"Set variable VAR to value VAL in current buffer."
(list 'set (list 'make-local-variable (list 'quote var)) val)))
;; Added in Emacs 24.3 (mirrors/emacs@b335efc3).
(unless (fboundp 'defvar-local)
(defmacro defvar-local (var val &optional docstring)
"Define VAR as a buffer-local variable with default value VAL.
Like `defvar' but additionally marks the variable as being automatically
buffer-local wherever it is set."
(declare (debug defvar) (doc-string 3))
(list 'progn (list 'defvar var val docstring)
(list 'make-variable-buffer-local (list 'quote var))))))
;; Add autoload function for vc (#153).
(autoload 'vc-responsible-backend "vc.elc")
;;
;; Macros
;;
(defmacro neo-util--to-bool (obj)
"If OBJ is non-nil, return t, else return nil."
`(and ,obj t))
(defmacro neo-global--with-buffer (&rest body)
"Execute the forms in BODY with global NeoTree buffer."
(declare (indent 0) (debug t))
`(let ((neotree-buffer (neo-global--get-buffer)))
(unless (null neotree-buffer)
(with-current-buffer neotree-buffer
,@body))))
(defmacro neo-global--with-window (&rest body)
"Execute the forms in BODY with global NeoTree window."
(declare (indent 0) (debug t))
`(save-selected-window
(neo-global--select-window)
,@body))
(defmacro neo-global--when-window (&rest body)
"Execute the forms in BODY when selected window is NeoTree window."
(declare (indent 0) (debug t))
`(when (eq (selected-window) neo-global--window)
,@body))
(defmacro neo-global--switch-to-buffer ()
"Switch to NeoTree buffer."
`(let ((neotree-buffer (neo-global--get-buffer)))
(unless (null neotree-buffer)
(switch-to-buffer neotree-buffer))))
(defmacro neo-buffer--with-editing-buffer (&rest body)
"Execute BODY in neotree buffer without read-only restriction."
`(let (rlt)
(neo-global--with-buffer
(setq buffer-read-only nil)
(setq rlt (progn ,@body))
(setq buffer-read-only t))
rlt))
(defmacro neo-buffer--with-resizable-window (&rest body)
"Execute BODY in neotree window without `window-size-fixed' restriction."
`(let (rlt)
(neo-global--with-buffer
(neo-buffer--unlock-width))
(setq rlt (progn ,@body))
(neo-global--with-buffer
(neo-buffer--lock-width))
rlt))
(defmacro neotree-make-executor (&rest fn-form)
"Make an open event handler, FN-FORM is event handler form."
(let* ((get-args-fn
(lambda (sym) (or (plist-get fn-form sym) (lambda (&rest _)))))
(file-fn (funcall get-args-fn :file-fn))
(dir-fn (funcall get-args-fn :dir-fn)))
`(lambda (&optional arg)
(interactive "P")
(neo-global--select-window)
(neo-buffer--execute arg ,file-fn ,dir-fn))))
;;
;; Customization
;;
(defgroup neotree nil
"Options for neotree."
:prefix "neo-"
:group 'files)
(defgroup neotree-vc-options nil
"Neotree-VC customizations."
:prefix "neo-vc-"
:group 'neotree
:link '(info-link "(neotree)Configuration"))
(defgroup neotree-confirmations nil
"Neotree confirmation customizations."
:prefix "neo-confirm-"
:group 'neotree)
(defcustom neo-window-position 'left
"*The position of NeoTree window."
:group 'neotree
:type '(choice (const left)
(const right)))
(defcustom neo-display-action '(neo-default-display-fn)
"*Action to use for displaying NeoTree window.
If you change the action so it doesn't use
`neo-default-display-fn', then other variables such as
`neo-window-position' won't be respected when opening NeoTree
window."
:type 'sexp
:group 'neotree)
(defcustom neo-create-file-auto-open nil
"*If non-nil, the file will auto open when created."
:type 'boolean
:group 'neotree)
(defcustom neo-banner-message nil
"*The banner message of neotree window."
:type 'string
:group 'neotree)
(defcustom neo-show-updir-line t
"*If non-nil, show the updir line (..)."
:type 'boolean
:group 'neotree)
(defcustom neo-show-slash-for-folder t
"*If non-nil, show the slash at the end of folder (folder/)"
:type 'boolean
:group 'neotree)
(defcustom neo-reset-size-on-open nil
"*If non-nil, the width of the noetree window will be reseted every time a file is open."
:type 'boolean
:group 'neotree)
(defcustom neo-theme 'classic
"*The tree style to display.
`classic' use icon to display, it only it suitable for GUI mode.
`ascii' is the simplest style, it will use +/- to display the fold state,
it suitable for terminal.
`arrow' use unicode arrow.
`nerd' use the nerdtree indentation mode and arrow.
`icons' use icons from `all-the-icons' when installed.
`nerd-icons' use icons from `nerd-icons' when installed."
:group 'neotree
:type '(choice (const classic)
(const ascii)
(const arrow)
(const icons)
(const nerd-icons)
(const nerd)))
(defcustom neo-mode-line-type 'neotree
"*The mode-line type to display, `default' is a non-modified mode-line, \
`neotree' is a compact mode-line that shows useful information about the
current node like the parent directory and the number of nodes,
`custom' uses the format stored in `neo-mode-line-custom-format',
`none' hide the mode-line."
:group 'neotree
:type '(choice (const default)
(const neotree)
(const custom)
(const none)))
(defcustom neo-mode-line-custom-format nil
"*If `neo-mode-line-type' is set to `custom', this variable specifiy \
the mode-line format."
:type 'sexp
:group 'neotree)
(defcustom neo-smart-open nil
"*If non-nil, every time when the neotree window is opened, it will try to find current file and jump to node."
:type 'boolean
:group 'neotree)
(defcustom neo-show-hidden-files nil
"*If non-nil, the hidden files are shown by default."
:type 'boolean
:group 'neotree)
(defcustom neo-autorefresh nil
"*If non-nil, the neotree buffer will auto refresh."
:type 'boolean
:group 'neotree)
(defcustom neo-window-width 25
"*Specifies the width of the NeoTree window."
:type 'integer
:group 'neotree)
(defcustom neo-window-fixed-size t
"*If the neotree windows is fixed, it won't be resize when rebalance windows."
:type 'boolean
:group 'neotree)
(defcustom neo-keymap-style 'default
"*The default keybindings for neotree-mode-map."
:group 'neotree
:type '(choice (const default)
(const concise)))
(defcustom neo-cwd-line-style 'text
"*The default header style."
:group 'neotree
:type '(choice (const text)
(const button)))
(defcustom neo-help-echo-style 'default
"The message NeoTree displays when the mouse moves onto nodes.
`default' means the node name is displayed if it has a
width (including the indent) larger than `neo-window-width', and
`none' means NeoTree doesn't display any messages."
:group 'neotree
:type '(choice (const default)
(const none)))
(defcustom neo-click-changes-root nil
"*If non-nil, clicking on a directory will change the current root to the directory."
:type 'boolean
:group 'neotree)
(defcustom neo-auto-indent-point nil
"*If non-nil the point is autmotically put on the first letter of a node."
:type 'boolean
:group 'neotree)
(defcustom neo-hidden-regexp-list
'("^\\." "\\.pyc$" "~$" "^#.*#$" "\\.elc$" "\\.o$")
"*The regexp list matching hidden files."
:type '(repeat (choice regexp))
:group 'neotree)
(defcustom neo-enter-hook nil
"Functions to run if enter node occured."
:type 'hook
:group 'neotree)
(defcustom neo-after-create-hook nil
"Hooks called after creating the neotree buffer."
:type 'hook
:group 'neotree)
(defcustom neo-vc-integration nil
"If non-nil, show VC status."
:group 'neotree-vc
:type '(set (const :tag "Use different faces" face)
(const :tag "Use different characters" char)))
(defcustom neo-vc-state-char-alist
'((up-to-date . ?\s)
(edited . ?E)
(added . ?+)
(removed . ?-)
(missing . ?!)
(needs-merge . ?M)
(conflict . ?!)
(unlocked-changes . ?!)
(needs-update . ?U)
(ignored . ?\s)
(user . ?U)
(unregistered . ?\s)
(nil . ?\s))
"Alist of vc-states to indicator characters.
This variable is used in `neo-vc-for-node' when
`neo-vc-integration' contains `char'."
:group 'neotree-vc
:type '(alist :key-type symbol
:value-type character))
(defcustom neo-confirm-change-root 'yes-or-no-p
"Confirmation asking for permission to change root if file was not found in root path."
:type '(choice (function-item :tag "Verbose" yes-or-no-p)
(function-item :tag "Succinct" y-or-n-p)
(function-item :tag "Off" off-p))
:group 'neotree-confirmations)
(defcustom neo-confirm-create-file 'yes-or-no-p
"Confirmation asking whether *NeoTree* should create a file."
:type '(choice (function-item :tag "Verbose" yes-or-no-p)
(function-item :tag "Succinct" y-or-n-p)
(function-item :tag "Off" off-p))
:group 'neotree-confirmations)
(defcustom neo-confirm-create-directory 'yes-or-no-p
"Confirmation asking whether *NeoTree* should create a directory."
:type '(choice (function-item :tag "Verbose" yes-or-no-p)
(function-item :tag "Succinct" y-or-n-p)
(function-item :tag "Off" off-p))
:group 'neotree-confirmations)
(defcustom neo-confirm-delete-file 'yes-or-no-p
"Confirmation asking whether *NeoTree* should delete the file."
:type '(choice (function-item :tag "Verbose" yes-or-no-p)
(function-item :tag "Succinct" y-or-n-p)
(function-item :tag "Off" off-p))
:group 'neotree-confirmations)
(defcustom neo-confirm-delete-directory-recursively 'yes-or-no-p
"Confirmation asking whether the directory should be deleted recursively."
:type '(choice (function-item :tag "Verbose" yes-or-no-p)
(function-item :tag "Succinct" y-or-n-p)
(function-item :tag "Off" off-p))
:group 'neotree-confirmations)
(defcustom neo-confirm-kill-buffers-for-files-in-directory 'yes-or-no-p
"Confirmation asking whether *NeoTree* should kill buffers for the directory in question."
:type '(choice (function-item :tag "Verbose" yes-or-no-p)
(function-item :tag "Succinct" y-or-n-p)
(function-item :tag "Off" off-p))
:group 'neotree-confirmations)
(defcustom neo-toggle-window-keep-p nil
"If not nil, not switch to *NeoTree* buffer when executing `neotree-toggle'."
:type 'boolean
:group 'neotree)
(defcustom neo-force-change-root t
"If not nil, do not prompt when switching root."
:type 'boolean
:group 'neotree)
(defcustom neo-filepath-sort-function 'string<
"Function to be called when sorting neotree nodes."
:type '(symbol (const :tag "Normal" string<)
(const :tag "Sort Hidden at Bottom" neo-sort-hidden-last)
(function :tag "Other"))
:group 'neotree)
(defcustom neo-default-system-application "xdg-open"
"*Name of the application that is used to open a file under point.
By default it is xdg-open."
:type 'string
:group 'neotree)
(defcustom neo-hide-cursor nil
"If not nil, hide cursor in NeoTree buffer and turn on line higlight."
:type 'boolean
:group 'neotree)
;;
;; Faces
;;
(defface neo-banner-face
'((((background dark)) (:foreground "lightblue" :weight bold))
(t (:foreground "DarkMagenta")))
"*Face used for the banner in neotree buffer."
:group 'neotree :group 'font-lock-highlighting-faces)
(defvar neo-banner-face 'neo-banner-face)
(defface neo-header-face
'((((background dark)) (:foreground "White"))
(t (:foreground "DarkMagenta")))
"*Face used for the header in neotree buffer."
:group 'neotree :group 'font-lock-highlighting-faces)
(defvar neo-header-face 'neo-header-face)
(defface neo-root-dir-face
'((((background dark)) (:foreground "lightblue" :weight bold))
(t (:foreground "DarkMagenta")))
"*Face used for the root dir in neotree buffer."
:group 'neotree :group 'font-lock-highlighting-faces)
(defvar neo-root-dir-face 'neo-root-dir-face)
(defface neo-dir-link-face
'((((background dark)) (:foreground "DeepSkyBlue"))
(t (:foreground "MediumBlue")))
"*Face used for expand sign [+] in neotree buffer."
:group 'neotree :group 'font-lock-highlighting-faces)
(defvar neo-dir-link-face 'neo-dir-link-face)
(defface neo-file-link-face
'((((background dark)) (:foreground "White"))
(t (:foreground "Black")))
"*Face used for open file/dir in neotree buffer."
:group 'neotree :group 'font-lock-highlighting-faces)
(defvar neo-file-link-face 'neo-file-link-face)
(defface neo-button-face
'((t (:underline nil)))
"*Face used for open file/dir in neotree buffer."
:group 'neotree :group 'font-lock-highlighting-faces)
(defvar neo-button-face 'neo-button-face)
(defface neo-expand-btn-face
'((((background dark)) (:foreground "SkyBlue"))
(t (:foreground "DarkCyan")))
"*Face used for open file/dir in neotree buffer."
:group 'neotree :group 'font-lock-highlighting-faces)
(defvar neo-expand-btn-face 'neo-expand-btn-face)
(defface neo-vc-default-face
'((((background dark)) (:foreground "White"))
(t (:foreground "Black")))
"*Face used for unknown files in the neotree buffer.
Used only when \(vc-state node\) returns nil."
:group 'neotree-vc :group 'font-lock-highlighting-faces)
(defvar neo-vc-default-face 'neo-vc-default-face)
(defface neo-vc-user-face
'((t (:foreground "Red" :slant italic)))
"*Face used for user-locked files in the neotree buffer."
:group 'neotree-vc :group 'font-lock-highlighting-faces)
(defvar neo-vc-user-face 'neo-vc-user-face)
(defface neo-vc-up-to-date-face
'((((background dark)) (:foreground "LightGray"))
(t (:foreground "DarkGray")))
"*Face used for vc-up-to-date files in the neotree buffer."
:group 'neotree-vc :group 'font-lock-highlighting-faces)
(defvar neo-vc-up-to-date-face 'neo-vc-up-to-date-face)
(defface neo-vc-edited-face
'((((background dark)) (:foreground "Magenta"))
(t (:foreground "DarkMagenta")))
"*Face used for vc-edited files in the neotree buffer."
:group 'neotree-vc :group 'font-lock-highlighting-faces)
(defvar neo-vc-edited-face 'neo-vc-edited-face)
(defface neo-vc-needs-update-face
'((t (:underline t)))
"*Face used for vc-needs-update files in the neotree buffer."
:group 'neotree-vc :group 'font-lock-highlighting-faces)
(defvar neo-vc-needs-update-face 'neo-vc-needs-update-face)
(defface neo-vc-needs-merge-face
'((((background dark)) (:foreground "Red1"))
(t (:foreground "Red3")))
"*Face used for vc-needs-merge files in the neotree buffer."
:group 'neotree-vc :group 'font-lock-highlighting-faces)
(defvar neo-vc-needs-merge-face 'neo-vc-needs-merge-face)
(defface neo-vc-unlocked-changes-face
'((t (:foreground "Red" :background "Blue")))
"*Face used for vc-unlocked-changes files in the neotree buffer."
:group 'neotree-vc :group 'font-lock-highlighting-faces)
(defvar neo-vc-unlocked-changes-face 'neo-vc-unlocked-changes-face)
(defface neo-vc-added-face
'((((background dark)) (:foreground "LightGreen"))
(t (:foreground "DarkGreen")))
"*Face used for vc-added files in the neotree buffer."
:group 'neotree-vc :group 'font-lock-highlighting-faces)
(defvar neo-vc-added-face 'neo-vc-added-face)
(defface neo-vc-removed-face
'((t (:strike-through t)))
"*Face used for vc-removed files in the neotree buffer."
:group 'neotree-vc :group 'font-lock-highlighting-faces)
(defvar neo-vc-removed-face 'neo-vc-removed-face)
(defface neo-vc-conflict-face
'((((background dark)) (:foreground "Red1"))
(t (:foreground "Red3")))
"*Face used for vc-conflict files in the neotree buffer."
:group 'neotree-vc :group 'font-lock-highlighting-faces)
(defvar neo-vc-conflict-face 'neo-vc-conflict-face)
(defface neo-vc-missing-face
'((((background dark)) (:foreground "Red1"))
(t (:foreground "Red3")))
"*Face used for vc-missing files in the neotree buffer."
:group 'neotree-vc :group 'font-lock-highlighting-faces)
(defvar neo-vc-missing-face 'neo-vc-missing-face)
(defface neo-vc-ignored-face
'((((background dark)) (:foreground "DarkGrey"))
(t (:foreground "LightGray")))
"*Face used for vc-ignored files in the neotree buffer."
:group 'neotree-vc :group 'font-lock-highlighting-faces)
(defvar neo-vc-ignored-face 'neo-vc-ignored-face)
(defface neo-vc-unregistered-face
nil
"*Face used for vc-unregistered files in the neotree buffer."
:group 'neotree-vc :group 'font-lock-highlighting-faces)
(defvar neo-vc-unregistered-face 'neo-vc-unregistered-face)
;;
;; Variables
;;
(defvar neo-global--buffer nil)
(defvar neo-global--window nil)
(defvar neo-global--autorefresh-timer nil)
(defvar neo-mode-line-format
(list
'(:eval
(let* ((fname (neo-buffer--get-filename-current-line))
(current (if fname fname neo-buffer--start-node))
(parent (if fname (file-name-directory current) current))
(nodes (neo-buffer--get-nodes parent))
(dirs (car nodes))
(files (cdr nodes))
(ndirs (length dirs))
(nfiles (length files))
(index
(when fname
(1+ (if (file-directory-p current)
(neo-buffer--get-node-index current dirs)
(+ ndirs (neo-buffer--get-node-index current files)))))))
(neo-mode-line--compute-format parent index ndirs nfiles))))
"Neotree mode-line displaying information on the current node.
This mode-line format is used if `neo-mode-line-type' is set to `neotree'")
(defvar-local neo-buffer--start-node nil
"Start node(i.e. directory) for the window.")
(defvar-local neo-buffer--start-line nil
"Index of the start line of the root.")
(defvar-local neo-buffer--cursor-pos (cons nil 1)
"To save the cursor position.
The car of the pair will store fullpath, and cdr will store line number.")
(defvar-local neo-buffer--last-window-pos (cons nil 1)
"To save the scroll position for NeoTree window.")
(defvar-local neo-buffer--show-hidden-file-p nil
"Show hidden nodes in tree.")
(defvar-local neo-buffer--expanded-node-list nil
"A list of expanded dir nodes.")
(defvar-local neo-buffer--node-list nil
"The model of current NeoTree buffer.")
(defvar-local neo-buffer--node-list-1 nil
"The model of current NeoTree buffer (temp).")
;;
;; Major mode definitions
;;
(defvar neotree-file-button-keymap
(let ((map (make-sparse-keymap)))
(define-key map [mouse-2]
(neotree-make-executor
:file-fn 'neo-open-file))
map)
"Keymap for file-node button.")
(defvar neotree-dir-button-keymap
(let ((map (make-sparse-keymap)))
(define-key map [mouse-2]
(neotree-make-executor :dir-fn 'neo-open-dir))
map)
"Keymap for dir-node button.")
(defvar neotree-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "TAB") (neotree-make-executor
:dir-fn 'neo-open-dir))
(define-key map (kbd "RET") (neotree-make-executor
:file-fn 'neo-open-file
:dir-fn 'neo-open-dir))
(define-key map (kbd "|") (neotree-make-executor
:file-fn 'neo-open-file-vertical-split))
(define-key map (kbd "-") (neotree-make-executor
:file-fn 'neo-open-file-horizontal-split))
(define-key map (kbd "a") (neotree-make-executor
:file-fn 'neo-open-file-ace-window))
(define-key map (kbd "d") (neotree-make-executor
:dir-fn 'neo-open-dired))
(define-key map (kbd "O") (neotree-make-executor
:dir-fn 'neo-open-dir-recursive))
(define-key map (kbd "SPC") 'neotree-quick-look)
(define-key map (kbd "g") 'neotree-refresh)
(define-key map (kbd "q") 'neotree-hide)
(define-key map (kbd "p") 'neotree-previous-line)
(define-key map (kbd "C-p") 'neotree-previous-line)
(define-key map (kbd "n") 'neotree-next-line)
(define-key map (kbd "C-n") 'neotree-next-line)
(define-key map (kbd "A") 'neotree-stretch-toggle)
(define-key map (kbd "U") 'neotree-select-up-node)
(define-key map (kbd "D") 'neotree-select-down-node)
(define-key map (kbd "H") 'neotree-hidden-file-toggle)
(define-key map (kbd "S") 'neotree-select-previous-sibling-node)
(define-key map (kbd "s") 'neotree-select-next-sibling-node)
(define-key map (kbd "o") 'neotree-open-file-in-system-application)
(define-key map (kbd "C-x C-f") 'find-file-other-window)
(define-key map (kbd "C-x 1") 'neotree-empty-fn)
(define-key map (kbd "C-x 2") 'neotree-empty-fn)
(define-key map (kbd "C-x 3") 'neotree-empty-fn)
(define-key map (kbd "C-c C-f") 'find-file-other-window)
(define-key map (kbd "C-c C-c") 'neotree-change-root)
(define-key map (kbd "C-c c") 'neotree-dir)
(define-key map (kbd "C-c C-a") 'neotree-collapse-all)
(cond
((eq neo-keymap-style 'default)
(define-key map (kbd "C-c C-n") 'neotree-create-node)
(define-key map (kbd "C-c C-d") 'neotree-delete-node)
(define-key map (kbd "C-c C-r") 'neotree-rename-node)
(define-key map (kbd "C-c C-p") 'neotree-copy-node))
((eq neo-keymap-style 'concise)
(define-key map (kbd "C") 'neotree-change-root)
(define-key map (kbd "c") 'neotree-create-node)
(define-key map (kbd "+") 'neotree-create-node)
(define-key map (kbd "d") 'neotree-delete-node)
(define-key map (kbd "r") 'neotree-rename-node)
(define-key map (kbd "e") 'neotree-enter)))
map)
"Keymap for `neotree-mode'.")
(define-derived-mode neotree-mode special-mode "NeoTree"
"A major mode for displaying the directory tree in text mode."
(setq indent-tabs-mode nil ; only spaces
buffer-read-only t ; read only
truncate-lines -1
neo-buffer--show-hidden-file-p neo-show-hidden-files)
(when neo-hide-cursor
(progn
(setq cursor-type nil)
(hl-line-mode +1)))
(pcase neo-mode-line-type
(`neotree
(setq-local mode-line-format neo-mode-line-format)
(add-hook 'post-command-hook 'force-mode-line-update nil t))
(`none (setq-local mode-line-format nil))
(`custom
(setq-local mode-line-format neo-mode-line-custom-format)
(add-hook 'post-command-hook 'force-mode-line-update nil t))
(_ nil))
;; fix for electric-indent-mode
;; for emacs 24.4
(if (fboundp 'electric-indent-local-mode)
(electric-indent-local-mode -1)
;; for emacs 24.3 or less
(add-hook 'electric-indent-functions
(lambda (arg) 'no-indent) nil 'local))
(when neo-auto-indent-point
(add-hook 'post-command-hook 'neo-hook--node-first-letter nil t)))
;;
;; Global methods
;;
(defun neo-global--window-exists-p ()
"Return non-nil if neotree window exists."
(and (not (null (window-buffer neo-global--window)))
(eql (window-buffer neo-global--window) (neo-global--get-buffer))))
(defun neo-global--select-window ()
"Select the NeoTree window."
(interactive)
(let ((window (neo-global--get-window t)))
(select-window window)))
(defun neo-global--get-window (&optional auto-create-p)
"Return the neotree window if it exists, else return nil.
But when the neotree window does not exist and AUTO-CREATE-P is non-nil,
it will create the neotree window and return it."
(unless (neo-global--window-exists-p)
(setf neo-global--window nil))
(when (and (null neo-global--window)
auto-create-p)
(setq neo-global--window
(neo-global--create-window)))
neo-global--window)
(defun neo-default-display-fn (buffer _alist)
"Display BUFFER to the left or right of the root window.
The side is decided according to `neo-window-position'.
The root window is the root window of the selected frame.
_ALIST is ignored."
(let ((window-pos (if (eq neo-window-position 'left) 'left 'right)))
(display-buffer-in-side-window buffer `((side . ,window-pos)))))
(defun neo-global--create-window ()
"Create global neotree window."
(let ((window nil)
(buffer (neo-global--get-buffer t)))
(setq window
(select-window
(display-buffer buffer neo-display-action)))
(neo-window--init window buffer)
(neo-global--attach)
(neo-global--reset-width)
window))
(defun neo-global--get-buffer (&optional init-p)
"Return the global neotree buffer if it exists.
If INIT-P is non-nil and global NeoTree buffer not exists, then create it."
(unless (equal (buffer-name neo-global--buffer)
neo-buffer-name)
(setf neo-global--buffer nil))
(when (and init-p
(null neo-global--buffer))
(save-window-excursion
(setq neo-global--buffer
(neo-buffer--create))))
neo-global--buffer)
(defun neo-global--file-in-root-p (path)
"Return non-nil if PATH in root dir."
(neo-global--with-buffer
(and (not (null neo-buffer--start-node))
(neo-path--file-in-directory-p path neo-buffer--start-node))))
(defun neo-global--alone-p ()
"Check whether the global neotree window is alone with some other window."
(let ((windows (window-list)))
(and (= (length windows)
2)
(member neo-global--window windows))))
(defun neo-global--do-autorefresh ()
"Do auto refresh."
(interactive)
(when (and neo-autorefresh (neo-global--window-exists-p)
(buffer-file-name))
(neotree-refresh t)))
(defun neo-global--open ()
"Show the NeoTree window."
(let ((valid-start-node-p nil))
(neo-global--with-buffer
(setf valid-start-node-p (neo-buffer--valid-start-node-p)))
(if (not valid-start-node-p)
(neo-global--open-dir (neo-path--get-working-dir))
(neo-global--get-window t))))
(defun neo-global--open-dir (path)
"Show the NeoTree window, and change root to PATH."
(neo-global--get-window t)
(neo-global--with-buffer
(neo-buffer--change-root path)))
(defun neo-global--open-and-find (path)
"Quick select node which specified PATH in NeoTree."
(let ((npath path)
root-dir)
(when (null npath)
(throw 'invalid-path "Invalid path to select."))
(setq root-dir (if (file-directory-p npath)
npath (neo-path--updir npath)))
(when (or (not (neo-global--window-exists-p))
(not (neo-global--file-in-root-p npath)))
(neo-global--open-dir root-dir))
(neo-global--with-window
(neo-buffer--select-file-node npath t))))
(defun neo-global--select-mru-window (arg)
"Create or find a window to select when open a file node.
The description of ARG is in `neotree-enter'."
(when (eq (safe-length (window-list)) 1)
(neo-buffer--with-resizable-window
(split-window-horizontally)))
(when neo-reset-size-on-open
(neo-global--when-window
(neo-window--zoom 'minimize)))
;; select target window
(cond
;; select window with winum
((and (integerp arg)
(bound-and-true-p winum-mode)
(fboundp 'winum-select-window-by-number))
(winum-select-window-by-number arg))
;; select window with window numbering
((and (integerp arg)
(boundp 'window-numbering-mode)
(symbol-value window-numbering-mode)
(fboundp 'select-window-by-number))
(select-window-by-number arg))
;; open node in a new vertically split window
((and (stringp arg) (string= arg "a")
(fboundp 'ace-select-window))
(ace-select-window))
((and (stringp arg) (string= arg "|"))
(select-window (get-mru-window))
(split-window-right)
(windmove-right))
;; open node in a new horizontally split window
((and (stringp arg) (string= arg "-"))
(select-window (get-mru-window))
(split-window-below)
(windmove-down)))
;; open node in last active window
(select-window (get-mru-window)))
(defun neo-global--detach ()
"Detach the global neotree buffer."
(when neo-global--autorefresh-timer
(cancel-timer neo-global--autorefresh-timer))
(neo-global--with-buffer
(neo-buffer--unlock-width))
(setq neo-global--buffer nil)
(setq neo-global--window nil))
(defun neo-global--attach ()
"Attach the global neotree buffer"
(when neo-global--autorefresh-timer
(cancel-timer neo-global--autorefresh-timer))
(when neo-autorefresh
(setq neo-global--autorefresh-timer
(run-with-idle-timer 2 10 'neo-global--do-autorefresh)))
(setq neo-global--buffer (get-buffer neo-buffer-name))
(setq neo-global--window (get-buffer-window
neo-global--buffer))
(neo-global--with-buffer
(neo-buffer--lock-width))
(run-hook-with-args 'neo-after-create-hook '(window)))
(defun neo-global--set-window-width (width)
"Set neotree window width to WIDTH."
(neo-global--with-window
(neo-buffer--with-resizable-window
(neo-util--set-window-width (selected-window) width))))
(defun neo-global--reset-width ()
"Set neotree window width to `neo-window-width'."
(neo-global--set-window-width neo-window-width))
;;
;; Advices
;;
(defadvice mouse-drag-vertical-line
(around neotree-drag-vertical-line (start-event) activate)
"Drag and drop is not affected by the lock."
(neo-buffer--with-resizable-window
ad-do-it))
(defadvice balance-windows
(around neotree-balance-windows activate)
"Fix neotree inhibits balance-windows."
(if (neo-global--window-exists-p)
(let (old-width)
(neo-global--with-window
(setq old-width (window-width)))
(neo-buffer--with-resizable-window
ad-do-it)
(neo-global--with-window
(neo-global--set-window-width old-width)))
ad-do-it))
(eval-after-load 'popwin
'(progn
(defadvice popwin:create-popup-window
(around neotree/popwin-popup-buffer activate)
(let ((neo-exists-p (neo-global--window-exists-p)))
(when neo-exists-p
(neo-global--detach))
ad-do-it
(when neo-exists-p
(neo-global--attach)
(neo-global--reset-width))))
(defadvice popwin:close-popup-window
(around neotree/popwin-close-popup-window activate)
(let ((neo-exists-p (neo-global--window-exists-p)))
(when neo-exists-p
(neo-global--detach))
ad-do-it
(when neo-exists-p
(neo-global--attach)
(neo-global--reset-width))))))
;;
;; Hooks
;;
(defun neo-hook--node-first-letter ()
"Move point to the first letter of the current node."
(when (or (eq this-command 'next-line)
(eq this-command 'previous-line))
(neo-point-auto-indent)))
;;
;; Util methods
;;
(defun neo-util--filter (condp lst)
"Apply CONDP to elements of LST keeping those that return non-nil.
Example:
(neo-util--filter 'symbolp '(a \"b\" 3 d4))
=> (a d4)
This procedure does not work when CONDP is the `null' function."
(delq nil
(mapcar (lambda (x) (and (funcall condp x) x)) lst)))
(defun neo-util--find (where which)
"Find element of the list WHERE matching predicate WHICH."
(catch 'found
(dolist (elt where)
(when (funcall which elt)
(throw 'found elt)))
nil))
(defun neo-util--make-printable-string (string)
"Strip newline character from STRING, like 'Icon\n'."
(replace-regexp-in-string "\n" "" string))
(defun neo-util--walk-dir (path)
"Return the subdirectories and subfiles of the PATH."
(let* ((full-path (neo-path--file-truename path)))
(condition-case nil
(directory-files
path 'full directory-files-no-dot-files-regexp)
('file-error
(message "Walk directory %S failed." path)
nil))))
(defun neo-util--hidden-path-filter (node)
"A filter function, if the NODE can not match each item in \
`neo-hidden-regexp-list', return t."
(if (not neo-buffer--show-hidden-file-p)
(let ((shortname (neo-path--file-short-name node)))
(null (neo-util--filter
(lambda (x) (not (null (string-match-p x shortname))))
neo-hidden-regexp-list)))
node))
(defun neo-str--trim-left (s)
"Remove whitespace at the beginning of S."
(if (string-match "\\`[ \t\n\r]+" s)
(replace-match "" t t s)
s))
(defun neo-str--trim-right (s)
"Remove whitespace at the end of S."
(if (string-match "[ \t\n\r]+\\'" s)
(replace-match "" t t s)
s))