Skip to content

Commit

Permalink
Mark file as dirty when changing crlf
Browse files Browse the repository at this point in the history
  • Loading branch information
evanlin96069 committed Apr 27, 2024
1 parent 099a5fd commit ba9f1a3
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 81 deletions.
49 changes: 37 additions & 12 deletions src/action.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,21 @@ bool editorUndo(void) {
if (gCurFile->action_current == gCurFile->action_head)
return false;

editorDeleteText(gCurFile->action_current->action->added_range);
editorPasteText(&gCurFile->action_current->action->deleted_text,
gCurFile->action_current->action->deleted_range.start_x,
gCurFile->action_current->action->deleted_range.start_y);
gCurFile->cursor = gCurFile->action_current->action->old_cursor;
switch (gCurFile->action_current->action->type) {
case ACTION_EDIT: {
EditAction* edit = &gCurFile->action_current->action->edit;
editorDeleteText(edit->added_range);
editorPasteText(&edit->deleted_text, edit->deleted_range.start_x,
edit->deleted_range.start_y);
gCurFile->cursor = edit->old_cursor;
} break;

case ACTION_ATTRI: {
AttributeAction* attri = &gCurFile->action_current->action->attri;
gCurFile->newline = attri->old_newline;
} break;
}

gCurFile->action_current = gCurFile->action_current->prev;
gCurFile->dirty--;
return true;
Expand All @@ -24,11 +34,22 @@ bool editorRedo(void) {
return false;

gCurFile->action_current = gCurFile->action_current->next;
editorDeleteText(gCurFile->action_current->action->deleted_range);
editorPasteText(&gCurFile->action_current->action->added_text,
gCurFile->action_current->action->added_range.start_x,
gCurFile->action_current->action->added_range.start_y);
gCurFile->cursor = gCurFile->action_current->action->new_cursor;

switch (gCurFile->action_current->action->type) {
case ACTION_EDIT: {
EditAction* edit = &gCurFile->action_current->action->edit;
editorDeleteText(edit->deleted_range);
editorPasteText(&edit->added_text, edit->added_range.start_x,
edit->added_range.start_y);
gCurFile->cursor = edit->new_cursor;
} break;

case ACTION_ATTRI: {
AttributeAction* attri = &gCurFile->action_current->action->attri;
gCurFile->newline = attri->new_newline;
} break;
}

gCurFile->dirty++;
return true;
}
Expand Down Expand Up @@ -60,8 +81,12 @@ void editorAppendAction(EditorAction* action) {
void editorFreeAction(EditorAction* action) {
if (!action)
return;
editorFreeClipboardContent(&action->deleted_text);
editorFreeClipboardContent(&action->added_text);

if (action->type == ACTION_EDIT) {
editorFreeClipboardContent(&action->edit.deleted_text);
editorFreeClipboardContent(&action->edit.added_text);
}

free(action);
}

Expand Down
20 changes: 19 additions & 1 deletion src/action.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ typedef struct EditorCursor {
int select_y;
} EditorCursor;

typedef struct EditorAction {
typedef struct EditAction {
EditorSelectRange deleted_range;
EditorClipboard deleted_text;

Expand All @@ -20,6 +20,24 @@ typedef struct EditorAction {

EditorCursor old_cursor;
EditorCursor new_cursor;
} EditAction;

typedef struct AttributeAction {
int old_newline;
int new_newline;
} AttributeAction;

typedef enum EditorActionType {
ACTION_EDIT,
ACTION_ATTRI,
} EditorActionType;

typedef struct EditorAction {
EditorActionType type;
union {
EditAction edit;
AttributeAction attri;
};
} EditorAction;

typedef struct EditorActionList {
Expand Down
20 changes: 18 additions & 2 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -207,13 +207,29 @@ CON_COMMAND(newline, "Set the EOL sequence (LF/CRLF).") {
return;
}

int nl;

if (strCaseCmp(args.argv[1], "lf") == 0) {
gCurFile->newline = NL_UNIX;
nl = NL_UNIX;
} else if (strCaseCmp(args.argv[1], "crlf") == 0) {
gCurFile->newline = NL_DOS;
nl = NL_DOS;
} else {
editorMsg("Usage: newline <LF/CRLF>");
return;
}

if (gCurFile->newline == nl) {
return;
}

EditorAction* action = calloc_s(1, sizeof(EditorAction));
action->type = ACTION_ATTRI;
action->attri.new_newline = nl;
action->attri.old_newline = gCurFile->newline;

gCurFile->newline = nl;

editorAppendAction(action);
}

CON_COMMAND(echo, "Echo text to console.") {
Expand Down
132 changes: 66 additions & 66 deletions src/input.c
Original file line number Diff line number Diff line change
Expand Up @@ -565,8 +565,12 @@ void editorProcessKeypress(void) {
bool should_scroll = true;

bool should_record_action = false;

EditorAction* action = calloc_s(1, sizeof(EditorAction));
action->old_cursor = gCurFile->cursor;
action->type = ACTION_EDIT;
EditAction* edit = &action->edit;

edit->old_cursor = gCurFile->cursor;

int c = input.type;
int x = input.data.cursor.x;
Expand All @@ -576,22 +580,22 @@ void editorProcessKeypress(void) {
case '\r': {
should_record_action = true;

getSelectStartEnd(&action->deleted_range);
getSelectStartEnd(&edit->deleted_range);

if (gCurFile->cursor.is_selected) {
editorCopyText(&action->deleted_text, action->deleted_range);
editorDeleteText(action->deleted_range);
editorCopyText(&edit->deleted_text, edit->deleted_range);
editorDeleteText(edit->deleted_range);
gCurFile->cursor.is_selected = false;
}

gCurFile->bracket_autocomplete = 0;

action->added_range.start_x = gCurFile->cursor.x;
action->added_range.start_y = gCurFile->cursor.y;
edit->added_range.start_x = gCurFile->cursor.x;
edit->added_range.start_y = gCurFile->cursor.y;
editorInsertNewline();
action->added_range.end_x = gCurFile->cursor.x;
action->added_range.end_y = gCurFile->cursor.y;
editorCopyText(&action->added_text, action->added_range);
edit->added_range.end_x = gCurFile->cursor.x;
edit->added_range.end_y = gCurFile->cursor.y;
editorCopyText(&edit->added_text, edit->added_range);
} break;

// Quit editor
Expand Down Expand Up @@ -756,9 +760,9 @@ void editorProcessKeypress(void) {
should_record_action = true;

if (gCurFile->cursor.is_selected) {
getSelectStartEnd(&action->deleted_range);
editorCopyText(&action->deleted_text, action->deleted_range);
editorDeleteText(action->deleted_range);
getSelectStartEnd(&edit->deleted_range);
editorCopyText(&edit->deleted_text, edit->deleted_range);
editorDeleteText(edit->deleted_range);
gCurFile->cursor.is_selected = false;
break;
}
Expand All @@ -785,8 +789,8 @@ void editorProcessKeypress(void) {
editorMoveCursor(ARROW_RIGHT);
}

action->deleted_range.end_x = gCurFile->cursor.x;
action->deleted_range.end_y = gCurFile->cursor.y;
edit->deleted_range.end_x = gCurFile->cursor.x;
edit->deleted_range.end_y = gCurFile->cursor.y;

if (should_delete_bracket)
editorMoveCursor(ARROW_LEFT);
Expand All @@ -812,10 +816,10 @@ void editorProcessKeypress(void) {
}
}
}
action->deleted_range.start_x = gCurFile->cursor.x;
action->deleted_range.start_y = gCurFile->cursor.y;
editorCopyText(&action->deleted_text, action->deleted_range);
editorDeleteText(action->deleted_range);
edit->deleted_range.start_x = gCurFile->cursor.x;
edit->deleted_range.start_y = gCurFile->cursor.y;
editorCopyText(&edit->deleted_text, edit->deleted_range);
editorDeleteText(edit->deleted_range);
} break;

// Action: Cut
Expand Down Expand Up @@ -847,14 +851,14 @@ void editorProcessKeypress(void) {
}
}

action->deleted_range = range;
editorCopyText(&action->deleted_text, action->deleted_range);
editorDeleteText(action->deleted_range);
edit->deleted_range = range;
editorCopyText(&edit->deleted_text, edit->deleted_range);
editorDeleteText(edit->deleted_range);
} else {
getSelectStartEnd(&action->deleted_range);
editorCopyText(&action->deleted_text, action->deleted_range);
editorCopyText(&gEditor.clipboard, action->deleted_range);
editorDeleteText(action->deleted_range);
getSelectStartEnd(&edit->deleted_range);
editorCopyText(&edit->deleted_text, edit->deleted_range);
editorCopyText(&gEditor.clipboard, edit->deleted_range);
editorDeleteText(edit->deleted_range);
gCurFile->cursor.is_selected = false;
}
editorCopyToSysClipboard(&gEditor.clipboard);
Expand Down Expand Up @@ -887,22 +891,22 @@ void editorProcessKeypress(void) {

should_record_action = true;

getSelectStartEnd(&action->deleted_range);
getSelectStartEnd(&edit->deleted_range);

if (gCurFile->cursor.is_selected) {
editorCopyText(&action->deleted_text, action->deleted_range);
editorDeleteText(action->deleted_range);
editorCopyText(&edit->deleted_text, edit->deleted_range);
editorDeleteText(edit->deleted_range);
gCurFile->cursor.is_selected = false;
}

action->added_range.start_x = gCurFile->cursor.x;
action->added_range.start_y = gCurFile->cursor.y;
edit->added_range.start_x = gCurFile->cursor.x;
edit->added_range.start_y = gCurFile->cursor.y;
editorPasteText(&gEditor.clipboard, gCurFile->cursor.x,
gCurFile->cursor.y);

action->added_range.end_x = gCurFile->cursor.x;
action->added_range.end_y = gCurFile->cursor.y;
editorCopyText(&action->added_text, action->added_range);
edit->added_range.end_x = gCurFile->cursor.x;
edit->added_range.end_y = gCurFile->cursor.y;
editorCopyText(&edit->added_text, edit->added_range);
} break;

// Undo
Expand Down Expand Up @@ -1078,18 +1082,17 @@ void editorProcessKeypress(void) {
case SHIFT_ALT_DOWN:
should_record_action = true;
gCurFile->cursor.is_selected = false;
action->old_cursor.is_selected = 0;
edit->old_cursor.is_selected = 0;
editorInsertRow(gCurFile, gCurFile->cursor.y,
gCurFile->row[gCurFile->cursor.y].data,
gCurFile->row[gCurFile->cursor.y].size);

action->added_range.start_x =
gCurFile->row[gCurFile->cursor.y].size;
action->added_range.start_y = gCurFile->cursor.y;
action->added_range.end_x =
edit->added_range.start_x = gCurFile->row[gCurFile->cursor.y].size;
edit->added_range.start_y = gCurFile->cursor.y;
edit->added_range.end_x =
gCurFile->row[gCurFile->cursor.y + 1].size;
action->added_range.end_y = gCurFile->cursor.y + 1;
editorCopyText(&action->added_text, action->added_range);
edit->added_range.end_y = gCurFile->cursor.y + 1;
editorCopyText(&edit->added_text, edit->added_range);

if (c == SHIFT_ALT_DOWN)
gCurFile->cursor.y++;
Expand Down Expand Up @@ -1121,27 +1124,24 @@ void editorProcessKeypress(void) {
if (c == ALT_UP) {
range.start_y--;
range.end_x = gCurFile->row[range.end_y].size;
editorCopyText(&action->added_text, range);
editorCopyText(&edit->added_text, range);
// Move empty string at the start to the end
char* temp = action->added_text.data[0];
memmove(&action->added_text.data[0],
&action->added_text.data[1],
(action->added_text.size - 1) * sizeof(char*));
action->added_text.data[action->added_text.size - 1] = temp;
char* temp = edit->added_text.data[0];
memmove(&edit->added_text.data[0], &edit->added_text.data[1],
(edit->added_text.size - 1) * sizeof(char*));
edit->added_text.data[edit->added_text.size - 1] = temp;
} else {
range.end_x = 0;
range.end_y++;
editorCopyText(&action->added_text, range);
editorCopyText(&edit->added_text, range);
// Move empty string at the end to the start
char* temp =
action->added_text.data[action->added_text.size - 1];
memmove(&action->added_text.data[1],
&action->added_text.data[0],
(action->added_text.size - 1) * sizeof(char*));
action->added_text.data[0] = temp;
char* temp = edit->added_text.data[edit->added_text.size - 1];
memmove(&edit->added_text.data[1], &edit->added_text.data[0],
(edit->added_text.size - 1) * sizeof(char*));
edit->added_text.data[0] = temp;
}
action->deleted_range = range;
editorCopyText(&action->deleted_text, range);
edit->deleted_range = range;
editorCopyText(&edit->deleted_text, range);
editorDeleteText(range);

if (c == ALT_UP) {
Expand All @@ -1155,10 +1155,10 @@ void editorProcessKeypress(void) {

range.start_x = paste_x;
range.start_y = gCurFile->cursor.y;
editorPasteText(&action->added_text, paste_x, gCurFile->cursor.y);
editorPasteText(&edit->added_text, paste_x, gCurFile->cursor.y);
range.end_x = gCurFile->cursor.x;
range.end_y = gCurFile->cursor.y;
action->added_range = range;
edit->added_range = range;

gCurFile->cursor.x = old_cx;
gCurFile->cursor.y = old_cy;
Expand Down Expand Up @@ -1333,17 +1333,17 @@ void editorProcessKeypress(void) {
c = input.data.unicode;
should_record_action = true;

getSelectStartEnd(&action->deleted_range);
getSelectStartEnd(&edit->deleted_range);

if (gCurFile->cursor.is_selected) {
editorCopyText(&action->deleted_text, action->deleted_range);
editorDeleteText(action->deleted_range);
editorCopyText(&edit->deleted_text, edit->deleted_range);
editorDeleteText(edit->deleted_range);
gCurFile->cursor.is_selected = false;
}

int x_offset = 0;
action->added_range.start_x = gCurFile->cursor.x;
action->added_range.start_y = gCurFile->cursor.y;
edit->added_range.start_x = gCurFile->cursor.x;
edit->added_range.start_y = gCurFile->cursor.y;

int close_bracket = isOpenBracket(c);
int open_bracket = isCloseBracket(c);
Expand Down Expand Up @@ -1386,9 +1386,9 @@ void editorProcessKeypress(void) {
editorInsertUnicode(c);
}

action->added_range.end_x = gCurFile->cursor.x + x_offset;
action->added_range.end_y = gCurFile->cursor.y;
editorCopyText(&action->added_text, action->added_range);
edit->added_range.end_x = gCurFile->cursor.x + x_offset;
edit->added_range.end_y = gCurFile->cursor.y;
editorCopyText(&edit->added_text, edit->added_range);

gCurFile->sx = editorRowCxToRx(&gCurFile->row[gCurFile->cursor.y],
gCurFile->cursor.x);
Expand Down Expand Up @@ -1418,7 +1418,7 @@ void editorProcessKeypress(void) {
mouse_click = 0;

if (should_record_action) {
action->new_cursor = gCurFile->cursor;
edit->new_cursor = gCurFile->cursor;
editorAppendAction(action);
} else {
editorFreeAction(action);
Expand Down

0 comments on commit ba9f1a3

Please sign in to comment.