startify.vim 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  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. \ [' Most recently used files:'],
  85. \ 'files',
  86. \ [' Most recently used files in the current directory:'],
  87. \ 'dir',
  88. \ [' My sessions:'],
  89. \ 'sessions',
  90. \ [' My bookmarks:'],
  91. \ 'bookmarks',
  92. \ ])
  93. for item in s:lists
  94. if type(item) == 1
  95. call s:show_{item}()
  96. else
  97. let s:last_message = item
  98. endif
  99. unlet item
  100. endfor
  101. silent $delete _
  102. if s:show_special
  103. call append('$', ['', ' [q] <quit>'])
  104. call s:register(line('$'), 'q', 'special', 'call s:close()', '', s:nowait_string)
  105. else
  106. " Don't overwrite the last regular entry, thus +1
  107. call s:register(line('$')+1, 'q', 'special', 'call s:close()', '', s:nowait_string)
  108. endif
  109. " compute first line offset
  110. let s:firstline = 2
  111. " increase offset if there is a custom header
  112. if exists('g:startify_custom_header')
  113. let s:firstline += len(g:startify_custom_header)
  114. endif
  115. " no special, no local Session.vim, but a section header
  116. if !s:show_special && !exists('l:show_session') && type(s:lists[0]) == 3
  117. let s:firstline += len(s:lists[0]) + 1
  118. endif
  119. let s:lastline = line('$')
  120. if exists('g:startify_custom_footer')
  121. call append('$', g:startify_custom_footer)
  122. endif
  123. setlocal nomodifiable nomodified
  124. call s:set_mappings()
  125. call cursor(s:firstline + (s:show_special ? 2 : 0), 5)
  126. autocmd startify CursorMoved <buffer> call s:set_cursor()
  127. set filetype=startify
  128. silent! doautocmd <nomodeline> User Startified
  129. endfunction
  130. " Function: #session_load {{{1
  131. function! startify#session_load(...) abort
  132. if !isdirectory(s:session_dir)
  133. echomsg 'The session directory does not exist: '. s:session_dir
  134. return
  135. elseif empty(startify#session_list_as_string(''))
  136. echomsg 'There are no sessions...'
  137. return
  138. endif
  139. let spath = s:session_dir . s:sep
  140. if a:0
  141. let spath .= a:1
  142. else
  143. if has('win32')
  144. call inputsave()
  145. let spath .= input(
  146. \ 'Load this session: ',
  147. \ fnamemodify(v:this_session, ':t'),
  148. \ 'custom,startify#session_list_as_string') | redraw
  149. call inputrestore()
  150. else
  151. let spath .= '__LAST__'
  152. endif
  153. endif
  154. if filereadable(spath)
  155. if get(g:, 'startify_session_persistence') && filewritable(v:this_session)
  156. call startify#session_write(fnameescape(v:this_session))
  157. endif
  158. call startify#session_delete_buffers()
  159. execute 'source '. fnameescape(spath)
  160. call s:create_last_session_link(spath)
  161. else
  162. echo 'No such file: '. spath
  163. endif
  164. endfunction
  165. " Function: #session_save {{{1
  166. function! startify#session_save(...) abort
  167. if !isdirectory(s:session_dir)
  168. if exists('*mkdir')
  169. echo 'The session directory does not exist: '. s:session_dir .'. Create it? [y/n]'
  170. if (nr2char(getchar()) == 'y')
  171. call mkdir(s:session_dir, 'p')
  172. else
  173. echo
  174. return
  175. endif
  176. else
  177. echo 'The session directory does not exist: '. s:session_dir
  178. return
  179. endif
  180. endif
  181. call inputsave()
  182. let sname = exists('a:1')
  183. \ ? a:1
  184. \ : input('Save under this session name: ', fnamemodify(v:this_session, ':t'), '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(...) 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 (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() abort
  295. if !s:delete_buffers
  296. return
  297. endif
  298. let n = 1
  299. while n <= bufnr('$')
  300. if buflisted(n)
  301. silent execute 'bdelete' n
  302. endif
  303. let n += 1
  304. endwhile
  305. endfunction
  306. " Function: #session_list {{{1
  307. function! startify#session_list(lead, ...) abort
  308. return filter(map(split(globpath(s:session_dir, '*'.a:lead.'*'), '\n'), 'fnamemodify(v:val, ":t")'), 'v:val !=# "__LAST__"')
  309. endfunction
  310. " Function: #session_list_as_string {{{1
  311. function! startify#session_list_as_string(lead, ...) abort
  312. return join(filter(map(split(globpath(s:session_dir, '*'.a:lead.'*'), '\n'), 'fnamemodify(v:val, ":t")'), 'v:val !=# "__LAST__"'), "\n")
  313. endfunction
  314. " Function: #debug {{{1
  315. function! startify#debug()
  316. if exists('s:entries')
  317. for k in sort(keys(s:entries))
  318. echomsg '['. k .'] = '. string(s:entries[k])
  319. endfor
  320. endif
  321. endfunction
  322. " Function: #open_buffers {{{1
  323. function! startify#open_buffers(...) abort
  324. if exists('a:1') " used in mappings
  325. call s:open_buffer(s:entries[a:1])
  326. return
  327. endif
  328. let marked = filter(copy(s:entries), 'v:val.marked')
  329. if empty(marked) " open current entry
  330. call s:open_buffer(s:entries[line('.')])
  331. return
  332. endif
  333. enew
  334. setlocal nobuflisted
  335. " Open all marked entries.
  336. for entry in sort(values(marked), 's:sort_by_tick')
  337. call s:open_buffer(entry)
  338. endfor
  339. wincmd =
  340. endfunction
  341. " Function: s:open_buffer {{{1
  342. function! s:open_buffer(entry)
  343. if a:entry.type == 'special'
  344. execute a:entry.cmd
  345. elseif a:entry.type == 'session'
  346. execute a:entry.cmd a:entry.path
  347. elseif a:entry.type == 'file'
  348. if line2byte('$') == -1
  349. execute 'edit' a:entry.path
  350. else
  351. if a:entry.cmd == 'tabnew'
  352. wincmd =
  353. endif
  354. execute a:entry.cmd a:entry.path
  355. endif
  356. call s:check_user_options()
  357. endif
  358. endfunction
  359. " Function: s:display_by_path {{{1
  360. function! s:display_by_path(path_prefix, path_format) abort
  361. let oldfiles = call(get(g:, 'startify_enable_unsafe') ? 's:filter_oldfiles_unsafe' : 's:filter_oldfiles',
  362. \ [a:path_prefix, a:path_format])
  363. let entry_format = "' ['. index .']'. repeat(' ', (3 - strlen(index)))"
  364. if exists('*WebDevIconsGetFileTypeSymbol') " support for vim-devicons
  365. let entry_format .= ". '('. WebDevIconsGetFileTypeSymbol(entry_path) .') '. entry_path"
  366. else
  367. let entry_format .= '. entry_path'
  368. endif
  369. if !empty(oldfiles)
  370. if exists('s:last_message')
  371. call s:print_section_header()
  372. endif
  373. for [absolute_path, entry_path] in oldfiles
  374. let index = s:get_index_as_string(s:entry_number)
  375. call append('$', eval(entry_format))
  376. if has('win32')
  377. let absolute_path = substitute(absolute_path, '\[', '\[[]', 'g')
  378. endif
  379. call s:register(line('$'), index, 'file', 'edit', absolute_path, s:nowait)
  380. let s:entry_number += 1
  381. endfor
  382. call append('$', '')
  383. endif
  384. endfunction
  385. " Function: s:filter_oldfiles {{{1
  386. function! s:filter_oldfiles(path_prefix, path_format) abort
  387. let path_prefix = '\V'. escape(a:path_prefix, '\')
  388. let counter = s:numfiles
  389. let entries = {}
  390. let oldfiles = []
  391. for fname in v:oldfiles
  392. if counter <= 0
  393. break
  394. endif
  395. let absolute_path = fnamemodify(resolve(fname), ":p")
  396. " filter duplicates, bookmarks and entries from the skiplist
  397. if has_key(entries, absolute_path)
  398. \ || !filereadable(absolute_path)
  399. \ || s:is_in_skiplist(absolute_path)
  400. \ || match(absolute_path, path_prefix)
  401. continue
  402. endif
  403. let entry_path = fnamemodify(absolute_path, a:path_format)
  404. let entries[absolute_path] = 1
  405. let counter -= 1
  406. let oldfiles += [[fnameescape(absolute_path), entry_path]]
  407. endfor
  408. return oldfiles
  409. endfun
  410. " Function: s:filter_oldfiles_unsafe {{{1
  411. function! s:filter_oldfiles_unsafe(path_prefix, path_format) abort
  412. let path_prefix = '\V'. escape(a:path_prefix, '\')
  413. let counter = s:numfiles
  414. let entries = {}
  415. let oldfiles = []
  416. for fname in v:oldfiles
  417. if counter <= 0
  418. break
  419. endif
  420. let absolute_path = glob(fnamemodify(fname, ":p"))
  421. if empty(absolute_path)
  422. \ || has_key(entries, absolute_path)
  423. \ || s:is_in_skiplist(absolute_path)
  424. \ || match(absolute_path, path_prefix)
  425. continue
  426. endif
  427. let entry_path = fnamemodify(absolute_path, a:path_format)
  428. let entries[absolute_path] = 1
  429. let counter -= 1
  430. let oldfiles += [[fnameescape(absolute_path), entry_path]]
  431. endfor
  432. return oldfiles
  433. endfun
  434. " Function: s:show_dir {{{1
  435. function! s:show_dir() abort
  436. return s:display_by_path(getcwd(), ':.')
  437. endfunction
  438. " Function: s:show_files {{{1
  439. function! s:show_files() abort
  440. return s:display_by_path('', s:relative_path)
  441. endfunction
  442. " Function: s:show_sessions {{{1
  443. function! s:show_sessions() abort
  444. let sfiles = filter(split(globpath(s:session_dir, '*'), '\n'),
  445. \ 'v:val !~# "x\.vim$" && v:val !~# "__LAST__$"')
  446. if empty(sfiles)
  447. if exists('s:last_message')
  448. unlet s:last_message
  449. endif
  450. return
  451. endif
  452. if exists('s:last_message')
  453. call s:print_section_header()
  454. endif
  455. for i in range(len(sfiles))
  456. let index = s:get_index_as_string(s:entry_number)
  457. let fname = fnamemodify(sfiles[i], ':t')
  458. call append('$', ' ['. index .']'. repeat(' ', (3 - strlen(index))) . fname)
  459. if has('win32')
  460. let fname = substitute(fname, '\[', '\[[]', 'g')
  461. endif
  462. call s:register(line('$'), index, 'session', 'SLoad', fname, s:nowait)
  463. let s:entry_number += 1
  464. endfor
  465. call append('$', '')
  466. endfunction
  467. " Function: s:show_bookmarks {{{1
  468. function! s:show_bookmarks() abort
  469. if !exists('g:startify_bookmarks') || empty(g:startify_bookmarks)
  470. return
  471. endif
  472. if exists('s:last_message')
  473. call s:print_section_header()
  474. endif
  475. for bookmark in g:startify_bookmarks
  476. if type(bookmark) == 4 " dict?
  477. let [index, fname] = items(bookmark)[0]
  478. else " string
  479. let [index, fname] = [s:get_index_as_string(s:entry_number), bookmark]
  480. let s:entry_number += 1
  481. endif
  482. call append('$', ' ['. index .']'. repeat(' ', (3 - strlen(index))) . fname)
  483. if has('win32')
  484. let fname = substitute(fname, '\[', '\[[]', 'g')
  485. endif
  486. call s:register(line('$'), index, 'file', 'edit', fname, s:nowait)
  487. unlet bookmark " avoid type mismatch for heterogeneous lists
  488. endfor
  489. call append('$', '')
  490. endfunction
  491. " Function: s:is_in_skiplist {{{1
  492. function! s:is_in_skiplist(arg) abort
  493. for regexp in s:skiplist
  494. try
  495. if a:arg =~# regexp
  496. return 1
  497. endif
  498. catch
  499. echohl WarningMsg
  500. echomsg 'startify: Pattern '. string(regexp) .' threw an exception. Read :help g:startify_skiplist'
  501. echohl NONE
  502. endtry
  503. endfor
  504. endfunction
  505. " Function: s:set_cursor {{{1
  506. function! s:set_cursor() abort
  507. let s:oldline = exists('s:newline') ? s:newline : 5
  508. let s:newline = line('.')
  509. " going up (-1) or down (1)
  510. let movement = 2 * (s:newline > s:oldline) - 1
  511. " skip section headers lines until an entry is found
  512. while index(b:startify_section_header_lines, s:newline) != -1
  513. let s:newline += movement
  514. endwhile
  515. " skip blank lines between lists
  516. if empty(getline(s:newline))
  517. let s:newline += movement
  518. endif
  519. " don't go beyond first or last entry
  520. let s:newline = max([s:firstline, min([s:lastline, s:newline])])
  521. call cursor(s:newline, 5)
  522. endfunction
  523. " Function: s:set_mappings {{{1
  524. function! s:set_mappings() abort
  525. execute "nnoremap <buffer>". s:nowait_string ."<silent> i :enew <bar> startinsert<cr>"
  526. execute "nnoremap <buffer>". s:nowait_string ."<silent> <insert> :enew <bar> startinsert<cr>"
  527. execute "nnoremap <buffer>". s:nowait_string ."<silent> b :call <sid>set_mark('B')<cr>"
  528. execute "nnoremap <buffer>". s:nowait_string ."<silent> s :call <sid>set_mark('S')<cr>"
  529. execute "nnoremap <buffer>". s:nowait_string ."<silent> t :call <sid>set_mark('T')<cr>"
  530. execute "nnoremap <buffer>". s:nowait_string ."<silent> v :call <sid>set_mark('V')<cr>"
  531. execute "nnoremap <buffer>". s:nowait_string ."<silent> <cr> :call startify#open_buffers()<cr>"
  532. execute "nnoremap <buffer>". s:nowait_string ."<silent> <2-LeftMouse> :call startify#open_buffers()<cr>"
  533. for k in keys(s:entries)
  534. execute 'nnoremap <buffer><silent>'. s:entries[k].nowait s:entries[k].index
  535. \ ':call startify#open_buffers('. string(k) .')<cr>'
  536. endfor
  537. " Prevent 'nnoremap j gj' mappings, since they would break navigation.
  538. " (One can't leave the [x].)
  539. if !empty(maparg('j', 'n'))
  540. nnoremap <buffer> j j
  541. endif
  542. if !empty(maparg('k', 'n'))
  543. nnoremap <buffer> k k
  544. endif
  545. endfunction
  546. " Function: s:set_mark {{{1
  547. function! s:set_mark(type, ...) abort
  548. let index = expand('<cword>')
  549. let line = exists('a:1') ? a:1 : line('.')
  550. let entry = s:entries[line]
  551. if entry.type != 'file'
  552. return
  553. endif
  554. let default_cmds = {
  555. \ 'B': 'edit',
  556. \ 'S': 'split',
  557. \ 'V': 'vsplit',
  558. \ 'T': 'tabnew',
  559. \ }
  560. setlocal modifiable
  561. if entry.marked && index[0] == a:type
  562. let entry.cmd = 'edit'
  563. let entry.marked = 0
  564. execute 'normal! ci]'. entry.index
  565. else
  566. let entry.cmd = default_cmds[a:type]
  567. let entry.marked = 1
  568. let entry.tick = s:tick
  569. let s:tick += 1
  570. execute 'normal! ci]'. repeat(a:type, len(index))
  571. endif
  572. setlocal nomodifiable nomodified
  573. endfunction
  574. " Function: s:sort_by_tick {{{1
  575. function! s:sort_by_tick(one, two)
  576. return a:one.tick - a:two.tick
  577. endfunction
  578. " Function: s:check_user_options {{{1
  579. function! s:check_user_options() abort
  580. let path = expand('%')
  581. let session = path . s:sep .'Session.vim'
  582. " change to VCS root directory
  583. if get(g:, 'startify_session_autoload') && filereadable(session)
  584. execute 'source' session
  585. elseif get(g:, 'startify_change_to_vcs_root')
  586. call s:cd_to_vcs_root(path)
  587. " change directory
  588. elseif get(g:, 'startify_change_to_dir', 1)
  589. if isdirectory(path)
  590. lcd %
  591. else
  592. lcd %:p:h
  593. endif
  594. endif
  595. endfunction
  596. " Function: s:cd_to_vcs_root {{{1
  597. function! s:cd_to_vcs_root(path) abort
  598. let dir = fnamemodify(a:path, ':p:h')
  599. for vcs in [ '.git', '.hg', '.bzr', '.svn' ]
  600. let root = finddir(vcs, dir .';')
  601. if !empty(root)
  602. execute 'cd '. fnamemodify(root, ':h')
  603. return
  604. endif
  605. endfor
  606. endfunction
  607. " Function: s:close {{{1
  608. function! s:close() abort
  609. if len(filter(range(0, bufnr('$')), 'buflisted(v:val)'))
  610. if bufloaded(bufnr('#')) && bufnr('#') != bufnr('%')
  611. buffer #
  612. else
  613. bnext
  614. endif
  615. else
  616. quit
  617. endif
  618. endfunction
  619. " Function: s:get_index_as_string {{{1
  620. function! s:get_index_as_string(idx) abort
  621. if exists('g:startify_custom_indices')
  622. let listlen = len(g:startify_custom_indices)
  623. return (a:idx < listlen) ? g:startify_custom_indices[a:idx] : string(a:idx - listlen)
  624. else
  625. return string(a:idx)
  626. endif
  627. endfunction
  628. " Function: s:print_section_header {{{1
  629. function! s:print_section_header() abort
  630. $
  631. let curline = line('.')
  632. for lnum in range(curline, curline + len(s:last_message) + 1)
  633. call add(b:startify_section_header_lines, lnum)
  634. endfor
  635. call append('$', s:last_message + [''])
  636. unlet s:last_message
  637. endfunction
  638. " Function: s:register {{{1
  639. function! s:register(line, index, type, cmd, path, wait)
  640. let s:entries[a:line] = {
  641. \ 'index': a:index,
  642. \ 'type': a:type,
  643. \ 'cmd': a:cmd,
  644. \ 'path': a:path,
  645. \ 'nowait': a:wait,
  646. \ 'marked': 0,
  647. \ }
  648. endfunction
  649. " Function: s:create_last_session_link {{{1
  650. function! s:create_last_session_link(spath)
  651. if !has('win32') && a:spath !~# '__LAST__$'
  652. let cmd = printf('ln -sf %s %s',
  653. \ shellescape(a:spath),
  654. \ shellescape(fnamemodify(a:spath, ':h') .'/__LAST__'))
  655. silent! call system(cmd)
  656. if v:shell_error
  657. echomsg "startify: Can't create 'last used session' symlink."
  658. endif
  659. endif
  660. endfunction