startify.vim 11 KB

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