startify.vim 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  1. " vim: et sw=2 sts=2
  2. " Plugin: https://github.com/mhinz/vim-startify
  3. " Description: A fancy start screen for Vim.
  4. " Maintainer: Marco Hinz <http://github.com/mhinz>
  5. if exists('g:autoloaded_startify') || &compatible
  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:delete_buffers = get(g:, 'startify_session_delete_buffers')
  13. let s:relative_path = get(g:, 'startify_relative_path') ? ':.' : ':p:~'
  14. let s:session_dir = resolve(expand(get(g:, 'startify_session_dir',
  15. \ has('win32') ? '$HOME\vimfiles\session' : '~/.vim/session')))
  16. let s:skiplist = get(g:, 'startify_skiplist', [
  17. \ 'COMMIT_EDITMSG',
  18. \ fnamemodify($VIMRUNTIME, ':p') .'/doc',
  19. \ 'bundle/.*/doc',
  20. \ ])
  21. " Function: #get_separator {{{1
  22. function! startify#get_separator() abort
  23. return !exists('+shellslash') || &shellslash ? '/' : '\'
  24. endfunction
  25. let s:sep = startify#get_separator()
  26. " Function: #get_lastline {{{1
  27. function! startify#get_lastline() abort
  28. return s:lastline + 1
  29. endfunction
  30. " Function: #insane_in_the_membrane {{{1
  31. function! startify#insane_in_the_membrane() abort
  32. if &insertmode
  33. return
  34. endif
  35. if !empty(v:servername) && exists('g:startify_skiplist_server')
  36. for servname in g:startify_skiplist_server
  37. if servname == v:servername
  38. return
  39. endif
  40. endfor
  41. endif
  42. setlocal
  43. \ bufhidden=wipe
  44. \ buftype=nofile
  45. \ nobuflisted
  46. \ nocursorcolumn
  47. \ nocursorline
  48. \ nolist
  49. \ nonumber
  50. \ noswapfile
  51. if empty(&statusline)
  52. setlocal statusline=\ startify
  53. endif
  54. if v:version >= 703
  55. setlocal norelativenumber
  56. endif
  57. if exists('g:startify_custom_header')
  58. call append('$', g:startify_custom_header)
  59. endif
  60. if s:show_special
  61. call append('$', [' [e] <empty buffer>', ''])
  62. endif
  63. let s:entry_number = 0
  64. if filereadable('Session.vim')
  65. call append('$', [' [0] '. getcwd() . s:sep .'Session.vim', ''])
  66. execute 'nnoremap <silent><buffer> 0 :call startify#session_delete_buffers() <bar> source Session.vim<cr>'
  67. let s:entry_number = 1
  68. let l:show_session = 1
  69. endif
  70. if empty(v:oldfiles)
  71. echohl WarningMsg
  72. echomsg "startify: Can't read viminfo file. Read :help startify-faq-02"
  73. echohl None
  74. endif
  75. let b:startify_section_header_lines = []
  76. let s:lists = get(g:, 'startify_list_order', [
  77. \ [' Most recently used files:'],
  78. \ 'files',
  79. \ [' Most recently used files in the current directory:'],
  80. \ 'dir',
  81. \ [' My sessions:'],
  82. \ 'sessions',
  83. \ [' My bookmarks:'],
  84. \ 'bookmarks',
  85. \ ])
  86. let s:tick = 0
  87. let s:entries = {}
  88. let s:markers = {}
  89. for item in s:lists
  90. if type(item) == 1
  91. call s:show_{item}()
  92. else
  93. let s:last_message = item
  94. endif
  95. unlet item
  96. endfor
  97. silent $delete _
  98. if s:show_special
  99. call append('$', ['', ' [q] <quit>'])
  100. endif
  101. " compute first line offset
  102. let s:firstline = 2
  103. " increase offset if there is a custom header
  104. if exists('g:startify_custom_header')
  105. let s:firstline += len(g:startify_custom_header)
  106. endif
  107. " no special, no local Session.vim, but a section header
  108. if !s:show_special && !exists('l:show_session') && type(s:lists[0]) == 3
  109. let s:firstline += len(s:lists[0]) + 1
  110. endif
  111. let s:lastline = line('$')
  112. if exists('g:startify_custom_footer')
  113. call append('$', g:startify_custom_footer)
  114. endif
  115. setlocal nomodifiable nomodified
  116. call s:set_mappings()
  117. call cursor(s:firstline + (s:show_special ? 2 : 0), 5)
  118. autocmd startify CursorMoved <buffer> call s:set_cursor()
  119. set filetype=startify
  120. silent! doautocmd <nomodeline> User Startified
  121. endfunction
  122. " Function: #session_load {{{1
  123. function! startify#session_load(...) abort
  124. if !isdirectory(s:session_dir)
  125. echomsg 'The session directory does not exist: '. s:session_dir
  126. return
  127. elseif empty(startify#session_list_as_string(''))
  128. echomsg 'There are no sessions...'
  129. return
  130. endif
  131. call inputsave()
  132. let spath = s:session_dir . s:sep . (exists('a:1')
  133. \ ? a:1
  134. \ : input('Load this session: ', fnamemodify(v:this_session, ':t'), 'custom,startify#session_list_as_string'))
  135. \ | redraw
  136. call inputrestore()
  137. if filereadable(spath)
  138. if get(g:, 'startify_session_persistence')
  139. \ && exists('v:this_session')
  140. \ && filewritable(v:this_session)
  141. call startify#session_write(fnameescape(v:this_session))
  142. endif
  143. call startify#session_delete_buffers()
  144. execute 'source '. fnameescape(spath)
  145. else
  146. echo 'No such file: '. spath
  147. endif
  148. endfunction
  149. " Function: #session_save {{{1
  150. function! startify#session_save(...) abort
  151. if !isdirectory(s:session_dir)
  152. if exists('*mkdir')
  153. echo 'The session directory does not exist: '. s:session_dir .'. Create it? [y/n]'
  154. if (nr2char(getchar()) == 'y')
  155. call mkdir(s:session_dir, 'p')
  156. else
  157. echo
  158. return
  159. endif
  160. else
  161. echo 'The session directory does not exist: '. s:session_dir
  162. return
  163. endif
  164. endif
  165. call inputsave()
  166. let sname = exists('a:1')
  167. \ ? a:1
  168. \ : input('Save under this session name: ', fnamemodify(v:this_session, ':t'), 'custom,startify#session_list_as_string')
  169. \ | redraw
  170. call inputrestore()
  171. if empty(sname)
  172. echo 'You gave an empty name!'
  173. return
  174. endif
  175. let spath = s:session_dir . s:sep . sname
  176. if !filereadable(spath)
  177. call startify#session_write(fnameescape(spath))
  178. echo 'Session saved under: '. spath
  179. return
  180. endif
  181. echo 'Session already exists. Overwrite? [y/n]' | redraw
  182. if nr2char(getchar()) == 'y'
  183. call startify#session_write(fnameescape(spath))
  184. echo 'Session saved under: '. spath
  185. else
  186. echo 'Did NOT save the session!'
  187. endif
  188. endfunction
  189. " Function: #session_close {{{1
  190. function! startify#session_close() abort
  191. if exists('v:this_session') && filewritable(v:this_session)
  192. call startify#session_write(fnameescape(v:this_session))
  193. let v:this_session = ''
  194. endif
  195. call startify#session_delete_buffers()
  196. Startify
  197. endfunction
  198. " Function: #session_write {{{1
  199. function! startify#session_write(spath)
  200. let ssop = &sessionoptions
  201. try
  202. " if this function is called while being in the Startify buffer
  203. " (by loading another session or running :SSave/:SLoad directly)
  204. " switch back to the previous buffer before saving the session
  205. if &filetype == 'startify'
  206. let callingbuffer = bufnr('#')
  207. if callingbuffer > 0
  208. execute 'buffer' callingbuffer
  209. endif
  210. endif
  211. " prevent saving already deleted buffers that were in the arglist
  212. for arg in argv()
  213. if !buflisted(arg)
  214. execute 'argdelete' fnameescape(arg)
  215. endif
  216. endfor
  217. set sessionoptions-=options
  218. execute 'mksession!' a:spath
  219. catch
  220. execute 'echoerr' string(v:exception)
  221. finally
  222. let &sessionoptions = ssop
  223. endtry
  224. if exists('g:startify_session_remove_lines')
  225. \ || exists('g:startify_session_savevars')
  226. \ || exists('g:startify_session_savecmds')
  227. execute 'split' a:spath
  228. " remove lines from the session file
  229. if exists('g:startify_session_remove_lines')
  230. for pattern in g:startify_session_remove_lines
  231. execute 'silent global/'. pattern .'/delete'
  232. endfor
  233. endif
  234. " put existing variables from savevars into session file
  235. if exists('g:startify_session_savevars')
  236. call append(line('$')-3, map(filter(copy(g:startify_session_savevars), 'exists(v:val)'), '"let ". v:val ." = ". strtrans(string(eval(v:val)))'))
  237. endif
  238. " put commands from savecmds into session file
  239. if exists('g:startify_session_savecmds')
  240. call append(line('$')-3, g:startify_session_savecmds)
  241. endif
  242. setlocal bufhidden=delete
  243. silent update
  244. silent hide
  245. endif
  246. endfunction
  247. " Function: #session_delete {{{1
  248. function! startify#session_delete(...) abort
  249. if !isdirectory(s:session_dir)
  250. echo 'The session directory does not exist: '. s:session_dir
  251. return
  252. elseif empty(startify#session_list_as_string(''))
  253. echo 'There are no sessions...'
  254. return
  255. endif
  256. call inputsave()
  257. let spath = s:session_dir . s:sep . (exists('a:1')
  258. \ ? a:1
  259. \ : input('Delete this session: ', fnamemodify(v:this_session, ':t'), 'custom,startify#session_list_as_string'))
  260. \ | redraw
  261. call inputrestore()
  262. echo 'Really delete '. spath .'? [y/n]' | redraw
  263. if (nr2char(getchar()) == 'y')
  264. if delete(spath) == 0
  265. echo 'Deleted session '. spath .'!'
  266. else
  267. echo 'Deletion failed!'
  268. endif
  269. else
  270. echo 'Deletion aborted!'
  271. endif
  272. endfunction
  273. " Function: #session_delete_buffers {{{1
  274. function! startify#session_delete_buffers() abort
  275. if !s:delete_buffers
  276. return
  277. endif
  278. let n = 1
  279. while n <= bufnr('$')
  280. if buflisted(n)
  281. silent execute 'bdelete' n
  282. endif
  283. let n += 1
  284. endwhile
  285. endfunction
  286. " Function: #session_list {{{1
  287. function! startify#session_list(lead, ...) abort
  288. return map(split(globpath(s:session_dir, '*'.a:lead.'*'), '\n'), 'fnamemodify(v:val, ":t")')
  289. endfunction
  290. " Function: #session_list_as_string {{{1
  291. function! startify#session_list_as_string(lead, ...) abort
  292. return join(map(split(globpath(s:session_dir, '*'.a:lead.'*'), '\n'), 'fnamemodify(v:val, ":t")'), "\n")
  293. endfunction
  294. " Function: #open_buffers {{{1
  295. function! startify#open_buffers(...) abort
  296. if exists('a:1') " used in mappings
  297. execute 'edit' s:entries[a:1]
  298. elseif empty(s:markers) " open the current entry
  299. call s:set_mark('B')
  300. return startify#open_buffers()
  301. else " open all marked entries in the order they were given
  302. enew
  303. setlocal nobuflisted
  304. for markers in sort(values(s:markers), 's:sort_by_tick')
  305. let path = s:entries[markers.original_index]
  306. let type = markers.type
  307. if line2byte('$') == -1
  308. execute 'edit' path
  309. elseif type == 'S'
  310. execute 'split' path
  311. elseif type == 'V'
  312. execute 'vsplit' path
  313. elseif type == 'T'
  314. execute 'tabnew' path
  315. else
  316. execute 'edit' path
  317. endif
  318. call s:check_user_options()
  319. endfor
  320. endif
  321. endfunction
  322. " Function: s:display_by_path {{{1
  323. function! s:display_by_path(path_prefix, path_format) abort
  324. let oldfiles = call(get(g:, 'startify_enable_unsafe') ? 's:filter_oldfiles_unsafe' : 's:filter_oldfiles',
  325. \ [a:path_prefix, a:path_format])
  326. if !empty(oldfiles)
  327. if exists('s:last_message')
  328. call s:print_section_header()
  329. endif
  330. for [absolute_path, entry_path] in oldfiles
  331. let index = s:get_index_as_string(s:entry_number)
  332. call append('$', ' ['. index .']'. repeat(' ', (3 - strlen(index))) . entry_path)
  333. if has('win32')
  334. let absolute_path = substitute(absolute_path, '\[', '\[[]', 'g')
  335. endif
  336. let s:entries[index] = absolute_path
  337. let s:entry_number += 1
  338. endfor
  339. call append('$', '')
  340. endif
  341. endfunction
  342. " Function: s:filter_oldfiles {{{1
  343. function! s:filter_oldfiles(path_prefix, path_format) abort
  344. let path_prefix = '\V'. escape(a:path_prefix, '\')
  345. let counter = s:numfiles
  346. let entries = {}
  347. let oldfiles = []
  348. for fname in v:oldfiles
  349. if counter <= 0
  350. break
  351. endif
  352. let absolute_path = fnamemodify(resolve(fname), ":p")
  353. " filter duplicates, bookmarks and entries from the skiplist
  354. if has_key(entries, absolute_path)
  355. \ || !filereadable(absolute_path)
  356. \ || s:is_in_skiplist(absolute_path)
  357. \ || (exists('g:startify_bookmarks') && s:is_bookmark(absolute_path))
  358. \ || match(absolute_path, path_prefix)
  359. continue
  360. endif
  361. let entry_path = fnamemodify(absolute_path, a:path_format)
  362. let entries[absolute_path] = 1
  363. let counter -= 1
  364. let oldfiles += [[fnameescape(absolute_path), entry_path]]
  365. endfor
  366. return oldfiles
  367. endfun
  368. " Function: s:filter_oldfiles_unsafe {{{1
  369. function! s:filter_oldfiles_unsafe(path_prefix, path_format) abort
  370. let path_prefix = '\V'. escape(a:path_prefix, '\')
  371. let counter = s:numfiles
  372. let entries = {}
  373. let oldfiles = []
  374. for fname in v:oldfiles
  375. if counter <= 0
  376. break
  377. endif
  378. let absolute_path = glob(fnamemodify(fname, ":p"))
  379. if empty(absolute_path)
  380. \ || has_key(entries, absolute_path)
  381. \ || s:is_in_skiplist(absolute_path)
  382. \ || match(absolute_path, path_prefix)
  383. continue
  384. endif
  385. let entry_path = fnamemodify(absolute_path, a:path_format)
  386. let entries[absolute_path] = 1
  387. let counter -= 1
  388. let oldfiles += [[fnameescape(absolute_path), entry_path]]
  389. endfor
  390. return oldfiles
  391. endfun
  392. " Function: s:show_dir {{{1
  393. function! s:show_dir() abort
  394. return s:display_by_path(getcwd(), ':.')
  395. endfunction
  396. " Function: s:show_files {{{1
  397. function! s:show_files() abort
  398. return s:display_by_path('', s:relative_path)
  399. endfunction
  400. " Function: s:show_sessions {{{1
  401. function! s:show_sessions() abort
  402. let sfiles = split(globpath(s:session_dir, '*'), '\n')
  403. if empty(sfiles)
  404. if exists('s:last_message')
  405. unlet s:last_message
  406. endif
  407. return
  408. endif
  409. if exists('s:last_message')
  410. call s:print_section_header()
  411. endif
  412. for i in range(len(sfiles))
  413. let index = s:get_index_as_string(s:entry_number)
  414. call append('$', ' ['. index .']'. repeat(' ', (3 - strlen(index))) . fnamemodify(sfiles[i], ':t'))
  415. execute 'nnoremap <buffer><silent>' index ':SLoad' fnamemodify(sfiles[i], ':t') '<cr>'
  416. let s:entry_number += 1
  417. endfor
  418. call append('$', '')
  419. endfunction
  420. " Function: s:show_bookmarks {{{1
  421. function! s:show_bookmarks() abort
  422. if !exists('g:startify_bookmarks') || empty(g:startify_bookmarks)
  423. return
  424. endif
  425. if exists('s:last_message')
  426. call s:print_section_header()
  427. endif
  428. for fname in g:startify_bookmarks
  429. let index = s:get_index_as_string(s:entry_number)
  430. call append('$', ' ['. index .']'. repeat(' ', (3 - strlen(index))) . fname)
  431. execute 'nnoremap <buffer><silent>' index ':edit' fnameescape(fname) '<bar> call <sid>check_user_options()<cr>'
  432. let s:entry_number += 1
  433. endfor
  434. call append('$', '')
  435. endfunction
  436. " Function: s:is_in_skiplist {{{1
  437. function! s:is_in_skiplist(arg) abort
  438. for regexp in s:skiplist
  439. if (a:arg =~# regexp)
  440. return 1
  441. endif
  442. endfor
  443. endfunction
  444. " Function: s:is_bookmark {{{1
  445. function! s:is_bookmark(arg) abort
  446. for foo in map(filter(copy(g:startify_bookmarks), '!isdirectory(v:val)'), 'resolve(fnamemodify(v:val, ":p"))')
  447. if foo == a:arg
  448. return 1
  449. endif
  450. endfor
  451. endfunction
  452. " Function: s:set_cursor {{{1
  453. function! s:set_cursor() abort
  454. let s:oldline = exists('s:newline') ? s:newline : 5
  455. let s:newline = line('.')
  456. " going up (-1) or down (1)
  457. let movement = 2 * (s:newline > s:oldline) - 1
  458. " skip section headers lines until an entry is found
  459. while index(b:startify_section_header_lines, s:newline) != -1
  460. let s:newline += movement
  461. endwhile
  462. " skip blank lines between lists
  463. if empty(getline(s:newline))
  464. let s:newline += movement
  465. endif
  466. " don't go beyond first or last entry
  467. let s:newline = max([s:firstline, min([s:lastline, s:newline])])
  468. call cursor(s:newline, 5)
  469. endfunction
  470. " Function: s:set_mappings {{{1
  471. function! s:set_mappings() abort
  472. for index in keys(s:entries)
  473. execute 'nnoremap <buffer><silent>' index ':call startify#open_buffers('. string(index) .')<cr>'
  474. endfor
  475. nnoremap <buffer><silent> e :enew<cr>
  476. nnoremap <buffer><silent> i :enew <bar> startinsert<cr>
  477. nnoremap <buffer><silent> <insert> :enew <bar> startinsert<cr>
  478. nnoremap <buffer><silent> b :call <sid>set_mark('B')<cr>
  479. nnoremap <buffer><silent> s :call <sid>set_mark('S')<cr>
  480. nnoremap <buffer><silent> t :call <sid>set_mark('T')<cr>
  481. nnoremap <buffer><silent> v :call <sid>set_mark('V')<cr>
  482. nnoremap <buffer><silent> <cr> :call startify#open_buffers()<cr>
  483. nnoremap <buffer><silent> <2-LeftMouse> :call startify#open_buffers()<cr>
  484. nnoremap <buffer><silent> q :call <sid>close()<cr>
  485. " Prevent 'nnoremap j gj' mappings, since they would break navigation.
  486. " (One can't leave the [x].)
  487. if !empty(maparg('j', 'n'))
  488. nnoremap <buffer> j j
  489. endif
  490. if !empty(maparg('k', 'n'))
  491. nnoremap <buffer> k k
  492. endif
  493. endfunction
  494. " Function: s:set_mark {{{1
  495. function! s:set_mark(type) abort
  496. let index = expand('<cword>')
  497. let line = line('.')
  498. if index =~# '[eq]'
  499. return
  500. endif
  501. setlocal modifiable
  502. if has_key(s:markers, line)
  503. if s:markers[line].type == a:type " remove type
  504. execute 'normal! ci]'. s:markers[line].original_index
  505. call remove(s:markers, line)
  506. else " update type
  507. execute 'normal! ci]'. repeat(a:type, len(index))
  508. let s:markers[line].type = a:type
  509. let s:markers[line].tick = s:tick
  510. let s:tick += 1
  511. endif
  512. else " set type
  513. let s:markers[line] = {
  514. \ 'line': line,
  515. \ 'type': a:type,
  516. \ 'original_index': index,
  517. \ 'tick': s:tick,
  518. \ }
  519. let s:tick += 1
  520. execute 'normal! ci]'. repeat(a:type, len(index))
  521. endif
  522. setlocal nomodifiable nomodified
  523. endfunction
  524. " Function: s:sort_by_tick {{{1
  525. function! s:sort_by_tick(one, two)
  526. return a:one.tick - a:two.tick
  527. endfunction
  528. " Function: s:check_user_options {{{1
  529. function! s:check_user_options() abort
  530. let path = expand('%')
  531. let session = path . s:sep .'Session.vim'
  532. " change to VCS root directory
  533. if get(g:, 'startify_session_autoload') && filereadable(session)
  534. execute 'source' session
  535. elseif get(g:, 'startify_change_to_vcs_root')
  536. call s:cd_to_vcs_root(path)
  537. " change directory
  538. elseif get(g:, 'startify_change_to_dir', 1)
  539. if isdirectory(path)
  540. lcd %
  541. else
  542. lcd %:h
  543. endif
  544. endif
  545. endfunction
  546. " Function: s:cd_to_vcs_root {{{1
  547. function! s:cd_to_vcs_root(path) abort
  548. let dir = fnamemodify(a:path, ':p:h')
  549. for vcs in [ '.git', '.hg', '.bzr', '.svn' ]
  550. let root = finddir(vcs, dir .';')
  551. if !empty(root)
  552. execute 'cd '. fnamemodify(root, ':h')
  553. return
  554. endif
  555. endfor
  556. endfunction
  557. " Function: s:close {{{1
  558. function! s:close() abort
  559. if len(filter(range(0, bufnr('$')), 'buflisted(v:val)'))
  560. if bufloaded(bufnr('#')) && bufnr('#') != bufnr('%')
  561. buffer #
  562. else
  563. bnext
  564. endif
  565. else
  566. quit
  567. endif
  568. endfunction
  569. " Function: s:get_index_as_string {{{1
  570. function! s:get_index_as_string(idx) abort
  571. if exists('g:startify_custom_indices')
  572. let listlen = len(g:startify_custom_indices)
  573. return (a:idx < listlen) ? g:startify_custom_indices[a:idx] : string(a:idx - listlen)
  574. else
  575. return string(a:idx)
  576. endif
  577. endfunction
  578. " Function: s:print_section_header {{{1
  579. function! s:print_section_header() abort
  580. $
  581. let curline = line('.')
  582. for lnum in range(curline, curline + len(s:last_message) + 1)
  583. call add(b:startify_section_header_lines, lnum)
  584. endfor
  585. call append('$', s:last_message + [''])
  586. unlet s:last_message
  587. endfunction