startify.vim 23 KB

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