startify.vim 14 KB

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