startify.vim 16 KB

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