startify.vim 27 KB

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