startify.vim 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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. endfor
  59. $delete
  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. if !empty(files)
  201. call append('$', '')
  202. endif
  203. return cnt
  204. endfunction
  205. " Function: s:show_files {{{1
  206. function! s:show_files(cnt) abort
  207. let cnt = a:cnt
  208. let num = s:numfiles
  209. let entries = {}
  210. if !empty(v:oldfiles)
  211. for fname in v:oldfiles
  212. let fullpath = resolve(fnamemodify(fname, ':p'))
  213. " filter duplicates, bookmarks and entries from the skiplist
  214. if has_key(entries, fullpath)
  215. \ || !filereadable(fullpath)
  216. \ || (exists('g:startify_skiplist') && s:is_in_skiplist(fullpath))
  217. \ || (exists('g:startify_bookmarks') && s:is_bookmark(fullpath))
  218. continue
  219. endif
  220. let entries[fullpath] = 1
  221. let index = s:get_index_as_string(cnt)
  222. call append('$', ' ['. index .']'. repeat(' ', (3 - strlen(index))) . fname)
  223. execute 'nnoremap <buffer>' index ':edit' fnameescape(fname) '<bar> call <sid>check_user_options()<cr>'
  224. let cnt += 1
  225. let num -= 1
  226. if !num
  227. break
  228. endif
  229. endfor
  230. call append('$', '')
  231. return cnt
  232. endif
  233. endfunction
  234. " Function: s:show_sessions {{{1
  235. function! s:show_sessions(cnt) abort
  236. let sfiles = split(globpath(s:session_dir, '*'), '\n')
  237. let slen = len(sfiles)
  238. if empty(sfiles)
  239. return a:cnt
  240. endif
  241. let cnt = a:cnt
  242. for i in range(slen)
  243. let idx = (i + cnt)
  244. let index = s:get_index_as_string(idx)
  245. call append('$', ' ['. index .']'. repeat(' ', (3 - strlen(index))) . fnamemodify(sfiles[i], ':t:r'))
  246. execute 'nnoremap <buffer>' index ':source' fnameescape(sfiles[i]) '<cr>'
  247. endfor
  248. call append('$', '')
  249. return idx + 1
  250. endfunction
  251. " Function: s:show_bookmarks {{{1
  252. function! s:show_bookmarks(cnt) abort
  253. let cnt = a:cnt
  254. if exists('g:startify_bookmarks')
  255. for fname in g:startify_bookmarks
  256. let index = s:get_index_as_string(cnt)
  257. call append('$', ' ['. index .']'. repeat(' ', (3 - strlen(index))) . fname)
  258. execute 'nnoremap <buffer>' index ':edit' fnameescape(fname) '<bar> call <sid>check_user_options()<cr>'
  259. let cnt += 1
  260. endfor
  261. call append('$', '')
  262. endif
  263. return cnt
  264. endfunction
  265. " Function: s:is_in_skiplist {{{1
  266. function! s:is_in_skiplist(arg) abort
  267. for regexp in g:startify_skiplist
  268. if (a:arg =~# regexp)
  269. return 1
  270. endif
  271. endfor
  272. endfunction
  273. " Function: s:is_bookmark {{{1
  274. function! s:is_bookmark(arg) abort
  275. "for foo in filter(map(copy(g:startify_bookmarks), 'resolve(fnamemodify(v:val, ":p"))'), '!isdirectory(v:val)')
  276. for foo in map(filter(copy(g:startify_bookmarks), '!isdirectory(v:val)'), 'resolve(fnamemodify(v:val, ":p"))')
  277. if foo == a:arg
  278. return 1
  279. endif
  280. endfor
  281. endfunction
  282. " Function: s:set_cursor {{{1
  283. function! s:set_cursor() abort
  284. let s:line_old = exists('s:line_new') ? s:line_new : 5
  285. let s:line_new = line('.')
  286. let offset = s:offset_header + 2
  287. if empty(getline(s:line_new))
  288. if (s:line_new > s:line_old)
  289. let s:line_new += 1
  290. call cursor(s:line_new, 5) " going down
  291. else
  292. let s:line_new -= 1
  293. call cursor((s:line_new < offset ? offset : s:line_new), 5) " going up
  294. endif
  295. else
  296. call cursor((s:line_new < offset ? offset : 0), 5) " hold cursor in column
  297. endif
  298. endfunction
  299. " Function: s:set_mark {{{1
  300. "
  301. " Markers are saved in the s:marked dict using the follow format:
  302. " - s:marked[0]: ID
  303. " - s:marked[1]: path
  304. " - s:marked[2]: type (buffer, split, vsplit)
  305. "
  306. function! s:set_mark(type) abort
  307. if !exists('s:marked')
  308. let s:marked = {}
  309. endif
  310. let [id, path] = matchlist(getline('.'), '\v\[(.*)\]\s+(.*)')[1:2]
  311. if path =~# '\V<empty buffer>\|<quit>' || path =~# '^\w\+$'
  312. return
  313. endif
  314. setlocal modifiable
  315. " set markers
  316. if id =~# '[BSTV]'
  317. " replace marker by old ID
  318. execute 'normal! ci]'. remove(s:marked, line('.'))[0]
  319. else
  320. " save ID and replace it by the marker of the given type
  321. let s:marked[line('.')] = [id, path, a:type]
  322. execute 'normal! ci]'. repeat(a:type, len(id))
  323. endif
  324. setlocal nomodifiable nomodified
  325. endfunction
  326. " Function: s:open_buffers {{{1
  327. function! s:open_buffers(cword) abort
  328. " markers found; open one or more buffers
  329. if exists('s:marked') && !empty(s:marked)
  330. enew
  331. setlocal nobuflisted
  332. for val in values(s:marked)
  333. let [path, type] = val[1:2]
  334. if line2byte('$') == -1
  335. " open in current window
  336. execute 'edit' path
  337. elseif type == 'S'
  338. " open in split
  339. execute 'split' path
  340. elseif type == 'V'
  341. " open in vsplit
  342. execute 'vsplit' path
  343. elseif type == 'T'
  344. " open in tab
  345. execute 'tabnew' path
  346. else
  347. " open in current window
  348. execute 'edit' path
  349. endif
  350. call s:check_user_options()
  351. endfor
  352. " remove markers for next instance of :Startify
  353. if exists('s:marked')
  354. unlet s:marked
  355. endif
  356. " no markers found; open a single buffer
  357. else
  358. execute 'normal' a:cword
  359. endif
  360. endfunction
  361. " Function: s:check_user_options {{{1
  362. function! s:check_user_options() abort
  363. let path = expand('%')
  364. let session = path . s:sep .'Session.vim'
  365. " autoload session
  366. if get(g:, 'startify_session_autoload') && filereadable(session)
  367. execute 'source' session
  368. " change to VCS root directory
  369. elseif get(g:, 'startify_change_to_vcs_root')
  370. call s:cd_to_vcs_root(path)
  371. " change directory
  372. elseif get(g:, 'startify_change_to_dir', 1)
  373. if isdirectory(path)
  374. lcd %
  375. else
  376. lcd %:h
  377. endif
  378. endif
  379. endfunction
  380. " Function: s:cd_to_vcs_root {{{1
  381. function! s:cd_to_vcs_root(path) abort
  382. let dir = fnamemodify(a:path, ':p:h')
  383. for vcs in [ '.git', '.hg', '.bzr', '.svn' ]
  384. let root = finddir(vcs, dir .';')
  385. if !empty(root)
  386. execute 'cd '. fnamemodify(root, ':h')
  387. return
  388. endif
  389. endfor
  390. endfunction
  391. " Function: s:close {{{1
  392. function! s:close() abort
  393. if len(filter(range(0, bufnr('$')), 'buflisted(v:val)'))
  394. if bufloaded(bufnr('#'))
  395. b #
  396. else
  397. bn
  398. endif
  399. else
  400. quit
  401. endif
  402. endfunction
  403. " Function: s:get_index_as_string {{{1
  404. function! s:get_index_as_string(idx) abort
  405. if exists('g:startify_custom_indices')
  406. let listlen = len(g:startify_custom_indices)
  407. return (a:idx < listlen) ? g:startify_custom_indices[a:idx] : string(a:idx - listlen)
  408. else
  409. return string(a:idx)
  410. endif
  411. endfunction
  412. " Function: s:restore_position {{{1
  413. function! s:restore_position() abort
  414. autocmd! startify *
  415. if line("'\"") > 0 && line("'\"") <= line('$')
  416. call cursor(getpos("'\"")[1:])
  417. endif
  418. endfunction
  419. " vim: et sw=2 sts=2