startify.vim 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  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. execute 'mksession!' a:spath
  189. catch
  190. execute 'echoerr' string(v:exception)
  191. finally
  192. let &sessionoptions = ssop
  193. endtry
  194. if exists('g:startify_session_savevars') || exists('g:startify_session_savecmds')
  195. execute 'split' a:spath
  196. " put existing variables from savevars into session file
  197. call append(line('$')-3, map(filter(get(g:, 'startify_session_savevars', []), 'exists(v:val)'), '"let ". v:val ." = ". strtrans(string(eval(v:val)))'))
  198. " put commands from savecmds into session file
  199. call append(line('$')-3, get(g:, 'startify_session_savecmds', []))
  200. setlocal bufhidden=delete
  201. silent update
  202. silent hide
  203. endif
  204. endfunction
  205. " Function: #session_delete {{{1
  206. function! startify#session_delete(...) abort
  207. if !isdirectory(s:session_dir)
  208. echo 'The session directory does not exist: '. s:session_dir
  209. return
  210. elseif empty(startify#session_list_as_string(''))
  211. echo 'There are no sessions...'
  212. return
  213. endif
  214. let spath = s:session_dir . s:sep . (exists('a:1')
  215. \ ? a:1
  216. \ : input('Delete this session: ', fnamemodify(v:this_session, ':t'), 'custom,startify#session_list_as_string'))
  217. \ | redraw
  218. echo 'Really delete '. spath .'? [y/n]' | redraw
  219. if (nr2char(getchar()) == 'y')
  220. if delete(spath) == 0
  221. echo 'Deleted session '. spath .'!'
  222. else
  223. echo 'Deletion failed!'
  224. endif
  225. else
  226. echo 'Deletion aborted!'
  227. endif
  228. endfunction
  229. " Function: #session_delete_buffers {{{1
  230. function! startify#session_delete_buffers() abort
  231. if !s:delete_buffers
  232. return
  233. endif
  234. let n = 1
  235. while n <= bufnr('$')
  236. if buflisted(n)
  237. silent execute 'bdelete' n
  238. endif
  239. let n += 1
  240. endwhile
  241. endfunction
  242. " Function: #session_list {{{1
  243. function! startify#session_list(lead, ...) abort
  244. return map(split(globpath(s:session_dir, '*'.a:lead.'*'), '\n'), 'fnamemodify(v:val, ":t")')
  245. endfunction
  246. " Function: #session_list_as_string {{{1
  247. function! startify#session_list_as_string(lead, ...) abort
  248. return join(map(split(globpath(s:session_dir, '*'.a:lead.'*'), '\n'), 'fnamemodify(v:val, ":t")'), "\n")
  249. endfunction
  250. " Function: s:show_dir {{{1
  251. function! s:show_dir(cnt) abort
  252. if empty(v:oldfiles)
  253. return a:cnt
  254. endif
  255. let cnt = a:cnt
  256. let num = s:numfiles
  257. let entries = {}
  258. let cwd = escape(getcwd(), '\')
  259. let files = filter(map(copy(v:oldfiles),
  260. \ 'glob(fnameescape(fnamemodify(resolve(v:val), ":p")))'), 'match(v:val, cwd) == 0')
  261. if !empty(files)
  262. if exists('s:last_message')
  263. call s:print_section_header()
  264. endif
  265. for abs_path in files
  266. let abs_path = glob(abs_path)
  267. " filter duplicates, bookmarks and entries from the skiplist
  268. if has_key(entries, abs_path)
  269. \ || !filereadable(abs_path)
  270. \ || (exists('g:startify_skiplist') && s:is_in_skiplist(abs_path))
  271. \ || (exists('g:startify_bookmarks') && s:is_bookmark(abs_path))
  272. continue
  273. endif
  274. let entries[abs_path] = 1
  275. let index = s:get_index_as_string(cnt)
  276. let display_path = fnamemodify(abs_path, s:relative_path ? ':.' : ':p:~')
  277. call append('$', ' ['. index .']'. repeat(' ', (3 - strlen(index))) . display_path)
  278. execute 'nnoremap <buffer><silent>' index ':edit' escape(abs_path, ' ') '<bar> call <sid>check_user_options()<cr>'
  279. let cnt += 1
  280. let num -= 1
  281. if !num
  282. break
  283. endif
  284. endfor
  285. call append('$', '')
  286. endif
  287. return cnt
  288. endfunction
  289. " Function: s:show_files {{{1
  290. function! s:show_files(cnt) abort
  291. if empty(v:oldfiles)
  292. return a:cnt
  293. endif
  294. if exists('s:last_message')
  295. call s:print_section_header()
  296. endif
  297. let cnt = a:cnt
  298. let num = s:numfiles
  299. let entries = {}
  300. for fname in v:oldfiles
  301. let abs_path = glob(fnameescape(fnamemodify(resolve(fname), ':p')))
  302. " filter duplicates, bookmarks and entries from the skiplist
  303. if has_key(entries, abs_path)
  304. \ || !filereadable(abs_path)
  305. \ || (exists('g:startify_skiplist') && s:is_in_skiplist(abs_path))
  306. \ || (exists('g:startify_bookmarks') && s:is_bookmark(abs_path))
  307. continue
  308. endif
  309. let entries[abs_path] = 1
  310. let index = s:get_index_as_string(cnt)
  311. let display_path = fnamemodify(abs_path, s:relative_path ? ':.' : ':p:~')
  312. call append('$', ' ['. index .']'. repeat(' ', (3 - strlen(index))) . display_path)
  313. execute 'nnoremap <buffer><silent>' index ':edit' escape(abs_path, ' ') '<bar> call <sid>check_user_options()<cr>'
  314. let cnt += 1
  315. let num -= 1
  316. if !num
  317. break
  318. endif
  319. endfor
  320. call append('$', '')
  321. return cnt
  322. endfunction
  323. " Function: s:show_sessions {{{1
  324. function! s:show_sessions(cnt) abort
  325. let sfiles = split(globpath(s:session_dir, '*'), '\n')
  326. if empty(sfiles)
  327. if exists('s:last_message')
  328. unlet s:last_message
  329. endif
  330. return a:cnt
  331. endif
  332. if exists('s:last_message')
  333. call s:print_section_header()
  334. endif
  335. let cnt = a:cnt
  336. for i in range(len(sfiles))
  337. let index = s:get_index_as_string(cnt)
  338. call append('$', ' ['. index .']'. repeat(' ', (3 - strlen(index))) . fnamemodify(sfiles[i], ':t'))
  339. execute 'nnoremap <buffer><silent>' index ':enew <bar> SLoad' fnamemodify(sfiles[i], ':t') '<cr>'
  340. let cnt += 1
  341. endfor
  342. call append('$', '')
  343. return cnt
  344. endfunction
  345. " Function: s:show_bookmarks {{{1
  346. function! s:show_bookmarks(cnt) abort
  347. if !exists('g:startify_bookmarks')
  348. return a:cnt
  349. endif
  350. if exists('s:last_message')
  351. call s:print_section_header()
  352. endif
  353. let cnt = a:cnt
  354. for fname in g:startify_bookmarks
  355. let index = s:get_index_as_string(cnt)
  356. call append('$', ' ['. index .']'. repeat(' ', (3 - strlen(index))) . fname)
  357. execute 'nnoremap <buffer><silent>' index ':edit' fnameescape(fname) '<bar> call <sid>check_user_options()<cr>'
  358. let cnt += 1
  359. endfor
  360. call append('$', '')
  361. return cnt
  362. endfunction
  363. " Function: s:is_in_skiplist {{{1
  364. function! s:is_in_skiplist(arg) abort
  365. for regexp in g:startify_skiplist
  366. if (a:arg =~# regexp)
  367. return 1
  368. endif
  369. endfor
  370. endfunction
  371. " Function: s:is_bookmark {{{1
  372. function! s:is_bookmark(arg) abort
  373. for foo in map(filter(copy(g:startify_bookmarks), '!isdirectory(v:val)'), 'resolve(fnamemodify(v:val, ":p"))')
  374. if foo == a:arg
  375. return 1
  376. endif
  377. endfor
  378. endfunction
  379. " Function: s:set_cursor {{{1
  380. function! s:set_cursor() abort
  381. let s:oldline = exists('s:newline') ? s:newline : 5
  382. let s:newline = line('.')
  383. " going up (-1) or down (1)
  384. let movement = 2 * (s:newline > s:oldline) - 1
  385. " skip section headers lines until an entry is found
  386. while index(s:section_header_lines, s:newline) != -1
  387. let s:newline += movement
  388. endwhile
  389. " skip blank lines between lists
  390. if empty(getline(s:newline))
  391. let s:newline += movement
  392. endif
  393. " don't go beyond first or last entry
  394. let s:newline = max([s:firstline, min([s:lastline, s:newline])])
  395. call cursor(s:newline, 5)
  396. endfunction
  397. " Function: s:set_mark {{{1
  398. "
  399. " Markers are saved in the s:marked dict using the follow format:
  400. " - s:marked[0]: ID
  401. " - s:marked[1]: path
  402. " - s:marked[2]: type (buffer, split, vsplit)
  403. "
  404. function! s:set_mark(type) abort
  405. if !exists('s:marked')
  406. let s:marked = {}
  407. endif
  408. let [id, path] = matchlist(getline('.'), '\v\[(.{-})\]\s+(.*)')[1:2]
  409. let path = fnamemodify(path, ':p')
  410. if path =~# '\V<empty buffer>\|<quit>' || path =~# '^\w\+$'
  411. return
  412. endif
  413. setlocal modifiable
  414. " set markers
  415. if id =~# '[BSTV]'
  416. " replace marker by old ID
  417. execute 'normal! ci]'. remove(s:marked, line('.'))[0]
  418. else
  419. " save ID and replace it by the marker of the given type
  420. let s:marked[line('.')] = [id, path, a:type]
  421. execute 'normal! ci]'. repeat(a:type, len(id))
  422. endif
  423. setlocal nomodifiable nomodified
  424. endfunction
  425. " Function: s:open_buffers {{{1
  426. function! s:open_buffers(cword) abort
  427. " markers found; open one or more buffers
  428. if exists('s:marked') && !empty(s:marked)
  429. enew
  430. setlocal nobuflisted
  431. for val in values(s:marked)
  432. let [path, type] = val[1:2]
  433. let path = fnameescape(path)
  434. if line2byte('$') == -1
  435. " open in current window
  436. execute 'edit' path
  437. elseif type == 'S'
  438. " open in split
  439. execute 'split' path
  440. elseif type == 'V'
  441. " open in vsplit
  442. execute 'vsplit' path
  443. elseif type == 'T'
  444. " open in tab
  445. execute 'tabnew' path
  446. else
  447. " open in current window
  448. execute 'edit' path
  449. endif
  450. call s:check_user_options()
  451. endfor
  452. " remove markers for next instance of :Startify
  453. if exists('s:marked')
  454. unlet s:marked
  455. endif
  456. " no markers found; open a single buffer
  457. else
  458. try
  459. execute 'normal' a:cword
  460. catch /E832/ " don't ask for undo encryption key twice
  461. edit
  462. catch /E325/ " swap file found
  463. endtry
  464. endif
  465. endfunction
  466. " Function: s:check_user_options {{{1
  467. function! s:check_user_options() abort
  468. let path = expand('%')
  469. let session = path . s:sep .'Session.vim'
  470. " autoload session
  471. if get(g:, 'startify_session_autoload') && filereadable(session)
  472. execute 'source' session
  473. " change to VCS root directory
  474. elseif get(g:, 'startify_change_to_vcs_root')
  475. call s:cd_to_vcs_root(path)
  476. " change directory
  477. elseif get(g:, 'startify_change_to_dir', 1)
  478. if isdirectory(path)
  479. lcd %
  480. else
  481. lcd %:h
  482. endif
  483. endif
  484. endfunction
  485. " Function: s:cd_to_vcs_root {{{1
  486. function! s:cd_to_vcs_root(path) abort
  487. let dir = fnamemodify(a:path, ':p:h')
  488. for vcs in [ '.git', '.hg', '.bzr', '.svn' ]
  489. let root = finddir(vcs, dir .';')
  490. if !empty(root)
  491. execute 'cd '. fnamemodify(root, ':h')
  492. return
  493. endif
  494. endfor
  495. endfunction
  496. " Function: s:close {{{1
  497. function! s:close() abort
  498. if len(filter(range(0, bufnr('$')), 'buflisted(v:val)'))
  499. if bufloaded(bufnr('#'))
  500. buffer #
  501. else
  502. bnext
  503. endif
  504. else
  505. quit
  506. endif
  507. endfunction
  508. " Function: s:get_index_as_string {{{1
  509. function! s:get_index_as_string(idx) abort
  510. if exists('g:startify_custom_indices')
  511. let listlen = len(g:startify_custom_indices)
  512. return (a:idx < listlen) ? g:startify_custom_indices[a:idx] : string(a:idx - listlen)
  513. else
  514. return string(a:idx)
  515. endif
  516. endfunction
  517. " Function: s:restore_position {{{1
  518. function! s:restore_position() abort
  519. autocmd! startify *
  520. if line("'\"") > 0 && line("'\"") <= line('$')
  521. call cursor(getpos("'\"")[1:])
  522. endif
  523. endfunction
  524. " Function: s:print_section_header {{{1
  525. function! s:print_section_header() abort
  526. $
  527. let curline = line('.')
  528. for lnum in range(curline, curline + len(s:last_message) + 1)
  529. call add(s:section_header_lines, lnum)
  530. endfor
  531. call append('$', s:last_message + [''])
  532. unlet s:last_message
  533. endfunction