handle more keys

This commit is contained in:
Night Kaly 2024-07-01 20:40:07 +01:00
parent 2e662a95c1
commit e603eaa74c
Signed by: night0721
GPG key ID: 957D67B8DB7A119B
4 changed files with 43 additions and 0 deletions

6
include/editor.h Normal file
View file

@ -0,0 +1,6 @@
#ifndef EDITOR_H_
#define EDITOR_H_
void insert_char(int c);
#endif

View file

@ -39,6 +39,9 @@ struct abuf {
void abAppend(struct abuf *ab, const char *s, int len); void abAppend(struct abuf *ab, const char *s, int len);
void append_row(char *s, size_t len);
void row_insert_char(row *row, int at, int c);
extern editor vip; extern editor vip;
#endif #endif

12
src/editor.c Normal file
View file

@ -0,0 +1,12 @@
#include "vip.h"
extern editor vip;
void insert_char(int c)
{
if (vip.cy == vip.rows) {
append_row("", 0);
}
row_insert_char(&vip.row[vip.cy], vip.cx, c);
vip.cx++;
}

View file

@ -11,10 +11,12 @@
#include "vip.h" #include "vip.h"
#include "term.h" #include "term.h"
#include "bar.h" #include "bar.h"
#include "editor.h"
#define CTRL_KEY(k) ((k) & 0x1f) #define CTRL_KEY(k) ((k) & 0x1f)
enum editorKey { enum editorKey {
BACKSPACE = 127,
ARROW_LEFT = 1000, ARROW_LEFT = 1000,
ARROW_RIGHT, ARROW_RIGHT,
ARROW_UP, ARROW_UP,
@ -300,6 +302,10 @@ void process_key()
{ {
int c = read_key(); int c = read_key();
switch (c) { switch (c) {
case '\r':
/* TBC */
break;
case CTRL_KEY('q'): case CTRL_KEY('q'):
write(STDOUT_FILENO, "\x1b[2J", 4); write(STDOUT_FILENO, "\x1b[2J", 4);
write(STDOUT_FILENO, "\x1b[H", 3); write(STDOUT_FILENO, "\x1b[H", 3);
@ -309,11 +315,19 @@ void process_key()
case HOME_KEY: case HOME_KEY:
vip.cx = 0; vip.cx = 0;
break; break;
case END_KEY: case END_KEY:
if (vip.cy < vip.rows) { if (vip.cy < vip.rows) {
vip.cx = vip.row[vip.cy].size; vip.cx = vip.row[vip.cy].size;
} }
break; break;
case BACKSPACE:
case CTRL_KEY('h'):
case DEL_KEY:
/* TBC */
break;
case PAGE_UP: case PAGE_UP:
case PAGE_DOWN: case PAGE_DOWN:
{ {
@ -335,6 +349,14 @@ void process_key()
case ARROW_RIGHT: case ARROW_RIGHT:
move_cursor(c); move_cursor(c);
break; break;
case CTRL_KEY('l'):
case '\x1b':
break;
default:
insert_char(c);
break;
} }
} }