startify.vim 4.9 KB

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