vis-config

lua scripts to configure vis editor

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

router.lua

(973B)


      1 local stack = {}
      2 local index = 0
      3 local allow = true
      4 
      5 local function push(item)
      6 	if item == stack[index] then
      7 		return
      8 	end
      9 
     10     for i = #stack, index + 1, -1 do
     11         stack[i] = nil
     12     end
     13 
     14     table.insert(stack, item)
     15     index = #stack
     16 end
     17 
     18 local function open(path)
     19 	vis:command(string.format('e %s', path))
     20 end
     21 
     22 local M = {}
     23 
     24 M.back = function()
     25     if index <= 1 then
     26     	return
     27     end
     28     index = index - 1
     29     open(stack[index])
     30 end
     31 
     32 M.forward = function()
     33     if index >= #stack then
     34     	return
     35     end
     36     index = index + 1
     37 	open(stack[index])
     38 end
     39 
     40 M.navigate = function(path, cmd)
     41 	if not path then
     42 		return
     43 	end
     44 
     45 	allow = true
     46 	open(path)
     47 
     48 	if cmd then
     49 		vis:command(cmd)
     50 	end
     51 end
     52 
     53 vis.events.subscribe(vis.events.WIN_OPEN, function(win)
     54 	if allow and win.file.name then
     55 		push(win.file.name)
     56 		allow = false
     57 	end
     58 end)
     59 
     60 vis:action_register('vis-router-back', M.back, "Go back")
     61 vis:action_register('vis-router-forward', M.forward, "Go Forward")
     62 
     63 return M