snix/users/sterni/acme/mkbqnkeyboard.bqn
sterni 473d3c3761 fix(sterni/acme/mkbqnkeyboard.bqn): allow multiple flags in one arg
Change-Id: I203cc50997f9292cef7a3383dadb96fc97108ca6
Reviewed-on: https://cl.tvl.fyi/c/depot/+/13099
Tested-by: BuildkiteCI
Autosubmit: sterni <sternenseemann@systemli.org>
Reviewed-by: sterni <sternenseemann@systemli.org>
2025-02-05 15:16:11 +00:00

66 lines
2.9 KiB
BQN
Executable file
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env BQN
# SPDX-FileCopyrightText: Copyright © 2024-2025 by sterni
# SPDX-License-Identifier: MIT
#
# Generate Compose Sequences for /lib/keyboard that enable entering BQN specific
# Unicode characters using their familiar key combinations in Plan 9 programs.
# TODO(sterni): move these helper functions somewhere reusable
LogBase ÷˜•math.Log10
Digs ( 1+)LogBase
DivMod ÷˜|
hd"0123456789ABCDEF"
ToHex {hd˜(16DivMod 1)(-1 𝕨(16Digs)) 𝕩} # 𝕨 is the min amount of digits
FromHex {+´(16𝕩)×hd𝕩}
IsAscii 127-@
# Parse CLI
opts {
flagsargs 2'-' (()¨) 𝕩
sort,help,inPlace "shi"1¨flags
argCount 2
{𝕤
•Out "Usage: "•name" [-s] [-i] /path/to/inputrc /path/to/lib/keyboard
-i Modify lib/keyboard in place. If not given, print to stdout.
-s Sort output by Unicode Codepoint."
•Exit ¬help
}(helpargCountargs) @
inputrcPathkeyboardPath •wdpath•file.At¨args
WriteOutput inPlace•out¨,keyboardPath•fLines
} •args
# Main Program
# Read inputrc, dropping comment and empty lines. Also drop the rule for \\.
# Since Plan9 requires an explicit keypress before entering a compose sequence,
# we don't need to add a way to type \ (or any ASCII character for that matter).
# This simplifies the parser below since we don't need to unescape anything.
inputrc1(("#"(1)0)¨/)•FLines opts.inputrcPath
# After removing all backslashes, the ASCII character used representing the used
# key and the resulting codepoint have a consistent position in the lines.
# Remove all ASCII chars in a second step.
# map contains pairs of ⟨BQN char, key used to type it as an ASCII char⟩
map(¬IsAscii¨/)51¨'\'(/)¨inputrc
# Render the first three fields of lib/keyboard:
# ⟨Codepoint (Hex), Compose Sequence, Resulting Character⟩
newfields{𝕊 ck: 4 ToHex c-@,"\"k,c}¨map
# In the file, the fields need be padded to a specific length…
fieldsz6120´˘¨>newfields
# … and there's a fourth name field separated by a tab.
# We don't make an effort to add a per character description there (yet).
tab@+9
newlines((tab"BQN char") ´fieldsz(¨))¨ newfields
# Due to the consistent spacing we can just compute the sort order on the
# resulting character at index 18.
Sort ( 18¨)
# Deduplicate output before writing, so the script can be executed multiple
# times or after inputrc has been changed without introducing duplicates. Since
# we duplicate on entire lines, existing alternative compose sequences aren't
# removed.
opts.WriteOutput Sortopts.sort (•FLines opts.keyboardPath)newlines