startify.vim 31 KB

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