startify.vim 17 KB

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