# Opinionated rainbow csv implementation

hook global BufCreate .*[.](csv) %{
    set-option buffer filetype csv
}

hook global WinSetOption filetype=csv %{
    try %{
        remove-highlighter window/csv
    }
    lua %{
        rgx = "(^[^\n,]*[,$])?"
        -- These colors are designed to match the Everforest Dark colorscheme.
        colors = {"blue", "green", "magenta", "rgb:e69875"}
        faces = {}
        for idx, color in pairs(colors) do
            if idx > 1 then
                rgx = rgx .. "([^\n,]*[,$])?"
            end
            faces[idx] = idx .. ":" .. color
        end
        kak.add_highlighter("window/csv", "regions")
        kak.add_highlighter("window/csv/comment", "region", "^", "\n", "group")
        kak.add_highlighter("window/csv/comment/", "regex", rgx, table.unpack(faces))
    }
}

hook global WinSetOption filetype=(?!csv).* %{
    try %{
        remove-highlighter window/csv
    }
}
