vis-config

lua scripts to configure vis editor

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

x11.lua

(2702B)


      1 local M = {
      2 	enabled = true,
      3 	fd = {},
      4 	status = {
      5 		left = '',
      6 		right = '',
      7 	},
      8 }
      9 
     10 local function write(cmd)
     11 	if not M.running() then
     12 		vis:info('x11 client is not running')
     13 		return false
     14 	end
     15 	M.fd.status:write(cmd .. '\n')
     16 	M.fd.status:flush()
     17 	return true
     18 end
     19 
     20 local function open_status()
     21 	local cmd = os.execute('qubesdb-read-bool /qubes-service/guivm') and
     22 		'exec xprop-sink 0x%x _MY_WIN_STATUS' or
     23 		'exec qrexec-client-vm @default user.WindowStatus+%d'
     24 	return vis:communicate('x11-status', string.format(cmd, M.id_xembed or M.id))
     25 end
     26 
     27 local function open_events()
     28 	local cmd = 'exec xev -id %d -event focus'
     29 	return vis:communicate('x11-events', string.format(cmd, M.id))
     30 end
     31 
     32 M.running = function()
     33 	return io.type(M.fd.status) == 'file'
     34 end
     35 
     36 M.init = function()
     37 	M.id_xembed = os.getenv('XEMBED')
     38 	M.id = os.getenv('WINDOWID')
     39 
     40 	if not M.id then
     41 		return
     42 	end
     43 
     44 	M.fd.status = open_status()
     45 	M.fd.events = open_events()
     46 
     47 	vis.events.subscribe(vis.events.PROCESS_RESPONSE, function(name, event, code, msg)
     48 		if name ~= 'x11-status' and name ~= 'x11-events' then
     49 			return
     50 		end
     51 
     52 		if name == 'x11-events' and event == 'STDOUT' then
     53 			if msg:match('FocusIn') then
     54 				M.set_title(M.status.left)
     55 				write(M.status.right)
     56 			end
     57 		end
     58 
     59 		if event == 'EXIT' then
     60 			vis:info(string.format('%s client exited: %s', name, code))
     61 		elseif event == 'SIGNAL' then
     62 			vis:info(string.format('%s client exited: %s', name, code))
     63 		end
     64 	end)
     65 
     66 end
     67 
     68 local function spawn_terminal(cmd)
     69 	return os.execute(string.format('terminal %s >/dev/null 2>&1 &', cmd))
     70 end
     71 
     72 M.navigate = function(path, cmd)
     73 	cmd = cmd and "+'"..cmd.."'" or ''
     74 	return spawn_terminal(string.format('vis %s %s', cmd, path))
     75 end
     76 
     77 M.tabnew = function(argv)
     78 	return spawn_terminal(string.format('vis %s', table.concat(argv, ' ')))
     79 end
     80 
     81 M.terminal = function(argv)
     82 	local cmd = os.getenv('SHELL') or 'sh'
     83 	if #argv ~= 0 then cmd = table.concat(argv, ' '):gsub('"', '\\"') end
     84 	return spawn_terminal(cmd)
     85 end
     86 
     87 M.set_title = function(title)
     88 	local f = io.open("/dev/tty", "w")
     89     if f then
     90         f:write("\27]2;" .. title .. "\7")
     91         f:close()
     92     end
     93 end
     94 
     95 M.set_status = function(left, right)
     96 	if left and left ~= M.status.left then
     97 		M.status.left = left
     98 		M.set_title(left)
     99 	end
    100 
    101 	if right and right ~= M.status.right then
    102 		M.status.right = right
    103 		write(right)
    104 	end
    105 end
    106 
    107 vis.events.subscribe(vis.events.INIT, function()
    108 	if M.enabled then
    109 		M.init()
    110 	end
    111 end)
    112 
    113 vis.events.subscribe(vis.events.WIN_OPEN, function()
    114 	if M.running() then
    115 		vis:command_register('tabnew',   M.tabnew,   'Run vis command in a new tab page')
    116 		vis:command_register('terminal', M.terminal, 'Run shell command in a new tab page')
    117 	end
    118 end)
    119 
    120 return M