From e0901a1efb05727111eb88d1b27b7d1a23a87365 Mon Sep 17 00:00:00 2001 From: Albert Cervin Date: Sun, 23 Jun 2024 22:30:37 +0200 Subject: Fix buffer list switch and search/replace Fix the buffer list return key action when buffers have the same name. Previously, it would pick the first it could find in the buffer list with the correct buffer name instead of the selected one. Now it uses text properties to pass the actual buffer pointer along instead. This however exposed a problem with the clearing of properties and where in the frame it happens. Search and replace highlighting assumed that they could color things in their respective command executions. However, ideally coloring should happen in update functions so now both search and replace implement the coloring in update hooks for the buffer they are operating on. For replace, this was already kinda how it worked and could be adapted with minimal effort. Search on the other hand needed a bit more rework. --- src/main/cmds.c | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) (limited to 'src/main/cmds.c') diff --git a/src/main/cmds.c b/src/main/cmds.c index 04e42b4..4da8346 100644 --- a/src/main/cmds.c +++ b/src/main/cmds.c @@ -22,6 +22,7 @@ int32_t _abort(struct command_ctx ctx, int argc, const char *argv[]) { abort_replace(); + abort_search(); abort_completion(); disable_completion(minibuffer_buffer()); minibuffer_abort_prompt(); @@ -253,6 +254,12 @@ void buffer_to_list_line(struct buffer *buffer, void *userdata) { .set_fg = true, .fg = Color_Blue, }}); + + buffer_add_text_property( + listbuf, (struct location){.line = begin.line, .col = 0}, + (struct location){.line = begin.line, + .col = buffer_num_chars(listbuf, begin.line)}, + (struct text_property){.type = TextProperty_Data, .userdata = buffer}); } } @@ -261,22 +268,18 @@ int32_t buflist_visit_cmd(struct command_ctx ctx, int argc, struct window *w = ctx.active_window; struct buffer_view *bv = window_buffer_view(w); - struct text_chunk text = buffer_line(bv->buffer, bv->dot.line); - - char *end = (char *)memchr(text.text, ' ', text.nbytes); - - if (end != NULL) { - uint32_t len = end - (char *)text.text; - char *bufname = (char *)malloc(len + 1); - strncpy(bufname, (const char *)text.text, len); - bufname[len] = '\0'; - - struct buffer *target = buffers_find(ctx.buffers, bufname); - free(bufname); - if (target != NULL) { - window_set_buffer(w, target); + struct text_property *props[16] = {0}; + uint32_t nprops; + buffer_get_text_properties(bv->buffer, bv->dot, props, 16, &nprops); + + for (uint32_t propi = 0; propi < nprops; ++propi) { + struct text_property *p = props[propi]; + if (p->type == TextProperty_Data) { + window_set_buffer(w, p->userdata); + return 0; } } + return 0; } @@ -497,6 +500,8 @@ void register_global_commands(struct commands *commands, register_search_replace_commands(commands); } +void teardown_global_commands(void) { cleanup_search_replace(); } + #define BUFFER_VIEW_WRAPCMD(fn) \ static int32_t fn##_cmd(struct command_ctx ctx, int argc, \ const char *argv[]) { \ -- cgit v1.2.3