startify.vim 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  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. if !filereadable(spath)
  283. echomsg 'No such session: '. spath
  284. return
  285. endif
  286. echo 'Really delete '. spath .'? [y/n]' | redraw
  287. if a:bang || nr2char(getchar()) == 'y'
  288. if delete(spath) == 0
  289. echo 'Deleted session '. spath .'!'
  290. else
  291. echo 'Deletion failed!'
  292. endif
  293. else
  294. echo 'Deletion aborted!'
  295. endif
  296. endfunction
  297. " Function: #session_delete_buffers {{{1
  298. function! startify#session_delete_buffers()
  299. if !s:delete_buffers
  300. return
  301. endif
  302. let n = 1
  303. while n <= bufnr('$')
  304. if buflisted(n)
  305. try
  306. silent execute 'bdelete' n
  307. catch
  308. echohl ErrorMsg
  309. echomsg v:exception
  310. echohl NONE
  311. endtry
  312. endif
  313. let n += 1
  314. endwhile
  315. endfunction
  316. " Function: #session_list {{{1
  317. function! startify#session_list(lead, ...) abort
  318. return filter(map(split(globpath(s:session_dir, '*'.a:lead.'*'), '\n'), 'fnamemodify(v:val, ":t")'), 'v:val !=# "__LAST__"')
  319. endfunction
  320. " Function: #session_list_as_string {{{1
  321. function! startify#session_list_as_string(lead, ...) abort
  322. return join(filter(map(split(globpath(s:session_dir, '*'.a:lead.'*'), '\n'), 'fnamemodify(v:val, ":t")'), 'v:val !=# "__LAST__"'), "\n")
  323. endfunction
  324. " Function: #debug {{{1
  325. function! startify#debug()
  326. if exists('s:entries')
  327. for k in sort(keys(s:entries))
  328. echomsg '['. k .'] = '. string(s:entries[k])
  329. endfor
  330. endif
  331. endfunction
  332. " Function: #open_buffers {{{1
  333. function! startify#open_buffers(...) abort
  334. if exists('a:1') " used in mappings
  335. call s:open_buffer(s:entries[a:1])
  336. return
  337. endif
  338. let marked = filter(copy(s:entries), 'v:val.marked')
  339. if empty(marked) " open current entry
  340. call s:open_buffer(s:entries[line('.')])
  341. return
  342. endif
  343. enew
  344. setlocal nobuflisted
  345. " Open all marked entries.
  346. for entry in sort(values(marked), 's:sort_by_tick')
  347. call s:open_buffer(entry)
  348. endfor
  349. wincmd =
  350. endfunction
  351. " Function: s:open_buffer {{{1
  352. function! s:open_buffer(entry)
  353. if a:entry.type == 'special'
  354. execute a:entry.cmd
  355. elseif a:entry.type == 'session'
  356. execute a:entry.cmd a:entry.path
  357. elseif a:entry.type == 'file'
  358. if line2byte('$') == -1
  359. execute 'edit' a:entry.path
  360. else
  361. if a:entry.cmd == 'tabnew'
  362. wincmd =
  363. endif
  364. execute a:entry.cmd a:entry.path
  365. endif
  366. call s:check_user_options()
  367. endif
  368. endfunction
  369. " Function: s:display_by_path {{{1
  370. function! s:display_by_path(path_prefix, path_format) abort
  371. let oldfiles = call(get(g:, 'startify_enable_unsafe') ? 's:filter_oldfiles_unsafe' : 's:filter_oldfiles',
  372. \ [a:path_prefix, a:path_format])
  373. let entry_format = "' ['. index .']'. repeat(' ', (3 - strlen(index)))"
  374. if exists('*WebDevIconsGetFileTypeSymbol') " support for vim-devicons
  375. let entry_format .= ". '('. WebDevIconsGetFileTypeSymbol(entry_path) .') '. entry_path"
  376. else
  377. let entry_format .= '. entry_path'
  378. endif
  379. if !empty(oldfiles)
  380. if exists('s:last_message')
  381. call s:print_section_header()
  382. endif
  383. for [absolute_path, entry_path] in oldfiles
  384. let index = s:get_index_as_string(s:entry_number)
  385. call append('$', eval(entry_format))
  386. if has('win32')
  387. let absolute_path = substitute(absolute_path, '\[', '\[[]', 'g')
  388. endif
  389. call s:register(line('$'), index, 'file', 'edit', absolute_path, s:nowait)
  390. let s:entry_number += 1
  391. endfor
  392. call append('$', '')
  393. endif
  394. endfunction
  395. " Function: s:filter_oldfiles {{{1
  396. function! s:filter_oldfiles(path_prefix, path_format) abort
  397. let path_prefix = '\V'. escape(a:path_prefix, '\')
  398. let counter = s:numfiles
  399. let entries = {}
  400. let oldfiles = []
  401. for fname in v:oldfiles
  402. if counter <= 0
  403. break
  404. endif
  405. let absolute_path = fnamemodify(resolve(fname), ":p")
  406. " filter duplicates, bookmarks and entries from the skiplist
  407. if has_key(entries, absolute_path)
  408. \ || !filereadable(absolute_path)
  409. \ || s:is_in_skiplist(absolute_path)
  410. \ || match(absolute_path, path_prefix)
  411. continue
  412. endif
  413. let entry_path = fnamemodify(absolute_path, a:path_format)
  414. let entries[absolute_path] = 1
  415. let counter -= 1
  416. let oldfiles += [[fnameescape(absolute_path), entry_path]]
  417. endfor
  418. return oldfiles
  419. endfun
  420. " Function: s:filter_oldfiles_unsafe {{{1
  421. function! s:filter_oldfiles_unsafe(path_prefix, path_format) abort
  422. let path_prefix = '\V'. escape(a:path_prefix, '\')
  423. let counter = s:numfiles
  424. let entries = {}
  425. let oldfiles = []
  426. for fname in v:oldfiles
  427. if counter <= 0
  428. break
  429. endif
  430. let absolute_path = glob(fnamemodify(fname, ":p"))
  431. if empty(absolute_path)
  432. \ || has_key(entries, absolute_path)
  433. \ || s:is_in_skiplist(absolute_path)
  434. \ || match(absolute_path, path_prefix)
  435. continue
  436. endif
  437. let entry_path = fnamemodify(absolute_path, a:path_format)
  438. let entries[absolute_path] = 1
  439. let counter -= 1
  440. let oldfiles += [[fnameescape(absolute_path), entry_path]]
  441. endfor
  442. return oldfiles
  443. endfun
  444. " Function: s:show_dir {{{1
  445. function! s:show_dir() abort
  446. return s:display_by_path(getcwd(), ':.')
  447. endfunction
  448. " Function: s:show_files {{{1
  449. function! s:show_files() abort
  450. return s:display_by_path('', s:relative_path)
  451. endfunction
  452. " Function: s:show_sessions {{{1
  453. function! s:show_sessions() abort
  454. let sfiles = filter(split(globpath(s:session_dir, '*'), '\n'),
  455. \ 'v:val !~# "x\.vim$" && v:val !~# "__LAST__$"')
  456. if empty(sfiles)
  457. if exists('s:last_message')
  458. unlet s:last_message
  459. endif
  460. return
  461. endif
  462. if exists('s:last_message')
  463. call s:print_section_header()
  464. endif
  465. for i in range(len(sfiles))
  466. let index = s:get_index_as_string(s:entry_number)
  467. let fname = fnamemodify(sfiles[i], ':t')
  468. call append('$', ' ['. index .']'. repeat(' ', (3 - strlen(index))) . fname)
  469. if has('win32')
  470. let fname = substitute(fname, '\[', '\[[]', 'g')
  471. endif
  472. call s:register(line('$'), index, 'session', 'SLoad', fname, s:nowait)
  473. let s:entry_number += 1
  474. endfor
  475. call append('$', '')
  476. endfunction
  477. " Function: s:show_bookmarks {{{1
  478. function! s:show_bookmarks() abort
  479. if !exists('g:startify_bookmarks') || empty(g:startify_bookmarks)
  480. return
  481. endif
  482. if exists('s:last_message')
  483. call s:print_section_header()
  484. endif
  485. for bookmark in g:startify_bookmarks
  486. if type(bookmark) == 4 " dict?
  487. let [index, fname] = items(bookmark)[0]
  488. else " string
  489. let [index, fname] = [s:get_index_as_string(s:entry_number), bookmark]
  490. let s:entry_number += 1
  491. endif
  492. call append('$', ' ['. index .']'. repeat(' ', (3 - strlen(index))) . fname)
  493. if has('win32')
  494. let fname = substitute(fname, '\[', '\[[]', 'g')
  495. endif
  496. call s:register(line('$'), index, 'file', 'edit', fname, s:nowait)
  497. unlet bookmark " avoid type mismatch for heterogeneous lists
  498. endfor
  499. call append('$', '')
  500. endfunction
  501. " Function: s:is_in_skiplist {{{1
  502. function! s:is_in_skiplist(arg) abort
  503. for regexp in s:skiplist
  504. try
  505. if a:arg =~# regexp
  506. return 1
  507. endif
  508. catch
  509. echohl WarningMsg
  510. echomsg 'startify: Pattern '. string(regexp) .' threw an exception. Read :help g:startify_skiplist'
  511. echohl NONE
  512. endtry
  513. endfor
  514. endfunction
  515. " Function: s:set_cursor {{{1
  516. function! s:set_cursor() abort
  517. let s:oldline = exists('s:newline') ? s:newline : 5
  518. let s:newline = line('.')
  519. " going up (-1) or down (1)
  520. let movement = 2 * (s:newline > s:oldline) - 1
  521. " skip section headers lines until an entry is found
  522. while index(b:startify_section_header_lines, s:newline) != -1
  523. let s:newline += movement
  524. endwhile
  525. " skip blank lines between lists
  526. if empty(getline(s:newline))
  527. let s:newline += movement
  528. endif
  529. " don't go beyond first or last entry
  530. let s:newline = max([s:firstline, min([s:lastline, s:newline])])
  531. call cursor(s:newline, 5)
  532. endfunction
  533. " Function: s:set_mappings {{{1
  534. function! s:set_mappings() abort
  535. execute "nnoremap <buffer>". s:nowait_string ."<silent> i :enew <bar> startinsert<cr>"
  536. execute "nnoremap <buffer>". s:nowait_string ."<silent> <insert> :enew <bar> startinsert<cr>"
  537. execute "nnoremap <buffer>". s:nowait_string ."<silent> b :call <sid>set_mark('B')<cr>"
  538. execute "nnoremap <buffer>". s:nowait_string ."<silent> s :call <sid>set_mark('S')<cr>"
  539. execute "nnoremap <buffer>". s:nowait_string ."<silent> t :call <sid>set_mark('T')<cr>"
  540. execute "nnoremap <buffer>". s:nowait_string ."<silent> v :call <sid>set_mark('V')<cr>"
  541. execute "nnoremap <buffer>". s:nowait_string ."<silent> <cr> :call startify#open_buffers()<cr>"
  542. execute "nnoremap <buffer>". s:nowait_string ."<silent> <2-LeftMouse> :call startify#open_buffers()<cr>"
  543. for k in keys(s:entries)
  544. execute 'nnoremap <buffer><silent>'. s:entries[k].nowait s:entries[k].index
  545. \ ':call startify#open_buffers('. string(k) .')<cr>'
  546. endfor
  547. " Prevent 'nnoremap j gj' mappings, since they would break navigation.
  548. " (One can't leave the [x].)
  549. if !empty(maparg('j', 'n'))
  550. nnoremap <buffer> j j
  551. endif
  552. if !empty(maparg('k', 'n'))
  553. nnoremap <buffer> k k
  554. endif
  555. endfunction
  556. " Function: s:set_mark {{{1
  557. function! s:set_mark(type, ...) abort
  558. let index = expand('<cword>')
  559. let line = exists('a:1') ? a:1 : line('.')
  560. let entry = s:entries[line]
  561. if entry.type != 'file'
  562. return
  563. endif
  564. let default_cmds = {
  565. \ 'B': 'edit',
  566. \ 'S': 'split',
  567. \ 'V': 'vsplit',
  568. \ 'T': 'tabnew',
  569. \ }
  570. setlocal modifiable
  571. if entry.marked && index[0] == a:type
  572. let entry.cmd = 'edit'
  573. let entry.marked = 0
  574. execute 'normal! ci]'. entry.index
  575. else
  576. let entry.cmd = default_cmds[a:type]
  577. let entry.marked = 1
  578. let entry.tick = s:tick
  579. let s:tick += 1
  580. execute 'normal! ci]'. repeat(a:type, len(index))
  581. endif
  582. setlocal nomodifiable nomodified
  583. endfunction
  584. " Function: s:sort_by_tick {{{1
  585. function! s:sort_by_tick(one, two)
  586. return a:one.tick - a:two.tick
  587. endfunction
  588. " Function: s:check_user_options {{{1
  589. function! s:check_user_options() abort
  590. let path = expand('%')
  591. let session = path . s:sep .'Session.vim'
  592. " change to VCS root directory
  593. if get(g:, 'startify_session_autoload') && filereadable(session)
  594. execute 'source' session
  595. elseif get(g:, 'startify_change_to_vcs_root')
  596. call s:cd_to_vcs_root(path)
  597. " change directory
  598. elseif get(g:, 'startify_change_to_dir', 1)
  599. if isdirectory(path)
  600. lcd %
  601. else
  602. lcd %:p:h
  603. endif
  604. endif
  605. endfunction
  606. " Function: s:cd_to_vcs_root {{{1
  607. function! s:cd_to_vcs_root(path) abort
  608. let dir = fnamemodify(a:path, ':p:h')
  609. for vcs in [ '.git', '.hg', '.bzr', '.svn' ]
  610. let root = finddir(vcs, dir .';')
  611. if !empty(root)
  612. execute 'cd '. fnamemodify(root, ':h')
  613. return
  614. endif
  615. endfor
  616. endfunction
  617. " Function: s:close {{{1
  618. function! s:close() abort
  619. if len(filter(range(0, bufnr('$')), 'buflisted(v:val)'))
  620. if bufloaded(bufnr('#')) && bufnr('#') != bufnr('%')
  621. buffer #
  622. else
  623. bnext
  624. endif
  625. else
  626. quit
  627. endif
  628. endfunction
  629. " Function: s:get_index_as_string {{{1
  630. function! s:get_index_as_string(idx) abort
  631. if exists('g:startify_custom_indices')
  632. let listlen = len(g:startify_custom_indices)
  633. return (a:idx < listlen) ? g:startify_custom_indices[a:idx] : string(a:idx - listlen)
  634. else
  635. return string(a:idx)
  636. endif
  637. endfunction
  638. " Function: s:print_section_header {{{1
  639. function! s:print_section_header() abort
  640. $
  641. let curline = line('.')
  642. for lnum in range(curline, curline + len(s:last_message) + 1)
  643. call add(b:startify_section_header_lines, lnum)
  644. endfor
  645. call append('$', s:last_message + [''])
  646. unlet s:last_message
  647. endfunction
  648. " Function: s:register {{{1
  649. function! s:register(line, index, type, cmd, path, wait)
  650. let s:entries[a:line] = {
  651. \ 'index': a:index,
  652. \ 'type': a:type,
  653. \ 'cmd': a:cmd,
  654. \ 'path': a:path,
  655. \ 'nowait': a:wait,
  656. \ 'marked': 0,
  657. \ }
  658. endfunction
  659. " Function: s:create_last_session_link {{{1
  660. function! s:create_last_session_link(spath)
  661. if !has('win32') && a:spath !~# '__LAST__$'
  662. let cmd = printf('ln -sf %s %s',
  663. \ shellescape(fnamemodify(a:spath, ':t')),
  664. \ shellescape(s:session_dir .'/__LAST__'))
  665. silent! call system(cmd)
  666. if v:shell_error
  667. echomsg "startify: Can't create 'last used session' symlink."
  668. endif
  669. endif
  670. endfunction