startify.vim 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  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),
  255. \ 'glob(fnameescape(fnamemodify(resolve(v:val), ":p")))'), 'match(v:val, cwd) == 0')
  256. if !empty(files)
  257. if exists('s:last_message')
  258. call s:print_section_header()
  259. endif
  260. for abs_path in files
  261. let abs_path = glob(abs_path)
  262. " filter duplicates, bookmarks and entries from the skiplist
  263. if has_key(entries, abs_path)
  264. \ || !filereadable(abs_path)
  265. \ || (exists('g:startify_skiplist') && s:is_in_skiplist(abs_path))
  266. \ || (exists('g:startify_bookmarks') && s:is_bookmark(abs_path))
  267. continue
  268. endif
  269. let entries[abs_path] = 1
  270. let index = s:get_index_as_string(cnt)
  271. let display_path = fnamemodify(abs_path, s:relative_path ? ':.' : ':p:~')
  272. call append('$', ' ['. index .']'. repeat(' ', (3 - strlen(index))) . display_path)
  273. execute 'nnoremap <buffer><silent>' index ':edit' abs_path '<bar> call <sid>check_user_options()<cr>'
  274. let cnt += 1
  275. let num -= 1
  276. if !num
  277. break
  278. endif
  279. endfor
  280. call append('$', '')
  281. endif
  282. return cnt
  283. endfunction
  284. " Function: s:show_files {{{1
  285. function! s:show_files(cnt) abort
  286. if empty(v:oldfiles)
  287. return a:cnt
  288. endif
  289. if exists('s:last_message')
  290. call s:print_section_header()
  291. endif
  292. let cnt = a:cnt
  293. let num = s:numfiles
  294. let entries = {}
  295. for fname in v:oldfiles
  296. let abs_path = glob(fnameescape(fnamemodify(resolve(fname), ':p')))
  297. " filter duplicates, bookmarks and entries from the skiplist
  298. if has_key(entries, abs_path)
  299. \ || !filereadable(abs_path)
  300. \ || (exists('g:startify_skiplist') && s:is_in_skiplist(abs_path))
  301. \ || (exists('g:startify_bookmarks') && s:is_bookmark(abs_path))
  302. continue
  303. endif
  304. let entries[abs_path] = 1
  305. let index = s:get_index_as_string(cnt)
  306. let display_path = fnamemodify(abs_path, s:relative_path ? ':.' : ':p:~')
  307. call append('$', ' ['. index .']'. repeat(' ', (3 - strlen(index))) . display_path)
  308. execute 'nnoremap <buffer><silent>' index ':edit' abs_path '<bar> call <sid>check_user_options()<cr>'
  309. let cnt += 1
  310. let num -= 1
  311. if !num
  312. break
  313. endif
  314. endfor
  315. call append('$', '')
  316. return cnt
  317. endfunction
  318. " Function: s:show_sessions {{{1
  319. function! s:show_sessions(cnt) abort
  320. let sfiles = split(globpath(s:session_dir, '*'), '\n')
  321. if empty(sfiles)
  322. if exists('s:last_message')
  323. unlet s:last_message
  324. endif
  325. return a:cnt
  326. endif
  327. if exists('s:last_message')
  328. call s:print_section_header()
  329. endif
  330. let cnt = a:cnt
  331. let slen = len(sfiles)
  332. for i in range(slen)
  333. let index = s:get_index_as_string(cnt)
  334. call append('$', ' ['. index .']'. repeat(' ', (3 - strlen(index))) . fnamemodify(sfiles[i], ':t:r'))
  335. execute 'nnoremap <buffer><silent>' index ':source' fnameescape(sfiles[i]) '<cr>'
  336. let cnt += 1
  337. endfor
  338. call append('$', '')
  339. return cnt
  340. endfunction
  341. " Function: s:show_bookmarks {{{1
  342. function! s:show_bookmarks(cnt) abort
  343. if !exists('g:startify_bookmarks')
  344. return a:cnt
  345. endif
  346. if exists('s:last_message')
  347. call s:print_section_header()
  348. endif
  349. let cnt = a:cnt
  350. for fname in g:startify_bookmarks
  351. let index = s:get_index_as_string(cnt)
  352. call append('$', ' ['. index .']'. repeat(' ', (3 - strlen(index))) . fname)
  353. execute 'nnoremap <buffer><silent>' index ':edit' fnameescape(fname) '<bar> call <sid>check_user_options()<cr>'
  354. let cnt += 1
  355. endfor
  356. call append('$', '')
  357. return cnt
  358. endfunction
  359. " Function: s:is_in_skiplist {{{1
  360. function! s:is_in_skiplist(arg) abort
  361. for regexp in g:startify_skiplist
  362. if (a:arg =~# regexp)
  363. return 1
  364. endif
  365. endfor
  366. endfunction
  367. " Function: s:is_bookmark {{{1
  368. function! s:is_bookmark(arg) abort
  369. for foo in map(filter(copy(g:startify_bookmarks), '!isdirectory(v:val)'), 'resolve(fnamemodify(v:val, ":p"))')
  370. if foo == a:arg
  371. return 1
  372. endif
  373. endfor
  374. endfunction
  375. " Function: s:set_cursor {{{1
  376. function! s:set_cursor() abort
  377. let s:oldline = exists('s:newline') ? s:newline : 5
  378. let s:newline = line('.')
  379. " going up (-1) or down (1)
  380. let movement = 2 * (s:newline > s:oldline) - 1
  381. " skip section headers lines until an entry is found
  382. while index(s:section_header_lines, s:newline) != -1
  383. let s:newline += movement
  384. endwhile
  385. " skip blank lines between lists
  386. if empty(getline(s:newline))
  387. let s:newline += movement
  388. endif
  389. " don't go beyond first or last entry
  390. let s:newline = max([s:firstline, min([s:lastline, s:newline])])
  391. call cursor(s:newline, 5)
  392. endfunction
  393. " Function: s:set_mark {{{1
  394. "
  395. " Markers are saved in the s:marked dict using the follow format:
  396. " - s:marked[0]: ID
  397. " - s:marked[1]: path
  398. " - s:marked[2]: type (buffer, split, vsplit)
  399. "
  400. function! s:set_mark(type) abort
  401. if !exists('s:marked')
  402. let s:marked = {}
  403. endif
  404. let [id, path] = matchlist(getline('.'), '\v\[(.*)\]\s+(.*)')[1:2]
  405. let path = fnamemodify(path, ':p')
  406. if path =~# '\V<empty buffer>\|<quit>' || path =~# '^\w\+$'
  407. return
  408. endif
  409. setlocal modifiable
  410. " set markers
  411. if id =~# '[BSTV]'
  412. " replace marker by old ID
  413. execute 'normal! ci]'. remove(s:marked, line('.'))[0]
  414. else
  415. " save ID and replace it by the marker of the given type
  416. let s:marked[line('.')] = [id, path, a:type]
  417. execute 'normal! ci]'. repeat(a:type, len(id))
  418. endif
  419. setlocal nomodifiable nomodified
  420. endfunction
  421. " Function: s:open_buffers {{{1
  422. function! s:open_buffers(cword) abort
  423. " markers found; open one or more buffers
  424. if exists('s:marked') && !empty(s:marked)
  425. enew
  426. setlocal nobuflisted
  427. for val in values(s:marked)
  428. let [path, type] = val[1:2]
  429. let path = fnameescape(path)
  430. if line2byte('$') == -1
  431. " open in current window
  432. execute 'edit' path
  433. elseif type == 'S'
  434. " open in split
  435. execute 'split' path
  436. elseif type == 'V'
  437. " open in vsplit
  438. execute 'vsplit' path
  439. elseif type == 'T'
  440. " open in tab
  441. execute 'tabnew' path
  442. else
  443. " open in current window
  444. execute 'edit' path
  445. endif
  446. call s:check_user_options()
  447. endfor
  448. " remove markers for next instance of :Startify
  449. if exists('s:marked')
  450. unlet s:marked
  451. endif
  452. " no markers found; open a single buffer
  453. else
  454. try
  455. execute 'normal' a:cword
  456. catch /E832/ " don't ask for undo encryption key twice
  457. edit
  458. catch /E325/ " swap file found
  459. endtry
  460. endif
  461. endfunction
  462. " Function: s:check_user_options {{{1
  463. function! s:check_user_options() abort
  464. let path = expand('%')
  465. let session = path . s:sep .'Session.vim'
  466. " autoload session
  467. if get(g:, 'startify_session_autoload') && filereadable(session)
  468. execute 'source' session
  469. " change to VCS root directory
  470. elseif get(g:, 'startify_change_to_vcs_root')
  471. call s:cd_to_vcs_root(path)
  472. " change directory
  473. elseif get(g:, 'startify_change_to_dir', 1)
  474. if isdirectory(path)
  475. lcd %
  476. else
  477. lcd %:h
  478. endif
  479. endif
  480. endfunction
  481. " Function: s:cd_to_vcs_root {{{1
  482. function! s:cd_to_vcs_root(path) abort
  483. let dir = fnamemodify(a:path, ':p:h')
  484. for vcs in [ '.git', '.hg', '.bzr', '.svn' ]
  485. let root = finddir(vcs, dir .';')
  486. if !empty(root)
  487. execute 'cd '. fnamemodify(root, ':h')
  488. return
  489. endif
  490. endfor
  491. endfunction
  492. " Function: s:close {{{1
  493. function! s:close() abort
  494. if len(filter(range(0, bufnr('$')), 'buflisted(v:val)'))
  495. if bufloaded(bufnr('#'))
  496. buffer #
  497. else
  498. bnext
  499. endif
  500. else
  501. quit
  502. endif
  503. endfunction
  504. " Function: s:get_index_as_string {{{1
  505. function! s:get_index_as_string(idx) abort
  506. if exists('g:startify_custom_indices')
  507. let listlen = len(g:startify_custom_indices)
  508. return (a:idx < listlen) ? g:startify_custom_indices[a:idx] : string(a:idx - listlen)
  509. else
  510. return string(a:idx)
  511. endif
  512. endfunction
  513. " Function: s:restore_position {{{1
  514. function! s:restore_position() abort
  515. autocmd! startify *
  516. if line("'\"") > 0 && line("'\"") <= line('$')
  517. call cursor(getpos("'\"")[1:])
  518. endif
  519. endfunction
  520. " Function: s:print_section_header {{{1
  521. function! s:print_section_header() abort
  522. $
  523. let curline = line('.')
  524. for lnum in range(curline, curline + len(s:last_message) + 1)
  525. call add(s:section_header_lines, lnum)
  526. endfor
  527. call append('$', s:last_message + [''])
  528. unlet s:last_message
  529. endfunction