startify.vim 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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.6
  5. if exists('g:autoloaded_startify') || &cp
  6. finish
  7. endif
  8. let g:autoloaded_startify = 1
  9. let s:session_dir = resolve(expand(get(g:, 'startify_session_dir',
  10. \ has('win32') ? '$HOME\vimfiles\session' : '~/.vim/session')))
  11. " Function: startify#insane_in_the_membrane {{{1
  12. function! startify#insane_in_the_membrane() abort
  13. if !empty(v:servername) && exists('g:startify_skiplist_server')
  14. for servname in g:startify_skiplist_server
  15. if (servname == v:servername)
  16. return
  17. endif
  18. endfor
  19. endif
  20. setlocal nonumber noswapfile bufhidden=wipe
  21. if (v:version >= 703)
  22. setlocal norelativenumber
  23. endif
  24. setfiletype startify
  25. let special = get(g:, 'startify_enable_special', 1)
  26. let sep = startify#get_separator()
  27. let cnt = 0
  28. if special
  29. call append('$', ' [e] <empty buffer>')
  30. endif
  31. if get(g:, 'startify_show_files', 1) && !empty(v:oldfiles)
  32. let entries = {}
  33. let numfiles = get(g:, 'startify_show_files_number', 10)
  34. if special
  35. call append('$', '')
  36. endif
  37. for fname in v:oldfiles
  38. let expfname = resolve(fnamemodify(fname, ':p'))
  39. " filter duplicates, bookmarks and entries from the skiplist
  40. if has_key(entries, expfname) ||
  41. \ !filereadable(expfname) ||
  42. \ (exists('g:startify_skiplist') && s:is_in_skiplist(expfname)) ||
  43. \ (exists('g:startify_bookmarks') && s:is_bookmark(expfname))
  44. continue
  45. endif
  46. let entries[expfname] = 1
  47. let index = s:get_index_as_string(cnt)
  48. call append('$', ' ['. index .']'. repeat(' ', (3 - strlen(index))) . fname)
  49. execute 'nnoremap <buffer> '. index .' :edit '. s:escape(fname) .' <bar> lcd %:h<cr>'
  50. let cnt += 1
  51. if (cnt == numfiles)
  52. break
  53. endif
  54. endfor
  55. endif
  56. let sfiles = split(globpath(s:session_dir, '*'), '\n')
  57. if get(g:, 'startify_show_sessions', 1) && !empty(sfiles)
  58. call append('$', '')
  59. for i in range(len(sfiles))
  60. let idx = (i + cnt)
  61. let index = s:get_index_as_string(idx)
  62. call append('$', ' ['. index .']'. repeat(' ', (3 - strlen(index))) . fnamemodify(sfiles[i], ':t:r'))
  63. execute 'nnoremap <buffer> '. index .' :source '. s:escape(sfiles[i]) .'<cr>'
  64. endfor
  65. let cnt = idx
  66. endif
  67. if exists('g:startify_bookmarks')
  68. call append('$', '')
  69. for fname in g:startify_bookmarks
  70. let cnt += 1
  71. let index = s:get_index_as_string(cnt)
  72. call append('$', ' ['. index .']'. repeat(' ', (3 - strlen(index))) . fname)
  73. execute 'nnoremap <buffer> '. index .' :edit '. s:escape(fname) .' <bar> lcd %:h<cr>'
  74. endfor
  75. endif
  76. if special
  77. call append('$', ['', ' [q] <quit>'])
  78. endif
  79. setlocal nomodifiable nomodified
  80. nnoremap <buffer><silent> e :enew<cr>
  81. nnoremap <buffer><silent> i :enew <bar> startinsert<cr>
  82. nnoremap <buffer><silent> <space> :call <SID>set_mark('B')<cr>
  83. nnoremap <buffer><silent> s :call <SID>set_mark('S')<cr>
  84. nnoremap <buffer><silent> v :call <SID>set_mark('V')<cr>
  85. nnoremap <buffer> <cr> :call <SID>open_buffers(expand('<cword>'))<cr>
  86. nnoremap <buffer> <2-LeftMouse> :execute 'normal '. matchstr(getline('.'), '\w\+')<cr>
  87. nnoremap <buffer> q
  88. \ :if (len(filter(range(0, bufnr('$')), 'buflisted(v:val)')) > 1) <bar>
  89. \ bd <bar>
  90. \ else <bar>
  91. \ quit <bar>
  92. \ endif<cr>
  93. if exists('g:startify_empty_buffer_key')
  94. execute 'nnoremap <buffer><silent> '. g:startify_empty_buffer_key .' :enew<cr>'
  95. endif
  96. autocmd! startify *
  97. autocmd startify CursorMoved <buffer> call s:set_cursor()
  98. autocmd startify BufLeave <buffer> autocmd! startify *
  99. call cursor(special ? 4 : 2, 5)
  100. endfunction
  101. " Function: startify#session_delete {{{1
  102. function! startify#session_delete(...) abort
  103. if !isdirectory(s:session_dir)
  104. echo 'The session directory does not exist: '. s:session_dir
  105. return
  106. elseif empty(startify#session_list_as_string(''))
  107. echo 'There are no sessions...'
  108. return
  109. endif
  110. let spath = s:session_dir . startify#get_separator() . (exists('a:1')
  111. \ ? a:1
  112. \ : input('Delete this session: ', fnamemodify(v:this_session, ':t'), 'custom,startify#session_list_as_string'))
  113. \ | redraw
  114. echo 'Really delete '. spath .'? [y/n]' | redraw
  115. if (nr2char(getchar()) == 'y')
  116. if delete(spath) == 0
  117. echo 'Deleted session '. spath .'!'
  118. else
  119. echo 'Deletion failed!'
  120. endif
  121. else
  122. echo 'Deletion aborted!'
  123. endif
  124. endfunction
  125. " Function: startify#session_save {{{1
  126. function! startify#session_save(...) abort
  127. if !isdirectory(s:session_dir)
  128. if exists('*mkdir')
  129. echo 'The session directory does not exist: '. s:session_dir .'. Create it? [y/n]' | redraw
  130. if (nr2char(getchar()) == 'y')
  131. call mkdir(s:session_dir, 'p')
  132. else
  133. echo
  134. return
  135. endif
  136. else
  137. echo 'The session directory does not exist: '. s:session_dir
  138. return
  139. endif
  140. endif
  141. if exists('a:1')
  142. let sname = a:1
  143. else
  144. let sname = input('Save under this session name: ', fnamemodify(v:this_session, ':t'), 'custom,startify#session_list_as_string')
  145. redraw
  146. if empty(sname)
  147. echo 'You gave an empty name!'
  148. return
  149. endif
  150. endif
  151. let spath = s:escape(s:session_dir . startify#get_separator() . sname)
  152. if !filereadable(spath)
  153. execute 'mksession '. spath | echo 'Session saved under: '. spath
  154. return
  155. endif
  156. echo 'Session already exists. Overwrite? [y/n]' | redraw
  157. if nr2char(getchar()) == 'y'
  158. execute 'mksession! '. spath | echo 'Session saved under: '. spath
  159. else
  160. echo 'Did NOT save the session!'
  161. endif
  162. endfunction
  163. " Function: startify#session_load {{{1
  164. function! startify#session_load(...) abort
  165. if !isdirectory(s:session_dir)
  166. echo 'The session directory does not exist: '. s:session_dir
  167. return
  168. elseif empty(startify#session_list_as_string(''))
  169. echo 'There are no sessions...'
  170. return
  171. endif
  172. let spath = s:session_dir . startify#get_separator() . (exists('a:1')
  173. \ ? a:1
  174. \ : input('Load this session: ', fnamemodify(v:this_session, ':t'), 'custom,startify#session_list_as_string'))
  175. \ | redraw
  176. if filereadable(spath)
  177. execute 'source '. s:escape(spath)
  178. else
  179. echo 'No such file: '. spath
  180. endif
  181. endfunction
  182. " Function: startify#session_list {{{1
  183. function! startify#session_list(lead, ...) abort
  184. return map(split(globpath(s:session_dir, '*'.a:lead.'*'), '\n'), 'fnamemodify(v:val, ":t")')
  185. endfunction
  186. " Function: startify#session_list_as_string {{{1
  187. function! startify#session_list_as_string(lead, ...) abort
  188. return join(map(split(globpath(s:session_dir, '*'.a:lead.'*'), '\n'), 'fnamemodify(v:val, ":t")'), "\n")
  189. endfunction
  190. " Function: startify#get_separator {{{1
  191. function! startify#get_separator() abort
  192. return !exists('+shellslash') || &shellslash ? '/' : '\'
  193. endfunction
  194. " Function: s:escape {{{1
  195. function! s:escape(path) abort
  196. return !exists('+shellslash') || &shellslash ? fnameescape(a:path) : escape(a:path, '\')
  197. endfunction
  198. " Function: s:is_in_skiplist {{{1
  199. function! s:is_in_skiplist(arg) abort
  200. for regexp in g:startify_skiplist
  201. if (a:arg =~# regexp)
  202. return 1
  203. endif
  204. endfor
  205. endfunction
  206. " Function: s:is_bookmark {{{1
  207. function! s:is_bookmark(arg) abort
  208. "for foo in filter(map(copy(g:startify_bookmarks), 'resolve(fnamemodify(v:val, ":p"))'), '!isdirectory(v:val)')
  209. for foo in map(filter(copy(g:startify_bookmarks), '!isdirectory(v:val)'), 'resolve(fnamemodify(v:val, ":p"))')
  210. if foo == a:arg
  211. return 1
  212. endif
  213. endfor
  214. endfunction
  215. " Function: s:open_buffers {{{1
  216. function! s:open_buffers(cword) abort
  217. if exists('s:marked') && !empty(s:marked)
  218. for i in range(len(s:marked))
  219. for val in values(s:marked)
  220. if val[0] == i
  221. if val[3] == 'S'
  222. execute 'split '. val[2]
  223. elseif val[3] == 'V'
  224. execute 'vsplit '. val[2]
  225. else
  226. execute 'edit '. val[2]
  227. endif
  228. continue
  229. endif
  230. endfor
  231. endfor
  232. else
  233. execute 'normal '. a:cword
  234. endif
  235. endfunction
  236. " Function: s:set_mark {{{1
  237. "
  238. " Markers are saved in the s:marked dict using the follow format:
  239. " - s:marked[0]: ID (for sorting)
  240. " - s:marked[1]: what the brackets contained before
  241. " - s:marked[2]: the actual path
  242. " - s:marked[3]: type (buffer, split, vsplit)
  243. "
  244. function! s:set_mark(type) abort
  245. if !exists('s:marked')
  246. let s:marked = {}
  247. let s:nmarked = 0
  248. endif
  249. " matches[1]: content between brackets
  250. " matches[2]: path
  251. let matches = matchlist(getline('.'), '\v\[(.*)\]\s+(.*)')
  252. if matches[2] =~ '\V<empty buffer>\|<quit>' || matches[2] =~ '^\w\+$'
  253. return
  254. endif
  255. setlocal modifiable
  256. if matches[1] =~ 'B\|S\|V'
  257. let s:nmarked -= 1
  258. execute 'normal! ci]'. remove(s:marked, line('.'))[1]
  259. else
  260. let s:marked[line('.')] = [s:nmarked, matches[1], matches[2], a:type]
  261. let s:nmarked += 1
  262. execute 'normal! ci]'. repeat(a:type, len(matches[1]))
  263. endif
  264. setlocal nomodifiable nomodified
  265. endfunction
  266. " Function: s:get_index_as_string {{{1
  267. function! s:get_index_as_string(idx) abort
  268. if exists('g:startify_custom_indices')
  269. let listlen = len(g:startify_custom_indices)
  270. return (a:idx < listlen) ? g:startify_custom_indices[a:idx] : string(a:idx - listlen)
  271. else
  272. return string(a:idx)
  273. endif
  274. endfunction
  275. " Function: s:set_cursor {{{1
  276. function! s:set_cursor() abort
  277. let s:line_old = exists('s:line_new') ? s:line_new : 5
  278. let s:line_new = line('.')
  279. if empty(getline(s:line_new))
  280. if (s:line_new > s:line_old)
  281. let s:line_new += 1
  282. call cursor(s:line_new, 5) " going down
  283. else
  284. let s:line_new -= 1
  285. call cursor((s:line_new < 2 ? 2 : s:line_new), 5) " going up
  286. endif
  287. else
  288. call cursor((s:line_new < 2 ? 2 : 0), 5) " hold cursor in column
  289. endif
  290. endfunction
  291. " vim: et sw=2 sts=2