vis-config

lua scripts to configure vis editor

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

session.lua

(2217B)


      1 local M = {
      2 	mru_max = 20,
      3 	mru_path = '/tmp/vis-mru',
      4 	pos_path = '/tmp/vis-pos',
      5 }
      6 
      7 -- local function create_session_dir()
      8 	-- local cmd = 'echo "${TMPDIR-/tmp}/vis-$(id -u)/$(pwd | tr \'/\' \'~\')" | xargs -I{} sh -c \'mkdir -p "{}" && echo "{}"\''
      9 	-- local full_cmd = "/bin/sh -c '" .. cmd:gsub("'", "'\\''") .. "'"
     10 	-- local fd = io.popen(full_cmd)
     11 	-- local dir = fd:read()
     12 	-- local success, msg, status = fd:close()
     13 	-- if not success then
     14 		-- print(msg)
     15 		-- os.exit(status)
     16 	-- end
     17 	-- return dir
     18 -- end
     19 
     20 -- M.dir = create_session_dir()
     21 -- M.mru_path = M.dir .. '/mru'
     22 -- M.pos_path = M.dir .. '/pos'
     23 
     24 local function read(path)
     25     local result = {}
     26     local f = io.open(path)
     27     if not f then return result end
     28     for line in f:lines() do
     29         table.insert(result, line)
     30     end
     31     f:close()
     32     return result
     33 end
     34 
     35 local function mru_write(win)
     36     local path = win.file.path
     37     if not path then return end
     38     local mru = read(M.mru_path)
     39 
     40     if path == mru[1] then return end
     41 
     42     local f = io.open(M.mru_path, 'w+')
     43     if f == nil then return end
     44 
     45 	for i,v in ipairs(mru) do
     46 		if v == path then
     47 			table.remove(mru, i)
     48 		end
     49 	end
     50 
     51     table.insert(mru, 1, path)
     52 
     53 	for _,v in pairs(mru) do
     54         f:write(string.format('%s\n', v))
     55 	end
     56 
     57     f:close()
     58 end
     59 
     60 local function pos_setup(win)
     61     local path = win.file.path
     62     if not path then return end
     63 	local poss = read(M.pos_path)
     64 
     65     for _,v in pairs(poss) do
     66         local pos, pospath = v:match('^(%d+) (.+)$')
     67     	if path == pospath then
     68     		win.selection.pos = tonumber(pos)
     69     		break
     70     	end
     71     end
     72 end
     73 
     74 local function pos_write(win)
     75     local path = win.file.path
     76     if not path then return end
     77 	local poss = read(M.pos_path)
     78 
     79     local f = io.open(M.pos_path, 'w+')
     80     if not f then return end
     81 
     82     for i,v in ipairs(poss) do
     83         local pos, pospath = v:match('^(%d+) (.+)$')
     84         if path ~= pospath then
     85 	        f:write(string.format('%s\n', v))
     86     	end
     87     end
     88 
     89     f:write(string.format('%d %s\n', win.selection.pos, path))
     90     f:close()
     91 end
     92 
     93 vis.events.subscribe(vis.events.WIN_OPEN, mru_write)
     94 vis.events.subscribe(vis.events.WIN_OPEN, pos_setup)
     95 vis.events.subscribe(vis.events.WIN_CLOSE, pos_write)
     96 
     97 return M