-- SPDX-FileCopyrightText: © 2026 Vladimir Zorin -- SPDX-License-Identifier: LicenseRef-OWL-1.0-or-later -- Licensed under OWL v1.0+. See LICENSE. local search = function(self, input) self:flush() local trimmed = (input or ""):match("^%s*(.-)%s*$") or "" if trimmed == "" then return false end local cmd, rest = trimmed:match("^(%S+)%s+(.*)$") local candidates = {} local metadata = {} local dedupe = {} local push = function(suffix, source_name) if type(suffix) ~= "string" or suffix == "" then return end if dedupe[suffix] then return end dedupe[suffix] = true table.insert(candidates, suffix) table.insert(metadata, { source = source_name }) end if not cmd then -- Still typing command name local commands = self:source("wiki_commands") if commands then for _, c in ipairs(commands:search(trimmed)) do push(c, "wiki_command") end end elseif cmd == "wiki" then local dbs = self:source("wiki_dbs") if dbs then for _, c in ipairs(dbs:search(rest or "")) do push(c, "wiki_db") end end elseif cmd == "view" then local results = self:source("wiki_results") if results then for _, c in ipairs(results:search(rest or "")) do push(c, "wiki_result") end end if #candidates == 0 then local entries = self:source("wiki_entries") if entries then for _, c in ipairs(entries:search(rest or "")) do push(c, "wiki_entry") end end end elseif cmd == "browse" then local indexes = self:source("wiki_indexes") if indexes then for _, c in ipairs(indexes:search(rest or "")) do push(c, "wiki_index") end end end self:provide(candidates) self:set_meta(metadata) return self:available() end return { search = search, }