startify.vim 13 KB

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