startify.vim 12 KB

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