startify.vim 14 KB

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