startify.vim 32 KB

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