startify.vim 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  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') ? ':.' : ':p:~'
  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 s:entry_number = 0
  66. if filereadable('Session.vim')
  67. call append('$', [' [0] '. getcwd() . s:sep .'Session.vim', ''])
  68. execute 'nnoremap <silent><buffer> 0 :call startify#session_delete_buffers() <bar> source Session.vim<cr>'
  69. let s:entry_number = 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-02"
  75. echohl None
  76. endif
  77. let b:startify_section_header_lines = []
  78. let s:lists = get(g:, 'startify_list_order', [
  79. \ [' Most recently used files:'],
  80. \ 'files',
  81. \ [' Most recently used 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. call s:show_{item}()
  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><silent> <cr> :call startify#open_buffers()<cr>
  123. nnoremap <buffer><silent> <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. call inputsave()
  140. let spath = s:session_dir . s:sep . (exists('a:1')
  141. \ ? a:1
  142. \ : input('Load this session: ', fnamemodify(v:this_session, ':t'), 'custom,startify#session_list_as_string'))
  143. \ | redraw
  144. call inputrestore()
  145. if filereadable(spath)
  146. if get(g:, 'startify_session_persistence')
  147. \ && exists('v:this_session')
  148. \ && filewritable(v:this_session)
  149. call startify#session_write(fnameescape(v:this_session))
  150. endif
  151. call startify#session_delete_buffers()
  152. execute 'source '. fnameescape(spath)
  153. else
  154. echo 'No such file: '. spath
  155. endif
  156. endfunction
  157. " Function: #session_save {{{1
  158. function! startify#session_save(...) abort
  159. if !isdirectory(s:session_dir)
  160. if exists('*mkdir')
  161. echo 'The session directory does not exist: '. s:session_dir .'. Create it? [y/n]'
  162. if (nr2char(getchar()) == 'y')
  163. call mkdir(s:session_dir, 'p')
  164. else
  165. echo
  166. return
  167. endif
  168. else
  169. echo 'The session directory does not exist: '. s:session_dir
  170. return
  171. endif
  172. endif
  173. if exists('a:1')
  174. let sname = a:1
  175. else
  176. call inputsave()
  177. let sname = input('Save under this session name: ', fnamemodify(v:this_session, ':t'), 'custom,startify#session_list_as_string')
  178. call inputrestore()
  179. redraw
  180. if empty(sname)
  181. echo 'You gave an empty name!'
  182. return
  183. endif
  184. endif
  185. let spath = s:session_dir . s:sep . sname
  186. if !filereadable(spath)
  187. call startify#session_write(fnameescape(spath))
  188. echo 'Session saved under: '. spath
  189. return
  190. endif
  191. echo 'Session already exists. Overwrite? [y/n]' | redraw
  192. if nr2char(getchar()) == 'y'
  193. call startify#session_write(fnameescape(spath))
  194. echo 'Session saved under: '. spath
  195. else
  196. echo 'Did NOT save the session!'
  197. endif
  198. endfunction
  199. " Function: #session_close {{{1
  200. function! startify#session_close() abort
  201. if exists('v:this_session') && filewritable(v:this_session)
  202. call startify#session_write(fnameescape(v:this_session))
  203. let v:this_session = ''
  204. endif
  205. call startify#session_delete_buffers()
  206. Startify
  207. endfunction
  208. " Function: #session_write {{{1
  209. function! startify#session_write(spath)
  210. let ssop = &sessionoptions
  211. try
  212. " if this function is called while being in the Startify buffer
  213. " (by loading another session or running :SSave/:SLoad directly)
  214. " switch back to the previous buffer before saving the session
  215. if &filetype == 'startify'
  216. let callingbuffer = bufnr('#')
  217. if callingbuffer > 0
  218. execute 'buffer' callingbuffer
  219. endif
  220. endif
  221. " prevent saving already deleted buffers that were in the arglist
  222. for arg in argv()
  223. if !buflisted(arg)
  224. execute 'argdelete' fnameescape(arg)
  225. endif
  226. endfor
  227. set sessionoptions-=options
  228. execute 'mksession!' a:spath
  229. catch
  230. execute 'echoerr' string(v:exception)
  231. finally
  232. let &sessionoptions = ssop
  233. endtry
  234. if exists('g:startify_session_savevars') || exists('g:startify_session_savecmds')
  235. execute 'split' a:spath
  236. " put existing variables from savevars into session file
  237. call append(line('$')-3, map(filter(copy(get(g:, 'startify_session_savevars', [])), 'exists(v:val)'), '"let ". v:val ." = ". strtrans(string(eval(v:val)))'))
  238. " put commands from savecmds into session file
  239. call append(line('$')-3, get(g:, 'startify_session_savecmds', []))
  240. setlocal bufhidden=delete
  241. silent update
  242. silent hide
  243. endif
  244. endfunction
  245. " Function: #session_delete {{{1
  246. function! startify#session_delete(...) abort
  247. if !isdirectory(s:session_dir)
  248. echo 'The session directory does not exist: '. s:session_dir
  249. return
  250. elseif empty(startify#session_list_as_string(''))
  251. echo 'There are no sessions...'
  252. return
  253. endif
  254. call inputsave()
  255. let spath = s:session_dir . s:sep . (exists('a:1')
  256. \ ? a:1
  257. \ : input('Delete this session: ', fnamemodify(v:this_session, ':t'), 'custom,startify#session_list_as_string'))
  258. \ | redraw
  259. call inputrestore()
  260. echo 'Really delete '. spath .'? [y/n]' | redraw
  261. if (nr2char(getchar()) == 'y')
  262. if delete(spath) == 0
  263. echo 'Deleted session '. spath .'!'
  264. else
  265. echo 'Deletion failed!'
  266. endif
  267. else
  268. echo 'Deletion aborted!'
  269. endif
  270. endfunction
  271. " Function: #session_delete_buffers {{{1
  272. function! startify#session_delete_buffers() abort
  273. if !s:delete_buffers
  274. return
  275. endif
  276. let n = 1
  277. while n <= bufnr('$')
  278. if buflisted(n)
  279. silent execute 'bdelete' n
  280. endif
  281. let n += 1
  282. endwhile
  283. endfunction
  284. " Function: #session_list {{{1
  285. function! startify#session_list(lead, ...) abort
  286. return map(split(globpath(s:session_dir, '*'.a:lead.'*'), '\n'), 'fnamemodify(v:val, ":t")')
  287. endfunction
  288. " Function: #session_list_as_string {{{1
  289. function! startify#session_list_as_string(lead, ...) abort
  290. return join(map(split(globpath(s:session_dir, '*'.a:lead.'*'), '\n'), 'fnamemodify(v:val, ":t")'), "\n")
  291. endfunction
  292. " Function: #open_buffers {{{1
  293. function! startify#open_buffers() abort
  294. " markers found; open one or more buffers
  295. if exists('s:marked') && !empty(s:marked)
  296. enew
  297. setlocal nobuflisted
  298. for val in values(s:marked)
  299. let [path, type] = val[1:2]
  300. let path = fnameescape(path)
  301. if line2byte('$') == -1
  302. " open in current window
  303. execute 'edit' path
  304. elseif type == 'S'
  305. " open in split
  306. execute 'split' path
  307. elseif type == 'V'
  308. " open in vsplit
  309. execute 'vsplit' path
  310. elseif type == 'T'
  311. " open in tab
  312. execute 'tabnew' path
  313. else
  314. " open in current window
  315. execute 'edit' path
  316. endif
  317. call s:check_user_options()
  318. endfor
  319. " remove markers for next instance of :Startify
  320. if exists('s:marked')
  321. unlet s:marked
  322. endif
  323. " no markers found; open a single buffer
  324. else
  325. try
  326. execute 'normal' expand('<cword>')
  327. catch /E832/ " don't ask for undo encryption key twice
  328. edit
  329. catch /E325/ " swap file found
  330. endtry
  331. endif
  332. endfunction
  333. " Function: s:display_by_path {{{1
  334. function! s:display_by_path(path_prefix, path_format) abort
  335. let oldfiles = s:filter_oldfiles(a:path_prefix, a:path_format)
  336. if !empty(oldfiles)
  337. if exists('s:last_message')
  338. call s:print_section_header()
  339. endif
  340. for [absolute_path, entry_path] in oldfiles
  341. let index = s:get_index_as_string(s:entry_number)
  342. call append('$', ' ['. index .']'. repeat(' ', (3 - strlen(index))) . entry_path)
  343. execute 'nnoremap <buffer><silent>' index ':edit' escape(absolute_path, ' ') '<bar> call <sid>check_user_options()<cr>'
  344. let s:entry_number += 1
  345. endfor
  346. call append('$', '')
  347. endif
  348. endfunction
  349. " Function: s:filter_oldfiles {{{1
  350. function! s:filter_oldfiles(path_prefix, path_format) abort
  351. let path_prefix = '\V'. escape(a:path_prefix, '\')
  352. let counter = s:numfiles
  353. let entries = {}
  354. let oldfiles = []
  355. for fname in v:oldfiles
  356. if counter <= 0
  357. break
  358. endif
  359. let absolute_path = glob(fnameescape(fnamemodify(resolve(fname), ":p")))
  360. " filter duplicates, bookmarks and entries from the skiplist
  361. if has_key(entries, absolute_path)
  362. \ || !filereadable(absolute_path)
  363. \ || s:is_in_skiplist(absolute_path)
  364. \ || (exists('g:startify_bookmarks') && s:is_bookmark(absolute_path))
  365. continue
  366. endif
  367. if match(absolute_path, path_prefix)
  368. continue
  369. endif
  370. let entry_path = fnamemodify(absolute_path, a:path_format)
  371. let entries[absolute_path] = 1
  372. let counter -= 1
  373. let oldfiles += [[absolute_path, entry_path]]
  374. endfor
  375. return oldfiles
  376. endfun
  377. " Function: s:show_dir {{{1
  378. function! s:show_dir() abort
  379. " let cwd = escape(getcwd(), '\')
  380. return s:display_by_path(getcwd(), ':.')
  381. endfunction
  382. " Function: s:show_files {{{1
  383. function! s:show_files() abort
  384. return s:display_by_path('', s:relative_path)
  385. endfunction
  386. " Function: s:show_sessions {{{1
  387. function! s:show_sessions() abort
  388. let sfiles = split(globpath(s:session_dir, '*'), '\n')
  389. if empty(sfiles)
  390. if exists('s:last_message')
  391. unlet s:last_message
  392. endif
  393. return
  394. endif
  395. if exists('s:last_message')
  396. call s:print_section_header()
  397. endif
  398. for i in range(len(sfiles))
  399. let index = s:get_index_as_string(s:entry_number)
  400. call append('$', ' ['. index .']'. repeat(' ', (3 - strlen(index))) . fnamemodify(sfiles[i], ':t'))
  401. execute 'nnoremap <buffer><silent>' index ':SLoad' fnamemodify(sfiles[i], ':t') '<cr>'
  402. let s:entry_number += 1
  403. endfor
  404. call append('$', '')
  405. endfunction
  406. " Function: s:show_bookmarks {{{1
  407. function! s:show_bookmarks() abort
  408. if !exists('g:startify_bookmarks') || empty(g:startify_bookmarks)
  409. return
  410. endif
  411. if exists('s:last_message')
  412. call s:print_section_header()
  413. endif
  414. for fname in g:startify_bookmarks
  415. let index = s:get_index_as_string(s:entry_number)
  416. call append('$', ' ['. index .']'. repeat(' ', (3 - strlen(index))) . fname)
  417. execute 'nnoremap <buffer><silent>' index ':edit' fnameescape(fname) '<bar> call <sid>check_user_options()<cr>'
  418. let s:entry_number += 1
  419. endfor
  420. call append('$', '')
  421. endfunction
  422. " Function: s:is_in_skiplist {{{1
  423. function! s:is_in_skiplist(arg) abort
  424. for regexp in s:skiplist
  425. if (a:arg =~# regexp)
  426. return 1
  427. endif
  428. endfor
  429. endfunction
  430. " Function: s:is_bookmark {{{1
  431. function! s:is_bookmark(arg) abort
  432. for foo in map(filter(copy(g:startify_bookmarks), '!isdirectory(v:val)'), 'resolve(fnamemodify(v:val, ":p"))')
  433. if foo == a:arg
  434. return 1
  435. endif
  436. endfor
  437. endfunction
  438. " Function: s:set_cursor {{{1
  439. function! s:set_cursor() abort
  440. let s:oldline = exists('s:newline') ? s:newline : 5
  441. let s:newline = line('.')
  442. " going up (-1) or down (1)
  443. let movement = 2 * (s:newline > s:oldline) - 1
  444. " skip section headers lines until an entry is found
  445. while index(b:startify_section_header_lines, s:newline) != -1
  446. let s:newline += movement
  447. endwhile
  448. " skip blank lines between lists
  449. if empty(getline(s:newline))
  450. let s:newline += movement
  451. endif
  452. " don't go beyond first or last entry
  453. let s:newline = max([s:firstline, min([s:lastline, s:newline])])
  454. call cursor(s:newline, 5)
  455. endfunction
  456. " Function: s:set_mark {{{1
  457. "
  458. " Markers are saved in the s:marked dict using the follow format:
  459. " - s:marked[0]: ID
  460. " - s:marked[1]: path
  461. " - s:marked[2]: type (buffer, split, vsplit)
  462. "
  463. function! s:set_mark(type) abort
  464. if !exists('s:marked')
  465. let s:marked = {}
  466. endif
  467. let [id, path] = matchlist(getline('.'), '\v\[(.{-})\]\s+(.*)')[1:2]
  468. let path = fnamemodify(path, ':p')
  469. if path =~# '\V<empty buffer>\|<quit>' || path =~# '^\w\+$'
  470. return
  471. endif
  472. setlocal modifiable
  473. " set markers
  474. if id =~# '[BSTV]'
  475. " replace marker by old ID
  476. execute 'normal! ci]'. remove(s:marked, line('.'))[0]
  477. else
  478. " save ID and replace it by the marker of the given type
  479. let s:marked[line('.')] = [id, path, a:type]
  480. execute 'normal! ci]'. repeat(a:type, len(id))
  481. endif
  482. setlocal nomodifiable nomodified
  483. endfunction
  484. " Function: s:check_user_options {{{1
  485. function! s:check_user_options() abort
  486. let path = expand('%')
  487. let session = path . s:sep .'Session.vim'
  488. " change to VCS root directory
  489. if get(g:, 'startify_session_autoload') && filereadable(session)
  490. execute 'source' session
  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('#')) && bufnr('#') != 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:print_section_header {{{1
  535. function! s:print_section_header() abort
  536. $
  537. let curline = line('.')
  538. for lnum in range(curline, curline + len(s:last_message) + 1)
  539. call add(b:startify_section_header_lines, lnum)
  540. endfor
  541. call append('$', s:last_message + [''])
  542. unlet s:last_message
  543. endfunction