startify.vim 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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. if !empty(v:oldfiles)
  189. for fname in v:oldfiles
  190. let fullpath = resolve(fnamemodify(fname, ':p'))
  191. " filter duplicates, bookmarks and entries from the skiplist
  192. if has_key(entries, fullpath)
  193. \ || !filereadable(fullpath)
  194. \ || (exists('g:startify_skiplist') && s:is_in_skiplist(fullpath))
  195. \ || (exists('g:startify_bookmarks') && s:is_bookmark(fullpath))
  196. continue
  197. endif
  198. let entries[fullpath] = 1
  199. let index = s:get_index_as_string(cnt)
  200. call append('$', ' ['. index .']'. repeat(' ', (3 - strlen(index))) . fname)
  201. execute 'nnoremap <buffer>' index ':edit' fnameescape(fname) s:cmd
  202. let cnt += 1
  203. let num -= 1
  204. if !num
  205. break
  206. endif
  207. endfor
  208. endif
  209. return cnt
  210. endfunction
  211. " Function: s:show_sessions {{{1
  212. function! s:show_sessions(cnt) abort
  213. let sfiles = split(globpath(s:session_dir, '*'), '\n')
  214. if empty(sfiles)
  215. return a:cnt
  216. endif
  217. let cnt = a:cnt
  218. for i in range(len(sfiles))
  219. let idx = (i + cnt)
  220. let index = s:get_index_as_string(idx)
  221. call append('$', ' ['. index .']'. repeat(' ', (3 - strlen(index))) . fnamemodify(sfiles[i], ':t:r'))
  222. execute 'nnoremap <buffer> '. index .' :source '. fnameescape(sfiles[i]) .'<cr>'
  223. endfor
  224. return idx
  225. endfunction
  226. " Function: s:show_bookmarks {{{1
  227. function! s:show_bookmarks(cnt) abort
  228. let cnt = a:cnt
  229. if exists('g:startify_bookmarks')
  230. for fname in g:startify_bookmarks
  231. let cnt += 1
  232. let index = s:get_index_as_string(cnt)
  233. call append('$', ' ['. index .']'. repeat(' ', (3 - strlen(index))) . fname)
  234. execute 'nnoremap <buffer> '. index .' :edit '. fnameescape(fname) . s:cmd
  235. endfor
  236. endif
  237. return cnt
  238. endfunction
  239. " Function: s:is_in_skiplist {{{1
  240. function! s:is_in_skiplist(arg) abort
  241. for regexp in g:startify_skiplist
  242. if (a:arg =~# regexp)
  243. return 1
  244. endif
  245. endfor
  246. endfunction
  247. " Function: s:is_bookmark {{{1
  248. function! s:is_bookmark(arg) abort
  249. "for foo in filter(map(copy(g:startify_bookmarks), 'resolve(fnamemodify(v:val, ":p"))'), '!isdirectory(v:val)')
  250. for foo in map(filter(copy(g:startify_bookmarks), '!isdirectory(v:val)'), 'resolve(fnamemodify(v:val, ":p"))')
  251. if foo == a:arg
  252. return 1
  253. endif
  254. endfor
  255. endfunction
  256. " Function: s:set_cursor {{{1
  257. function! s:set_cursor() abort
  258. let s:line_old = exists('s:line_new') ? s:line_new : 5
  259. let s:line_new = line('.')
  260. let offset = s:offset_header + 2
  261. if empty(getline(s:line_new))
  262. if (s:line_new > s:line_old)
  263. let s:line_new += 1
  264. call cursor(s:line_new, 5) " going down
  265. else
  266. let s:line_new -= 1
  267. call cursor((s:line_new < offset ? offset : s:line_new), 5) " going up
  268. endif
  269. else
  270. call cursor((s:line_new < offset ? offset : 0), 5) " hold cursor in column
  271. endif
  272. endfunction
  273. " Function: s:set_mark {{{1
  274. "
  275. " Markers are saved in the s:marked dict using the follow format:
  276. " - s:marked[0]: ID (for sorting)
  277. " - s:marked[1]: what the brackets contained before
  278. " - s:marked[2]: the actual path
  279. " - s:marked[3]: type (buffer, split, vsplit)
  280. "
  281. function! s:set_mark(type) abort
  282. if !exists('s:marked')
  283. let s:marked = {}
  284. let s:nmarked = 0
  285. endif
  286. " matches[1]: content between brackets
  287. " matches[2]: path
  288. let matches = matchlist(getline('.'), '\v\[(.*)\]\s+(.*)')
  289. if matches[2] =~ '\V<empty buffer>\|<quit>' || matches[2] =~ '^\w\+$'
  290. return
  291. endif
  292. setlocal modifiable
  293. if matches[1] =~ 'B\|S\|V'
  294. let s:nmarked -= 1
  295. execute 'normal! ci]'. remove(s:marked, line('.'))[1]
  296. else
  297. let s:marked[line('.')] = [s:nmarked, matches[1], matches[2], a:type]
  298. let s:nmarked += 1
  299. execute 'normal! ci]'. repeat(a:type, len(matches[1]))
  300. endif
  301. setlocal nomodifiable nomodified
  302. endfunction
  303. " Function: s:open_buffers {{{1
  304. function! s:open_buffers(cword) abort
  305. if exists('s:marked') && !empty(s:marked)
  306. enew
  307. setlocal nobuflisted
  308. for i in range(len(s:marked))
  309. for val in values(s:marked)
  310. if val[0] == i
  311. if val[3] == 'S'
  312. if line2byte('$') == -1
  313. execute 'edit' val[2]
  314. else
  315. execute 'split' val[2]
  316. endif
  317. elseif val[3] == 'V'
  318. if line2byte('$') == -1
  319. execute 'edit' val[2]
  320. else
  321. execute 'vsplit' val[2]
  322. endif
  323. else
  324. execute 'edit' val[2]
  325. endif
  326. continue
  327. endif
  328. endfor
  329. endfor
  330. else
  331. execute 'normal' a:cword
  332. endif
  333. if exists('s:marked')
  334. unlet s:marked
  335. unlet s:nmarked
  336. endif
  337. endfunction
  338. " Function: s:close {{{1
  339. function! s:close() abort
  340. if len(filter(range(0, bufnr('$')), 'buflisted(v:val)'))
  341. if bufloaded(bufnr('#'))
  342. b #
  343. else
  344. bn
  345. endif
  346. else
  347. quit
  348. endif
  349. endfunction
  350. " Function: s:get_index_as_string {{{1
  351. function! s:get_index_as_string(idx) abort
  352. if exists('g:startify_custom_indices')
  353. let listlen = len(g:startify_custom_indices)
  354. return (a:idx < listlen) ? g:startify_custom_indices[a:idx] : string(a:idx - listlen)
  355. else
  356. return string(a:idx)
  357. endif
  358. endfunction
  359. " vim: et sw=2 sts=2