startify.vim 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  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. \ nobuflisted
  48. \ nocursorcolumn
  49. \ nocursorline
  50. \ nolist
  51. \ nonumber
  52. \ noswapfile
  53. if empty(&statusline)
  54. setlocal statusline=\ startify
  55. endif
  56. if v:version >= 703
  57. setlocal norelativenumber
  58. endif
  59. " Must be global so that it can be read by syntax/startify.vim.
  60. if exists('g:startify_custom_header')
  61. if type(g:startify_custom_header) == type([])
  62. let g:startify_header = copy(g:startify_custom_header)
  63. else
  64. let g:startify_header = eval(g:startify_custom_header)
  65. endif
  66. else
  67. let g:startify_header = startify#fortune#cowsay()
  68. endif
  69. if !empty(g:startify_header)
  70. let g:startify_header += [''] " add blank line
  71. endif
  72. call append('$', g:startify_header)
  73. let s:tick = 0
  74. let s:entries = {}
  75. if s:show_special
  76. call append('$', [' [e] <empty buffer>', ''])
  77. endif
  78. call s:register(line('$')-1, 'e', 'special', 'enew', '', s:nowait_string)
  79. let s:entry_number = 0
  80. if filereadable('Session.vim')
  81. call append('$', [' [0] '. getcwd() . s:sep .'Session.vim', ''])
  82. call s:register(line('$')-1, '0', 'session',
  83. \ 'call startify#session_delete_buffers() | source', 'Session.vim',
  84. \ s:nowait)
  85. let s:entry_number = 1
  86. let l:show_session = 1
  87. endif
  88. if empty(v:oldfiles)
  89. echohl WarningMsg
  90. echomsg "startify: Can't read viminfo file. Read :help startify-faq-02"
  91. echohl NONE
  92. endif
  93. let b:startify_section_header_lines = []
  94. let s:lists = get(g:, 'startify_list_order', [
  95. \ [' MRU'], 'files',
  96. \ [' MRU '. getcwd()], 'dir',
  97. \ [' Sessions'], 'sessions',
  98. \ [' Bookmarks'], 'bookmarks',
  99. \ [' Commands'], 'commands',
  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. silent file Startify
  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. " clean up session before saving it
  242. for cmd in get(g:, 'startify_session_before_save', [])
  243. execute cmd
  244. endfor
  245. let ssop = &sessionoptions
  246. set sessionoptions-=options
  247. try
  248. execute 'mksession!' a:spath
  249. catch
  250. echohl ErrorMsg
  251. echomsg v:exception
  252. echohl NONE
  253. return
  254. finally
  255. let &sessionoptions = ssop
  256. endtry
  257. if exists('g:startify_session_remove_lines')
  258. \ || exists('g:startify_session_savevars')
  259. \ || exists('g:startify_session_savecmds')
  260. silent execute 'split' a:spath
  261. " remove lines from the session file
  262. if exists('g:startify_session_remove_lines')
  263. for pattern in g:startify_session_remove_lines
  264. execute 'silent global/'. pattern .'/delete _'
  265. endfor
  266. endif
  267. " put existing variables from savevars into session file
  268. if exists('g:startify_session_savevars')
  269. call append(line('$')-3, map(filter(copy(g:startify_session_savevars), 'exists(v:val)'), '"let ". v:val ." = ". strtrans(string(eval(v:val)))'))
  270. endif
  271. " put commands from savecmds into session file
  272. if exists('g:startify_session_savecmds')
  273. call append(line('$')-3, g:startify_session_savecmds)
  274. endif
  275. setlocal bufhidden=delete
  276. silent update
  277. silent hide
  278. endif
  279. call s:create_last_session_link(a:spath)
  280. endfunction
  281. " Function: #session_delete {{{1
  282. function! startify#session_delete(bang, ...) abort
  283. if !isdirectory(s:session_dir)
  284. echo 'The session directory does not exist: '. s:session_dir
  285. return
  286. elseif empty(startify#session_list_as_string(''))
  287. echo 'There are no sessions...'
  288. return
  289. endif
  290. call inputsave()
  291. let spath = s:session_dir . s:sep . (exists('a:1')
  292. \ ? a:1
  293. \ : input('Delete this session: ', fnamemodify(v:this_session, ':t'), 'custom,startify#session_list_as_string'))
  294. \ | redraw
  295. call inputrestore()
  296. if !filereadable(spath)
  297. echomsg 'No such session: '. spath
  298. return
  299. endif
  300. echo 'Really delete '. spath .'? [y/n]' | redraw
  301. if a:bang || nr2char(getchar()) == 'y'
  302. if delete(spath) == 0
  303. echo 'Deleted session '. spath .'!'
  304. else
  305. echo 'Deletion failed!'
  306. endif
  307. else
  308. echo 'Deletion aborted!'
  309. endif
  310. endfunction
  311. " Function: #session_delete_buffers {{{1
  312. function! startify#session_delete_buffers()
  313. if !s:delete_buffers
  314. return
  315. endif
  316. let n = 1
  317. while n <= bufnr('$')
  318. if buflisted(n)
  319. try
  320. silent execute 'bdelete' n
  321. catch
  322. echohl ErrorMsg
  323. echomsg v:exception
  324. echohl NONE
  325. endtry
  326. endif
  327. let n += 1
  328. endwhile
  329. endfunction
  330. " Function: #session_list {{{1
  331. function! startify#session_list(lead, ...) abort
  332. return filter(map(split(globpath(s:session_dir, '*'.a:lead.'*'), '\n'), 'fnamemodify(v:val, ":t")'), 'v:val !=# "__LAST__"')
  333. endfunction
  334. " Function: #session_list_as_string {{{1
  335. function! startify#session_list_as_string(lead, ...) abort
  336. return join(filter(map(split(globpath(s:session_dir, '*'.a:lead.'*'), '\n'), 'fnamemodify(v:val, ":t")'), 'v:val !=# "__LAST__"'), "\n")
  337. endfunction
  338. " Function: #debug {{{1
  339. function! startify#debug()
  340. if exists('s:entries')
  341. for k in sort(keys(s:entries))
  342. echomsg '['. k .'] = '. string(s:entries[k])
  343. endfor
  344. endif
  345. endfunction
  346. " Function: #open_buffers {{{1
  347. function! startify#open_buffers(...) abort
  348. if exists('a:1') " used in mappings
  349. call s:open_buffer(s:entries[a:1])
  350. return
  351. endif
  352. let marked = filter(copy(s:entries), 'v:val.marked')
  353. if empty(marked) " open current entry
  354. call s:open_buffer(s:entries[line('.')])
  355. return
  356. endif
  357. enew
  358. setlocal nobuflisted
  359. " Open all marked entries.
  360. for entry in sort(values(marked), 's:sort_by_tick')
  361. call s:open_buffer(entry)
  362. endfor
  363. wincmd =
  364. endfunction
  365. " Function: s:open_buffer {{{1
  366. function! s:open_buffer(entry)
  367. if a:entry.type == 'special'
  368. execute a:entry.cmd
  369. elseif a:entry.type == 'session'
  370. execute a:entry.cmd fnameescape(a:entry.path)
  371. elseif a:entry.type == 'file'
  372. if line2byte('$') == -1
  373. execute 'edit' fnameescape(a:entry.path)
  374. else
  375. if a:entry.cmd == 'tabnew'
  376. wincmd =
  377. endif
  378. execute a:entry.cmd fnameescape(a:entry.path)
  379. endif
  380. call s:check_user_options(a:entry.path)
  381. endif
  382. endfunction
  383. " Function: s:display_by_path {{{1
  384. function! s:display_by_path(path_prefix, path_format, use_env) abort
  385. let oldfiles = call(get(g:, 'startify_enable_unsafe') ? 's:filter_oldfiles_unsafe' : 's:filter_oldfiles',
  386. \ [a:path_prefix, a:path_format, a:use_env])
  387. let entry_format = "' ['. index .']'. repeat(' ', (3 - strlen(index)))"
  388. if exists('*WebDevIconsGetFileTypeSymbol') " support for vim-devicons
  389. let entry_format .= ". WebDevIconsGetFileTypeSymbol(entry_path) .' '. entry_path"
  390. else
  391. let entry_format .= '. entry_path'
  392. endif
  393. if !empty(oldfiles)
  394. if exists('s:last_message')
  395. call s:print_section_header()
  396. endif
  397. for [absolute_path, entry_path] in oldfiles
  398. let index = s:get_index_as_string(s:entry_number)
  399. call append('$', eval(entry_format))
  400. if has('win32')
  401. let absolute_path = substitute(absolute_path, '\[', '\[[]', 'g')
  402. endif
  403. call s:register(line('$'), index, 'file', 'edit', absolute_path, s:nowait)
  404. let s:entry_number += 1
  405. endfor
  406. call append('$', '')
  407. endif
  408. endfunction
  409. " Function: s:filter_oldfiles {{{1
  410. function! s:filter_oldfiles(path_prefix, path_format, use_env) abort
  411. let path_prefix = '\V'. escape(a:path_prefix, '\')
  412. let counter = s:numfiles
  413. let entries = {}
  414. let oldfiles = []
  415. for fname in v:oldfiles
  416. if counter <= 0
  417. break
  418. endif
  419. let absolute_path = fnamemodify(resolve(fname), ":p")
  420. " filter duplicates, bookmarks and entries from the skiplist
  421. if has_key(entries, absolute_path)
  422. \ || !filereadable(absolute_path)
  423. \ || s:is_in_skiplist(absolute_path)
  424. \ || match(absolute_path, path_prefix)
  425. continue
  426. endif
  427. let entry_path = ''
  428. if s:tf
  429. let entry_path = s:transform(absolute_path)
  430. endif
  431. if empty(entry_path)
  432. let entry_path = fnamemodify(absolute_path, a:path_format)
  433. endif
  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. if get(g:, 'startify_session_sort')
  498. function! s:sort_by_mtime(foo, bar)
  499. return getftime(a:foo) <= getftime(a:bar)
  500. endfunction
  501. call sort(sfiles, 's:sort_by_mtime')
  502. endif
  503. for i in range(len(sfiles))
  504. let index = s:get_index_as_string(s:entry_number)
  505. let fname = fnamemodify(sfiles[i], ':t')
  506. call append('$', ' ['. index .']'. repeat(' ', (3 - strlen(index))) . fname)
  507. if has('win32')
  508. let fname = substitute(fname, '\[', '\[[]', 'g')
  509. endif
  510. call s:register(line('$'), index, 'session', 'SLoad', fname, s:nowait)
  511. let s:entry_number += 1
  512. endfor
  513. call append('$', '')
  514. endfunction
  515. " Function: s:show_bookmarks {{{1
  516. function! s:show_bookmarks() abort
  517. if !exists('g:startify_bookmarks') || empty(g:startify_bookmarks)
  518. return
  519. endif
  520. if exists('s:last_message')
  521. call s:print_section_header()
  522. endif
  523. for bookmark in g:startify_bookmarks
  524. if type(bookmark) == type({})
  525. let [index, path] = items(bookmark)[0]
  526. else " string
  527. let [index, path] = [s:get_index_as_string(s:entry_number), bookmark]
  528. let s:entry_number += 1
  529. endif
  530. let entry_path = ''
  531. if s:tf
  532. let entry_path = s:transform(fnamemodify(resolve(expand(path)), ':p'))
  533. endif
  534. if empty(entry_path)
  535. let entry_path = path
  536. endif
  537. call append('$', ' ['. index .']'. repeat(' ', (3 - strlen(index))) . entry_path)
  538. if has('win32')
  539. let path = substitute(path, '\[', '\[[]', 'g')
  540. endif
  541. call s:register(line('$'), index, 'file', 'edit', path, s:nowait)
  542. unlet bookmark " avoid type mismatch for heterogeneous lists
  543. endfor
  544. call append('$', '')
  545. endfunction
  546. " Function: s:show_commands {{{1
  547. function! s:show_commands() abort
  548. if !exists('g:startify_commands') || empty(g:startify_commands)
  549. return
  550. endif
  551. if exists('s:last_message')
  552. call s:print_section_header()
  553. endif
  554. for entry in g:startify_commands
  555. if type(entry) == type({}) " with custom index
  556. let [index, command] = items(entry)[0]
  557. else
  558. let command = entry
  559. let index = s:get_index_as_string(s:entry_number)
  560. let s:entry_number += 1
  561. endif
  562. " If no list is given, the description is the command itself.
  563. let [desc, cmd] = type(command) == type([]) ? command : [command, command]
  564. call append('$', ' ['. index .']'. repeat(' ', (3 - strlen(index))) . desc)
  565. call s:register(line('$'), index, 'special', cmd, '', s:nowait_string)
  566. unlet entry command
  567. endfor
  568. call append('$', '')
  569. endfunction
  570. " Function: s:is_in_skiplist {{{1
  571. function! s:is_in_skiplist(arg) abort
  572. for regexp in s:skiplist
  573. try
  574. if a:arg =~# regexp
  575. return 1
  576. endif
  577. catch
  578. echohl WarningMsg
  579. echomsg 'startify: Pattern '. string(regexp) .' threw an exception. Read :help g:startify_skiplist'
  580. echohl NONE
  581. endtry
  582. endfor
  583. endfunction
  584. " Function: s:set_cursor {{{1
  585. function! s:set_cursor() abort
  586. let s:oldline = exists('s:newline') ? s:newline : 5
  587. let s:newline = line('.')
  588. " going up (-1) or down (1)
  589. let movement = 2 * (s:newline > s:oldline) - 1
  590. " skip section headers lines until an entry is found
  591. while index(b:startify_section_header_lines, s:newline) != -1
  592. let s:newline += movement
  593. endwhile
  594. " skip blank lines between lists
  595. if empty(getline(s:newline))
  596. let s:newline += movement
  597. endif
  598. " don't go beyond first or last entry
  599. let s:newline = max([s:firstline, min([s:lastline, s:newline])])
  600. call cursor(s:newline, 5)
  601. endfunction
  602. " Function: s:set_mappings {{{1
  603. function! s:set_mappings() abort
  604. execute "nnoremap <buffer>". s:nowait_string ."<silent> i :enew <bar> startinsert<cr>"
  605. execute "nnoremap <buffer>". s:nowait_string ."<silent> <insert> :enew <bar> startinsert<cr>"
  606. execute "nnoremap <buffer>". s:nowait_string ."<silent> b :call <sid>set_mark('B')<cr>"
  607. execute "nnoremap <buffer>". s:nowait_string ."<silent> s :call <sid>set_mark('S')<cr>"
  608. execute "nnoremap <buffer>". s:nowait_string ."<silent> t :call <sid>set_mark('T')<cr>"
  609. execute "nnoremap <buffer>". s:nowait_string ."<silent> v :call <sid>set_mark('V')<cr>"
  610. execute "nnoremap <buffer>". s:nowait_string ."<silent> <cr> :call startify#open_buffers()<cr>"
  611. execute "nnoremap <buffer>". s:nowait_string ."<silent> <2-LeftMouse> :call startify#open_buffers()<cr>"
  612. " Without these mappings n/N wouldn't work properly, since autocmds always
  613. " force the cursor back on the index.
  614. nnoremap <buffer><expr> n ' j'[v:searchforward].'n'
  615. nnoremap <buffer><expr> N 'j '[v:searchforward].'N'
  616. for k in keys(s:entries)
  617. execute 'nnoremap <buffer><silent>'. s:entries[k].nowait s:entries[k].index
  618. \ ':call startify#open_buffers('. string(k) .')<cr>'
  619. endfor
  620. " Prevent 'nnoremap j gj' mappings, since they would break navigation.
  621. " (One can't leave the [x].)
  622. if !empty(maparg('j', 'n'))
  623. nnoremap <buffer> j j
  624. endif
  625. if !empty(maparg('k', 'n'))
  626. nnoremap <buffer> k k
  627. endif
  628. endfunction
  629. " Function: s:set_mark {{{1
  630. function! s:set_mark(type, ...) abort
  631. let index = expand('<cword>')
  632. let line = exists('a:1') ? a:1 : line('.')
  633. let entry = s:entries[line]
  634. if entry.type != 'file'
  635. return
  636. endif
  637. let default_cmds = {
  638. \ 'B': 'edit',
  639. \ 'S': 'split',
  640. \ 'V': 'vsplit',
  641. \ 'T': 'tabnew',
  642. \ }
  643. setlocal modifiable
  644. if entry.marked && index[0] == a:type
  645. let entry.cmd = 'edit'
  646. let entry.marked = 0
  647. execute 'normal! ci]'. entry.index
  648. else
  649. let entry.cmd = default_cmds[a:type]
  650. let entry.marked = 1
  651. let entry.tick = s:tick
  652. let s:tick += 1
  653. execute 'normal! ci]'. repeat(a:type, len(index))
  654. endif
  655. setlocal nomodifiable nomodified
  656. endfunction
  657. " Function: s:sort_by_tick {{{1
  658. function! s:sort_by_tick(one, two)
  659. return a:one.tick - a:two.tick
  660. endfunction
  661. " Function: s:check_user_options {{{1
  662. function! s:check_user_options(path) abort
  663. let session = a:path . s:sep .'Session.vim'
  664. if get(g:, 'startify_session_autoload') && filereadable(session)
  665. execute 'source' session
  666. elseif get(g:, 'startify_change_to_vcs_root')
  667. call s:cd_to_vcs_root(a:path)
  668. elseif get(g:, 'startify_change_to_dir', 1)
  669. execute 'lcd' fnameescape(isdirectory(a:path) ? a:path : fnamemodify(a:path, ':h'))
  670. endif
  671. endfunction
  672. " Function: s:cd_to_vcs_root {{{1
  673. function! s:cd_to_vcs_root(path) abort
  674. let dir = fnamemodify(a:path, ':p:h')
  675. for vcs in [ '.git', '.hg', '.bzr', '.svn' ]
  676. let root = finddir(vcs, dir .';')
  677. if !empty(root)
  678. execute 'cd '. fnameescape(fnamemodify(root, ':h'))
  679. return
  680. endif
  681. endfor
  682. endfunction
  683. " Function: s:close {{{1
  684. function! s:close() abort
  685. if len(filter(range(0, bufnr('$')), 'buflisted(v:val)')) - &buflisted
  686. if bufloaded(bufnr('#')) && bufnr('#') != bufnr('%')
  687. buffer #
  688. else
  689. bnext
  690. endif
  691. else
  692. quit
  693. endif
  694. endfunction
  695. " Function: s:get_index_as_string {{{1
  696. function! s:get_index_as_string(idx) abort
  697. if exists('g:startify_custom_indices')
  698. let listlen = len(g:startify_custom_indices)
  699. return (a:idx < listlen) ? g:startify_custom_indices[a:idx] : string(a:idx - listlen)
  700. else
  701. return string(a:idx)
  702. endif
  703. endfunction
  704. " Function: s:print_section_header {{{1
  705. function! s:print_section_header() abort
  706. $
  707. let curline = line('.')
  708. for lnum in range(curline, curline + len(s:last_message) + 1)
  709. call add(b:startify_section_header_lines, lnum)
  710. endfor
  711. call append('$', s:last_message + [''])
  712. unlet s:last_message
  713. endfunction
  714. " Function: s:register {{{1
  715. function! s:register(line, index, type, cmd, path, wait)
  716. let s:entries[a:line] = {
  717. \ 'index': a:index,
  718. \ 'type': a:type,
  719. \ 'cmd': a:cmd,
  720. \ 'path': a:path,
  721. \ 'nowait': a:wait,
  722. \ 'marked': 0,
  723. \ }
  724. endfunction
  725. " Function: s:create_last_session_link {{{1
  726. function! s:create_last_session_link(spath)
  727. if !has('win32') && a:spath !~# '__LAST__$'
  728. let cmd = printf('ln -sf %s %s',
  729. \ shellescape(fnamemodify(a:spath, ':t')),
  730. \ shellescape(s:session_dir .'/__LAST__'))
  731. silent! call system(cmd)
  732. if v:shell_error
  733. echomsg "startify: Can't create 'last used session' symlink."
  734. endif
  735. endif
  736. endfunction
  737. " Function: s:init_env {{{1
  738. function! s:init_env()
  739. let s:env = []
  740. let ignore = { 'PWD': 1, 'OLDPWD': 1 }
  741. function! s:get_env()
  742. redir => s
  743. silent! execute "norm!:ec$\<c-a>'\<c-b>\<right>\<right>\<del>'\<cr>"
  744. redir END
  745. redraw
  746. return split(s)
  747. endfunction
  748. function! s:compare_by_key_len(foo, bar)
  749. return len(a:foo[0]) - len(a:bar[0])
  750. endfunction
  751. function! s:compare_by_val_len(foo, bar)
  752. return len(a:bar[1]) - len(a:foo[1])
  753. endfunction
  754. for k in s:get_env()
  755. silent! execute "let v = eval('$'.k)"
  756. if has('win32') ? (v[1] != ':') : (v[0] != '/')
  757. \ || has_key(ignore, k)
  758. \ || len(k) > len(v)
  759. continue
  760. endif
  761. call insert(s:env, [k,v], 0)
  762. endfor
  763. let s:env = sort(s:env, 's:compare_by_key_len')
  764. let s:env = sort(s:env, 's:compare_by_val_len')
  765. endfunction
  766. " Function: s:transform {{{1
  767. function s:transform(absolute_path)
  768. for [k,V] in g:startify_transformations
  769. if a:absolute_path =~ k
  770. return type(V) == type('') ? V : V(a:absolute_path)
  771. endif
  772. unlet V
  773. endfor
  774. return ''
  775. endfunction