startify.vim 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  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. " Init: autocmds {{{1
  31. if get(g:, 'startify_session_persistence')
  32. autocmd startify VimLeave *
  33. \ if exists('v:this_session') && filewritable(v:this_session) |
  34. \ call s:session_write(fnameescape(v:this_session)) |
  35. \ endif
  36. endif
  37. " Function: #get_separator {{{1
  38. function! startify#get_separator() abort
  39. return !exists('+shellslash') || &shellslash ? '/' : '\'
  40. endfunction
  41. let s:sep = startify#get_separator()
  42. " Function: #get_lastline {{{1
  43. function! startify#get_lastline() abort
  44. return s:lastline
  45. endfunction
  46. " Function: #insane_in_the_membrane {{{1
  47. function! startify#insane_in_the_membrane() abort
  48. if !empty(v:servername) && exists('g:startify_skiplist_server')
  49. for servname in g:startify_skiplist_server
  50. if servname == v:servername
  51. return
  52. endif
  53. endfor
  54. endif
  55. setlocal noswapfile nobuflisted buftype=nofile bufhidden=wipe
  56. setlocal nonumber nocursorline nolist statusline=\ startify
  57. set filetype=startify
  58. if v:version >= 703
  59. setlocal norelativenumber
  60. endif
  61. let cnt = 0
  62. let s:headoff = 0
  63. if exists('g:startify_custom_header')
  64. call append('$', g:startify_custom_header)
  65. let s:headoff += len(g:startify_custom_header)
  66. endif
  67. if s:show_special
  68. call append('$', [' [e] <empty buffer>', ''])
  69. endif
  70. if get(g:, 'startify_session_detection', 1) && filereadable('Session.vim')
  71. call append('$', [' [0] '. getcwd() . s:sep .'Session.vim', ''])
  72. execute 'nnoremap <buffer> 0 :source Session.vim<cr>'
  73. let cnt = 1
  74. endif
  75. for item in s:lists
  76. if type(item) == 1
  77. let cnt = s:show_{item}(cnt)
  78. "unlet s:
  79. else
  80. let s:last_message = item
  81. endif
  82. unlet item
  83. endfor
  84. silent $delete
  85. if s:show_special
  86. call append('$', ['', ' [q] <quit>'])
  87. endif
  88. let s:lastline = line('$')
  89. if exists('g:startify_custom_footer')
  90. call append('$', g:startify_custom_footer)
  91. endif
  92. setlocal nomodifiable nomodified
  93. nnoremap <buffer><silent> e :enew<cr>
  94. nnoremap <buffer><silent> i :enew <bar> startinsert<cr>
  95. nnoremap <buffer><silent> <insert> :enew <bar> startinsert<cr>
  96. nnoremap <buffer><silent> b :call <sid>set_mark('B')<cr>
  97. nnoremap <buffer><silent> s :call <sid>set_mark('S')<cr>
  98. nnoremap <buffer><silent> t :call <sid>set_mark('T')<cr>
  99. nnoremap <buffer><silent> v :call <sid>set_mark('V')<cr>
  100. nnoremap <buffer> <cr> :call <sid>open_buffers(expand('<cword>'))<cr>
  101. nnoremap <buffer> <2-LeftMouse> :execute 'normal' matchstr(getline('.'), '\w\+')<cr>
  102. nnoremap <buffer><silent> q :call <sid>close()<cr>
  103. if exists('g:startify_empty_buffer_key')
  104. execute 'nnoremap <buffer><silent> '. g:startify_empty_buffer_key .' :enew<cr>'
  105. endif
  106. autocmd startify CursorMoved <buffer> call s:set_cursor()
  107. if s:restore_position
  108. autocmd startify BufReadPost * call s:restore_position()
  109. endif
  110. call cursor((s:show_special ? 4 : 2) + s:headoff, 5)
  111. silent! doautocmd <nomodeline> startify User
  112. endfunction
  113. " Function: #session_load {{{1
  114. function! startify#session_load(...) abort
  115. if !isdirectory(s:session_dir)
  116. echo 'The session directory does not exist: '. s:session_dir
  117. return
  118. elseif empty(startify#session_list_as_string(''))
  119. echo 'There are no sessions...'
  120. return
  121. endif
  122. let spath = s:session_dir . s:sep . (exists('a:1')
  123. \ ? a:1
  124. \ : input('Load this session: ', fnamemodify(v:this_session, ':t'), 'custom,startify#session_list_as_string'))
  125. \ | redraw
  126. if filereadable(spath)
  127. execute 'source '. fnameescape(spath)
  128. else
  129. echo 'No such file: '. spath
  130. endif
  131. endfunction
  132. " Function: #session_save {{{1
  133. function! startify#session_save(...) abort
  134. if !isdirectory(s:session_dir)
  135. if exists('*mkdir')
  136. echo 'The session directory does not exist: '. s:session_dir .'. Create it? [y/n]'
  137. if (nr2char(getchar()) == 'y')
  138. call mkdir(s:session_dir, 'p')
  139. else
  140. echo
  141. return
  142. endif
  143. else
  144. echo 'The session directory does not exist: '. s:session_dir
  145. return
  146. endif
  147. endif
  148. if exists('a:1')
  149. let sname = a:1
  150. else
  151. let sname = input('Save under this session name: ', fnamemodify(v:this_session, ':t'), 'custom,startify#session_list_as_string')
  152. redraw
  153. if empty(sname)
  154. echo 'You gave an empty name!'
  155. return
  156. endif
  157. endif
  158. let spath = s:session_dir . s:sep . sname
  159. if !filereadable(spath)
  160. call s:session_write(fnameescape(spath))
  161. echo 'Session saved under: '. spath
  162. return
  163. endif
  164. echo 'Session already exists. Overwrite? [y/n]' | redraw
  165. if nr2char(getchar()) == 'y'
  166. call s:session_write(fnameescape(spath))
  167. echo 'Session saved under: '. spath
  168. else
  169. echo 'Did NOT save the session!'
  170. endif
  171. endfunction
  172. " Function: #session_delete {{{1
  173. function! startify#session_delete(...) abort
  174. if !isdirectory(s:session_dir)
  175. echo 'The session directory does not exist: '. s:session_dir
  176. return
  177. elseif empty(startify#session_list_as_string(''))
  178. echo 'There are no sessions...'
  179. return
  180. endif
  181. let spath = s:session_dir . s:sep . (exists('a:1')
  182. \ ? a:1
  183. \ : input('Delete this session: ', fnamemodify(v:this_session, ':t'), 'custom,startify#session_list_as_string'))
  184. \ | redraw
  185. echo 'Really delete '. spath .'? [y/n]' | redraw
  186. if (nr2char(getchar()) == 'y')
  187. if delete(spath) == 0
  188. echo 'Deleted session '. spath .'!'
  189. else
  190. echo 'Deletion failed!'
  191. endif
  192. else
  193. echo 'Deletion aborted!'
  194. endif
  195. endfunction
  196. " Function: #session_list {{{1
  197. function! startify#session_list(lead, ...) abort
  198. return map(split(globpath(s:session_dir, '*'.a:lead.'*'), '\n'), 'fnamemodify(v:val, ":t")')
  199. endfunction
  200. " Function: #session_list_as_string {{{1
  201. function! startify#session_list_as_string(lead, ...) abort
  202. return join(map(split(globpath(s:session_dir, '*'.a:lead.'*'), '\n'), 'fnamemodify(v:val, ":t")'), "\n")
  203. endfunction
  204. " Function: s:show_dir {{{1
  205. function! s:show_dir(cnt) abort
  206. let cnt = a:cnt
  207. let num = s:numfiles
  208. let files = []
  209. for fname in split(glob('.\=*'))
  210. if isdirectory(fname)
  211. \ || (exists('g:startify_skiplist') && s:is_in_skiplist(resolve(fnamemodify(fname, ':p'))))
  212. continue
  213. endif
  214. call add(files, [getftime(fname), fname])
  215. endfor
  216. if !empty(files)
  217. if exists('s:last_message')
  218. call append('$', s:last_message)
  219. unlet s:last_message
  220. endif
  221. function! l:compare(x, y)
  222. return a:y[0] - a:x[0]
  223. endfunction
  224. call sort(files, 'l:compare')
  225. for items in files
  226. let index = s:get_index_as_string(cnt)
  227. let fname = items[1]
  228. call append('$', ' ['. index .']'. repeat(' ', (3 - strlen(index))) . fname)
  229. execute 'nnoremap <buffer>' index ':edit' fnameescape(fname) '<cr>'
  230. let cnt += 1
  231. let num -= 1
  232. if !num
  233. break
  234. endif
  235. endfor
  236. call append('$', '')
  237. endif
  238. return cnt
  239. endfunction
  240. " Function: s:show_files {{{1
  241. function! s:show_files(cnt) abort
  242. let cnt = a:cnt
  243. let num = s:numfiles
  244. let entries = {}
  245. if !empty(v:oldfiles)
  246. if exists('s:last_message')
  247. call append('$', s:last_message)
  248. unlet s:last_message
  249. endif
  250. for fname in v:oldfiles
  251. let fullpath = resolve(fnamemodify(fname, ':p'))
  252. " filter duplicates, bookmarks and entries from the skiplist
  253. if has_key(entries, fullpath)
  254. \ || !filereadable(fullpath)
  255. \ || (exists('g:startify_skiplist') && s:is_in_skiplist(fullpath))
  256. \ || (exists('g:startify_bookmarks') && s:is_bookmark(fullpath))
  257. continue
  258. endif
  259. let entries[fullpath] = 1
  260. let index = s:get_index_as_string(cnt)
  261. call append('$', ' ['. index .']'. repeat(' ', (3 - strlen(index))) . fname)
  262. execute 'nnoremap <buffer>' index ':edit' fnameescape(fname) '<bar> call <sid>check_user_options()<cr>'
  263. let cnt += 1
  264. let num -= 1
  265. if !num
  266. break
  267. endif
  268. endfor
  269. call append('$', '')
  270. return cnt
  271. endif
  272. endfunction
  273. " Function: s:show_sessions {{{1
  274. function! s:show_sessions(cnt) abort
  275. let sfiles = split(globpath(s:session_dir, '*'), '\n')
  276. let slen = len(sfiles)
  277. if empty(sfiles)
  278. if exists('s:last_message')
  279. unlet s:last_message
  280. endif
  281. return a:cnt
  282. endif
  283. let cnt = a:cnt
  284. if exists('s:last_message')
  285. call append('$', s:last_message)
  286. endif
  287. for i in range(slen)
  288. let idx = (i + cnt)
  289. let index = s:get_index_as_string(idx)
  290. call append('$', ' ['. index .']'. repeat(' ', (3 - strlen(index))) . fnamemodify(sfiles[i], ':t:r'))
  291. execute 'nnoremap <buffer>' index ':source' fnameescape(sfiles[i]) '<cr>'
  292. endfor
  293. call append('$', '')
  294. return idx + 1
  295. endfunction
  296. " Function: s:show_bookmarks {{{1
  297. function! s:show_bookmarks(cnt) abort
  298. let cnt = a:cnt
  299. if exists('g:startify_bookmarks')
  300. if exists('s:last_message')
  301. call append('$', s:last_message)
  302. unlet s:last_message
  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