summaryrefslogtreecommitdiff
path: root/xP/public
diff options
context:
space:
mode:
authorPřemysl Eric Janouch <p@janouch.name>2022-09-18 01:46:00 +0200
committerPřemysl Eric Janouch <p@janouch.name>2022-09-18 01:59:11 +0200
commitff243c1d110b646329fa0b0d8d4f1a52f320d03b (patch)
tree751002403bc2da2d69c2a899de4432a9b7d4296d /xP/public
parente2ef7d668c6d4526ddd1e1e21e60bc0a69e99c2e (diff)
downloadxK-ff243c1d110b646329fa0b0d8d4f1a52f320d03b.tar.gz
xK-ff243c1d110b646329fa0b0d8d4f1a52f320d03b.tar.xz
xK-ff243c1d110b646329fa0b0d8d4f1a52f320d03b.zip
xP: implement Readline's M-l, M-u, M-c
Diffstat (limited to 'xP/public')
-rw-r--r--xP/public/xP.js47
1 files changed, 43 insertions, 4 deletions
diff --git a/xP/public/xP.js b/xP/public/xP.js
index aa2c39a..1cdb8b0 100644
--- a/xP/public/xP.js
+++ b/xP/public/xP.js
@@ -742,7 +742,7 @@ let Input = {
return true
},
- backward: (b, textarea) => {
+ backward: textarea => {
if (textarea.selectionStart !== textarea.selectionEnd)
return false
@@ -756,7 +756,7 @@ let Input = {
return true
},
- forward: (b, textarea) => {
+ forward: textarea => {
if (textarea.selectionStart !== textarea.selectionEnd)
return false
@@ -769,6 +769,42 @@ let Input = {
return true
},
+ modifyWord: (textarea, cb) => {
+ let start = textarea.selectionStart
+ let end = textarea.selectionEnd
+ if (start === end) {
+ let len = textarea.value.length
+ while (start < len && /\s/.test(textarea.value.charAt(start)))
+ start++;
+ end = start
+ while (end < len && !/\s/.test(textarea.value.charAt(end)))
+ end++;
+ }
+ if (start === end)
+ return false
+
+ const text = textarea.value, modified = cb(text.substring(start, end))
+ textarea.value = text.slice(0, start) + modified + text.slice(end)
+ end = start + modified.length
+ textarea.setSelectionRange(end, end)
+ return true
+ },
+
+ downcase: textarea => {
+ return Input.modifyWord(textarea, text => text.toLowerCase())
+ },
+
+ upcase: textarea => {
+ return Input.modifyWord(textarea, text => text.toUpperCase())
+ },
+
+ capitalize: textarea => {
+ return Input.modifyWord(textarea, text => {
+ const cps = Array.from(text.toLowerCase())
+ return cps[0].toUpperCase() + cps.slice(1).join('')
+ })
+ },
+
first: (b, textarea) => {
if (b.historyAt <= 0)
return false
@@ -823,8 +859,11 @@ let Input = {
if (hasShortcutModifiers(event)) {
handled = true
switch (event.key) {
- case 'b': success = Input.backward(b, textarea); break
- case 'f': success = Input.forward(b, textarea); break
+ case 'b': success = Input.backward(textarea); break
+ case 'f': success = Input.forward(textarea); break
+ case 'l': success = Input.downcase(textarea); break
+ case 'u': success = Input.upcase(textarea); break
+ case 'c': success = Input.capitalize(textarea); break
case '<': success = Input.first(b, textarea); break
case '>': success = Input.last(b, textarea); break
case 'p': success = Input.previous(b, textarea); break