startify.vim 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828
  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. silent 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, use_env) abort
  371. let oldfiles = call(get(g:, 'startify_enable_unsafe') ? 's:filter_oldfiles_unsafe' : 's:filter_oldfiles',
  372. \ [a:path_prefix, a:path_format, a:use_env])
  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, use_env) 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. if a:use_env
  419. call s:init_env()
  420. for i in range(len(oldfiles))
  421. for [k,v] in s:env
  422. let p = oldfiles[i][1]
  423. if !stridx(tolower(p), tolower(v))
  424. let oldfiles[i][1] = printf('$%s%s', k, p[len(v):])
  425. break
  426. endif
  427. endfor
  428. endfor
  429. endif
  430. return oldfiles
  431. endfun
  432. " Function: s:filter_oldfiles_unsafe {{{1
  433. function! s:filter_oldfiles_unsafe(path_prefix, path_format, use_env) abort
  434. let path_prefix = '\V'. escape(a:path_prefix, '\')
  435. let counter = s:numfiles
  436. let entries = {}
  437. let oldfiles = []
  438. for fname in v:oldfiles
  439. if counter <= 0
  440. break
  441. endif
  442. let absolute_path = glob(fnamemodify(fname, ":p"))
  443. if empty(absolute_path)
  444. \ || has_key(entries, absolute_path)
  445. \ || s:is_in_skiplist(absolute_path)
  446. \ || match(absolute_path, path_prefix)
  447. continue
  448. endif
  449. let entry_path = fnamemodify(absolute_path, a:path_format)
  450. let entries[absolute_path] = 1
  451. let counter -= 1
  452. let oldfiles += [[fnameescape(absolute_path), entry_path]]
  453. endfor
  454. return oldfiles
  455. endfun
  456. " Function: s:show_dir {{{1
  457. function! s:show_dir() abort
  458. return s:display_by_path(getcwd(), ':.', 0)
  459. endfunction
  460. " Function: s:show_files {{{1
  461. function! s:show_files() abort
  462. return s:display_by_path('', s:relative_path, get(g:, 'startify_use_env'))
  463. endfunction
  464. " Function: s:show_sessions {{{1
  465. function! s:show_sessions() abort
  466. let sfiles = filter(split(globpath(s:session_dir, '*'), '\n'),
  467. \ 'v:val !~# "x\.vim$" && v:val !~# "__LAST__$"')
  468. if empty(sfiles)
  469. if exists('s:last_message')
  470. unlet s:last_message
  471. endif
  472. return
  473. endif
  474. if exists('s:last_message')
  475. call s:print_section_header()
  476. endif
  477. for i in range(len(sfiles))
  478. let index = s:get_index_as_string(s:entry_number)
  479. let fname = fnamemodify(sfiles[i], ':t')
  480. call append('$', ' ['. index .']'. repeat(' ', (3 - strlen(index))) . fname)
  481. if has('win32')
  482. let fname = substitute(fname, '\[', '\[[]', 'g')
  483. endif
  484. call s:register(line('$'), index, 'session', 'SLoad', fname, s:nowait)
  485. let s:entry_number += 1
  486. endfor
  487. call append('$', '')
  488. endfunction
  489. " Function: s:show_bookmarks {{{1
  490. function! s:show_bookmarks() abort
  491. if !exists('g:startify_bookmarks') || empty(g:startify_bookmarks)
  492. return
  493. endif
  494. if exists('s:last_message')
  495. call s:print_section_header()
  496. endif
  497. for bookmark in g:startify_bookmarks
  498. if type(bookmark) == 4 " dict?
  499. let [index, fname] = items(bookmark)[0]
  500. else " string
  501. let [index, fname] = [s:get_index_as_string(s:entry_number), bookmark]
  502. let s:entry_number += 1
  503. endif
  504. call append('$', ' ['. index .']'. repeat(' ', (3 - strlen(index))) . fname)
  505. if has('win32')
  506. let fname = substitute(fname, '\[', '\[[]', 'g')
  507. endif
  508. call s:register(line('$'), index, 'file', 'edit', fname, s:nowait)
  509. unlet bookmark " avoid type mismatch for heterogeneous lists
  510. endfor
  511. call append('$', '')
  512. endfunction
  513. " Function: s:is_in_skiplist {{{1
  514. function! s:is_in_skiplist(arg) abort
  515. for regexp in s:skiplist
  516. try
  517. if a:arg =~# regexp
  518. return 1
  519. endif
  520. catch
  521. echohl WarningMsg
  522. echomsg 'startify: Pattern '. string(regexp) .' threw an exception. Read :help g:startify_skiplist'
  523. echohl NONE
  524. endtry
  525. endfor
  526. endfunction
  527. " Function: s:set_cursor {{{1
  528. function! s:set_cursor() abort
  529. let s:oldline = exists('s:newline') ? s:newline : 5
  530. let s:newline = line('.')
  531. " going up (-1) or down (1)
  532. let movement = 2 * (s:newline > s:oldline) - 1
  533. " skip section headers lines until an entry is found
  534. while index(b:startify_section_header_lines, s:newline) != -1
  535. let s:newline += movement
  536. endwhile
  537. " skip blank lines between lists
  538. if empty(getline(s:newline))
  539. let s:newline += movement
  540. endif
  541. " don't go beyond first or last entry
  542. let s:newline = max([s:firstline, min([s:lastline, s:newline])])
  543. call cursor(s:newline, 5)
  544. endfunction
  545. " Function: s:set_mappings {{{1
  546. function! s:set_mappings() abort
  547. execute "nnoremap <buffer>". s:nowait_string ."<silent> i :enew <bar> startinsert<cr>"
  548. execute "nnoremap <buffer>". s:nowait_string ."<silent> <insert> :enew <bar> startinsert<cr>"
  549. execute "nnoremap <buffer>". s:nowait_string ."<silent> b :call <sid>set_mark('B')<cr>"
  550. execute "nnoremap <buffer>". s:nowait_string ."<silent> s :call <sid>set_mark('S')<cr>"
  551. execute "nnoremap <buffer>". s:nowait_string ."<silent> t :call <sid>set_mark('T')<cr>"
  552. execute "nnoremap <buffer>". s:nowait_string ."<silent> v :call <sid>set_mark('V')<cr>"
  553. execute "nnoremap <buffer>". s:nowait_string ."<silent> <cr> :call startify#open_buffers()<cr>"
  554. execute "nnoremap <buffer>". s:nowait_string ."<silent> <2-LeftMouse> :call startify#open_buffers()<cr>"
  555. for k in keys(s:entries)
  556. execute 'nnoremap <buffer><silent>'. s:entries[k].nowait s:entries[k].index
  557. \ ':call startify#open_buffers('. string(k) .')<cr>'
  558. endfor
  559. " Prevent 'nnoremap j gj' mappings, since they would break navigation.
  560. " (One can't leave the [x].)
  561. if !empty(maparg('j', 'n'))
  562. nnoremap <buffer> j j
  563. endif
  564. if !empty(maparg('k', 'n'))
  565. nnoremap <buffer> k k
  566. endif
  567. endfunction
  568. " Function: s:set_mark {{{1
  569. function! s:set_mark(type, ...) abort
  570. let index = expand('<cword>')
  571. let line = exists('a:1') ? a:1 : line('.')
  572. let entry = s:entries[line]
  573. if entry.type != 'file'
  574. return
  575. endif
  576. let default_cmds = {
  577. \ 'B': 'edit',
  578. \ 'S': 'split',
  579. \ 'V': 'vsplit',
  580. \ 'T': 'tabnew',
  581. \ }
  582. setlocal modifiable
  583. if entry.marked && index[0] == a:type
  584. let entry.cmd = 'edit'
  585. let entry.marked = 0
  586. execute 'normal! ci]'. entry.index
  587. else
  588. let entry.cmd = default_cmds[a:type]
  589. let entry.marked = 1
  590. let entry.tick = s:tick
  591. let s:tick += 1
  592. execute 'normal! ci]'. repeat(a:type, len(index))
  593. endif
  594. setlocal nomodifiable nomodified
  595. endfunction
  596. " Function: s:sort_by_tick {{{1
  597. function! s:sort_by_tick(one, two)
  598. return a:one.tick - a:two.tick
  599. endfunction
  600. " Function: s:check_user_options {{{1
  601. function! s:check_user_options() abort
  602. let path = expand('%')
  603. let session = path . s:sep .'Session.vim'
  604. " change to VCS root directory
  605. if get(g:, 'startify_session_autoload') && filereadable(session)
  606. execute 'source' session
  607. elseif get(g:, 'startify_change_to_vcs_root')
  608. call s:cd_to_vcs_root(path)
  609. " change directory
  610. elseif get(g:, 'startify_change_to_dir', 1)
  611. if isdirectory(path)
  612. lcd %
  613. else
  614. lcd %:p:h
  615. endif
  616. endif
  617. endfunction
  618. " Function: s:cd_to_vcs_root {{{1
  619. function! s:cd_to_vcs_root(path) abort
  620. let dir = fnamemodify(a:path, ':p:h')
  621. for vcs in [ '.git', '.hg', '.bzr', '.svn' ]
  622. let root = finddir(vcs, dir .';')
  623. if !empty(root)
  624. execute 'cd '. fnamemodify(root, ':h')
  625. return
  626. endif
  627. endfor
  628. endfunction
  629. " Function: s:close {{{1
  630. function! s:close() abort
  631. if len(filter(range(0, bufnr('$')), 'buflisted(v:val)'))
  632. if bufloaded(bufnr('#')) && bufnr('#') != bufnr('%')
  633. buffer #
  634. else
  635. bnext
  636. endif
  637. else
  638. quit
  639. endif
  640. endfunction
  641. " Function: s:get_index_as_string {{{1
  642. function! s:get_index_as_string(idx) abort
  643. if exists('g:startify_custom_indices')
  644. let listlen = len(g:startify_custom_indices)
  645. return (a:idx < listlen) ? g:startify_custom_indices[a:idx] : string(a:idx - listlen)
  646. else
  647. return string(a:idx)
  648. endif
  649. endfunction
  650. " Function: s:print_section_header {{{1
  651. function! s:print_section_header() abort
  652. $
  653. let curline = line('.')
  654. for lnum in range(curline, curline + len(s:last_message) + 1)
  655. call add(b:startify_section_header_lines, lnum)
  656. endfor
  657. call append('$', s:last_message + [''])
  658. unlet s:last_message
  659. endfunction
  660. " Function: s:register {{{1
  661. function! s:register(line, index, type, cmd, path, wait)
  662. let s:entries[a:line] = {
  663. \ 'index': a:index,
  664. \ 'type': a:type,
  665. \ 'cmd': a:cmd,
  666. \ 'path': a:path,
  667. \ 'nowait': a:wait,
  668. \ 'marked': 0,
  669. \ }
  670. endfunction
  671. " Function: s:create_last_session_link {{{1
  672. function! s:create_last_session_link(spath)
  673. if !has('win32') && a:spath !~# '__LAST__$'
  674. let cmd = printf('ln -sf %s %s',
  675. \ shellescape(fnamemodify(a:spath, ':t')),
  676. \ shellescape(s:session_dir .'/__LAST__'))
  677. silent! call system(cmd)
  678. if v:shell_error
  679. echomsg "startify: Can't create 'last used session' symlink."
  680. endif
  681. endif
  682. endfunction
  683. " Function: s:init_env {{{1
  684. function! s:init_env()
  685. let s:env = []
  686. let ignore = { 'PWD': 1, 'OLDPWD': 1 }
  687. function! s:get_env()
  688. redir => s
  689. silent! execute "norm!:ec$\<c-a>'\<c-b>\<right>\<right>\<del>'\<cr>"
  690. redir END
  691. redraw
  692. return split(s)
  693. endfunction
  694. function! s:compare_by_key_len(foo, bar)
  695. return len(a:foo[0]) - len(a:bar[0])
  696. endfunction
  697. function! s:compare_by_val_len(foo, bar)
  698. return len(a:bar[1]) - len(a:foo[1])
  699. endfunction
  700. for k in s:get_env()
  701. silent! execute "let v = eval('$'.k)"
  702. if has('win32') ? (v[1] != ':') : (v[0] != '/')
  703. \ || has_key(ignore, k)
  704. \ || len(k) > len(v)
  705. continue
  706. endif
  707. call insert(s:env, [k,v], 0)
  708. endfor
  709. let s:env = sort(s:env, 's:compare_by_key_len')
  710. let s:env = sort(s:env, 's:compare_by_val_len')
  711. endfunction