From b979257c3a7018d18dd85510663979470cbd3092 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=99emysl=20Eric=20Janouch?= Date: Sun, 18 Sep 2022 00:38:31 +0200 Subject: [PATCH] xP: implement Readline's M-< and M-> --- xP/public/xP.js | 85 ++++++++++++++++++++++++++++--------------------- 1 file changed, 49 insertions(+), 36 deletions(-) diff --git a/xP/public/xP.js b/xP/public/xP.js index 30491d3..71c1b6e 100644 --- a/xP/public/xP.js +++ b/xP/public/xP.js @@ -692,7 +692,7 @@ let Input = { textarea.selectionStart, textarea.selectionEnd, textarea.value] }, - complete: textarea => { + complete: (b, textarea) => { if (textarea.selectionStart !== textarea.selectionEnd) return false @@ -726,7 +726,7 @@ let Input = { return true }, - submit: textarea => { + submit: (b, textarea) => { rpc.send({ command: 'BufferInput', bufferName: bufferCurrent, @@ -736,41 +736,49 @@ let Input = { // b.history[b.history.length] is virtual, and is represented // either by textarea contents when it's currently being edited, // or by b.input in all other cases. - let b = buffers.get(bufferCurrent) b.history.push(textarea.value) b.historyAt = b.history.length textarea.value = '' return true }, - previous: textarea => { - let b = buffers.get(bufferCurrent) - if (b === undefined) + first: (b, textarea) => { + if (b.historyAt <= 0) return false - if (b.historyAt > 0) { - if (b.historyAt == b.history.length) - b.input = textarea.value - textarea.value = b.history[--b.historyAt] - } else { - beep() - } + if (b.historyAt == b.history.length) + b.input = textarea.value + textarea.value = b.history[(b.historyAt = 0)] return true }, - next: textarea => { - let b = buffers.get(bufferCurrent) - if (b === undefined) + last: (b, textarea) => { + if (b.historyAt >= b.history.length) return false - if (b.historyAt < b.history.length) { - if (++b.historyAt == b.history.length) - textarea.value = b.input - else - textarea.value = b.history[b.historyAt] - } else { - beep() - } + b.historyAt = b.history.length + textarea.value = b.input + return true + }, + + previous: (b, textarea) => { + 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 }, @@ -778,28 +786,33 @@ let Input = { // TODO: And perhaps on other actions, too. rpc.send({command: 'Active'}) + let b = buffers.get(bufferCurrent) + if (b === undefined) + return + let textarea = event.currentTarget let handled = false + let success = true if (hasShortcutModifiers(event)) { + handled = true switch (event.key) { - case 'p': - handled = Input.previous(textarea) - break - case 'n': - handled = Input.next(textarea) - break + case '<': success = Input.first(b, textarea); break + case '>': success = Input.last(b, textarea); break + case 'p': success = Input.previous(b, textarea); break + case 'n': success = Input.next(b, textarea); break + default: handled = false } } else if (!event.altKey && !event.ctrlKey && !event.metaKey && !event.shiftKey) { + handled = true switch (event.keyCode) { - case 9: - handled = Input.complete(textarea) - break - case 13: - handled = Input.submit(textarea) - break + case 9: success = Input.complete(b, textarea); break + case 13: success = Input.submit(b, textarea); break + default: handled = false } } + if (!success) + beep() if (handled) event.preventDefault() },