startify.vim 12 KB

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