aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPřemysl Eric Janouch <p@janouch.name>2022-09-11 15:54:39 +0200
committerPřemysl Eric Janouch <p@janouch.name>2022-09-11 19:10:40 +0200
commit23deca45c9c181071782caa8029806732fab7873 (patch)
tree351c0f8ea1a44bb238c80a2ecbb2f6e38999b7d6
parent62773acaa099d7633a6d83d44b790f4f53fb274e (diff)
downloadxK-23deca45c9c181071782caa8029806732fab7873.tar.gz
xK-23deca45c9c181071782caa8029806732fab7873.tar.xz
xK-23deca45c9c181071782caa8029806732fab7873.zip
xP: fix non-ASCII text completion
-rw-r--r--xP/public/xP.js16
1 files changed, 13 insertions, 3 deletions
diff --git a/xP/public/xP.js b/xP/public/xP.js
index 7fa6bf3..89c8e1c 100644
--- a/xP/public/xP.js
+++ b/xP/public/xP.js
@@ -129,6 +129,9 @@ class RelayRpc extends EventTarget {
// ---- Utilities --------------------------------------------------------------
+function utf8Encode(s) { return new TextEncoder().encode(s) }
+function utf8Decode(s) { return new TextDecoder().decode(s) }
+
// On macOS, the Alt/Option key transforms characters, which basically breaks
// all event.altKey shortcuts, so require combining them with Control as well
// on that system.
@@ -598,15 +601,22 @@ let Input = {
command: 'BufferComplete',
bufferName: bufferCurrent,
text: textarea.value,
- position: textarea.selectionEnd,
+ position: utf8Encode(
+ textarea.value.slice(0, textarea.selectionEnd)).length,
}).then(resp => {
if (!Input.stamp(textarea).every((v, k) => v === state[k]))
return
+ let preceding = utf8Encode(textarea.value).slice(0, resp.start)
+ let start = utf8Decode(preceding).length
+
// TODO: Somehow display remaining options, or cycle through.
- if (resp.completions.length)
+ if (resp.completions.length) {
textarea.setRangeText(resp.completions[0],
- resp.start, textarea.selectionEnd, 'end')
+ start, textarea.selectionEnd, 'end')
+ } else {
+ beep()
+ }
if (resp.completions.length === 1)
textarea.setRangeText(' ',
textarea.selectionStart, textarea.selectionEnd, 'end')