diff --git a/.clang-format b/.clang-format index 126d419..6c31eac 100644 --- a/.clang-format +++ b/.clang-format @@ -2,109 +2,12 @@ # modified .clang-format from # https://github.com/torvalds/linux/blob/master/.clang-format -AccessModifierOffset: -4 +BasedOnStyle: WebKit + AlignAfterOpenBracket: Align -AlignConsecutiveAssignments: false -AlignConsecutiveDeclarations: false +AlignConsecutiveAssignments: true +AlignConsecutiveDeclarations: true AlignEscapedNewlines: Left AlignOperands: true AlignTrailingComments: false -AllowAllParametersOfDeclarationOnNextLine: false -AllowShortBlocksOnASingleLine: false -AllowShortCaseLabelsOnASingleLine: false -AllowShortFunctionsOnASingleLine: None -AllowShortIfStatementsOnASingleLine: false -AllowShortLoopsOnASingleLine: false -AlwaysBreakAfterDefinitionReturnType: true -AlwaysBreakAfterReturnType: None -AlwaysBreakBeforeMultilineStrings: false -AlwaysBreakTemplateDeclarations: false -BinPackArguments: true -BinPackParameters: true -BraceWrapping: - AfterClass: false - AfterControlStatement: false - AfterEnum: false - AfterFunction: true - AfterNamespace: true - AfterObjCDeclaration: false - AfterStruct: false - AfterUnion: false - AfterExternBlock: false - BeforeCatch: false - BeforeElse: false - IndentBraces: false - SplitEmptyFunction: true - SplitEmptyRecord: true - SplitEmptyNamespace: true -BreakBeforeBinaryOperators: None -BreakBeforeBraces: Custom -BreakBeforeInheritanceComma: false -BreakBeforeTernaryOperators: false -BreakConstructorInitializersBeforeComma: false -BreakConstructorInitializers: BeforeComma -BreakAfterJavaFieldAnnotations: false -BreakStringLiterals: false -ColumnLimit: 80 -CommentPragmas: '^ IWYU pragma:' -CompactNamespaces: false -ConstructorInitializerAllOnOneLineOrOnePerLine: false -ConstructorInitializerIndentWidth: 4 -ContinuationIndentWidth: 4 -Cpp11BracedListStyle: false -DerivePointerAlignment: false -DisableFormat: false -ExperimentalAutoDetectBinPacking: false -FixNamespaceComments: false - - -IncludeBlocks: Preserve -IncludeCategories: - - Regex: '.*' - Priority: 1 -IncludeIsMainRegex: '(Test)?$' -IndentCaseLabels: false -IndentGotoLabels: false -IndentPPDirectives: None -IndentWidth: 4 -IndentWrappedFunctionNames: false -JavaScriptQuotes: Leave -JavaScriptWrapImports: true -KeepEmptyLinesAtTheStartOfBlocks: false -MacroBlockBegin: '' -MacroBlockEnd: '' -MaxEmptyLinesToKeep: 1 -NamespaceIndentation: None -ObjCBinPackProtocolList: Auto -ObjCBlockIndentWidth: 4 -ObjCSpaceAfterProperty: true -ObjCSpaceBeforeProtocolList: true - -# Taken from git's rules -PenaltyBreakAssignment: 10 -PenaltyBreakBeforeFirstCallParameter: 30 -PenaltyBreakComment: 10 -PenaltyBreakFirstLessLess: 0 -PenaltyBreakString: 10 -PenaltyExcessCharacter: 100 -PenaltyReturnTypeOnItsOwnLine: 60 - -PointerAlignment: Right -ReflowComments: false -SortIncludes: false -SortUsingDeclarations: false -SpaceAfterCStyleCast: false -SpaceAfterTemplateKeyword: true -SpaceBeforeAssignmentOperators: true -SpaceBeforeCtorInitializerColon: true -SpaceBeforeInheritanceColon: true -SpaceBeforeParens: ControlStatementsExceptForEachMacros -SpaceBeforeRangeBasedForLoopColon: true -SpaceInEmptyParentheses: false -SpacesBeforeTrailingComments: 1 -SpacesInAngles: false -SpacesInContainerLiterals: false -SpacesInCStyleCastParentheses: false -SpacesInParentheses: false -SpacesInSquareBrackets: false -TabWidth: 4 \ No newline at end of file +PointerAlignment: Right \ No newline at end of file diff --git a/src/book.c b/src/book.c index 5dd0551..ec48dbd 100644 --- a/src/book.c +++ b/src/book.c @@ -7,7 +7,7 @@ void book_init(struct book *b, char *title) { b->n_sheets = 0; - b->sheets = malloc(0); + b->sheets = malloc(0); if (!book_add_sheet(b)) exit(1); @@ -39,7 +39,7 @@ bool book_add_sheet(struct book *b) b->n_sheets++; - void* tmp = realloc(b->sheets, sizeof(struct sheet*) * (b->n_sheets + 1)); + void *tmp = realloc(b->sheets, sizeof(struct sheet *) * (b->n_sheets + 1)); if (tmp == NULL) return false; b->sheets = tmp; @@ -49,7 +49,7 @@ bool book_add_sheet(struct book *b) s = malloc(sizeof(struct sheet)); sheet_init(s, DEFAULT_WIDTH, DEFAULT_HEIGHT, DEFAULT_SHEET_TITLE); - b->sheets[b->n_sheets-1] = s; + b->sheets[b->n_sheets - 1] = s; return true; } diff --git a/src/cell.c b/src/cell.c index 12ec5b9..cb4335e 100644 --- a/src/cell.c +++ b/src/cell.c @@ -1,6 +1,6 @@ #include "cell.h" -#include #include +#include #include void cell_parse_function(struct cell *cell); @@ -8,10 +8,10 @@ void cell_parse_function(struct cell *cell); void cell_init(struct cell *c, size_t x, size_t y, enum cell_type type) { c->referenced_by = NULL; - c->x_pos = x; - c->y_pos = y; - c->type = type; - c->text = calloc(1,1); + c->x_pos = x; + c->y_pos = y; + c->type = type; + c->text = calloc(1, 1); c->value.integer = 0; } @@ -29,15 +29,15 @@ void cell_delete(struct cell *c) void cell_set_type(struct cell *cell) { if (cell->text[0] == '=') { - //cell_parse_function(cell); + // cell_parse_function(cell); return; } char *endptr; - int i = strtoll(cell->text, &endptr, 10); + int i = strtoll(cell->text, &endptr, 10); if (*endptr == '\0') { - cell->type = Integer; + cell->type = Integer; cell->value.integer = i; return; } @@ -45,7 +45,7 @@ void cell_set_type(struct cell *cell) int d = strtold(cell->text, &endptr); if (*endptr == '\0') { - cell->type = Floating; + cell->type = Floating; cell->value.floating = d; return; } diff --git a/src/cell.h b/src/cell.h index eef842f..a350323 100644 --- a/src/cell.h +++ b/src/cell.h @@ -1,17 +1,15 @@ #ifndef CELL_H #define CELL_H +#include "common.h" #include #include #include -#include "common.h" -enum cell_type { - Text, - Integer, - Floating, - Date -}; +enum cell_type { Text, + Integer, + Floating, + Date }; struct cell_list { struct cell *cell; diff --git a/src/celltree.c b/src/celltree.c index 4a21225..b715ec7 100644 --- a/src/celltree.c +++ b/src/celltree.c @@ -1,10 +1,11 @@ #include "celltree.h" -#include -#include #include +#include +#include /* returns true if (ax,ay) `greater than` (bx,by) */ -static bool greater_than(int x_a, int y_a, int x_b, int y_b) +static bool +greater_than(int x_a, int y_a, int x_b, int y_b) { if (x_a == x_b) return y_a > y_b; @@ -25,7 +26,8 @@ void celltree_delete(struct celltree *root) free(root); } -struct cell* celltree_search(struct celltree *root, size_t x, size_t y) +struct cell * +celltree_search(struct celltree *root, size_t x, size_t y) { if (root == NULL) { return NULL; @@ -50,9 +52,9 @@ void celltree_insert(struct celltree **root, struct cell *c) *root = malloc(sizeof **root); if (*root == NULL) exit(errno); - (*root)->cell = c; + (*root)->cell = c; (*root)->right = NULL; - (*root)->left = NULL; + (*root)->left = NULL; return; } diff --git a/src/celltree.h b/src/celltree.h index 633ac99..fc0dbf6 100644 --- a/src/celltree.h +++ b/src/celltree.h @@ -1,8 +1,8 @@ #ifndef CELLTREE_H #define CELLTREE_H -#include #include "cell.h" +#include /* TODO: implement red-black tree */ struct celltree { diff --git a/src/common.h b/src/common.h index 78befa1..1cc5bbc 100644 --- a/src/common.h +++ b/src/common.h @@ -2,6 +2,8 @@ #ifndef COMMON_H #define COMMON_H -enum modes {Edit, Command, G}; +enum modes { Edit, + Command, + G }; #endif \ No newline at end of file diff --git a/src/config.h b/src/config.h index 7174bdf..38da092 100644 --- a/src/config.h +++ b/src/config.h @@ -3,12 +3,11 @@ #define CONFIG_H // strings -#define DEFAULT_BOOK_TITLE "Untitled" +#define DEFAULT_BOOK_TITLE "Untitled" #define DEFAULT_SHEET_TITLE "Sheet " -#define DEFAULT_HEIGHT 40 -#define DEFAULT_WIDTH 80 -#define MAX_STR 64 - +#define DEFAULT_HEIGHT 40 +#define DEFAULT_WIDTH 80 +#define MAX_STR 64 // dimensions #define CELL_SIZE 12 @@ -17,7 +16,6 @@ #define N_CELLS_WIDTH 5 #define N_CELLS_HEIGHT 5 - // colors #define COLOR_TITLE 20 #define COLOR_BACKGROUND 21 diff --git a/src/draw.c b/src/draw.c index e36ea02..0888804 100644 --- a/src/draw.c +++ b/src/draw.c @@ -2,16 +2,17 @@ #include "draw.h" #include "cell.h" #include "celltree.h" -#include "config.h" // colors, sizes ... -#include "sheet.h" // struct sheet -#include // ncurses functions -#include // handle sigterm -#include // strlen +#include "config.h" // colors, sizes ... +#include "sheet.h" // struct sheet +#include // ncurses functions +#include // handle sigterm +#include // strlen #define _STR(x) #x #define STR(x) _STR(x) -static void cleanup() +static void +cleanup() { clear(); echo(); @@ -20,17 +21,20 @@ static void cleanup() endwin(); } -static int x_pos(int x) +static int +x_pos(int x) { return x * CELL_SIZE + Y_AXIS_WIDTH; } -static int y_pos(int y) +static int +y_pos(int y) { return y + 2; } -static int get_cell_color(int const row, int const col) +static int +get_cell_color(int const row, int const col) { return (row + col) % 2 ? COLOR_LIGHTER_GRAY : COLOR_LIGHT_GRAY; } @@ -77,7 +81,7 @@ void draw_init(struct book *book) refresh(); } -void draw_cell(struct cell const * const cell) +void draw_cell(struct cell const *const cell) { int x = x_pos(cell->x_pos); int y = y_pos(cell->y_pos); @@ -88,22 +92,23 @@ void draw_cell(struct cell const * const cell) attron(COLOR_PAIR(color)); - switch(cell->type){ - case Integer: - printw("%" STR(CELL_SIZE) "li", cell->value.integer); - break; - case Floating: - printw("%" STR(CELL_SIZE) "lf", cell->value.floating); - break; - default: - printw("%-" STR(CELL_SIZE) "s", cell->text); - break; + switch (cell->type) { + case Integer: + printw("%" STR(CELL_SIZE) "li", cell->value.integer); + break; + case Floating: + printw("%" STR(CELL_SIZE) "lf", cell->value.floating); + break; + default: + printw("%-" STR(CELL_SIZE) "s", cell->text); + break; } attroff(COLOR_PAIR(color)); } -static void draw_celltree(struct celltree const *const root) +static void +draw_celltree(struct celltree const *const root) { if (root == NULL) { return; @@ -114,18 +119,20 @@ static void draw_celltree(struct celltree const *const root) draw_celltree(root->left); } -static void draw_row_num(int n) +static void +draw_row_num(int n) { attron(COLOR_PAIR(COLOR_GRAY)); printw("%" STR(Y_AXIS_WIDTH) "d", n); attroff(COLOR_PAIR(COLOR_GRAY)); } -static void draw_row_header(int row) +static void +draw_row_header(int row) { // TODO: make it dynamic char buf[4] = " "; - int i = sizeof buf - 2; + int i = sizeof buf - 2; row++; while (row && i >= 0) { @@ -139,11 +146,12 @@ static void draw_row_header(int row) attroff(COLOR_PAIR(COLOR_GRAY)); } -static void draw_sheet(struct sheet *s) +static void +draw_sheet(struct sheet *s) { addch('\n'); - int width = getmaxx(stdscr) - Y_AXIS_WIDTH; + int width = getmaxx(stdscr) - Y_AXIS_WIDTH; int n_cells_wide = width / CELL_SIZE; int n_cells_tall = getmaxy(stdscr) - 3; @@ -164,18 +172,17 @@ static void draw_sheet(struct sheet *s) attron(COLOR_PAIR(color)); - mvprintw(y_pos(row), - x_pos(col), - "%" STR(CELL_SIZE) "s", " "); + mvprintw(y_pos(row), x_pos(col), "%" STR(CELL_SIZE) "s", " "); } attroff(COLOR_PAIR(color)); } - + // draw on top of empty sheet draw_celltree(s->root_cell); } -static void draw_input_bar() +static void +draw_input_bar() { int width, height; @@ -213,10 +220,9 @@ void draw_highlight(int const x, int const y) { static int prev_x; static int prev_y; - int color; + int color; - mvchgat(y_pos(y), x_pos(x), CELL_SIZE, 0, COLOR_HIGHLIGHTED, - NULL); + mvchgat(y_pos(y), x_pos(x), CELL_SIZE, 0, COLOR_HIGHLIGHTED, NULL); color = get_cell_color(prev_y, prev_x); @@ -244,4 +250,3 @@ void draw_right_status(char const *const s) mvprintw(y - 1, x - 1 - strlen(s), "%s", s); attron(COLOR_PAIR(COLOR_TITLE)); } - diff --git a/src/draw.h b/src/draw.h index 6306e6e..36f0930 100644 --- a/src/draw.h +++ b/src/draw.h @@ -3,8 +3,8 @@ #define DRAW_H #include "book.h" -#include #include "cell.h" +#include void draw_highlight(int const x, int const y); @@ -12,7 +12,7 @@ void draw_init(struct book *book); void draw_book(struct book *b, size_t tab); -void draw_cell(struct cell const * const cell); +void draw_cell(struct cell const *const cell); void draw_left_status(const char *const s); diff --git a/src/function_parse.c b/src/function_parse.c index 8b1aa24..2aab728 100644 --- a/src/function_parse.c +++ b/src/function_parse.c @@ -1,10 +1,10 @@ #include +#include +#include #include #include #include -#include -#include static char *operators[] = { "+", @@ -32,22 +32,23 @@ enum type { }; struct token { - char const * value; - size_t value_len; - enum type type; + char const *value; + size_t value_len; + enum type type; }; struct token_list { struct token *tokens; - size_t len; - char *error_msg; + size_t len; + char *error_msg; }; // returns length of string in haystack matching needle, or 0 if it // doesn't exist -static int contains(char * const *haystack, char* needle) +static int +contains(char *const *haystack, char *needle) { - for(char *s = *haystack; s; s++){ + for (char *s = *haystack; s; s++) { if (strcmp(s, needle) == 0) { return strlen(s); } @@ -56,11 +57,12 @@ static int contains(char * const *haystack, char* needle) return false; } -static struct token eat(char **str, int (*f)(int), enum type type) +static struct token +eat(char **str, int (*f)(int), enum type type) { struct token tok; - tok.type = type; - tok.value = *str; + tok.type = type; + tok.value = *str; tok.value_len = 0; while (f(**str)) { @@ -71,18 +73,21 @@ static struct token eat(char **str, int (*f)(int), enum type type) return tok; } -static struct token tokenize_identifier(char** str) +static struct token +tokenize_identifier(char **str) { return eat(str, isalnum, identifier); } -static struct token tokenize_number(char **str) +static struct token +tokenize_number(char **str) { return eat(str, isdigit, identifier); } // Returns a token array from function -static struct token_list tokenize(char *str) +static struct token_list +tokenize(char *str) { struct token_list *tok_list = malloc(sizeof *tok_list); @@ -100,30 +105,26 @@ static struct token_list tokenize(char *str) // identifiers start with letters but can contain numbers while (*str && tok_list->len < max_tok) { struct token t; - size_t n; + size_t n; if (isalpha(*str)) { - t = tokenize_identifier(&str); - } - else if (!isalnum(*str) ) { + t = tokenize_identifier(&str); + } else if (!isalnum(*str)) { t = tokenize_number(&str); - } - else if ((n = contains(operators, str))) { - t.type = operator; - t.value = str; + } else if ((n = contains(operators, str))) { + t.type = operator; + t.value = str; t.value_len = n; - } - else if ((n = contains(separators, str))) { - t.type = separator; - t.value = str; + } else if ((n = contains(separators, str))) { + t.type = separator; + t.value = str; t.value_len = n; - } - else { - t.type = error; - t.value = str; + } else { + t.type = error; + t.value = str; t.value_len = strlen(str); } - + tok_list->tokens[tok_list->len] = t; tok_list->len += 1; } @@ -131,17 +132,16 @@ static struct token_list tokenize(char *str) return *tok_list; } -// credits: Daniel J. Bernstein +// credits: Daniel J. Bernstein // https://web.archive.org/web/20220328102559/http://www.cse.yorku.ca/~oz/hash.html -static unsigned long hash(unsigned char *str) +static unsigned long +hash(unsigned char *str) { unsigned long hash = 5381; - int c = 0; + int c = 0; - for(size_t i=0; str[i] != '\0'; i++) + for (size_t i = 0; str[i] != '\0'; i++) hash = hash * 33 + c; return hash; } - - diff --git a/src/function_parse.h b/src/function_parse.h index d1f9988..ecff94a 100644 --- a/src/function_parse.h +++ b/src/function_parse.h @@ -2,6 +2,4 @@ #ifndef FUNCTION_PARSE_H #define FUNCTION_PARSE_H - - #endif \ No newline at end of file diff --git a/src/interface.c b/src/interface.c index 8e0f95f..7e2e5c5 100644 --- a/src/interface.c +++ b/src/interface.c @@ -1,20 +1,20 @@ #include "interface.h" #include "cell.h" +#include "celltree.h" +#include "draw.h" +#include "keymap.h" #include #include -#include "celltree.h" -#include "keymap.h" -#include "draw.h" int g_display_sel_x = 0; int g_display_sel_y = 0; -int g_tab = 0; -//int g_right_scrolled = 0; -//int g_down_scrolled = 0; +int g_tab = 0; +// int g_right_scrolled = 0; +// int g_down_scrolled = 0; struct book *g_book = NULL; -struct cell *g_cur = NULL; +struct cell *g_cur = NULL; enum modes mode = Command; @@ -82,7 +82,7 @@ void interface_editor_backspace() getyx(stdscr, y, x); move(y, x - 1); - g_cur->text = realloc(g_cur->text, textlen); + g_cur->text = realloc(g_cur->text, textlen); g_cur->text[textlen - 1] = '\0'; refresh(); @@ -94,8 +94,8 @@ void interface_editor_append(const char c) size_t textlen = strlen(g_cur->text); - g_cur->text = realloc(g_cur->text, textlen + 2); - g_cur->text[textlen] = c; + g_cur->text = realloc(g_cur->text, textlen + 2); + g_cur->text[textlen] = c; g_cur->text[textlen + 1] = '\0'; getyx(stdscr, y, x); @@ -104,11 +104,12 @@ void interface_editor_append(const char c) refresh(); } -static void start_edit_cell() +static void +start_edit_cell() { // struct cell* cur g_cur = celltree_search(g_book->sheets[g_tab]->root_cell, g_display_sel_x, - g_display_sel_y); + g_display_sel_y); if (g_cur == NULL) { g_cur = malloc(sizeof(struct cell)); diff --git a/src/interface.h b/src/interface.h index 174f1b0..f32372e 100644 --- a/src/interface.h +++ b/src/interface.h @@ -50,7 +50,6 @@ void interface_editor_append(const char c); /* Remove last char */ void interface_editor_backspace(); - /* * Mode switching * -------------- @@ -65,5 +64,4 @@ void interface_mode_edit(); /* switch to normal mode */ void interface_mode_normal(); - #endif diff --git a/src/keymap.c b/src/keymap.c index 5bb78b4..e5157e0 100644 --- a/src/keymap.c +++ b/src/keymap.c @@ -6,12 +6,12 @@ #define KEY_ESC 27 -static void * const keymap_always[1024] = { +static void *const keymap_always[1024] = { [KEY_RESIZE] = interface_handle_resize, [27] /*ESC*/ = interface_mode_normal, }; -static void * const keymap_normal[1024] = { +static void *const keymap_normal[1024] = { ['i'] = interface_mode_edit, ['a'] = interface_mode_edit, ['g'] = interface_mode_g, @@ -25,17 +25,18 @@ static void * const keymap_normal[1024] = { ['l'] = interface_move_right, }; -static void * const keymap_edit[1024] = { - [KEY_ESC] = interface_mode_normal, +static void *const keymap_edit[1024] = { + [KEY_ESC] = interface_mode_normal, [KEY_BACKSPACE] = interface_editor_backspace, }; -static void * const keymap_g[1024] = { +static void *const keymap_g[1024] = { ['t'] = interface_next_tab, ['T'] = interface_prev_tab, }; -static void undefined() +static void +undefined() { int height = getmaxy(stdscr); move(height - 1, 0); @@ -55,7 +56,7 @@ void keymap_parse_key(size_t c, enum modes mode) break; case Edit: cmd = keymap_edit[c]; - if (cmd == NULL){ + if (cmd == NULL) { interface_editor_append(c); return; } @@ -65,7 +66,7 @@ void keymap_parse_key(size_t c, enum modes mode) break; } - if(mode != Edit) { + if (mode != Edit) { mode = Command; } diff --git a/src/main.c b/src/main.c index db72f60..81e6278 100644 --- a/src/main.c +++ b/src/main.c @@ -1,13 +1,12 @@ #include "book.h" -#include "draw.h" #include "config.h" +#include "draw.h" #include "interface.h" #include "sheet.h" #include #include #include - int main(int argc, char **argv) { (void)argv; diff --git a/src/sheet.c b/src/sheet.c index 5bfc5d8..91d7e0e 100644 --- a/src/sheet.c +++ b/src/sheet.c @@ -3,8 +3,8 @@ void sheet_init(struct sheet *s, size_t width, size_t height, char *title) { - s->width = width; - s->height = height; + s->width = width; + s->height = height; s->root_cell = NULL; if (title) { diff --git a/src/sheet.h b/src/sheet.h index 2bd9f5e..b56c650 100644 --- a/src/sheet.h +++ b/src/sheet.h @@ -1,20 +1,20 @@ #ifndef SHEET_H #define SHEET_H -#include -#include -#include -#include #include "config.h" +#include +#include +#include +#include struct sheet { struct celltree *root_cell; size_t width; size_t height; - char* title; + char *title; }; -void sheet_init(struct sheet *s, size_t width, size_t height, char* title); +void sheet_init(struct sheet *s, size_t width, size_t height, char *title); void sheet_delete(struct sheet *s); #endif \ No newline at end of file