startify.vim 24 KB

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