startify.vim 18 KB

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