aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/tui/input.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/nvim/tui/input.c')
-rw-r--r--src/nvim/tui/input.c146
1 files changed, 73 insertions, 73 deletions
diff --git a/src/nvim/tui/input.c b/src/nvim/tui/input.c
index 5130678a81..a5768cfc06 100644
--- a/src/nvim/tui/input.c
+++ b/src/nvim/tui/input.c
@@ -15,7 +15,6 @@
#include "nvim/option_vars.h"
#include "nvim/os/os.h"
#include "nvim/os/os_defs.h"
-#include "nvim/rbuffer.h"
#include "nvim/strings.h"
#include "nvim/tui/input.h"
#include "nvim/tui/input_defs.h"
@@ -153,7 +152,7 @@ void tinput_init(TermInput *input, Loop *loop)
termkey_set_canonflags(input->tk, curflags | TERMKEY_CANON_DELBS);
// setup input handle
- rstream_init_fd(loop, &input->read_stream, input->in_fd, READ_STREAM_SIZE);
+ rstream_init_fd(loop, &input->read_stream, input->in_fd);
// initialize a timer handle for handling ESC with libtermkey
uv_timer_init(&loop->uv, &input->timer_handle);
@@ -211,9 +210,9 @@ static void tinput_flush(TermInput *input)
input->key_buffer_len = 0;
}
-static void tinput_enqueue(TermInput *input, char *buf, size_t size)
+static void tinput_enqueue(TermInput *input, const char *buf, size_t size)
{
- if (input->key_buffer_len > KEY_BUFFER_SIZE - 0xff) {
+ if (input->key_buffer_len > KEY_BUFFER_SIZE - size) {
// don't ever let the buffer get too full or we risk putting incomplete keys into it
tinput_flush(input);
}
@@ -463,8 +462,10 @@ static void tinput_timer_cb(uv_timer_t *handle)
TermInput *input = handle->data;
// If the raw buffer is not empty, process the raw buffer first because it is
// processing an incomplete bracketed paster sequence.
- if (rbuffer_size(input->read_stream.buffer)) {
- handle_raw_buffer(input, true);
+ size_t size = rstream_available(&input->read_stream);
+ if (size) {
+ size_t consumed = handle_raw_buffer(input, true, input->read_stream.read_pos, size);
+ rstream_consume(&input->read_stream, consumed);
}
tk_getkeys(input, true);
tinput_flush(input);
@@ -478,39 +479,37 @@ static void tinput_timer_cb(uv_timer_t *handle)
///
/// @param input the input stream
/// @return true iff handle_focus_event consumed some input
-static bool handle_focus_event(TermInput *input)
+static size_t handle_focus_event(TermInput *input, const char *ptr, size_t size)
{
- if (rbuffer_size(input->read_stream.buffer) > 2
- && (!rbuffer_cmp(input->read_stream.buffer, "\x1b[I", 3)
- || !rbuffer_cmp(input->read_stream.buffer, "\x1b[O", 3))) {
- bool focus_gained = *rbuffer_get(input->read_stream.buffer, 2) == 'I';
- // Advance past the sequence
- rbuffer_consumed(input->read_stream.buffer, 3);
+ if (size >= 3
+ && (!memcmp(ptr, "\x1b[I", 3)
+ || !memcmp(ptr, "\x1b[O", 3))) {
+ bool focus_gained = ptr[2] == 'I';
MAXSIZE_TEMP_ARRAY(args, 1);
ADD_C(args, BOOLEAN_OBJ(focus_gained));
rpc_send_event(ui_client_channel_id, "nvim_ui_set_focus", args);
- return true;
+ return 3; // Advance past the sequence
}
- return false;
+ return 0;
}
#define START_PASTE "\x1b[200~"
#define END_PASTE "\x1b[201~"
-static HandleState handle_bracketed_paste(TermInput *input)
+static size_t handle_bracketed_paste(TermInput *input, const char *ptr, size_t size,
+ bool *incomplete)
{
- size_t buf_size = rbuffer_size(input->read_stream.buffer);
- if (buf_size > 5
- && (!rbuffer_cmp(input->read_stream.buffer, START_PASTE, 6)
- || !rbuffer_cmp(input->read_stream.buffer, END_PASTE, 6))) {
- bool enable = *rbuffer_get(input->read_stream.buffer, 4) == '0';
+ if (size >= 6
+ && (!memcmp(ptr, START_PASTE, 6)
+ || !memcmp(ptr, END_PASTE, 6))) {
+ bool enable = ptr[4] == '0';
if (input->paste && enable) {
- return kNotApplicable; // Pasting "start paste" code literally.
+ return 0; // Pasting "start paste" code literally.
}
+
// Advance past the sequence
- rbuffer_consumed(input->read_stream.buffer, 6);
if (!!input->paste == enable) {
- return kComplete; // Spurious "disable paste" code.
+ return 6; // Spurious "disable paste" code.
}
if (enable) {
@@ -525,15 +524,15 @@ static HandleState handle_bracketed_paste(TermInput *input)
// Paste phase: "disabled".
input->paste = 0;
}
- return kComplete;
- } else if (buf_size < 6
- && (!rbuffer_cmp(input->read_stream.buffer, START_PASTE, buf_size)
- || !rbuffer_cmp(input->read_stream.buffer,
- END_PASTE, buf_size))) {
+ return 6;
+ } else if (size < 6
+ && (!memcmp(ptr, START_PASTE, size)
+ || !memcmp(ptr, END_PASTE, size))) {
// Wait for further input, as the sequence may be split.
- return kIncomplete;
+ *incomplete = true;
+ return 0;
}
- return kNotApplicable;
+ return 0;
}
/// Handle an OSC or DCS response sequence from the terminal.
@@ -644,20 +643,31 @@ static void handle_unknown_csi(TermInput *input, const TermKeyKey *key)
}
}
-static void handle_raw_buffer(TermInput *input, bool force)
+static size_t handle_raw_buffer(TermInput *input, bool force, const char *data, size_t size)
{
- HandleState is_paste = kNotApplicable;
+ const char *ptr = data;
do {
- if (!force
- && (handle_focus_event(input)
- || (is_paste = handle_bracketed_paste(input)) != kNotApplicable)) {
- if (is_paste == kIncomplete) {
+ if (!force) {
+ size_t consumed = handle_focus_event(input, ptr, size);
+ if (consumed) {
+ ptr += consumed;
+ size -= consumed;
+ continue;
+ }
+
+ bool incomplete = false;
+ consumed = handle_bracketed_paste(input, ptr, size, &incomplete);
+ if (incomplete) {
+ assert(consumed == 0);
// Wait for the next input, leaving it in the raw buffer due to an
// incomplete sequence.
- return;
+ return (size_t)(ptr - data);
+ } else if (consumed) {
+ ptr += consumed;
+ size -= consumed;
+ continue;
}
- continue;
}
//
@@ -666,55 +676,47 @@ static void handle_raw_buffer(TermInput *input, bool force)
// calls (above) depend on this.
//
size_t count = 0;
- RBUFFER_EACH(input->read_stream.buffer, c, i) {
+ for (size_t i = 0; i < size; i++) {
count = i + 1;
- if (c == '\x1b' && count > 1) {
+ if (ptr[i] == '\x1b' && count > 1) {
count--;
break;
}
}
// Push bytes directly (paste).
if (input->paste) {
- RBUFFER_UNTIL_EMPTY(input->read_stream.buffer, ptr, len) {
- size_t consumed = MIN(count, len);
- assert(consumed <= input->read_stream.buffer->size);
- tinput_enqueue(input, ptr, consumed);
- rbuffer_consumed(input->read_stream.buffer, consumed);
- if (!(count -= consumed)) {
- break;
- }
- }
+ tinput_enqueue(input, ptr, count);
+ ptr += count;
+ size -= count;
continue;
}
+
// Push through libtermkey (translates to "<keycode>" strings, etc.).
- RBUFFER_UNTIL_EMPTY(input->read_stream.buffer, ptr, len) {
- const size_t size = MIN(count, len);
- if (size > termkey_get_buffer_remaining(input->tk)) {
+ {
+ const size_t to_use = MIN(count, size);
+ if (to_use > termkey_get_buffer_remaining(input->tk)) {
// We are processing a very long escape sequence. Increase termkey's
// internal buffer size. We don't handle out of memory situations so
// abort if it fails
- const size_t delta = size - termkey_get_buffer_remaining(input->tk);
+ const size_t delta = to_use - termkey_get_buffer_remaining(input->tk);
const size_t bufsize = termkey_get_buffer_size(input->tk);
if (!termkey_set_buffer_size(input->tk, MAX(bufsize + delta, bufsize * 2))) {
abort();
}
}
- size_t consumed = termkey_push_bytes(input->tk, ptr, size);
+ size_t consumed = termkey_push_bytes(input->tk, ptr, to_use);
// We resize termkey's buffer when it runs out of space, so this should
// never happen
- assert(consumed <= rbuffer_size(input->read_stream.buffer));
- rbuffer_consumed(input->read_stream.buffer, consumed);
+ assert(consumed <= to_use);
+ ptr += consumed;
+ size -= consumed;
// Process the input buffer now for any keys
tk_getkeys(input, false);
-
- if (!(count -= consumed)) {
- break;
- }
}
- } while (rbuffer_size(input->read_stream.buffer));
+ } while (size);
const size_t tk_size = termkey_get_buffer_size(input->tk);
const size_t tk_remaining = termkey_get_buffer_remaining(input->tk);
@@ -726,23 +728,25 @@ static void handle_raw_buffer(TermInput *input, bool force)
abort();
}
}
+
+ return (size_t)(ptr - data);
}
-static void tinput_read_cb(RStream *stream, RBuffer *buf, size_t count_, void *data, bool eof)
+static size_t tinput_read_cb(RStream *stream, const char *buf, size_t count_, void *data, bool eof)
{
TermInput *input = data;
+ size_t consumed = handle_raw_buffer(input, false, buf, count_);
+ tinput_flush(input);
+
if (eof) {
loop_schedule_fast(&main_loop, event_create(tinput_done_event, NULL));
- return;
+ return consumed;
}
- handle_raw_buffer(input, false);
- tinput_flush(input);
-
// An incomplete sequence was found. Leave it in the raw buffer and wait for
// the next input.
- if (rbuffer_size(input->read_stream.buffer)) {
+ if (consumed < count_) {
// If 'ttimeout' is not set, start the timer with a timeout of 0 to process
// the next input.
int64_t ms = input->ttimeout
@@ -750,11 +754,7 @@ static void tinput_read_cb(RStream *stream, RBuffer *buf, size_t count_, void *d
// Stop the current timer if already running
uv_timer_stop(&input->timer_handle);
uv_timer_start(&input->timer_handle, tinput_timer_cb, (uint32_t)ms, 0);
- return;
}
- // Make sure the next input escape sequence fits into the ring buffer without
- // wraparound, else it could be misinterpreted (because rbuffer_read_ptr()
- // exposes the underlying buffer to callers unaware of the wraparound).
- rbuffer_reset(input->read_stream.buffer);
+ return consumed;
}