startify.vim 24 KB

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