optfrag.lua 1.2 KB
Newer Older
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
1 2
--[[
Option fragment module for Alpine Wall
3
Copyright (C) 2012-2016 Kaarle Ritvanen
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
4
See LICENSE file for license details
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
5 6 7
]]--


8
local M = {}
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
9

10
function M.combinations(of1, ...)
11 12
   local arg = {...}

13 14
   if #arg == 0 then return of1 end

15
   if not of1 then return M.combinations(...) end
16 17 18

   local of2 = arg[1]
   table.remove(arg, 1)
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
19
   if not of2 then return M.combinations(of1, table.unpack(arg)) end
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
20 21 22 23 24 25 26

   local res = {}
   for i, x in ipairs(of1) do
      for i, y in ipairs(of2) do

	 local of = {}
	 for k, v in pairs(x) do
27
	    if k ~= 'match' then of[k] = v end
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
28 29 30 31
	 end

	 local match = true
	 for k, v in pairs(y) do
32
	    if k ~= 'match' then
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
33 34 35 36 37 38 39 40 41
	       if of[k] and v ~= of[k] then
		  match = false
		  break
	       end
	       of[k] = v
	    end
	 end

	 if match then
42 43 44 45
	    if x.match then
	       if y.match then of.match = x.match..' '..y.match
	       else of.match = x.match end
	    else of.match = y.match end
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
46 47 48 49 50
	    table.insert(res, of)
	 end
      end
   end

Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
51
   return M.combinations(res, table.unpack(arg))
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
52
end
53

54 55
function M.location(of) return of.family..'/'..of.table..'/'..of.chain end

56
function M.command(of)
57
   return (of.match and of.match..' ' or '')..
58 59 60
      (of.target and '-j '..of.target or '')
end

61
return M