startify.vim 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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. let s:chdir = (get(g:, 'startify_change_to_dir', 1) ? '<bar> if isdirectory(expand("%")) <bar> lcd % <bar> else <bar> lcd %:h <bar> endif' : '') .'<cr>'
  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:chdir
  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:chdir
  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:chdir()
  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:chdir {{{1
  348. function! s:chdir() abort
  349. if get(g:, 'startify_change_to_dir', 1)
  350. if isdirectory(expand('%'))
  351. lcd %
  352. else
  353. lcd %:h
  354. endif
  355. endif
  356. endfunction
  357. " Function: s:close {{{1
  358. function! s:close() abort
  359. if len(filter(range(0, bufnr('$')), 'buflisted(v:val)'))
  360. if bufloaded(bufnr('#'))
  361. b #
  362. else
  363. bn
  364. endif
  365. else
  366. quit
  367. endif
  368. endfunction
  369. " Function: s:get_index_as_string {{{1
  370. function! s:get_index_as_string(idx) abort
  371. if exists('g:startify_custom_indices')
  372. let listlen = len(g:startify_custom_indices)
  373. return (a:idx < listlen) ? g:startify_custom_indices[a:idx] : string(a:idx - listlen)
  374. else
  375. return string(a:idx)
  376. endif
  377. endfunction
  378. " Function: s:restore_position {{{1
  379. function! s:restore_position() abort
  380. autocmd! startify *
  381. if line("'\"") > 0 && line("'\"") <= line('$')
  382. call cursor(getpos("'\"")[1:])
  383. endif
  384. endfunction
  385. " vim: et sw=2 sts=2