startify.vim 18 KB

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