startify.vim 17 KB

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