startify.vim 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  1. " vim: et sw=2 sts=2
  2. " Plugin: https://github.com/mhinz/vim-startify
  3. " Description: A fancy start screen for Vim.
  4. " Maintainer: Marco Hinz <http://github.com/mhinz>
  5. if exists('g:autoloaded_startify') || &compatible
  6. finish
  7. endif
  8. let g:autoloaded_startify = 1
  9. " Init: values {{{1
  10. let s:numfiles = get(g:, 'startify_files_number', 10)
  11. let s:show_special = get(g:, 'startify_enable_special', 1)
  12. let s:delete_buffers = get(g:, 'startify_session_delete_buffers')
  13. let s:relative_path = get(g:, 'startify_relative_path') ? ':.' : ':p:~'
  14. let s:session_dir = resolve(expand(get(g:, 'startify_session_dir',
  15. \ has('win32') ? '$HOME\vimfiles\session' : '~/.vim/session')))
  16. let s:skiplist = get(g:, 'startify_skiplist', [
  17. \ 'COMMIT_EDITMSG',
  18. \ fnamemodify($VIMRUNTIME, ':p') .'/doc',
  19. \ 'bundle/.*/doc',
  20. \ ])
  21. " Function: #get_separator {{{1
  22. function! startify#get_separator() abort
  23. return !exists('+shellslash') || &shellslash ? '/' : '\'
  24. endfunction
  25. let s:sep = startify#get_separator()
  26. " Function: #get_lastline {{{1
  27. function! startify#get_lastline() abort
  28. return s:lastline + 1
  29. endfunction
  30. " Function: #insane_in_the_membrane {{{1
  31. function! startify#insane_in_the_membrane() abort
  32. if &insertmode
  33. return
  34. endif
  35. if !empty(v:servername) && exists('g:startify_skiplist_server')
  36. for servname in g:startify_skiplist_server
  37. if servname == v:servername
  38. return
  39. endif
  40. endfor
  41. endif
  42. setlocal
  43. \ bufhidden=wipe
  44. \ buftype=nofile
  45. \ nobuflisted
  46. \ nocursorcolumn
  47. \ nocursorline
  48. \ nolist
  49. \ nonumber
  50. \ noswapfile
  51. if empty(&statusline)
  52. setlocal statusline=\ startify
  53. endif
  54. if v:version >= 703
  55. setlocal norelativenumber
  56. endif
  57. if exists('g:startify_custom_header')
  58. call append('$', g:startify_custom_header)
  59. endif
  60. let s:tick = 0
  61. let s:entries = {}
  62. if s:show_special
  63. call append('$', [' [e] <empty buffer>', ''])
  64. let s:entries[line('$')-1] = {
  65. \ 'index': 'e',
  66. \ 'type': 'special',
  67. \ 'cmd': 'enew',
  68. \ 'path': '',
  69. \ 'marked': 0,
  70. \ }
  71. endif
  72. let s:entry_number = 0
  73. if filereadable('Session.vim')
  74. call append('$', [' [0] '. getcwd() . s:sep .'Session.vim', ''])
  75. let s:entries[line('$')-1] = {
  76. \ 'index': '0',
  77. \ 'type': 'session',
  78. \ 'cmd': 'call startify#session_delete_buffers() | source',
  79. \ 'path': 'Session.vim',
  80. \ 'marked': 0,
  81. \ }
  82. let s:entry_number = 1
  83. let l:show_session = 1
  84. endif
  85. if empty(v:oldfiles)
  86. echohl WarningMsg
  87. echomsg "startify: Can't read viminfo file. Read :help startify-faq-02"
  88. echohl None
  89. endif
  90. let b:startify_section_header_lines = []
  91. let s:lists = get(g:, 'startify_list_order', [
  92. \ [' Most recently used files:'],
  93. \ 'files',
  94. \ [' Most recently used files in the current directory:'],
  95. \ 'dir',
  96. \ [' My sessions:'],
  97. \ 'sessions',
  98. \ [' My bookmarks:'],
  99. \ 'bookmarks',
  100. \ ])
  101. for item in s:lists
  102. if type(item) == 1
  103. call s:show_{item}()
  104. else
  105. let s:last_message = item
  106. endif
  107. unlet item
  108. endfor
  109. silent $delete _
  110. if s:show_special
  111. call append('$', ['', ' [q] <quit>'])
  112. let s:entries[line('$')] = {
  113. \ 'index': 'q',
  114. \ 'type': 'special',
  115. \ 'cmd': 'call <sid>close()',
  116. \ 'path': '',
  117. \ 'marked': 0,
  118. \ }
  119. endif
  120. " compute first line offset
  121. let s:firstline = 2
  122. " increase offset if there is a custom header
  123. if exists('g:startify_custom_header')
  124. let s:firstline += len(g:startify_custom_header)
  125. endif
  126. " no special, no local Session.vim, but a section header
  127. if !s:show_special && !exists('l:show_session') && type(s:lists[0]) == 3
  128. let s:firstline += len(s:lists[0]) + 1
  129. endif
  130. let s:lastline = line('$')
  131. if exists('g:startify_custom_footer')
  132. call append('$', g:startify_custom_footer)
  133. endif
  134. setlocal nomodifiable nomodified
  135. call s:set_mappings()
  136. call cursor(s:firstline + (s:show_special ? 2 : 0), 5)
  137. autocmd startify CursorMoved <buffer> call s:set_cursor()
  138. set filetype=startify
  139. silent! doautocmd <nomodeline> User Startified
  140. endfunction
  141. " Function: #session_load {{{1
  142. function! startify#session_load(...) abort
  143. if !isdirectory(s:session_dir)
  144. echomsg 'The session directory does not exist: '. s:session_dir
  145. return
  146. elseif empty(startify#session_list_as_string(''))
  147. echomsg 'There are no sessions...'
  148. return
  149. endif
  150. call inputsave()
  151. let spath = s:session_dir . s:sep . (exists('a:1')
  152. \ ? a:1
  153. \ : input('Load this session: ', fnamemodify(v:this_session, ':t'), 'custom,startify#session_list_as_string'))
  154. \ | redraw
  155. call inputrestore()
  156. if filereadable(spath)
  157. if get(g:, 'startify_session_persistence')
  158. \ && exists('v:this_session')
  159. \ && filewritable(v:this_session)
  160. call startify#session_write(fnameescape(v:this_session))
  161. endif
  162. call startify#session_delete_buffers()
  163. execute 'source '. fnameescape(spath)
  164. else
  165. echo 'No such file: '. spath
  166. endif
  167. endfunction
  168. " Function: #session_save {{{1
  169. function! startify#session_save(...) abort
  170. if !isdirectory(s:session_dir)
  171. if exists('*mkdir')
  172. echo 'The session directory does not exist: '. s:session_dir .'. Create it? [y/n]'
  173. if (nr2char(getchar()) == 'y')
  174. call mkdir(s:session_dir, 'p')
  175. else
  176. echo
  177. return
  178. endif
  179. else
  180. echo 'The session directory does not exist: '. s:session_dir
  181. return
  182. endif
  183. endif
  184. call inputsave()
  185. let sname = exists('a:1')
  186. \ ? a:1
  187. \ : input('Save under this session name: ', fnamemodify(v:this_session, ':t'), 'custom,startify#session_list_as_string')
  188. \ | redraw
  189. call inputrestore()
  190. if empty(sname)
  191. echo 'You gave an empty name!'
  192. return
  193. endif
  194. let spath = s:session_dir . s:sep . sname
  195. if !filereadable(spath)
  196. call startify#session_write(fnameescape(spath))
  197. echo 'Session saved under: '. spath
  198. return
  199. endif
  200. echo 'Session already exists. Overwrite? [y/n]' | redraw
  201. if nr2char(getchar()) == 'y'
  202. call startify#session_write(fnameescape(spath))
  203. echo 'Session saved under: '. spath
  204. else
  205. echo 'Did NOT save the session!'
  206. endif
  207. endfunction
  208. " Function: #session_close {{{1
  209. function! startify#session_close() abort
  210. if exists('v:this_session') && filewritable(v:this_session)
  211. call startify#session_write(fnameescape(v:this_session))
  212. let v:this_session = ''
  213. endif
  214. call startify#session_delete_buffers()
  215. Startify
  216. endfunction
  217. " Function: #session_write {{{1
  218. function! startify#session_write(spath)
  219. let ssop = &sessionoptions
  220. try
  221. " if this function is called while being in the Startify buffer
  222. " (by loading another session or running :SSave/:SLoad directly)
  223. " switch back to the previous buffer before saving the session
  224. if &filetype == 'startify'
  225. let callingbuffer = bufnr('#')
  226. if callingbuffer > 0
  227. execute 'buffer' callingbuffer
  228. endif
  229. endif
  230. " prevent saving already deleted buffers that were in the arglist
  231. for arg in argv()
  232. if !buflisted(arg)
  233. execute 'argdelete' fnameescape(arg)
  234. endif
  235. endfor
  236. set sessionoptions-=options
  237. execute 'mksession!' a:spath
  238. catch
  239. execute 'echoerr' string(v:exception)
  240. finally
  241. let &sessionoptions = ssop
  242. endtry
  243. if exists('g:startify_session_remove_lines')
  244. \ || exists('g:startify_session_savevars')
  245. \ || exists('g:startify_session_savecmds')
  246. execute 'split' a:spath
  247. " remove lines from the session file
  248. if exists('g:startify_session_remove_lines')
  249. for pattern in g:startify_session_remove_lines
  250. execute 'silent global/'. pattern .'/delete'
  251. endfor
  252. endif
  253. " put existing variables from savevars into session file
  254. if exists('g:startify_session_savevars')
  255. call append(line('$')-3, map(filter(copy(g:startify_session_savevars), 'exists(v:val)'), '"let ". v:val ." = ". strtrans(string(eval(v:val)))'))
  256. endif
  257. " put commands from savecmds into session file
  258. if exists('g:startify_session_savecmds')
  259. call append(line('$')-3, g:startify_session_savecmds)
  260. endif
  261. setlocal bufhidden=delete
  262. silent update
  263. silent hide
  264. endif
  265. endfunction
  266. " Function: #session_delete {{{1
  267. function! startify#session_delete(...) abort
  268. if !isdirectory(s:session_dir)
  269. echo 'The session directory does not exist: '. s:session_dir
  270. return
  271. elseif empty(startify#session_list_as_string(''))
  272. echo 'There are no sessions...'
  273. return
  274. endif
  275. call inputsave()
  276. let spath = s:session_dir . s:sep . (exists('a:1')
  277. \ ? a:1
  278. \ : input('Delete this session: ', fnamemodify(v:this_session, ':t'), 'custom,startify#session_list_as_string'))
  279. \ | redraw
  280. call inputrestore()
  281. echo 'Really delete '. spath .'? [y/n]' | redraw
  282. if (nr2char(getchar()) == 'y')
  283. if delete(spath) == 0
  284. echo 'Deleted session '. spath .'!'
  285. else
  286. echo 'Deletion failed!'
  287. endif
  288. else
  289. echo 'Deletion aborted!'
  290. endif
  291. endfunction
  292. " Function: #session_delete_buffers {{{1
  293. function! startify#session_delete_buffers() abort
  294. if !s:delete_buffers
  295. return
  296. endif
  297. let n = 1
  298. while n <= bufnr('$')
  299. if buflisted(n)
  300. silent execute 'bdelete' n
  301. endif
  302. let n += 1
  303. endwhile
  304. endfunction
  305. " Function: #session_list {{{1
  306. function! startify#session_list(lead, ...) abort
  307. return map(split(globpath(s:session_dir, '*'.a:lead.'*'), '\n'), 'fnamemodify(v:val, ":t")')
  308. endfunction
  309. " Function: #session_list_as_string {{{1
  310. function! startify#session_list_as_string(lead, ...) abort
  311. return join(map(split(globpath(s:session_dir, '*'.a:lead.'*'), '\n'), 'fnamemodify(v:val, ":t")'), "\n")
  312. endfunction
  313. " Function: startify#debug {{{1
  314. function! startify#debug()
  315. for k in sort(keys(s:entries))
  316. echomsg '['. k .'] = '. string(s:entries[k])
  317. endfor
  318. endfunction
  319. " Function: #open_buffers {{{1
  320. function! startify#open_buffers(...) abort
  321. if exists('a:1') " used in mappings
  322. let entry = s:entries[a:1]
  323. execute entry.cmd entry.path
  324. return
  325. endif
  326. let marked = filter(copy(s:entries), 'v:val.marked')
  327. if empty(marked) " open current entry
  328. let entry = s:entries[line('.')]
  329. execute entry.cmd entry.path
  330. return
  331. endif
  332. enew
  333. setlocal nobuflisted
  334. " Open all marked entries.
  335. for entry in sort(values(marked), 's:sort_by_tick')
  336. if entry.type == 'special'
  337. execute entry.cmd
  338. elseif entry.type == 'session'
  339. execute entry.cmd entry.path
  340. elseif entry.type == 'file'
  341. if line2byte('$') == -1
  342. execute 'edit' entry.path
  343. else
  344. execute entry.cmd entry.path
  345. endif
  346. call s:check_user_options()
  347. endif
  348. endfor
  349. endfunction
  350. " Function: s:display_by_path {{{1
  351. function! s:display_by_path(path_prefix, path_format) abort
  352. let oldfiles = call(get(g:, 'startify_enable_unsafe') ? 's:filter_oldfiles_unsafe' : 's:filter_oldfiles',
  353. \ [a:path_prefix, a:path_format])
  354. if !empty(oldfiles)
  355. if exists('s:last_message')
  356. call s:print_section_header()
  357. endif
  358. for [absolute_path, entry_path] in oldfiles
  359. let index = s:get_index_as_string(s:entry_number)
  360. call append('$', ' ['. index .']'. repeat(' ', (3 - strlen(index))) . entry_path)
  361. if has('win32')
  362. let absolute_path = substitute(absolute_path, '\[', '\[[]', 'g')
  363. endif
  364. let s:entries[line('$')] = {
  365. \ 'index': index,
  366. \ 'type': 'file',
  367. \ 'cmd': 'edit',
  368. \ 'path': absolute_path,
  369. \ 'marked': 0,
  370. \ }
  371. let s:entry_number += 1
  372. endfor
  373. call append('$', '')
  374. endif
  375. endfunction
  376. " Function: s:filter_oldfiles {{{1
  377. function! s:filter_oldfiles(path_prefix, path_format) abort
  378. let path_prefix = '\V'. escape(a:path_prefix, '\')
  379. let counter = s:numfiles
  380. let entries = {}
  381. let oldfiles = []
  382. for fname in v:oldfiles
  383. if counter <= 0
  384. break
  385. endif
  386. let absolute_path = fnamemodify(resolve(fname), ":p")
  387. " filter duplicates, bookmarks and entries from the skiplist
  388. if has_key(entries, absolute_path)
  389. \ || !filereadable(absolute_path)
  390. \ || s:is_in_skiplist(absolute_path)
  391. \ || (exists('g:startify_bookmarks') && s:is_bookmark(absolute_path))
  392. \ || match(absolute_path, path_prefix)
  393. continue
  394. endif
  395. let entry_path = fnamemodify(absolute_path, a:path_format)
  396. let entries[absolute_path] = 1
  397. let counter -= 1
  398. let oldfiles += [[fnameescape(absolute_path), entry_path]]
  399. endfor
  400. return oldfiles
  401. endfun
  402. " Function: s:filter_oldfiles_unsafe {{{1
  403. function! s:filter_oldfiles_unsafe(path_prefix, path_format) abort
  404. let path_prefix = '\V'. escape(a:path_prefix, '\')
  405. let counter = s:numfiles
  406. let entries = {}
  407. let oldfiles = []
  408. for fname in v:oldfiles
  409. if counter <= 0
  410. break
  411. endif
  412. let absolute_path = glob(fnamemodify(fname, ":p"))
  413. if empty(absolute_path)
  414. \ || has_key(entries, absolute_path)
  415. \ || s:is_in_skiplist(absolute_path)
  416. \ || match(absolute_path, path_prefix)
  417. continue
  418. endif
  419. let entry_path = fnamemodify(absolute_path, a:path_format)
  420. let entries[absolute_path] = 1
  421. let counter -= 1
  422. let oldfiles += [[fnameescape(absolute_path), entry_path]]
  423. endfor
  424. return oldfiles
  425. endfun
  426. " Function: s:show_dir {{{1
  427. function! s:show_dir() abort
  428. return s:display_by_path(getcwd(), ':.')
  429. endfunction
  430. " Function: s:show_files {{{1
  431. function! s:show_files() abort
  432. return s:display_by_path('', s:relative_path)
  433. endfunction
  434. " Function: s:show_sessions {{{1
  435. function! s:show_sessions() abort
  436. let sfiles = split(globpath(s:session_dir, '*'), '\n')
  437. if empty(sfiles)
  438. if exists('s:last_message')
  439. unlet s:last_message
  440. endif
  441. return
  442. endif
  443. if exists('s:last_message')
  444. call s:print_section_header()
  445. endif
  446. for i in range(len(sfiles))
  447. let index = s:get_index_as_string(s:entry_number)
  448. let fname = fnamemodify(sfiles[i], ':t')
  449. call append('$', ' ['. index .']'. repeat(' ', (3 - strlen(index))) . fname)
  450. if has('win32')
  451. let fname = substitute(fname, '\[', '\[[]', 'g')
  452. endif
  453. let s:entries[line('$')] = {
  454. \ 'index': index,
  455. \ 'type': 'session',
  456. \ 'cmd': 'SLoad',
  457. \ 'path': fname,
  458. \ 'marked': 0,
  459. \ }
  460. let s:entry_number += 1
  461. endfor
  462. call append('$', '')
  463. endfunction
  464. " Function: s:show_bookmarks {{{1
  465. function! s:show_bookmarks() abort
  466. if !exists('g:startify_bookmarks') || empty(g:startify_bookmarks)
  467. return
  468. endif
  469. if exists('s:last_message')
  470. call s:print_section_header()
  471. endif
  472. for fname in g:startify_bookmarks
  473. let index = s:get_index_as_string(s:entry_number)
  474. call append('$', ' ['. index .']'. repeat(' ', (3 - strlen(index))) . fname)
  475. if has('win32')
  476. let absolute_path = substitute(fname, '\[', '\[[]', 'g')
  477. endif
  478. let s:entries[line('$')] = {
  479. \ 'index': index,
  480. \ 'type': 'file',
  481. \ 'cmd': 'edit',
  482. \ 'path': fname,
  483. \ 'marked': 0,
  484. \ }
  485. let s:entry_number += 1
  486. endfor
  487. call append('$', '')
  488. endfunction
  489. " Function: s:is_in_skiplist {{{1
  490. function! s:is_in_skiplist(arg) abort
  491. for regexp in s:skiplist
  492. if (a:arg =~# regexp)
  493. return 1
  494. endif
  495. endfor
  496. endfunction
  497. " Function: s:is_bookmark {{{1
  498. function! s:is_bookmark(arg) abort
  499. for foo in map(filter(copy(g:startify_bookmarks), '!isdirectory(v:val)'), 'resolve(fnamemodify(v:val, ":p"))')
  500. if foo == a:arg
  501. return 1
  502. endif
  503. endfor
  504. endfunction
  505. " Function: s:set_cursor {{{1
  506. function! s:set_cursor() abort
  507. let s:oldline = exists('s:newline') ? s:newline : 5
  508. let s:newline = line('.')
  509. " going up (-1) or down (1)
  510. let movement = 2 * (s:newline > s:oldline) - 1
  511. " skip section headers lines until an entry is found
  512. while index(b:startify_section_header_lines, s:newline) != -1
  513. let s:newline += movement
  514. endwhile
  515. " skip blank lines between lists
  516. if empty(getline(s:newline))
  517. let s:newline += movement
  518. endif
  519. " don't go beyond first or last entry
  520. let s:newline = max([s:firstline, min([s:lastline, s:newline])])
  521. call cursor(s:newline, 5)
  522. endfunction
  523. " Function: s:set_mappings {{{1
  524. function! s:set_mappings() abort
  525. for k in keys(s:entries)
  526. execute 'nnoremap <buffer><silent>' s:entries[k].index
  527. \ ':call startify#open_buffers('. string(k) .')<cr>'
  528. endfor
  529. nnoremap <buffer><silent> i :enew <bar> startinsert<cr>
  530. nnoremap <buffer><silent> <insert> :enew <bar> startinsert<cr>
  531. nnoremap <buffer><silent> b :call <sid>set_mark('B')<cr>
  532. nnoremap <buffer><silent> s :call <sid>set_mark('S')<cr>
  533. nnoremap <buffer><silent> t :call <sid>set_mark('T')<cr>
  534. nnoremap <buffer><silent> v :call <sid>set_mark('V')<cr>
  535. nnoremap <buffer><silent> <cr> :call startify#open_buffers()<cr>
  536. nnoremap <buffer><silent> <2-LeftMouse> :call startify#open_buffers()<cr>
  537. " Prevent 'nnoremap j gj' mappings, since they would break navigation.
  538. " (One can't leave the [x].)
  539. if !empty(maparg('j', 'n'))
  540. nnoremap <buffer> j j
  541. endif
  542. if !empty(maparg('k', 'n'))
  543. nnoremap <buffer> k k
  544. endif
  545. endfunction
  546. " Function: s:set_mark {{{1
  547. function! s:set_mark(type, ...) abort
  548. let index = expand('<cword>')
  549. let line = exists('a:1') ? a:1 : line('.')
  550. let entry = s:entries[line]
  551. if entry.type != 'file'
  552. return
  553. endif
  554. let default_cmds = {
  555. \ 'B': 'edit',
  556. \ 'S': 'split',
  557. \ 'V': 'vsplit',
  558. \ 'T': 'tabnew',
  559. \ }
  560. setlocal modifiable
  561. if entry.marked && index[0] == a:type
  562. let entry.cmd = 'edit'
  563. let entry.marked = 0
  564. execute 'normal! ci]'. entry.index
  565. else
  566. let entry.cmd = default_cmds[a:type]
  567. let entry.marked = 1
  568. let entry.tick = s:tick
  569. let s:tick += 1
  570. execute 'normal! ci]'. repeat(a:type, len(index))
  571. endif
  572. setlocal nomodifiable nomodified
  573. endfunction
  574. " Function: s:sort_by_tick {{{1
  575. function! s:sort_by_tick(one, two)
  576. return a:one.tick - a:two.tick
  577. endfunction
  578. " Function: s:check_user_options {{{1
  579. function! s:check_user_options() abort
  580. let path = expand('%')
  581. let session = path . s:sep .'Session.vim'
  582. " change to VCS root directory
  583. if get(g:, 'startify_session_autoload') && filereadable(session)
  584. execute 'source' session
  585. elseif get(g:, 'startify_change_to_vcs_root')
  586. call s:cd_to_vcs_root(path)
  587. " change directory
  588. elseif get(g:, 'startify_change_to_dir', 1)
  589. if isdirectory(path)
  590. lcd %
  591. else
  592. lcd %:h
  593. endif
  594. endif
  595. endfunction
  596. " Function: s:cd_to_vcs_root {{{1
  597. function! s:cd_to_vcs_root(path) abort
  598. let dir = fnamemodify(a:path, ':p:h')
  599. for vcs in [ '.git', '.hg', '.bzr', '.svn' ]
  600. let root = finddir(vcs, dir .';')
  601. if !empty(root)
  602. execute 'cd '. fnamemodify(root, ':h')
  603. return
  604. endif
  605. endfor
  606. endfunction
  607. " Function: s:close {{{1
  608. function! s:close() abort
  609. if len(filter(range(0, bufnr('$')), 'buflisted(v:val)'))
  610. if bufloaded(bufnr('#')) && bufnr('#') != bufnr('%')
  611. buffer #
  612. else
  613. bnext
  614. endif
  615. else
  616. quit
  617. endif
  618. endfunction
  619. " Function: s:get_index_as_string {{{1
  620. function! s:get_index_as_string(idx) abort
  621. if exists('g:startify_custom_indices')
  622. let listlen = len(g:startify_custom_indices)
  623. return (a:idx < listlen) ? g:startify_custom_indices[a:idx] : string(a:idx - listlen)
  624. else
  625. return string(a:idx)
  626. endif
  627. endfunction
  628. " Function: s:print_section_header {{{1
  629. function! s:print_section_header() abort
  630. $
  631. let curline = line('.')
  632. for lnum in range(curline, curline + len(s:last_message) + 1)
  633. call add(b:startify_section_header_lines, lnum)
  634. endfor
  635. call append('$', s:last_message + [''])
  636. unlet s:last_message
  637. endfunction