startify.vim 18 KB

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