startify.vim 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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: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. " Function: #get_separator {{{1
  16. function! startify#get_separator() abort
  17. return !exists('+shellslash') || &shellslash ? '/' : '\'
  18. endfunction
  19. let s:sep = startify#get_separator()
  20. " Function: #insane_in_the_membrane {{{1
  21. function! startify#insane_in_the_membrane() abort
  22. if !empty(v:servername) && exists('g:startify_skiplist_server')
  23. for servname in g:startify_skiplist_server
  24. if servname == v:servername
  25. return
  26. endif
  27. endfor
  28. endif
  29. setlocal noswapfile nobuflisted buftype=nofile bufhidden=wipe
  30. setlocal nonumber nolist statusline=\ startify
  31. setfiletype startify
  32. if v:version >= 703
  33. setlocal norelativenumber
  34. endif
  35. let cnt = 0
  36. let s:offset_header = 0
  37. if exists('g:startify_custom_header')
  38. call append('$', g:startify_custom_header)
  39. let s:offset_header += len(g:startify_custom_header)
  40. endif
  41. if s:show_special
  42. call append('$', [' [e] <empty buffer>', ''])
  43. endif
  44. if get(g:, 'startify_session_detection', 1) && filereadable('Session.vim')
  45. call append('$', [' [0] '. getcwd() . s:sep .'Session.vim', ''])
  46. execute 'nnoremap <buffer> 0 :source Session.vim<cr>'
  47. let cnt = 1
  48. endif
  49. for list in get(g:, 'startify_list_order', ['files', 'sessions', 'bookmarks'])
  50. let cnt = s:show_{list}(cnt)
  51. call append('$', '')
  52. endfor
  53. if s:show_special
  54. call append('$', ' [q] <quit>')
  55. endif
  56. setlocal nomodifiable nomodified
  57. nnoremap <buffer><silent> e :enew<cr>
  58. nnoremap <buffer><silent> i :enew <bar> startinsert<cr>
  59. nnoremap <buffer><silent> b :call <sid>set_mark('B')<cr>
  60. nnoremap <buffer><silent> s :call <sid>set_mark('S')<cr>
  61. nnoremap <buffer><silent> v :call <sid>set_mark('V')<cr>
  62. nnoremap <buffer> <cr> :call <sid>open_buffers(expand('<cword>'))<cr>
  63. nnoremap <buffer> <2-LeftMouse> :execute 'normal' matchstr(getline('.'), '\w\+')<cr>
  64. nnoremap <buffer><silent> q :call <sid>close()<cr>
  65. if exists('g:startify_empty_buffer_key')
  66. execute 'nnoremap <buffer><silent> '. g:startify_empty_buffer_key .' :enew<cr>'
  67. endif
  68. autocmd startify CursorMoved <buffer> call s:set_cursor()
  69. if s:restore_position
  70. autocmd startify BufReadPost * call s:restore_position()
  71. endif
  72. 1
  73. call cursor((s:show_special ? 4 : 2) + s:offset_header, 5)
  74. endfunction
  75. " Function: #session_load {{{1
  76. function! startify#session_load(...) abort
  77. if !isdirectory(s:session_dir)
  78. echo 'The session directory does not exist: '. s:session_dir
  79. return
  80. elseif empty(startify#session_list_as_string(''))
  81. echo 'There are no sessions...'
  82. return
  83. endif
  84. let spath = s:session_dir . s:sep . (exists('a:1')
  85. \ ? a:1
  86. \ : input('Load this session: ', fnamemodify(v:this_session, ':t'), 'custom,startify#session_list_as_string'))
  87. \ | redraw
  88. if filereadable(spath)
  89. execute 'source '. fnameescape(spath)
  90. else
  91. echo 'No such file: '. spath
  92. endif
  93. endfunction
  94. " Function: #session_save {{{1
  95. function! startify#session_save(...) abort
  96. if !isdirectory(s:session_dir)
  97. if exists('*mkdir')
  98. echo 'The session directory does not exist: '. s:session_dir .'. Create it? [y/n]' | redraw
  99. if (nr2char(getchar()) == 'y')
  100. call mkdir(s:session_dir, 'p')
  101. else
  102. echo
  103. return
  104. endif
  105. else
  106. echo 'The session directory does not exist: '. s:session_dir
  107. return
  108. endif
  109. endif
  110. if exists('a:1')
  111. let sname = a:1
  112. else
  113. let sname = input('Save under this session name: ', fnamemodify(v:this_session, ':t'), 'custom,startify#session_list_as_string')
  114. redraw
  115. if empty(sname)
  116. echo 'You gave an empty name!'
  117. return
  118. endif
  119. endif
  120. let spath = s:session_dir . s:sep . sname
  121. if !filereadable(spath)
  122. execute 'mksession '. fnameescape(spath) | echo 'Session saved under: '. spath
  123. return
  124. endif
  125. echo 'Session already exists. Overwrite? [y/n]' | redraw
  126. if nr2char(getchar()) == 'y'
  127. execute 'mksession! '. fnameescape(spath) | echo 'Session saved under: '. spath
  128. else
  129. echo 'Did NOT save the session!'
  130. endif
  131. endfunction
  132. " Function: #session_delete {{{1
  133. function! startify#session_delete(...) abort
  134. if !isdirectory(s:session_dir)
  135. echo 'The session directory does not exist: '. s:session_dir
  136. return
  137. elseif empty(startify#session_list_as_string(''))
  138. echo 'There are no sessions...'
  139. return
  140. endif
  141. let spath = s:session_dir . s:sep . (exists('a:1')
  142. \ ? a:1
  143. \ : input('Delete this session: ', fnamemodify(v:this_session, ':t'), 'custom,startify#session_list_as_string'))
  144. \ | redraw
  145. echo 'Really delete '. spath .'? [y/n]' | redraw
  146. if (nr2char(getchar()) == 'y')
  147. if delete(spath) == 0
  148. echo 'Deleted session '. spath .'!'
  149. else
  150. echo 'Deletion failed!'
  151. endif
  152. else
  153. echo 'Deletion aborted!'
  154. endif
  155. endfunction
  156. " Function: #session_list {{{1
  157. function! startify#session_list(lead, ...) abort
  158. return map(split(globpath(s:session_dir, '*'.a:lead.'*'), '\n'), 'fnamemodify(v:val, ":t")')
  159. endfunction
  160. " Function: #session_list_as_string {{{1
  161. function! startify#session_list_as_string(lead, ...) abort
  162. return join(map(split(globpath(s:session_dir, '*'.a:lead.'*'), '\n'), 'fnamemodify(v:val, ":t")'), "\n")
  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) '<bar> call <sid>check_user_options()<cr>'
  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) '<bar> call <sid>check_user_options()<cr>'
  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
  287. " - s:marked[1]: path
  288. " - s:marked[2]: type (buffer, split, vsplit)
  289. "
  290. function! s:set_mark(type) abort
  291. if !exists('s:marked')
  292. let s:marked = {}
  293. endif
  294. let [id, path] = matchlist(getline('.'), '\v\[(.*)\]\s+(.*)')[1:2]
  295. if path =~# '\V<empty buffer>\|<quit>'
  296. return
  297. endif
  298. setlocal modifiable
  299. " set markers
  300. if id =~# '[BSV]'
  301. " replace marker by old ID
  302. execute 'normal! ci]'. remove(s:marked, line('.'))[0]
  303. else
  304. " save ID and replace it by the marker of the given type
  305. let s:marked[line('.')] = [id, path, a:type]
  306. execute 'normal! ci]'. repeat(a:type, len(id))
  307. endif
  308. setlocal nomodifiable nomodified
  309. endfunction
  310. " Function: s:open_buffers {{{1
  311. function! s:open_buffers(cword) abort
  312. " markers found; open one or more buffers
  313. if exists('s:marked') && !empty(s:marked)
  314. enew
  315. setlocal nobuflisted
  316. for val in values(s:marked)
  317. let [path, type] = val[1:2]
  318. " open in split
  319. if type == 'S'
  320. if line2byte('$') == -1
  321. execute 'edit' path
  322. else
  323. execute 'split' path
  324. endif
  325. " open in vsplit
  326. elseif type == 'V'
  327. if line2byte('$') == -1
  328. execute 'edit' path
  329. else
  330. execute 'vsplit' path
  331. endif
  332. " open in current window
  333. else
  334. execute 'edit' path
  335. endif
  336. call s:check_user_options()
  337. endfor
  338. " remove markers for next instance of :Startify
  339. if exists('s:marked')
  340. unlet s:marked
  341. endif
  342. " no markers found; open a single buffer
  343. else
  344. execute 'normal' a:cword
  345. endif
  346. endfunction
  347. " Function: s:check_user_options {{{1
  348. function! s:check_user_options() abort
  349. let path = expand('%') . s:sep .'Session.vim'
  350. " autoload session
  351. if get(g:, 'startify_session_autoload') && filereadable(path)
  352. execute 'source' path
  353. " change directory
  354. elseif get(g:, 'startify_change_to_dir', 1)
  355. if isdirectory(expand('%'))
  356. lcd %
  357. else
  358. lcd %:h
  359. endif
  360. endif
  361. endfunction
  362. " Function: s:close {{{1
  363. function! s:close() abort
  364. if len(filter(range(0, bufnr('$')), 'buflisted(v:val)'))
  365. if bufloaded(bufnr('#'))
  366. b #
  367. else
  368. bn
  369. endif
  370. else
  371. quit
  372. endif
  373. endfunction
  374. " Function: s:get_index_as_string {{{1
  375. function! s:get_index_as_string(idx) abort
  376. if exists('g:startify_custom_indices')
  377. let listlen = len(g:startify_custom_indices)
  378. return (a:idx < listlen) ? g:startify_custom_indices[a:idx] : string(a:idx - listlen)
  379. else
  380. return string(a:idx)
  381. endif
  382. endfunction
  383. " Function: s:restore_position {{{1
  384. function! s:restore_position() abort
  385. autocmd! startify *
  386. if line("'\"") > 0 && line("'\"") <= line('$')
  387. call cursor(getpos("'\"")[1:])
  388. endif
  389. endfunction
  390. " vim: et sw=2 sts=2