startify.vim 12 KB

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