diff --git a/doc/Possible-key-values.md b/doc/Possible-key-values.md index c420f8d14..e6a1ddd60 100644 --- a/doc/Possible-key-values.md +++ b/doc/Possible-key-values.md @@ -161,8 +161,8 @@ Where `` is one of the kinds documented below and `` is a space separated list of attributes. `` depends on the ``. Attributes are: -- `symbol='Sym'` is the symbol to be shown on the keyboard. -- `flags=''` is a collection of flags that change the behavior of the key. +- `symbol='Sym'` specifies the symbol to be shown on the keyboard. +- `flags=''` changes the behavior of the key. `` is a coma separated list of: + `dim`: Make the symbol dimmer. + `small`: Make the symbol smaller. @@ -172,4 +172,18 @@ Attributes are: Defines a key that outputs an arbitrary string. `` is a string wrapped in single-quotes (`'`), escaping of other single quotes is allowed with `\'`. -For example: `:str symbol='Sym':'Output string'` +For example: +- `:str:'Arbitrary string with a \' inside'` +- `:str symbol='Symbol':'Output string'` + +### Kind `char` + +Defines a key that outputs a single character. `` is the character to +output, unquoted. +This kind of key can be used to define a character key with a different symbol +on it. `char` keys can be modified by `ctrl` and other modifiers, unlike `str` +keys. + +For example: +- `:char symbol='q':љ`, which is used to implement `ctrl` shortcuts in cyrillic + layouts. diff --git a/srcs/juloo.keyboard2/KeyValue.java b/srcs/juloo.keyboard2/KeyValue.java index b6c09d54d..320eaaa92 100644 --- a/srcs/juloo.keyboard2/KeyValue.java +++ b/srcs/juloo.keyboard2/KeyValue.java @@ -393,7 +393,14 @@ public static KeyValue makeStringKey(String str) public static KeyValue makeCharKey(char c) { - return new KeyValue(String.valueOf(c), Kind.Char, c, 0); + return makeCharKey(c, null, 0); + } + + public static KeyValue makeCharKey(char c, String symbol, int flags) + { + if (symbol == null) + symbol = String.valueOf(c); + return new KeyValue(symbol, Kind.Char, c, flags); } public static KeyValue makeComposePending(String symbol, int state, int flags) diff --git a/srcs/juloo.keyboard2/KeyValueParser.java b/srcs/juloo.keyboard2/KeyValueParser.java index 178046e9a..7e4ba262e 100644 --- a/srcs/juloo.keyboard2/KeyValueParser.java +++ b/srcs/juloo.keyboard2/KeyValueParser.java @@ -9,21 +9,7 @@ - If [str] doesn't start with a [:] character, it is interpreted as an arbitrary string key. -[(kind)] specifies the kind of the key, it can be: -- [str]: An arbitrary string key. The payload is the string to output when - typed and is quoted by single quotes ([']). The payload can contain single - quotes if they are escaped with a backslash ([\']). - -The [(attributes)] part is a space-separated list of attributes, all optional, -of the form: [attrname='attrvalue']. - -Attributes can be: -- [flags]: Add flags that change the behavior of the key. - Value is a coma separated list of: - - [dim]: Make the symbol dimmer on the keyboard. - - [small]: Make the symbol smaller on the keyboard. -- [symbol]: Specify the symbol that is rendered on the keyboard. - It can contain single quotes if they are escaped: ([\']). +For the different kinds and attributes, see doc/Possible-key-values.md. Examples: - [:str flags=dim,small symbol='MyKey':'My arbitrary string']. @@ -36,6 +22,7 @@ public final class KeyValueParser static Pattern ATTR_PAT; static Pattern QUOTED_PAT; static Pattern PAYLOAD_START_PAT; + static Pattern SINGLE_CHAR_PAT; static public KeyValue parse(String str) throws ParseError { @@ -73,11 +60,14 @@ static public KeyValue parse(String str) throws ParseError switch (kind) { case "str": - String payload = parseSingleQuotedString(m); + String str_payload = parseSingleQuotedString(m); if (symbol == null) - return KeyValue.makeStringKey(payload, flags); - return KeyValue.makeStringKeyWithSymbol(payload, symbol, flags); + return KeyValue.makeStringKey(str_payload, flags); + return KeyValue.makeStringKeyWithSymbol(str_payload, symbol, flags); + case "char": + char char_payload = parseOneChar(m); + return KeyValue.makeCharKey(char_payload, symbol, flags); default: break; } parseError("Unknown kind '"+kind+"'", m, 1); @@ -91,6 +81,13 @@ static String parseSingleQuotedString(Matcher m) throws ParseError return m.group(1).replace("\\'", "'"); } + static char parseOneChar(Matcher m) throws ParseError + { + if (!match(m, SINGLE_CHAR_PAT)) + parseError("Expected a character", m); + return m.group(0).charAt(0); + } + static int parseFlags(String s, Matcher m) throws ParseError { int flags = 0; @@ -121,6 +118,7 @@ static void init() ATTR_PAT = Pattern.compile("\\s*(\\w+)\\s*="); QUOTED_PAT = Pattern.compile("'(([^'\\\\]+|\\\\')*)'"); PAYLOAD_START_PAT = Pattern.compile("\\s*:"); + SINGLE_CHAR_PAT = Pattern.compile("."); } static void parseError(String msg, Matcher m) throws ParseError diff --git a/srcs/layouts/cyrl_lynyertz_sr.xml b/srcs/layouts/cyrl_lynyertz_sr.xml index 675c6b32d..2d7ba6094 100644 --- a/srcs/layouts/cyrl_lynyertz_sr.xml +++ b/srcs/layouts/cyrl_lynyertz_sr.xml @@ -8,32 +8,32 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/test/juloo.keyboard2/KeyValueParserTest.java b/test/juloo.keyboard2/KeyValueParserTest.java index 900ae3d64..a636ebf41 100644 --- a/test/juloo.keyboard2/KeyValueParserTest.java +++ b/test/juloo.keyboard2/KeyValueParserTest.java @@ -10,7 +10,7 @@ public class KeyValueParserTest public KeyValueParserTest() {} @Test - public void parse() throws Exception + public void parseStr() throws Exception { Utils.parse(":str:'Foo'", KeyValue.makeStringKey("Foo")); Utils.parse(":str flags='dim':'Foo'", KeyValue.makeStringKey("Foo", KeyValue.FLAG_SECONDARY)); @@ -34,6 +34,13 @@ public void parse() throws Exception Utils.expect_error(":str flags='':'"); } + @Test + public void parseChar() throws Exception + { + Utils.parse(":char symbol='a':b", KeyValue.makeCharKey('b', "a", 0)); + Utils.parse(":char:b", KeyValue.makeCharKey('b', "b", 0)); + } + /** JUnit removes these functions from stacktraces. */ static class Utils {