чување 021fe0f7a633c38fba3bdfe0a1450ab9e96fc513
родитељ 130ffcfebc5b5aa924999ffb954a6c64dff0b211
Аутор: Страхиња Радић <contact@strahinja.org>
Датум: Sun, 10 Apr 2022 23:44:20 +0200
DrawState.in_* -> DrawState.mode
Signed-off-by: Страхиња Радић <contact@strahinja.org>
Diffstat:
| M | TODO | | | 4 | ---- |
| M | draw.c | | | 32 | ++++++++++++++------------------ |
| M | draw.h | | | 24 | +++++++++++++++++------- |
| M | sled.c | | | 69 | ++++++++++++++++++++++++++++++++++----------------------------------- |
измењених датотека: 4, додавања: 65(+), брисања: 64(-)
diff --git a/TODO b/TODO
@@ -1,12 +1,8 @@
TODO
====
- [x] Word wrap
-
[ ] Replace (patch)
- [x] Cut
-
[~] Code optimization (struct coord...)
diff --git a/draw.c b/draw.c
@@ -109,11 +109,10 @@ init_drawstate(struct DrawState* state, char* error, char* action,
state->input_dialog_width = calculate_input_dialog_width(state);
if (!skip_paste_and_mode_init)
{
- state->paste = NULL;
- state->paste_count = 0;
- state->paste_size = 0;
- state->in_overwrite_mode = 0;
- state->in_command_mode = 0;
+ state->paste = NULL;
+ state->paste_count = 0;
+ state->paste_size = 0;
+ CLEAR(state->mode, M_OVERWRITE | M_COMMAND);
}
state->prompt = prompt;
state->prompt_callback = NULL;
@@ -122,11 +121,8 @@ init_drawstate(struct DrawState* state, char* error, char* action,
state->running = 1;
state->dirty = 0;
state->recalculate_size = 1;
- state->in_prompt = 0;
- state->in_selection_mode = 0;
- state->show_help = 0;
- state->show_input_dialog = 0;
- state->input_numerical = 0;
+ CLEAR(state->mode,
+ M_HELP | M_INPUT | M_INPUT_NUM | M_PROMPT | M_SELECTION);
init_coord(&state->sel_start, 0, 0);
init_coord(&state->sel_end, 0, 0);
}
@@ -183,15 +179,15 @@ format_mode(char* result, size_t result_size, const char* format,
const char* mode_label = mode_labels[UNKNOWN_MODE];
if (!result)
return;
- if (state->show_help)
+ if (IN(state->mode, M_HELP))
mode_label = mode_labels[HELP_MODE];
- else if (state->in_prompt)
+ else if (IN(state->mode, M_PROMPT))
mode_label = mode_labels[PROMPT_MODE];
- else if (state->in_selection_mode)
+ else if (IN(state->mode, M_SELECTION))
mode_label = mode_labels[SELECTION_MODE];
- else if (state->in_overwrite_mode)
+ else if (IN(state->mode, M_OVERWRITE))
mode_label = mode_labels[OVERWRITE_MODE];
- else if (state->in_command_mode)
+ else if (IN(state->mode, M_COMMAND))
mode_label = mode_labels[COMMAND_MODE];
else
mode_label = mode_labels[INSERT_MODE];
@@ -731,7 +727,7 @@ draw_status(const struct DrawState* state)
int current_start = 0;
const struct StatusSegment* pseg = status_segments;
- if (state->in_prompt && *state->prompt)
+ if (IN(state->mode, M_PROMPT) && *state->prompt)
{
if (!draw_string(0, state->maxy - 1, PROMPT_FG, PROMPT_BG,
state->prompt, strlen(state->prompt) + 2,
@@ -810,11 +806,11 @@ draw(const struct DrawState* state)
if (!draw_screen(state))
return NULL;
- if (state->show_help)
+ if (IN(state->mode, M_HELP))
if (!draw_help(state))
return NULL;
- if (state->show_input_dialog)
+ if (IN(state->mode, M_INPUT))
if (!draw_input_dialog(state))
return NULL;
diff --git a/draw.h b/draw.h
@@ -21,6 +21,22 @@
#define TAB_SIZE 8
#define MAXINPUT (512 + 1)
+#define M_NONE 0
+#define M_COMMAND 1
+#define M_OVERWRITE 1 << 1
+#define M_SELECTION 1 << 2
+#define M_PROMPT 1 << 3
+#define M_HELP 1 << 4
+#define M_INPUT 1 << 5
+#define M_INPUT_NUM 1 << 6
+
+/* clang-format off */
+#define CLEAR(v, c) do { (v) &= ~(c); } while(0)
+#define IN(v, c) (((v) & (c)) == (c))
+#define SET(v, c) do { (v) |= (c); } while(0)
+#define TOGGLE(v, c) do { (v) ^= (c); } while(0)
+/* clang-format on */
+
typedef enum { LEFT, CENTER, RIGHT } Alignment;
struct Coord {
@@ -64,13 +80,7 @@ struct DrawState {
int running;
int dirty;
int recalculate_size;
- int in_prompt;
- int in_command_mode;
- int in_overwrite_mode;
- int in_selection_mode;
- int show_help;
- int show_input_dialog;
- int input_numerical;
+ int mode;
int maxx;
int maxy;
struct Coord sel_start;
diff --git a/sled.c b/sled.c
@@ -80,7 +80,7 @@ clear_selection(struct DrawState* state)
{
if (!state)
return 1;
- state->in_selection_mode = 0;
+ CLEAR(state->mode, M_SELECTION);
init_coord(&state->sel_start, 0, 0);
init_coord(&state->sel_end, 0, 0);
return 0;
@@ -229,13 +229,13 @@ handle_key_event(struct tb_event* ev, struct DrawState* state)
*state->error = 0;
- if (state->in_prompt)
+ if (IN(state->mode, M_PROMPT))
{
if (ev->ch == 'y' || ev->ch == 'n')
{
if (state->prompt_callback)
state->prompt_callback(state, ev);
- state->in_prompt = 0;
+ CLEAR(state->mode, M_PROMPT);
*state->prompt = 0;
}
else if (ev->key == TB_KEY_ESC)
@@ -248,10 +248,10 @@ handle_key_event(struct tb_event* ev, struct DrawState* state)
if (ev->key != TB_KEY_ARROW_DOWN && ev->key != TB_KEY_ARROW_UP
&& ev->key != TB_KEY_PGDN && ev->key != TB_KEY_PGUP
&& ev->key != TB_KEY_CTRL_B && ev->key != TB_KEY_CTRL_F
- && !(state->in_command_mode && strchr("jkgGHLM", ev->ch)))
+ && !(IN(state->mode, M_COMMAND) && strchr("jkgGHLM", ev->ch)))
state->saved_column = -1;
- if (state->show_input_dialog)
+ if (IN(state->mode, M_INPUT))
{
for (int key = 0; key < LEN(input_dialog_keys); key++)
{
@@ -265,11 +265,11 @@ handle_key_event(struct tb_event* ev, struct DrawState* state)
}
if (ev->ch)
{
- if ((state->input_numerical
+ if ((IN(state->mode, M_INPUT_NUM)
&& u32_strchr(
(const uint32_t*)L"0123456789",
ev->ch))
- || !state->input_numerical)
+ || !IN(state->mode, M_INPUT_NUM))
input_insert_char(state, ev->ch);
else
strcpy(state->error, errors[ERR_INPUT_NONNUM]);
@@ -286,7 +286,7 @@ handle_key_event(struct tb_event* ev, struct DrawState* state)
return;
}
- if (state->show_help)
+ if (IN(state->mode, M_HELP))
{
strcpy(state->error, errors[ERR_DLG_OPEN]);
return;
@@ -301,7 +301,7 @@ handle_key_event(struct tb_event* ev, struct DrawState* state)
return;
}
- if (state->in_command_mode)
+ if (IN(state->mode, M_COMMAND))
{
for (int key = 0; key < LEN(command_keys); key++)
{
@@ -400,14 +400,14 @@ insert_char(struct DrawState* state, uint32_t ch)
len = buffer_line->length;
i = len + 1;
- if (!state->in_overwrite_mode)
+ if (!IN(state->mode, M_OVERWRITE))
while (i < MAXBUFLINE - 2 && i > state->coord.col)
{
buffer_line->text[i] = buffer_line->text[i - 1];
i--;
}
buffer_line->text[state->coord.col] = ch;
- if (!state->in_overwrite_mode)
+ if (!IN(state->mode, M_OVERWRITE))
buffer_line->length++;
position_to(state, state->coord.row, state->coord.col + 1);
if (add_null)
@@ -643,7 +643,7 @@ position_to(struct DrawState* state, const int row, const int column)
state->first_shown.col
= state->display_column - (state->maxx - 1);
- if (state->in_selection_mode)
+ if (IN(state->mode, M_SELECTION))
assign_coord(&state->sel_end, state->coord);
}
@@ -836,7 +836,8 @@ search_update_callback(struct DrawState* state)
int
cancel_close(struct DrawState* state)
{
- if (!*state->error && !state->in_prompt && !state->show_help)
+ if (!*state->error && !IN(state->mode, M_PROMPT)
+ && !IN(state->mode, M_HELP))
{
if (SEL_NOT_EMPTY(state))
clear_selection(state);
@@ -847,8 +848,7 @@ cancel_close(struct DrawState* state)
{
*state->error = 0;
*state->prompt = 0;
- state->in_prompt = 0;
- state->show_help = 0;
+ CLEAR(state->mode, M_PROMPT | M_HELP);
}
return 0;
@@ -869,7 +869,7 @@ enter_command_mode(struct DrawState* state)
{
if (!state)
return 1;
- state->in_command_mode = 1;
+ SET(state->mode, M_COMMAND);
return 0;
}
@@ -878,7 +878,7 @@ enter_insert_mode(struct DrawState* state)
{
if (!state)
return 1;
- state->in_command_mode = 0;
+ CLEAR(state->mode, M_COMMAND);
return 0;
}
@@ -1066,7 +1066,7 @@ exit_program(struct DrawState* state)
{
if (state->dirty)
{
- state->in_prompt = 1;
+ SET(state->mode, M_PROMPT);
strcpy(state->prompt, prompt_dirty);
state->prompt_callback = quit_callback;
}
@@ -1084,8 +1084,8 @@ input_cancel(struct DrawState* state)
*state->error = 0;
return 0;
}
- state->show_input_dialog = 0;
- *state->input = 0;
+ CLEAR(state->mode, M_INPUT);
+ *state->input = 0;
if (state->input_update_callback)
state->input_update_callback(state);
state->input_callback = NULL;
@@ -1100,10 +1100,10 @@ input_enter(struct DrawState* state)
{
state->input_callback(state);
if (!*state->error)
- state->show_input_dialog = 0;
+ CLEAR(state->mode, M_INPUT);
}
else
- state->show_input_dialog = 0;
+ CLEAR(state->mode, M_INPUT);
return 0;
}
@@ -1633,7 +1633,7 @@ reread_or_callback(struct DrawState* state)
return 1;
if (state->dirty)
{
- state->in_prompt = 1;
+ SET(state->mode, M_PROMPT);
strcpy(state->prompt, prompt_reread_dirty);
state->prompt_callback = reread_callback;
}
@@ -1656,8 +1656,7 @@ show_go_to_line(struct DrawState* state)
return 0;
}
}
- state->input_numerical = 1;
- state->show_input_dialog = 1;
+ SET(state->mode, M_INPUT | M_INPUT_NUM);
*state->input = 0;
*state->error = 0;
input_position_to(state, 0);
@@ -1689,10 +1688,10 @@ show_search(struct DrawState* state)
return 0;
}
}
- *state->input = 0;
- state->input_numerical = 0;
- state->show_input_dialog = 1;
- *state->error = 0;
+ *state->input = 0;
+ SET(state->mode, M_INPUT);
+ CLEAR(state->mode, M_INPUT_NUM);
+ *state->error = 0;
input_position_to(state, 0);
return 0;
}
@@ -1700,7 +1699,7 @@ show_search(struct DrawState* state)
int
show_help(struct DrawState* state)
{
- state->show_help = 1;
+ SET(state->mode, M_HELP);
*state->error = 0;
return 0;
}
@@ -1866,17 +1865,17 @@ simple_yank(struct DrawState* state)
int
toggle_overwrite(struct DrawState* state)
{
- state->in_overwrite_mode = !state->in_overwrite_mode;
- if (state->in_overwrite_mode)
- state->in_command_mode = 0;
+ TOGGLE(state->mode, M_OVERWRITE);
+ if (IN(state->mode, M_OVERWRITE))
+ CLEAR(state->mode, M_COMMAND);
return 0;
}
int
toggle_selection(struct DrawState* state)
{
- state->in_selection_mode = !state->in_selection_mode;
- if (state->in_selection_mode)
+ TOGGLE(state->mode, M_SELECTION);
+ if (IN(state->mode, M_SELECTION))
assign_coord(&state->sel_start, state->coord);
assign_coord(&state->sel_end, state->coord);
return 0;