optfrag.lua 1.03 KB
Newer Older
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
1 2 3 4 5 6 7 8 9
--[[
Option fragment module for Alpine Wall
Copyright (C) 2012 Kaarle Ritvanen
Licensed under the terms of GPL2
]]--


module(..., package.seeall)

10 11 12 13 14 15 16 17
function combinations(of1, ...)
   if #arg == 0 then return of1 end

   if not of1 then return combinations(unpack(arg)) end

   local of2 = arg[1]
   table.remove(arg, 1)
   if not of2 then return combinations(of1, unpack(arg)) end
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48

   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
	    if k ~= 'opts' then of[k] = v end
	 end

	 local match = true
	 for k, v in pairs(y) do
	    if k ~= 'opts' then
	       if of[k] and v ~= of[k] then
		  match = false
		  break
	       end
	       of[k] = v
	    end
	 end

	 if match then
	    if x.opts then
	       if y.opts then of.opts = x.opts..' '..y.opts
	       else of.opts = x.opts end
	    else of.opts = y.opts end
	    table.insert(res, of)
	 end
      end
   end

49
   return combinations(res, unpack(arg))
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
50
end
51 52

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