vis-config

lua scripts to configure vis editor

git clone https://9o.is/git/vis-config.git

utils.lua

(1443B)


      1 function runq(cmd)
      2 	return os.execute(cmd .. ' >/dev/null 2>&1')
      3 end
      4 
      5 function start_lspc(lspc, name)
      6 	if not (lspc.enabled and runq('type '..name)) then
      7 		return false
      8 	end
      9 
     10 	if not lspc.autostart and not lspc.running[name] then
     11 		vis:command('lspc-start-server')
     12 		return true
     13 	end
     14 	
     15 	if lspc.running[name] then
     16 		return true
     17 	end
     18 end
     19 
     20 function map(modes, ...)
     21 	if type(modes) ~= 'table' then
     22 		vis:map(modes, ...)
     23 		return
     24 	end
     25 	for _, mode in pairs(modes) do
     26 		vis:map(mode, ...)
     27 	end
     28 end
     29 
     30 function mapwin(win, modes, ...)
     31 	if type(modes) ~= 'table' then
     32 		win:map(modes, ...)
     33 		return
     34 	end
     35 	for _, mode in pairs(modes) do
     36 		win:map(mode, ...)
     37 	end
     38 end
     39 
     40 function ins(x)
     41 	return function()
     42 		local pos = vis.win.selection.pos
     43 		vis:feedkeys(x)
     44 		vis.win.selection.pos = pos
     45 		return 1
     46 	end
     47 end
     48 
     49 function cmd(x)
     50 	return function()
     51 		local fn = vis.win.file.name
     52 		if fn then
     53 			x = string.format(x, fn)
     54 		end
     55 		vis:command(x)
     56 	end
     57 end
     58 
     59 function content(win)
     60 	local sel = win.selection
     61 	local range = sel.anchored and sel.range or win.file:text_object_word(sel.pos)
     62 	return win.file:content(range)
     63 end
     64 
     65 vis.events.subscribe(vis.events.WIN_OPEN, function(win)
     66 	if settings == nil then return end
     67 	local window_settings = settings[win.syntax]
     68 
     69 	if type(window_settings) == "table" then
     70 		for _, setting in pairs(window_settings) do
     71 			vis:command(setting)
     72 		end
     73 	elseif type(window_settings) == "function" then
     74 		window_settings(win)
     75 	end
     76 end)