summaryrefslogtreecommitdiff
path: root/lisp/neotree.el
blob: 0156cb9bb7b36007a7d86afe6906c56ef6adef93 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
;;; neotree.el --- A tree plugin like NerdTree for Vim

;; Copyright (C) 2014 jaypei

;; Author: jaypei <jaypei97159@gmail.com>
;; URL: https://github.com/jaypei/emacs-neotree
;; Version: 0.5.1
;; 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."
  :group 'neotree
  :type '(choice (const classic)
                 (const ascii)
                 (const arrow)
                 (const 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))

(defun neo-str--trim (s)
  "Remove whitespace at the beginning and end of S."
  (neo-str--trim-left (neo-str--trim-right s)))

(defun neo-path--expand-name (path &optional current-dir)
  (expand-file-name (or (if (file-name-absolute-p path) path)
			(let ((r-path path))
			  (setq r-path (substitute-in-file-name r-path))
			  (setq r-path (expand-file-name r-path current-dir))
			  r-path))))

(defun neo-path--shorten (path len)
  "Shorten a given PATH to a specified LEN.
This is needed for paths, which are to long for the window to display
completely.  The function cuts of the first part of the path to remain
the last folder (the current one)."
  (let ((result
         (if (> (length path) len)
             (concat "<" (substring path (- (- len 2))))
           path)))
    (when result
      (decode-coding-string result 'utf-8))))

(defun neo-path--insert-chroot-button (label path face)
  (insert-button
   label
   'action '(lambda (x) (neotree-change-root))
   'follow-link t
   'face face
   'neo-full-path path))

(defun neo-path--insert-header-buttonized (path)
  "Shortens the PATH to (window-body-width) and displays any \
visible remains as buttons that, when clicked, navigate to that
parent directory."
  (let* ((dirs (reverse (cl-maplist 'identity (reverse (split-string path "/" :omitnulls)))))
         (last (car-safe (car-safe (last dirs)))))
    (neo-path--insert-chroot-button "/" "/" 'neo-root-dir-face)
    (dolist (dir dirs)
      (if (string= (car dir) last)
          (neo-buffer--insert-with-face last 'neo-root-dir-face)
        (neo-path--insert-chroot-button
         (concat (car dir) "/")
         (apply 'neo-path--join (cons "/" (reverse dir)))
         'neo-root-dir-face))))
  ;;shorten the line if need be
  (when (> (current-column) (window-body-width))
    (forward-char (- (window-body-width)))
    (delete-region (point-at-bol) (point))
    (let* ((button (button-at (point)))
           (path (if button (overlay-get button 'neo-full-path) "/")))
      (neo-path--insert-chroot-button "<" path 'neo-root-dir-face))
    (end-of-line)))

(defun neo-path--updir (path)
  (let ((r-path (neo-path--expand-name path)))
    (if (and (> (length r-path) 0)
             (equal (substring r-path -1) "/"))
        (setq r-path (substring r-path 0 -1)))
    (if (eq (length r-path) 0)
        (setq r-path "/"))
    (directory-file-name
     (file-name-directory r-path))))

(defun neo-path--join (root &rest dirs)
  "Joins a series of directories together with ROOT and DIRS.
Like Python's os.path.join,
  (neo-path--join \"/tmp\" \"a\" \"b\" \"c\") => /tmp/a/b/c ."
  (or (if (not dirs) root)
      (let ((tdir (car dirs))
            (epath nil))
        (setq epath
              (or (if (equal tdir ".") root)
                  (if (equal tdir "..") (neo-path--updir root))
                  (neo-path--expand-name tdir root)))
        (apply 'neo-path--join
               epath
               (cdr dirs)))))

(defun neo-path--file-short-name (file)
  "Base file/directory name by FILE.
Taken from http://lists.gnu.org/archive/html/emacs-devel/2011-01/msg01238.html"
  (or (if (string= file "/") "/")
      (neo-util--make-printable-string (file-name-nondirectory (directory-file-name file)))))

(defun neo-path--file-truename (path)
  (let ((rlt (file-truename path)))
    (if (not (null rlt))
        (progn
          (if (and (file-directory-p rlt)
                   (> (length rlt) 0)
                   (not (equal (substring rlt -1) "/")))
              (setq rlt (concat rlt "/")))
          rlt)
      nil)))

(defun neo-path--has-subfile-p (dir)
  "To determine whether a directory(DIR) contain files."
  (and (file-exists-p dir)
       (file-directory-p dir)
       (neo-util--walk-dir dir)
       t))

(defun neo-path--match-path-directory (path)
  (let ((true-path (neo-path--file-truename path))
        (rlt-path nil))
    (setq rlt-path
          (catch 'rlt
            (if (file-directory-p true-path)
                (throw 'rlt true-path))
            (setq true-path
                  (file-name-directory true-path))
            (if (file-directory-p true-path)
                (throw 'rlt true-path))))
    (if (not (null rlt-path))
        (setq rlt-path (neo-path--join "." rlt-path "./")))
    rlt-path))

(defun neo-path--get-working-dir ()
  "Return a directory name of the current buffer."
  (file-name-as-directory (file-truename default-directory)))

(defun neo-path--strip (path)
  "Remove whitespace at the end of PATH."
  (let* ((rlt (neo-str--trim path))
         (pos (string-match "[\\\\/]+\\'" rlt)))
    (when pos
      (setq rlt (replace-match "" t t rlt))
      (when (eq (length rlt) 0)
        (setq rlt "/")))
    rlt))

(defun neo-path--path-equal-p (path1 path2)
  "Return non-nil if pathes PATH1 and PATH2 are the same path."
  (string-equal (neo-path--strip path1)
                (neo-path--strip path2)))

(defun neo-path--file-equal-p (file1 file2)
  "Return non-nil if files FILE1 and FILE2 name the same file.
If FILE1 or FILE2 does not exist, the return value is unspecified."
  (unless (or (null file1)
              (null file2))
    (let ((nfile1 (neo-path--strip file1))
          (nfile2 (neo-path--strip file2)))
      (file-equal-p nfile1 nfile2))))

(defun neo-path--file-in-directory-p (file dir)
  "Return non-nil if FILE is in DIR or a subdirectory of DIR.
A directory is considered to be \"in\" itself.
Return nil if DIR is not an existing directory."
  (let ((nfile (neo-path--strip file))
        (ndir (neo-path--strip dir)))
    (setq ndir (concat ndir "/"))
    (file-in-directory-p nfile ndir)))

(defun neo-util--kill-buffers-for-path (path)
  "Kill all buffers for files in PATH."
  (let ((buffer (find-buffer-visiting path)))
    (when buffer
      (kill-buffer buffer)))
  (dolist (filename (directory-files path t directory-files-no-dot-files-regexp))
    (let ((buffer (find-buffer-visiting filename)))
      (when buffer
        (kill-buffer buffer))
      (when (and
             (file-directory-p filename)
             (neo-path--has-subfile-p filename))
        (neo-util--kill-buffers-for-path filename)))))

(defun neo-util--set-window-width (window n)
  "Make WINDOW N columns width."
  (let ((w (max n window-min-width)))
    (unless (null window)
      (if (> (window-width) w)
          (shrink-window-horizontally (- (window-width) w))
        (if (< (window-width) w)
            (enlarge-window-horizontally (- w (window-width))))))))

(defun neo-point-auto-indent ()
  "Put the point on the first letter of the current node."
  (when (neo-buffer--get-filename-current-line)
    (beginning-of-line 1)
    (re-search-forward "[^-\s+]" (line-end-position 1) t)
    (backward-char 1)))

(defun off-p (msg)
  "Returns true regardless of message value in the argument."
  t)

(defun neo-sort-hidden-last (x y)
  "Sort normally but with hidden files last."
  (let ((x-hidden (neo-filepath-hidden-p x))
        (y-hidden (neo-filepath-hidden-p y)))
    (cond
     ((and x-hidden (not y-hidden))
      nil)
     ((and (not x-hidden) y-hidden)
      t)
     (t
      (string< x y)))))

(defun neo-filepath-hidden-p (node)
  "Return whether or not node is a hidden path."
  (let ((shortname (neo-path--file-short-name node)))
    (neo-util--filter
     (lambda (x) (not (null (string-match-p x shortname))))
     neo-hidden-regexp-list)))

(defun neo-get-unsaved-buffers-from-projectile ()
  "Return list of unsaved buffers from projectile buffers."
  (interactive)
  (let ((rlist '())
        (rtag t))
    (condition-case nil
        (projectile-project-buffers)
      (error (setq rtag nil)))
    (when (and rtag (fboundp 'projectile-project-buffers))
      (dolist (buf (projectile-project-buffers))
        (with-current-buffer buf
          (if (and (buffer-modified-p) buffer-file-name)
              (setq rlist (cons (buffer-file-name) rlist))
            ))))
    rlist))

;;
;; Buffer methods
;;

(defun neo-buffer--newline-and-begin ()
  "Insert new line."
  (newline)
  (beginning-of-line))

(defun neo-buffer--get-icon (name)
  "Get image by NAME."
  (let ((icon-path (neo-path--join neo-dir "icons"))
        image)
    (setq image (create-image
                 (neo-path--join icon-path (concat name ".xpm"))
                 'xpm nil :ascent 'center :mask '(heuristic t)))
    image))

(defun neo-buffer--insert-fold-symbol (name &optional node-name)
  "Write icon by NAME, the icon style affected by neo-theme.
`open' write opened folder icon.
`close' write closed folder icon.
`leaf' write leaf icon.
Optional NODE-NAME is used for the `icons' theme"
  (let ((n-insert-image (lambda (n)
                          (insert-image (neo-buffer--get-icon n))))
        (n-insert-symbol (lambda (n)
                           (neo-buffer--insert-with-face
                            n 'neo-expand-btn-face))))
    (cond
     ((and (display-graphic-p) (equal neo-theme 'classic))
      (or (and (equal name 'open)  (funcall n-insert-image "open"))
          (and (equal name 'close) (funcall n-insert-image "close"))
          (and (equal name 'leaf)  (funcall n-insert-image "leaf"))))
     ((equal neo-theme 'arrow)
      (or (and (equal name 'open)  (funcall n-insert-symbol "▾"))
          (and (equal name 'close) (funcall n-insert-symbol "▸"))))
     ((equal neo-theme 'nerd)
      (or (and (equal name 'open)  (funcall n-insert-symbol "▾ "))
          (and (equal name 'close) (funcall n-insert-symbol "▸ "))
          (and (equal name 'leaf)  (funcall n-insert-symbol "  "))))
     ((and (display-graphic-p) (equal neo-theme 'icons))
      (unless (require 'all-the-icons nil 'noerror)
        (error "Package `all-the-icons' isn't installed"))
      (setq-local tab-width 1)
      (or (and (equal name 'open)  (insert (all-the-icons-icon-for-dir-with-chevron (directory-file-name node-name) "down")))
          (and (equal name 'close) (insert (all-the-icons-icon-for-dir-with-chevron (directory-file-name node-name) "right")))
          (and (equal name 'leaf)  (insert (format "\t\t\t%s\t" (all-the-icons-icon-for-file node-name))))))
     (t
      (or (and (equal name 'open)  (funcall n-insert-symbol "- "))
          (and (equal name 'close) (funcall n-insert-symbol "+ ")))))))

(defun neo-buffer--save-cursor-pos (&optional node-path line-pos)
  "Save cursor position.
If NODE-PATH and LINE-POS is nil, it will be save the current line node position."
  (let ((cur-node-path nil)
        (cur-line-pos nil)
        (ws-wind (selected-window))
        (ws-pos (window-start)))
    (setq cur-node-path (if node-path
                            node-path
                          (neo-buffer--get-filename-current-line)))
    (setq cur-line-pos (if line-pos
                           line-pos
                         (line-number-at-pos)))
    (setq neo-buffer--cursor-pos (cons cur-node-path cur-line-pos))
    (setq neo-buffer--last-window-pos (cons ws-wind ws-pos))))

(defun neo-buffer--goto-cursor-pos ()
  "Jump to saved cursor position."
  (let ((line-pos nil)
        (node (car neo-buffer--cursor-pos))
        (line-pos (cdr neo-buffer--cursor-pos))
        (ws-wind (car neo-buffer--last-window-pos))
        (ws-pos (cdr neo-buffer--last-window-pos)))
    (catch 'line-pos-founded
      (unless (null node)
        (setq line-pos 0)
        (mapc
         (lambda (x)
           (setq line-pos (1+ line-pos))
           (unless (null x)
             (when (neo-path--path-equal-p x node)
               (throw 'line-pos-founded line-pos))))
         neo-buffer--node-list))
      (setq line-pos (cdr neo-buffer--cursor-pos))
      (throw 'line-pos-founded line-pos))
    ;; goto line
    (goto-char (point-min))
    (neo-buffer--forward-line (1- line-pos))
    ;; scroll window
    (when (equal (selected-window) ws-wind)
      (set-window-start ws-wind ws-pos t))))

(defun neo-buffer--node-list-clear ()
  "Clear node list."
  (setq neo-buffer--node-list nil))

(defun neo-buffer--node-list-set (line-num path)
  "Set value in node list.
LINE-NUM is the index of node list.
PATH is value."
  (let ((node-list-length (length neo-buffer--node-list))
        (node-index line-num))
    (when (null node-index)
      (setq node-index (line-number-at-pos)))
    (when (< node-list-length node-index)
      (setq neo-buffer--node-list
            (vconcat neo-buffer--node-list
                     (make-vector (- node-index node-list-length) nil))))
    (aset neo-buffer--node-list (1- node-index) path))
  neo-buffer--node-list)

(defun neo-buffer--insert-with-face (content face)
  (let ((pos-start (point)))
    (insert content)
    (set-text-properties pos-start
                         (point)
                         (list 'face face))))

(defun neo-buffer--valid-start-node-p ()
  (and (not (null neo-buffer--start-node))
       (file-accessible-directory-p neo-buffer--start-node)))

(defun neo-buffer--create ()
  "Create and switch to NeoTree buffer."
  (switch-to-buffer
   (generate-new-buffer-name neo-buffer-name))
  (neotree-mode)
  ;; disable linum-mode
  (when (and (boundp 'linum-mode)
             (not (null linum-mode)))
    (linum-mode -1))
  ;; Use inside helm window in NeoTree
  ;; Refs https://github.com/jaypei/emacs-neotree/issues/226
  (setq-local helm-split-window-inside-p t)
  (current-buffer))

(defun neo-buffer--insert-banner ()
  (unless (null neo-banner-message)
    (let ((start (point)))
      (insert neo-banner-message)
      (set-text-properties start (point) '(face neo-banner-face)))
    (neo-buffer--newline-and-begin)))

(defun neo-buffer--insert-root-entry (node)
  (neo-buffer--node-list-set nil node)
  (cond ((eq neo-cwd-line-style 'button)
         (neo-path--insert-header-buttonized node))
        (t
         (neo-buffer--insert-with-face (neo-path--shorten node (window-body-width))
                                       'neo-root-dir-face)))
  (neo-buffer--newline-and-begin)
  (when neo-show-updir-line
    (neo-buffer--insert-fold-symbol 'close node)
    (insert-button ".."
                   'action '(lambda (x) (neotree-change-root))
                   'follow-link t
                   'face neo-dir-link-face
                   'neo-full-path (neo-path--updir node))
    (neo-buffer--newline-and-begin)))

(defun neo-buffer--help-echo-message (node-name)
  (cond
   ((eq neo-help-echo-style 'default)
    (if (<= (+ (current-column) (string-width node-name))
            neo-window-width)
        nil
      node-name))
   (t nil)))

(defun neo-buffer--insert-dir-entry (node depth expanded)
  (let ((node-short-name (neo-path--file-short-name node)))
    (insert-char ?\s (* (- depth 1) 2)) ; indent
    (when (memq 'char neo-vc-integration)
      (insert-char ?\s 2))
    (neo-buffer--insert-fold-symbol
     (if expanded 'open 'close) node)
    (insert-button (if neo-show-slash-for-folder (concat node-short-name "/") node-short-name)
                   'follow-link t
                   'face neo-dir-link-face
                   'neo-full-path node
                   'keymap neotree-dir-button-keymap
                   'help-echo (neo-buffer--help-echo-message node-short-name))
    (neo-buffer--node-list-set nil node)
    (neo-buffer--newline-and-begin)))

(defun neo-buffer--insert-file-entry (node depth)
  (let ((node-short-name (neo-path--file-short-name node))
        (vc (when neo-vc-integration (neo-vc-for-node node))))
    (insert-char ?\s (* (- depth 1) 2)) ; indent
    (when (memq 'char neo-vc-integration)
      (insert-char (car vc))
      (insert-char ?\s))
    (neo-buffer--insert-fold-symbol 'leaf node-short-name)
    (insert-button node-short-name
                   'follow-link t
                   'face (if (memq 'face neo-vc-integration)
                             (cdr vc)
                           neo-file-link-face)
                   'neo-full-path node
                   'keymap neotree-file-button-keymap
                   'help-echo (neo-buffer--help-echo-message node-short-name))
    (neo-buffer--node-list-set nil node)
    (neo-buffer--newline-and-begin)))

(defun neo-vc-for-node (node)
  (let* ((backend (ignore-errors
                    (vc-responsible-backend node)))
         (vc-state (when backend (vc-state node backend))))
    (cons (cdr (assoc vc-state neo-vc-state-char-alist))
          (cl-case vc-state
            (up-to-date       neo-vc-up-to-date-face)
            (edited           neo-vc-edited-face)
            (needs-update     neo-vc-needs-update-face)
            (needs-merge      neo-vc-needs-merge-face)
            (unlocked-changes neo-vc-unlocked-changes-face)
            (added            neo-vc-added-face)
            (removed          neo-vc-removed-face)
            (conflict         neo-vc-conflict-face)
            (missing          neo-vc-missing-face)
            (ignored          neo-vc-ignored-face)
            (unregistered     neo-vc-unregistered-face)
            (user             neo-vc-user-face)
            (otherwise        neo-vc-default-face)))))

(defun neo-buffer--get-nodes (path)
  (let* ((nodes (neo-util--walk-dir path))
         (comp neo-filepath-sort-function)
         (nodes (neo-util--filter 'neo-util--hidden-path-filter nodes)))
    (cons (sort (neo-util--filter 'file-directory-p nodes) comp)
          (sort (neo-util--filter #'(lambda (f) (not (file-directory-p f))) nodes) comp))))

(defun neo-buffer--get-node-index (node nodes)
  "Return the index of NODE in NODES.

NODES can be a list of directory or files.
Return nil if NODE has not been found in NODES."
  (let ((i 0)
        (l (length nodes))
        (cur (car nodes))
        (rest (cdr nodes)))
    (while (and cur (not (equal cur node)))
      (setq i (1+ i))
      (setq cur (car rest))
      (setq rest (cdr rest)))
    (if (< i l) i)))

(defun neo-buffer--expanded-node-p (node)
  "Return non-nil if NODE is expanded."
  (neo-util--to-bool
   (neo-util--find
    neo-buffer--expanded-node-list
    #'(lambda (x) (equal x node)))))

(defun neo-buffer--set-expand (node do-expand)
  "Set the expanded state of the NODE to DO-EXPAND.
Return the new expand state for NODE (t for expanded, nil for collapsed)."
  (if (not do-expand)
      (setq neo-buffer--expanded-node-list
            (neo-util--filter
             #'(lambda (x) (not (equal node x)))
             neo-buffer--expanded-node-list))
    (push node neo-buffer--expanded-node-list))
  do-expand)

(defun neo-buffer--toggle-expand (node)
  (neo-buffer--set-expand node (not (neo-buffer--expanded-node-p node))))

(defun neo-buffer--insert-tree (path depth)
  (if (eq depth 1)
      (neo-buffer--insert-root-entry path))
  (let* ((contents (neo-buffer--get-nodes path))
         (nodes (car contents))
         (leafs (cdr contents))
         (default-directory path))
    (dolist (node nodes)
      (let ((expanded (neo-buffer--expanded-node-p node)))
        (neo-buffer--insert-dir-entry
         node depth expanded)
        (if expanded (neo-buffer--insert-tree (concat node "/") (+ depth 1)))))
    (dolist (leaf leafs)
      (neo-buffer--insert-file-entry leaf depth))))

(defun neo-buffer--refresh (save-pos-p &optional non-neotree-buffer)
  "Refresh the NeoTree buffer.
If SAVE-POS-P is non-nil, it will be auto save current line number."
  (let ((start-node neo-buffer--start-node))
    (unless start-node
      (setq start-node default-directory))
    (neo-buffer--with-editing-buffer
     ;; save context
     (when save-pos-p
       (neo-buffer--save-cursor-pos))
     (when non-neotree-buffer
       (setq neo-buffer--start-node start-node))
     ;; starting refresh
     (erase-buffer)
     (neo-buffer--node-list-clear)
     (neo-buffer--insert-banner)
     (setq neo-buffer--start-line neo-header-height)
     (neo-buffer--insert-tree start-node 1))
    ;; restore context
    (neo-buffer--goto-cursor-pos)))

(defun neo-buffer--post-move ()
  "Reset current directory when position moved."
  (funcall
   (neotree-make-executor
    :file-fn
    '(lambda (path _)
       (setq default-directory (neo-path--updir btn-full-path)))
    :dir-fn
    '(lambda (path _)
       (setq default-directory (file-name-as-directory path))))))

(defun neo-buffer--get-button-current-line ()
  "Return the first button in current line."
  (let* ((btn-position nil)
         (pos-line-start (line-beginning-position))
         (pos-line-end (line-end-position))
         ;; NOTE: cannot find button when the button
         ;;       at beginning of the line
         (current-button (or (button-at (point))
                             (button-at pos-line-start))))
    (if (null current-button)
        (progn
          (setf btn-position
                (catch 'ret-button
                  (let* ((next-button (next-button pos-line-start))
                         (pos-btn nil))
                    (if (null next-button) (throw 'ret-button nil))
                    (setf pos-btn (overlay-start next-button))
                    (if (> pos-btn pos-line-end) (throw 'ret-button nil))
                    (throw 'ret-button pos-btn))))
          (if (null btn-position)
              nil
            (setf current-button (button-at btn-position)))))
    current-button))

(defun neo-buffer--get-filename-current-line (&optional default)
  "Return filename for first button in current line.
If there is no button in current line, then return DEFAULT."
  (let ((btn (neo-buffer--get-button-current-line)))
    (if (not (null btn))
        (button-get btn 'neo-full-path)
      default)))

(defun neo-buffer--lock-width ()
  "Lock the width size for NeoTree window."
  (if neo-window-fixed-size
      (setq window-size-fixed 'width)))

(defun neo-buffer--unlock-width ()
  "Unlock the width size for NeoTree window."
  (setq window-size-fixed nil))

(defun neo-buffer--rename-node ()
  "Rename current node as another path."
  (interactive)
  (let* ((current-path (neo-buffer--get-filename-current-line))
         (buffer (find-buffer-visiting current-path))
         to-path
         msg)
    (unless (null current-path)
      (setq msg (format "Rename [%s] to: " (neo-path--file-short-name current-path)))
      (setq to-path (read-file-name msg (file-name-directory current-path)))
      (if buffer
          (with-current-buffer buffer
            (set-visited-file-name to-path nil t)))
      (rename-file current-path to-path 1)
      (neo-buffer--refresh t)
      (message "Rename successful."))))

(defun neo-buffer--copy-node ()
  "Copies current node as another path."
  (interactive)
  (let* ((current-path (neo-buffer--get-filename-current-line))
         (buffer (find-buffer-visiting current-path))
         to-path
         msg)
    (unless (null current-path)
      (setq msg (format "Copy [%s] to: " (neo-path--file-short-name current-path)))
      (setq to-path (read-file-name msg (file-name-directory current-path)))
      (if (file-directory-p current-path)
          (copy-directory current-path to-path)
        (copy-file current-path to-path))
      (neo-buffer--refresh t)
      (message "Copy successful."))))

(defun neo-buffer--select-file-node (file &optional recursive-p)
  "Select the node that corresponds to the FILE.
If RECURSIVE-P is non nil, find files will recursively."
  (let ((efile file)
        (iter-curr-dir nil)
        (file-node-find-p nil)
        (file-node-list nil))
    (unless (file-name-absolute-p efile)
      (setq efile (expand-file-name efile)))
    (setq iter-curr-dir efile)
    (catch 'return
      (while t
        (setq iter-curr-dir (neo-path--updir iter-curr-dir))
        (push iter-curr-dir file-node-list)
        (when (neo-path--file-equal-p iter-curr-dir neo-buffer--start-node)
          (setq file-node-find-p t)
          (throw 'return nil))
        (let ((niter-curr-dir (file-remote-p iter-curr-dir 'localname)))
          (unless niter-curr-dir
            (setq niter-curr-dir iter-curr-dir))
          (when (neo-path--file-equal-p niter-curr-dir "/")
            (setq file-node-find-p nil)
            (throw 'return nil)))))
    (when file-node-find-p
      (dolist (p file-node-list)
        (neo-buffer--set-expand p t))
      (neo-buffer--save-cursor-pos file)
      (neo-buffer--refresh nil))))

(defun neo-buffer--change-root (root-dir)
  "Change the tree root to ROOT-DIR."
  (let ((path root-dir)
        start-path)
    (unless (and (file-exists-p path)
                 (file-directory-p path))
      (throw 'error "The path is not a valid directory."))
    (setq start-path (expand-file-name (substitute-in-file-name path)))
    (setq neo-buffer--start-node start-path)
    (cd start-path)
    (neo-buffer--save-cursor-pos path nil)
    (neo-buffer--refresh nil)))

(defun neo-buffer--get-nodes-for-select-down-node (path)
  "Return the node list for the down dir selection."
  (if path
      (when (file-name-directory path)
        (if (neo-buffer--expanded-node-p path)
            (neo-buffer--get-nodes path)
          (neo-buffer--get-nodes (file-name-directory path))))
    (neo-buffer--get-nodes (file-name-as-directory neo-buffer--start-node))))

(defun neo-buffer--get-nodes-for-sibling (path)
  "Return the node list for the sibling selection. Return nil of no nodes can
be found.
The returned list is a directory list if path is a directory, otherwise it is
a file list."
  (when path
    (let ((nodes (neo-buffer--get-nodes (file-name-directory path))))
      (if (file-directory-p path)
          (car nodes)
        (cdr nodes)))))

(defun neo-buffer--sibling (path &optional previous)
  "Return the next sibling of node PATH.
If PREVIOUS is non-nil the previous sibling is returned."
  (let* ((nodes (neo-buffer--get-nodes-for-sibling path)))
    (when nodes
      (let ((i (neo-buffer--get-node-index path nodes))
            (l (length nodes)))
        (if i (nth (mod (+ i (if previous -1 1)) l) nodes))))))

(defun neo-buffer--execute (arg &optional file-fn dir-fn)
  "Define the behaviors for keyboard event.
ARG is the parameter for command.
If FILE-FN is non-nil, it will executed when a file node.
If DIR-FN is non-nil, it will executed when a dir node."
  (interactive "P")
  (let* ((btn-full-path (neo-buffer--get-filename-current-line))
         is-file-p
         enter-fn)
    (unless (null btn-full-path)
      (setq is-file-p (not (file-directory-p btn-full-path))
            enter-fn (if is-file-p file-fn dir-fn))
      (unless (null enter-fn)
        (funcall enter-fn btn-full-path arg)
        (run-hook-with-args
         'neo-enter-hook
         (if is-file-p 'file 'directory)
         btn-full-path
         arg)))
    btn-full-path))

(defun neo-buffer--set-show-hidden-file-p (show-p)
  "If SHOW-P is non-nil, show hidden nodes in tree."
  (setq neo-buffer--show-hidden-file-p show-p)
  (neo-buffer--refresh t))

(defun neo-buffer--forward-line (n)
  "Move N lines forward in NeoTree buffer."
  (forward-line (or n 1))
  (neo-buffer--post-move))

;;
;; Mode-line methods
;;

(defun neo-mode-line--compute-format (parent index ndirs nfiles)
  "Return a formated string to be used in the `neotree' mode-line."
  (let* ((nall (+ ndirs nfiles))
         (has-dirs (> ndirs 0))
         (has-files (> nfiles 0))
         (msg-index (when index (format "[%s/%s] " index nall)))
         (msg-ndirs (when has-dirs (format (if has-files " (D:%s" " (D:%s)") ndirs)))
         (msg-nfiles (when has-files (format (if has-dirs " F:%s)" " (F:%s)") nfiles)))
         (msg-directory (file-name-nondirectory (directory-file-name parent)))
         (msg-directory-max-length (- (window-width)
                                      (length msg-index)
                                      (length msg-ndirs)
                                      (length msg-nfiles))))
    (setq msg-directory (if (<= (length msg-directory) msg-directory-max-length)
                            msg-directory
                          (concat (substring msg-directory
                                             0 (- msg-directory-max-length 3))
                                  "...")))
    (propertize
     (decode-coding-string (concat msg-index msg-directory msg-ndirs msg-nfiles) 'utf-8)
     'help-echo (decode-coding-string parent 'utf-8))))

;;
;; Window methods
;;

(defun neo-window--init (window buffer)
  "Make WINDOW a NeoTree window.
NeoTree buffer is BUFFER."
  (neo-buffer--with-resizable-window
   (switch-to-buffer buffer)
   (set-window-parameter window 'no-delete-other-windows t)
   (set-window-dedicated-p window t))
  window)

(defun neo-window--zoom (method)
  "Zoom the NeoTree window, the METHOD should one of these options:
'maximize 'minimize 'zoom-in 'zoom-out."
  (neo-buffer--unlock-width)
  (cond
   ((eq method 'maximize)
    (maximize-window))
   ((eq method 'minimize)
    (neo-util--set-window-width (selected-window) neo-window-width))
   ((eq method 'zoom-in)
    (shrink-window-horizontally 2))
   ((eq method 'zoom-out)
    (enlarge-window-horizontally 2)))
  (neo-buffer--lock-width))

(defun neo-window--minimize-p ()
  "Return non-nil when the NeoTree window is minimize."
  (<= (window-width) neo-window-width))

;;
;; Interactive functions
;;

(defun neotree-next-line (&optional count)
  "Move next line in NeoTree buffer.
Optional COUNT argument, moves COUNT lines down."
  (interactive "p")
  (neo-buffer--forward-line (or count 1)))

(defun neotree-previous-line (&optional count)
  "Move previous line in NeoTree buffer.
Optional COUNT argument, moves COUNT lines up."
  (interactive "p")
  (neo-buffer--forward-line (- (or count 1))))

;;;###autoload
(defun neotree-find (&optional path default-path)
  "Quick select node which specified PATH in NeoTree.
If path is nil and no buffer file name, then use DEFAULT-PATH,"
  (interactive)
  (let* ((ndefault-path (if default-path default-path
                          (neo-path--get-working-dir)))
         (npath (if path path
                  (or (buffer-file-name) ndefault-path)))
         (do-open-p nil))
    (if (and (not neo-force-change-root)
             (not (neo-global--file-in-root-p npath))
             (neo-global--window-exists-p))
        (setq do-open-p (funcall neo-confirm-change-root "File not found in root path, do you want to change root?"))
      (setq do-open-p t))
    (when do-open-p
      (neo-global--open-and-find npath))
    (when neo-auto-indent-point
      (neo-point-auto-indent)))
  (neo-global--select-window))

(defun neotree-click-changes-root-toggle ()
  "Toggle the variable neo-click-changes-root.
If true, clicking on a directory will change the current root to
the directory instead of showing the directory contents."
  (interactive)
  (setq neo-click-changes-root (not neo-click-changes-root)))

(defun neo-open-dir (full-path &optional arg)
  "Toggle fold a directory node.

FULL-PATH is the path of the directory.
ARG is ignored."
  (if neo-click-changes-root
      (neotree-change-root)
    (progn
      (let ((new-state (neo-buffer--toggle-expand full-path)))
        (neo-buffer--refresh t)
        (when neo-auto-indent-point
          (when new-state (forward-line 1))
          (neo-point-auto-indent))))))


(defun neo--expand-recursive (path state)
  "Set the state of children recursively.

The children of PATH will have state STATE."
  (let ((children (car (neo-buffer--get-nodes path) )))
    (dolist (node children)
      (neo-buffer--set-expand node state)
      (neo--expand-recursive node state ))))

(defun neo-open-dir-recursive (full-path &optional arg)  
  "Toggle fold a directory node recursively.

The children of the node will also be opened recursively.
FULL-PATH is the path of the directory.
ARG is ignored."
  (if neo-click-changes-root
      (neotree-change-root)    
    (let ((new-state (neo-buffer--toggle-expand full-path))
          (children (car (neo-buffer--get-nodes full-path))))
      (dolist (node children)
        (neo-buffer--set-expand node new-state)
        (neo--expand-recursive node new-state))
      (neo-buffer--refresh t))))

(defun neo-open-dired (full-path &optional arg)
  "Open file or directory node in `dired-mode'.

FULL-PATH is the path of node.
ARG is same as `neo-open-file'."
  (neo-global--select-mru-window arg)
  (dired full-path))

(defun neo-open-file (full-path &optional arg)
  "Open a file node.

FULL-PATH is the file path you want to open.
If ARG is an integer then the node is opened in a window selected via
`winum' or`window-numbering' (if available) according to the passed number.
If ARG is `|' then the node is opened in new vertically split window.
If ARG is `-' then the node is opened in new horizontally split window."
  (neo-global--select-mru-window arg)
  (find-file full-path))

(defun neo-open-file-vertical-split (full-path arg)
  "Open the current node is a vertically split window.
FULL-PATH and ARG are the same as `neo-open-file'."
  (neo-open-file full-path "|"))

(defun neo-open-file-horizontal-split (full-path arg)
  "Open the current node is horizontally split window.
FULL-PATH and ARG are the same as `neo-open-file'."
  (neo-open-file full-path "-"))

(defun neo-open-file-ace-window (full-path arg)
  "Open the current node in a window chosen by ace-window.
FULL-PATH and ARG are the same as `neo-open-file'."
  (neo-open-file full-path "a"))

(defun neotree-open-file-in-system-application ()
  "Open a file under point in the system application."
  (interactive)
  (call-process neo-default-system-application nil 0 nil
                (neo-buffer--get-filename-current-line)))

(defun neotree-change-root ()
  "Change root to current node dir.
If current node is a file, then it will do nothing.
If cannot find any node in current line, it equivalent to using `neotree-dir'."
  (interactive)
  (neo-global--select-window)
  (let ((btn-full-path (neo-buffer--get-filename-current-line)))
    (if (null btn-full-path)
        (call-interactively 'neotree-dir)
      (neo-global--open-dir btn-full-path))))

(defun neotree-select-up-node ()
  "Select the parent directory of the current node. Change the root if
necessary. "
  (interactive)
  (neo-global--select-window)
  (let* ((btn-full-path (neo-buffer--get-filename-current-line))
         (btn-parent-dir (if btn-full-path (file-name-directory btn-full-path)))
         (root-slash (file-name-as-directory neo-buffer--start-node)))
    (cond
     ((equal btn-parent-dir root-slash) (neo-global--open-dir root-slash))
     (btn-parent-dir (neotree-find btn-parent-dir))
     (t (neo-global--open-dir (file-name-directory
                               (directory-file-name root-slash)))))))

(defun neotree-select-down-node ()
  "Select an expanded directory or content directory according to the
current node, in this order:
- select the first expanded child node if the current node has one
- select the content of current node if it is expanded
- select the next expanded sibling if the current node is not expanded."
  (interactive)
  (let* ((btn-full-path (neo-buffer--get-filename-current-line))
         (path (if btn-full-path btn-full-path neo-buffer--start-node))
         (nodes (neo-buffer--get-nodes-for-select-down-node path)))
    (when nodes
      (if (or (equal path neo-buffer--start-node)
              (neo-buffer--expanded-node-p path))
          ;; select the first expanded child node
          (let ((expanded-dir (catch 'break
                                (dolist (node (car nodes))
                                  (if (neo-buffer--expanded-node-p node)
                                      (throw 'break node)))
                                nil)))
            (if expanded-dir
                (neotree-find expanded-dir)
              ;; select the directory content if needed
              (let ((dirs (car nodes))
                    (files (cdr nodes)))
                (if (> (length dirs) 0)
                    (neotree-find (car dirs))
                  (when (> (length files) 0)
                    (neotree-find (car files)))))))
        ;; select the next expanded sibling
        (let ((sibling (neo-buffer--sibling path)))
          (while (and (not (neo-buffer--expanded-node-p sibling))
                      (not (equal sibling path)))
            (setq sibling (neo-buffer--sibling sibling)))
          (when (not (string< sibling path))
            ;; select next expanded sibling
            (neotree-find sibling)))))))

(defun neotree-select-next-sibling-node ()
  "Select the next sibling of current node.
If the current node is the last node then the first node is selected."
  (interactive)
  (let ((sibling (neo-buffer--sibling (neo-buffer--get-filename-current-line))))
    (when sibling (neotree-find sibling))))

(defun neotree-select-previous-sibling-node ()
  "Select the previous sibling of current node.
If the current node is the first node then the last node is selected."
  (interactive)
  (let ((sibling (neo-buffer--sibling (neo-buffer--get-filename-current-line) t)))
    (when sibling (neotree-find sibling))))

(defun neotree-create-node (filename)
  "Create a file or directory use specified FILENAME in current node."
  (interactive
   (let* ((current-dir (neo-buffer--get-filename-current-line neo-buffer--start-node))
          (current-dir (neo-path--match-path-directory current-dir))
          (filename (read-file-name "Filename:" current-dir)))
     (if (file-directory-p filename)
         (setq filename (concat filename "/")))
     (list filename)))
  (catch 'rlt
    (let ((is-file nil))
      (when (= (length filename) 0)
        (throw 'rlt nil))
      (setq is-file (not (equal (substring filename -1) "/")))
      (when (file-exists-p filename)
        (message "File %S already exists." filename)
        (throw 'rlt nil))
      (when (and is-file
                 (funcall neo-confirm-create-file (format "Do you want to create file %S ?"
                                                          filename)))
        ;; ensure parent directory exist before saving
        (mkdir (substring filename 0 (+ 1 (cl-position ?/ filename :from-end t))) t)
        ;; NOTE: create a empty file
        (write-region "" nil filename)
        (neo-buffer--save-cursor-pos filename)
        (neo-buffer--refresh nil)
        (if neo-create-file-auto-open
            (find-file-other-window filename)))
      (when (and (not is-file)
                 (funcall neo-confirm-create-directory (format "Do you want to create directory %S?"
                                                               filename)))
        (mkdir filename t)
        (neo-buffer--save-cursor-pos filename)
        (neo-buffer--refresh nil)))))

(defun neotree-delete-node ()
  "Delete current node."
  (interactive)
  (let* ((filename (neo-buffer--get-filename-current-line))
         (buffer (find-buffer-visiting filename))
         (deleted-p nil)
         (trash delete-by-moving-to-trash))
    (catch 'end
      (if (null filename) (throw 'end nil))
      (if (not (file-exists-p filename)) (throw 'end nil))
      (if (not (funcall neo-confirm-delete-file (format "Do you really want to delete %S?"
                                                        filename)))
          (throw 'end nil))
      (if (file-directory-p filename)
          ;; delete directory
          (progn
            (unless (neo-path--has-subfile-p filename)
              (delete-directory filename nil trash)
              (setq deleted-p t)
              (throw 'end nil))
            (when (funcall neo-confirm-delete-directory-recursively
                           (format "%S is a directory, delete it recursively?"
                                   filename))
              (when (funcall neo-confirm-kill-buffers-for-files-in-directory
                             (format "kill buffers for files in directory %S?"
                                     filename))
                (neo-util--kill-buffers-for-path filename))
              (delete-directory filename t trash)
              (setq deleted-p t)))
        ;; delete file
        (progn
          (delete-file filename trash)
          (when buffer
            (kill-buffer-ask buffer))
          (setq deleted-p t))))
    (when deleted-p
      (message "%S deleted." filename)
      (neo-buffer--refresh t))
    filename))

(defun neotree-rename-node ()
  "Rename current node."
  (interactive)
  (neo-buffer--rename-node))

(defun neotree-copy-node ()
  "Copy current node."
  (interactive)
  (neo-buffer--copy-node))

(defun neotree-hidden-file-toggle ()
  "Toggle show hidden files."
  (interactive)
  (neo-buffer--set-show-hidden-file-p (not neo-buffer--show-hidden-file-p)))

(defun neotree-empty-fn ()
  "Used to bind the empty function to the shortcut."
  (interactive))

(defun neotree-refresh (&optional is-auto-refresh)
  "Refresh the NeoTree buffer."
  (interactive)
  (if (eq (current-buffer) (neo-global--get-buffer))
      (neo-buffer--refresh t)
    (save-excursion
      (let ((cw (selected-window)))  ;; save current window
        (if is-auto-refresh
            (let ((origin-buffer-file-name (buffer-file-name)))
              (when (and (fboundp 'projectile-project-p)
                         (projectile-project-p)
                         (fboundp 'projectile-project-root))
                (neo-global--open-dir (projectile-project-root))
                (neotree-find (projectile-project-root)))
              (neotree-find origin-buffer-file-name))
          (neo-buffer--refresh t t))
        (recenter)
        (when (or is-auto-refresh neo-toggle-window-keep-p)
          (select-window cw))))))

(defun neotree-stretch-toggle ()
  "Make the NeoTree window toggle maximize/minimize."
  (interactive)
  (neo-global--with-window
    (if (neo-window--minimize-p)
        (neo-window--zoom 'maximize)
      (neo-window--zoom 'minimize))))

(defun neotree-collapse-all ()
  (interactive)
  "Collapse all expanded folders in the neotree buffer"
  (setq list-of-expanded-folders neo-buffer--expanded-node-list)
  (dolist (folder list-of-expanded-folders)
    (neo-buffer--toggle-expand folder)
    (neo-buffer--refresh t)
    )
  )
;;;###autoload
(defun neotree-projectile-action ()
  "Integration with `Projectile'.

Usage:
    (setq projectile-switch-project-action 'neotree-projectile-action).

When running `projectile-switch-project' (C-c p p), `neotree' will change root
automatically."
  (interactive)
  (cond
   ((fboundp 'projectile-project-root)
    (neotree-dir (projectile-project-root)))
   (t
    (error "Projectile is not available"))))

;;;###autoload
(defun neotree-toggle ()
  "Toggle show the NeoTree window."
  (interactive)
  (if (neo-global--window-exists-p)
      (neotree-hide)
    (neotree-show)))

;;;###autoload
(defun neotree-show ()
  "Show the NeoTree window."
  (interactive)
  (let ((cw (selected-window))
        (path (buffer-file-name)))  ;; save current window and buffer
    (if neo-smart-open
        (progn
          (when (and (fboundp 'projectile-project-p)
                     (projectile-project-p)
                     (fboundp 'projectile-project-root))
            (neotree-dir (projectile-project-root)))
          (neotree-find path))
      (neo-global--open))
    (neo-global--select-window)
    (when neo-toggle-window-keep-p
      (select-window cw))))

;;;###autoload
(defun neotree-hide ()
  "Close the NeoTree window."
  (interactive)
  (if (neo-global--window-exists-p)
      (delete-window neo-global--window)))

;;;###autoload
(defun neotree-dir (path)
  "Show the NeoTree window, and change root to PATH."
  (interactive "DDirectory: ")
  (neo-global--open-dir path)
  (neo-global--select-window))

;;;###autoload
(defalias 'neotree 'neotree-show "Show the NeoTree window.")

;;
;; backward compatible
;;

(defun neo-bc--make-obsolete-message (from to)
  (message "Warning: `%S' is obsolete. Use `%S' instead." from to))

(defun neo-buffer--enter-file (path)
  (neo-bc--make-obsolete-message 'neo-buffer--enter-file 'neo-open-file))

(defun neo-buffer--enter-dir (path)
  (neo-bc--make-obsolete-message 'neo-buffer--enter-dir 'neo-open-dir))

(defun neotree-enter (&optional arg)
  "NeoTree typical open event.
ARG are the same as `neo-open-file'."
  (interactive "P")
  (neo-buffer--execute arg 'neo-open-file 'neo-open-dir))

(defun neotree-quick-look (&optional arg)
  "Quick Look like NeoTree open event.
ARG are the same as `neo-open-file'."
  (interactive "P")
  (neotree-enter arg)
  (neo-global--select-window))

(defun neotree-enter-vertical-split ()
  "NeoTree open event, file node will opened in new vertically split window."
  (interactive)
  (neo-buffer--execute nil 'neo-open-file-vertical-split 'neo-open-dir))

(defun neotree-enter-horizontal-split ()
  "NeoTree open event, file node will opened in new horizontally split window."
  (interactive)
  (neo-buffer--execute nil 'neo-open-file-horizontal-split 'neo-open-dir))

(defun neotree-enter-ace-window ()
  "NeoTree open event, file node will be opened in window chosen by ace-window."
  (interactive)
  (neo-buffer--execute nil 'neo-open-file-ace-window 'neo-open-dir))

(defun neotree-copy-filepath-to-yank-ring ()
  "Neotree convenience interactive function: file node path will be added to the kill ring."
  (interactive)
  (kill-new (neo-buffer--get-filename-current-line)))

(defun neotree-split-window-sensibly (&optional window)
  "An neotree-version of split-window-sensibly,
which is used to fix issue #209.
(setq split-window-preferred-function 'neotree-split-window-sensibly)"
  (let ((window (or window (selected-window))))
    (or (split-window-sensibly window)
        (and (get-buffer-window neo-buffer-name)
             (not (window-minibuffer-p window))
             ;; If WINDOW is the only window on its frame
             ;; (or only include Neo window) and is not the
             ;; minibuffer window, try to split it vertically disregarding
             ;; the value of `split-height-threshold'.
             (let ((split-height-threshold 0))
               (when (window-splittable-p window)
                 (with-selected-window window
                   (split-window-below))))))))

(provide 'neotree)
;;; neotree.el ends here