startify.vim 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  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. for arg in argv()
  205. if !buflisted(arg)
  206. execute 'argdelete' fnameescape(arg)
  207. endif
  208. endfor
  209. set sessionoptions-=options
  210. execute 'mksession!' a:spath
  211. catch
  212. execute 'echoerr' string(v:exception)
  213. finally
  214. let &sessionoptions = ssop
  215. endtry
  216. if exists('g:startify_session_savevars') || exists('g:startify_session_savecmds')
  217. execute 'split' a:spath
  218. " put existing variables from savevars into session file
  219. call append(line('$')-3, map(filter(get(g:, 'startify_session_savevars', []), 'exists(v:val)'), '"let ". v:val ." = ". strtrans(string(eval(v:val)))'))
  220. " put commands from savecmds into session file
  221. call append(line('$')-3, get(g:, 'startify_session_savecmds', []))
  222. setlocal bufhidden=delete
  223. silent update
  224. silent hide
  225. endif
  226. endfunction
  227. " Function: #session_delete {{{1
  228. function! startify#session_delete(...) abort
  229. if !isdirectory(s:session_dir)
  230. echo 'The session directory does not exist: '. s:session_dir
  231. return
  232. elseif empty(startify#session_list_as_string(''))
  233. echo 'There are no sessions...'
  234. return
  235. endif
  236. let spath = s:session_dir . s:sep . (exists('a:1')
  237. \ ? a:1
  238. \ : input('Delete this session: ', fnamemodify(v:this_session, ':t'), 'custom,startify#session_list_as_string'))
  239. \ | redraw
  240. echo 'Really delete '. spath .'? [y/n]' | redraw
  241. if (nr2char(getchar()) == 'y')
  242. if delete(spath) == 0
  243. echo 'Deleted session '. spath .'!'
  244. else
  245. echo 'Deletion failed!'
  246. endif
  247. else
  248. echo 'Deletion aborted!'
  249. endif
  250. endfunction
  251. " Function: #session_delete_buffers {{{1
  252. function! startify#session_delete_buffers() abort
  253. if !s:delete_buffers
  254. return
  255. endif
  256. let n = 1
  257. while n <= bufnr('$')
  258. if buflisted(n)
  259. silent execute 'bdelete' n
  260. endif
  261. let n += 1
  262. endwhile
  263. endfunction
  264. " Function: #session_list {{{1
  265. function! startify#session_list(lead, ...) abort
  266. return map(split(globpath(s:session_dir, '*'.a:lead.'*'), '\n'), 'fnamemodify(v:val, ":t")')
  267. endfunction
  268. " Function: #session_list_as_string {{{1
  269. function! startify#session_list_as_string(lead, ...) abort
  270. return join(map(split(globpath(s:session_dir, '*'.a:lead.'*'), '\n'), 'fnamemodify(v:val, ":t")'), "\n")
  271. endfunction
  272. " Function: s:show_dir {{{1
  273. function! s:show_dir(cnt) abort
  274. if empty(v:oldfiles)
  275. return a:cnt
  276. endif
  277. let cnt = a:cnt
  278. let num = s:numfiles
  279. let entries = {}
  280. let cwd = escape(getcwd(), '\')
  281. let files = filter(map(copy(v:oldfiles),
  282. \ 'glob(fnameescape(fnamemodify(resolve(v:val), ":p")))'), 'match(v:val, cwd) == 0')
  283. if !empty(files)
  284. if exists('s:last_message')
  285. call s:print_section_header()
  286. endif
  287. for abs_path in files
  288. let abs_path = glob(abs_path)
  289. " filter duplicates, bookmarks and entries from the skiplist
  290. if has_key(entries, abs_path)
  291. \ || !filereadable(abs_path)
  292. \ || s:is_in_skiplist(abs_path)
  293. \ || (exists('g:startify_bookmarks') && s:is_bookmark(abs_path))
  294. continue
  295. endif
  296. let entries[abs_path] = 1
  297. let index = s:get_index_as_string(cnt)
  298. let display_path = fnamemodify(abs_path, s:relative_path ? ':.' : ':p:~')
  299. call append('$', ' ['. index .']'. repeat(' ', (3 - strlen(index))) . display_path)
  300. execute 'nnoremap <buffer><silent>' index ':edit' escape(abs_path, ' ') '<bar> call <sid>check_user_options()<cr>'
  301. let cnt += 1
  302. let num -= 1
  303. if !num
  304. break
  305. endif
  306. endfor
  307. call append('$', '')
  308. endif
  309. return cnt
  310. endfunction
  311. " Function: s:show_files {{{1
  312. function! s:show_files(cnt) abort
  313. if empty(v:oldfiles)
  314. return a:cnt
  315. endif
  316. if exists('s:last_message')
  317. call s:print_section_header()
  318. endif
  319. let cnt = a:cnt
  320. let num = s:numfiles
  321. let entries = {}
  322. for fname in v:oldfiles
  323. let abs_path = glob(fnameescape(fnamemodify(resolve(fname), ':p')))
  324. " filter duplicates, bookmarks and entries from the skiplist
  325. if has_key(entries, abs_path)
  326. \ || !filereadable(abs_path)
  327. \ || s:is_in_skiplist(abs_path)
  328. \ || (exists('g:startify_bookmarks') && s:is_bookmark(abs_path))
  329. continue
  330. endif
  331. let entries[abs_path] = 1
  332. let index = s:get_index_as_string(cnt)
  333. let display_path = fnamemodify(abs_path, s:relative_path ? ':.' : ':p:~')
  334. call append('$', ' ['. index .']'. repeat(' ', (3 - strlen(index))) . display_path)
  335. execute 'nnoremap <buffer><silent>' index ':edit' escape(abs_path, ' ') '<bar> call <sid>check_user_options()<cr>'
  336. let cnt += 1
  337. let num -= 1
  338. if !num
  339. break
  340. endif
  341. endfor
  342. call append('$', '')
  343. return cnt
  344. endfunction
  345. " Function: s:show_sessions {{{1
  346. function! s:show_sessions(cnt) abort
  347. let sfiles = split(globpath(s:session_dir, '*'), '\n')
  348. if empty(sfiles)
  349. if exists('s:last_message')
  350. unlet s:last_message
  351. endif
  352. return a:cnt
  353. endif
  354. if exists('s:last_message')
  355. call s:print_section_header()
  356. endif
  357. let cnt = a:cnt
  358. for i in range(len(sfiles))
  359. let index = s:get_index_as_string(cnt)
  360. call append('$', ' ['. index .']'. repeat(' ', (3 - strlen(index))) . fnamemodify(sfiles[i], ':t'))
  361. execute 'nnoremap <buffer><silent>' index ':SLoad' fnamemodify(sfiles[i], ':t') '<cr>'
  362. let cnt += 1
  363. endfor
  364. call append('$', '')
  365. return cnt
  366. endfunction
  367. " Function: s:show_bookmarks {{{1
  368. function! s:show_bookmarks(cnt) abort
  369. if !exists('g:startify_bookmarks')
  370. return a:cnt
  371. endif
  372. if exists('s:last_message')
  373. call s:print_section_header()
  374. endif
  375. let cnt = a:cnt
  376. for fname in g:startify_bookmarks
  377. let index = s:get_index_as_string(cnt)
  378. call append('$', ' ['. index .']'. repeat(' ', (3 - strlen(index))) . fname)
  379. execute 'nnoremap <buffer><silent>' index ':edit' fnameescape(fname) '<bar> call <sid>check_user_options()<cr>'
  380. let cnt += 1
  381. endfor
  382. call append('$', '')
  383. return cnt
  384. endfunction
  385. " Function: s:is_in_skiplist {{{1
  386. function! s:is_in_skiplist(arg) abort
  387. for regexp in s:skiplist
  388. if (a:arg =~# regexp)
  389. return 1
  390. endif
  391. endfor
  392. endfunction
  393. " Function: s:is_bookmark {{{1
  394. function! s:is_bookmark(arg) abort
  395. for foo in map(filter(copy(g:startify_bookmarks), '!isdirectory(v:val)'), 'resolve(fnamemodify(v:val, ":p"))')
  396. if foo == a:arg
  397. return 1
  398. endif
  399. endfor
  400. endfunction
  401. " Function: s:set_cursor {{{1
  402. function! s:set_cursor() abort
  403. let s:oldline = exists('s:newline') ? s:newline : 5
  404. let s:newline = line('.')
  405. " going up (-1) or down (1)
  406. let movement = 2 * (s:newline > s:oldline) - 1
  407. " skip section headers lines until an entry is found
  408. while index(s:section_header_lines, s:newline) != -1
  409. let s:newline += movement
  410. endwhile
  411. " skip blank lines between lists
  412. if empty(getline(s:newline))
  413. let s:newline += movement
  414. endif
  415. " don't go beyond first or last entry
  416. let s:newline = max([s:firstline, min([s:lastline, s:newline])])
  417. call cursor(s:newline, 5)
  418. endfunction
  419. " Function: s:set_mark {{{1
  420. "
  421. " Markers are saved in the s:marked dict using the follow format:
  422. " - s:marked[0]: ID
  423. " - s:marked[1]: path
  424. " - s:marked[2]: type (buffer, split, vsplit)
  425. "
  426. function! s:set_mark(type) abort
  427. if !exists('s:marked')
  428. let s:marked = {}
  429. endif
  430. let [id, path] = matchlist(getline('.'), '\v\[(.{-})\]\s+(.*)')[1:2]
  431. let path = fnamemodify(path, ':p')
  432. if path =~# '\V<empty buffer>\|<quit>' || path =~# '^\w\+$'
  433. return
  434. endif
  435. setlocal modifiable
  436. " set markers
  437. if id =~# '[BSTV]'
  438. " replace marker by old ID
  439. execute 'normal! ci]'. remove(s:marked, line('.'))[0]
  440. else
  441. " save ID and replace it by the marker of the given type
  442. let s:marked[line('.')] = [id, path, a:type]
  443. execute 'normal! ci]'. repeat(a:type, len(id))
  444. endif
  445. setlocal nomodifiable nomodified
  446. endfunction
  447. " Function: s:open_buffers {{{1
  448. function! s:open_buffers(cword) abort
  449. " markers found; open one or more buffers
  450. if exists('s:marked') && !empty(s:marked)
  451. enew
  452. setlocal nobuflisted
  453. for val in values(s:marked)
  454. let [path, type] = val[1:2]
  455. let path = fnameescape(path)
  456. if line2byte('$') == -1
  457. " open in current window
  458. execute 'edit' path
  459. elseif type == 'S'
  460. " open in split
  461. execute 'split' path
  462. elseif type == 'V'
  463. " open in vsplit
  464. execute 'vsplit' path
  465. elseif type == 'T'
  466. " open in tab
  467. execute 'tabnew' path
  468. else
  469. " open in current window
  470. execute 'edit' path
  471. endif
  472. call s:check_user_options()
  473. endfor
  474. " remove markers for next instance of :Startify
  475. if exists('s:marked')
  476. unlet s:marked
  477. endif
  478. " no markers found; open a single buffer
  479. else
  480. try
  481. execute 'normal' a:cword
  482. catch /E832/ " don't ask for undo encryption key twice
  483. edit
  484. catch /E325/ " swap file found
  485. endtry
  486. endif
  487. endfunction
  488. " Function: s:check_user_options {{{1
  489. function! s:check_user_options() abort
  490. let path = expand('%')
  491. let session = path . s:sep .'Session.vim'
  492. " autoload session
  493. if get(g:, 'startify_session_autoload') && filereadable(session)
  494. execute 'source' session
  495. " change to VCS root directory
  496. elseif get(g:, 'startify_change_to_vcs_root')
  497. call s:cd_to_vcs_root(path)
  498. " change directory
  499. elseif get(g:, 'startify_change_to_dir', 1)
  500. if isdirectory(path)
  501. lcd %
  502. else
  503. lcd %:h
  504. endif
  505. endif
  506. endfunction
  507. " Function: s:cd_to_vcs_root {{{1
  508. function! s:cd_to_vcs_root(path) abort
  509. let dir = fnamemodify(a:path, ':p:h')
  510. for vcs in [ '.git', '.hg', '.bzr', '.svn' ]
  511. let root = finddir(vcs, dir .';')
  512. if !empty(root)
  513. execute 'cd '. fnamemodify(root, ':h')
  514. return
  515. endif
  516. endfor
  517. endfunction
  518. " Function: s:close {{{1
  519. function! s:close() abort
  520. if len(filter(range(0, bufnr('$')), 'buflisted(v:val)'))
  521. if bufloaded(bufnr('#'))
  522. buffer #
  523. else
  524. bnext
  525. endif
  526. else
  527. quit
  528. endif
  529. endfunction
  530. " Function: s:get_index_as_string {{{1
  531. function! s:get_index_as_string(idx) abort
  532. if exists('g:startify_custom_indices')
  533. let listlen = len(g:startify_custom_indices)
  534. return (a:idx < listlen) ? g:startify_custom_indices[a:idx] : string(a:idx - listlen)
  535. else
  536. return string(a:idx)
  537. endif
  538. endfunction
  539. " Function: s:print_section_header {{{1
  540. function! s:print_section_header() abort
  541. $
  542. let curline = line('.')
  543. for lnum in range(curline, curline + len(s:last_message) + 1)
  544. call add(s:section_header_lines, lnum)
  545. endfor
  546. call append('$', s:last_message + [''])
  547. unlet s:last_message
  548. endfunction