startify.vim 17 KB

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