startify.vim 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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 num = s:numfiles
  163. let files = []
  164. for fname in split(glob('.\=*'))
  165. if isdirectory(fname)
  166. \ || (exists('g:startify_skiplist') && s:is_in_skiplist(resolve(fnamemodify(fname, ':p'))))
  167. continue
  168. endif
  169. call add(files, [getftime(fname), fname])
  170. endfor
  171. function! l:compare(x, y)
  172. return a:y[0] - a:x[0]
  173. endfunction
  174. call sort(files, 'l:compare')
  175. for items in files
  176. let index = s:get_index_as_string(cnt)
  177. let fname = items[1]
  178. call append('$', ' ['. index .']'. repeat(' ', (3 - strlen(index))) . fname)
  179. execute 'nnoremap <buffer>' index ':edit' fnameescape(fname) '<cr>'
  180. let cnt += 1
  181. let num -= 1
  182. if !num
  183. break
  184. endif
  185. endfor
  186. return cnt
  187. endfunction
  188. " Function: s:show_files {{{1
  189. function! s:show_files(cnt) abort
  190. let cnt = a:cnt
  191. let num = s:numfiles
  192. let entries = {}
  193. if !empty(v:oldfiles)
  194. for fname in v:oldfiles
  195. let fullpath = resolve(fnamemodify(fname, ':p'))
  196. " filter duplicates, bookmarks and entries from the skiplist
  197. if has_key(entries, fullpath)
  198. \ || !filereadable(fullpath)
  199. \ || (exists('g:startify_skiplist') && s:is_in_skiplist(fullpath))
  200. \ || (exists('g:startify_bookmarks') && s:is_bookmark(fullpath))
  201. continue
  202. endif
  203. let entries[fullpath] = 1
  204. let index = s:get_index_as_string(cnt)
  205. call append('$', ' ['. index .']'. repeat(' ', (3 - strlen(index))) . fname)
  206. execute 'nnoremap <buffer>' index ':edit' fnameescape(fname) s:cmd
  207. let cnt += 1
  208. let num -= 1
  209. if !num
  210. break
  211. endif
  212. endfor
  213. endif
  214. return cnt
  215. endfunction
  216. " Function: s:show_sessions {{{1
  217. function! s:show_sessions(cnt) abort
  218. let sfiles = split(globpath(s:session_dir, '*'), '\n')
  219. if empty(sfiles)
  220. return a:cnt
  221. endif
  222. let cnt = a:cnt
  223. for i in range(len(sfiles))
  224. let idx = (i + cnt)
  225. let index = s:get_index_as_string(idx)
  226. call append('$', ' ['. index .']'. repeat(' ', (3 - strlen(index))) . fnamemodify(sfiles[i], ':t:r'))
  227. execute 'nnoremap <buffer> '. index .' :source '. fnameescape(sfiles[i]) .'<cr>'
  228. endfor
  229. return idx
  230. endfunction
  231. " Function: s:show_bookmarks {{{1
  232. function! s:show_bookmarks(cnt) abort
  233. let cnt = a:cnt
  234. if exists('g:startify_bookmarks')
  235. for fname in g:startify_bookmarks
  236. let cnt += 1
  237. let index = s:get_index_as_string(cnt)
  238. call append('$', ' ['. index .']'. repeat(' ', (3 - strlen(index))) . fname)
  239. execute 'nnoremap <buffer> '. index .' :edit '. fnameescape(fname) . s:cmd
  240. endfor
  241. endif
  242. return cnt
  243. endfunction
  244. " Function: s:is_in_skiplist {{{1
  245. function! s:is_in_skiplist(arg) abort
  246. for regexp in g:startify_skiplist
  247. if (a:arg =~# regexp)
  248. return 1
  249. endif
  250. endfor
  251. endfunction
  252. " Function: s:is_bookmark {{{1
  253. function! s:is_bookmark(arg) abort
  254. "for foo in filter(map(copy(g:startify_bookmarks), 'resolve(fnamemodify(v:val, ":p"))'), '!isdirectory(v:val)')
  255. for foo in map(filter(copy(g:startify_bookmarks), '!isdirectory(v:val)'), 'resolve(fnamemodify(v:val, ":p"))')
  256. if foo == a:arg
  257. return 1
  258. endif
  259. endfor
  260. endfunction
  261. " Function: s:set_cursor {{{1
  262. function! s:set_cursor() abort
  263. let s:line_old = exists('s:line_new') ? s:line_new : 5
  264. let s:line_new = line('.')
  265. let offset = s:offset_header + 2
  266. if empty(getline(s:line_new))
  267. if (s:line_new > s:line_old)
  268. let s:line_new += 1
  269. call cursor(s:line_new, 5) " going down
  270. else
  271. let s:line_new -= 1
  272. call cursor((s:line_new < offset ? offset : s:line_new), 5) " going up
  273. endif
  274. else
  275. call cursor((s:line_new < offset ? offset : 0), 5) " hold cursor in column
  276. endif
  277. endfunction
  278. " Function: s:set_mark {{{1
  279. "
  280. " Markers are saved in the s:marked dict using the follow format:
  281. " - s:marked[0]: ID (for sorting)
  282. " - s:marked[1]: what the brackets contained before
  283. " - s:marked[2]: the actual path
  284. " - s:marked[3]: type (buffer, split, vsplit)
  285. "
  286. function! s:set_mark(type) abort
  287. if !exists('s:marked')
  288. let s:marked = {}
  289. let s:nmarked = 0
  290. endif
  291. " matches[1]: content between brackets
  292. " matches[2]: path
  293. let matches = matchlist(getline('.'), '\v\[(.*)\]\s+(.*)')
  294. if matches[2] =~ '\V<empty buffer>\|<quit>' || matches[2] =~ '^\w\+$'
  295. return
  296. endif
  297. setlocal modifiable
  298. if matches[1] =~ 'B\|S\|V'
  299. let s:nmarked -= 1
  300. execute 'normal! ci]'. remove(s:marked, line('.'))[1]
  301. else
  302. let s:marked[line('.')] = [s:nmarked, matches[1], matches[2], a:type]
  303. let s:nmarked += 1
  304. execute 'normal! ci]'. repeat(a:type, len(matches[1]))
  305. endif
  306. setlocal nomodifiable nomodified
  307. endfunction
  308. " Function: s:open_buffers {{{1
  309. function! s:open_buffers(cword) abort
  310. if exists('s:marked') && !empty(s:marked)
  311. enew
  312. setlocal nobuflisted
  313. for i in range(len(s:marked))
  314. for val in values(s:marked)
  315. if val[0] == i
  316. if val[3] == 'S'
  317. if line2byte('$') == -1
  318. execute 'edit' val[2]
  319. else
  320. execute 'split' val[2]
  321. endif
  322. elseif val[3] == 'V'
  323. if line2byte('$') == -1
  324. execute 'edit' val[2]
  325. else
  326. execute 'vsplit' val[2]
  327. endif
  328. else
  329. execute 'edit' val[2]
  330. endif
  331. continue
  332. endif
  333. endfor
  334. endfor
  335. else
  336. execute 'normal' a:cword
  337. endif
  338. if exists('s:marked')
  339. unlet s:marked
  340. unlet s:nmarked
  341. endif
  342. endfunction
  343. " Function: s:close {{{1
  344. function! s:close() abort
  345. if len(filter(range(0, bufnr('$')), 'buflisted(v:val)'))
  346. if bufloaded(bufnr('#'))
  347. b #
  348. else
  349. bn
  350. endif
  351. else
  352. quit
  353. endif
  354. endfunction
  355. " Function: s:get_index_as_string {{{1
  356. function! s:get_index_as_string(idx) abort
  357. if exists('g:startify_custom_indices')
  358. let listlen = len(g:startify_custom_indices)
  359. return (a:idx < listlen) ? g:startify_custom_indices[a:idx] : string(a:idx - listlen)
  360. else
  361. return string(a:idx)
  362. endif
  363. endfunction
  364. " Function: s:restore_position {{{1
  365. function! s:restore_position() abort
  366. autocmd! startify *
  367. if line("'\"") > 0 && line("'\"") <= line('$')
  368. call cursor(getpos("'\"")[1:])
  369. endif
  370. endfunction
  371. " vim: et sw=2 sts=2