CE edit modes
This commit is contained in:
48
ce/edit.c
48
ce/edit.c
@@ -27,6 +27,9 @@
|
|||||||
|
|
||||||
struct edit_line {
|
struct edit_line {
|
||||||
struct list_node_link lines_link;
|
struct list_node_link lines_link;
|
||||||
|
struct list_node_link select_link;
|
||||||
|
size_t select_start;
|
||||||
|
size_t select_end;
|
||||||
struct gapbuffer gb;
|
struct gapbuffer gb;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -35,6 +38,18 @@ struct cursor {
|
|||||||
size_t line;
|
size_t line;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#define EDIT_MODE_NORMAL 0
|
||||||
|
#define EDIT_MODE_COMAMND 1
|
||||||
|
|
||||||
|
static const char* string_modes[] = {
|
||||||
|
[EDIT_MODE_NORMAL] = "Normal",
|
||||||
|
[EDIT_MODE_COMAMND] = "Command",
|
||||||
|
};
|
||||||
|
|
||||||
|
struct edit_select {
|
||||||
|
struct list_node_link* lines;
|
||||||
|
};
|
||||||
|
|
||||||
struct editor {
|
struct editor {
|
||||||
struct list_node_link* lines;
|
struct list_node_link* lines;
|
||||||
struct edit_line* current_line;
|
struct edit_line* current_line;
|
||||||
@@ -42,6 +57,8 @@ struct editor {
|
|||||||
size_t col_offset;
|
size_t col_offset;
|
||||||
size_t row_offset;
|
size_t row_offset;
|
||||||
size_t total_lines;
|
size_t total_lines;
|
||||||
|
struct edit_select select;
|
||||||
|
int mode;
|
||||||
};
|
};
|
||||||
|
|
||||||
static struct editor editor;
|
static struct editor editor;
|
||||||
@@ -207,8 +224,9 @@ void edit_start (const char* file_path, const char* text) {
|
|||||||
bbptr += w;
|
bbptr += w;
|
||||||
bb_remaining -= w;
|
bb_remaining -= w;
|
||||||
|
|
||||||
w = snprintf (bbptr, bb_remaining, " Editing %s; line %zu col %zu" ANSIQ_GR_RESET, file_path,
|
w = snprintf (bbptr, bb_remaining, " Editing %s; line %zu col %zu; Mode: %s" ANSIQ_GR_RESET,
|
||||||
editor.cursor.line + 1, editor.cursor.col + 1);
|
file_path, editor.cursor.line + 1, editor.cursor.col + 1,
|
||||||
|
string_modes[editor.mode]);
|
||||||
bbptr += w;
|
bbptr += w;
|
||||||
bb_remaining -= w;
|
bb_remaining -= w;
|
||||||
|
|
||||||
@@ -225,20 +243,25 @@ void edit_start (const char* file_path, const char* text) {
|
|||||||
|
|
||||||
switch (ch) {
|
switch (ch) {
|
||||||
case '\b':
|
case '\b':
|
||||||
|
if (editor.mode == EDIT_MODE_NORMAL) {
|
||||||
if (editor.cursor.col > 0) {
|
if (editor.cursor.col > 0) {
|
||||||
gapbuffer_move (&editor.current_line->gb, editor.cursor.col);
|
gapbuffer_move (&editor.current_line->gb, editor.cursor.col);
|
||||||
editor.cursor.col--;
|
editor.cursor.col--;
|
||||||
gapbuffer_backspace (&editor.current_line->gb);
|
gapbuffer_backspace (&editor.current_line->gb);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case '\t':
|
case '\t':
|
||||||
|
if (editor.mode == EDIT_MODE_NORMAL) {
|
||||||
gapbuffer_move (&editor.current_line->gb, editor.cursor.col);
|
gapbuffer_move (&editor.current_line->gb, editor.cursor.col);
|
||||||
for (size_t i = 0; i < sizeof (TAB_INSERT) - 1; i++)
|
for (size_t i = 0; i < sizeof (TAB_INSERT) - 1; i++)
|
||||||
gapbuffer_insert (&wrealloc, NULL, &editor.current_line->gb, TAB_INSERT[i]);
|
gapbuffer_insert (&wrealloc, NULL, &editor.current_line->gb, TAB_INSERT[i]);
|
||||||
|
|
||||||
editor.cursor.col += sizeof (TAB_INSERT) - 1;
|
editor.cursor.col += sizeof (TAB_INSERT) - 1;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case '\n': {
|
case '\n': {
|
||||||
|
if (editor.mode == EDIT_MODE_NORMAL) {
|
||||||
gapbuffer_move (&editor.current_line->gb, editor.cursor.col);
|
gapbuffer_move (&editor.current_line->gb, editor.cursor.col);
|
||||||
|
|
||||||
struct edit_line* new_line = malloc (sizeof (*new_line));
|
struct edit_line* new_line = malloc (sizeof (*new_line));
|
||||||
@@ -264,12 +287,15 @@ void edit_start (const char* file_path, const char* text) {
|
|||||||
editor.cursor.col = 0;
|
editor.cursor.col = 0;
|
||||||
editor.cursor.line++;
|
editor.cursor.line++;
|
||||||
editor.current_line = new_line;
|
editor.current_line = new_line;
|
||||||
|
}
|
||||||
} break;
|
} break;
|
||||||
case KB_DELETE:
|
case KB_DELETE:
|
||||||
|
if (editor.mode == EDIT_MODE_NORMAL) {
|
||||||
if (editor.cursor.col < gapbuffer_length (&editor.current_line->gb)) {
|
if (editor.cursor.col < gapbuffer_length (&editor.current_line->gb)) {
|
||||||
gapbuffer_move (&editor.current_line->gb, editor.cursor.col);
|
gapbuffer_move (&editor.current_line->gb, editor.cursor.col);
|
||||||
editor.current_line->gb.gap_end++;
|
editor.current_line->gb.gap_end++;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case KB_LEFT:
|
case KB_LEFT:
|
||||||
if (editor.cursor.col > 0)
|
if (editor.cursor.col > 0)
|
||||||
@@ -305,15 +331,29 @@ void edit_start (const char* file_path, const char* text) {
|
|||||||
case KB_END:
|
case KB_END:
|
||||||
editor.cursor.col = gapbuffer_length (&editor.current_line->gb);
|
editor.cursor.col = gapbuffer_length (&editor.current_line->gb);
|
||||||
break;
|
break;
|
||||||
case KB_CTRL ('Q'):
|
case KB_ESCAPE:
|
||||||
edit_run = false;
|
editor.mode = EDIT_MODE_NORMAL;
|
||||||
break;
|
break;
|
||||||
|
case KB_CTRL ('X'):
|
||||||
|
editor.mode = EDIT_MODE_COMAMND;
|
||||||
default:
|
default:
|
||||||
if (isprint (ch)) {
|
if (isprint (ch)) {
|
||||||
|
if (editor.mode == EDIT_MODE_COMAMND) {
|
||||||
|
switch (ch) {
|
||||||
|
case 'q':
|
||||||
|
edit_run = false;
|
||||||
|
editor.mode = EDIT_MODE_NORMAL;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
editor.mode = EDIT_MODE_NORMAL;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} else if (editor.mode == EDIT_MODE_NORMAL) {
|
||||||
gapbuffer_move (&editor.current_line->gb, editor.cursor.col);
|
gapbuffer_move (&editor.current_line->gb, editor.cursor.col);
|
||||||
gapbuffer_insert (&wrealloc, NULL, &editor.current_line->gb, ch);
|
gapbuffer_insert (&wrealloc, NULL, &editor.current_line->gb, ch);
|
||||||
editor.cursor.col++;
|
editor.cursor.col++;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -20,6 +20,7 @@
|
|||||||
#define KB_PAGEDN 0xe7
|
#define KB_PAGEDN 0xe7
|
||||||
#define KB_INSERT 0xe8
|
#define KB_INSERT 0xe8
|
||||||
#define KB_DELETE 0xe9
|
#define KB_DELETE 0xe9
|
||||||
|
#define KB_ESCAPE 0x1b
|
||||||
|
|
||||||
#define KB_CTRL(x) ((x) - '@')
|
#define KB_CTRL(x) ((x) - '@')
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user