ref: 25c64d5eb622d0bb801bf3bc2453b4cb075ee63b
parent: de871ed7ecaed3a301b4007f1f49b3320abfe64d
author: glenda <glenda@krsna>
date: Mon Aug 18 20:47:32 EDT 2025
fix-blunders
--- a/fc.c
+++ b/fc.c
@@ -31,11 +31,12 @@
#define DEFAULT_PARAGRAPH_H 256
#define DEFAULT_BOXWIDTH 128
#define DEFAULT_BOXHEIGHT 32
-#define DEFAULT_MAXFORMULA 256
-#define DEFAULT_MAXCONTENT 512
-#define DEFAULT_MAXBOXES 300
+#define MAXFORMULA 256
+#define MAXCONTENT 384
+#define MAXBOXES 300
+
enum {
T_TEXT = 0,
T_NUMBER,
@@ -115,9 +116,6 @@
int redraw_interval;
int gridsnap;
int gridsize;
- int max_boxes;
- int max_formula;
- int max_content;
int max_eval_depth;
int max_recalc_passes;
int formula_precision;
@@ -139,8 +137,8 @@
struct Box {
Point pos;
Rectangle r;
- char content[DEFAULT_MAXCONTENT];
- char formula[DEFAULT_MAXFORMULA];
+ char content[MAXCONTENT];
+ char formula[MAXFORMULA];
char label[32];
int type;
double value;
@@ -152,11 +150,11 @@
typedef struct Sheet Sheet;
struct Sheet {
- Box boxes[DEFAULT_MAXBOXES];
+ Box boxes[MAXBOXES];
int nboxes;
int selected;
int editing;
- char editbuf[DEFAULT_MAXCONTENT];
+ char editbuf[MAXCONTENT];
int editpos;
int editing_label;
char labelbuf[32];
@@ -196,7 +194,7 @@
} range;
int op;
int func;
- char str[DEFAULT_MAXCONTENT];
+ char str[MAXCONTENT];
};
};
@@ -380,9 +378,6 @@
{"redraw_interval", CFG_INT, &config.redraw_interval, 0, nil},
{"gridsize", CFG_INT, &config.gridsize, 0, nil},
{"gridsnap", CFG_BOOL, &config.gridsnap, 0, nil},
- {"max_boxes", CFG_INT, &config.max_boxes, 0, nil},
- {"max_formula", CFG_INT, &config.max_formula, 0, nil},
- {"max_content", CFG_INT, &config.max_content, 0, nil},
{"max_eval_depth", CFG_INT, &config.max_eval_depth, 0, nil},
{"max_recalc_passes", CFG_INT, &config.max_recalc_passes, 0, nil},
{"formula_precision", CFG_INT, &config.formula_precision, 0, update_formula_format},
@@ -955,13 +950,13 @@
Box *box = &sheet.boxes[sheet.nboxes-1];
char *content = line + 10;
- if(formula_len > 0 && formula_len < config.max_formula){
+ if(formula_len > 0 && formula_len < MAXFORMULA){
strncpy(box->formula, content, formula_len);
box->formula[formula_len] = '\0';
formula_len = 0;
} else {
- strncpy(box->formula, content, config.max_formula-1);
- box->formula[config.max_formula-1] = '\0';
+ strncpy(box->formula, content, MAXFORMULA-1);
+ box->formula[MAXFORMULA-1] = '\0';
}
}
continue;
@@ -987,7 +982,7 @@
nf = tokenize(line, fields, nelem(fields));
if(nf >= 2 && strcmp(fields[0], "box") == 0){
- if(sheet.nboxes < config.max_boxes){
+ if(sheet.nboxes < MAXBOXES){
Box *box = &sheet.boxes[sheet.nboxes];
memset(box, 0, sizeof(Box));
box->r = Rect(0, 0, config.box_width, config.box_height);
@@ -998,7 +993,7 @@
box->pos.x = atoi(fields[1]);
box->pos.y = atoi(fields[2]);
box->r = Rect(box->pos.x, box->pos.y,
- box->pos.x + config.box_width, box->pos.y + config.box_height);
+ box->pos.x + config.box_width, box->pos.y + config.box_height);
} else if(nf >= 2 && strcmp(fields[0], "type") == 0){
int type = atoi(fields[1]);
if (type >= 0 && type < MAXBOXTYPES) {
@@ -1005,7 +1000,6 @@
Box *box = &sheet.boxes[sheet.nboxes-1];
box->type = type;
- // Resize box if it's a paragraph
if(type == T_PARAGRAPH){
box->r = Rect(box->pos.x, box->pos.y,
box->pos.x + config.paragraph_width,
@@ -1070,9 +1064,6 @@
Bprint(b, "redraw_interval %d\n", config.redraw_interval);
Bprint(b, "gridsize %d\n", config.gridsize);
Bprint(b, "gridsnap %d\n", config.gridsnap);
- Bprint(b, "max_boxes %d\n", config.max_boxes);
- Bprint(b, "max_formula %d\n", config.max_formula);
- Bprint(b, "max_content %d\n", config.max_content);
Bprint(b, "max_eval_depth %d\n", config.max_eval_depth);
Bprint(b, "max_recalc_passes %d\n", config.max_recalc_passes);
Bprint(b, "formula_precision %d\n", config.formula_precision);
@@ -1115,9 +1106,6 @@
config.redraw_interval = DEFAULT_REDRAW_INTERVAL;
config.gridsnap = 1;
config.gridsize = 32;
- config.max_boxes = DEFAULT_MAXBOXES;
- config.max_formula = DEFAULT_MAXFORMULA;
- config.max_content = DEFAULT_MAXCONTENT;
config.max_eval_depth = DEFAULT_MAX_EVAL_DEPTH;
config.max_recalc_passes = DEFAULT_MAX_RECALC_PASSES;
config.formula_precision = DEFAULT_FORMULA_PRECISION;
@@ -1262,15 +1250,6 @@
if(config.formula_precision < 0) config.formula_precision = 0;
if(config.formula_precision > 10) config.formula_precision = 10;
-
- if(config.max_boxes < 3) config.max_boxes = 3;
- if(config.max_boxes > 1000) config.max_boxes = 1000;
-
- if(config.max_formula < 256) config.max_formula = 256;
- if(config.max_formula > 512) config.max_formula = 512;
-
- if(config.max_content < 32) config.max_content = 32;
- if(config.max_content > 1024) config.max_content = 1024;
if(config.max_eval_depth < 1) config.max_eval_depth = 1;
if(config.max_eval_depth > 100) config.max_eval_depth = 100;
@@ -1500,7 +1479,7 @@
if(*p == '"') {
p++;
i = 0;
- while(*p && *p != '"' && i < config.max_content-1)
+ while(*p && *p != '"' && i < MAXCONTENT-1)
t->str[i++] = *p++;
t->str[i] = '\0';
if(*p == '"')
@@ -1990,10 +1969,10 @@
if(*endp == '\0') {
b->type = T_NUMBER;
b->value = val;
- snprint(b->content, config.max_content, config.formula_format, val);
+ snprint(b->content, MAXCONTENT, config.formula_format, val);
} else {
b->type = T_TEXT;
- strncpy(b->content, b->formula, config.max_content);
+ strncpy(b->content, b->formula, MAXCONTENT);
}
return;
}
@@ -2004,7 +1983,7 @@
e.depth = 0;
e.pos = 0;
e.ntokens = tokenize_formula(b->formula, e.tokens, nelem(e.tokens));
- strncpy(b->content, b->formula, config.max_content);
+ strncpy(b->content, b->formula, MAXCONTENT);
b->dirty = 1;
b->nrefs = 0;
@@ -2037,11 +2016,11 @@
b->value = eval_expr(&e);
if(b->formula[0] == '=' && b->formula[1] == '"') {
- strncpy(b->content, b->formula + 2, config.max_content);
+ strncpy(b->content, b->formula + 2, MAXCONTENT);
char *p = strchr(b->content, '"');
if(p) *p = '\0';
} else {
- snprint(b->content, config.max_content, config.formula_format, b->value);
+ snprint(b->content, MAXCONTENT, config.formula_format, b->value);
}
b->dirty = 0;
@@ -2103,7 +2082,7 @@
{
Box *b;
- if(sheet.nboxes >= config.max_boxes)
+ if(sheet.nboxes >= MAXBOXES)
return -1;
b = &sheet.boxes[sheet.nboxes];
@@ -2148,17 +2127,17 @@
parse_paragraph(Box *b)
{
if(b->formula[0] == '"'){
- strncpy(b->content, b->formula + 1, config.max_content - 1);
+ strncpy(b->content, b->formula + 1, MAXCONTENT-1);
} else {
- strncpy(b->content, b->formula, config.max_content - 1);
+ strncpy(b->content, b->formula, MAXCONTENT-1);
}
- b->content[config.max_content-1] = '\0';
+ b->content[MAXCONTENT-1] = '\0';
}
void
parse_text(Box *b)
{
- strncpy(b->content, b->formula, config.max_content);
+ strncpy(b->content, b->formula, MAXCONTENT);
}
void
@@ -2166,7 +2145,7 @@
{
char *endp;
b->value = strtod(b->formula, &endp);
- snprint(b->content, config.max_content, config.formula_format, b->value);
+ snprint(b->content, MAXCONTENT, config.formula_format, b->value);
}
void
@@ -2221,11 +2200,49 @@
}
Point p = addpt(b->r.min, Pt(config.box_text_margin, config.box_label_offset_y));
+ char *text_to_draw = (sheet.editing == idx) ? sheet.editbuf : b->content;
+ int max_width = Dx(b->r) - (2 * config.box_text_margin);
+ int line_height = font->height;
+ char *text = text_to_draw;
+ char *line_start = text;
+ char line_buffer[MAXCONTENT];
- if(sheet.editing == idx){
- string(dst, p, colors[0], ZP, font, sheet.editbuf);
- } else {
- string(dst, p, colors[0], ZP, font, b->content);
+ while (*line_start) {
+ char *word_ptr = line_start;
+ char *line_end = line_start;
+
+ while (*word_ptr) {
+ char *word_start = word_ptr;
+ while(*word_ptr && !isspace(*word_ptr))
+ word_ptr++;
+
+ int word_len = word_ptr - line_start;
+ if(word_len >= MAXCONTENT) break;
+
+ strncpy(line_buffer, line_start, word_len);
+ line_buffer[word_len] = '\0';
+
+ if(stringwidth(font, line_buffer) > max_width) {
+ if(line_end == line_start)
+ line_end = word_start;
+ break;
+ }
+ line_end = word_ptr;
+
+ while(*word_ptr && isspace(*word_ptr))
+ word_ptr++;
+ }
+
+ int len = line_end - line_start;
+ stringn(dst, p, colors[0], ZP, font, line_start, len);
+ p.y += line_height;
+
+ if (p.y > b->r.max.y - config.box_text_margin - line_height)
+ break;
+
+ line_start = line_end;
+ while(*line_start && isspace(*line_start))
+ line_start++;
}
if (b->type == T_FORMULA && config.show_formula_indicator){
@@ -2381,7 +2398,6 @@
Image *bg = boxbg;
int idx = b - sheet.boxes;
- /* Determine background color based on state (editing, selected) */
if(sheet.editing == idx)
bg = boxediting;
else if(sheet.editing_label == idx)
@@ -2389,11 +2405,9 @@
else if(b->selected)
bg = boxselected;
- /* Draw box background and border */
draw(dst, b->r, bg, nil, ZP);
border(dst, b->r, 1, colors[0], ZP);
- /* Draw the cell's label or default name */
char cellname[32];
if(sheet.editing_label == idx){
snprint(cellname, sizeof(cellname), "%s", sheet.labelbuf);
@@ -2410,7 +2424,6 @@
string(dst, Pt(b->r.min.x + 2, b->r.min.y + 2), colors[3], ZP, font, cellname);
}
- /* Text wrapping logic */
Point p = addpt(b->r.min, Pt(config.box_text_margin, config.box_label_offset_y));
char *text_to_draw = (sheet.editing == idx) ? sheet.editbuf : b->content;
int max_width = Dx(b->r) - (2 * config.box_text_margin);
@@ -2418,7 +2431,7 @@
char *text = text_to_draw;
char *line_start = text;
- char line_buffer[DEFAULT_MAXCONTENT];
+ char line_buffer[MAXCONTENT];
while (*line_start) {
char *word_ptr = line_start;
@@ -2430,7 +2443,7 @@
word_ptr++;
int word_len = word_ptr - line_start;
- if(word_len >= config.max_content) break;
+ if(word_len >= MAXCONTENT) break;
strncpy(line_buffer, line_start, word_len);
line_buffer[word_len] = '\0';
@@ -2877,12 +2890,12 @@
if(key == '\n') {
strcpy(b->formula, sheet.editbuf);
+
if(sheet.editbuf[0] == '=') {
b->type = T_FORMULA;
} else if(sheet.editbuf[0] == '"') {
b->type = T_PARAGRAPH;
- /* Add this line to resize the box */
b->r = Rect(b->pos.x, b->pos.y, b->pos.x + config.paragraph_width, b->pos.y + config.paragraph_height);
} else {
char *endp;
@@ -2889,6 +2902,7 @@
strtod(sheet.editbuf, &endp);
b->type = (*endp == '\0') ? T_NUMBER : T_TEXT;
}
+
if (b->type >= 0 && b->type < MAXBOXTYPES) {
BoxType *bt = &boxtypes[b->type];
if (bt->parse) bt->parse(b);
@@ -2909,7 +2923,7 @@
sheet.editbuf[sheet.editpos] = '\0';
sheet.needredraw = 1;
}
- } else if(key >= 32 && key < 127 && sheet.editpos < config.max_content-1) {
+ } else if(key >= 32 && key < 127 && sheet.editpos < MAXCONTENT-1) {
sheet.editbuf[sheet.editpos++] = key;
sheet.editbuf[sheet.editpos] = '\0';
sheet.needredraw = 1;
@@ -3035,13 +3049,20 @@
void
handle_edit_mouse(Mouse m)
{
+ Box *b = &sheet.boxes[sheet.editing];
+
if(m.buttons & 4){
- Box *b = &sheet.boxes[sheet.editing];
+ if(b->type == T_PARAGRAPH && sheet.editbuf[0] != '"') {
+ snprint(b->formula, MAXFORMULA, "\"%s\"", sheet.editbuf);
+ } else {
+ strcpy(b->formula, sheet.editbuf);
+ }
- strcpy(b->formula, sheet.editbuf);
-
if(sheet.editbuf[0] == '=') {
b->type = T_FORMULA;
+ } else if(sheet.editbuf[0] == '"') {
+ b->type = T_PARAGRAPH;
+ b->r = Rect(b->pos.x, b->pos.y, b->pos.x + config.paragraph_width, b->pos.y + config.paragraph_height);
} else {
char *endp;
strtod(sheet.editbuf, &endp);
@@ -3092,8 +3113,8 @@
int idx = addbox(p);
if(idx >= 0 && nargs > 2) {
- strncpy(sheet.boxes[idx].formula, args[2], config.max_formula-1);
- sheet.boxes[idx].formula[config.max_formula-1] = '\0';
+ strncpy(sheet.boxes[idx].formula, args[2], MAXFORMULA-1);
+ sheet.boxes[idx].formula[MAXFORMULA-1] = '\0';
BoxType *bt = &boxtypes[sheet.boxes[idx].type];
if(bt->parse) bt->parse(&sheet.boxes[idx]);
--
⑨