startify.vim 12 KB

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