startify.vim 16 KB

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