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