startify.vim 21 KB

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