startify.vim 13 KB

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