startify.vim 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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.5
  5. if exists('g:autoloaded_startify') || &cp
  6. finish
  7. endif
  8. let g:autoloaded_startify = 1
  9. let s:session_dir = resolve(expand(get(g:, 'startify_session_dir',
  10. \ has('win32') ? '$HOME\vimfiles\session' : '~/.vim/session')))
  11. " Function: startify#insane_in_the_membrane {{{1
  12. function! startify#insane_in_the_membrane() abort
  13. if !empty(v:servername) && exists('g:startify_skiplist_server')
  14. for servname in g:startify_skiplist_server
  15. if (servname == v:servername)
  16. return
  17. endif
  18. endfor
  19. endif
  20. setlocal nonumber noswapfile bufhidden=wipe
  21. if (v:version >= 703)
  22. setlocal norelativenumber
  23. endif
  24. setfiletype startify
  25. let special = get(g:, 'startify_enable_special', 1)
  26. let sep = startify#get_separator()
  27. let cnt = 0
  28. if special
  29. call append('$', ' [e] <empty buffer>')
  30. endif
  31. if get(g:, 'startify_show_files', 1) && !empty(v:oldfiles)
  32. let entries = {}
  33. let numfiles = get(g:, 'startify_show_files_number', 10)
  34. if special
  35. call append('$', '')
  36. endif
  37. for fname in v:oldfiles
  38. let expfname = resolve(fnamemodify(fname, ':p'))
  39. " filter duplicates, bookmarks and entries from the skiplist
  40. if has_key(entries, expfname) ||
  41. \ !filereadable(expfname) ||
  42. \ (exists('g:startify_skiplist') && startify#is_in_skiplist(expfname)) ||
  43. \ (exists('g:startify_bookmarks') && startify#is_bookmark(expfname))
  44. continue
  45. endif
  46. let entries[expfname] = 1
  47. let index = startify#get_index_as_string(cnt)
  48. call append('$', ' ['. index .']'. repeat(' ', (3 - strlen(index))) . fname)
  49. execute 'nnoremap <buffer> '. index .' :edit '. startify#escape(fname) .' <bar> lcd %:h<cr>'
  50. let cnt += 1
  51. if (cnt == numfiles)
  52. break
  53. endif
  54. endfor
  55. endif
  56. let sfiles = split(globpath(s:session_dir, '*'), '\n')
  57. if get(g:, 'startify_show_sessions', 1) && !empty(sfiles)
  58. call append('$', '')
  59. for i in range(len(sfiles))
  60. let idx = (i + cnt)
  61. let index = startify#get_index_as_string(idx)
  62. call append('$', ' ['. index .']'. repeat(' ', (3 - strlen(index))) . fnamemodify(sfiles[i], ':t:r'))
  63. execute 'nnoremap <buffer> '. index .' :bd <bar> tabnew +source\ '. startify#escape(sfiles[i]) .' <bar> if (line2byte("$") == -1) <bar> close <bar> endif<cr>'
  64. endfor
  65. let cnt = idx
  66. endif
  67. if exists('g:startify_bookmarks')
  68. call append('$', '')
  69. for fname in g:startify_bookmarks
  70. let cnt += 1
  71. let index = startify#get_index_as_string(cnt)
  72. call append('$', ' ['. index .']'. repeat(' ', (3 - strlen(index))) . fname)
  73. execute 'nnoremap <buffer> '. index .' :edit '. startify#escape(fname) .' <bar> lcd %:h<cr>'
  74. endfor
  75. endif
  76. if special
  77. call append('$', ['', ' [q] <quit>'])
  78. endif
  79. setlocal nomodifiable nomodified
  80. nnoremap <buffer><silent> e :enew<cr>
  81. nnoremap <buffer><silent> i :enew <bar> startinsert<cr>
  82. nnoremap <buffer><silent> <space> :call <SID>set_mark('B')<cr>
  83. nnoremap <buffer><silent> s :call <SID>set_mark('S')<cr>
  84. nnoremap <buffer><silent> v :call <SID>set_mark('V')<cr>
  85. nnoremap <buffer> <cr> :call <SID>open_buffers(expand('<cword>'))<cr>
  86. nnoremap <buffer> <2-LeftMouse> :execute 'normal '. matchstr(getline('.'), '\w\+')<cr>
  87. nnoremap <buffer> q
  88. \ :if (len(filter(range(0, bufnr('$')), 'buflisted(v:val)')) > 1) <bar>
  89. \ bd <bar>
  90. \ else <bar>
  91. \ quit <bar>
  92. \ endif<cr>
  93. if exists('g:startify_empty_buffer_key')
  94. execute 'nnoremap <buffer><silent> '. g:startify_empty_buffer_key .' :enew<cr>'
  95. endif
  96. autocmd! startify *
  97. autocmd startify CursorMoved <buffer> call startify#set_cursor()
  98. autocmd startify BufLeave <buffer> autocmd! startify *
  99. call cursor(special ? 4 : 2, 5)
  100. endfunction
  101. " Function: startify#get_session_names {{{1
  102. function! startify#get_session_names(lead, ...) abort
  103. return map(split(globpath(s:session_dir, '*'.a:lead.'*', '\n')), 'fnamemodify(v:val, ":t")')
  104. endfunction
  105. " Function: startify#get_session_names_as_string {{{1
  106. function! startify#get_session_names_as_string(lead, ...) abort
  107. return join(map(split(globpath(s:session_dir, '*'.a:lead.'*', '\n')), 'fnamemodify(v:val, ":t")'), "\n")
  108. endfunction
  109. " Function: startify#escape {{{1
  110. function! startify#escape(path) abort
  111. return !exists('+shellslash') || &shellslash ? fnameescape(a:path) : escape(a:path, '\')
  112. endfunction
  113. " Function: startify#get_separator {{{1
  114. function! startify#get_separator() abort
  115. return !exists('+shellslash') || &shellslash ? '/' : '\'
  116. endfunction
  117. " Function: startify#is_in_skiplist {{{1
  118. function! startify#is_in_skiplist(arg) abort
  119. for regexp in g:startify_skiplist
  120. if (a:arg =~# regexp)
  121. return 1
  122. endif
  123. endfor
  124. endfunction
  125. " Function: startify#is_bookmark {{{1
  126. function! startify#is_bookmark(arg) abort
  127. "for foo in filter(map(copy(g:startify_bookmarks), 'resolve(fnamemodify(v:val, ":p"))'), '!isdirectory(v:val)')
  128. for foo in map(filter(copy(g:startify_bookmarks), '!isdirectory(v:val)'), 'resolve(fnamemodify(v:val, ":p"))')
  129. if foo == a:arg
  130. return 1
  131. endif
  132. endfor
  133. endfunction
  134. " Function: startify#open_buffers {{{1
  135. function! startify#open_buffers(cword) abort
  136. if exists('s:marked') && !empty(s:marked)
  137. for i in range(len(s:marked))
  138. for val in values(s:marked)
  139. if val[0] == i
  140. if val[3] == 'S'
  141. execute 'split '. val[2]
  142. elseif val[3] == 'V'
  143. execute 'vsplit '. val[2]
  144. else
  145. execute 'edit '. val[2]
  146. endif
  147. continue
  148. endif
  149. endfor
  150. endfor
  151. else
  152. execute 'normal '. a:cword
  153. endif
  154. endfunction
  155. " Function: startify#delete_session {{{1
  156. function! startify#delete_session(...) abort
  157. if !isdirectory(s:session_dir)
  158. echo 'The session directory does not exist: '. s:session_dir
  159. return
  160. elseif empty(startify#get_session_names_as_string(''))
  161. echo 'There are no sessions...'
  162. return
  163. endif
  164. let spath = s:session_dir . startify#get_separator() . (exists('a:1')
  165. \ ? a:1
  166. \ : input('Delete this session: ', fnamemodify(v:this_session, ':t'), 'custom,startify#get_session_names_as_string'))
  167. \ | redraw
  168. echo 'Really delete '. spath .'? [y/n]' | redraw
  169. if (nr2char(getchar()) == 'y')
  170. if delete(spath) == 0
  171. echo 'Deleted session '. spath .'!'
  172. else
  173. echo 'Deletion failed!'
  174. endif
  175. else
  176. echo 'Deletion aborted!'
  177. endif
  178. endfunction
  179. " Function: startify#save_session {{{1
  180. function! startify#save_session(...) abort
  181. if !isdirectory(s:session_dir)
  182. if exists('*mkdir')
  183. echo 'The session directory does not exist: '. s:session_dir .'. Create it? [y/n]' | redraw
  184. if (nr2char(getchar()) == 'y')
  185. call mkdir(s:session_dir, 'p')
  186. else
  187. echo
  188. return
  189. endif
  190. else
  191. echo 'The session directory does not exist: '. s:session_dir
  192. return
  193. endif
  194. endif
  195. let spath = s:session_dir . startify#get_separator() . (exists('a:1')
  196. \ ? a:1
  197. \ : input('Save under this session name: ', fnamemodify(v:this_session, ':t'), 'custom,startify#get_session_names_as_string'))
  198. \ | redraw
  199. let spath = startify#escape(spath)
  200. if !filereadable(spath)
  201. execute 'mksession '. spath | echo 'Session saved under: '. spath
  202. return
  203. endif
  204. echo 'Session already exists. Overwrite? [y/n]' | redraw
  205. if nr2char(getchar()) == 'y'
  206. execute 'mksession! '. spath | echo 'Session saved under: '. spath
  207. else
  208. echo 'Did NOT save the session!'
  209. endif
  210. endfunction
  211. " Function: startify#load_session {{{1
  212. function! startify#load_session(...) abort
  213. if !isdirectory(s:session_dir)
  214. echo 'The session directory does not exist: '. s:session_dir
  215. return
  216. elseif empty(startify#get_session_names_as_string(''))
  217. echo 'There are no sessions...'
  218. return
  219. endif
  220. let spath = s:session_dir . startify#get_separator() . (exists('a:1')
  221. \ ? a:1
  222. \ : input('Load this session: ', fnamemodify(v:this_session, ':t'), 'custom,startify#get_session_names_as_string'))
  223. \ | redraw
  224. if filereadable(spath)
  225. execute 'source '. startify#escape(spath)
  226. else
  227. echo 'No such file: '. spath
  228. endif
  229. endfunction
  230. " Function: startify#set_mark {{{1
  231. "
  232. " Markers are saved in the s:marked dict using the follow format:
  233. " - s:marked[0]: ID (for sorting)
  234. " - s:marked[1]: what the brackets contained before
  235. " - s:marked[2]: the actual path
  236. " - s:marked[3]: type (buffer, split, vsplit)
  237. "
  238. function! startify#set_mark(type) abort
  239. if !exists('s:marked')
  240. let s:marked = {}
  241. let s:nmarked = 0
  242. endif
  243. " matches[1]: content between brackets
  244. " matches[2]: path
  245. let matches = matchlist(getline('.'), '\v\[(.*)\]\s+(.*)')
  246. if matches[2] =~ '\V<empty buffer>\|<quit>' || matches[2] =~ '^\w\+$'
  247. return
  248. endif
  249. setlocal modifiable
  250. if matches[1] =~ 'B\|S\|V'
  251. let s:nmarked -= 1
  252. execute 'normal! ci]'. remove(s:marked, line('.'))[1]
  253. else
  254. let s:marked[line('.')] = [s:nmarked, matches[1], matches[2], a:type]
  255. let s:nmarked += 1
  256. execute 'normal! ci]'. repeat(a:type, len(matches[1]))
  257. endif
  258. setlocal nomodifiable nomodified
  259. endfunction
  260. " Function: startify#get_index_as_string {{{1
  261. function! startify#get_index_as_string(idx) abort
  262. if exists('g:startify_custom_indices')
  263. let listlen = len(g:startify_custom_indices)
  264. return (a:idx < listlen) ? g:startify_custom_indices[a:idx] : string(a:idx - listlen)
  265. else
  266. return string(a:idx)
  267. endif
  268. endfunction
  269. " Function: startify#set_cursor {{{1
  270. function! startify#set_cursor() abort
  271. let s:line_old = exists('s:line_new') ? s:line_new : 5
  272. let s:line_new = line('.')
  273. if empty(getline(s:line_new))
  274. if (s:line_new > s:line_old)
  275. let s:line_new += 1
  276. call cursor(s:line_new, 5) " going down
  277. else
  278. let s:line_new -= 1
  279. call cursor((s:line_new < 2 ? 2 : s:line_new), 5) " going up
  280. endif
  281. else
  282. call cursor((s:line_new < 2 ? 2 : 0), 5) " hold cursor in column
  283. endif
  284. endfunction
  285. " vim: et sw=2 sts=2