startify.vim 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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> b :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. autocmd startify WinLeave <buffer> bd
  100. call cursor(special ? 4 : 2, 5)
  101. endfunction
  102. " Function: startify#session_delete {{{1
  103. function! startify#session_delete(...) abort
  104. if !isdirectory(s:session_dir)
  105. echo 'The session directory does not exist: '. s:session_dir
  106. return
  107. elseif empty(startify#session_list_as_string(''))
  108. echo 'There are no sessions...'
  109. return
  110. endif
  111. let spath = s:session_dir . startify#get_separator() . (exists('a:1')
  112. \ ? a:1
  113. \ : input('Delete this session: ', fnamemodify(v:this_session, ':t'), 'custom,startify#session_list_as_string'))
  114. \ | redraw
  115. echo 'Really delete '. spath .'? [y/n]' | redraw
  116. if (nr2char(getchar()) == 'y')
  117. if delete(spath) == 0
  118. echo 'Deleted session '. spath .'!'
  119. else
  120. echo 'Deletion failed!'
  121. endif
  122. else
  123. echo 'Deletion aborted!'
  124. endif
  125. endfunction
  126. " Function: startify#session_save {{{1
  127. function! startify#session_save(...) abort
  128. if !isdirectory(s:session_dir)
  129. if exists('*mkdir')
  130. echo 'The session directory does not exist: '. s:session_dir .'. Create it? [y/n]' | redraw
  131. if (nr2char(getchar()) == 'y')
  132. call mkdir(s:session_dir, 'p')
  133. else
  134. echo
  135. return
  136. endif
  137. else
  138. echo 'The session directory does not exist: '. s:session_dir
  139. return
  140. endif
  141. endif
  142. if exists('a:1')
  143. let sname = a:1
  144. else
  145. let sname = input('Save under this session name: ', fnamemodify(v:this_session, ':t'), 'custom,startify#session_list_as_string')
  146. redraw
  147. if empty(sname)
  148. echo 'You gave an empty name!'
  149. return
  150. endif
  151. endif
  152. let spath = s:session_dir . startify#get_separator() . sname
  153. if !filereadable(spath)
  154. execute 'mksession '. s:escape(spath) | echo 'Session saved under: '. spath
  155. return
  156. endif
  157. echo 'Session already exists. Overwrite? [y/n]' | redraw
  158. if nr2char(getchar()) == 'y'
  159. execute 'mksession! '. s:escape(spath) | echo 'Session saved under: '. spath
  160. else
  161. echo 'Did NOT save the session!'
  162. endif
  163. endfunction
  164. " Function: startify#session_load {{{1
  165. function! startify#session_load(...) abort
  166. if !isdirectory(s:session_dir)
  167. echo 'The session directory does not exist: '. s:session_dir
  168. return
  169. elseif empty(startify#session_list_as_string(''))
  170. echo 'There are no sessions...'
  171. return
  172. endif
  173. let spath = s:session_dir . startify#get_separator() . (exists('a:1')
  174. \ ? a:1
  175. \ : input('Load this session: ', fnamemodify(v:this_session, ':t'), 'custom,startify#session_list_as_string'))
  176. \ | redraw
  177. if filereadable(spath)
  178. execute 'source '. s:escape(spath)
  179. else
  180. echo 'No such file: '. spath
  181. endif
  182. endfunction
  183. " Function: startify#session_list {{{1
  184. function! startify#session_list(lead, ...) abort
  185. return map(split(globpath(s:session_dir, '*'.a:lead.'*'), '\n'), 'fnamemodify(v:val, ":t")')
  186. endfunction
  187. " Function: startify#session_list_as_string {{{1
  188. function! startify#session_list_as_string(lead, ...) abort
  189. return join(map(split(globpath(s:session_dir, '*'.a:lead.'*'), '\n'), 'fnamemodify(v:val, ":t")'), "\n")
  190. endfunction
  191. " Function: startify#get_separator {{{1
  192. function! startify#get_separator() abort
  193. return !exists('+shellslash') || &shellslash ? '/' : '\'
  194. endfunction
  195. " Function: s:escape {{{1
  196. function! s:escape(path) abort
  197. return !exists('+shellslash') || &shellslash ? fnameescape(a:path) : escape(a:path, '\')
  198. endfunction
  199. " Function: s:is_in_skiplist {{{1
  200. function! s:is_in_skiplist(arg) abort
  201. for regexp in g:startify_skiplist
  202. if (a:arg =~# regexp)
  203. return 1
  204. endif
  205. endfor
  206. endfunction
  207. " Function: s:is_bookmark {{{1
  208. function! s:is_bookmark(arg) abort
  209. "for foo in filter(map(copy(g:startify_bookmarks), 'resolve(fnamemodify(v:val, ":p"))'), '!isdirectory(v:val)')
  210. for foo in map(filter(copy(g:startify_bookmarks), '!isdirectory(v:val)'), 'resolve(fnamemodify(v:val, ":p"))')
  211. if foo == a:arg
  212. return 1
  213. endif
  214. endfor
  215. endfunction
  216. " Function: s:open_buffers {{{1
  217. function! s:open_buffers(cword) abort
  218. if exists('s:marked') && !empty(s:marked)
  219. for i in range(len(s:marked))
  220. for val in values(s:marked)
  221. if val[0] == i
  222. if val[3] == 'S'
  223. execute 'split '. val[2]
  224. elseif val[3] == 'V'
  225. execute 'vsplit '. val[2]
  226. else
  227. execute 'edit '. val[2]
  228. endif
  229. continue
  230. endif
  231. endfor
  232. endfor
  233. else
  234. execute 'normal '. a:cword
  235. endif
  236. endfunction
  237. " Function: s:set_mark {{{1
  238. "
  239. " Markers are saved in the s:marked dict using the follow format:
  240. " - s:marked[0]: ID (for sorting)
  241. " - s:marked[1]: what the brackets contained before
  242. " - s:marked[2]: the actual path
  243. " - s:marked[3]: type (buffer, split, vsplit)
  244. "
  245. function! s:set_mark(type) abort
  246. if !exists('s:marked')
  247. let s:marked = {}
  248. let s:nmarked = 0
  249. endif
  250. " matches[1]: content between brackets
  251. " matches[2]: path
  252. let matches = matchlist(getline('.'), '\v\[(.*)\]\s+(.*)')
  253. if matches[2] =~ '\V<empty buffer>\|<quit>' || matches[2] =~ '^\w\+$'
  254. return
  255. endif
  256. setlocal modifiable
  257. if matches[1] =~ 'B\|S\|V'
  258. let s:nmarked -= 1
  259. execute 'normal! ci]'. remove(s:marked, line('.'))[1]
  260. else
  261. let s:marked[line('.')] = [s:nmarked, matches[1], matches[2], a:type]
  262. let s:nmarked += 1
  263. execute 'normal! ci]'. repeat(a:type, len(matches[1]))
  264. endif
  265. setlocal nomodifiable nomodified
  266. endfunction
  267. " Function: s:get_index_as_string {{{1
  268. function! s:get_index_as_string(idx) abort
  269. if exists('g:startify_custom_indices')
  270. let listlen = len(g:startify_custom_indices)
  271. return (a:idx < listlen) ? g:startify_custom_indices[a:idx] : string(a:idx - listlen)
  272. else
  273. return string(a:idx)
  274. endif
  275. endfunction
  276. " Function: s:set_cursor {{{1
  277. function! s:set_cursor() abort
  278. let s:line_old = exists('s:line_new') ? s:line_new : 5
  279. let s:line_new = line('.')
  280. if empty(getline(s:line_new))
  281. if (s:line_new > s:line_old)
  282. let s:line_new += 1
  283. call cursor(s:line_new, 5) " going down
  284. else
  285. let s:line_new -= 1
  286. call cursor((s:line_new < 2 ? 2 : s:line_new), 5) " going up
  287. endif
  288. else
  289. call cursor((s:line_new < 2 ? 2 : 0), 5) " hold cursor in column
  290. endif
  291. endfunction
  292. " vim: et sw=2 sts=2