startify.vim 13 KB

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