startify.vim 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  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. let cnt = a:cnt
  227. let num = s:numfiles
  228. let entries = {}
  229. let cwd = getcwd()
  230. let files = filter(map(copy(v:oldfiles), 'resolve(fnamemodify(v:val, ":p"))'), 'match(v:val, cwd) == 0')
  231. if !empty(files)
  232. if exists('s:last_message')
  233. call s:print_section_header()
  234. endif
  235. for fname in files
  236. let fullpath = resolve(fnamemodify(fname, ':p'))
  237. " filter duplicates, bookmarks and entries from the skiplist
  238. if has_key(entries, fullpath)
  239. \ || !filereadable(fullpath)
  240. \ || (exists('g:startify_skiplist') && s:is_in_skiplist(fullpath))
  241. \ || (exists('g:startify_bookmarks') && s:is_bookmark(fullpath))
  242. continue
  243. endif
  244. let entries[fullpath] = 1
  245. let index = s:get_index_as_string(cnt)
  246. call append('$', ' ['. index .']'. repeat(' ', (3 - strlen(index))) . fname)
  247. execute 'nnoremap <buffer>' index ':edit' fnameescape(fname) '<bar> call <sid>check_user_options()<cr>'
  248. let cnt += 1
  249. let num -= 1
  250. if !num
  251. break
  252. endif
  253. endfor
  254. call append('$', '')
  255. return cnt
  256. endif
  257. endfunction
  258. " Function: s:show_files {{{1
  259. function! s:show_files(cnt) abort
  260. let cnt = a:cnt
  261. let num = s:numfiles
  262. let entries = {}
  263. if !empty(v:oldfiles)
  264. if exists('s:last_message')
  265. call s:print_section_header()
  266. endif
  267. for fname in v:oldfiles
  268. let fullpath = resolve(fnamemodify(fname, ':p'))
  269. " filter duplicates, bookmarks and entries from the skiplist
  270. if has_key(entries, fullpath)
  271. \ || !filereadable(fullpath)
  272. \ || (exists('g:startify_skiplist') && s:is_in_skiplist(fullpath))
  273. \ || (exists('g:startify_bookmarks') && s:is_bookmark(fullpath))
  274. continue
  275. endif
  276. let entries[fullpath] = 1
  277. let index = s:get_index_as_string(cnt)
  278. call append('$', ' ['. index .']'. repeat(' ', (3 - strlen(index))) . fname)
  279. execute 'nnoremap <buffer>' index ':edit' fnameescape(fname) '<bar> call <sid>check_user_options()<cr>'
  280. let cnt += 1
  281. let num -= 1
  282. if !num
  283. break
  284. endif
  285. endfor
  286. call append('$', '')
  287. return cnt
  288. endif
  289. endfunction
  290. " Function: s:show_sessions {{{1
  291. function! s:show_sessions(cnt) abort
  292. let sfiles = split(globpath(s:session_dir, '*'), '\n')
  293. let slen = len(sfiles)
  294. if empty(sfiles)
  295. if exists('s:last_message')
  296. unlet s:last_message
  297. endif
  298. return a:cnt
  299. endif
  300. let cnt = a:cnt
  301. if exists('s:last_message')
  302. call s:print_section_header()
  303. endif
  304. for i in range(slen)
  305. let idx = (i + cnt)
  306. let index = s:get_index_as_string(idx)
  307. call append('$', ' ['. index .']'. repeat(' ', (3 - strlen(index))) . fnamemodify(sfiles[i], ':t:r'))
  308. execute 'nnoremap <buffer>' index ':source' fnameescape(sfiles[i]) '<cr>'
  309. endfor
  310. call append('$', '')
  311. return idx + 1
  312. endfunction
  313. " Function: s:show_bookmarks {{{1
  314. function! s:show_bookmarks(cnt) abort
  315. let cnt = a:cnt
  316. if exists('g:startify_bookmarks')
  317. if exists('s:last_message')
  318. call s:print_section_header()
  319. endif
  320. for fname in g:startify_bookmarks
  321. let index = s:get_index_as_string(cnt)
  322. call append('$', ' ['. index .']'. repeat(' ', (3 - strlen(index))) . fname)
  323. execute 'nnoremap <buffer>' index ':edit' fnameescape(fname) '<bar> call <sid>check_user_options()<cr>'
  324. let cnt += 1
  325. endfor
  326. call append('$', '')
  327. endif
  328. return cnt
  329. endfunction
  330. " Function: s:is_in_skiplist {{{1
  331. function! s:is_in_skiplist(arg) abort
  332. for regexp in g:startify_skiplist
  333. if (a:arg =~# regexp)
  334. return 1
  335. endif
  336. endfor
  337. endfunction
  338. " Function: s:is_bookmark {{{1
  339. function! s:is_bookmark(arg) abort
  340. for foo in map(filter(copy(g:startify_bookmarks), '!isdirectory(v:val)'), 'resolve(fnamemodify(v:val, ":p"))')
  341. if foo == a:arg
  342. return 1
  343. endif
  344. endfor
  345. endfunction
  346. " Function: s:set_cursor {{{1
  347. function! s:set_cursor() abort
  348. let s:oldline = exists('s:newline') ? s:newline : 5
  349. let s:newline = line('.')
  350. " going up (-1) or down (1)
  351. let movement = 2 * (s:newline > s:oldline) - 1
  352. " skip section headers lines until an entry is found
  353. while index(s:section_header_lines, s:newline) != -1
  354. let s:newline += movement
  355. endwhile
  356. " skip blank lines between lists
  357. if empty(getline(s:newline))
  358. let s:newline += movement
  359. endif
  360. " don't go beyond first or last entry
  361. let s:newline = max([s:firstline, min([s:lastline, s:newline])])
  362. call cursor(s:newline, 5)
  363. endfunction
  364. " Function: s:set_mark {{{1
  365. "
  366. " Markers are saved in the s:marked dict using the follow format:
  367. " - s:marked[0]: ID
  368. " - s:marked[1]: path
  369. " - s:marked[2]: type (buffer, split, vsplit)
  370. "
  371. function! s:set_mark(type) abort
  372. if !exists('s:marked')
  373. let s:marked = {}
  374. endif
  375. let [id, path] = matchlist(getline('.'), '\v\[(.*)\]\s+(.*)')[1:2]
  376. if path =~# '\V<empty buffer>\|<quit>' || path =~# '^\w\+$'
  377. return
  378. endif
  379. setlocal modifiable
  380. " set markers
  381. if id =~# '[BSTV]'
  382. " replace marker by old ID
  383. execute 'normal! ci]'. remove(s:marked, line('.'))[0]
  384. else
  385. " save ID and replace it by the marker of the given type
  386. let s:marked[line('.')] = [id, path, a:type]
  387. execute 'normal! ci]'. repeat(a:type, len(id))
  388. endif
  389. setlocal nomodifiable nomodified
  390. endfunction
  391. " Function: s:open_buffers {{{1
  392. function! s:open_buffers(cword) abort
  393. " markers found; open one or more buffers
  394. if exists('s:marked') && !empty(s:marked)
  395. enew
  396. setlocal nobuflisted
  397. for val in values(s:marked)
  398. let [path, type] = val[1:2]
  399. let path = fnameescape(path)
  400. if line2byte('$') == -1
  401. " open in current window
  402. execute 'edit' path
  403. elseif type == 'S'
  404. " open in split
  405. execute 'split' path
  406. elseif type == 'V'
  407. " open in vsplit
  408. execute 'vsplit' path
  409. elseif type == 'T'
  410. " open in tab
  411. execute 'tabnew' path
  412. else
  413. " open in current window
  414. execute 'edit' path
  415. endif
  416. call s:check_user_options()
  417. endfor
  418. " remove markers for next instance of :Startify
  419. if exists('s:marked')
  420. unlet s:marked
  421. endif
  422. " no markers found; open a single buffer
  423. else
  424. try
  425. execute 'normal' a:cword
  426. catch /E832/ " don't ask for undo encryption key twice
  427. edit
  428. catch /E325/ " swap file found
  429. endtry
  430. endif
  431. endfunction
  432. " Function: s:check_user_options {{{1
  433. function! s:check_user_options() abort
  434. let path = expand('%')
  435. let session = path . s:sep .'Session.vim'
  436. " autoload session
  437. if get(g:, 'startify_session_autoload') && filereadable(session)
  438. execute 'source' session
  439. " change to VCS root directory
  440. elseif get(g:, 'startify_change_to_vcs_root')
  441. call s:cd_to_vcs_root(path)
  442. " change directory
  443. elseif get(g:, 'startify_change_to_dir', 1)
  444. if isdirectory(path)
  445. lcd %
  446. else
  447. lcd %:h
  448. endif
  449. endif
  450. endfunction
  451. " Function: s:cd_to_vcs_root {{{1
  452. function! s:cd_to_vcs_root(path) abort
  453. let dir = fnamemodify(a:path, ':p:h')
  454. for vcs in [ '.git', '.hg', '.bzr', '.svn' ]
  455. let root = finddir(vcs, dir .';')
  456. if !empty(root)
  457. execute 'cd '. fnamemodify(root, ':h')
  458. return
  459. endif
  460. endfor
  461. endfunction
  462. " Function: s:close {{{1
  463. function! s:close() abort
  464. if len(filter(range(0, bufnr('$')), 'buflisted(v:val)'))
  465. if bufloaded(bufnr('#'))
  466. buffer #
  467. else
  468. bnext
  469. endif
  470. else
  471. quit
  472. endif
  473. endfunction
  474. " Function: s:get_index_as_string {{{1
  475. function! s:get_index_as_string(idx) abort
  476. if exists('g:startify_custom_indices')
  477. let listlen = len(g:startify_custom_indices)
  478. return (a:idx < listlen) ? g:startify_custom_indices[a:idx] : string(a:idx - listlen)
  479. else
  480. return string(a:idx)
  481. endif
  482. endfunction
  483. " Function: s:restore_position {{{1
  484. function! s:restore_position() abort
  485. autocmd! startify *
  486. if line("'\"") > 0 && line("'\"") <= line('$')
  487. call cursor(getpos("'\"")[1:])
  488. endif
  489. endfunction
  490. " Function: s:session_write {{{1
  491. function! s:session_write(spath)
  492. let ssop = &sessionoptions
  493. try
  494. set sessionoptions-=options
  495. execute 'mksession!' a:spath
  496. catch
  497. execute 'echoerr' string(v:exception)
  498. finally
  499. let &sessionoptions = ssop
  500. endtry
  501. if exists('g:startify_session_savevars') || exists('g:startify_session_savecmds')
  502. execute 'split' a:spath
  503. " put existing variables from savevars into session file
  504. call append(line('$')-3, map(filter(get(g:, 'startify_session_savevars', []), 'exists(v:val)'), '"let ". v:val ." = ". strtrans(string(eval(v:val)))'))
  505. " put commands from savecmds into session file
  506. call append(line('$')-3, get(g:, 'startify_session_savecmds', []))
  507. setlocal bufhidden=delete
  508. silent update
  509. silent hide
  510. endif
  511. endfunction
  512. " Function: s:print_section_header {{{1
  513. function! s:print_section_header() abort
  514. $
  515. let curline = line('.')
  516. for lnum in range(curline, curline + len(s:last_message) + 1)
  517. call add(s:section_header_lines, lnum)
  518. endfor
  519. call append('$', s:last_message + [''])
  520. unlet s:last_message
  521. endfunction