Implemting a my_unctrl

This commit is contained in:
Anonymus Raccoon
2020-05-23 15:25:19 +02:00
parent 704c5d1b79
commit ae76418f04
7 changed files with 65 additions and 8 deletions
+2
View File
@@ -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);
+1
View File
@@ -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), \
+29 -3
View File
@@ -10,15 +10,41 @@
#include <string.h>
#include <stdio.h>
#include <malloc.h>
#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);
}
+2 -2
View File
@@ -8,16 +8,16 @@
#include "shell.h"
#include "builtin.h"
#include "key_functions.h"
#include "my_ncurses.h"
#include "utility.h"
#include <termios.h>
#include <malloc.h>
#include <stddef.h>
#include <ncurses.h>
#include <string.h>
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;
+7 -1
View File
@@ -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}
};
};
int get_emacs_bindings_size()
{
return sizeof(emacs_bindings);
}
+6 -1
View File
@@ -12,6 +12,7 @@
#include <stddef.h>
#include <string.h>
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);
+18 -1
View File
@@ -7,7 +7,7 @@
#include "my_ncurses.h"
#include <stdio.h>
#include <ncurses.h>
#include <unctrl.h>
#include <unistd.h>
#include <sys/ioctl.h>
@@ -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;