startify.vim 29 KB

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