startify.vim 17 KB

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