List principals who are witnesses.fh_lua

--[[
@Title: List principals who are witnesses
@Type: Standard
@Author: Jane Taubman
@Version: 1.1
@Keywords: 
@LastUpdated: 19 Jan 2021 
@Licence: This plugin is copyright (c) 2021 Jane Taubman and contributors, and is licensed under the MIT License
which is hereby incorporated by reference (see https://pluginstore.family-historian.co.uk/fh-plugin-licence)
@Description: 
Creates a Result set listing all witness links for the principals of a fact.  This can be used by highlighting some or all of the Witness column and pressing delete to remove witnesses to their own events.
This is useful as a witness sentence is used in preference to a fact sentence, but imported data may include these links, but the sentence is not required in reports.
]]
fhInitialise(7); -- implies users will need version 7 as a minimum; change if that is not the case

function main()
    local tblResults = createResultTable()
    -- Define Columns
    tblResults.record = {title='Record'}
    tblResults.fact = {title='Fact'}
    tblResults.witness = {title='Witness'}
    
    local ptrWitness = fhNewItemPtr()
    local ptrWitnessRec = fhNewItemPtr()
    local ptrHusb = fhNewItemPtr()
    local ptrWife = fhNewItemPtr()
    for pi in records('INDI') do
        for px in facts(pi) do
            ptrWitness:MoveTo(px,'~._SHAR')
            while ptrWitness:IsNotNull() do
                ptrWitnessRec:MoveTo(ptrWitness,'~>')
                if ptrWitnessRec:IsSame(pi) then
                    print(fhGetDisplayText(pi)..'- '.. fhGetDisplayText(ptrWitness))
                    
                    tblResults:newRow()
                    -- Set Columns
                    tblResults.record:set(pi:Clone())
                    tblResults.fact:set(px:Clone())
                    tblResults.witness:set(ptrWitness:Clone())
                end
                ptrWitness:MoveNext("SAME_TAG")
            end
        end
    end
    for pi in records('FAM') do
        ptrHusb:MoveTo(pi,'~.HUSB>')
        ptrWife:MoveTo(pi,'~.WIFE>')
        for px in facts(pi) do
            ptrWitness:MoveTo(px,'~._SHAR')
            while ptrWitness:IsNotNull() do
                ptrWitnessRec:MoveTo(ptrWitness,'~>')
                if ptrWitnessRec:IsSame(ptrHusb) then
                    tblResults:newRow()
                    -- Set Columns
                    tblResults.record:set(pi:Clone())
                    tblResults.fact:set(px:Clone())
                    tblResults.witness:set(ptrWitness:Clone())
                end
                if ptrWitnessRec:IsSame(ptrWife) then
                    tblResults:newRow()
                    -- Set Columns
                    tblResults.record:set(pi:Clone())
                    tblResults.fact:set(px:Clone())
                    tblResults.witness:set(ptrWitness:Clone())
                end
                ptrWitness:MoveNext("SAME_TAG")
            end
        end
    end

    if tblResults:rowCount() == 0 then
	     fhMessageBox('None Found')
    else
        fhOutputResultSetTitles("Facts with Witnesses who are principals", "Facts with Witnesses who are principals", "Date: %#x")
        tblResults:outputResults()
    end
end
-- Additional functions


function records(type)
    local pi = fhNewItemPtr()
    local p2 = fhNewItemPtr()
    pi:MoveToFirstRecord(type)
    return function ()
        p2:MoveTo(pi)
        pi:MoveNext()
        if p2:IsNotNull() then return p2 end
    end
end

function facts(pi)
    local pf = fhNewItemPtr()
    local pf2 = fhNewItemPtr()
    pf:MoveToFirstChildItem(pi)
    return function ()
        while pf:IsNotNull() do
            pf2:MoveTo(pf)
            pf:MoveNext()
            if fhIsFact(pf2) then return pf2 end
        end
    end
end

function createResultTable()
    local tblOutput_mt = {} -- create metatable
    local iC = 0 -- Define count of lines
    local tblOutput = {} -- Define Columns Table
    tblOutput_mt.col = 0
    tblOutput_mt.seq = {}
    tblOutput_mt.__newindex =

    function (t,k,v)
        -- Set Values to Defaults if not supplied
        if v.content == nil then v.content = {} end
        if v.title == nil then v.title = k end
        if v.type == nil then v.type = 'notset' end
        if v.width == nil then v.width = 140 end
        if v.align == nil then v.align = 'align_left' end
        if v.sort == nil then v.sort = 0 end
        if v.sortAscending == nil then v.sortAscending = true end
        if v.sortType == nil then v.sortType = 'default' end
        if v.visibility == nil then v. visibility = 'show' end
        v.set =

        function(self,value)
            self.content[iC] = value
            if self.type == 'notset' then
                if type(value) == 'string' then
                    self.type = 'text'
                elseif type(value) == 'number' then
                    self.type = 'integer'
                    self.width = '30'
                else
                    self.type = 'item'
                end
            end
        end
        rawset(t,k,v) -- update original table
        local m = getmetatable(t)
        m.col = m.col + 1
        table.insert(m.seq,k)
    end
    tblOutput_mt.__call =

    function (t)
        local i = 0
        local m = getmetatable(t)
        local n = table.getn(m.seq)
        return function ()
            i = i + 1
            if i <= n then
                return t[m.seq[i]]
            end
        end
    end
    tblOutput.newRow = function(t)
        iC = iC + 1
    end
    tblOutput.rowCount = function(t)
        return iC
    end
    tblOutput.outputResults = function(self)
        for l in self() do
            fhOutputResultSetColumn(l.title, l.type, l.content, iC, l.width,l.align,l.sort,l.sortAscending,l.sortType,l.visibility )
        end
    end
    setmetatable(tblOutput, tblOutput_mt)
    return tblOutput
end
if not(table.getn) then

    function table.getn(t)
        local count = 0
        for _, __ in pairs(t) do
            count = count + 1
        end
        return count
    end
end
if _VERSION == "Lua 5.3" then
    unpack = table.unpack
end

main();

Source:List-principals-who-are-witnesses.fh_lua