startify.vim 27 KB

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