startify.vim 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084
  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 seesion_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. call s:open_buffer(b:startify.entries[a:1])
  324. return
  325. endif
  326. let marked = filter(copy(b:startify.entries), 'v:val.marked')
  327. if empty(marked) " open current entry
  328. call s:open_buffer(b:startify.entries[line('.')])
  329. return
  330. endif
  331. enew
  332. setlocal nobuflisted
  333. " Open all marked entries.
  334. for entry in sort(values(marked), 's:sort_by_tick')
  335. call s:open_buffer(entry)
  336. endfor
  337. wincmd =
  338. if exists('#User#StartifyAllBuffersOpened')
  339. autocmd <nomodeline> User StartifyAllBuffersOpened
  340. endif
  341. endfunction
  342. " Function: s:get_lists {{{1
  343. function! s:get_lists() abort
  344. if exists('g:startify_lists')
  345. return g:startify_lists
  346. elseif exists('g:startify_list_order')
  347. " Convert old g:startify_list_order format to newer g:startify_lists format.
  348. let lists = []
  349. for item in g:startify_list_order
  350. if type(item) == type([])
  351. let header = item
  352. else
  353. if exists('header')
  354. let lists += [{ 'type': item, 'header': header }]
  355. unlet header
  356. else
  357. let lists += [{ 'type': item }]
  358. endif
  359. endif
  360. unlet item
  361. endfor
  362. return lists
  363. else
  364. return [
  365. \ { 'header': [s:padding_left .'MRU'], 'type': 'files' },
  366. \ { 'header': [s:padding_left .'MRU '. getcwd()], 'type': 'dir' },
  367. \ { 'header': [s:padding_left .'Sessions'], 'type': 'sessions' },
  368. \ { 'header': [s:padding_left .'Bookmarks'], 'type': 'bookmarks' },
  369. \ { 'header': [s:padding_left .'Commands'], 'type': 'commands' },
  370. \ ]
  371. endif
  372. endfunction
  373. " Function: s:show_lists {{{1
  374. function! s:show_lists(lists) abort
  375. for list in a:lists
  376. if !has_key(list, 'type')
  377. continue
  378. endif
  379. let b:startify.indices = copy(get(list, 'indices', []))
  380. if type(list.type) == type('')
  381. if has_key(list, 'header')
  382. let s:last_message = list.header
  383. endif
  384. call s:show_{list.type}()
  385. elseif type(list.type) == type(function('tr'))
  386. try
  387. let entries = list.type()
  388. catch
  389. call s:warn(v:exception)
  390. continue
  391. endtry
  392. if empty(entries)
  393. unlet! s:last_message
  394. continue
  395. endif
  396. if has_key(list, 'header')
  397. let s:last_message = list.header
  398. call s:print_section_header()
  399. endif
  400. for entry in entries
  401. let cmd = get(entry, 'cmd', 'edit')
  402. let path = get(entry, 'path', '')
  403. let type = get(entry, 'type', empty(path) ? 'special' : 'file')
  404. let index = s:get_index_as_string()
  405. call append('$', s:padding_left .'['. index .']'. repeat(' ', (3 - strlen(index))) . entry.line)
  406. call s:register(line('$'), index, type, cmd, path)
  407. endfor
  408. call append('$', '')
  409. else
  410. call s:warn('Wrong format for g:startify_lists: '. string(list))
  411. endif
  412. endfor
  413. endfunction
  414. " Function: s:open_buffer {{{1
  415. function! s:open_buffer(entry)
  416. if a:entry.type == 'special'
  417. execute a:entry.cmd
  418. elseif a:entry.type == 'session'
  419. execute a:entry.cmd a:entry.path
  420. elseif a:entry.type == 'file'
  421. if line2byte('$') == -1
  422. execute 'edit' a:entry.path
  423. else
  424. if a:entry.cmd == 'tabnew'
  425. wincmd =
  426. endif
  427. execute a:entry.cmd a:entry.path
  428. endif
  429. call s:check_user_options(a:entry.path)
  430. endif
  431. if exists('#User#StartifyBufferOpened')
  432. autocmd <nomodeline> User StartifyBufferOpened
  433. endif
  434. endfunction
  435. " Function: s:set_custom_section {{{1
  436. function! s:set_custom_section(section) abort
  437. if type(a:section) == type([])
  438. return copy(a:section)
  439. elseif type(a:section) == type('')
  440. return empty(a:section) ? [] : eval(a:section)
  441. endif
  442. return []
  443. endfunction
  444. " Function: s:display_by_path {{{1
  445. function! s:display_by_path(path_prefix, path_format, use_env) abort
  446. let oldfiles = call(get(g:, 'startify_enable_unsafe') ? 's:filter_oldfiles_unsafe' : 's:filter_oldfiles',
  447. \ [a:path_prefix, a:path_format, a:use_env])
  448. let entry_format = "s:padding_left .'['. index .']'. repeat(' ', (3 - strlen(index))) ."
  449. if exists('*StartifyEntryFormat')
  450. let entry_format .= StartifyEntryFormat()
  451. else
  452. let entry_format .= 'entry_path'
  453. endif
  454. if !empty(oldfiles)
  455. if exists('s:last_message')
  456. call s:print_section_header()
  457. endif
  458. for [absolute_path, entry_path] in oldfiles
  459. let index = s:get_index_as_string()
  460. call append('$', eval(entry_format))
  461. if has('win32')
  462. let absolute_path = substitute(absolute_path, '\[', '\[[]', 'g')
  463. endif
  464. call s:register(line('$'), index, 'file', 'edit', absolute_path)
  465. endfor
  466. call append('$', '')
  467. endif
  468. endfunction
  469. " Function: s:filter_oldfiles {{{1
  470. function! s:filter_oldfiles(path_prefix, path_format, use_env) abort
  471. let path_prefix = '\V'. escape(a:path_prefix, '\')
  472. let counter = s:numfiles
  473. let entries = {}
  474. let oldfiles = []
  475. for fname in v:oldfiles
  476. if counter <= 0
  477. break
  478. endif
  479. if s:is_in_skiplist(fname)
  480. " https://github.com/mhinz/vim-startify/issues/353
  481. continue
  482. endif
  483. let absolute_path = fnamemodify(resolve(fname), ":p")
  484. " filter duplicates, bookmarks and entries from the skiplist
  485. if has_key(entries, absolute_path)
  486. \ || !filereadable(absolute_path)
  487. \ || s:is_in_skiplist(absolute_path)
  488. \ || match(absolute_path, path_prefix)
  489. continue
  490. endif
  491. let entry_path = ''
  492. if s:tf
  493. let entry_path = s:transform(absolute_path)
  494. endif
  495. if empty(entry_path)
  496. let entry_path = fnamemodify(absolute_path, a:path_format)
  497. endif
  498. let entries[absolute_path] = 1
  499. let counter -= 1
  500. let oldfiles += [[fnameescape(absolute_path), entry_path]]
  501. endfor
  502. if a:use_env
  503. call s:init_env()
  504. for i in range(len(oldfiles))
  505. for [k,v] in s:env
  506. let p = oldfiles[i][0]
  507. if !stridx(tolower(p), tolower(v))
  508. let oldfiles[i][1] = printf('$%s%s', k, p[len(v):])
  509. break
  510. endif
  511. endfor
  512. endfor
  513. endif
  514. return oldfiles
  515. endfunction
  516. " Function: s:filter_oldfiles_unsafe {{{1
  517. function! s:filter_oldfiles_unsafe(path_prefix, path_format, use_env) abort
  518. let path_prefix = '\V'. escape(a:path_prefix, '\')
  519. let counter = s:numfiles
  520. let entries = {}
  521. let oldfiles = []
  522. let is_dir = escape(s:sep, '\') . '$'
  523. for fname in v:oldfiles
  524. if counter <= 0
  525. break
  526. endif
  527. if s:is_in_skiplist(fname)
  528. " https://github.com/mhinz/vim-startify/issues/353
  529. continue
  530. endif
  531. let absolute_path = glob(fnamemodify(fname, ":p"))
  532. if empty(absolute_path)
  533. \ || has_key(entries, absolute_path)
  534. \ || (absolute_path =~ is_dir)
  535. \ || match(absolute_path, path_prefix)
  536. \ || s:is_in_skiplist(absolute_path)
  537. continue
  538. endif
  539. let entry_path = fnamemodify(absolute_path, a:path_format)
  540. let entries[absolute_path] = 1
  541. let counter -= 1
  542. let oldfiles += [[fnameescape(absolute_path), entry_path]]
  543. endfor
  544. return oldfiles
  545. endfunction
  546. " Function: s:show_dir {{{1
  547. function! s:show_dir() abort
  548. return s:display_by_path(getcwd() . s:sep, ':.', 0)
  549. endfunction
  550. " Function: s:show_files {{{1
  551. function! s:show_files() abort
  552. return s:display_by_path('', s:relative_path, get(g:, 'startify_use_env'))
  553. endfunction
  554. " Function: s:show_sessions {{{1
  555. function! s:show_sessions() abort
  556. let limit = get(g:, 'startify_session_number', 999) - 1
  557. if limit <= -1
  558. return
  559. endif
  560. let sfiles = split(globpath(s:session_dir, '*'), '\n')
  561. let sfiles = filter(sfiles, 'v:val !~# "__LAST__$"')
  562. let sfiles = filter(sfiles,
  563. \ '!(v:val =~# "x\.vim$" && index(sfiles, v:val[:-6].".vim") >= 0)')
  564. if empty(sfiles)
  565. if exists('s:last_message')
  566. unlet s:last_message
  567. endif
  568. return
  569. endif
  570. if exists('s:last_message')
  571. call s:print_section_header()
  572. endif
  573. if get(g:, 'startify_session_sort')
  574. function! s:sort_by_mtime(foo, bar)
  575. let foo = getftime(a:foo)
  576. let bar = getftime(a:bar)
  577. return foo == bar ? 0 : (foo < bar ? 1 : -1)
  578. endfunction
  579. call sort(sfiles, 's:sort_by_mtime')
  580. endif
  581. for i in range(len(sfiles))
  582. let index = s:get_index_as_string()
  583. let fname = fnamemodify(sfiles[i], ':t')
  584. let dname = sfiles[i] ==# v:this_session ? fname.' (*)' : fname
  585. call append('$', s:padding_left .'['. index .']'. repeat(' ', (3 - strlen(index))) . dname)
  586. if has('win32')
  587. let fname = substitute(fname, '\[', '\[[]', 'g')
  588. endif
  589. call s:register(line('$'), index, 'session', 'SLoad', fname)
  590. if i == limit
  591. break
  592. endif
  593. endfor
  594. call append('$', '')
  595. endfunction
  596. " Function: s:show_bookmarks {{{1
  597. function! s:show_bookmarks() abort
  598. if !exists('g:startify_bookmarks') || empty(g:startify_bookmarks)
  599. return
  600. endif
  601. if exists('s:last_message')
  602. call s:print_section_header()
  603. endif
  604. for bookmark in g:startify_bookmarks
  605. if type(bookmark) == type({})
  606. let [index, path] = items(bookmark)[0]
  607. else " string
  608. let [index, path] = [s:get_index_as_string(), bookmark]
  609. endif
  610. let entry_path = ''
  611. if s:tf
  612. let entry_path = s:transform(fnamemodify(resolve(expand(path)), ':p'))
  613. endif
  614. if empty(entry_path)
  615. let entry_path = path
  616. endif
  617. call append('$', s:padding_left .'['. index .']'. repeat(' ', (3 - strlen(index))) . entry_path)
  618. if has('win32')
  619. let path = substitute(path, '\[', '\[[]', 'g')
  620. endif
  621. call s:register(line('$'), index, 'file', 'edit', fnameescape(expand(path)))
  622. unlet bookmark " avoid type mismatch for heterogeneous lists
  623. endfor
  624. call append('$', '')
  625. endfunction
  626. " Function: s:show_commands {{{1
  627. function! s:show_commands() abort
  628. if !exists('g:startify_commands') || empty(g:startify_commands)
  629. return
  630. endif
  631. if exists('s:last_message')
  632. call s:print_section_header()
  633. endif
  634. for entry in g:startify_commands
  635. if type(entry) == type({}) " with custom index
  636. let [index, command] = items(entry)[0]
  637. else
  638. let command = entry
  639. let index = s:get_index_as_string()
  640. endif
  641. " If no list is given, the description is the command itself.
  642. let [desc, cmd] = type(command) == type([]) ? command : [command, command]
  643. call append('$', s:padding_left .'['. index .']'. repeat(' ', (3 - strlen(index))) . desc)
  644. call s:register(line('$'), index, 'special', cmd, '')
  645. unlet entry command
  646. endfor
  647. call append('$', '')
  648. endfunction
  649. " Function: s:is_in_skiplist {{{1
  650. function! s:is_in_skiplist(arg) abort
  651. for regexp in s:skiplist
  652. try
  653. if a:arg =~# regexp
  654. return 1
  655. endif
  656. catch
  657. call s:warn('Pattern '. string(regexp) .' threw an exception. Read :help g:startify_skiplist')
  658. endtry
  659. endfor
  660. endfunction
  661. " Function: s:set_cursor {{{1
  662. function! s:set_cursor() abort
  663. let b:startify.oldline = exists('b:startify.newline') ? b:startify.newline : 2 + len(s:padding_left)
  664. let b:startify.newline = line('.')
  665. " going up (-1) or down (1)
  666. if b:startify.oldline == b:startify.newline
  667. \ && col('.') != s:fixed_column
  668. \ && !b:startify.leftmouse
  669. let movement = 2 * (col('.') > s:fixed_column) - 1
  670. let b:startify.newline += movement
  671. else
  672. let movement = 2 * (b:startify.newline > b:startify.oldline) - 1
  673. let b:startify.leftmouse = 0
  674. endif
  675. " skip section headers lines until an entry is found
  676. while index(b:startify.section_header_lines, b:startify.newline) != -1
  677. let b:startify.newline += movement
  678. endwhile
  679. " skip blank lines between lists
  680. if empty(getline(b:startify.newline))
  681. let b:startify.newline += movement
  682. endif
  683. " don't go beyond first or last entry
  684. let b:startify.newline = max([b:startify.firstline, min([b:startify.lastline, b:startify.newline])])
  685. call cursor(b:startify.newline, s:fixed_column)
  686. endfunction
  687. " Function: s:set_mappings {{{1
  688. function! s:set_mappings() abort
  689. nnoremap <buffer><nowait><silent> i :enew <bar> startinsert<cr>
  690. nnoremap <buffer><nowait><silent> <insert> :enew <bar> startinsert<cr>
  691. nnoremap <buffer><nowait><silent> b :call <sid>set_mark('B')<cr>
  692. nnoremap <buffer><nowait><silent> s :call <sid>set_mark('S')<cr>
  693. nnoremap <buffer><nowait><silent> t :call <sid>set_mark('T')<cr>
  694. nnoremap <buffer><nowait><silent> v :call <sid>set_mark('V')<cr>
  695. nnoremap <buffer><nowait><silent> <cr> :call startify#open_buffers()<cr>
  696. nnoremap <buffer><nowait><silent> <LeftMouse> :call <sid>leftmouse()<cr>
  697. nnoremap <buffer><nowait><silent> <2-LeftMouse> :call startify#open_buffers()<cr>
  698. nnoremap <buffer><nowait><silent> <MiddleMouse> :enew <bar> execute 'normal! "'.(v:register=='"'?'*':v:register).'gp'<cr>
  699. " Without these mappings n/N wouldn't work properly, since autocmds always
  700. " force the cursor back on the index.
  701. nnoremap <buffer><expr> n ' j'[v:searchforward].'n'
  702. nnoremap <buffer><expr> N 'j '[v:searchforward].'N'
  703. function! s:leftmouse()
  704. " feedkeys() triggers CursorMoved which calls s:set_cursor() which checks
  705. " .leftmouse.
  706. let b:startify.leftmouse = 1
  707. call feedkeys("\<LeftMouse>", 'nt')
  708. endfunction
  709. function! s:compare_by_index(foo, bar)
  710. return a:foo.index - a:bar.index
  711. endfunction
  712. for entry in sort(values(b:startify.entries), 's:compare_by_index')
  713. execute 'nnoremap <buffer><silent><nowait>' entry.index
  714. \ ':call startify#open_buffers('. string(entry.line) .')<cr>'
  715. endfor
  716. endfunction
  717. " Function: s:set_mark {{{1
  718. function! s:set_mark(type, ...) abort
  719. let index = expand('<cword>')
  720. let line = exists('a:1') ? a:1 : line('.')
  721. let entry = b:startify.entries[line]
  722. if entry.type != 'file'
  723. return
  724. endif
  725. let default_cmds = {
  726. \ 'B': 'edit',
  727. \ 'S': 'split',
  728. \ 'V': 'vsplit',
  729. \ 'T': 'tabnew',
  730. \ }
  731. setlocal modifiable
  732. if entry.marked && index[0] == a:type
  733. let entry.cmd = 'edit'
  734. let entry.marked = 0
  735. execute 'normal! ci]'. entry.index
  736. else
  737. let entry.cmd = default_cmds[a:type]
  738. let entry.marked = 1
  739. let entry.tick = b:startify.tick
  740. let b:startify.tick += 1
  741. execute 'normal! ci]'. repeat(a:type, len(index))
  742. endif
  743. " Reset cursor to fixed column, which is important for s:set_cursor().
  744. call cursor(line('.'), s:fixed_column)
  745. setlocal nomodifiable nomodified
  746. endfunction
  747. " Function: s:sort_by_tick {{{1
  748. function! s:sort_by_tick(one, two)
  749. return a:one.tick - a:two.tick
  750. endfunction
  751. " Function: s:check_user_options {{{1
  752. function! s:check_user_options(path) abort
  753. let session = a:path . s:sep .'Session.vim'
  754. if get(g:, 'startify_session_autoload') && filereadable(glob(session))
  755. execute 'silent bwipeout' a:path
  756. call startify#session_delete_buffers()
  757. execute 'source' session
  758. return
  759. endif
  760. if get(g:, 'startify_change_to_vcs_root') && s:cd_to_vcs_root(a:path)
  761. return
  762. endif
  763. if get(g:, 'startify_change_to_dir', 1)
  764. if isdirectory(a:path)
  765. execute 'lcd' a:path
  766. else
  767. let dir = fnamemodify(a:path, ':h')
  768. if isdirectory(dir)
  769. execute 'lcd' dir
  770. else
  771. " Do nothing. E.g. a:path == `scp://foo/bar`
  772. endif
  773. endif
  774. endif
  775. endfunction
  776. " Function: s:cd_to_vcs_root {{{1
  777. function! s:cd_to_vcs_root(path) abort
  778. let dir = fnamemodify(a:path, ':p:h')
  779. for vcs in [ '.git', '.hg', '.bzr', '.svn' ]
  780. let root = finddir(vcs, dir .';')
  781. if !empty(root)
  782. execute 'lcd' fnameescape(fnamemodify(root, ':h'))
  783. return 1
  784. endif
  785. endfor
  786. return 0
  787. endfunction
  788. " Function: s:close {{{1
  789. function! s:close() abort
  790. if len(filter(range(0, bufnr('$')), 'buflisted(v:val)')) - &buflisted
  791. if bufloaded(bufnr('#')) && bufnr('#') != bufnr('%')
  792. buffer #
  793. else
  794. bnext
  795. endif
  796. else
  797. quit
  798. endif
  799. endfunction
  800. " Function: s:get_index_as_string {{{1
  801. function! s:get_index_as_string() abort
  802. if !empty(b:startify.indices)
  803. return remove(b:startify.indices, 0)
  804. elseif exists('g:startify_custom_indices')
  805. let listlen = len(g:startify_custom_indices)
  806. if b:startify.entry_number < listlen
  807. let idx = g:startify_custom_indices[b:startify.entry_number]
  808. else
  809. let idx = string(b:startify.entry_number - listlen)
  810. endif
  811. else
  812. let idx = string(b:startify.entry_number)
  813. endif
  814. let b:startify.entry_number += 1
  815. return idx
  816. endfunction
  817. " Function: s:print_section_header {{{1
  818. function! s:print_section_header() abort
  819. $
  820. let curline = line('.')
  821. for lnum in range(curline, curline + len(s:last_message) + 1)
  822. call add(b:startify.section_header_lines, lnum)
  823. endfor
  824. call append('$', s:last_message + [''])
  825. unlet s:last_message
  826. endfunction
  827. " Function: s:register {{{1
  828. function! s:register(line, index, type, cmd, path)
  829. let b:startify.entries[a:line] = {
  830. \ 'index': a:index,
  831. \ 'type': a:type,
  832. \ 'line': a:line,
  833. \ 'cmd': a:cmd,
  834. \ 'path': a:path,
  835. \ 'marked': 0,
  836. \ }
  837. endfunction
  838. " Function: s:create_last_session_link {{{1
  839. function! s:create_last_session_link(session_path)
  840. if !has('win32') && a:session_path !~# '__LAST__$'
  841. let cmd = printf('ln -sf %s %s',
  842. \ shellescape(fnamemodify(a:session_path, ':t')),
  843. \ shellescape(s:session_dir .'/__LAST__'))
  844. call system(cmd)
  845. if v:shell_error
  846. call s:warn("Can't create 'last used session' symlink.")
  847. endif
  848. endif
  849. endfunction
  850. " Function: s:init_env {{{1
  851. function! s:init_env()
  852. let s:env = []
  853. let ignore = {
  854. \ 'HOME': 1,
  855. \ 'OLDPWD': 1,
  856. \ 'PWD': 1,
  857. \ }
  858. function! s:get_env()
  859. redir => s
  860. silent! execute "norm!:ec$\<c-a>'\<c-b>\<right>\<right>\<del>'\<cr>"
  861. redir END
  862. redraw
  863. return split(s)
  864. endfunction
  865. function! s:compare_by_key_len(foo, bar)
  866. return len(a:foo[0]) - len(a:bar[0])
  867. endfunction
  868. function! s:compare_by_val_len(foo, bar)
  869. return len(a:bar[1]) - len(a:foo[1])
  870. endfunction
  871. for k in s:get_env()
  872. silent! execute "let v = eval('$'.k)"
  873. if has('win32') ? (v[1] != ':') : (v[0] != '/')
  874. \ || has_key(ignore, k)
  875. \ || len(k) > len(v)
  876. continue
  877. endif
  878. call insert(s:env, [k,v], 0)
  879. endfor
  880. let s:env = sort(s:env, 's:compare_by_key_len')
  881. let s:env = sort(s:env, 's:compare_by_val_len')
  882. endfunction
  883. " Function: s:transform {{{1
  884. function s:transform(absolute_path)
  885. for [k,V] in g:startify_transformations
  886. if a:absolute_path =~ k
  887. return type(V) == type('') ? V : V(a:absolute_path)
  888. endif
  889. unlet V
  890. endfor
  891. return ''
  892. endfunction
  893. " Function: s:warn {{{1
  894. function! s:warn(msg) abort
  895. echohl WarningMsg
  896. echomsg 'startify: '. a:msg
  897. echohl NONE
  898. endfunction
  899. " Function: s:get_session_path {{{1
  900. function! s:get_session_path() abort
  901. if exists('g:startify_session_dir')
  902. let path = g:startify_session_dir
  903. elseif has('nvim')
  904. let path = has('nvim-0.3.1')
  905. \ ? stdpath('data').'/session'
  906. \ : has('win32')
  907. \ ? '~/AppData/Local/nvim-data/session'
  908. \ : '~/.local/share/nvim/session'
  909. else " Vim
  910. let path = has('win32')
  911. \ ? '~/vimfiles/session'
  912. \ : '~/.vim/session'
  913. endif
  914. return resolve(expand(path))
  915. endfunction
  916. " Init: values {{{1
  917. let s:sep = startify#get_separator()
  918. let s:numfiles = get(g:, 'startify_files_number', 10)
  919. let s:show_special = get(g:, 'startify_enable_special', 1)
  920. let s:relative_path = get(g:, 'startify_relative_path') ? ':~:.' : ':p:~'
  921. let s:tf = exists('g:startify_transformations')
  922. let s:session_dir = s:get_session_path()
  923. let s:skiplist = get(g:, 'startify_skiplist', [
  924. \ 'COMMIT_EDITMSG',
  925. \ 'runtime/doc/.*\.txt',
  926. \ 'bundle/.*/doc/.*\.txt',
  927. \ 'plugged/.*/doc/.*\.txt',
  928. \ escape(fnamemodify(resolve($VIMRUNTIME), ':p'), '\') .'doc/.*\.txt',
  929. \ ])
  930. let s:padding_left = repeat(' ', get(g:, 'startify_padding_left', 3))
  931. let s:fixed_column = len(s:padding_left) + 2