startify.vim 25 KB

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