Skip to content

Commit

Permalink
Merge pull request #46 from olin/multi-line-support
Browse files Browse the repository at this point in the history
Insert multi-line support
  • Loading branch information
labseven authored May 9, 2019
2 parents 638f667 + 0b843e4 commit c2a42df
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 3 deletions.
21 changes: 18 additions & 3 deletions src/cursor.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,28 @@
* It also maps the drawing area to the canvas nicely.
*/

Cursor *cursor_new() {
/* Make a new cursor.
*
* Initialized to (0, 0).
*/
Cursor *cursor_new() { return cursor_newyx(0, 0); }

/* Make a new cursor at a given position.
*/
Cursor *cursor_newyx(int y, int x) {
Cursor *cursor = malloc(sizeof(Cursor));
cursor->x = 0;
cursor->y = 0;
cursor->x = x;
cursor->y = y;
return cursor;
}

/* Make a copy of an existing cursor.
*
*/
Cursor *cursor_copy(Cursor *original) {
return cursor_newyx(original->y, original->x);
}

void cursor_move_up(Cursor *cursor, View *view) {
if (cursor->y == 0) {
view_move_up(view);
Expand Down
2 changes: 2 additions & 0 deletions src/cursor.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ typedef struct CURSOR {
} Cursor;

Cursor *cursor_new();
Cursor *cursor_newyx(int y, int x);
Cursor *cursor_copy(Cursor *original);
void cursor_free(Cursor *cursor);

void cursor_move_up(Cursor *cursor, View *view);
Expand Down
45 changes: 45 additions & 0 deletions src/fe_modes.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ mode_brush_config_t mode_brush_config = {
.state = PAINT_OFF,
};

typedef struct {
Cursor *last_dir_change;
} mode_insert_config_t;

mode_insert_config_t mode_insert_config = {NULL};

///////////////////////
// GENERAL FUNCTIONS //
///////////////////////
Expand Down Expand Up @@ -269,11 +275,25 @@ int mode_insert(reason_t reason, State *state) {
return 0;
}

mode_insert_config_t *mode_cfg = &mode_insert_config;
// init config cursor
if (mode_cfg->last_dir_change == NULL) {
mode_cfg->last_dir_change = cursor_copy(state->cursor);
}

// watch for view shifts
const int vx = state->view->x;
const int vy = state->view->y;
// insert mode behavior
if ((state->ch_in == KEY_LEFT) || (state->ch_in == KEY_RIGHT) ||
(state->ch_in == KEY_UP) || (state->ch_in == KEY_DOWN)) {
cursor_key_to_move(state->ch_in, state->cursor, state->view);
state->last_arrow_direction = state->ch_in;

// update direction change cursor
Cursor *old = mode_cfg->last_dir_change;
mode_cfg->last_dir_change = cursor_copy(state->cursor);
cursor_free(old);
} else {
if (' ' <= state->ch_in &&
state->ch_in <= '~') { // check if ch is printable
Expand All @@ -286,8 +306,33 @@ int mode_insert(reason_t reason, State *state) {
front_setcharcursor(' ');
} else if (state->ch_in == KEY_DC) {
front_setcharcursor(' ');
} else if (state->ch_in == KEY_ENTER) {
// return to beginning of "line" on ENTER
// TODO: check if out of view and recenter
if (state->last_arrow_direction == KEY_RIGHT ||
state->last_arrow_direction == KEY_LEFT) {
// return down if left/right
state->cursor->x = mode_cfg->last_dir_change->x;
cursor_move_down(state->cursor, state->view);
mode_cfg->last_dir_change->y = state->cursor->y;
} else if (state->last_arrow_direction == KEY_UP ||
state->last_arrow_direction == KEY_DOWN) {
// return right if up/down
state->cursor->y = mode_cfg->last_dir_change->y;
cursor_move_right(state->cursor, state->view);
mode_cfg->last_dir_change->x = state->cursor->x;
}
}
}
// update last_dir_change with view diffs
const int dx = vx - state->view->x;
const int dy = vy - state->view->y;
if (dx != 0) {
mode_cfg->last_dir_change->x += dx;
}
if (dy != 0) {
mode_cfg->last_dir_change->y += dy;
}
return 0;
}

Expand Down

0 comments on commit c2a42df

Please sign in to comment.