startify.vim 18 KB

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