startify.vim 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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. \ || (exists('g:startify_skiplist') && s:is_in_skiplist(resolve(fnamemodify(fname, ':p'))))
  181. continue
  182. endif
  183. call add(files, [getftime(fname), fname])
  184. endfor
  185. function! l:compare(x, y)
  186. return a:y[0] - a:x[0]
  187. endfunction
  188. call sort(files, 'l:compare')
  189. for items in files
  190. let index = s:get_index_as_string(cnt)
  191. let fname = items[1]
  192. call append('$', ' ['. index .']'. repeat(' ', (3 - strlen(index))) . fname)
  193. execute 'nnoremap <buffer>' index ':edit' fnameescape(fname) '<cr>'
  194. let cnt += 1
  195. if (cnt == s:numfiles)
  196. break
  197. endif
  198. endfor
  199. return cnt
  200. endfunction
  201. " Function: s:show_files {{{1
  202. function! s:show_files(cnt) abort
  203. let cnt = a:cnt
  204. let num = s:numfiles
  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 num -= 1
  224. if !num
  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. enew
  325. setlocal nobuflisted
  326. for i in range(len(s:marked))
  327. for val in values(s:marked)
  328. if val[0] == i
  329. if val[3] == 'S'
  330. if line2byte('$') == -1
  331. execute 'edit' val[2]
  332. else
  333. execute 'split' val[2]
  334. endif
  335. elseif val[3] == 'V'
  336. if line2byte('$') == -1
  337. execute 'edit' val[2]
  338. else
  339. execute 'vsplit' val[2]
  340. endif
  341. else
  342. execute 'edit' val[2]
  343. endif
  344. continue
  345. endif
  346. endfor
  347. endfor
  348. else
  349. execute 'normal' a:cword
  350. endif
  351. if exists('s:marked')
  352. unlet s:marked
  353. unlet s:nmarked
  354. endif
  355. endfunction
  356. " Function: s:close {{{1
  357. function! s:close() abort
  358. if len(filter(range(0, bufnr('$')), 'buflisted(v:val)'))
  359. if bufloaded(bufnr('#'))
  360. b #
  361. else
  362. bn
  363. endif
  364. else
  365. quit
  366. endif
  367. endfunction
  368. " Function: s:get_index_as_string {{{1
  369. function! s:get_index_as_string(idx) abort
  370. if exists('g:startify_custom_indices')
  371. let listlen = len(g:startify_custom_indices)
  372. return (a:idx < listlen) ? g:startify_custom_indices[a:idx] : string(a:idx - listlen)
  373. else
  374. return string(a:idx)
  375. endif
  376. endfunction
  377. " vim: et sw=2 sts=2