startify.vim 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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.7
  5. if exists('g:autoloaded_startify') || &compatible
  6. finish
  7. endif
  8. let g:autoloaded_startify = 1
  9. " Init: values {{{1
  10. let s:numfiles = get(g:, 'startify_files_number', 10)
  11. let s:show_special = get(g:, 'startify_enable_special', 1)
  12. let s:restore_position = get(g:, 'startify_restore_position')
  13. let s:session_dir = resolve(expand(get(g:, 'startify_session_dir',
  14. \ has('win32') ? '$HOME\vimfiles\session' : '~/.vim/session')))
  15. " Init: autocmds {{{1
  16. if get(g:, 'startify_session_persistence')
  17. autocmd startify VimLeave *
  18. \ if exists('v:this_session') && filewritable(v:this_session) |
  19. \ execute 'mksession!' fnameescape(v:this_session) |
  20. \ endif
  21. endif
  22. " Function: #get_separator {{{1
  23. function! startify#get_separator() abort
  24. return !exists('+shellslash') || &shellslash ? '/' : '\'
  25. endfunction
  26. let s:sep = startify#get_separator()
  27. " Function: #insane_in_the_membrane {{{1
  28. function! startify#insane_in_the_membrane() abort
  29. if !empty(v:servername) && exists('g:startify_skiplist_server')
  30. for servname in g:startify_skiplist_server
  31. if servname == v:servername
  32. return
  33. endif
  34. endfor
  35. endif
  36. setlocal noswapfile nobuflisted buftype=nofile bufhidden=wipe
  37. setlocal nonumber nolist statusline=\ startify
  38. setfiletype startify
  39. if v:version >= 703
  40. setlocal norelativenumber
  41. endif
  42. let cnt = 0
  43. let s:headoff = 0
  44. if exists('g:startify_custom_header')
  45. call append('$', g:startify_custom_header)
  46. let s:headoff += len(g:startify_custom_header)
  47. endif
  48. if s:show_special
  49. call append('$', [' [e] <empty buffer>', ''])
  50. endif
  51. if get(g:, 'startify_session_detection', 1) && filereadable('Session.vim')
  52. call append('$', [' [0] '. getcwd() . s:sep .'Session.vim', ''])
  53. execute 'nnoremap <buffer> 0 :source Session.vim<cr>'
  54. let cnt = 1
  55. endif
  56. for list in get(g:, 'startify_list_order', ['files', 'sessions', 'bookmarks'])
  57. let cnt = s:show_{list}(cnt)
  58. endfor
  59. $delete
  60. if s:show_special
  61. call append('$', ['', ' [q] <quit>'])
  62. endif
  63. let s:lastline = line('$')
  64. if exists('g:startify_custom_footer')
  65. call append('$', g:startify_custom_footer)
  66. endif
  67. setlocal nomodifiable nomodified
  68. nnoremap <buffer><silent> e :enew<cr>
  69. nnoremap <buffer><silent> i :enew <bar> startinsert<cr>
  70. nnoremap <buffer><silent> <insert> :enew <bar> startinsert<cr>
  71. nnoremap <buffer><silent> b :call <sid>set_mark('B')<cr>
  72. nnoremap <buffer><silent> s :call <sid>set_mark('S')<cr>
  73. nnoremap <buffer><silent> t :call <sid>set_mark('T')<cr>
  74. nnoremap <buffer><silent> v :call <sid>set_mark('V')<cr>
  75. nnoremap <buffer> <cr> :call <sid>open_buffers(expand('<cword>'))<cr>
  76. nnoremap <buffer> <2-LeftMouse> :execute 'normal' matchstr(getline('.'), '\w\+')<cr>
  77. nnoremap <buffer><silent> q :call <sid>close()<cr>
  78. if exists('g:startify_empty_buffer_key')
  79. execute 'nnoremap <buffer><silent> '. g:startify_empty_buffer_key .' :enew<cr>'
  80. endif
  81. autocmd startify CursorMoved <buffer> call s:set_cursor()
  82. if s:restore_position
  83. autocmd startify BufReadPost * call s:restore_position()
  84. endif
  85. 1
  86. call cursor((s:show_special ? 4 : 2) + s:headoff, 5)
  87. endfunction
  88. " Function: #session_load {{{1
  89. function! startify#session_load(...) abort
  90. if !isdirectory(s:session_dir)
  91. echo 'The session directory does not exist: '. s:session_dir
  92. return
  93. elseif empty(startify#session_list_as_string(''))
  94. echo 'There are no sessions...'
  95. return
  96. endif
  97. let spath = s:session_dir . s:sep . (exists('a:1')
  98. \ ? a:1
  99. \ : input('Load this session: ', fnamemodify(v:this_session, ':t'), 'custom,startify#session_list_as_string'))
  100. \ | redraw
  101. if filereadable(spath)
  102. execute 'source '. fnameescape(spath)
  103. else
  104. echo 'No such file: '. spath
  105. endif
  106. endfunction
  107. " Function: #session_save {{{1
  108. function! startify#session_save(...) abort
  109. if !isdirectory(s:session_dir)
  110. if exists('*mkdir')
  111. echo 'The session directory does not exist: '. s:session_dir .'. Create it? [y/n]'
  112. if (nr2char(getchar()) == 'y')
  113. call mkdir(s:session_dir, 'p')
  114. else
  115. echo
  116. return
  117. endif
  118. else
  119. echo 'The session directory does not exist: '. s:session_dir
  120. return
  121. endif
  122. endif
  123. if exists('a:1')
  124. let sname = a:1
  125. else
  126. let sname = input('Save under this session name: ', fnamemodify(v:this_session, ':t'), 'custom,startify#session_list_as_string')
  127. redraw
  128. if empty(sname)
  129. echo 'You gave an empty name!'
  130. return
  131. endif
  132. endif
  133. let spath = s:session_dir . s:sep . sname
  134. if !filereadable(spath)
  135. execute 'mksession '. fnameescape(spath) | echo 'Session saved under: '. spath
  136. return
  137. endif
  138. echo 'Session already exists. Overwrite? [y/n]' | redraw
  139. if nr2char(getchar()) == 'y'
  140. execute 'mksession! '. fnameescape(spath) | echo 'Session saved under: '. spath
  141. else
  142. echo 'Did NOT save the session!'
  143. endif
  144. endfunction
  145. " Function: #session_delete {{{1
  146. function! startify#session_delete(...) abort
  147. if !isdirectory(s:session_dir)
  148. echo 'The session directory does not exist: '. s:session_dir
  149. return
  150. elseif empty(startify#session_list_as_string(''))
  151. echo 'There are no sessions...'
  152. return
  153. endif
  154. let spath = s:session_dir . s:sep . (exists('a:1')
  155. \ ? a:1
  156. \ : input('Delete this session: ', fnamemodify(v:this_session, ':t'), 'custom,startify#session_list_as_string'))
  157. \ | redraw
  158. echo 'Really delete '. spath .'? [y/n]' | redraw
  159. if (nr2char(getchar()) == 'y')
  160. if delete(spath) == 0
  161. echo 'Deleted session '. spath .'!'
  162. else
  163. echo 'Deletion failed!'
  164. endif
  165. else
  166. echo 'Deletion aborted!'
  167. endif
  168. endfunction
  169. " Function: #session_list {{{1
  170. function! startify#session_list(lead, ...) abort
  171. return map(split(globpath(s:session_dir, '*'.a:lead.'*'), '\n'), 'fnamemodify(v:val, ":t")')
  172. endfunction
  173. " Function: #session_list_as_string {{{1
  174. function! startify#session_list_as_string(lead, ...) abort
  175. return join(map(split(globpath(s:session_dir, '*'.a:lead.'*'), '\n'), 'fnamemodify(v:val, ":t")'), "\n")
  176. endfunction
  177. " Function: s:show_dir {{{1
  178. function! s:show_dir(cnt) abort
  179. let cnt = a:cnt
  180. let num = s:numfiles
  181. let files = []
  182. for fname in split(glob('.\=*'))
  183. if isdirectory(fname)
  184. \ || (exists('g:startify_skiplist') && s:is_in_skiplist(resolve(fnamemodify(fname, ':p'))))
  185. continue
  186. endif
  187. call add(files, [getftime(fname), fname])
  188. endfor
  189. function! l:compare(x, y)
  190. return a:y[0] - a:x[0]
  191. endfunction
  192. call sort(files, 'l:compare')
  193. for items in files
  194. let index = s:get_index_as_string(cnt)
  195. let fname = items[1]
  196. call append('$', ' ['. index .']'. repeat(' ', (3 - strlen(index))) . fname)
  197. execute 'nnoremap <buffer>' index ':edit' fnameescape(fname) '<cr>'
  198. let cnt += 1
  199. let num -= 1
  200. if !num
  201. break
  202. endif
  203. endfor
  204. if !empty(files)
  205. call append('$', '')
  206. endif
  207. return cnt
  208. endfunction
  209. " Function: s:show_files {{{1
  210. function! s:show_files(cnt) abort
  211. let cnt = a:cnt
  212. let num = s:numfiles
  213. let entries = {}
  214. if !empty(v:oldfiles)
  215. for fname in v:oldfiles
  216. let fullpath = resolve(fnamemodify(fname, ':p'))
  217. " filter duplicates, bookmarks and entries from the skiplist
  218. if has_key(entries, fullpath)
  219. \ || !filereadable(fullpath)
  220. \ || (exists('g:startify_skiplist') && s:is_in_skiplist(fullpath))
  221. \ || (exists('g:startify_bookmarks') && s:is_bookmark(fullpath))
  222. continue
  223. endif
  224. let entries[fullpath] = 1
  225. let index = s:get_index_as_string(cnt)
  226. call append('$', ' ['. index .']'. repeat(' ', (3 - strlen(index))) . fname)
  227. execute 'nnoremap <buffer>' index ':edit' fnameescape(fname) '<bar> call <sid>check_user_options()<cr>'
  228. let cnt += 1
  229. let num -= 1
  230. if !num
  231. break
  232. endif
  233. endfor
  234. call append('$', '')
  235. return cnt
  236. endif
  237. endfunction
  238. " Function: s:show_sessions {{{1
  239. function! s:show_sessions(cnt) abort
  240. let sfiles = split(globpath(s:session_dir, '*'), '\n')
  241. let slen = len(sfiles)
  242. if empty(sfiles)
  243. return a:cnt
  244. endif
  245. let cnt = a:cnt
  246. for i in range(slen)
  247. let idx = (i + cnt)
  248. let index = s:get_index_as_string(idx)
  249. call append('$', ' ['. index .']'. repeat(' ', (3 - strlen(index))) . fnamemodify(sfiles[i], ':t:r'))
  250. execute 'nnoremap <buffer>' index ':source' fnameescape(sfiles[i]) '<cr>'
  251. endfor
  252. call append('$', '')
  253. return idx + 1
  254. endfunction
  255. " Function: s:show_bookmarks {{{1
  256. function! s:show_bookmarks(cnt) abort
  257. let cnt = a:cnt
  258. if exists('g:startify_bookmarks')
  259. for fname in g:startify_bookmarks
  260. let index = s:get_index_as_string(cnt)
  261. call append('$', ' ['. index .']'. repeat(' ', (3 - strlen(index))) . fname)
  262. execute 'nnoremap <buffer>' index ':edit' fnameescape(fname) '<bar> call <sid>check_user_options()<cr>'
  263. let cnt += 1
  264. endfor
  265. call append('$', '')
  266. endif
  267. return cnt
  268. endfunction
  269. " Function: s:is_in_skiplist {{{1
  270. function! s:is_in_skiplist(arg) abort
  271. for regexp in g:startify_skiplist
  272. if (a:arg =~# regexp)
  273. return 1
  274. endif
  275. endfor
  276. endfunction
  277. " Function: s:is_bookmark {{{1
  278. function! s:is_bookmark(arg) abort
  279. "for foo in filter(map(copy(g:startify_bookmarks), 'resolve(fnamemodify(v:val, ":p"))'), '!isdirectory(v:val)')
  280. for foo in map(filter(copy(g:startify_bookmarks), '!isdirectory(v:val)'), 'resolve(fnamemodify(v:val, ":p"))')
  281. if foo == a:arg
  282. return 1
  283. endif
  284. endfor
  285. endfunction
  286. " Function: s:set_cursor {{{1
  287. function! s:set_cursor() abort
  288. let s:oldline = exists('s:newline') ? s:newline : 5
  289. let s:newline = line('.')
  290. let headoff = s:headoff + 2
  291. echom 'newline: '. s:newline
  292. " going down
  293. if s:newline > s:oldline
  294. if empty(getline(s:newline))
  295. let s:newline += 1
  296. endif
  297. if s:newline > s:lastline
  298. call cursor(headoff, 5)
  299. let s:newline = headoff
  300. else
  301. call cursor(s:newline, 5)
  302. endif
  303. " going up
  304. elseif s:newline < s:oldline
  305. if empty(getline(s:newline))
  306. let s:newline -= 1
  307. endif
  308. if s:newline < headoff
  309. call cursor(s:lastline, 5)
  310. let s:newline = s:lastline
  311. else
  312. call cursor(s:newline, 5)
  313. endif
  314. " hold cursor in column
  315. else
  316. call cursor(s:newline, 5)
  317. endif
  318. endfunction
  319. " Function: s:set_mark {{{1
  320. "
  321. " Markers are saved in the s:marked dict using the follow format:
  322. " - s:marked[0]: ID
  323. " - s:marked[1]: path
  324. " - s:marked[2]: type (buffer, split, vsplit)
  325. "
  326. function! s:set_mark(type) abort
  327. if !exists('s:marked')
  328. let s:marked = {}
  329. endif
  330. let [id, path] = matchlist(getline('.'), '\v\[(.*)\]\s+(.*)')[1:2]
  331. if path =~# '\V<empty buffer>\|<quit>' || path =~# '^\w\+$'
  332. return
  333. endif
  334. setlocal modifiable
  335. " set markers
  336. if id =~# '[BSTV]'
  337. " replace marker by old ID
  338. execute 'normal! ci]'. remove(s:marked, line('.'))[0]
  339. else
  340. " save ID and replace it by the marker of the given type
  341. let s:marked[line('.')] = [id, path, a:type]
  342. execute 'normal! ci]'. repeat(a:type, len(id))
  343. endif
  344. setlocal nomodifiable nomodified
  345. endfunction
  346. " Function: s:open_buffers {{{1
  347. function! s:open_buffers(cword) abort
  348. " markers found; open one or more buffers
  349. if exists('s:marked') && !empty(s:marked)
  350. enew
  351. setlocal nobuflisted
  352. for val in values(s:marked)
  353. let [path, type] = val[1:2]
  354. if line2byte('$') == -1
  355. " open in current window
  356. execute 'edit' path
  357. elseif type == 'S'
  358. " open in split
  359. execute 'split' path
  360. elseif type == 'V'
  361. " open in vsplit
  362. execute 'vsplit' path
  363. elseif type == 'T'
  364. " open in tab
  365. execute 'tabnew' path
  366. else
  367. " open in current window
  368. execute 'edit' path
  369. endif
  370. call s:check_user_options()
  371. endfor
  372. " remove markers for next instance of :Startify
  373. if exists('s:marked')
  374. unlet s:marked
  375. endif
  376. " no markers found; open a single buffer
  377. else
  378. execute 'normal' a:cword
  379. endif
  380. endfunction
  381. " Function: s:check_user_options {{{1
  382. function! s:check_user_options() abort
  383. let path = expand('%')
  384. let session = path . s:sep .'Session.vim'
  385. " autoload session
  386. if get(g:, 'startify_session_autoload') && filereadable(session)
  387. execute 'source' session
  388. " change to VCS root directory
  389. elseif get(g:, 'startify_change_to_vcs_root')
  390. call s:cd_to_vcs_root(path)
  391. " change directory
  392. elseif get(g:, 'startify_change_to_dir', 1)
  393. if isdirectory(path)
  394. lcd %
  395. else
  396. lcd %:h
  397. endif
  398. endif
  399. endfunction
  400. " Function: s:cd_to_vcs_root {{{1
  401. function! s:cd_to_vcs_root(path) abort
  402. let dir = fnamemodify(a:path, ':p:h')
  403. for vcs in [ '.git', '.hg', '.bzr', '.svn' ]
  404. let root = finddir(vcs, dir .';')
  405. if !empty(root)
  406. execute 'cd '. fnamemodify(root, ':h')
  407. return
  408. endif
  409. endfor
  410. endfunction
  411. " Function: s:close {{{1
  412. function! s:close() abort
  413. if len(filter(range(0, bufnr('$')), 'buflisted(v:val)'))
  414. if bufloaded(bufnr('#'))
  415. b #
  416. else
  417. bn
  418. endif
  419. else
  420. quit
  421. endif
  422. endfunction
  423. " Function: s:get_index_as_string {{{1
  424. function! s:get_index_as_string(idx) abort
  425. if exists('g:startify_custom_indices')
  426. let listlen = len(g:startify_custom_indices)
  427. return (a:idx < listlen) ? g:startify_custom_indices[a:idx] : string(a:idx - listlen)
  428. else
  429. return string(a:idx)
  430. endif
  431. endfunction
  432. " Function: s:restore_position {{{1
  433. function! s:restore_position() abort
  434. autocmd! startify *
  435. if line("'\"") > 0 && line("'\"") <= line('$')
  436. call cursor(getpos("'\"")[1:])
  437. endif
  438. endfunction
  439. " vim: et sw=2 sts=2