startify.vim 12 KB

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