startify.vim 21 KB

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