Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: configure decimal_precision & column_width #885

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/cmds/cmds.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/cmds/cmds_normal.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/cmds/cmds_visual.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 2 additions & 0 deletions src/conf.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
7 changes: 7 additions & 0 deletions src/doc
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
10 changes: 5 additions & 5 deletions src/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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++)
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion src/formats/xls.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/formats/xlsx.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
25 changes: 23 additions & 2 deletions src/gram.y
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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); }
Expand Down Expand Up @@ -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);
Expand Down
16 changes: 15 additions & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();

/*
Expand All @@ -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.
*/
Expand Down
2 changes: 0 additions & 2 deletions src/sc.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

Expand Down
6 changes: 3 additions & 3 deletions src/vmtbl.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

Expand Down