startify.vim 16 KB

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