summaryrefslogtreecommitdiff
path: root/command.c
diff options
context:
space:
mode:
authorLeo Tenenbaum <pommicket@gmail.com>2021-03-03 19:17:48 -0500
committerLeo Tenenbaum <pommicket@gmail.com>2021-03-03 19:17:48 -0500
commite3eb333ae2b07467e14dd9e2f845889f75f01a16 (patch)
tree64599a9b11f757a5e480c9f94b3ddedb57678d4e /command.c
parent74cae07c859e68876ee98b99e1c1761d4c205484 (diff)
start autocomplete
Diffstat (limited to 'command.c')
-rw-r--r--command.c35
1 files changed, 35 insertions, 0 deletions
diff --git a/command.c b/command.c
index 727b2c6..f0dd1ec 100644
--- a/command.c
+++ b/command.c
@@ -204,6 +204,39 @@ void command_execute(Ted *ted, Command c, i64 argument) {
}
}
break;
+
+ case CMD_AUTOCOMPLETE: {
+ if (buffer && !buffer->view_only && !buffer->is_line_buffer) {
+ buffer->selection = false;
+ if (is_word(buffer_char_after_cursor(buffer)))
+ buffer_cursor_move_right_words(buffer, 1);
+ else
+ buffer_scroll_to_cursor(buffer);
+ char *start = str32_to_utf8_cstr(buffer_word_at_cursor(buffer));
+ char *completions[2] = {0};
+ size_t ncompletions = tags_beginning_with(ted, start, completions, 2);
+ switch (ncompletions) {
+ case 0:
+ ted->cursor_error_time = time_get_seconds();
+ break;
+ case 1: {
+ char *str = completions[0] + strlen(start);
+ buffer_start_edit_chain(buffer); // don't merge with other edits
+ buffer_insert_utf8_at_cursor(buffer, str);
+ buffer_end_edit_chain(buffer);
+ } break;
+ case 2:
+ // open autocomplete selection menu
+ ted->autocomplete = true;
+ break;
+ default: assert(0); break;
+ }
+ free(completions[0]);
+ free(completions[1]);
+ free(start);
+ }
+ } break;
+
case CMD_UNDO:
if (buffer) buffer_undo(buffer, argument);
break;
@@ -301,6 +334,8 @@ void command_execute(Ted *ted, Command c, i64 argument) {
if (*ted->error_shown) {
// dismiss error box
*ted->error_shown = '\0';
+ } else if (ted->autocomplete) {
+ ted->autocomplete = false;
} else if (ted->menu) {
menu_escape(ted);
} else if (ted->find) {