startify.vim 12 KB

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