aboutsummaryrefslogtreecommitdiff
path: root/ht/gen-unicode-map.sh
blob: 7e94880145c893763508539df24d6059bd39b5f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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