startify.vim 18 KB

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