startify.vim 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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 *
  75. autocmd startify CursorMoved <buffer> call s:set_cursor()
  76. 1
  77. call cursor((s:show_special ? 4 : 2) + s:offset_header, 5)
  78. endfunction
  79. " Function: #session_load {{{1
  80. function! startify#session_load(...) abort
  81. if !isdirectory(s:session_dir)
  82. echo 'The session directory does not exist: '. s:session_dir
  83. return
  84. elseif empty(startify#session_list_as_string(''))
  85. echo 'There are no sessions...'
  86. return
  87. endif
  88. let spath = s:session_dir . startify#get_separator() . (exists('a:1')
  89. \ ? a:1
  90. \ : input('Load this session: ', fnamemodify(v:this_session, ':t'), 'custom,startify#session_list_as_string'))
  91. \ | redraw
  92. if filereadable(spath)
  93. execute 'source '. fnameescape(spath)
  94. else
  95. echo 'No such file: '. spath
  96. endif
  97. endfunction
  98. " Function: #session_save {{{1
  99. function! startify#session_save(...) abort
  100. if !isdirectory(s:session_dir)
  101. if exists('*mkdir')
  102. echo 'The session directory does not exist: '. s:session_dir .'. Create it? [y/n]' | redraw
  103. if (nr2char(getchar()) == 'y')
  104. call mkdir(s:session_dir, 'p')
  105. else
  106. echo
  107. return
  108. endif
  109. else
  110. echo 'The session directory does not exist: '. s:session_dir
  111. return
  112. endif
  113. endif
  114. if exists('a:1')
  115. let sname = a:1
  116. else
  117. let sname = input('Save under this session name: ', fnamemodify(v:this_session, ':t'), 'custom,startify#session_list_as_string')
  118. redraw
  119. if empty(sname)
  120. echo 'You gave an empty name!'
  121. return
  122. endif
  123. endif
  124. let spath = s:session_dir . startify#get_separator() . sname
  125. if !filereadable(spath)
  126. execute 'mksession '. fnameescape(spath) | echo 'Session saved under: '. spath
  127. return
  128. endif
  129. echo 'Session already exists. Overwrite? [y/n]' | redraw
  130. if nr2char(getchar()) == 'y'
  131. execute 'mksession! '. fnameescape(spath) | echo 'Session saved under: '. spath
  132. else
  133. echo 'Did NOT save the session!'
  134. endif
  135. endfunction
  136. " Function: #session_delete {{{1
  137. function! startify#session_delete(...) abort
  138. if !isdirectory(s:session_dir)
  139. echo 'The session directory does not exist: '. s:session_dir
  140. return
  141. elseif empty(startify#session_list_as_string(''))
  142. echo 'There are no sessions...'
  143. return
  144. endif
  145. let spath = s:session_dir . startify#get_separator() . (exists('a:1')
  146. \ ? a:1
  147. \ : input('Delete this session: ', fnamemodify(v:this_session, ':t'), 'custom,startify#session_list_as_string'))
  148. \ | redraw
  149. echo 'Really delete '. spath .'? [y/n]' | redraw
  150. if (nr2char(getchar()) == 'y')
  151. if delete(spath) == 0
  152. echo 'Deleted session '. spath .'!'
  153. else
  154. echo 'Deletion failed!'
  155. endif
  156. else
  157. echo 'Deletion aborted!'
  158. endif
  159. endfunction
  160. " Function: #session_list {{{1
  161. function! startify#session_list(lead, ...) abort
  162. return map(split(globpath(s:session_dir, '*'.a:lead.'*'), '\n'), 'fnamemodify(v:val, ":t")')
  163. endfunction
  164. " Function: #session_list_as_string {{{1
  165. function! startify#session_list_as_string(lead, ...) abort
  166. return join(map(split(globpath(s:session_dir, '*'.a:lead.'*'), '\n'), 'fnamemodify(v:val, ":t")'), "\n")
  167. endfunction
  168. " Function: #get_separator {{{1
  169. function! startify#get_separator() abort
  170. return !exists('+shellslash') || &shellslash ? '/' : '\'
  171. endfunction
  172. " Function: s:show_dir {{{1
  173. function! s:show_dir(cnt) abort
  174. let cnt = a:cnt
  175. let files = []
  176. if s:show_special
  177. call append('$', '')
  178. endif
  179. for fname in split(glob('.\=*'))
  180. if isdirectory(fname)
  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. enew
  324. if exists('s:marked') && !empty(s:marked)
  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. endfunction
  351. " Function: s:close {{{1
  352. function! s:close() abort
  353. if len(filter(range(0, bufnr('$')), 'buflisted(v:val)'))
  354. if bufloaded(bufnr('#'))
  355. b #
  356. else
  357. bn
  358. endif
  359. else
  360. quit
  361. endif
  362. endfunction
  363. " Function: s:get_index_as_string {{{1
  364. function! s:get_index_as_string(idx) abort
  365. if exists('g:startify_custom_indices')
  366. let listlen = len(g:startify_custom_indices)
  367. return (a:idx < listlen) ? g:startify_custom_indices[a:idx] : string(a:idx - listlen)
  368. else
  369. return string(a:idx)
  370. endif
  371. endfunction
  372. " vim: et sw=2 sts=2