diff --git a/src/canvas.c b/src/canvas.c index 5493cdc..c1e074f 100644 --- a/src/canvas.c +++ b/src/canvas.c @@ -237,6 +237,8 @@ int canvas_resize(Canvas **canvas_pointer, int newrows, int newcols) { * Requires a pointer to a canvas pointer. * * Returns 1 if the canvas was truncated, 0 otherwise. + * + * TODO: update docstring */ Canvas *canvas_trimc(Canvas *orig, char ignore, bool right, bool bottom, bool left, bool top) { @@ -353,6 +355,7 @@ Canvas *canvas_trimc(Canvas *orig, char ignore, bool right, bool bottom, (right ? mr : orig->num_cols - 1)); } +// TODO: drop this or fill it out inline Canvas *canvas_trim(Canvas *orig, int right, int bottom, int left, int top) {} diff --git a/src/fe_modes.c b/src/fe_modes.c index 8d17a5f..d4f8b17 100644 --- a/src/fe_modes.c +++ b/src/fe_modes.c @@ -39,6 +39,7 @@ editor_mode_t modes[] = { {"Pan", "Pan around the canvas", mode_pan}, {"Free-Line", "Draw a line with your arrow keys", mode_free_line}, {"Brush", "Paint with arrow keys and mouse", mode_brush}, + {"Trim", "Remove empty space", mode_trim}, }; typedef struct { @@ -475,3 +476,39 @@ int mode_brush(reason_t reason, State *state) { return 0; } + +/* mode_trim + * + * Trim edges of the canvas. + * + * Select a direction to trim and + */ +int mode_trim(reason_t reason, State *state) { + if (reason == NEW_KEY) { + int dir = 0; // 0: top, 1: right, 2: down, 3: left + switch (state->ch_in) { + case KEY_UP: + dir = 0; + break; + case KEY_RIGHT: + dir = 1; + break; + case KEY_DOWN: + dir = 2; + break; + case KEY_LEFT: + dir = 3; + break; + default: + return 0; + } + Canvas *old_canvas = state->view->canvas; + // trimc order : right bottom left top + Canvas *new_canvas = + canvas_trimc(old_canvas, ' ', dir == 1, dir == 2, dir == 3, dir == 0); + state->view->canvas = new_canvas; + canvas_free(old_canvas); + redraw_canvas_win(); + } + return 0; +} diff --git a/src/fe_modes.h b/src/fe_modes.h index 39f0b0e..75bcf3e 100644 --- a/src/fe_modes.h +++ b/src/fe_modes.h @@ -17,6 +17,7 @@ mode_function_t mode_insert; mode_function_t mode_pan; mode_function_t mode_free_line; mode_function_t mode_brush; +mode_function_t mode_trim; typedef struct { char *name; diff --git a/src/mode_id.h b/src/mode_id.h index 98d806c..be363a7 100644 --- a/src/mode_id.h +++ b/src/mode_id.h @@ -12,6 +12,7 @@ typedef enum { MODE_PAN, MODE_FREE_LINE, MODE_BRUSH, + MODE_TRIM, // ^ add your mode above LAST, // used to get number of elements