startify.vim 4.3 KB

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