startify.vim 17 KB

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