From 02b6bbeaa87d50b2d1041e68e5f99fbd93edf97c Mon Sep 17 00:00:00 2001 From: Florian Wehner Date: Fri, 25 Aug 2023 15:04:39 -0400 Subject: [PATCH] Handling of UTF character codes for special keys added UTF character codes are now kept as a single entity and not broken into single characters. This also works with multiple repeated codes. Main application is enabling the use of special keys like the arrow keys. --- README.md | 2 +- gencompose.py | 15 +++++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 0c4b114..299d959 100644 --- a/README.md +++ b/README.md @@ -163,7 +163,7 @@ $ head xcompose.compose * `DefaultKeyBinding.dict` map doesn't work in some popular programs: - JetBrain editors such as PyCharm, see: https://youtrack.jetbrains.com/issue/IDEA-127470 - QT apps such as Qutebrowser, see (and vote): https://bugreports.qt.io/browse/QTBUG-393 - +* UTF character codes like `\UF703` (the right arrow key) are kept as is. This will break any key combinations starting with `\U`. #### Related Resources diff --git a/gencompose.py b/gencompose.py index 1d715f5..fef8d2f 100644 --- a/gencompose.py +++ b/gencompose.py @@ -104,16 +104,26 @@ def merge(source, destination): def read_paths(data): """ Unpack first key of a dictionary as nested dictionary path + Do not split utf character codes. >>> read_paths({'cat': '>-.-<'}) {'c': {'a': {'t': '>-.-<'}}} """ + utf_pattern = re.compile(r'\\U[0-9A-Fa-f]{4,6}') + parsed = {} for k, v in data.items(): - parsed = merge(parsed, key_nest(list(k) + [v])) + # If the key matches the utf pattern, split it into individual utf codes and nest them + if utf_pattern.search(k): + utf_codes = utf_pattern.findall(k) + nested_data = v + for code in reversed(utf_codes): + nested_data = {code: nested_data} + parsed = merge(parsed, nested_data) + else: + parsed = merge(parsed, key_nest(list(k) + [v])) return parsed - def data_to_mac_dict(data): """ converts dictionary data to macos keymap.dict format @@ -133,6 +143,7 @@ def data_to_mac_dict(data): text = re.sub('"INSERT:(.+)",*', repl, text) text = re.sub('},*', '};', text) text = re.sub('": ', '" = ', text) + text = re.sub(r'\\\\U([0-9A-Fa-f]{4,6})', r'\\U\1', text) return text