startify.vim 11 KB

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