startify.vim 17 KB

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