startify.vim 17 KB

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