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 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. call cursor((s:show_special ? 4 : 2) + s:offset_header, 5)
  76. endfunction
  77. " Function: #session_load {{{1
  78. function! startify#session_load(...) abort
  79. if !isdirectory(s:session_dir)
  80. echo 'The session directory does not exist: '. s:session_dir
  81. return
  82. elseif empty(startify#session_list_as_string(''))
  83. echo 'There are no sessions...'
  84. return
  85. endif
  86. let spath = s:session_dir . startify#get_separator() . (exists('a:1')
  87. \ ? a:1
  88. \ : input('Load this session: ', fnamemodify(v:this_session, ':t'), 'custom,startify#session_list_as_string'))
  89. \ | redraw
  90. if filereadable(spath)
  91. execute 'source '. fnameescape(spath)
  92. else
  93. echo 'No such file: '. spath
  94. endif
  95. endfunction
  96. " Function: #session_save {{{1
  97. function! startify#session_save(...) abort
  98. if !isdirectory(s:session_dir)
  99. if exists('*mkdir')
  100. echo 'The session directory does not exist: '. s:session_dir .'. Create it? [y/n]' | redraw
  101. if (nr2char(getchar()) == 'y')
  102. call mkdir(s:session_dir, 'p')
  103. else
  104. echo
  105. return
  106. endif
  107. else
  108. echo 'The session directory does not exist: '. s:session_dir
  109. return
  110. endif
  111. endif
  112. if exists('a:1')
  113. let sname = a:1
  114. else
  115. let sname = input('Save under this session name: ', fnamemodify(v:this_session, ':t'), 'custom,startify#session_list_as_string')
  116. redraw
  117. if empty(sname)
  118. echo 'You gave an empty name!'
  119. return
  120. endif
  121. endif
  122. let spath = s:session_dir . startify#get_separator() . sname
  123. if !filereadable(spath)
  124. execute 'mksession '. fnameescape(spath) | echo 'Session saved under: '. spath
  125. return
  126. endif
  127. echo 'Session already exists. Overwrite? [y/n]' | redraw
  128. if nr2char(getchar()) == 'y'
  129. execute 'mksession! '. fnameescape(spath) | echo 'Session saved under: '. spath
  130. else
  131. echo 'Did NOT save the session!'
  132. endif
  133. endfunction
  134. " Function: #session_delete {{{1
  135. function! startify#session_delete(...) abort
  136. if !isdirectory(s:session_dir)
  137. echo 'The session directory does not exist: '. s:session_dir
  138. return
  139. elseif empty(startify#session_list_as_string(''))
  140. echo 'There are no sessions...'
  141. return
  142. endif
  143. let spath = s:session_dir . startify#get_separator() . (exists('a:1')
  144. \ ? a:1
  145. \ : input('Delete this session: ', fnamemodify(v:this_session, ':t'), 'custom,startify#session_list_as_string'))
  146. \ | redraw
  147. echo 'Really delete '. spath .'? [y/n]' | redraw
  148. if (nr2char(getchar()) == 'y')
  149. if delete(spath) == 0
  150. echo 'Deleted session '. spath .'!'
  151. else
  152. echo 'Deletion failed!'
  153. endif
  154. else
  155. echo 'Deletion aborted!'
  156. endif
  157. endfunction
  158. " Function: #session_list {{{1
  159. function! startify#session_list(lead, ...) abort
  160. return map(split(globpath(s:session_dir, '*'.a:lead.'*'), '\n'), 'fnamemodify(v:val, ":t")')
  161. endfunction
  162. " Function: #session_list_as_string {{{1
  163. function! startify#session_list_as_string(lead, ...) abort
  164. return join(map(split(globpath(s:session_dir, '*'.a:lead.'*'), '\n'), 'fnamemodify(v:val, ":t")'), "\n")
  165. endfunction
  166. " Function: #get_separator {{{1
  167. function! startify#get_separator() abort
  168. return !exists('+shellslash') || &shellslash ? '/' : '\'
  169. endfunction
  170. " Function: s:show_dir {{{1
  171. function! s:show_dir(cnt) abort
  172. let cnt = a:cnt
  173. let files = []
  174. if s:show_special
  175. call append('$', '')
  176. endif
  177. for fname in split(glob('.\=*'))
  178. if isdirectory(fname)
  179. continue
  180. endif
  181. call add(files, [getftime(fname), fname])
  182. endfor
  183. function! l:compare(x, y)
  184. return a:y[0] - a:x[0]
  185. endfunction
  186. call sort(files, 'l:compare')
  187. for items in files
  188. let index = s:get_index_as_string(cnt)
  189. let fname = items[1]
  190. call append('$', ' ['. index .']'. repeat(' ', (3 - strlen(index))) . fname)
  191. execute 'nnoremap <buffer>' index ':edit' fnameescape(fname) '<cr>'
  192. let cnt += 1
  193. if (cnt == s:numfiles)
  194. break
  195. endif
  196. endfor
  197. return cnt
  198. endfunction
  199. " Function: s:show_files {{{1
  200. function! s:show_files(cnt) abort
  201. let cnt = a:cnt
  202. let num = s:numfiles
  203. let entries = {}
  204. if s:show_special || s:show_dir
  205. call append('$', '')
  206. endif
  207. for fname in v:oldfiles
  208. let expfname = resolve(fnamemodify(fname, ':p'))
  209. " filter duplicates, bookmarks and entries from the skiplist
  210. if has_key(entries, expfname)
  211. \ || !filereadable(expfname)
  212. \ || (exists('g:startify_skiplist') && s:is_in_skiplist(expfname))
  213. \ || (exists('g:startify_bookmarks') && s:is_bookmark(expfname))
  214. continue
  215. endif
  216. let entries[expfname] = 1
  217. let index = s:get_index_as_string(cnt)
  218. call append('$', ' ['. index .']'. repeat(' ', (3 - strlen(index))) . fname)
  219. execute 'nnoremap <buffer>' index ':edit' fnameescape(fname) s:cmd
  220. let cnt += 1
  221. let num -= 1
  222. if !num
  223. break
  224. endif
  225. endfor
  226. return cnt
  227. endfunction
  228. " Function: s:show_sessions {{{1
  229. function! s:show_sessions(sfiles, cnt) abort
  230. let cnt = a:cnt
  231. if s:show_special || s:show_dir || s:show_files
  232. call append('$', '')
  233. endif
  234. for i in range(len(a:sfiles))
  235. let idx = (i + cnt)
  236. let index = s:get_index_as_string(idx)
  237. call append('$', ' ['. index .']'. repeat(' ', (3 - strlen(index))) . fnamemodify(a:sfiles[i], ':t:r'))
  238. execute 'nnoremap <buffer> '. index .' :source '. fnameescape(a:sfiles[i]) .'<cr>'
  239. endfor
  240. return idx
  241. endfunction
  242. " Function: s:show_bookmarks {{{1
  243. function! s:show_bookmarks(cnt) abort
  244. let cnt = a:cnt
  245. if s:show_special || s:show_dir || s:show_files || s:show_sessions
  246. call append('$', '')
  247. endif
  248. for fname in g:startify_bookmarks
  249. let cnt += 1
  250. let index = s:get_index_as_string(cnt)
  251. call append('$', ' ['. index .']'. repeat(' ', (3 - strlen(index))) . fname)
  252. execute 'nnoremap <buffer> '. index .' :edit '. fnameescape(fname) . s:cmd
  253. endfor
  254. endfunction
  255. " Function: s:is_in_skiplist {{{1
  256. function! s:is_in_skiplist(arg) abort
  257. for regexp in g:startify_skiplist
  258. if (a:arg =~# regexp)
  259. return 1
  260. endif
  261. endfor
  262. endfunction
  263. " Function: s:is_bookmark {{{1
  264. function! s:is_bookmark(arg) abort
  265. "for foo in filter(map(copy(g:startify_bookmarks), 'resolve(fnamemodify(v:val, ":p"))'), '!isdirectory(v:val)')
  266. for foo in map(filter(copy(g:startify_bookmarks), '!isdirectory(v:val)'), 'resolve(fnamemodify(v:val, ":p"))')
  267. if foo == a:arg
  268. return 1
  269. endif
  270. endfor
  271. endfunction
  272. " Function: s:set_cursor {{{1
  273. function! s:set_cursor() abort
  274. let s:line_old = exists('s:line_new') ? s:line_new : 5
  275. let s:line_new = line('.')
  276. let offset = s:offset_header + 2
  277. if empty(getline(s:line_new))
  278. if (s:line_new > s:line_old)
  279. let s:line_new += 1
  280. call cursor(s:line_new, 5) " going down
  281. else
  282. let s:line_new -= 1
  283. call cursor((s:line_new < offset ? offset : s:line_new), 5) " going up
  284. endif
  285. else
  286. call cursor((s:line_new < offset ? offset : 0), 5) " hold cursor in column
  287. endif
  288. endfunction
  289. " Function: s:set_mark {{{1
  290. "
  291. " Markers are saved in the s:marked dict using the follow format:
  292. " - s:marked[0]: ID (for sorting)
  293. " - s:marked[1]: what the brackets contained before
  294. " - s:marked[2]: the actual path
  295. " - s:marked[3]: type (buffer, split, vsplit)
  296. "
  297. function! s:set_mark(type) abort
  298. if !exists('s:marked')
  299. let s:marked = {}
  300. let s:nmarked = 0
  301. endif
  302. " matches[1]: content between brackets
  303. " matches[2]: path
  304. let matches = matchlist(getline('.'), '\v\[(.*)\]\s+(.*)')
  305. if matches[2] =~ '\V<empty buffer>\|<quit>' || matches[2] =~ '^\w\+$'
  306. return
  307. endif
  308. setlocal modifiable
  309. if matches[1] =~ 'B\|S\|V'
  310. let s:nmarked -= 1
  311. execute 'normal! ci]'. remove(s:marked, line('.'))[1]
  312. else
  313. let s:marked[line('.')] = [s:nmarked, matches[1], matches[2], a:type]
  314. let s:nmarked += 1
  315. execute 'normal! ci]'. repeat(a:type, len(matches[1]))
  316. endif
  317. setlocal nomodifiable nomodified
  318. endfunction
  319. " Function: s:open_buffers {{{1
  320. function! s:open_buffers(cword) abort
  321. if exists('s:marked') && !empty(s:marked)
  322. enew
  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