From ae76418f04e6db54662ce8de13e8c46104e2e309 Mon Sep 17 00:00:00 2001
From: Anonymus Raccoon
Date: Sat, 23 May 2020 15:25:19 +0200
Subject: [PATCH] Implemting a my_unctrl
---
include/key_functions.h | 2 ++
include/my_ncurses.h | 1 +
src/builtin/builtin_bindkey.c | 32 ++++++++++++++++++++---
src/key_bindings/basic_typing_functions.c | 4 +--
src/key_bindings/default_bindings.c | 8 +++++-
src/main.c | 7 ++++-
src/my_ncurses/string_utils.c | 19 +++++++++++++-
7 files changed, 65 insertions(+), 8 deletions(-)
diff --git a/include/key_functions.h b/include/key_functions.h
index 467eb43..f80fbb4 100644
--- a/include/key_functions.h
+++ b/include/key_functions.h
@@ -24,6 +24,8 @@ typedef struct binding
extern const key_function_t key_functions[];
extern const binding_t emacs_bindings[];
+int get_emacs_bindings_size();
+
int buffer_get_display_pos(buffer_t *buffer);
diff --git a/include/my_ncurses.h b/include/my_ncurses.h
index c33bb57..f2109ae 100644
--- a/include/my_ncurses.h
+++ b/include/my_ncurses.h
@@ -42,6 +42,7 @@ void my_move(my_window *window, int y, int x);
void my_getcuryx(int *y, int *x);
void my_getmaxyx(int *y, int *x);
int my_getch(void);
+const char *my_unctrl(int c);
void my_addstr(my_window *window, const char *str);
#define my_mvaddstr(window, y, x, str) (my_move(window, y, x), \
diff --git a/src/builtin/builtin_bindkey.c b/src/builtin/builtin_bindkey.c
index d72f1f3..95e60dd 100644
--- a/src/builtin/builtin_bindkey.c
+++ b/src/builtin/builtin_bindkey.c
@@ -10,15 +10,41 @@
#include
#include
#include
+#include "my_ncurses.h"
+
+void print_binding(binding_t *binding)
+{
+ const char *key = my_unctrl(binding->key);
+ const char *func = NULL;
+
+ for (int i = 0; key_functions[i].run; i++) {
+ if (key_functions[i].run == binding->func) {
+ func = key_functions[i].name;
+ break;
+ }
+ }
+ printf("%s -> %s\n", key, func);
+}
int builtin_bindkey(char **argv, env_t *env)
{
- if (!argv[0]) {
+ binding_t *tmp;
- } else if (!strcmp(argv[0], "-l")) {
+ if (!argv[1]) {
+ if (!env->bindings)
+ return (0);
+ for (int i = 0; env->bindings[i].func; i++)
+ print_binding(&env->bindings[i]);
+ } else if (!strcmp(argv[1], "-l")) {
for (int i = 0; key_functions[i].name; i++)
puts(key_functions[i].name);
- }
+ } else if (!argv[2]) {
+ // for (tmp = env->bindings; tmp && tmp->key != )
+ print_binding(tmp);
+ } else if (!argv[3]) {
+
+ } else
+ puts("Invalid usage of bindkey.");
free(argv);
return (0);
}
\ No newline at end of file
diff --git a/src/key_bindings/basic_typing_functions.c b/src/key_bindings/basic_typing_functions.c
index cf5391a..e031cb4 100644
--- a/src/key_bindings/basic_typing_functions.c
+++ b/src/key_bindings/basic_typing_functions.c
@@ -8,16 +8,16 @@
#include "shell.h"
#include "builtin.h"
#include "key_functions.h"
+#include "my_ncurses.h"
#include "utility.h"
#include
#include
#include
-#include
#include
int self_insert_command(int key, buffer_t *buffer, env_t *env)
{
- const char *chars = key == '\t' ? "\t" : unctrl(key);
+ const char *chars = key == '\t' ? "\t" : my_unctrl(key);
int charslen = strlen(chars);
int len = (buffer->buffer ? strlen(buffer->buffer) : 0) + charslen;
diff --git a/src/key_bindings/default_bindings.c b/src/key_bindings/default_bindings.c
index 406c350..6562fdd 100644
--- a/src/key_bindings/default_bindings.c
+++ b/src/key_bindings/default_bindings.c
@@ -22,6 +22,7 @@ const key_function_t key_functions[] = {
{"end-of-line", &end_of_line_command},
{"up-history", &up_history_command},
{"down-history", &down_history_command},
+ {"complete-command", &complete_command},
{NULL, NULL}
};
@@ -39,4 +40,9 @@ const binding_t emacs_bindings[] = {
{KEY_DOWN, &down_history_command},
{'\t', &complete_command},
{0, NULL}
-};
\ No newline at end of file
+};
+
+int get_emacs_bindings_size()
+{
+ return sizeof(emacs_bindings);
+}
\ No newline at end of file
diff --git a/src/main.c b/src/main.c
index c96729b..ec8f7ff 100644
--- a/src/main.c
+++ b/src/main.c
@@ -12,6 +12,7 @@
#include
#include
+
env_t *create_env(char **env)
{
env_t *envt = malloc(sizeof(*envt));
@@ -25,7 +26,11 @@ env_t *create_env(char **env)
envt->env = envcp;
envt->vars = NULL;
envt->history = NULL;
- envt->bindings = &emacs_bindings;
+ envt->bindings = malloc(get_emacs_bindings_size());
+ if (envt->bindings) {
+ for (int i = 0; emacs_bindings[i].func; i++)
+ envt->bindings[i] = emacs_bindings[i];
+ }
envt->window = NULL;
envt->alias = NULL;
return (envt);
diff --git a/src/my_ncurses/string_utils.c b/src/my_ncurses/string_utils.c
index c48e6df..a7f76c3 100644
--- a/src/my_ncurses/string_utils.c
+++ b/src/my_ncurses/string_utils.c
@@ -7,7 +7,7 @@
#include "my_ncurses.h"
#include
-#include
+#include
#include
#include
@@ -32,6 +32,23 @@ void my_addstr(my_window *window, const char *str)
printf("%s", str);
}
+const char *my_unctrl(int c)
+{
+ static char str[5];
+
+ if (c == KEY_DC)
+ return "^?";
+ if ((c & 0xFFFF) == CSI(0)) {
+ str[0] = '^';
+ str[1] = '[';
+ str[2] = c >> 16u;
+ str[3] = c >> 24u;
+ str[4] = '\0';
+ return (str);
+ }
+ return (unctrl(c));
+}
+
void my_getmaxyx(int *y, int *x)
{
struct winsize size;