startify.vim 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. setfiletype startify
  44. let special = get(g:, 'startify_enable_special', 1)
  45. let sep = startify#get_separator()
  46. let cnt = 0
  47. if special
  48. call append('$', ' [e] <empty buffer>')
  49. endif
  50. if get(g:, 'startify_show_files', 1) && !empty(v:oldfiles)
  51. let numfiles = get(g:, 'startify_show_files_number', 10)
  52. if special
  53. call append('$', '')
  54. endif
  55. for fname in v:oldfiles
  56. let expfname = expand(fname)
  57. if !filereadable(expfname) || (exists('g:startify_skiplist') && startify#is_in_skiplist(expfname))
  58. continue
  59. endif
  60. let index = s:get_index_as_string(cnt)
  61. call append('$', ' ['. index .']'. repeat(' ', (3 - strlen(index))) . fname)
  62. execute 'nnoremap <buffer> '. index .' :edit '. startify#escape(fname) .' <bar> lcd %:h<cr>'
  63. let cnt += 1
  64. if (cnt == numfiles)
  65. break
  66. endif
  67. endfor
  68. endif
  69. let sfiles = split(globpath(g:startify_session_dir, '*'), '\n')
  70. if get(g:, 'startify_show_sessions', 1) && !empty(sfiles)
  71. call append('$', '')
  72. for i in range(len(sfiles))
  73. let idx = (i + cnt)
  74. let index = s:get_index_as_string(idx)
  75. call append('$', ' ['. index .']'. repeat(' ', (3 - strlen(index))) . fnamemodify(sfiles[i], ':t:r'))
  76. execute 'nnoremap <buffer> '. index .' :source '. startify#escape(sfiles[i]) .'<cr>'
  77. endfor
  78. let cnt = idx
  79. endif
  80. if exists('g:startify_bookmarks')
  81. call append('$', '')
  82. for fname in g:startify_bookmarks
  83. let cnt += 1
  84. let index = s:get_index_as_string(cnt)
  85. call append('$', ' ['. index .']'. repeat(' ', (3 - strlen(index))) . fname)
  86. execute 'nnoremap <buffer> '. index .' :edit '. startify#escape(fname) .' <bar> lcd %:h<cr>'
  87. endfor
  88. endif
  89. if special
  90. call append('$', ['', ' [q] <quit>'])
  91. endif
  92. setlocal nomodifiable nomodified
  93. nnoremap <buffer><silent> e :enew<cr>
  94. nnoremap <buffer><silent> i :enew <bar> startinsert<cr>
  95. nnoremap <buffer> <space> :call <SID>set_mark('B')<cr>
  96. nnoremap <buffer> s :call <SID>set_mark('S')<cr>
  97. nnoremap <buffer> v :call <SID>set_mark('V')<cr>
  98. nnoremap <buffer> <cr> :call <SID>open_buffers(expand('<cword>'))<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 BufLeave <buffer> try | wincmd c | catch /E444/ | endtry | autocmd! startify *
  112. call cursor(special ? 4 : 2, 5)
  113. endfunction
  114. " Function: s:open_buffers {{{1
  115. function! s:open_buffers(cword) abort
  116. if exists('s:marked') && !empty(s:marked)
  117. for i in range(len(s:marked))
  118. for val in values(s:marked)
  119. if val[0] == i
  120. if val[3] == 'S'
  121. execute 'split '. val[2]
  122. elseif val[3] == 'V'
  123. execute 'vsplit '. val[2]
  124. else
  125. execute 'edit '. val[2]
  126. endif
  127. continue
  128. endif
  129. endfor
  130. endfor
  131. else
  132. execute 'normal '. a:cword
  133. endif
  134. endfunction
  135. " Function: s:set_mark {{{1
  136. "
  137. " Markers are saved in the s:marked dict using the follow format:
  138. " - s:marked[0]: ID (for sorting)
  139. " - s:marked[1]: what the brackets contained before
  140. " - s:marked[2]: the actual path
  141. " - s:marked[3]: type (buffer, split, vsplit)
  142. "
  143. function! s:set_mark(type) abort
  144. if !exists('s:marked')
  145. let s:marked = {}
  146. let s:nmarked = 0
  147. endif
  148. " matches[1]: content between brackets
  149. " matches[2]: path
  150. let matches = matchlist(getline('.'), '\v\[(.*)\]\s+(.*)')
  151. setlocal modifiable
  152. if matches[1] =~ 'B\|S\|V'
  153. let s:nmarked -= 1
  154. execute 'normal! ci]'. remove(s:marked, line('.'))[1]
  155. else
  156. let s:marked[line('.')] = [s:nmarked, matches[1], matches[2], a:type]
  157. let s:nmarked += 1
  158. execute 'normal! ci]'. repeat(a:type, len(matches[1]))
  159. endif
  160. setlocal nomodifiable nomodified
  161. endfunction
  162. " Function: s:get_index_as_string {{{1
  163. function! s:get_index_as_string(idx) abort
  164. if exists('g:startify_custom_indices')
  165. let listlen = len(g:startify_custom_indices)
  166. return (a:idx < listlen) ? g:startify_custom_indices[a:idx] : string(a:idx - listlen)
  167. else
  168. return string(a:idx)
  169. endif
  170. endfunction
  171. " Function: s:set_cursor {{{1
  172. function! s:set_cursor() abort
  173. let s:line_old = exists('s:line_new') ? s:line_new : 5
  174. let s:line_new = line('.')
  175. if empty(getline(s:line_new))
  176. if (s:line_new > s:line_old)
  177. let s:line_new += 1
  178. call cursor(s:line_new, 5) " going down
  179. else
  180. let s:line_new -= 1
  181. call cursor((s:line_new < 2 ? 2 : s:line_new), 5) " going up
  182. endif
  183. else
  184. call cursor((s:line_new < 2 ? 2 : 0), 5) " hold cursor in column
  185. endif
  186. endfunction
  187. " vim: et sw=2 sts=2