startify.vim 11 KB

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