startify.vim 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  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. call s:set_mappings()
  115. call cursor(s:firstline + (s:show_special ? 2 : 0), 5)
  116. autocmd startify CursorMoved <buffer> call s:set_cursor()
  117. set filetype=startify
  118. silent! doautocmd <nomodeline> User Startified
  119. endfunction
  120. " Function: #session_load {{{1
  121. function! startify#session_load(...) abort
  122. if !isdirectory(s:session_dir)
  123. echomsg 'The session directory does not exist: '. s:session_dir
  124. return
  125. elseif empty(startify#session_list_as_string(''))
  126. echomsg 'There are no sessions...'
  127. return
  128. endif
  129. call inputsave()
  130. let spath = s:session_dir . s:sep . (exists('a:1')
  131. \ ? a:1
  132. \ : input('Load this session: ', fnamemodify(v:this_session, ':t'), 'custom,startify#session_list_as_string'))
  133. \ | redraw
  134. call inputrestore()
  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. call inputsave()
  164. let sname = exists('a:1')
  165. \ ? a:1
  166. \ : input('Save under this session name: ', fnamemodify(v:this_session, ':t'), 'custom,startify#session_list_as_string')
  167. \ | redraw
  168. call inputrestore()
  169. if empty(sname)
  170. echo 'You gave an empty name!'
  171. return
  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_close {{{1
  188. function! startify#session_close() abort
  189. if exists('v:this_session') && filewritable(v:this_session)
  190. call startify#session_write(fnameescape(v:this_session))
  191. let v:this_session = ''
  192. endif
  193. call startify#session_delete_buffers()
  194. Startify
  195. endfunction
  196. " Function: #session_write {{{1
  197. function! startify#session_write(spath)
  198. let ssop = &sessionoptions
  199. try
  200. " if this function is called while being in the Startify buffer
  201. " (by loading another session or running :SSave/:SLoad directly)
  202. " switch back to the previous buffer before saving the session
  203. if &filetype == 'startify'
  204. let callingbuffer = bufnr('#')
  205. if callingbuffer > 0
  206. execute 'buffer' callingbuffer
  207. endif
  208. endif
  209. " prevent saving already deleted buffers that were in the arglist
  210. for arg in argv()
  211. if !buflisted(arg)
  212. execute 'argdelete' fnameescape(arg)
  213. endif
  214. endfor
  215. set sessionoptions-=options
  216. execute 'mksession!' a:spath
  217. catch
  218. execute 'echoerr' string(v:exception)
  219. finally
  220. let &sessionoptions = ssop
  221. endtry
  222. if exists('g:startify_session_savevars') || exists('g:startify_session_savecmds')
  223. execute 'split' a:spath
  224. " put existing variables from savevars into session file
  225. call append(line('$')-3, map(filter(copy(get(g:, 'startify_session_savevars', [])), 'exists(v:val)'), '"let ". v:val ." = ". strtrans(string(eval(v:val)))'))
  226. " put commands from savecmds into session file
  227. call append(line('$')-3, get(g:, 'startify_session_savecmds', []))
  228. setlocal bufhidden=delete
  229. silent update
  230. silent hide
  231. endif
  232. endfunction
  233. " Function: #session_delete {{{1
  234. function! startify#session_delete(...) abort
  235. if !isdirectory(s:session_dir)
  236. echo 'The session directory does not exist: '. s:session_dir
  237. return
  238. elseif empty(startify#session_list_as_string(''))
  239. echo 'There are no sessions...'
  240. return
  241. endif
  242. call inputsave()
  243. let spath = s:session_dir . s:sep . (exists('a:1')
  244. \ ? a:1
  245. \ : input('Delete this session: ', fnamemodify(v:this_session, ':t'), 'custom,startify#session_list_as_string'))
  246. \ | redraw
  247. call inputrestore()
  248. echo 'Really delete '. spath .'? [y/n]' | redraw
  249. if (nr2char(getchar()) == 'y')
  250. if delete(spath) == 0
  251. echo 'Deleted session '. spath .'!'
  252. else
  253. echo 'Deletion failed!'
  254. endif
  255. else
  256. echo 'Deletion aborted!'
  257. endif
  258. endfunction
  259. " Function: #session_delete_buffers {{{1
  260. function! startify#session_delete_buffers() abort
  261. if !s:delete_buffers
  262. return
  263. endif
  264. let n = 1
  265. while n <= bufnr('$')
  266. if buflisted(n)
  267. silent execute 'bdelete' n
  268. endif
  269. let n += 1
  270. endwhile
  271. endfunction
  272. " Function: #session_list {{{1
  273. function! startify#session_list(lead, ...) abort
  274. return map(split(globpath(s:session_dir, '*'.a:lead.'*'), '\n'), 'fnamemodify(v:val, ":t")')
  275. endfunction
  276. " Function: #session_list_as_string {{{1
  277. function! startify#session_list_as_string(lead, ...) abort
  278. return join(map(split(globpath(s:session_dir, '*'.a:lead.'*'), '\n'), 'fnamemodify(v:val, ":t")'), "\n")
  279. endfunction
  280. " Function: #open_buffers {{{1
  281. function! startify#open_buffers() abort
  282. " markers found; open one or more buffers
  283. if exists('s:marked') && !empty(s:marked)
  284. enew
  285. setlocal nobuflisted
  286. for val in values(s:marked)
  287. let [path, type] = val[1:2]
  288. let path = fnameescape(path)
  289. if line2byte('$') == -1
  290. " open in current window
  291. execute 'edit' path
  292. elseif type == 'S'
  293. " open in split
  294. execute 'split' path
  295. elseif type == 'V'
  296. " open in vsplit
  297. execute 'vsplit' path
  298. elseif type == 'T'
  299. " open in tab
  300. execute 'tabnew' path
  301. else
  302. " open in current window
  303. execute 'edit' path
  304. endif
  305. call s:check_user_options()
  306. endfor
  307. " remove markers for next instance of :Startify
  308. if exists('s:marked')
  309. unlet s:marked
  310. endif
  311. " no markers found; open a single buffer
  312. else
  313. try
  314. execute 'normal' expand('<cword>')
  315. catch /E832/ " don't ask for undo encryption key twice
  316. edit
  317. catch /E325/ " swap file found
  318. endtry
  319. endif
  320. endfunction
  321. " Function: s:display_by_path {{{1
  322. function! s:display_by_path(path_prefix, path_format) abort
  323. let oldfiles = call(get(g:, 'startify_enable_unsafe') ? 's:filter_oldfiles_unsafe' : 's:filter_oldfiles',
  324. \ [a:path_prefix, a:path_format])
  325. if !empty(oldfiles)
  326. if exists('s:last_message')
  327. call s:print_section_header()
  328. endif
  329. for [absolute_path, entry_path] in oldfiles
  330. let index = s:get_index_as_string(s:entry_number)
  331. call append('$', ' ['. index .']'. repeat(' ', (3 - strlen(index))) . entry_path)
  332. execute 'nnoremap <buffer><silent>' index ':edit' escape(absolute_path, ' ') '<bar> call <sid>check_user_options()<cr>'
  333. let s:entry_number += 1
  334. endfor
  335. call append('$', '')
  336. endif
  337. endfunction
  338. " Function: s:filter_oldfiles {{{1
  339. function! s:filter_oldfiles(path_prefix, path_format) abort
  340. let path_prefix = '\V'. escape(a:path_prefix, '\')
  341. let counter = s:numfiles
  342. let entries = {}
  343. let oldfiles = []
  344. for fname in v:oldfiles
  345. if counter <= 0
  346. break
  347. endif
  348. let absolute_path = glob(fnameescape(fnamemodify(resolve(fname), ":p")))
  349. " filter duplicates, bookmarks and entries from the skiplist
  350. if has_key(entries, absolute_path)
  351. \ || !filereadable(absolute_path)
  352. \ || s:is_in_skiplist(absolute_path)
  353. \ || (exists('g:startify_bookmarks') && s:is_bookmark(absolute_path))
  354. \ || match(absolute_path, path_prefix)
  355. continue
  356. endif
  357. let entry_path = fnamemodify(absolute_path, a:path_format)
  358. let entries[absolute_path] = 1
  359. let counter -= 1
  360. let oldfiles += [[absolute_path, entry_path]]
  361. endfor
  362. return oldfiles
  363. endfun
  364. " Function: s:filter_oldfiles_unsafe {{{1
  365. function! s:filter_oldfiles_unsafe(path_prefix, path_format) abort
  366. let path_prefix = '\V'. escape(a:path_prefix, '\')
  367. let counter = s:numfiles
  368. let entries = {}
  369. let oldfiles = []
  370. for fname in v:oldfiles
  371. if counter <= 0
  372. break
  373. endif
  374. let absolute_path = glob(fnameescape(fnamemodify(fname, ":p")))
  375. if empty(absolute_path)
  376. \ || has_key(entries, absolute_path)
  377. \ || s:is_in_skiplist(absolute_path)
  378. \ || match(absolute_path, path_prefix)
  379. continue
  380. endif
  381. let entry_path = fnamemodify(absolute_path, a:path_format)
  382. let entries[absolute_path] = 1
  383. let counter -= 1
  384. let oldfiles += [[absolute_path, entry_path]]
  385. endfor
  386. return oldfiles
  387. endfun
  388. " Function: s:show_dir {{{1
  389. function! s:show_dir() abort
  390. " let cwd = escape(getcwd(), '\')
  391. return s:display_by_path(getcwd(), ':.')
  392. endfunction
  393. " Function: s:show_files {{{1
  394. function! s:show_files() abort
  395. return s:display_by_path('', s:relative_path)
  396. endfunction
  397. " Function: s:show_sessions {{{1
  398. function! s:show_sessions() abort
  399. let sfiles = split(globpath(s:session_dir, '*'), '\n')
  400. if empty(sfiles)
  401. if exists('s:last_message')
  402. unlet s:last_message
  403. endif
  404. return
  405. endif
  406. if exists('s:last_message')
  407. call s:print_section_header()
  408. endif
  409. for i in range(len(sfiles))
  410. let index = s:get_index_as_string(s:entry_number)
  411. call append('$', ' ['. index .']'. repeat(' ', (3 - strlen(index))) . fnamemodify(sfiles[i], ':t'))
  412. execute 'nnoremap <buffer><silent>' index ':SLoad' fnamemodify(sfiles[i], ':t') '<cr>'
  413. let s:entry_number += 1
  414. endfor
  415. call append('$', '')
  416. endfunction
  417. " Function: s:show_bookmarks {{{1
  418. function! s:show_bookmarks() abort
  419. if !exists('g:startify_bookmarks') || empty(g:startify_bookmarks)
  420. return
  421. endif
  422. if exists('s:last_message')
  423. call s:print_section_header()
  424. endif
  425. for fname in g:startify_bookmarks
  426. let index = s:get_index_as_string(s:entry_number)
  427. call append('$', ' ['. index .']'. repeat(' ', (3 - strlen(index))) . fname)
  428. execute 'nnoremap <buffer><silent>' index ':edit' fnameescape(fname) '<bar> call <sid>check_user_options()<cr>'
  429. let s:entry_number += 1
  430. endfor
  431. call append('$', '')
  432. endfunction
  433. " Function: s:is_in_skiplist {{{1
  434. function! s:is_in_skiplist(arg) abort
  435. for regexp in s:skiplist
  436. if (a:arg =~# regexp)
  437. return 1
  438. endif
  439. endfor
  440. endfunction
  441. " Function: s:is_bookmark {{{1
  442. function! s:is_bookmark(arg) abort
  443. for foo in map(filter(copy(g:startify_bookmarks), '!isdirectory(v:val)'), 'resolve(fnamemodify(v:val, ":p"))')
  444. if foo == a:arg
  445. return 1
  446. endif
  447. endfor
  448. endfunction
  449. " Function: s:set_cursor {{{1
  450. function! s:set_cursor() abort
  451. let s:oldline = exists('s:newline') ? s:newline : 5
  452. let s:newline = line('.')
  453. " going up (-1) or down (1)
  454. let movement = 2 * (s:newline > s:oldline) - 1
  455. " skip section headers lines until an entry is found
  456. while index(b:startify_section_header_lines, s:newline) != -1
  457. let s:newline += movement
  458. endwhile
  459. " skip blank lines between lists
  460. if empty(getline(s:newline))
  461. let s:newline += movement
  462. endif
  463. " don't go beyond first or last entry
  464. let s:newline = max([s:firstline, min([s:lastline, s:newline])])
  465. call cursor(s:newline, 5)
  466. endfunction
  467. " Function: s:set_mappings {{{1
  468. function! s:set_mappings() abort
  469. nnoremap <buffer><silent> e :enew<cr>
  470. nnoremap <buffer><silent> i :enew <bar> startinsert<cr>
  471. nnoremap <buffer><silent> <insert> :enew <bar> startinsert<cr>
  472. nnoremap <buffer><silent> b :call <sid>set_mark('B')<cr>
  473. nnoremap <buffer><silent> s :call <sid>set_mark('S')<cr>
  474. nnoremap <buffer><silent> t :call <sid>set_mark('T')<cr>
  475. nnoremap <buffer><silent> v :call <sid>set_mark('V')<cr>
  476. nnoremap <buffer><silent> <cr> :call startify#open_buffers()<cr>
  477. nnoremap <buffer><silent> <2-LeftMouse> :execute 'normal' matchstr(getline('.'), '\w\+')<cr>
  478. nnoremap <buffer><silent> q :call <sid>close()<cr>
  479. " Prevent 'nnoremap j gj' mappings, since they would break navigation.
  480. " (One can't leave the [x].)
  481. if !empty(mapcheck('h', 'n'))
  482. nnoremap <buffer> h h
  483. endif
  484. if !empty(mapcheck('j', 'n'))
  485. nnoremap <buffer> j j
  486. endif
  487. if !empty(mapcheck('k', 'n'))
  488. nnoremap <buffer> k k
  489. endif
  490. if !empty(mapcheck('l', 'n'))
  491. nnoremap <buffer> l l
  492. endif
  493. endfunction
  494. " Function: s:set_mark {{{1
  495. "
  496. " Markers are saved in the s:marked dict using the follow format:
  497. " - s:marked[0]: ID
  498. " - s:marked[1]: path
  499. " - s:marked[2]: type (buffer, split, vsplit)
  500. "
  501. function! s:set_mark(type) abort
  502. if !exists('s:marked')
  503. let s:marked = {}
  504. endif
  505. let [id, path] = matchlist(getline('.'), '\v\[(.{-})\]\s+(.*)')[1:2]
  506. let path = fnamemodify(path, ':p')
  507. if path =~# '\V<empty buffer>\|<quit>' || path =~# '^\w\+$'
  508. return
  509. endif
  510. setlocal modifiable
  511. " set markers
  512. if id =~# '[BSTV]'
  513. " replace marker by old ID
  514. execute 'normal! ci]'. remove(s:marked, line('.'))[0]
  515. else
  516. " save ID and replace it by the marker of the given type
  517. let s:marked[line('.')] = [id, path, a:type]
  518. execute 'normal! ci]'. repeat(a:type, len(id))
  519. endif
  520. setlocal nomodifiable nomodified
  521. endfunction
  522. " Function: s:check_user_options {{{1
  523. function! s:check_user_options() abort
  524. let path = expand('%')
  525. let session = path . s:sep .'Session.vim'
  526. " change to VCS root directory
  527. if get(g:, 'startify_session_autoload') && filereadable(session)
  528. execute 'source' session
  529. elseif get(g:, 'startify_change_to_vcs_root')
  530. call s:cd_to_vcs_root(path)
  531. " change directory
  532. elseif get(g:, 'startify_change_to_dir', 1)
  533. if isdirectory(path)
  534. lcd %
  535. else
  536. lcd %:h
  537. endif
  538. endif
  539. endfunction
  540. " Function: s:cd_to_vcs_root {{{1
  541. function! s:cd_to_vcs_root(path) abort
  542. let dir = fnamemodify(a:path, ':p:h')
  543. for vcs in [ '.git', '.hg', '.bzr', '.svn' ]
  544. let root = finddir(vcs, dir .';')
  545. if !empty(root)
  546. execute 'cd '. fnamemodify(root, ':h')
  547. return
  548. endif
  549. endfor
  550. endfunction
  551. " Function: s:close {{{1
  552. function! s:close() abort
  553. if len(filter(range(0, bufnr('$')), 'buflisted(v:val)'))
  554. if bufloaded(bufnr('#')) && bufnr('#') != bufnr('%')
  555. buffer #
  556. else
  557. bnext
  558. endif
  559. else
  560. quit
  561. endif
  562. endfunction
  563. " Function: s:get_index_as_string {{{1
  564. function! s:get_index_as_string(idx) abort
  565. if exists('g:startify_custom_indices')
  566. let listlen = len(g:startify_custom_indices)
  567. return (a:idx < listlen) ? g:startify_custom_indices[a:idx] : string(a:idx - listlen)
  568. else
  569. return string(a:idx)
  570. endif
  571. endfunction
  572. " Function: s:print_section_header {{{1
  573. function! s:print_section_header() abort
  574. $
  575. let curline = line('.')
  576. for lnum in range(curline, curline + len(s:last_message) + 1)
  577. call add(b:startify_section_header_lines, lnum)
  578. endfor
  579. call append('$', s:last_message + [''])
  580. unlet s:last_message
  581. endfunction