xP: implement Readline's M-< and M->

This commit is contained in:
Přemysl Eric Janouch 2022-09-18 00:38:31 +02:00
parent 840b646700
commit b979257c3a
Signed by: p
GPG Key ID: A0420B94F92B9493
1 changed files with 49 additions and 36 deletions

View File

@ -692,7 +692,7 @@ let Input = {
textarea.selectionStart, textarea.selectionEnd, textarea.value] textarea.selectionStart, textarea.selectionEnd, textarea.value]
}, },
complete: textarea => { complete: (b, textarea) => {
if (textarea.selectionStart !== textarea.selectionEnd) if (textarea.selectionStart !== textarea.selectionEnd)
return false return false
@ -726,7 +726,7 @@ let Input = {
return true return true
}, },
submit: textarea => { submit: (b, textarea) => {
rpc.send({ rpc.send({
command: 'BufferInput', command: 'BufferInput',
bufferName: bufferCurrent, bufferName: bufferCurrent,
@ -736,41 +736,49 @@ let Input = {
// b.history[b.history.length] is virtual, and is represented // b.history[b.history.length] is virtual, and is represented
// either by textarea contents when it's currently being edited, // either by textarea contents when it's currently being edited,
// or by b.input in all other cases. // or by b.input in all other cases.
let b = buffers.get(bufferCurrent)
b.history.push(textarea.value) b.history.push(textarea.value)
b.historyAt = b.history.length b.historyAt = b.history.length
textarea.value = '' textarea.value = ''
return true return true
}, },
previous: textarea => { first: (b, textarea) => {
let b = buffers.get(bufferCurrent) if (b.historyAt <= 0)
if (b === undefined)
return false return false
if (b.historyAt > 0) { if (b.historyAt == b.history.length)
if (b.historyAt == b.history.length) b.input = textarea.value
b.input = textarea.value textarea.value = b.history[(b.historyAt = 0)]
textarea.value = b.history[--b.historyAt]
} else {
beep()
}
return true return true
}, },
next: textarea => { last: (b, textarea) => {
let b = buffers.get(bufferCurrent) if (b.historyAt >= b.history.length)
if (b === undefined)
return false return false
if (b.historyAt < b.history.length) { b.historyAt = b.history.length
if (++b.historyAt == b.history.length) textarea.value = b.input
textarea.value = b.input return true
else },
textarea.value = b.history[b.historyAt]
} else { previous: (b, textarea) => {
beep() if (b.historyAt <= 0)
} return false
if (b.historyAt == b.history.length)
b.input = textarea.value
textarea.value = b.history[--b.historyAt]
return true
},
next: (b, textarea) => {
if (b.historyAt >= b.history.length)
return false
if (++b.historyAt == b.history.length)
textarea.value = b.input
else
textarea.value = b.history[b.historyAt]
return true return true
}, },
@ -778,28 +786,33 @@ let Input = {
// TODO: And perhaps on other actions, too. // TODO: And perhaps on other actions, too.
rpc.send({command: 'Active'}) rpc.send({command: 'Active'})
let b = buffers.get(bufferCurrent)
if (b === undefined)
return
let textarea = event.currentTarget let textarea = event.currentTarget
let handled = false let handled = false
let success = true
if (hasShortcutModifiers(event)) { if (hasShortcutModifiers(event)) {
handled = true
switch (event.key) { switch (event.key) {
case 'p': case '<': success = Input.first(b, textarea); break
handled = Input.previous(textarea) case '>': success = Input.last(b, textarea); break
break case 'p': success = Input.previous(b, textarea); break
case 'n': case 'n': success = Input.next(b, textarea); break
handled = Input.next(textarea) default: handled = false
break
} }
} else if (!event.altKey && !event.ctrlKey && !event.metaKey && } else if (!event.altKey && !event.ctrlKey && !event.metaKey &&
!event.shiftKey) { !event.shiftKey) {
handled = true
switch (event.keyCode) { switch (event.keyCode) {
case 9: case 9: success = Input.complete(b, textarea); break
handled = Input.complete(textarea) case 13: success = Input.submit(b, textarea); break
break default: handled = false
case 13:
handled = Input.submit(textarea)
break
} }
} }
if (!success)
beep()
if (handled) if (handled)
event.preventDefault() event.preventDefault()
}, },