startify.vim 16 KB

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