startify.vim 14 KB

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