startify.vim 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. " Plugin: https://github.com/mhinz/vim-startify
  2. " Description: Start screen displaying recently used stuff.
  3. " Maintainer: Marco Hinz <http://github.com/mhinz>
  4. " Version: 1.4
  5. if exists('g:loaded_startify') || &cp
  6. finish
  7. endif
  8. let g:loaded_startify = 1
  9. " Init {{{1
  10. let g:startify_session_dir = resolve(expand(get(g:, 'startify_session_dir',
  11. \ has('win32') ? '$HOME\vimfiles\session' : '~/.vim/session')))
  12. if exists('g:startify_bookmarks')
  13. let exclude = map(copy(g:startify_bookmarks), 'expand(v:val)')
  14. if exists('g:startify_skiplist')
  15. call extend(g:startify_skiplist, exclude)
  16. else
  17. let g:startify_skiplist = exclude
  18. endif
  19. endif
  20. augroup startify
  21. autocmd!
  22. autocmd VimEnter *
  23. \ if !argc() && (line2byte('$') == -1) && (v:progname =~? '^[gm]\=vim\%[\.exe]$') |
  24. \ call s:insane_in_the_membrane() |
  25. \ endif
  26. augroup END
  27. command! -nargs=? -bar -complete=customlist,startify#get_session_names SSave call startify#save_session(<f-args>)
  28. command! -nargs=? -bar -complete=customlist,startify#get_session_names SLoad call startify#load_session(<f-args>)
  29. command! -nargs=0 -bar Startify enew | call s:insane_in_the_membrane()
  30. " Function: s:insane_in_the_membrane {{{1
  31. function! s:insane_in_the_membrane() abort
  32. if !empty(v:servername) && exists('g:startify_skiplist_server')
  33. for servname in g:startify_skiplist_server
  34. if (servname == v:servername)
  35. return
  36. endif
  37. endfor
  38. endif
  39. setlocal nonumber noswapfile bufhidden=wipe
  40. if (v:version >= 703)
  41. setlocal norelativenumber
  42. endif
  43. if get(g:, 'startify_unlisted_buffer', 1)
  44. setlocal nobuflisted
  45. endif
  46. setfiletype startify
  47. let special = get(g:, 'startify_enable_special', 1)
  48. let sep = startify#get_separator()
  49. let cnt = 0
  50. if special
  51. call append('$', ' [e] <empty buffer>')
  52. endif
  53. if get(g:, 'startify_show_files', 1) && !empty(v:oldfiles)
  54. let numfiles = get(g:, 'startify_show_files_number', 10)
  55. if special
  56. call append('$', '')
  57. endif
  58. for fname in v:oldfiles
  59. let expfname = expand(fname)
  60. if !filereadable(expfname) || (exists('g:startify_skiplist') && startify#is_in_skiplist(expfname))
  61. continue
  62. endif
  63. let index = s:get_index_as_string(cnt)
  64. call append('$', ' ['. index .']'. repeat(' ', (3 - strlen(index))) . fname)
  65. execute 'nnoremap <buffer> '. index .' :edit '. startify#escape(fname) .' <bar> lcd %:h<cr>'
  66. let cnt += 1
  67. if (cnt == numfiles)
  68. break
  69. endif
  70. endfor
  71. endif
  72. let sfiles = split(globpath(g:startify_session_dir, '*'), '\n')
  73. if get(g:, 'startify_show_sessions', 1) && !empty(sfiles)
  74. call append('$', '')
  75. for i in range(len(sfiles))
  76. let idx = (i + cnt)
  77. let index = s:get_index_as_string(idx)
  78. call append('$', ' ['. index .']'. repeat(' ', (3 - strlen(index))) . fnamemodify(sfiles[i], ':t:r'))
  79. execute 'nnoremap <buffer> '. index .' :source '. startify#escape(sfiles[i]) .'<cr>'
  80. endfor
  81. let cnt = idx
  82. endif
  83. if exists('g:startify_bookmarks')
  84. call append('$', '')
  85. for fname in g:startify_bookmarks
  86. let cnt += 1
  87. let index = s:get_index_as_string(cnt)
  88. call append('$', ' ['. index .']'. repeat(' ', (3 - strlen(index))) . fname)
  89. execute 'nnoremap <buffer> '. index .' :edit '. startify#escape(fname) .' <bar> lcd %:h<cr>'
  90. endfor
  91. endif
  92. if special
  93. call append('$', ['', ' [q] <quit>'])
  94. endif
  95. setlocal nomodifiable nomodified
  96. nnoremap <buffer><silent> e :enew<cr>
  97. nnoremap <buffer><silent> i :enew <bar> startinsert<cr>
  98. nnoremap <buffer> <cr> :normal <c-r><c-w><cr>
  99. nnoremap <buffer> <2-LeftMouse> :execute 'normal '. matchstr(getline('.'), '\w\+')<cr>
  100. nnoremap <buffer> q
  101. \ :if (len(filter(range(0, bufnr('$')), 'buflisted(v:val)')) > 1) <bar>
  102. \ bd <bar>
  103. \ else <bar>
  104. \ quit <bar>
  105. \ endif<cr>
  106. if exists('g:startify_empty_buffer_key')
  107. execute 'nnoremap <buffer><silent> '. g:startify_empty_buffer_key .' :enew<cr>'
  108. endif
  109. autocmd! startify *
  110. autocmd startify CursorMoved <buffer> call s:set_cursor()
  111. autocmd startify BufWipeout <buffer> autocmd! startify *
  112. call cursor(special ? 4 : 2, 5)
  113. endfunction
  114. " Function: s:get_index_as_string {{{1
  115. function! s:get_index_as_string(idx) abort
  116. if exists('g:startify_custom_indices')
  117. let listlen = len(g:startify_custom_indices)
  118. return (a:idx < listlen) ? g:startify_custom_indices[a:idx] : string(a:idx - listlen)
  119. else
  120. return string(a:idx)
  121. endif
  122. endfunction
  123. " Function: s:set_cursor {{{1
  124. function! s:set_cursor() abort
  125. let s:line_old = exists('s:line_new') ? s:line_new : 5
  126. let s:line_new = line('.')
  127. if empty(getline(s:line_new))
  128. if (s:line_new > s:line_old)
  129. let s:line_new += 1
  130. call cursor(s:line_new, 5) " going down
  131. else
  132. let s:line_new -= 1
  133. call cursor((s:line_new < 2 ? 2 : s:line_new), 5) " going up
  134. endif
  135. else
  136. call cursor((s:line_new < 2 ? 2 : 0), 5) " hold cursor in column
  137. endif
  138. endfunction
  139. " vim: et sw=2 sts=2