diff options
Diffstat (limited to 'ht/gen-unicode-map.sh')
-rwxr-xr-x | ht/gen-unicode-map.sh | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/ht/gen-unicode-map.sh b/ht/gen-unicode-map.sh new file mode 100755 index 0000000..7e94880 --- /dev/null +++ b/ht/gen-unicode-map.sh @@ -0,0 +1,49 @@ +#!/bin/sh +gofmt <<EOF +// Code generated by running "go generate" in janouch.name/haven. DO NOT EDIT. + +package $GOPACKAGE + +import "janouch.name/haven/nexgb/xproto" + +// KeysymToRune tries to translate an X11 keysym to an appropriate rune. +// Returns -1 when no match is found. +func KeysymToRune(ks xproto.Keysym) rune { + // Visible Latin-1 is mapped 1:1 + if (ks >= 0x20 && ks <= 0x7e) || (ks >= 0xa0 && ks <= 0xff) { + return rune(ks) + } + // Directly encoded 24-bit Unicode (going even above plane 16) + if (ks & 0xff000000) == 0x01000000 { + return rune(ks & 0x00ffffff) + } + + min, max := 0, len(keysymToRuneTable)-1 + for max >= min { + mid := (min + max) / 2 + if keysymToRuneTable[mid].keysym < ks { + min = mid + 1 + } else if keysymToRuneTable[mid].keysym > ks { + max = mid - 1 + } else { + return keysymToRuneTable[mid].unicode + } + } + return -1 +} + +var keysymToRuneTable = []struct{ + keysym xproto.Keysym + unicode rune +}{ +$(curl --silent --show-error --location \ + https://invisible-island.net/datafiles/release/xterm.tar.gz | \ +tar --wildcards -xzOf - 'xterm-*/unicode/keysym.map' | \ +sort | perl -lne ' + next unless /^(0x([0-9a-f]{4}))\s+U([0-9a-f]{4})\s*(?:\#\s*(.*))?$/; + my ($keysym, $ks, $unicode, $comment) = ($1, hex($2), $3, ($4 // "")); + print "{$keysym, 0x$unicode}, // $comment" + if $ks >= 0x100 && $unicode ne "0000"; +') +} +EOF |