startify.vim 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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> <space> :call <SID>set_mark('B')<cr>
  99. nnoremap <buffer> s :call <SID>set_mark('S')<cr>
  100. nnoremap <buffer> v :call <SID>set_mark('V')<cr>
  101. nnoremap <buffer> <cr> :call <SID>open_buffers(expand('<cword>'))<cr>
  102. nnoremap <buffer> <2-LeftMouse> :execute 'normal '. matchstr(getline('.'), '\w\+')<cr>
  103. nnoremap <buffer> q
  104. \ :if (len(filter(range(0, bufnr('$')), 'buflisted(v:val)')) > 1) <bar>
  105. \ bd <bar>
  106. \ else <bar>
  107. \ quit <bar>
  108. \ endif<cr>
  109. if exists('g:startify_empty_buffer_key')
  110. execute 'nnoremap <buffer><silent> '. g:startify_empty_buffer_key .' :enew<cr>'
  111. endif
  112. autocmd! startify *
  113. autocmd startify CursorMoved <buffer> call s:set_cursor()
  114. autocmd startify BufLeave <buffer> try | wincmd c | catch | autocmd! startify *
  115. call cursor(special ? 4 : 2, 5)
  116. endfunction
  117. " Function: s:open_buffers {{{1
  118. function! s:open_buffers(cword) abort
  119. if exists('s:marked') && !empty(s:marked)
  120. for i in range(len(s:marked))
  121. for val in values(s:marked)
  122. if val[0] == i
  123. if val[3] == 'S'
  124. execute 'split '. val[2]
  125. elseif val[3] == 'V'
  126. execute 'vsplit '. val[2]
  127. else
  128. execute 'edit '. val[2]
  129. endif
  130. continue
  131. endif
  132. endfor
  133. endfor
  134. else
  135. execute 'normal '. a:cword
  136. endif
  137. endfunction
  138. " Function: s:set_mark {{{1
  139. "
  140. " Markers are saved in the s:marked dict using the follow format:
  141. " - s:marked[0]: ID (for sorting)
  142. " - s:marked[1]: what the brackets contained before
  143. " - s:marked[2]: the actual path
  144. " - s:marked[3]: type (buffer, split, vsplit)
  145. "
  146. function! s:set_mark(type) abort
  147. if !exists('s:marked')
  148. let s:marked = {}
  149. let s:nmarked = 0
  150. endif
  151. " matches[1]: content between brackets
  152. " matches[2]: path
  153. let matches = matchlist(getline('.'), '\v\[(.*)\]\s+(.*)')
  154. setlocal modifiable
  155. if matches[1] =~ 'B\|S\|V'
  156. let s:nmarked -= 1
  157. execute 'normal! ci]'. remove(s:marked, line('.'))[1]
  158. else
  159. let s:marked[line('.')] = [s:nmarked, matches[1], matches[2], a:type]
  160. let s:nmarked += 1
  161. execute 'normal! ci]'. repeat(a:type, len(matches[1]))
  162. endif
  163. setlocal nomodifiable nomodified
  164. endfunction
  165. " Function: s:get_index_as_string {{{1
  166. function! s:get_index_as_string(idx) abort
  167. if exists('g:startify_custom_indices')
  168. let listlen = len(g:startify_custom_indices)
  169. return (a:idx < listlen) ? g:startify_custom_indices[a:idx] : string(a:idx - listlen)
  170. else
  171. return string(a:idx)
  172. endif
  173. endfunction
  174. " Function: s:set_cursor {{{1
  175. function! s:set_cursor() abort
  176. let s:line_old = exists('s:line_new') ? s:line_new : 5
  177. let s:line_new = line('.')
  178. if empty(getline(s:line_new))
  179. if (s:line_new > s:line_old)
  180. let s:line_new += 1
  181. call cursor(s:line_new, 5) " going down
  182. else
  183. let s:line_new -= 1
  184. call cursor((s:line_new < 2 ? 2 : s:line_new), 5) " going up
  185. endif
  186. else
  187. call cursor((s:line_new < 2 ? 2 : 0), 5) " hold cursor in column
  188. endif
  189. endfunction
  190. " vim: et sw=2 sts=2