startify.vim 17 KB

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