startify.vim 6.6 KB

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