From 3a401f0771efe6e2d03c1920c7937f3779e2a0e0 Mon Sep 17 00:00:00 2001 From: Aaron Bull Schaefer Date: Sun, 2 Jun 2019 16:26:25 -0700 Subject: [PATCH] Trim whitespace from spell check word length variable Due to ambiguity in the POSIX standard, GNU and BSD versions of the `wc` utility use slightly different whitespace conventions when formatting their output [1]. When limiting the output to just counting the number of bytes (as is done by Kakoune when calculating the length of words for spell check highlighting), the BSD version of `wc -c` has some additional leading whitespace: gnu$ printf %s "test" | wc -c 4 bsd$ printf %s "test" | wc -c 4 This leading whitespace needs to be removed before defining the "region" to highlight, or `set-option` will not be able to parse the given `spell_regions` and will complain that there are "not enough elements in tuple." In other words, the region `1.21+8|Error` on Linux ends up looking like `1.21+ 8|Error` on macOS, which is invalid. Removing the whitespace could be accomplished in a number of ways, but using arithmetic expansion [2] is POSIX compliant and does not require shelling out to another process. [1]: https://unix.stackexchange.com/questions/205906/extra-space-with-counted-line-number [2]: https://mywiki.wooledge.org/ArithmeticExpression --- rc/tools/spell.kak | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/rc/tools/spell.kak b/rc/tools/spell.kak index 10c0f915..442dbc9f 100644 --- a/rc/tools/spell.kak +++ b/rc/tools/spell.kak @@ -41,7 +41,8 @@ Formats of language supported: pos=$(printf %s\\n "$line" | cut -d ' ' -f 3) fi word=$(printf %s\\n "$line" | cut -d ' ' -f 2) - len=$(printf %s "$word" | wc -c) + # trim whitespace to make `wc` output consistent across implementations + len=$(($(printf %s "$word" | wc -c))) regions="$regions $line_num.$pos+${len}|Error" ;; '') line_num=$((line_num + 1));;