|
| 1 | +""" |
| 2 | +# UnicodeREPL |
| 3 | +
|
| 4 | +On an empty line, press ^ to enter unicode_repl mode and backspace to exit. |
| 5 | +
|
| 6 | +In unicode_repl mode, `\\u(XXXX)` tab completes to the symbol at Unicode |
| 7 | +codepage `XXXX` where `XXXX` is a hexadecimal string of any length. |
| 8 | +
|
| 9 | +## Exported Symbols |
| 10 | +
|
| 11 | +[`ufind`](@ref) |
| 12 | +""" |
| 13 | +module UnicodeREPL |
| 14 | + |
| 15 | +using ReplMaker |
| 16 | +import REPL |
| 17 | +import REPL.LineEdit.complete_line |
| 18 | +import REPL.LineEdit.CompletionProvider |
| 19 | + |
| 20 | +export ufind |
| 21 | + |
| 22 | +struct UnicodeCompletionProvider <: CompletionProvider |
| 23 | + repl_completion_provider::CompletionProvider |
| 24 | +end |
| 25 | + |
| 26 | +function substitution(istr::AbstractString) |
| 27 | + substr = split(istr, "\\u(") |
| 28 | + if length(substr) == 1 |
| 29 | + return istr |
| 30 | + else |
| 31 | + ostr = substr[1] |
| 32 | + for str in substr[2:end] |
| 33 | + m = match(r"^[0-9a-fA-F]+[)]", str) |
| 34 | + ostr *= |
| 35 | + if m == nothing |
| 36 | + "\\u(" * str |
| 37 | + else |
| 38 | + codepoint = m.match[1:end-1] |
| 39 | + Meta.parse("\"\\u$codepoint\"") * str[length(m.match)+1:end] |
| 40 | + end |
| 41 | + end |
| 42 | + return ostr |
| 43 | + end |
| 44 | +end |
| 45 | + |
| 46 | +""" |
| 47 | + ufind(r::Union{AbstractString,Regex}) |
| 48 | +
|
| 49 | +Return the Unicode completion pairs whose keys match `r` |
| 50 | +
|
| 51 | +## Example |
| 52 | +```julia-repl |
| 53 | +julia> using UnicodeREPL |
| 54 | +REPL mode unicode_repl initialized. Press ^ to enter and backspace to exit. |
| 55 | +
|
| 56 | +julia> ufind("feed") |
| 57 | +2-element Vector{Pair{String, String}}: |
| 58 | + "\\\\:breast-feeding:" => "🤱" |
| 59 | + "\\\\linefeed" => "↴" |
| 60 | +
|
| 61 | +julia> ufind(r"^[^:]*feed") |
| 62 | +1-element Vector{Pair{String, String}}: |
| 63 | + "\\\\linefeed" => "↴" |
| 64 | +
|
| 65 | +julia> |
| 66 | +``` |
| 67 | +
|
| 68 | +Note that Unicode codepages themselves are not included, so the mapping |
| 69 | +`"\\\\u(feed)" => "ﻭ"` is not in the output. |
| 70 | +""" |
| 71 | +function ufind(r::Union{AbstractString,Regex}) |
| 72 | + completions = vcat(collect(REPL.REPLCompletions.latex_symbols), |
| 73 | + collect(REPL.REPLCompletions.emoji_symbols)) |
| 74 | + filter(pair->contains(pair.first,r), completions) |> sort |
| 75 | +end |
| 76 | + |
| 77 | +function input_handler(inputstr) |
| 78 | + Main.eval(Meta.parse(substitution(inputstr))) |
| 79 | +end |
| 80 | + |
| 81 | +function complete_line(x::UnicodeCompletionProvider, s::REPL.LineEdit.PromptState) |
| 82 | + firstpart = String(s.input_buffer.data[1:s.input_buffer.ptr-1]) |
| 83 | + firstpartsub = substitution(firstpart) |
| 84 | + if firstpart != firstpartsub |
| 85 | + return ([firstpartsub], firstpart, true) |
| 86 | + elseif VERSION <= v"1.9-" |
| 87 | + return complete_line(x.repl_completion_provider, s) |
| 88 | + else |
| 89 | + return complete_line(x.repl_completion_provider, s, Main) |
| 90 | + end |
| 91 | +end |
| 92 | + |
| 93 | +function __init__() |
| 94 | + if !isinteractive() |
| 95 | + @info "Session is not interactive" |
| 96 | + return |
| 97 | + end |
| 98 | + initrepl(input_handler; |
| 99 | + prompt_text="unicode repl> ", |
| 100 | + prompt_color=26, |
| 101 | + start_key='^', |
| 102 | + mode_name=:unicode_repl, |
| 103 | + completion_provider=UnicodeCompletionProvider(REPL.REPLCompletionProvider())) |
| 104 | +end |
| 105 | + |
| 106 | +end # module |
0 commit comments