startify.vim 21 KB

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