startify.vim 33 KB

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