#!/bin/env julia

const filename = "julia-vim-L2U-table"
const sourcefile = joinpath(JULIA_HOME, "..", "..", "doc", "manual", "unicode-input-table.rst")

isreadable(sourcefile) || error("unable to find source file $sourcefile")

function fix_compose_chars(uni)
  u = uni[1]
  isc = ('\u0300' ≤ u ≤ '\u036F') ||
        ('\u1DC0' ≤ u ≤ '\u1DFF') ||
        ('\u20D0' ≤ u ≤ '\u20FF') ||
        ('\uFE20' ≤ u ≤ '\uFE2F')
  return isc ? "\u25CC" * uni : uni
end

open("$filename.txt","w") do f
    print(f, """
      $filename.txt  LaTeX-to-Unicode reference table

      ===================================================================
      LATEX-TO-UNICODE REFERENCE TABLE    *L2U-ref* *julia-vim-L2U-reference*

        Note: This file is autogenerated from the script '$(basename(Base.source_path()))'
        The symbols are based on the documentation of Julia version $VERSION
        See |julia-vim| for the LaTeX-to-Unicode manual.

      """)

    col_headers = ["Code point(s)", "Character(s)", "Tab completion sequence(s)", "Unicode name(s)"]

    code = String[]
    unicode = String[]
    latex = String[]
    desc = String[]

    open(sourcefile) do sf
        for l in eachline(sf)
            m = match(r"
                ^(U\+[0-9A-F]{5}(\ \+\ U\+[0-9A-F]{5})*)
                \s+
                (\X)
                \s+
                (\\\\[^[:space:],]+(,\ \\\\[^[:space:],]+)*)
                \s+
                (.*)$
                "x, l)
            m == nothing && continue
            c, _, u, l, _, d = m.captures
            push!(code, c)
            push!(unicode, fix_compose_chars(u))
            push!(latex, replace(l, "\\\\", "\\"))
            push!(desc, d)
        end
    end

    cw = max(length(col_headers[1]), maximum(map(length, code)))
    uw = max(length(col_headers[2]), maximum(map(length, unicode)))
    lw = max(length(col_headers[3]), maximum(map(length, latex)))
    dw = max(length(col_headers[4]), maximum(map(length, desc)))

    print_padded(c, u, l, d) = println(f, rpad(c, cw), " ", rpad(u, uw), " ", rpad(l, lw), " ", d)

    print_padded(col_headers[1:3]..., col_headers[4] * "~")
    print_padded("-"^cw, "-"^uw, "-"^lw, "-"^dw)

    for (c, u, l, d) in zip(code, unicode, latex, desc)
        print_padded(c, u, l, d)
    end
    print_padded("-"^cw, "-"^uw, "-"^lw, "-"^dw)

    println(f)
    println(f, "  vim", # separated to avoid an error from vim which otherwise tries to parse this line
               ":tw=$(cw+uw+lw+dw+3):et:ft=help:norl:")
end
