--[[ @Title: Family groups @Type: Standard @Author: Torleif Haugødegård @Version: 1.2.1 @LastUpdated: 16 Nov 2026 @Licence: This plugin is copyright (c) 2026 Torleif Haugødegård and is licensed under the MIT License which is hereby incorporated by reference (see https://pluginstore.family-historian.co.uk/fh-plugin-licence) @Description: Finds groups of people connected through family relationships and shows the youngest and the oldest person in each group ]] local minSize = 1 -- 1 = show all groups, including single individuals -- Union-find: each person points towards a "root" in their group local parent = {} local function find(id) while parent[id] ~= id do parent[id] = parent[parent[id]] -- path compression id = parent[id] end return id end local function union(a, b) local ra, rb = find(a), find(b) if ra ~= rb then parent[ra] = rb end end -- 1) Register all individuals local totalPeople = 0 local indi = fhNewItemPtr() indi:MoveToFirstRecord("INDI") while not indi:IsNull() do local id = fhGetRecordId(indi) parent[id] = id totalPeople = totalPeople + 1 indi:MoveNext() end -- Helper: get the record ID from a link field (HUSB, WIFE, CHIL[n]) local function getId(famPtr, path) local field = fhGetItemPtr(famPtr, path) if field:IsNull() then return nil end local person = fhGetValueAsLink(field) if person:IsNull() then return nil end return fhGetRecordId(person) end -- 2) Connect all members of each family local fam = fhNewItemPtr() fam:MoveToFirstRecord("FAM") while not fam:IsNull() do local members = {} -- Collect ALL HUSB, WIFE and CHIL links (a family may have two HUSB or two WIFE) local field = fhNewItemPtr() field:MoveToFirstChildItem(fam) while not field:IsNull() do local tag = fhGetTag(field) if tag == "HUSB" or tag == "WIFE" or tag == "CHIL" then local person = fhGetValueAsLink(field) if not person:IsNull() then members[#members + 1] = fhGetRecordId(person) end end field:MoveNext() end for i = 2, #members do if parent[members[1]] and parent[members[i]] then union(members[1], members[i]) end end fam:MoveNext() end -- 3) Count the size of each group local groupSize = {} for id in pairs(parent) do local root = find(id) groupSize[root] = (groupSize[root] or 0) + 1 end -- Get a year from a date field (nil if unknown) local function yearFromPath(personPtr, path) local d = fhGetItemPtr(personPtr, path) if d:IsNull() then return nil end local text = fhGetDisplayText(d) local year = text and tostring(text):match("(%d%d%d%d)") return year and tonumber(year) or nil end -- Birth year, with baptism (BAPM or CHR) as a fallback local function birthYear(personPtr) local paths = { "~.BIRT.DATE", "~.BIRT-DATE", "~.BAPM.DATE", "~.BAPM-DATE", "~.CHR.DATE", "~.CHR-DATE" } for _, path in ipairs(paths) do local year = yearFromPath(personPtr, path) if year then return year end end return nil end -- 4) Find the youngest and the oldest person in each group local youngestId, youngestYear = {}, {} local oldestId, oldestYear = {}, {} local yearsFound = 0 local p = fhNewItemPtr() p:MoveToFirstRecord("INDI") while not p:IsNull() do local id = fhGetRecordId(p) local root = find(id) local year = birthYear(p) if year then yearsFound = yearsFound + 1 end -- youngest: latest birth year local isBetter = false if not youngestId[root] then isBetter = true elseif year and (not youngestYear[root] or year > youngestYear[root]) then isBetter = true end if isBetter then youngestId[root] = id youngestYear[root] = year end -- oldest: earliest birth year local isOlder = false if not oldestId[root] then isOlder = true elseif year and (not oldestYear[root] or year < oldestYear[root]) then isOlder = true end if isOlder then oldestId[root] = id oldestYear[root] = year end p:MoveNext() end -- 5) Select groups local groups = {} for root, size in pairs(groupSize) do if size >= minSize then groups[#groups + 1] = root end end -- Largest group first table.sort(groups, function(a, b) if groupSize[a] ~= groupSize[b] then return groupSize[a] > groupSize[b] end return youngestId[a] < youngestId[b] end) -- 6) Build the columns local nr, sizeCol, idCol, personCol, yearCol = {}, {}, {}, {}, {} local oIdCol, oPersonCol, oYearCol = {}, {}, {} for i, root in ipairs(groups) do nr[i] = i sizeCol[i] = groupSize[root] idCol[i] = youngestId[root] yearCol[i] = youngestYear[root] and tostring(youngestYear[root]) or "unknown" local ptr = fhNewItemPtr() ptr:MoveToRecordById("INDI", youngestId[root]) personCol[i] = ptr oIdCol[i] = oldestId[root] oYearCol[i] = oldestYear[root] and tostring(oldestYear[root]) or "unknown" local optr = fhNewItemPtr() optr:MoveToRecordById("INDI", oldestId[root]) oPersonCol[i] = optr end -- Column widths are set in characters. Each character is converted to a width -- using the same scale that worked for the Treetops plugin (4 per character + 4). local function charWidth(chars) return chars * 4 + 4 end -- Choose what to display: "groups", "summary" or "both" -- If only the last table appears in your version of Family Historian, -- run the plugin once with "groups" and once with "summary". local view = "both" -- 7) Summary statistics local numGroups, largest, singles = 0, 0, 0 for _, size in pairs(groupSize) do numGroups = numGroups + 1 if size > largest then largest = size end if size == 1 then singles = singles + 1 end end local multiGroups = numGroups - singles local connected = totalPeople - singles local average = numGroups > 0 and (totalPeople / numGroups) or 0 -- 8) Groups table if view ~= "summary" then local count = #groups fhOutputResultSetTitles("Family groups with youngest and oldest person (birth year found for " .. yearsFound .. " people)") fhOutputResultSetColumn("Group", "integer", nr, count, charWidth(8)) fhOutputResultSetColumn("People", "integer", sizeCol, count, charWidth(8)) fhOutputResultSetColumn("Youngest ID", "integer", idCol, count, charWidth(12)) fhOutputResultSetColumn("Youngest person", "item", personCol, count, charWidth(40)) fhOutputResultSetColumn("Born", "text", yearCol, count, charWidth(8)) fhOutputResultSetColumn("Oldest ID", "integer", oIdCol, count, charWidth(12)) fhOutputResultSetColumn("Oldest person", "item", oPersonCol, count, charWidth(40)) fhOutputResultSetColumn("Born", "text", oYearCol, count, charWidth(8)) end -- 9) Summary table if view ~= "groups" then local labels = { "Total people", "Total groups", "Groups of 2+", "Single individuals", "Connected people", "Largest group", "Average group size", "Birth year found" } local values = { tostring(totalPeople), tostring(numGroups), tostring(multiGroups), tostring(singles), tostring(connected), tostring(largest), string.format("%.1f", average), tostring(yearsFound) } fhOutputResultSetTitles("Family groups") fhOutputResultSetColumn("Measure", "text", labels, #labels, charWidth(20)) fhOutputResultSetColumn("Value", "text", values, #values, 100) end