startify.vim 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  1. " vim: et sw=2 sts=2
  2. " Plugin: https://github.com/mhinz/vim-startify
  3. " Description: A fancy start screen for Vim.
  4. " Maintainer: Marco Hinz <http://github.com/mhinz>
  5. if exists('g:autoloaded_startify') || &compatible
  6. finish
  7. endif
  8. let g:autoloaded_startify = 1
  9. " Init: values {{{1
  10. let s:nowait_string = v:version >= 704 || (v:version == 703 && has('patch1261')) ? '<nowait>' : ''
  11. let s:nowait = get(g:, 'startify_mapping_nowait') ? s:nowait_string : ''
  12. let s:numfiles = get(g:, 'startify_files_number', 10)
  13. let s:show_special = get(g:, 'startify_enable_special', 1)
  14. let s:delete_buffers = get(g:, 'startify_session_delete_buffers')
  15. let s:relative_path = get(g:, 'startify_relative_path') ? ':.:~' : ':p:~'
  16. let s:session_dir = resolve(expand(get(g:, 'startify_session_dir',
  17. \ has('win32') ? '$HOME\vimfiles\session' : '~/.vim/session')))
  18. let s:skiplist = get(g:, 'startify_skiplist', [
  19. \ 'COMMIT_EDITMSG',
  20. \ escape(fnamemodify(resolve($VIMRUNTIME), ':p'), '\') .'doc',
  21. \ 'bundle/.*/doc',
  22. \ ])
  23. let s:transformations = {}
  24. if exists('g:startify_transformations')
  25. for [k,V] in items(g:startify_transformations)
  26. call extend(s:transformations, { fnamemodify(resolve(expand(k)), ':p'): V })
  27. unlet V " avoid type mismatch dict <-> string
  28. endfor
  29. let s:tf = 1
  30. else
  31. let s:tf = 0
  32. endif
  33. " Function: #get_separator {{{1
  34. function! startify#get_separator() abort
  35. return !exists('+shellslash') || &shellslash ? '/' : '\'
  36. endfunction
  37. let s:sep = startify#get_separator()
  38. " Function: #get_lastline {{{1
  39. function! startify#get_lastline() abort
  40. return s:lastline + 1
  41. endfunction
  42. " Function: #insane_in_the_membrane {{{1
  43. function! startify#insane_in_the_membrane() abort
  44. if &insertmode
  45. return
  46. endif
  47. if !empty(v:servername) && exists('g:startify_skiplist_server')
  48. for servname in g:startify_skiplist_server
  49. if servname == v:servername
  50. return
  51. endif
  52. endfor
  53. endif
  54. setlocal
  55. \ bufhidden=wipe
  56. \ buftype=nofile
  57. \ nobuflisted
  58. \ nocursorcolumn
  59. \ nocursorline
  60. \ nolist
  61. \ nonumber
  62. \ noswapfile
  63. if empty(&statusline)
  64. setlocal statusline=\ startify
  65. endif
  66. if v:version >= 703
  67. setlocal norelativenumber
  68. endif
  69. " Must be global so that it can be read by syntax/startify.vim.
  70. if exists('g:startify_custom_header')
  71. if type(g:startify_custom_header) == type([])
  72. let g:startify_header = copy(g:startify_custom_header)
  73. else
  74. let g:startify_header = eval(g:startify_custom_header)
  75. endif
  76. else
  77. let g:startify_header = startify#fortune#cowsay()
  78. endif
  79. if !empty(g:startify_header)
  80. let g:startify_header += [''] " add blank line
  81. endif
  82. call append('$', g:startify_header)
  83. let s:tick = 0
  84. let s:entries = {}
  85. if s:show_special
  86. call append('$', [' [e] <empty buffer>', ''])
  87. endif
  88. call s:register(line('$')-1, 'e', 'special', 'enew', '', s:nowait_string)
  89. let s:entry_number = 0
  90. if filereadable('Session.vim')
  91. call append('$', [' [0] '. getcwd() . s:sep .'Session.vim', ''])
  92. call s:register(line('$')-1, '0', 'session',
  93. \ 'call startify#session_delete_buffers() | source', 'Session.vim',
  94. \ s:nowait)
  95. let s:entry_number = 1
  96. let l:show_session = 1
  97. endif
  98. if empty(v:oldfiles)
  99. echohl WarningMsg
  100. echomsg "startify: Can't read viminfo file. Read :help startify-faq-02"
  101. echohl NONE
  102. endif
  103. let b:startify_section_header_lines = []
  104. let s:lists = get(g:, 'startify_list_order', [
  105. \ [' MRU'], 'files',
  106. \ [' MRU '. getcwd()], 'dir',
  107. \ [' Sessions'], 'sessions',
  108. \ [' Bookmarks'], 'bookmarks',
  109. \ ])
  110. for item in s:lists
  111. if type(item) == 1
  112. call s:show_{item}()
  113. else
  114. let s:last_message = item
  115. endif
  116. unlet item
  117. endfor
  118. silent $delete _
  119. if s:show_special
  120. call append('$', ['', ' [q] <quit>'])
  121. call s:register(line('$'), 'q', 'special', 'call s:close()', '', s:nowait_string)
  122. else
  123. " Don't overwrite the last regular entry, thus +1
  124. call s:register(line('$')+1, 'q', 'special', 'call s:close()', '', s:nowait_string)
  125. endif
  126. " compute first line offset
  127. let s:firstline = 2
  128. let s:firstline += len(g:startify_header)
  129. " no special, no local Session.vim, but a section header
  130. if !s:show_special && !exists('l:show_session') && type(s:lists[0]) == 3
  131. let s:firstline += len(s:lists[0]) + 1
  132. endif
  133. let s:lastline = line('$')
  134. if exists('g:startify_custom_footer')
  135. call append('$', g:startify_custom_footer)
  136. endif
  137. setlocal nomodifiable nomodified
  138. call s:set_mappings()
  139. call cursor(s:firstline + (s:show_special ? 2 : 0), 5)
  140. autocmd startify CursorMoved <buffer> call s:set_cursor()
  141. set filetype=startify
  142. silent! doautocmd <nomodeline> User Startified
  143. endfunction
  144. " Function: #session_load {{{1
  145. function! startify#session_load(...) abort
  146. if !isdirectory(s:session_dir)
  147. echomsg 'The session directory does not exist: '. s:session_dir
  148. return
  149. elseif empty(startify#session_list_as_string(''))
  150. echomsg 'There are no sessions...'
  151. return
  152. endif
  153. let spath = s:session_dir . s:sep
  154. if a:0
  155. let spath .= a:1
  156. else
  157. if has('win32')
  158. call inputsave()
  159. let spath .= input(
  160. \ 'Load this session: ',
  161. \ fnamemodify(v:this_session, ':t'),
  162. \ 'custom,startify#session_list_as_string') | redraw
  163. call inputrestore()
  164. else
  165. let spath .= '__LAST__'
  166. endif
  167. endif
  168. if filereadable(spath)
  169. if get(g:, 'startify_session_persistence') && filewritable(v:this_session)
  170. call startify#session_write(fnameescape(v:this_session))
  171. endif
  172. call startify#session_delete_buffers()
  173. execute 'source '. fnameescape(spath)
  174. call s:create_last_session_link(spath)
  175. else
  176. echo 'No such file: '. spath
  177. endif
  178. endfunction
  179. " Function: #session_save {{{1
  180. function! startify#session_save(...) abort
  181. if !isdirectory(s:session_dir)
  182. if exists('*mkdir')
  183. echo 'The session directory does not exist: '. s:session_dir .'. Create it? [y/n]'
  184. if (nr2char(getchar()) == 'y')
  185. call mkdir(s:session_dir, 'p')
  186. else
  187. echo
  188. return
  189. endif
  190. else
  191. echo 'The session directory does not exist: '. s:session_dir
  192. return
  193. endif
  194. endif
  195. call inputsave()
  196. let vsession = fnamemodify(v:this_session, ':t')
  197. if vsession ==# '__LAST__'
  198. let vsession = ''
  199. endif
  200. let sname = exists('a:1')
  201. \ ? a:1
  202. \ : input('Save under this session name: ', vsession, 'custom,startify#session_list_as_string')
  203. \ | redraw
  204. call inputrestore()
  205. if empty(sname)
  206. echo 'You gave an empty name!'
  207. return
  208. endif
  209. let spath = s:session_dir . s:sep . sname
  210. if !filereadable(spath)
  211. call startify#session_write(fnameescape(spath))
  212. echo 'Session saved under: '. spath
  213. return
  214. endif
  215. echo 'Session already exists. Overwrite? [y/n]' | redraw
  216. if nr2char(getchar()) == 'y'
  217. call startify#session_write(fnameescape(spath))
  218. echo 'Session saved under: '. spath
  219. else
  220. echo 'Did NOT save the session!'
  221. endif
  222. endfunction
  223. " Function: #session_close {{{1
  224. function! startify#session_close() abort
  225. if exists('v:this_session') && filewritable(v:this_session)
  226. call startify#session_write(fnameescape(v:this_session))
  227. let v:this_session = ''
  228. endif
  229. call startify#session_delete_buffers()
  230. Startify
  231. endfunction
  232. " Function: #session_write {{{1
  233. function! startify#session_write(spath)
  234. " if this function is called while being in the Startify buffer
  235. " (by loading another session or running :SSave/:SLoad directly)
  236. " switch back to the previous buffer before saving the session
  237. if &filetype == 'startify'
  238. let callingbuffer = bufnr('#')
  239. if callingbuffer > 0
  240. execute 'buffer' callingbuffer
  241. endif
  242. endif
  243. " prevent saving already deleted buffers that were in the arglist
  244. for arg in argv()
  245. if !buflisted(arg)
  246. execute 'silent! argdelete' fnameescape(arg)
  247. endif
  248. endfor
  249. let ssop = &sessionoptions
  250. set sessionoptions-=options
  251. try
  252. execute 'mksession!' a:spath
  253. catch
  254. echohl ErrorMsg
  255. echomsg v:exception
  256. echohl NONE
  257. return
  258. finally
  259. let &sessionoptions = ssop
  260. endtry
  261. if exists('g:startify_session_remove_lines')
  262. \ || exists('g:startify_session_savevars')
  263. \ || exists('g:startify_session_savecmds')
  264. silent execute 'split' a:spath
  265. " remove lines from the session file
  266. if exists('g:startify_session_remove_lines')
  267. for pattern in g:startify_session_remove_lines
  268. execute 'silent global/'. pattern .'/delete _'
  269. endfor
  270. endif
  271. " put existing variables from savevars into session file
  272. if exists('g:startify_session_savevars')
  273. call append(line('$')-3, map(filter(copy(g:startify_session_savevars), 'exists(v:val)'), '"let ". v:val ." = ". strtrans(string(eval(v:val)))'))
  274. endif
  275. " put commands from savecmds into session file
  276. if exists('g:startify_session_savecmds')
  277. call append(line('$')-3, g:startify_session_savecmds)
  278. endif
  279. setlocal bufhidden=delete
  280. silent update
  281. silent hide
  282. endif
  283. call s:create_last_session_link(a:spath)
  284. endfunction
  285. " Function: #session_delete {{{1
  286. function! startify#session_delete(bang, ...) abort
  287. if !isdirectory(s:session_dir)
  288. echo 'The session directory does not exist: '. s:session_dir
  289. return
  290. elseif empty(startify#session_list_as_string(''))
  291. echo 'There are no sessions...'
  292. return
  293. endif
  294. call inputsave()
  295. let spath = s:session_dir . s:sep . (exists('a:1')
  296. \ ? a:1
  297. \ : input('Delete this session: ', fnamemodify(v:this_session, ':t'), 'custom,startify#session_list_as_string'))
  298. \ | redraw
  299. call inputrestore()
  300. if !filereadable(spath)
  301. echomsg 'No such session: '. spath
  302. return
  303. endif
  304. echo 'Really delete '. spath .'? [y/n]' | redraw
  305. if a:bang || nr2char(getchar()) == 'y'
  306. if delete(spath) == 0
  307. echo 'Deleted session '. spath .'!'
  308. else
  309. echo 'Deletion failed!'
  310. endif
  311. else
  312. echo 'Deletion aborted!'
  313. endif
  314. endfunction
  315. " Function: #session_delete_buffers {{{1
  316. function! startify#session_delete_buffers()
  317. if !s:delete_buffers
  318. return
  319. endif
  320. let n = 1
  321. while n <= bufnr('$')
  322. if buflisted(n)
  323. try
  324. silent execute 'bdelete' n
  325. catch
  326. echohl ErrorMsg
  327. echomsg v:exception
  328. echohl NONE
  329. endtry
  330. endif
  331. let n += 1
  332. endwhile
  333. endfunction
  334. " Function: #session_list {{{1
  335. function! startify#session_list(lead, ...) abort
  336. return filter(map(split(globpath(s:session_dir, '*'.a:lead.'*'), '\n'), 'fnamemodify(v:val, ":t")'), 'v:val !=# "__LAST__"')
  337. endfunction
  338. " Function: #session_list_as_string {{{1
  339. function! startify#session_list_as_string(lead, ...) abort
  340. return join(filter(map(split(globpath(s:session_dir, '*'.a:lead.'*'), '\n'), 'fnamemodify(v:val, ":t")'), 'v:val !=# "__LAST__"'), "\n")
  341. endfunction
  342. " Function: #debug {{{1
  343. function! startify#debug()
  344. if exists('s:entries')
  345. for k in sort(keys(s:entries))
  346. echomsg '['. k .'] = '. string(s:entries[k])
  347. endfor
  348. endif
  349. endfunction
  350. " Function: #open_buffers {{{1
  351. function! startify#open_buffers(...) abort
  352. if exists('a:1') " used in mappings
  353. call s:open_buffer(s:entries[a:1])
  354. return
  355. endif
  356. let marked = filter(copy(s:entries), 'v:val.marked')
  357. if empty(marked) " open current entry
  358. call s:open_buffer(s:entries[line('.')])
  359. return
  360. endif
  361. enew
  362. setlocal nobuflisted
  363. " Open all marked entries.
  364. for entry in sort(values(marked), 's:sort_by_tick')
  365. call s:open_buffer(entry)
  366. endfor
  367. wincmd =
  368. endfunction
  369. " Function: s:open_buffer {{{1
  370. function! s:open_buffer(entry)
  371. if a:entry.type == 'special'
  372. execute a:entry.cmd
  373. elseif a:entry.type == 'session'
  374. execute a:entry.cmd a:entry.path
  375. elseif a:entry.type == 'file'
  376. if line2byte('$') == -1
  377. execute 'edit' a:entry.path
  378. else
  379. if a:entry.cmd == 'tabnew'
  380. wincmd =
  381. endif
  382. execute a:entry.cmd a:entry.path
  383. endif
  384. call s:check_user_options()
  385. endif
  386. endfunction
  387. " Function: s:display_by_path {{{1
  388. function! s:display_by_path(path_prefix, path_format, use_env) abort
  389. let oldfiles = call(get(g:, 'startify_enable_unsafe') ? 's:filter_oldfiles_unsafe' : 's:filter_oldfiles',
  390. \ [a:path_prefix, a:path_format, a:use_env])
  391. let entry_format = "' ['. index .']'. repeat(' ', (3 - strlen(index)))"
  392. if exists('*WebDevIconsGetFileTypeSymbol') " support for vim-devicons
  393. let entry_format .= ". '('. WebDevIconsGetFileTypeSymbol(entry_path) .') '. entry_path"
  394. else
  395. let entry_format .= '. entry_path'
  396. endif
  397. if !empty(oldfiles)
  398. if exists('s:last_message')
  399. call s:print_section_header()
  400. endif
  401. for [absolute_path, entry_path] in oldfiles
  402. let index = s:get_index_as_string(s:entry_number)
  403. call append('$', eval(entry_format))
  404. if has('win32')
  405. let absolute_path = substitute(absolute_path, '\[', '\[[]', 'g')
  406. endif
  407. call s:register(line('$'), index, 'file', 'edit', absolute_path, s:nowait)
  408. let s:entry_number += 1
  409. endfor
  410. call append('$', '')
  411. endif
  412. endfunction
  413. " Function: s:filter_oldfiles {{{1
  414. function! s:filter_oldfiles(path_prefix, path_format, use_env) abort
  415. let path_prefix = '\V'. escape(a:path_prefix, '\')
  416. let counter = s:numfiles
  417. let entries = {}
  418. let oldfiles = []
  419. for fname in v:oldfiles
  420. if counter <= 0
  421. break
  422. endif
  423. let absolute_path = fnamemodify(resolve(fname), ":p")
  424. " filter duplicates, bookmarks and entries from the skiplist
  425. if has_key(entries, absolute_path)
  426. \ || !filereadable(absolute_path)
  427. \ || s:is_in_skiplist(absolute_path)
  428. \ || match(absolute_path, path_prefix)
  429. continue
  430. endif
  431. let entry_path = s:tf
  432. \ ? s:transform(absolute_path)
  433. \ : fnamemodify(absolute_path, a:path_format)
  434. let entries[absolute_path] = 1
  435. let counter -= 1
  436. let oldfiles += [[fnameescape(absolute_path), entry_path]]
  437. endfor
  438. if a:use_env
  439. call s:init_env()
  440. for i in range(len(oldfiles))
  441. for [k,v] in s:env
  442. let p = oldfiles[i][1]
  443. if !stridx(tolower(p), tolower(v))
  444. let oldfiles[i][1] = printf('$%s%s', k, p[len(v):])
  445. break
  446. endif
  447. endfor
  448. endfor
  449. endif
  450. return oldfiles
  451. endfun
  452. " Function: s:filter_oldfiles_unsafe {{{1
  453. function! s:filter_oldfiles_unsafe(path_prefix, path_format, use_env) abort
  454. let path_prefix = '\V'. escape(a:path_prefix, '\')
  455. let counter = s:numfiles
  456. let entries = {}
  457. let oldfiles = []
  458. for fname in v:oldfiles
  459. if counter <= 0
  460. break
  461. endif
  462. let absolute_path = glob(fnamemodify(fname, ":p"))
  463. if empty(absolute_path)
  464. \ || has_key(entries, absolute_path)
  465. \ || s:is_in_skiplist(absolute_path)
  466. \ || match(absolute_path, path_prefix)
  467. continue
  468. endif
  469. let entry_path = fnamemodify(absolute_path, a:path_format)
  470. let entries[absolute_path] = 1
  471. let counter -= 1
  472. let oldfiles += [[fnameescape(absolute_path), entry_path]]
  473. endfor
  474. return oldfiles
  475. endfun
  476. " Function: s:show_dir {{{1
  477. function! s:show_dir() abort
  478. return s:display_by_path(getcwd(), ':.', 0)
  479. endfunction
  480. " Function: s:show_files {{{1
  481. function! s:show_files() abort
  482. return s:display_by_path('', s:relative_path, get(g:, 'startify_use_env'))
  483. endfunction
  484. " Function: s:show_sessions {{{1
  485. function! s:show_sessions() abort
  486. let sfiles = filter(split(globpath(s:session_dir, '*'), '\n'),
  487. \ 'v:val !~# "x\.vim$" && v:val !~# "__LAST__$"')
  488. if empty(sfiles)
  489. if exists('s:last_message')
  490. unlet s:last_message
  491. endif
  492. return
  493. endif
  494. if exists('s:last_message')
  495. call s:print_section_header()
  496. endif
  497. for i in range(len(sfiles))
  498. let index = s:get_index_as_string(s:entry_number)
  499. let fname = fnamemodify(sfiles[i], ':t')
  500. call append('$', ' ['. index .']'. repeat(' ', (3 - strlen(index))) . fname)
  501. if has('win32')
  502. let fname = substitute(fname, '\[', '\[[]', 'g')
  503. endif
  504. call s:register(line('$'), index, 'session', 'SLoad', fname, s:nowait)
  505. let s:entry_number += 1
  506. endfor
  507. call append('$', '')
  508. endfunction
  509. " Function: s:show_bookmarks {{{1
  510. function! s:show_bookmarks() abort
  511. if !exists('g:startify_bookmarks') || empty(g:startify_bookmarks)
  512. return
  513. endif
  514. if exists('s:last_message')
  515. call s:print_section_header()
  516. endif
  517. for bookmark in g:startify_bookmarks
  518. if type(bookmark) == type({})
  519. let [index, path] = items(bookmark)[0]
  520. else " string
  521. let [index, path] = [s:get_index_as_string(s:entry_number), bookmark]
  522. let s:entry_number += 1
  523. endif
  524. if s:tf
  525. let entry_path = s:transform(fnamemodify(resolve(expand(path)), ':p'))
  526. else
  527. let entry_path = path
  528. endif
  529. call append('$', ' ['. index .']'. repeat(' ', (3 - strlen(index))) . entry_path)
  530. if has('win32')
  531. let path = substitute(path, '\[', '\[[]', 'g')
  532. endif
  533. call s:register(line('$'), index, 'file', 'edit', path, s:nowait)
  534. unlet bookmark " avoid type mismatch for heterogeneous lists
  535. endfor
  536. call append('$', '')
  537. endfunction
  538. " Function: s:is_in_skiplist {{{1
  539. function! s:is_in_skiplist(arg) abort
  540. for regexp in s:skiplist
  541. try
  542. if a:arg =~# regexp
  543. return 1
  544. endif
  545. catch
  546. echohl WarningMsg
  547. echomsg 'startify: Pattern '. string(regexp) .' threw an exception. Read :help g:startify_skiplist'
  548. echohl NONE
  549. endtry
  550. endfor
  551. endfunction
  552. " Function: s:set_cursor {{{1
  553. function! s:set_cursor() abort
  554. let s:oldline = exists('s:newline') ? s:newline : 5
  555. let s:newline = line('.')
  556. " going up (-1) or down (1)
  557. let movement = 2 * (s:newline > s:oldline) - 1
  558. " skip section headers lines until an entry is found
  559. while index(b:startify_section_header_lines, s:newline) != -1
  560. let s:newline += movement
  561. endwhile
  562. " skip blank lines between lists
  563. if empty(getline(s:newline))
  564. let s:newline += movement
  565. endif
  566. " don't go beyond first or last entry
  567. let s:newline = max([s:firstline, min([s:lastline, s:newline])])
  568. call cursor(s:newline, 5)
  569. endfunction
  570. " Function: s:set_mappings {{{1
  571. function! s:set_mappings() abort
  572. execute "nnoremap <buffer>". s:nowait_string ."<silent> i :enew <bar> startinsert<cr>"
  573. execute "nnoremap <buffer>". s:nowait_string ."<silent> <insert> :enew <bar> startinsert<cr>"
  574. execute "nnoremap <buffer>". s:nowait_string ."<silent> b :call <sid>set_mark('B')<cr>"
  575. execute "nnoremap <buffer>". s:nowait_string ."<silent> s :call <sid>set_mark('S')<cr>"
  576. execute "nnoremap <buffer>". s:nowait_string ."<silent> t :call <sid>set_mark('T')<cr>"
  577. execute "nnoremap <buffer>". s:nowait_string ."<silent> v :call <sid>set_mark('V')<cr>"
  578. execute "nnoremap <buffer>". s:nowait_string ."<silent> <cr> :call startify#open_buffers()<cr>"
  579. execute "nnoremap <buffer>". s:nowait_string ."<silent> <2-LeftMouse> :call startify#open_buffers()<cr>"
  580. for k in keys(s:entries)
  581. execute 'nnoremap <buffer><silent>'. s:entries[k].nowait s:entries[k].index
  582. \ ':call startify#open_buffers('. string(k) .')<cr>'
  583. endfor
  584. " Prevent 'nnoremap j gj' mappings, since they would break navigation.
  585. " (One can't leave the [x].)
  586. if !empty(maparg('j', 'n'))
  587. nnoremap <buffer> j j
  588. endif
  589. if !empty(maparg('k', 'n'))
  590. nnoremap <buffer> k k
  591. endif
  592. endfunction
  593. " Function: s:set_mark {{{1
  594. function! s:set_mark(type, ...) abort
  595. let index = expand('<cword>')
  596. let line = exists('a:1') ? a:1 : line('.')
  597. let entry = s:entries[line]
  598. if entry.type != 'file'
  599. return
  600. endif
  601. let default_cmds = {
  602. \ 'B': 'edit',
  603. \ 'S': 'split',
  604. \ 'V': 'vsplit',
  605. \ 'T': 'tabnew',
  606. \ }
  607. setlocal modifiable
  608. if entry.marked && index[0] == a:type
  609. let entry.cmd = 'edit'
  610. let entry.marked = 0
  611. execute 'normal! ci]'. entry.index
  612. else
  613. let entry.cmd = default_cmds[a:type]
  614. let entry.marked = 1
  615. let entry.tick = s:tick
  616. let s:tick += 1
  617. execute 'normal! ci]'. repeat(a:type, len(index))
  618. endif
  619. setlocal nomodifiable nomodified
  620. endfunction
  621. " Function: s:sort_by_tick {{{1
  622. function! s:sort_by_tick(one, two)
  623. return a:one.tick - a:two.tick
  624. endfunction
  625. " Function: s:check_user_options {{{1
  626. function! s:check_user_options() abort
  627. let path = expand('%')
  628. let session = path . s:sep .'Session.vim'
  629. " change to VCS root directory
  630. if get(g:, 'startify_session_autoload') && filereadable(session)
  631. execute 'source' session
  632. elseif get(g:, 'startify_change_to_vcs_root')
  633. call s:cd_to_vcs_root(path)
  634. " change directory
  635. elseif get(g:, 'startify_change_to_dir', 1)
  636. if isdirectory(path)
  637. lcd %
  638. else
  639. lcd %:p:h
  640. endif
  641. endif
  642. endfunction
  643. " Function: s:cd_to_vcs_root {{{1
  644. function! s:cd_to_vcs_root(path) abort
  645. let dir = fnamemodify(a:path, ':p:h')
  646. for vcs in [ '.git', '.hg', '.bzr', '.svn' ]
  647. let root = finddir(vcs, dir .';')
  648. if !empty(root)
  649. execute 'cd '. fnamemodify(root, ':h')
  650. return
  651. endif
  652. endfor
  653. endfunction
  654. " Function: s:close {{{1
  655. function! s:close() abort
  656. if len(filter(range(0, bufnr('$')), 'buflisted(v:val)')) - &buflisted
  657. if bufloaded(bufnr('#')) && bufnr('#') != bufnr('%')
  658. buffer #
  659. else
  660. bnext
  661. endif
  662. else
  663. quit
  664. endif
  665. endfunction
  666. " Function: s:get_index_as_string {{{1
  667. function! s:get_index_as_string(idx) abort
  668. if exists('g:startify_custom_indices')
  669. let listlen = len(g:startify_custom_indices)
  670. return (a:idx < listlen) ? g:startify_custom_indices[a:idx] : string(a:idx - listlen)
  671. else
  672. return string(a:idx)
  673. endif
  674. endfunction
  675. " Function: s:print_section_header {{{1
  676. function! s:print_section_header() abort
  677. $
  678. let curline = line('.')
  679. for lnum in range(curline, curline + len(s:last_message) + 1)
  680. call add(b:startify_section_header_lines, lnum)
  681. endfor
  682. call append('$', s:last_message + [''])
  683. unlet s:last_message
  684. endfunction
  685. " Function: s:register {{{1
  686. function! s:register(line, index, type, cmd, path, wait)
  687. let s:entries[a:line] = {
  688. \ 'index': a:index,
  689. \ 'type': a:type,
  690. \ 'cmd': a:cmd,
  691. \ 'path': a:path,
  692. \ 'nowait': a:wait,
  693. \ 'marked': 0,
  694. \ }
  695. endfunction
  696. " Function: s:create_last_session_link {{{1
  697. function! s:create_last_session_link(spath)
  698. if !has('win32') && a:spath !~# '__LAST__$'
  699. let cmd = printf('ln -sf %s %s',
  700. \ shellescape(fnamemodify(a:spath, ':t')),
  701. \ shellescape(s:session_dir .'/__LAST__'))
  702. silent! call system(cmd)
  703. if v:shell_error
  704. echomsg "startify: Can't create 'last used session' symlink."
  705. endif
  706. endif
  707. endfunction
  708. " Function: s:init_env {{{1
  709. function! s:init_env()
  710. let s:env = []
  711. let ignore = { 'PWD': 1, 'OLDPWD': 1 }
  712. function! s:get_env()
  713. redir => s
  714. silent! execute "norm!:ec$\<c-a>'\<c-b>\<right>\<right>\<del>'\<cr>"
  715. redir END
  716. redraw
  717. return split(s)
  718. endfunction
  719. function! s:compare_by_key_len(foo, bar)
  720. return len(a:foo[0]) - len(a:bar[0])
  721. endfunction
  722. function! s:compare_by_val_len(foo, bar)
  723. return len(a:bar[1]) - len(a:foo[1])
  724. endfunction
  725. for k in s:get_env()
  726. silent! execute "let v = eval('$'.k)"
  727. if has('win32') ? (v[1] != ':') : (v[0] != '/')
  728. \ || has_key(ignore, k)
  729. \ || len(k) > len(v)
  730. continue
  731. endif
  732. call insert(s:env, [k,v], 0)
  733. endfor
  734. let s:env = sort(s:env, 's:compare_by_key_len')
  735. let s:env = sort(s:env, 's:compare_by_val_len')
  736. endfunction
  737. " Function: s:transform {{{1
  738. function s:transform(absolute_path)
  739. if has_key(s:transformations, a:absolute_path)
  740. let V = s:transformations[a:absolute_path]
  741. return type(V) == type('') ? V : V(a:absolute_path)
  742. endif
  743. return a:absolute_path
  744. endfunction