xP: implement Readline's M-l, M-u, M-c

This commit is contained in:
Přemysl Eric Janouch 2022-09-18 01:46:00 +02:00
parent e2ef7d668c
commit ff243c1d11
Signed by: p
GPG Key ID: A0420B94F92B9493
1 changed files with 43 additions and 4 deletions

View File

@ -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