startify.vim 12 KB

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