From 665c0448ceedb668852cca520869fa635d7b343f Mon Sep 17 00:00:00 2001 From: Pim Snel Date: Wed, 14 Aug 2024 20:49:29 +0200 Subject: [PATCH] Feature: configure decimal_precision & column_width --- src/cmds/cmds.c | 8 ++++---- src/cmds/cmds_normal.c | 2 +- src/cmds/cmds_visual.c | 2 +- src/conf.c | 2 ++ src/doc | 7 +++++++ src/file.c | 10 +++++----- src/formats/xls.c | 2 +- src/formats/xlsx.c | 2 +- src/gram.y | 25 +++++++++++++++++++++++-- src/main.c | 16 +++++++++++++++- src/sc.h | 2 -- src/vmtbl.c | 6 +++--- 12 files changed, 63 insertions(+), 21 deletions(-) diff --git a/src/cmds/cmds.c b/src/cmds/cmds.c index e7863e85..63d83e77 100644 --- a/src/cmds/cmds.c +++ b/src/cmds/cmds.c @@ -320,8 +320,8 @@ void int_deletecol(struct sheet * sh, int col, int mult) { } for (; i < sh->maxcols - 1; i++) { - sh->fwidth[i] = DEFWIDTH; - sh->precision[i] = DEFPREC; + sh->fwidth[i] = get_conf_int("column_width"); + sh->precision[i] = get_conf_int("decimal_precision"); sh->realfmt[i] = DEFREFMT; sh->col_hidden[i] = FALSE; sh->col_frozen[i] = FALSE; @@ -880,8 +880,8 @@ void insert_col(struct sheet * sh, int after) { sh->col_frozen[c] = sh->col_frozen[c-1]; } for (c = sh->curcol + after; c - sh->curcol - after < 1; c++) { - sh->fwidth[c] = DEFWIDTH; - sh->precision[c] = DEFPREC; + sh->fwidth[c] = get_conf_int("column_width"); + sh->precision[c] = get_conf_int("decimal_precision"); sh->realfmt[c] = DEFREFMT; sh->col_hidden[c] = FALSE; sh->col_frozen[c] = FALSE; diff --git a/src/cmds/cmds_normal.c b/src/cmds/cmds_normal.c index fea567c7..e13251af 100644 --- a/src/cmds/cmds_normal.c +++ b/src/cmds/cmds_normal.c @@ -214,7 +214,7 @@ void do_normalmode(struct block * buf) { c = sr->tlcol; cf = sr->brcol; } - auto_fit(sh, c, cf, DEFWIDTH); // auto justify columns + auto_fit(sh, c, cf, get_conf_int("column_width")); // auto justify columns ui_update(TRUE); break; } diff --git a/src/cmds/cmds_visual.c b/src/cmds/cmds_visual.c index 19c67788..b81b181a 100644 --- a/src/cmds/cmds_visual.c +++ b/src/cmds/cmds_visual.c @@ -418,7 +418,7 @@ void do_visualmode(struct block * buf) { // auto_fit } else if (buf->value == ctl('j')) { - auto_fit(sh, r->tlcol, r->brcol, DEFWIDTH); // auto justify columns + auto_fit(sh, r->tlcol, r->brcol, get_conf_int("column_width")); // auto justify columns exit_visualmode(); chg_mode('.'); ui_show_header(); diff --git a/src/conf.c b/src/conf.c index cc1e1989..7a2df24f 100644 --- a/src/conf.c +++ b/src/conf.c @@ -69,6 +69,8 @@ const char default_config[] = "quiet=0\n" "numeric_zero=1\n" "numeric_decimal=1\n" + "decimal_precision=2\n" + "column_width=10\n" "overlap=0\n" "truncate=0\n" "autowrap=0\n" diff --git a/src/doc b/src/doc index 6ab07f1f..3c439c7e 100755 --- a/src/doc +++ b/src/doc @@ -1379,6 +1379,13 @@ Commands for handling cell content: When these are set, the zero digit or decimal point will correspondingly initiate numeric entry, but only when 'numeric' is also set. + 'column_width' [default 10] + Sets the default column width. Defaults to 10 characters. + + 'decimal_precision' [default 2] + Sets the default decimal precision for columns. + Defaults to 2 decimals, showing 1 as 1.00 + 'newline_action' [default 0] Set it to 'j' to move the cursor down after an entry. Set it to 'l' to move right, or set it to '0' to take no action. diff --git a/src/file.c b/src/file.c index fafa2d3c..036bb0bd 100644 --- a/src/file.c +++ b/src/file.c @@ -104,8 +104,8 @@ void erasedb(struct sheet * sheet, int _free) { int r, c; for (c = 0; c < sheet->maxcols; c++) { - sheet->fwidth[c] = DEFWIDTH; - sheet->precision[c] = DEFPREC; + sheet->fwidth[c] = get_conf_int("column_width"); + sheet->precision[c] = get_conf_int("decimal_precision"); sheet->realfmt[c] = DEFREFMT; } @@ -426,7 +426,7 @@ void write_fd(FILE * f, struct roman * doc) { fprintf (f, "nb_frozen_screencols %d\n", sh->nb_frozen_screencols); for (c = 0; c <= sh->maxcol; c++) - if (sh->fwidth[c] != DEFWIDTH || sh->precision[c] != DEFPREC || sh->realfmt[c] != DEFREFMT) + if (sh->fwidth[c] != get_conf_int("column_width") || sh->precision[c] != get_conf_int("decimal_precision") || sh->realfmt[c] != DEFREFMT) (void) fprintf (f, "format %s %d %d %d\n", coltoa(c), sh->fwidth[c], sh->precision[c], sh->realfmt[c]); for (r = 0; r <= sh->maxrow; r++) @@ -1203,7 +1203,7 @@ int import_csv(char * fname, char d) { roman->cur_sh->maxrow = r-1; roman->cur_sh->maxcol = cf-1; - auto_fit(roman->cur_sh, 0, roman->cur_sh->maxcols, DEFWIDTH); + auto_fit(roman->cur_sh, 0, roman->cur_sh->maxcols, get_conf_int("column_width")); fclose(f); @@ -1366,7 +1366,7 @@ int import_markdown(char * fname) { roman->cur_sh->maxrow = r-1; roman->cur_sh->maxcol = cf-1; - auto_fit(roman->cur_sh, 0, roman->cur_sh->maxcols, DEFWIDTH); + auto_fit(roman->cur_sh, 0, roman->cur_sh->maxcols, get_conf_int("column_width")); fclose(f); diff --git a/src/formats/xls.c b/src/formats/xls.c index b531de20..a79b25ac 100644 --- a/src/formats/xls.c +++ b/src/formats/xls.c @@ -174,7 +174,7 @@ int open_xls(char * fname, char * encoding) { } xls_close_WS(pWS); xls_close_WB(pWB); - auto_fit(sh, 0, sh->maxcols, DEFWIDTH); + auto_fit(sh, 0, sh->maxcols, get_conf_int("column_width")); return 0; #else return -1; diff --git a/src/formats/xlsx.c b/src/formats/xlsx.c index 614009ec..3e0aee7f 100644 --- a/src/formats/xlsx.c +++ b/src/formats/xlsx.c @@ -533,7 +533,7 @@ int open_xlsx(char * fname, char * encoding) { struct roman * roman = session->cur_doc; struct sheet * sh = roman->cur_sh; - auto_fit(sh, 0, sh->maxcols, DEFWIDTH); + auto_fit(sh, 0, sh->maxcols, get_conf_int("column_width")); deleterow(sh, sh->currow, 1); cur_node = cur_node->next; diff --git a/src/gram.y b/src/gram.y index 374c9857..e682881c 100755 --- a/src/gram.y +++ b/src/gram.y @@ -300,6 +300,8 @@ token S_YANKCOL %token K_IGNORECASE %token K_NOIGNORECASE %token K_TM_GMTOFF +%token K_COLUMN_WIDTH +%token K_DECIMAL_PRECISION %token K_COMMAND_TIMEOUT %token K_MAPPING_TIMEOUT %token K_NEWLINE_ACTION @@ -703,8 +705,8 @@ command: scxfree(tmp); } */ - | S_AUTOFIT COL ':' COL { auto_fit(session->cur_doc->cur_sh, $2, $4, DEFWIDTH); } // auto justificado de columnas - | S_AUTOFIT COL { auto_fit(session->cur_doc->cur_sh, $2, $2, DEFWIDTH); } // auto justificado de columna + | S_AUTOFIT COL ':' COL { auto_fit(session->cur_doc->cur_sh, $2, $4, get_conf_int("column_width")); } // auto justificado de columnas + | S_AUTOFIT COL { auto_fit(session->cur_doc->cur_sh, $2, $2, get_conf_int("column_width")); } // auto justificado de columna | S_PAD NUMBER COL ':' COL { pad(session->cur_doc->cur_sh, $2, 0, $3, session->cur_doc->cur_sh->maxrow, $5); } @@ -1737,6 +1739,25 @@ setitem : | K_NEWLINE_ACTION '=' NUMBER { if ($3 == 0) parse_str(user_conf_d, "newline_action=0", TRUE); } + + | K_COLUMN_WIDTH { parse_str(user_conf_d, "column_width=10", TRUE); } + | K_COLUMN_WIDTH '=' num { + char * s = scxmalloc((unsigned) BUFFERSIZE); + + sprintf(s, "column_width=%d", (int) $3); + parse_str(user_conf_d, s, TRUE); + scxfree(s); + } + + | K_DECIMAL_PRECISION { parse_str(user_conf_d, "decimal_precision=2", TRUE); } + | K_DECIMAL_PRECISION '=' num { + char * s = scxmalloc((unsigned) BUFFERSIZE); + + sprintf(s, "decimal_precision=%d", (int) $3); + parse_str(user_conf_d, s, TRUE); + scxfree(s); + } + | K_COMMAND_TIMEOUT { parse_str(user_conf_d, "command_timeout=3000", TRUE); } | K_COMMAND_TIMEOUT '=' num { char * s = scxmalloc((unsigned) BUFFERSIZE); diff --git a/src/main.c b/src/main.c index 945803da..398bf79b 100644 --- a/src/main.c +++ b/src/main.c @@ -109,7 +109,7 @@ int brokenpipe = FALSE; /* Set to true if SIGPIPE is received */ int optimize = 0; /* Causes numeric expressions to be optimizedv */ int rndtoeven = 0; int rowsinrange = 1; -int colsinrange = DEFWIDTH; +int colsinrange = 10; FILE * fdoutput; /* Output file descriptor (stdout or file) */ // used by interp @@ -268,6 +268,11 @@ int main (int argc, char ** argv) { * readfile_argv(argc, argv); */ + /* + * first call off create_empty_wb(): Initial setup of empty workbook with + * application default config values. This is run before load_rc() because + * load_rc() can change data in the initial workbook. + */ create_empty_wb(); /* @@ -278,6 +283,15 @@ int main (int argc, char ** argv) { */ load_rc(); + /* + * second call of create_empty_wb() this time the empty workbook is created + * with config values possibly overwritten by user and loaded with + * load_rc(). + * + * example in scimrc: decimal_precision=3 + */ + create_empty_wb(); + /* load file passed as argv to sc-im. * if more than one file is passed, consider the last one. */ diff --git a/src/sc.h b/src/sc.h index b8f16d52..eba970dd 100644 --- a/src/sc.h +++ b/src/sc.h @@ -68,8 +68,6 @@ #define REFMTENG 2 #define REFMTDATE 3 #define REFMTLDATE 4 -#define DEFWIDTH 10 /* Default column width and precision */ -#define DEFPREC 2 #define DEFREFMT REFMTFIX /* Make default format fixed point THA 10/14/90 */ #define FKEYS 24 /* Number of function keys available */ diff --git a/src/vmtbl.c b/src/vmtbl.c index 0dc86682..5f5aa7ac 100644 --- a/src/vmtbl.c +++ b/src/vmtbl.c @@ -154,7 +154,7 @@ int growtbl(struct sheet * sh, int rowcol, int toprow, int topcol) { int startval; startval = SC_DISPLAY_ROWS; newrows = startval > MINROWS ? startval : MINROWS; - startval = SC_DISPLAY_COLS / DEFWIDTH; + startval = SC_DISPLAY_COLS / get_conf_int("column_width"); newcols = startval > MINCOLS ? startval : MINCOLS; } #else @@ -233,8 +233,8 @@ int growtbl(struct sheet * sh, int rowcol, int toprow, int topcol) { GROWALLOC(col_hidden2, sh->col_hidden, newcols, unsigned char, nowider); memset(sh->col_hidden + sh->maxcols, 0, (newcols - sh->maxcols) * sizeof(unsigned char)); for (i = sh->maxcols; i < newcols; i++) { - sh->fwidth[i] = DEFWIDTH; - sh->precision[i] = DEFPREC; + sh->fwidth[i] = get_conf_int("column_width"); + sh->precision[i] = get_conf_int("decimal_precision"); sh->realfmt[i] = DEFREFMT; }