startify.vim 12 KB

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