startify.vim 10 KB

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