startify.vim 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075
  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. let dname = sfiles[i] ==# v:this_session ? fname.' (*)' : fname
  587. call append('$', s:padding_left .'['. index .']'. repeat(' ', (3 - strlen(index))) . dname)
  588. if has('win32')
  589. let fname = substitute(fname, '\[', '\[[]', 'g')
  590. endif
  591. call s:register(line('$'), index, 'session', 'SLoad', fname)
  592. if i == limit
  593. break
  594. endif
  595. endfor
  596. call append('$', '')
  597. endfunction
  598. " Function: s:show_bookmarks {{{1
  599. function! s:show_bookmarks() abort
  600. if !exists('g:startify_bookmarks') || empty(g:startify_bookmarks)
  601. return
  602. endif
  603. if exists('s:last_message')
  604. call s:print_section_header()
  605. endif
  606. for bookmark in g:startify_bookmarks
  607. if type(bookmark) == type({})
  608. let [index, path] = items(bookmark)[0]
  609. else " string
  610. let [index, path] = [s:get_index_as_string(), bookmark]
  611. endif
  612. let entry_path = ''
  613. if s:tf
  614. let entry_path = s:transform(fnamemodify(resolve(expand(path)), ':p'))
  615. endif
  616. if empty(entry_path)
  617. let entry_path = path
  618. endif
  619. call append('$', s:padding_left .'['. index .']'. repeat(' ', (3 - strlen(index))) . entry_path)
  620. if has('win32')
  621. let path = substitute(path, '\[', '\[[]', 'g')
  622. endif
  623. call s:register(line('$'), index, 'file', 'edit', fnameescape(expand(path)))
  624. unlet bookmark " avoid type mismatch for heterogeneous lists
  625. endfor
  626. call append('$', '')
  627. endfunction
  628. " Function: s:show_commands {{{1
  629. function! s:show_commands() abort
  630. if !exists('g:startify_commands') || empty(g:startify_commands)
  631. return
  632. endif
  633. if exists('s:last_message')
  634. call s:print_section_header()
  635. endif
  636. for entry in g:startify_commands
  637. if type(entry) == type({}) " with custom index
  638. let [index, command] = items(entry)[0]
  639. else
  640. let command = entry
  641. let index = s:get_index_as_string()
  642. endif
  643. " If no list is given, the description is the command itself.
  644. let [desc, cmd] = type(command) == type([]) ? command : [command, command]
  645. call append('$', s:padding_left .'['. index .']'. repeat(' ', (3 - strlen(index))) . desc)
  646. call s:register(line('$'), index, 'special', cmd, '')
  647. unlet entry command
  648. endfor
  649. call append('$', '')
  650. endfunction
  651. " Function: s:is_in_skiplist {{{1
  652. function! s:is_in_skiplist(arg) abort
  653. for regexp in s:skiplist
  654. try
  655. if a:arg =~# regexp
  656. return 1
  657. endif
  658. catch
  659. call s:warn('Pattern '. string(regexp) .' threw an exception. Read :help g:startify_skiplist')
  660. endtry
  661. endfor
  662. endfunction
  663. " Function: s:set_cursor {{{1
  664. function! s:set_cursor() abort
  665. let b:startify.oldline = exists('b:startify.newline') ? b:startify.newline : 2 + len(s:padding_left)
  666. let b:startify.newline = line('.')
  667. " going up (-1) or down (1)
  668. if b:startify.oldline == b:startify.newline && col('.') != s:fixed_column
  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. endif
  674. " skip section headers lines until an entry is found
  675. while index(b:startify.section_header_lines, b:startify.newline) != -1
  676. let b:startify.newline += movement
  677. endwhile
  678. " skip blank lines between lists
  679. if empty(getline(b:startify.newline))
  680. let b:startify.newline += movement
  681. endif
  682. " don't go beyond first or last entry
  683. let b:startify.newline = max([b:startify.firstline, min([b:startify.lastline, b:startify.newline])])
  684. call cursor(b:startify.newline, s:fixed_column)
  685. endfunction
  686. " Function: s:set_mappings {{{1
  687. function! s:set_mappings() abort
  688. nnoremap <buffer><nowait><silent> i :enew <bar> startinsert<cr>
  689. nnoremap <buffer><nowait><silent> <insert> :enew <bar> startinsert<cr>
  690. nnoremap <buffer><nowait><silent> b :call <sid>set_mark('B')<cr>
  691. nnoremap <buffer><nowait><silent> s :call <sid>set_mark('S')<cr>
  692. nnoremap <buffer><nowait><silent> t :call <sid>set_mark('T')<cr>
  693. nnoremap <buffer><nowait><silent> v :call <sid>set_mark('V')<cr>
  694. nnoremap <buffer><nowait><silent> <cr> :call startify#open_buffers()<cr>
  695. nnoremap <buffer><nowait><silent> <2-LeftMouse> :call startify#open_buffers()<cr>
  696. nnoremap <buffer><nowait><silent> <MiddleMouse> :enew <bar> execute 'normal! "'.(v:register=='"'?'*':v:register).'gp'<cr>
  697. " Without these mappings n/N wouldn't work properly, since autocmds always
  698. " force the cursor back on the index.
  699. nnoremap <buffer><expr> n ' j'[v:searchforward].'n'
  700. nnoremap <buffer><expr> N 'j '[v:searchforward].'N'
  701. function! s:compare_by_index(foo, bar)
  702. return a:foo.index - a:bar.index
  703. endfunction
  704. for entry in sort(values(b:startify.entries), 's:compare_by_index')
  705. execute 'nnoremap <buffer><silent><nowait>' entry.index
  706. \ ':call startify#open_buffers('. string(entry.line) .')<cr>'
  707. endfor
  708. endfunction
  709. " Function: s:set_mark {{{1
  710. function! s:set_mark(type, ...) abort
  711. let index = expand('<cword>')
  712. let line = exists('a:1') ? a:1 : line('.')
  713. let entry = b:startify.entries[line]
  714. if entry.type != 'file'
  715. return
  716. endif
  717. let default_cmds = {
  718. \ 'B': 'edit',
  719. \ 'S': 'split',
  720. \ 'V': 'vsplit',
  721. \ 'T': 'tabnew',
  722. \ }
  723. setlocal modifiable
  724. if entry.marked && index[0] == a:type
  725. let entry.cmd = 'edit'
  726. let entry.marked = 0
  727. execute 'normal! ci]'. entry.index
  728. else
  729. let entry.cmd = default_cmds[a:type]
  730. let entry.marked = 1
  731. let entry.tick = b:startify.tick
  732. let b:startify.tick += 1
  733. execute 'normal! ci]'. repeat(a:type, len(index))
  734. endif
  735. " Reset cursor to fixed column, which is important for s:set_cursor().
  736. call cursor(line('.'), s:fixed_column)
  737. setlocal nomodifiable nomodified
  738. endfunction
  739. " Function: s:sort_by_tick {{{1
  740. function! s:sort_by_tick(one, two)
  741. return a:one.tick - a:two.tick
  742. endfunction
  743. " Function: s:check_user_options {{{1
  744. function! s:check_user_options(path) abort
  745. let session = a:path . s:sep .'Session.vim'
  746. if get(g:, 'startify_session_autoload') && filereadable(glob(session))
  747. execute 'silent bwipeout' a:path
  748. call startify#session_delete_buffers()
  749. execute 'source' session
  750. return
  751. endif
  752. if get(g:, 'startify_change_to_vcs_root') && s:cd_to_vcs_root(a:path)
  753. return
  754. endif
  755. if get(g:, 'startify_change_to_dir', 1)
  756. if isdirectory(a:path)
  757. execute 'lcd' a:path
  758. else
  759. let dir = fnamemodify(a:path, ':h')
  760. if isdirectory(dir)
  761. execute 'lcd' dir
  762. else
  763. " Do nothing. E.g. a:path == `scp://foo/bar`
  764. endif
  765. endif
  766. endif
  767. endfunction
  768. " Function: s:cd_to_vcs_root {{{1
  769. function! s:cd_to_vcs_root(path) abort
  770. let dir = fnamemodify(a:path, ':p:h')
  771. for vcs in [ '.git', '.hg', '.bzr', '.svn' ]
  772. let root = finddir(vcs, dir .';')
  773. if !empty(root)
  774. execute 'cd '. fnameescape(fnamemodify(root, ':h'))
  775. return 1
  776. endif
  777. endfor
  778. return 0
  779. endfunction
  780. " Function: s:close {{{1
  781. function! s:close() abort
  782. if len(filter(range(0, bufnr('$')), 'buflisted(v:val)')) - &buflisted
  783. if bufloaded(bufnr('#')) && bufnr('#') != bufnr('%')
  784. buffer #
  785. else
  786. bnext
  787. endif
  788. else
  789. quit
  790. endif
  791. endfunction
  792. " Function: s:get_index_as_string {{{1
  793. function! s:get_index_as_string() abort
  794. if !empty(b:startify.indices)
  795. return remove(b:startify.indices, 0)
  796. elseif exists('g:startify_custom_indices')
  797. let listlen = len(g:startify_custom_indices)
  798. if b:startify.entry_number < listlen
  799. let idx = g:startify_custom_indices[b:startify.entry_number]
  800. else
  801. let idx = string(b:startify.entry_number - listlen)
  802. endif
  803. else
  804. let idx = string(b:startify.entry_number)
  805. endif
  806. let b:startify.entry_number += 1
  807. return idx
  808. endfunction
  809. " Function: s:print_section_header {{{1
  810. function! s:print_section_header() abort
  811. $
  812. let curline = line('.')
  813. for lnum in range(curline, curline + len(s:last_message) + 1)
  814. call add(b:startify.section_header_lines, lnum)
  815. endfor
  816. call append('$', s:last_message + [''])
  817. unlet s:last_message
  818. endfunction
  819. " Function: s:register {{{1
  820. function! s:register(line, index, type, cmd, path)
  821. let b:startify.entries[a:line] = {
  822. \ 'index': a:index,
  823. \ 'type': a:type,
  824. \ 'line': a:line,
  825. \ 'cmd': a:cmd,
  826. \ 'path': a:path,
  827. \ 'marked': 0,
  828. \ }
  829. endfunction
  830. " Function: s:create_last_session_link {{{1
  831. function! s:create_last_session_link(spath)
  832. if !has('win32') && a:spath !~# '__LAST__$'
  833. let cmd = printf('ln -sf %s %s',
  834. \ shellescape(fnamemodify(a:spath, ':t')),
  835. \ shellescape(s:session_dir .'/__LAST__'))
  836. call system(cmd)
  837. if v:shell_error
  838. call s:warn("Can't create 'last used session' symlink.")
  839. endif
  840. endif
  841. endfunction
  842. " Function: s:init_env {{{1
  843. function! s:init_env()
  844. let s:env = []
  845. let ignore = {
  846. \ 'HOME': 1,
  847. \ 'OLDPWD': 1,
  848. \ 'PWD': 1,
  849. \ }
  850. function! s:get_env()
  851. redir => s
  852. silent! execute "norm!:ec$\<c-a>'\<c-b>\<right>\<right>\<del>'\<cr>"
  853. redir END
  854. redraw
  855. return split(s)
  856. endfunction
  857. function! s:compare_by_key_len(foo, bar)
  858. return len(a:foo[0]) - len(a:bar[0])
  859. endfunction
  860. function! s:compare_by_val_len(foo, bar)
  861. return len(a:bar[1]) - len(a:foo[1])
  862. endfunction
  863. for k in s:get_env()
  864. silent! execute "let v = eval('$'.k)"
  865. if has('win32') ? (v[1] != ':') : (v[0] != '/')
  866. \ || has_key(ignore, k)
  867. \ || len(k) > len(v)
  868. continue
  869. endif
  870. call insert(s:env, [k,v], 0)
  871. endfor
  872. let s:env = sort(s:env, 's:compare_by_key_len')
  873. let s:env = sort(s:env, 's:compare_by_val_len')
  874. endfunction
  875. " Function: s:transform {{{1
  876. function s:transform(absolute_path)
  877. for [k,V] in g:startify_transformations
  878. if a:absolute_path =~ k
  879. return type(V) == type('') ? V : V(a:absolute_path)
  880. endif
  881. unlet V
  882. endfor
  883. return ''
  884. endfunction
  885. " Function: s:warn {{{1
  886. function! s:warn(msg) abort
  887. echohl WarningMsg
  888. echomsg 'startify: '. a:msg
  889. echohl NONE
  890. endfunction
  891. " Function: s:get_session_path {{{1
  892. function! s:get_session_path() abort
  893. if exists('g:startify_session_dir')
  894. let path = g:startify_session_dir
  895. elseif has('nvim')
  896. let path = has('nvim-0.3.1')
  897. \ ? stdpath('data').'/session'
  898. \ : has('win32')
  899. \ ? '~/AppData/Local/nvim-data/session'
  900. \ : '~/.local/share/nvim/session'
  901. else " Vim
  902. let path = has('win32')
  903. \ ? '~/vimfiles/session'
  904. \ : '~/.vim/session'
  905. endif
  906. return resolve(expand(path))
  907. endfunction
  908. " Init: values {{{1
  909. let s:sep = startify#get_separator()
  910. let s:numfiles = get(g:, 'startify_files_number', 10)
  911. let s:show_special = get(g:, 'startify_enable_special', 1)
  912. let s:relative_path = get(g:, 'startify_relative_path') ? ':~:.' : ':p:~'
  913. let s:tf = exists('g:startify_transformations')
  914. let s:session_dir = s:get_session_path()
  915. let s:skiplist = get(g:, 'startify_skiplist', [
  916. \ 'COMMIT_EDITMSG',
  917. \ 'runtime/doc/.*\.txt',
  918. \ 'bundle/.*/doc/.*\.txt',
  919. \ 'plugged/.*/doc/.*\.txt',
  920. \ escape(fnamemodify(resolve($VIMRUNTIME), ':p'), '\') .'doc/.*\.txt',
  921. \ ])
  922. let s:padding_left = repeat(' ', get(g:, 'startify_padding_left', 3))
  923. let s:fixed_column = len(s:padding_left) + 2