startify.vim 19 KB

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