dotfiles

configuration files for shell, text editor, graphical environment, etc.
git clone git://src.adamsgaard.dk/dotfiles
Log | Files | Refs | README | LICENSE

cursors.lua (1405B)


      1 local module = {}
      2 local cursors = {}
      3 module.path = os.getenv('HOME') .. '/.cursors'
      4 
      5 function apply_cursor_pos(win)
      6 	if win.file == nil or win.file.path == nil then return end
      7 	local pos = cursors[win.file.path]
      8 	if pos == nil then return end
      9 	win.selection.pos = tonumber(pos)
     10 	vis:feedkeys("zz")
     11 end
     12 
     13 function file_exists(path)
     14 	local f = io.open(path)
     15 	if f == nil then return false
     16 	else f:close() return true 
     17 	end
     18 end	
     19 
     20 function read_cursors()
     21 	cursors = {}
     22 	local f = io.open(module.path)
     23 	if f == nil then return end
     24 	for line in f:lines() do
     25 		for k, v in string.gmatch(line, '(.+)%s(%d+)') do
     26 			cursors[k] = v
     27 		end 
     28 	end
     29 	f:close()
     30 	for win in vis:windows() do
     31 		apply_cursor_pos(win)
     32 	end
     33 end
     34 
     35 function write_cursors()
     36 	local f = io.open(module.path, 'w+')
     37 	if f == nil then return end
     38 	local a = {}
     39 	for k in pairs(cursors) do table.insert(a, k) end
     40 	table.sort(a)
     41 	for i,k in ipairs(a) do 
     42 		f:write(string.format('%s %d\n', k, cursors[k]))
     43 	end
     44 	f:close()
     45 end
     46 
     47 function set_cursor_pos(win)
     48 	if win.file == nil or win.file.path == nil then return end
     49 	if not file_exists(win.file.path) then return end
     50 	cursors[win.file.path] = win.selection.pos
     51 end
     52 
     53 vis.events.subscribe(vis.events.INIT, read_cursors)
     54 vis.events.subscribe(vis.events.WIN_OPEN, apply_cursor_pos)
     55 vis.events.subscribe(vis.events.WIN_CLOSE, set_cursor_pos)
     56 vis.events.subscribe(vis.events.QUIT, write_cursors)
     57 
     58 return module