startify.vim 21 KB

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