startify.vim 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. " vim: et sw=2 sts=2
  2. " Plugin: https://github.com/mhinz/vim-startify
  3. " Description: Start screen displaying recently used stuff.
  4. " Maintainer: Marco Hinz <http://github.com/mhinz>
  5. " Version: 1.8
  6. if exists('g:autoloaded_startify') || &compatible
  7. finish
  8. endif
  9. let g:autoloaded_startify = 1
  10. " Init: values {{{1
  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. if exists('g:startify_list_order')
  17. let s:lists = g:startify_list_order
  18. else
  19. let s:lists = [
  20. \ [' Last recently opened files:'],
  21. \ 'files',
  22. \ [' Last recently modified files in the current directory:'],
  23. \ 'dir',
  24. \ [' My sessions:'],
  25. \ 'sessions',
  26. \ [' My bookmarks:'],
  27. \ 'bookmarks',
  28. \ ]
  29. endif
  30. let s:secoff = type(s:lists[0]) == 3 ? (len(s:lists[0]) + 1) : 0
  31. let s:section_header_lines = []
  32. " Init: autocmds {{{1
  33. if get(g:, 'startify_session_persistence')
  34. autocmd startify VimLeave *
  35. \ if exists('v:this_session') && filewritable(v:this_session) |
  36. \ call s:session_write(fnameescape(v:this_session)) |
  37. \ endif
  38. endif
  39. " Function: #get_separator {{{1
  40. function! startify#get_separator() abort
  41. return !exists('+shellslash') || &shellslash ? '/' : '\'
  42. endfunction
  43. let s:sep = startify#get_separator()
  44. " Function: #get_lastline {{{1
  45. function! startify#get_lastline() abort
  46. return s:lastline
  47. endfunction
  48. " Function: #insane_in_the_membrane {{{1
  49. function! startify#insane_in_the_membrane() abort
  50. if !empty(v:servername) && exists('g:startify_skiplist_server')
  51. for servname in g:startify_skiplist_server
  52. if servname == v:servername
  53. return
  54. endif
  55. endfor
  56. endif
  57. setlocal noswapfile nobuflisted buftype=nofile bufhidden=wipe
  58. setlocal nonumber nocursorline nolist statusline=\ startify
  59. set filetype=startify
  60. if v:version >= 703
  61. setlocal norelativenumber
  62. endif
  63. let cnt = 0
  64. let s:headoff = 2
  65. if exists('g:startify_custom_header')
  66. call append('$', g:startify_custom_header)
  67. let s:headoff += len(g:startify_custom_header)
  68. endif
  69. if s:show_special
  70. call append('$', [' [e] <empty buffer>', ''])
  71. endif
  72. if get(g:, 'startify_session_detection', 1) && filereadable('Session.vim')
  73. call append('$', [' [0] '. getcwd() . s:sep .'Session.vim', ''])
  74. execute 'nnoremap <buffer> 0 :source Session.vim<cr>'
  75. let cnt = 1
  76. endif
  77. for item in s:lists
  78. if type(item) == 1
  79. let cnt = s:show_{item}(cnt)
  80. else
  81. let s:last_message = item
  82. endif
  83. unlet item
  84. endfor
  85. silent $delete _
  86. for item in s:section_header_lines
  87. call matchadd('StartifySection', '\%'. item .'l', -1)
  88. endfor
  89. if s:show_special
  90. call append('$', ['', ' [q] <quit>'])
  91. endif
  92. let s:lastline = line('$')
  93. if exists('g:startify_custom_footer')
  94. call append('$', g:startify_custom_footer)
  95. endif
  96. setlocal nomodifiable nomodified
  97. nnoremap <buffer><silent> e :enew<cr>
  98. nnoremap <buffer><silent> i :enew <bar> startinsert<cr>
  99. nnoremap <buffer><silent> <insert> :enew <bar> startinsert<cr>
  100. nnoremap <buffer><silent> b :call <sid>set_mark('B')<cr>
  101. nnoremap <buffer><silent> s :call <sid>set_mark('S')<cr>
  102. nnoremap <buffer><silent> t :call <sid>set_mark('T')<cr>
  103. nnoremap <buffer><silent> v :call <sid>set_mark('V')<cr>
  104. nnoremap <buffer> <cr> :call <sid>open_buffers(expand('<cword>'))<cr>
  105. nnoremap <buffer> <2-LeftMouse> :execute 'normal' matchstr(getline('.'), '\w\+')<cr>
  106. nnoremap <buffer><silent> q :call <sid>close()<cr>
  107. if exists('g:startify_empty_buffer_key')
  108. execute 'nnoremap <buffer><silent> '. g:startify_empty_buffer_key .' :enew<cr>'
  109. endif
  110. autocmd startify BufLeave <buffer> call clearmatches()
  111. autocmd startify CursorMoved <buffer> call s:set_cursor()
  112. if s:restore_position
  113. autocmd startify BufReadPost * call s:restore_position()
  114. endif
  115. call cursor((s:show_special ? 2 : 0) + s:headoff + s:secoff, 5)
  116. silent! doautocmd <nomodeline> startify User
  117. endfunction
  118. " Function: #session_load {{{1
  119. function! startify#session_load(...) abort
  120. if !isdirectory(s:session_dir)
  121. echo 'The session directory does not exist: '. s:session_dir
  122. return
  123. elseif empty(startify#session_list_as_string(''))
  124. echo 'There are no sessions...'
  125. return
  126. endif
  127. let spath = s:session_dir . s:sep . (exists('a:1')
  128. \ ? a:1
  129. \ : input('Load this session: ', fnamemodify(v:this_session, ':t'), 'custom,startify#session_list_as_string'))
  130. \ | redraw
  131. if filereadable(spath)
  132. execute 'source '. fnameescape(spath)
  133. else
  134. echo 'No such file: '. spath
  135. endif
  136. endfunction
  137. " Function: #session_save {{{1
  138. function! startify#session_save(...) abort
  139. if !isdirectory(s:session_dir)
  140. if exists('*mkdir')
  141. echo 'The session directory does not exist: '. s:session_dir .'. Create it? [y/n]'
  142. if (nr2char(getchar()) == 'y')
  143. call mkdir(s:session_dir, 'p')
  144. else
  145. echo
  146. return
  147. endif
  148. else
  149. echo 'The session directory does not exist: '. s:session_dir
  150. return
  151. endif
  152. endif
  153. if exists('a:1')
  154. let sname = a:1
  155. else
  156. let sname = input('Save under this session name: ', fnamemodify(v:this_session, ':t'), 'custom,startify#session_list_as_string')
  157. redraw
  158. if empty(sname)
  159. echo 'You gave an empty name!'
  160. return
  161. endif
  162. endif
  163. let spath = s:session_dir . s:sep . sname
  164. if !filereadable(spath)
  165. call s:session_write(fnameescape(spath))
  166. echo 'Session saved under: '. spath
  167. return
  168. endif
  169. echo 'Session already exists. Overwrite? [y/n]' | redraw
  170. if nr2char(getchar()) == 'y'
  171. call s:session_write(fnameescape(spath))
  172. echo 'Session saved under: '. spath
  173. else
  174. echo 'Did NOT save the session!'
  175. endif
  176. endfunction
  177. " Function: #session_delete {{{1
  178. function! startify#session_delete(...) abort
  179. if !isdirectory(s:session_dir)
  180. echo 'The session directory does not exist: '. s:session_dir
  181. return
  182. elseif empty(startify#session_list_as_string(''))
  183. echo 'There are no sessions...'
  184. return
  185. endif
  186. let spath = s:session_dir . s:sep . (exists('a:1')
  187. \ ? a:1
  188. \ : input('Delete this session: ', fnamemodify(v:this_session, ':t'), 'custom,startify#session_list_as_string'))
  189. \ | redraw
  190. echo 'Really delete '. spath .'? [y/n]' | redraw
  191. if (nr2char(getchar()) == 'y')
  192. if delete(spath) == 0
  193. echo 'Deleted session '. spath .'!'
  194. else
  195. echo 'Deletion failed!'
  196. endif
  197. else
  198. echo 'Deletion aborted!'
  199. endif
  200. endfunction
  201. " Function: #session_list {{{1
  202. function! startify#session_list(lead, ...) abort
  203. return map(split(globpath(s:session_dir, '*'.a:lead.'*'), '\n'), 'fnamemodify(v:val, ":t")')
  204. endfunction
  205. " Function: #session_list_as_string {{{1
  206. function! startify#session_list_as_string(lead, ...) abort
  207. return join(map(split(globpath(s:session_dir, '*'.a:lead.'*'), '\n'), 'fnamemodify(v:val, ":t")'), "\n")
  208. endfunction
  209. " Function: s:show_dir {{{1
  210. function! s:show_dir(cnt) abort
  211. let cnt = a:cnt
  212. let num = s:numfiles
  213. let files = []
  214. for fname in split(glob('.\=*'))
  215. if isdirectory(fname)
  216. \ || (exists('g:startify_skiplist') && s:is_in_skiplist(resolve(fnamemodify(fname, ':p'))))
  217. continue
  218. endif
  219. call add(files, [getftime(fname), fname])
  220. endfor
  221. if !empty(files)
  222. if exists('s:last_message')
  223. call s:print_section_header()
  224. endif
  225. function! l:compare(x, y)
  226. return a:y[0] - a:x[0]
  227. endfunction
  228. call sort(files, 'l:compare')
  229. for items in files
  230. let index = s:get_index_as_string(cnt)
  231. let fname = items[1]
  232. call append('$', ' ['. index .']'. repeat(' ', (3 - strlen(index))) . fname)
  233. execute 'nnoremap <buffer>' index ':edit' fnameescape(fname) '<cr>'
  234. let cnt += 1
  235. let num -= 1
  236. if !num
  237. break
  238. endif
  239. endfor
  240. call append('$', '')
  241. endif
  242. return cnt
  243. endfunction
  244. " Function: s:show_files {{{1
  245. function! s:show_files(cnt) abort
  246. let cnt = a:cnt
  247. let num = s:numfiles
  248. let entries = {}
  249. if !empty(v:oldfiles)
  250. if exists('s:last_message')
  251. call s:print_section_header()
  252. endif
  253. for fname in v:oldfiles
  254. let fullpath = resolve(fnamemodify(fname, ':p'))
  255. " filter duplicates, bookmarks and entries from the skiplist
  256. if has_key(entries, fullpath)
  257. \ || !filereadable(fullpath)
  258. \ || (exists('g:startify_skiplist') && s:is_in_skiplist(fullpath))
  259. \ || (exists('g:startify_bookmarks') && s:is_bookmark(fullpath))
  260. continue
  261. endif
  262. let entries[fullpath] = 1
  263. let index = s:get_index_as_string(cnt)
  264. call append('$', ' ['. index .']'. repeat(' ', (3 - strlen(index))) . fname)
  265. execute 'nnoremap <buffer>' index ':edit' fnameescape(fname) '<bar> call <sid>check_user_options()<cr>'
  266. let cnt += 1
  267. let num -= 1
  268. if !num
  269. break
  270. endif
  271. endfor
  272. call append('$', '')
  273. return cnt
  274. endif
  275. endfunction
  276. " Function: s:show_sessions {{{1
  277. function! s:show_sessions(cnt) abort
  278. let sfiles = split(globpath(s:session_dir, '*'), '\n')
  279. let slen = len(sfiles)
  280. if empty(sfiles)
  281. if exists('s:last_message')
  282. unlet s:last_message
  283. endif
  284. return a:cnt
  285. endif
  286. let cnt = a:cnt
  287. if exists('s:last_message')
  288. call s:print_section_header()
  289. endif
  290. for i in range(slen)
  291. let idx = (i + cnt)
  292. let index = s:get_index_as_string(idx)
  293. call append('$', ' ['. index .']'. repeat(' ', (3 - strlen(index))) . fnamemodify(sfiles[i], ':t:r'))
  294. execute 'nnoremap <buffer>' index ':source' fnameescape(sfiles[i]) '<cr>'
  295. endfor
  296. call append('$', '')
  297. return idx + 1
  298. endfunction
  299. " Function: s:show_bookmarks {{{1
  300. function! s:show_bookmarks(cnt) abort
  301. let cnt = a:cnt
  302. if exists('g:startify_bookmarks')
  303. if exists('s:last_message')
  304. call s:print_section_header()
  305. endif
  306. for fname in g:startify_bookmarks
  307. let index = s:get_index_as_string(cnt)
  308. call append('$', ' ['. index .']'. repeat(' ', (3 - strlen(index))) . fname)
  309. execute 'nnoremap <buffer>' index ':edit' fnameescape(fname) '<bar> call <sid>check_user_options()<cr>'
  310. let cnt += 1
  311. endfor
  312. call append('$', '')
  313. endif
  314. return cnt
  315. endfunction
  316. " Function: s:is_in_skiplist {{{1
  317. function! s:is_in_skiplist(arg) abort
  318. for regexp in g:startify_skiplist
  319. if (a:arg =~# regexp)
  320. return 1
  321. endif
  322. endfor
  323. endfunction
  324. " Function: s:is_bookmark {{{1
  325. function! s:is_bookmark(arg) abort
  326. for foo in map(filter(copy(g:startify_bookmarks), '!isdirectory(v:val)'), 'resolve(fnamemodify(v:val, ":p"))')
  327. if foo == a:arg
  328. return 1
  329. endif
  330. endfor
  331. endfunction
  332. " Function: s:set_cursor {{{1
  333. function! s:set_cursor() abort
  334. let s:oldline = exists('s:newline') ? s:newline : 5
  335. let s:newline = line('.')
  336. if !exists('s:firstline')
  337. let s:firstline = s:show_special ? s:headoff : (s:headoff + s:secoff)
  338. endif
  339. " going down
  340. if s:newline > s:oldline
  341. while index(s:section_header_lines, s:newline) != -1
  342. let s:newline += 1
  343. endwhile
  344. if empty(getline(s:newline)) | let s:newline += 1 | endif
  345. if s:newline > s:lastline | let s:newline = s:lastline | endif
  346. " going up
  347. elseif s:newline < s:oldline
  348. while index(s:section_header_lines, s:newline) != -1
  349. let s:newline -= 1
  350. endwhile
  351. if empty(getline(s:newline)) | let s:newline -= 1 | endif
  352. if s:newline < s:firstline | let s:newline = s:firstline | endif
  353. endif
  354. call cursor(s:newline, 5)
  355. endfunction
  356. " Function: s:set_mark {{{1
  357. "
  358. " Markers are saved in the s:marked dict using the follow format:
  359. " - s:marked[0]: ID
  360. " - s:marked[1]: path
  361. " - s:marked[2]: type (buffer, split, vsplit)
  362. "
  363. function! s:set_mark(type) abort
  364. if !exists('s:marked')
  365. let s:marked = {}
  366. endif
  367. let [id, path] = matchlist(getline('.'), '\v\[(.*)\]\s+(.*)')[1:2]
  368. if path =~# '\V<empty buffer>\|<quit>' || path =~# '^\w\+$'
  369. return
  370. endif
  371. setlocal modifiable
  372. " set markers
  373. if id =~# '[BSTV]'
  374. " replace marker by old ID
  375. execute 'normal! ci]'. remove(s:marked, line('.'))[0]
  376. else
  377. " save ID and replace it by the marker of the given type
  378. let s:marked[line('.')] = [id, path, a:type]
  379. execute 'normal! ci]'. repeat(a:type, len(id))
  380. endif
  381. setlocal nomodifiable nomodified
  382. endfunction
  383. " Function: s:open_buffers {{{1
  384. function! s:open_buffers(cword) abort
  385. " markers found; open one or more buffers
  386. if exists('s:marked') && !empty(s:marked)
  387. enew
  388. setlocal nobuflisted
  389. for val in values(s:marked)
  390. let [path, type] = val[1:2]
  391. if line2byte('$') == -1
  392. " open in current window
  393. execute 'edit' path
  394. elseif type == 'S'
  395. " open in split
  396. execute 'split' path
  397. elseif type == 'V'
  398. " open in vsplit
  399. execute 'vsplit' path
  400. elseif type == 'T'
  401. " open in tab
  402. execute 'tabnew' path
  403. else
  404. " open in current window
  405. execute 'edit' path
  406. endif
  407. call s:check_user_options()
  408. endfor
  409. " remove markers for next instance of :Startify
  410. if exists('s:marked')
  411. unlet s:marked
  412. endif
  413. " no markers found; open a single buffer
  414. else
  415. execute 'normal' a:cword
  416. endif
  417. endfunction
  418. " Function: s:check_user_options {{{1
  419. function! s:check_user_options() abort
  420. let path = expand('%')
  421. let session = path . s:sep .'Session.vim'
  422. " autoload session
  423. if get(g:, 'startify_session_autoload') && filereadable(session)
  424. execute 'source' session
  425. " change to VCS root directory
  426. elseif get(g:, 'startify_change_to_vcs_root')
  427. call s:cd_to_vcs_root(path)
  428. " change directory
  429. elseif get(g:, 'startify_change_to_dir', 1)
  430. if isdirectory(path)
  431. lcd %
  432. else
  433. lcd %:h
  434. endif
  435. endif
  436. endfunction
  437. " Function: s:cd_to_vcs_root {{{1
  438. function! s:cd_to_vcs_root(path) abort
  439. let dir = fnamemodify(a:path, ':p:h')
  440. for vcs in [ '.git', '.hg', '.bzr', '.svn' ]
  441. let root = finddir(vcs, dir .';')
  442. if !empty(root)
  443. execute 'cd '. fnamemodify(root, ':h')
  444. return
  445. endif
  446. endfor
  447. endfunction
  448. " Function: s:close {{{1
  449. function! s:close() abort
  450. if len(filter(range(0, bufnr('$')), 'buflisted(v:val)'))
  451. if bufloaded(bufnr('#'))
  452. buffer #
  453. else
  454. bnext
  455. endif
  456. else
  457. quit
  458. endif
  459. endfunction
  460. " Function: s:get_index_as_string {{{1
  461. function! s:get_index_as_string(idx) abort
  462. if exists('g:startify_custom_indices')
  463. let listlen = len(g:startify_custom_indices)
  464. return (a:idx < listlen) ? g:startify_custom_indices[a:idx] : string(a:idx - listlen)
  465. else
  466. return string(a:idx)
  467. endif
  468. endfunction
  469. " Function: s:restore_position {{{1
  470. function! s:restore_position() abort
  471. autocmd! startify *
  472. if line("'\"") > 0 && line("'\"") <= line('$')
  473. call cursor(getpos("'\"")[1:])
  474. endif
  475. endfunction
  476. " Function: s:session_write {{{1
  477. function! s:session_write(spath)
  478. let ssop = &sessionoptions
  479. try
  480. set sessionoptions-=options
  481. execute 'mksession!' a:spath
  482. catch
  483. execute 'echoerr' string(v:exception)
  484. finally
  485. let &sessionoptions = ssop
  486. endtry
  487. if exists('g:startify_session_savevars') || exists('g:startify_session_savecmds')
  488. execute 'split' a:spath
  489. " put existing variables from savevars into session file
  490. call append(line('$')-3, map(filter(get(g:, 'startify_session_savevars', []), 'exists(v:val)'), '"let ". v:val ." = ". strtrans(string(eval(v:val)))'))
  491. " put commands from savecmds into session file
  492. call append(line('$')-3, get(g:, 'startify_session_savecmds', []))
  493. setlocal bufhidden=delete
  494. silent update
  495. silent hide
  496. endif
  497. endfunction
  498. " Function: s:print_section_header {{{1
  499. function! s:print_section_header() abort
  500. $
  501. let curline = line('.')
  502. for lnum in range(curline, curline + len(s:last_message) + 1)
  503. call add(s:section_header_lines, lnum)
  504. endfor
  505. call append('$', s:last_message + [''])
  506. unlet s:last_message
  507. endfunction