startify.vim 17 KB

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