dotfiles

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

fzf-mru.lua (2133B)


      1 local module = {}
      2 module.fzfmru_filepath = os.getenv('HOME') .. '/.mru'
      3 module.fzfmru_path = "fzf"
      4 module.fzfmru_args = ""
      5 module.fzfmru_history = 20
      6 
      7 function read_mru()
      8     local mru = {}
      9     local f = io.open(module.fzfmru_filepath)
     10     if f == nil then return end
     11     for line in f:lines() do
     12         table.insert(mru, line)
     13     end
     14     f:close()
     15 
     16     return mru
     17 end
     18 
     19 function write_mru(win)
     20     local file_path = win.file.path
     21     local mru = read_mru()
     22 
     23     -- check if mru data exists
     24     if mru == nil then mru = {} end
     25     -- check if we opened any file
     26     if file_path == nil then return end
     27     -- check duplicate
     28     if file_path == mru[1] then return end
     29 
     30     local f = io.open(module.fzfmru_filepath, 'w+')
     31     if f == nil then return end
     32 
     33     table.insert(mru, 1, file_path)
     34 
     35     for i,k in ipairs(mru) do
     36         if i > module.fzfmru_history then break end
     37         if i == 1 or k ~= file_path then
     38             f:write(string.format('%s\n', k))
     39         end
     40     end
     41 
     42     f:close()
     43 end
     44 
     45 vis.events.subscribe(vis.events.WIN_OPEN, write_mru)
     46 
     47 vis:command_register("fzfmru", function(argv, force, win, selection, range)
     48     local command = "cat " .. module.fzfmru_filepath .. " | " .. module.fzfmru_path .. " " .. module.fzfmru_args .. " " .. table.concat(argv, " ")
     49 
     50     local file = io.popen(command)
     51     local output = file:read()
     52     local success, msg, status = file:close()
     53 
     54     if status == 0 then
     55         vis:feedkeys(string.format(":e '%s'<Enter>", output))
     56     elseif status == 1 then
     57         vis:info(string.format("fzf-open: No match. Command %s exited with return value %i." , command, status))
     58     elseif status == 2 then
     59         vis:info(string.format("fzf-open: Error. Command %s exited with return value %i." , command, status))
     60     elseif status == 130 then
     61         vis:info(string.format("fzf-open: Interrupted. Command %s exited with return value %i" , command, status))
     62     else
     63         vis:info(string.format("fzf-open: Unknown exit status %i. command %s exited with return value %i" , status, command, status, status))
     64     end
     65 
     66     vis:feedkeys("<vis-redraw>")
     67 
     68     return true;
     69 end)
     70 
     71 return module