startify.vim 14 KB

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