Initial commit: X11 C code editor
This commit is contained in:
commit
8eae6763f6
21 files changed
+36389
No files matched your search
@@ -0,0 +1,191 @@
|
||||
#include "editor.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
static void free_step(UndoStep *step) {
|
||||
free(step->lines);
|
||||
step->lines = NULL;
|
||||
step->num_lines = 0;
|
||||
}
|
||||
|
||||
static int save_state_to_step(const Editor *ed, UndoStep *step) {
|
||||
size_t bytes = (size_t)ed->num_lines * MAX_LEN;
|
||||
step->lines = malloc(bytes);
|
||||
if (!step->lines) return 0;
|
||||
memcpy(step->lines, ed->lines, bytes);
|
||||
step->num_lines = ed->num_lines;
|
||||
step->cx = ed->cx;
|
||||
step->cy = ed->cy;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void restore_state_from_step(Editor *ed, const UndoStep *step) {
|
||||
if (!step->lines) return;
|
||||
memcpy(ed->lines, step->lines, (size_t)step->num_lines * MAX_LEN);
|
||||
ed->num_lines = step->num_lines;
|
||||
ed->cx = step->cx;
|
||||
ed->cy = step->cy;
|
||||
ed->has_sel = 0;
|
||||
editor_ensure_cursor_visible(ed);
|
||||
}
|
||||
|
||||
void history_push_undo(Editor *ed) {
|
||||
for (int i = 0; i < ed->redo_count; i++) {
|
||||
free_step(&ed->redo_stack[i]);
|
||||
}
|
||||
ed->redo_count = 0;
|
||||
|
||||
if (ed->undo_count < ed->saved_undo_level) {
|
||||
ed->saved_undo_level = -1;
|
||||
}
|
||||
|
||||
if (ed->undo_count >= MAX_UNDO) {
|
||||
free_step(ed->undo_stack);
|
||||
memmove(ed->undo_stack, ed->undo_stack + 1, (MAX_UNDO - 1) * sizeof(UndoStep));
|
||||
ed->undo_count = MAX_UNDO - 1;
|
||||
if (ed->saved_undo_level > 0) ed->saved_undo_level--;
|
||||
else ed->saved_undo_level = -1;
|
||||
}
|
||||
|
||||
if (save_state_to_step(ed, &ed->undo_stack[ed->undo_count])) {
|
||||
ed->undo_count++;
|
||||
}
|
||||
editor_set_dirty(ed, 1);
|
||||
}
|
||||
|
||||
void history_undo(Editor *ed) {
|
||||
if (ed->undo_count <= 0) {
|
||||
snprintf(ed->status_msg, sizeof(ed->status_msg), "Nothing to undo");
|
||||
return;
|
||||
}
|
||||
|
||||
if (ed->redo_count >= MAX_UNDO) {
|
||||
free_step(ed->redo_stack);
|
||||
memmove(ed->redo_stack, ed->redo_stack + 1, (MAX_UNDO - 1) * sizeof(UndoStep));
|
||||
ed->redo_count = MAX_UNDO - 1;
|
||||
}
|
||||
if (save_state_to_step(ed, &ed->redo_stack[ed->redo_count])) {
|
||||
ed->redo_count++;
|
||||
}
|
||||
|
||||
ed->undo_count--;
|
||||
restore_state_from_step(ed, &ed->undo_stack[ed->undo_count]);
|
||||
free_step(&ed->undo_stack[ed->undo_count]);
|
||||
editor_set_dirty(ed, ed->undo_count != ed->saved_undo_level);
|
||||
snprintf(ed->status_msg, sizeof(ed->status_msg), "Undo");
|
||||
}
|
||||
|
||||
void history_redo(Editor *ed) {
|
||||
if (ed->redo_count <= 0) {
|
||||
snprintf(ed->status_msg, sizeof(ed->status_msg), "Nothing to redo");
|
||||
return;
|
||||
}
|
||||
|
||||
if (ed->undo_count >= MAX_UNDO) {
|
||||
free_step(ed->undo_stack);
|
||||
memmove(ed->undo_stack, ed->undo_stack + 1, (MAX_UNDO - 1) * sizeof(UndoStep));
|
||||
ed->undo_count = MAX_UNDO - 1;
|
||||
}
|
||||
if (save_state_to_step(ed, &ed->undo_stack[ed->undo_count])) {
|
||||
ed->undo_count++;
|
||||
}
|
||||
|
||||
ed->redo_count--;
|
||||
restore_state_from_step(ed, &ed->redo_stack[ed->redo_count]);
|
||||
free_step(&ed->redo_stack[ed->redo_count]);
|
||||
editor_set_dirty(ed, ed->undo_count != ed->saved_undo_level);
|
||||
snprintf(ed->status_msg, sizeof(ed->status_msg), "Redo");
|
||||
}
|
||||
|
||||
void history_free_all(Editor *ed) {
|
||||
for (int i = 0; i < ed->undo_count; i++) free_step(&ed->undo_stack[i]);
|
||||
for (int i = 0; i < ed->redo_count; i++) free_step(&ed->redo_stack[i]);
|
||||
ed->undo_count = 0;
|
||||
ed->redo_count = 0;
|
||||
}
|
||||
|
||||
void search_find_next(Editor *ed, int from_cur_pos) {
|
||||
int qlen = (int)strlen(ed->search_query);
|
||||
if (qlen == 0) {
|
||||
ed->has_sel = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
int start_y = ed->cy;
|
||||
int start_x = ed->cx;
|
||||
if (ed->has_sel) {
|
||||
int sy, sx, ey, ex;
|
||||
editor_get_sel_bounds(ed, &sy, &sx, &ey, &ex);
|
||||
start_y = sy;
|
||||
start_x = from_cur_pos ? sx : (sx + 1);
|
||||
} else if (!from_cur_pos) {
|
||||
start_x = ed->cx + 1;
|
||||
}
|
||||
|
||||
for (int pass = 0; pass < 2; pass++) {
|
||||
int y_begin = (pass == 0) ? start_y : 0;
|
||||
int y_end = (pass == 0) ? ed->num_lines : (start_y + 1);
|
||||
|
||||
for (int y = y_begin; y < y_end; y++) {
|
||||
int line_len = (int)strlen(ed->lines[y]);
|
||||
int x_begin = (pass == 0 && y == start_y) ? start_x : 0;
|
||||
if (x_begin > line_len) continue;
|
||||
|
||||
const char *found = strstr(&ed->lines[y][x_begin], ed->search_query);
|
||||
if (found) {
|
||||
int pos = (int)(found - ed->lines[y]);
|
||||
ed->has_sel = 1;
|
||||
ed->sel_ay = y;
|
||||
ed->sel_ax = pos;
|
||||
ed->cy = y;
|
||||
ed->cx = pos + qlen;
|
||||
editor_ensure_cursor_visible(ed);
|
||||
snprintf(ed->status_msg, sizeof(ed->status_msg), "Found at Ln %d", y + 1);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
snprintf(ed->status_msg, sizeof(ed->status_msg), "Not found: %s", ed->search_query);
|
||||
}
|
||||
|
||||
void search_find_prev(Editor *ed) {
|
||||
int qlen = (int)strlen(ed->search_query);
|
||||
if (qlen == 0) return;
|
||||
|
||||
int limit_y = ed->cy;
|
||||
int limit_x = ed->cx;
|
||||
if (ed->has_sel) {
|
||||
int sy, sx, ey, ex;
|
||||
editor_get_sel_bounds(ed, &sy, &sx, &ey, &ex);
|
||||
limit_y = sy;
|
||||
limit_x = sx;
|
||||
}
|
||||
|
||||
for (int pass = 0; pass < 2; pass++) {
|
||||
int y_begin = (pass == 0) ? limit_y : (ed->num_lines - 1);
|
||||
int y_end = (pass == 0) ? 0 : limit_y;
|
||||
|
||||
for (int y = y_begin; y >= y_end; y--) {
|
||||
int line_len = (int)strlen(ed->lines[y]);
|
||||
int max_x = (pass == 0 && y == limit_y) ? (limit_x - 1) : (line_len - qlen);
|
||||
if (max_x > line_len - qlen) max_x = line_len - qlen;
|
||||
|
||||
for (int x = max_x; x >= 0; x--) {
|
||||
if (strncmp(&ed->lines[y][x], ed->search_query, (size_t)qlen) == 0) {
|
||||
ed->has_sel = 1;
|
||||
ed->sel_ay = y;
|
||||
ed->sel_ax = x;
|
||||
ed->cy = y;
|
||||
ed->cx = x + qlen;
|
||||
editor_ensure_cursor_visible(ed);
|
||||
snprintf(ed->status_msg, sizeof(ed->status_msg), "Found at Ln %d", y + 1);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
snprintf(ed->status_msg, sizeof(ed->status_msg), "Not found: %s", ed->search_query);
|
||||
}
|
||||
Reference in new issue
Block a user