startify.vim 14 KB

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