-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2
More file actions
94 lines (92 loc) · 3.89 KB
/
2
File metadata and controls
94 lines (92 loc) · 3.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include "vim_insert_mode.h"
#include "../messages.h"
#include "../formatting/parse_expr.h"
#define DELETE_KEY 127
str_res parseInputFormatted(Cellulose *client, coord_int_t x, coord_int_t y, str *cell_input) {
str_res expanded_str = strNew();
// return early
if (expanded_str.result != str_no_error)
return expanded_str;
for (size_t char_index = 2; char_index < cell_input->len; ++char_index) {
if (char_index > 2 && cell_input->contents[char_index - 1] == '%' ) {
switch (cell_input->contents[char_index]) {
case 'x': appendNum(&expanded_str.string, x); break;
case 'y': appendNum(&expanded_str.string, y); break;
case '[': {
++char_index;
instructions_t byte_code = compileExpr(cell_input->contents, &char_index, x + 1, y + 1);
int res = 0;
cell_num_t value = evaluateByteCode(&byte_code, &res);
appendDouble(&expanded_str.string, value);
VEC_FREE(byte_code);
} break;
}
} else if (cell_input->contents[char_index] != '%') {
pushChar(&expanded_str.string, cell_input->contents[char_index] );
}
}
return expanded_str;
}
int setSelectedCells(Cellulose *client, coord_int_t x, coord_int_t y, bool exists, void *cell_input) {
str* str_cell_input = (str*)cell_input;
str input;
if (str_cell_input->len > 2 && str_cell_input->contents[0] == 'f' && str_cell_input->contents[1] == '|') {
input = parseInputFormatted(client, x, y, str_cell_input).string;
} else {
input = strClone(str_cell_input).string;
}
if (exists && CELL_P(y, x).cell_type != t_empty) {
free(CELL_P(y, x).displayed_value);
if (CELL_P(y, x).cell_type == t_str)
free(CELL_P(y, x).cell_value.str);
}
setCell(client, x, y, &input );
return 0;
}
int parseCliInput(Cellulose *client, cursor_t *cursor, int input, str *cell_input) {
switch (input) {
case 10: {
if (cursor->mode == COMMAND_MODE) {
cursor->mode = NORMAL_MODE;
int res = runCommand(client, cursor, cell_input->contents);
free(cell_input->contents);
cell_input->contents = NULL;
return res;
}
cursor->mode = NORMAL_MODE;
// checks to see if there are value in the cell being replaced and frees them if there are
if (cellExist(client, cursor->x, cursor->y ) && CUR_CELL_P.cell_type != t_empty ) {
free(CUR_CELL_P.displayed_value);
// free the actual value if the cell type is str because that is stored on the heap
if (CUR_CELL_P.cell_type == t_str)
free(CUR_CELL_P.cell_value.str);
CUR_CELL_P.cell_type = t_empty;
}
// sets all of the cells selected to the inputed value
if (cursor->visual_state != visual_state_NONE)
iterSelectedCells(client, cursor, &setSelectedCells, cell_input),
client->redraw_spreadsheet = true,
cursor->visual_state = visual_state_NONE,
free(cell_input->contents),
cell_input->contents = NULL;
else
setCell(client, cursor->x, cursor->y, cell_input);
} break;
case DELETE_KEY:
move(CLIENT_SHEET_HEIGHT + 1, 0);
printw(COLUMN_HEADER_BG);
client->redraw_cli = true;
popFront(cell_input);
break;
case 27:
cursor->mode = NORMAL_MODE;
free(cell_input->contents);
cell_input->contents = NULL;
break;
default: {
if (pushChar(cell_input, input ) == -1)
exit(1); // right now just exit with an error
} break;
}
return 0;
}