startify.vim 17 KB

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