From 46ba488e246e7ffe69968be2e6a24e17fcdab407 Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Sun, 18 Jun 2017 22:44:25 -0400 Subject: [PATCH] added xresource config options --- Makefile | 3 +- config.h | 70 ++-------------------- inc/edit.h | 24 ++++++++ inc/x11.h | 2 + lib/buf.c | 7 ++- lib/colors.c | 28 ++++----- lib/config.c | 165 +++++++++++++++++++++++++++++++++++++++++++++++++++ lib/utf8.c | 3 +- lib/utils.c | 21 +++++++ lib/view.c | 6 +- lib/win.c | 54 ++++++++++------- lib/x11.c | 16 ++--- tests/tide.c | 2 +- tide.c | 12 ++-- 14 files changed, 292 insertions(+), 121 deletions(-) create mode 100644 lib/config.c diff --git a/Makefile b/Makefile index 4d58821..4f2da71 100644 --- a/Makefile +++ b/Makefile @@ -12,7 +12,8 @@ LIBEDIT_OBJS = \ lib/view.o \ lib/x11.o \ lib/win.o \ - lib/colors.o + lib/colors.o \ + lib/config.o TEST_BINS = \ tests/tide \ diff --git a/config.h b/config.h index e801e37..64531e9 100644 --- a/config.h +++ b/config.h @@ -1,10 +1,5 @@ /* extern the config variables */ -extern char *FontString, *DefaultTags; -extern unsigned int ColorPalette[16]; extern char *ShellCmd[], *SedCmd[], *PickFileCmd[], *PickTagCmd[], *OpenCmd[]; -extern int CLR_NormalText, CLR_GutterText, CLR_SelectedText, CLR_TagsBkg, - CLR_EditBkg, CLR_HorBorder, CLR_VerBorder, CLR_Ruler, CLR_ScrollBkg, - CLR_ThumbBkg, CLR_Cursor, CLR_CurrentLine, CLR_Comment; /* OS-Specific Config ******************************************************************************/ @@ -19,33 +14,15 @@ extern int CLR_NormalText, CLR_GutterText, CLR_SelectedText, CLR_TagsBkg, /* General Config ******************************************************************************/ enum { - Width = 640, /* default window width */ - Height = 480, /* default window height */ - ExpandTabs = 1, /* Tabs will be expanded to spaces */ - TabWidth = 4, /* maximum number of spaces used to represent a tab */ - ScrollLines = 4, /* number of lines to scroll by for mouse wheel scrolling */ - BufSize = 8192, /* default buffer size */ - EventTimeout = 50, /* Maximum blocking wait time for events */ - TrimOnSave = 1, /* Enable trimming of trailing whitespace on save */ - DefaultCRLF = 0, /* Default to Unix line endings */ - DefaultCharset = UTF_8, /* We assume UTF-8 because nothing else matters */ - FontCacheSize = 16, /* Maximum number of fonts allowed in the font cache */ - LineSpacing = LNSPACE, /* Set the vertical spacing between lines */ - DblClickTime = 500, /* Millisecond time for detecting double clicks */ - RulePosition = 80, /* Column in which the vertical ruler appears */ - CopyIndent = 1, /* New lines will inherit the indent of the preceding line */ - LineNumbers = 1, /* Enable line numbers by default or not */ - MaxScanDistance = 16384, /* Maximum distance to scan before visible lines for syntax higlighting */ + BufSize = 8192, /* default buffer size */ + FontCacheSize = 16, /* Maximum number of fonts allowed in the font cache */ + DefaultCRLF = 0, /* Default to Unix line endings */ + DefaultCharset = UTF_8, /* We assume UTF-8 because nothing else matters */ + MaxScanDistance = 16384, /* Maximum distance to scan before visible lines for syntax higlighting */ }; #ifdef INCLUDE_DEFS -/* Default contents of the tags region */ -char* DefaultTags = "Quit Save Undo Redo Cut Copy Paste | Find "; - -/* Default font string */ -char* FontString = FONT; - /* The shell: Filled in with $SHELL. Used to execute commands */ char* ShellCmd[] = { NULL, "-c", NULL, NULL }; @@ -61,42 +38,5 @@ char* PickTagCmd[] = { "picktag", NULL, "tags", NULL, NULL }; /* Open a new instance of the editor */ char* OpenCmd[] = { "tide", NULL, NULL }; -/* Solarized Theme - 16 color palette used for drawing */ -unsigned int ColorPalette[16] = { - 0xff002b36, // 00 - Base03 - 0xff073642, // 01 - Base02 - 0xff586e75, // 02 - Base01 - 0xff657b83, // 03 - Base00 - 0xff839496, // 04 - Base0 - 0xff93a1a1, // 05 - Base1 - 0xffeee8d5, // 06 - Base2 - 0xfffdf6e3, // 07 - Base3 - 0xffb58900, // 08 - Yellow - 0xffcb4b16, // 09 - Orange - 0xffdc322f, // 10 - Red - 0xffd33682, // 11 - Magenta - 0xff6c71c4, // 12 - Violet - 0xff268bd2, // 13 - Blue - 0xff2aa198, // 14 - Cyan - 0xff859900 // 15 - Green -}; - -#define COLOR_PAIR(bg, fg) (((bg) << 8) | (fg)) -int CLR_TagsBkg = 1; // Background color for the tags region -int CLR_EditBkg = 0; // Background color for the edit region -int CLR_ScrollBkg = 3; // Background color for the scroll region -int CLR_ThumbBkg = 0; // Background color of the scroll thumb -int CLR_HorBorder = 2; // Horizontal border color -int CLR_VerBorder = 2; // Vertical border color -int CLR_Ruler = 1; // Ruler color -int CLR_Cursor = 7; // Cursor color - -/* Text Color Attributes BG, FG */ -int CLR_NormalText = COLOR_PAIR( 0, 4 ); -int CLR_SelectedText = COLOR_PAIR( 4, 0 ); -int CLR_GutterText = COLOR_PAIR( 0, 3 ); -int CLR_CurrentLine = COLOR_PAIR( 11, 7 ); // Current Line Number -int CLR_Comment = COLOR_PAIR( 0, 2 ); // Comment color - #undef INCLUDE_DEFS #endif diff --git a/inc/edit.h b/inc/edit.h index 5e655f1..1ff0a5e 100644 --- a/inc/edit.h +++ b/inc/edit.h @@ -19,6 +19,7 @@ char* dirname(char* path); bool try_chdir(char* fpath); char* strconcat(char* dest, ...); bool file_exists(char* path); +char* strmcat(char* first, ...); /* Buffer management functions *****************************************************************************/ @@ -232,3 +233,26 @@ int cmdrun(char** cmd, char** err); char* cmdread(char** cmd, char** err); void cmdwrite(char** cmd, char* text, char** err); char* cmdwriteread(char** cmd, char* text, char** err); + +/* Configuration Data + *****************************************************************************/ +enum { + FontString = 0, TagString, WinWidth, WinHeight, LineSpacing, LineNumbers, + RulerColumn, EventTimeout, CopyIndent, TrimOnSave, ExpandTabs, TabWidth, + ScrollLines, DblClickTime, + Color00, Color01, Color02, Color03, Color04, Color05, Color06, Color07, + Color08, Color09, Color10, Color11, Color12, Color13, Color14, Color15, + BkgRuler, BkgGutter, BkgTags, BkgEdit, BkgScroll, BkgThumb, BkgBorder, + TxtCursor, TxtNormal, TxtSelected, TxtGutter, TxtCurrentLine, + SynNormal, SynComment, SynConstant, SynString, SynChar, SynNumber, + SynBoolean, SynFloat, SynVariable, SynFunction, SynKeyword, SynOperator, + SynPreProc, SynType, SynStatement, SynSpecial +}; + +void config_init(void* disp); +void config_set_int(int key, int val); +void config_set_bool(int key, bool val); +void config_set_str(int key, char* val); +int config_get_int(int key); +bool config_get_bool(int key); +char* config_get_str(int key); diff --git a/inc/x11.h b/inc/x11.h index 6137c6a..bb3c746 100644 --- a/inc/x11.h +++ b/inc/x11.h @@ -115,6 +115,7 @@ enum { void x11_init(XConfig* cfg); void x11_deinit(void); +char* x11_cfg_get(char* opt); int x11_keybtnstate(void); bool x11_keymodsset(int mask); void x11_window(char* name, int width, int height); @@ -147,3 +148,4 @@ void x11_draw_utf8(XFont font, int fg, int bg, int x, int y, char* str); bool x11_sel_get(int selid, void(*cbfn)(char*)); bool x11_sel_set(int selid, char* str); + diff --git a/lib/buf.c b/lib/buf.c index 9050732..d0d39f4 100644 --- a/lib/buf.c +++ b/lib/buf.c @@ -33,8 +33,8 @@ void buf_init(Buf* buf, void (*errfn)(char*)) { /* reset the state to defaults */ buf->modified = false; - buf->expand_tabs = (ExpandTabs == 1); - buf->copy_indent = (CopyIndent == 1); + buf->expand_tabs = config_get_bool(ExpandTabs); + buf->copy_indent = config_get_bool(CopyIndent); buf->charset = DefaultCharset; buf->crlf = DefaultCRLF; buf->bufsize = BufSize; @@ -147,7 +147,8 @@ size_t buf_insert(Buf* buf, bool fmt, size_t off, Rune rune) { bool is_eol = (rune == '\n' || rune == RUNE_CRLF); buf->modified = true; if (fmt && buf->expand_tabs && rune == '\t') { - size_t n = (TabWidth - ((off - buf_bol(buf, off)) % TabWidth)); + size_t tabwidth = config_get_int(TabWidth); + size_t n = (tabwidth - ((off - buf_bol(buf, off)) % tabwidth)); log_insert(buf, &(buf->undo), off, off+n); for(; n > 0; n--) off += insert(buf, off, ' '); } else { diff --git a/lib/colors.c b/lib/colors.c index 2ee094c..1e4585d 100644 --- a/lib/colors.c +++ b/lib/colors.c @@ -24,7 +24,7 @@ static SyntaxDef Syntaxes[] = { .name = "text", .extensions = (char*[]){ 0 }, .rules = (SyntaxRule[]){ - { .color = 2, .oneol = END, .beg = "#" }, + { .color = SynComment, .oneol = END, .beg = "#" }, {0,0,0} } }, @@ -33,10 +33,10 @@ static SyntaxDef Syntaxes[] = { .extensions = (char*[]){ ".c", ".h", ".C", ".cpp", ".CPP", ".hpp", ".cc", ".c++", ".cxx", 0 }, .rules = (SyntaxRule[]){ - { .color = Literal, .oneol = END, .beg = "\"", .end = "\"" }, - { .color = Literal, .oneol = END, .beg = "'", .end = "'" }, - { .color = Comment, .oneol = END, .beg = "//" }, - { .color = Comment, .oneol = CONT, .beg = "/*", .end = "*/" }, + { .color = SynConstant, .oneol = END, .beg = "\"", .end = "\"" }, + { .color = SynConstant, .oneol = END, .beg = "'", .end = "'" }, + { .color = SynComment, .oneol = END, .beg = "//" }, + { .color = SynComment, .oneol = CONT, .beg = "/*", .end = "*/" }, {0,0,0,0} } }, @@ -44,13 +44,13 @@ static SyntaxDef Syntaxes[] = { .name = "diff", .extensions = (char*[]){ ".diff", ".patch", 0 }, .rules = (SyntaxRule[]){ - { .color = Location, .oneol = END, .beg = "@@" }, - { .color = Info, .oneol = END, .beg = "Index:" }, - { .color = Info, .oneol = END, .beg = "---" }, - { .color = Info, .oneol = END, .beg = "+++" }, - { .color = Deleted, .oneol = END, .beg = "-" }, - { .color = Added, .oneol = END, .beg = "+" }, - { .color = Normal, .oneol = END, .beg = "" }, + { .color = SynPreProc, .oneol = END, .beg = "@@" }, + { .color = SynType, .oneol = END, .beg = "Index:" }, + { .color = SynStatement, .oneol = END, .beg = "---" }, + { .color = SynType, .oneol = END, .beg = "+++" }, + { .color = SynSpecial, .oneol = END, .beg = "-" }, + { .color = SynVariable, .oneol = END, .beg = "+" }, + { .color = SynNormal, .oneol = END, .beg = "" }, {0,0,0,0} } }, @@ -80,7 +80,7 @@ bool apply_rule(SyntaxRule* rule, Buf* buf, size_t* off, int* color) { break; *off = *off + 1; } - *color = rule->color; + *color = config_get_int(rule->color); } return ret; } @@ -90,7 +90,7 @@ SyntaxSpan* colors_scan(SyntaxDef* syntax, SyntaxSpan* spans, Buf* buf, size_t b SyntaxSpan* currspan = spans; if (!syntax) return firstspan; - int color = CLR_Comment; + int color = config_get_int(SynComment); for (size_t off = beg; off < end; off++) { size_t start = off; for (SyntaxRule* rules = syntax->rules; rules && rules->beg; rules++) diff --git a/lib/config.c b/lib/config.c new file mode 100644 index 0000000..5e9d981 --- /dev/null +++ b/lib/config.c @@ -0,0 +1,165 @@ +#include +#include +#include +#include +#include + +XrmDatabase DB; +struct { + char* name; + enum { STRING, INTEGER, BOOLEAN } type; + union { + bool opt; + int num; + char* str; + } value; +} Options[] = { + [FontString] = { "tide.ui.font", STRING, { + .str = "Liberation Mono:pixelsize=14:antialias=true:autohint=true" } }, + [TagString] = { "tide.ui.tags", STRING, { + .str = "Quit Save Undo Redo Cut Copy Paste | Find " } }, + + /* user interface related options */ + [WinWidth] = { "tide.ui.width", INTEGER, { .num = 640 } }, + [WinHeight] = { "tide.ui.height", INTEGER, { .num = 480 } }, + [LineSpacing] = { "tide.ui.line_spacing", INTEGER, { .num = 0 } }, + [LineNumbers] = { "tide.ui.line_numbers", BOOLEAN, { .opt = true } }, + [RulerColumn] = { "tide.ui.ruler_column", INTEGER, { .num = 80 } }, + [EventTimeout] = { "tide.ui.timeout", INTEGER, { .num = 50 } }, + + /* input related options */ + [CopyIndent] = { "tide.input.copy_indent", BOOLEAN, { .opt = true } }, + [TrimOnSave] = { "tide.input.trim_on_save", BOOLEAN, { .opt = false } }, + [ExpandTabs] = { "tide.input.expand_tabs", BOOLEAN, { .opt = true } }, + [TabWidth] = { "tide.input.tab_width", INTEGER, { .num = 4 } }, + [ScrollLines] = { "tide.input.scroll_lines", INTEGER, { .num = 4 } }, + [DblClickTime] = { "tide.input.click_time", INTEGER, { .num = 500 } }, + + /* default color palette definition */ + [Color00] = { "tide.palette.00", INTEGER, { .num = 0xff002b36 } }, + [Color01] = { "tide.palette.01", INTEGER, { .num = 0xff073642 } }, + [Color02] = { "tide.palette.02", INTEGER, { .num = 0xff586e75 } }, + [Color03] = { "tide.palette.03", INTEGER, { .num = 0xff657b83 } }, + [Color04] = { "tide.palette.04", INTEGER, { .num = 0xff839496 } }, + [Color05] = { "tide.palette.05", INTEGER, { .num = 0xff93a1a1 } }, + [Color06] = { "tide.palette.06", INTEGER, { .num = 0xffeee8d5 } }, + [Color07] = { "tide.palette.07", INTEGER, { .num = 0xfffdf6e3 } }, + [Color08] = { "tide.palette.08", INTEGER, { .num = 0xffb58900 } }, + [Color09] = { "tide.palette.09", INTEGER, { .num = 0xffcb4b16 } }, + [Color10] = { "tide.palette.10", INTEGER, { .num = 0xffdc322f } }, + [Color11] = { "tide.palette.11", INTEGER, { .num = 0xffd33682 } }, + [Color12] = { "tide.palette.12", INTEGER, { .num = 0xff6c71c4 } }, + [Color13] = { "tide.palette.13", INTEGER, { .num = 0xff268bd2 } }, + [Color14] = { "tide.palette.14", INTEGER, { .num = 0xff2aa198 } }, + [Color15] = { "tide.palette.15", INTEGER, { .num = 0xff859900 } }, + + /* Background and UI Colors */ + [BkgRuler] = { "tide.colors.ruler", INTEGER, { .num = 0x01 } }, + [BkgGutter] = { "tide.colors.gutter", INTEGER, { .num = 0x01 } }, + [BkgTags] = { "tide.colors.bkg_tags", INTEGER, { .num = 0x01 } }, + [BkgEdit] = { "tide.colors.bkg_edit", INTEGER, { .num = 0x00 } }, + [BkgScroll] = { "tide.colors.bkg_scroll", INTEGER, { .num = 0x03 } }, + [BkgThumb] = { "tide.colors.bkg_thumb", INTEGER, { .num = 0x00 } }, + [BkgBorder] = { "tide.colors.border", INTEGER, { .num = 0x03 } }, + + /* Base Text Colors */ + [TxtCursor] = { "tide.colors.text.cursor", INTEGER, { .num = 0x0007 } }, + [TxtNormal] = { "tide.colors.text.normal", INTEGER, { .num = 0x0004 } }, + [TxtSelected] = { "tide.colors.text.selected", INTEGER, { .num = 0x0400 } }, + [TxtGutter] = { "tide.colors.text.gutter", INTEGER, { .num = 0x0003 } }, + [TxtCurrentLine] = { "tide.colors.text.currline", INTEGER, { .num = 0x0D07 } }, + + /* Syntax Colors */ + [SynNormal] = { "tide.colors.syntax.normal", INTEGER, { .num = 0x0004 } }, + [SynComment] = { "tide.colors.syntax.comment", INTEGER, { .num = 0x0002 } }, + [SynConstant] = { "tide.colors.syntax.constant", INTEGER, { .num = 0x000E } }, + [SynString] = { "tide.colors.syntax.string", INTEGER, { .num = 0x000E } }, + [SynChar] = { "tide.colors.syntax.character", INTEGER, { .num = 0x000E } }, + [SynNumber] = { "tide.colors.syntax.number", INTEGER, { .num = 0x000E } }, + [SynBoolean] = { "tide.colors.syntax.booleean", INTEGER, { .num = 0x000E } }, + [SynFloat] = { "tide.colors.syntax.float", INTEGER, { .num = 0x000E } }, + [SynVariable] = { "tide.colors.syntax.variable", INTEGER, { .num = 0x000F } }, + [SynFunction] = { "tide.colors.syntax.function", INTEGER, { .num = 0x000E } }, + [SynKeyword] = { "tide.colors.syntax.keyword", INTEGER, { .num = 0x000E } }, + [SynOperator] = { "tide.colors.syntax.operator", INTEGER, { .num = 0x000E } }, + [SynPreProc] = { "tide.colors.syntax.preprocessor", INTEGER, { .num = 0x000D } }, + [SynType] = { "tide.colors.syntax.type", INTEGER, { .num = 0x0009 } }, + [SynStatement] = { "tide.colors.syntax.statement", INTEGER, { .num = 0x000E } }, + [SynSpecial] = { "tide.colors.syntax.special", INTEGER, { .num = 0x000A } }, +}; + +void config_init(void* disp) { + XrmDatabase db; + char *homedir = getenv("HOME"), + *userfile = strmcat(homedir, "/.config/tiderc", 0), + *rootfile = strmcat(homedir, "/.Xdefaults", 0); + XrmInitialize(); + + /* load from xrdb or .Xdefaults */ + if (XResourceManagerString(disp) != NULL) + db = XrmGetStringDatabase(XResourceManagerString(disp)); + else + db = XrmGetFileDatabase(rootfile); + XrmMergeDatabases(db, &DB); + + /* load user settings from ~/.config/tiderc */ + db = XrmGetFileDatabase(userfile); + (void)XrmMergeDatabases(db, &DB); + + /* cleanup */ + free(userfile); + free(rootfile); + + char* type; + XrmValue val; + for (size_t i = 0; i < nelem(Options); i++) { + if (XrmGetResource(DB, Options[i].name, NULL, &type, &val)) { + char* value = (char*)(val.addr); + size_t len = val.size; + switch (Options[i].type) { + case STRING: + if (*value == '\'') value++, len -= 2; + Options[i].value.str = strndup(value, len); + break; + + case INTEGER: + Options[i].value.num = strtol(value, NULL, 0); + break; + + case BOOLEAN: + Options[i].value.opt = !strncmp(value, "true", len); + break; + } + } + } +} + +void config_set_int(int key, int val) { + assert(Options[key].type == INTEGER); + Options[key].value.num = val; +} + +void config_set_bool(int key, bool val) { + assert(Options[key].type == BOOLEAN); + Options[key].value.opt = val; +} + +void config_set_str(int key, char* val) { + assert(Options[key].type == STRING); + Options[key].value.str = val; +} + +int config_get_int(int key) { + assert(Options[key].type == INTEGER); + return Options[key].value.num; +} + +bool config_get_bool(int key) { + assert(Options[key].type == BOOLEAN); + return Options[key].value.num; +} + +char* config_get_str(int key) { + assert(Options[key].type == STRING); + return Options[key].value.str; +} diff --git a/lib/utf8.c b/lib/utf8.c index 77f6bee..23806b7 100644 --- a/lib/utf8.c +++ b/lib/utf8.c @@ -76,7 +76,8 @@ bool utf8decode(Rune* rune, size_t* length, int byte) { } int runewidth(unsigned col, Rune r) { - if (r == '\t') return (TabWidth - (col % TabWidth)); + size_t tabwidth = config_get_int(TabWidth); + if (r == '\t') return (tabwidth - (col % tabwidth)); int width = wcwidth(r); if (width < 0) width = 1; return width; diff --git a/lib/utils.c b/lib/utils.c index f94eb87..c9228dd 100644 --- a/lib/utils.c +++ b/lib/utils.c @@ -148,3 +148,24 @@ bool file_exists(char* path) { struct stat st; return (stat(path, &st) < 0); } + +char* strmcat(char* first, ...) { + va_list args; + /* calculate the length of the final string */ + size_t len = strlen(first); + va_start(args, first); + for (char* s = NULL; (s = va_arg(args, char*));) + len += strlen(s); + va_end(args); + + /* allocate the final string and copy the args into it */ + char *str = malloc(len+1), *curr = str; + while (first && *first) *(curr++) = *(first++); + va_start(args, first); + for (char* s = NULL; (s = va_arg(args, char*));) + while (s && *s) *(curr++) = *(s++); + va_end(args); + /* null terminate and return */ + *curr = '\0'; + return str; +} diff --git a/lib/view.c b/lib/view.c index ff7013f..101a248 100644 --- a/lib/view.c +++ b/lib/view.c @@ -387,7 +387,7 @@ void view_scrollpage(View* view, int move) { void view_indent(View* view, int dir) { Buf* buf = &(view->buffer); - unsigned indoff = (buf->expand_tabs ? TabWidth : 1); + unsigned indoff = (buf->expand_tabs ? config_get_int(TabWidth) : 1); selswap(&(view->selection)); view->selection.beg = buf_bol(buf, view->selection.beg); view->selection.end = buf_eol(buf, view->selection.end); @@ -571,7 +571,9 @@ static size_t fill_row(View* view, unsigned row, size_t pos, size_t* line) { } clearrow(view, row); for (size_t x = 0; x < view->ncols;) { - uint32_t attr = (in_selection(view->selection, pos) ? CLR_SelectedText : CLR_NormalText); + uint32_t attr = (in_selection(view->selection, pos) + ? config_get_int(TxtSelected) + : config_get_int(TxtNormal)); Rune r = buf_get(&(view->buffer), pos++); x += setcell(view, row, x, attr, r); if (buf_iseol(&(view->buffer), pos-1)) { diff --git a/lib/win.c b/lib/win.c index 5744607..e9731a6 100644 --- a/lib/win.c +++ b/lib/win.c @@ -38,17 +38,17 @@ static void win_init(void (*errfn)(char*)) { for (int i = 0; i < SCROLL; i++) view_init(&(Regions[i].view), NULL, errfn); x11_init(&Config); - Font = x11_font_load(FontString); + Font = x11_font_load(config_get_str(FontString)); } void win_window(char* name, void (*errfn)(char*)) { win_init(errfn); - x11_window(name, Width, Height); + x11_window(name, config_get_int(WinWidth), config_get_int(WinHeight)); } void win_dialog(char* name, void (*errfn)(char*)) { win_init(errfn); - x11_dialog(name, Width, Height); + x11_dialog(name, config_get_int(WinWidth), config_get_int(WinHeight)); } static bool update_focus(void) { @@ -69,7 +69,7 @@ void win_loop(void) { x11_show(); x11_flip(); while (x11_running()) { - bool pending = x11_events_await(EventTimeout); + bool pending = x11_events_await(config_get_int(EventTimeout)); int nevents = x11_events_queued(); if (update_focus() || pending || nevents) { x11_events_take(); @@ -221,16 +221,25 @@ static void onredraw(int width, int height) { for (int i = 0; i < SCROLL; i++) { View* view = win_view(i); - x11_draw_rect((i == TAGS ? CLR_TagsBkg : CLR_EditBkg), - 0, Regions[i].y - 3, width, Regions[i].height + 8); - x11_draw_rect(CLR_HorBorder, 0, Regions[i].y - 3, width, 1); + x11_draw_rect((i == TAGS ? config_get_int(BkgTags) + : config_get_int(BkgEdit)), + 0, Regions[i].y - 3, width, Regions[i].height + 8); + x11_draw_rect(config_get_int(BkgBorder), 0, Regions[i].y - 3, width, 1); if (i == EDIT) { size_t gsz = gutter_size(); if (Ruler) - x11_draw_rect(CLR_Ruler, ((Ruler+2) * fwidth) + gsz, Regions[i].y-2, 1, Regions[i].height+7); + x11_draw_rect( config_get_int(BkgRuler), + ((Ruler+2) * fwidth) + gsz, + Regions[i].y-2, + 1, + Regions[i].height+7 ); if (ShowLineNumbers) - x11_draw_rect(CLR_Ruler, Regions[SCROLL].width, Regions[SCROLL].y-2, gsz, Regions[SCROLL].height+7); + x11_draw_rect( config_get_int(BkgGutter), + Regions[SCROLL].width, + Regions[SCROLL].y-2, + gsz, + Regions[SCROLL].height+7 ); } size_t gcols = gutter_cols(); @@ -251,13 +260,16 @@ static void onredraw(int width, int height) { size_t thumboff = (size_t)((thumbreg * ScrollOffset) + (Regions[SCROLL].y - 2)); size_t thumbsz = (size_t)(thumbreg * ScrollVisible); if (thumbsz < 5) thumbsz = 5; - x11_draw_rect(CLR_VerBorder, Regions[SCROLL].width, Regions[SCROLL].y - 2, 1, Regions[SCROLL].height); - x11_draw_rect(CLR_ScrollBkg, 0, Regions[SCROLL].y - 2, Regions[SCROLL].width, thumbreg); - x11_draw_rect(CLR_ThumbBkg, 0, thumboff, Regions[SCROLL].width, thumbsz); + x11_draw_rect(config_get_int(BkgBorder), + Regions[SCROLL].width, Regions[SCROLL].y - 2, 1, Regions[SCROLL].height); + x11_draw_rect(config_get_int(BkgScroll), + 0, Regions[SCROLL].y - 2, Regions[SCROLL].width, thumbreg); + x11_draw_rect(config_get_int(BkgThumb), + 0, thumboff, Regions[SCROLL].width, thumbsz); /* place the cursor on screen */ if (Regions[Focused].csrx != SIZE_MAX && Regions[Focused].csry != SIZE_MAX) { - x11_draw_rect(CLR_Cursor, + x11_draw_rect(config_get_int(TxtCursor), Regions[Focused].x + (Regions[Focused].csrx * fwidth), Regions[Focused].y + (Regions[Focused].csry * fheight), 1, fheight); @@ -325,10 +337,10 @@ static void scroll_actions(int btn, bool pressed, int x, int y) { view_scroll(win_view(EDIT), +row); break; case MouseWheelUp: - view_scroll(win_view(EDIT), -ScrollLines); + view_scroll(win_view(EDIT), -(config_get_int(ScrollLines))); break; case MouseWheelDn: - view_scroll(win_view(EDIT), +ScrollLines); + view_scroll(win_view(EDIT), +(config_get_int(ScrollLines))); break; } } @@ -343,9 +355,9 @@ static void onmousedrag(int state, int x, int y) { } static void onmousebtn(int btn, bool pressed, int x, int y) { + if (x < Regions[Focused].x) + x = Regions[Focused].x; WinRegion id = getregion(x, y); - if (id == FOCUSED && x < Regions[Focused].x) - id = Focused, x = Regions[Focused].x; size_t row = (y-Regions[id].y) / x11_font_height(Font); size_t col = (x-Regions[id].x) / x11_font_width(Font); @@ -364,19 +376,19 @@ static void onmousebtn(int btn, bool pressed, int x, int y) { static void onwheelup(WinRegion id, bool pressed, size_t row, size_t col) { if (!pressed) return; - view_scroll(win_view(id), -ScrollLines); + view_scroll(win_view(id), -(config_get_int(ScrollLines))); } static void onwheeldn(WinRegion id, bool pressed, size_t row, size_t col) { if (!pressed) return; - view_scroll(win_view(id), +ScrollLines); + view_scroll(win_view(id), +(config_get_int(ScrollLines))); } static void draw_line_num(bool current, size_t x, size_t y, size_t gcols, size_t num) { if (ShowLineNumbers) { - int color = CLR_GutterText; + int color = config_get_int(TxtGutter); if (current) { - color = CLR_CurrentLine; + color = config_get_int(TxtCurrentLine); size_t fheight = x11_font_height(Font); x11_draw_rect((color >> 8), x-3, y-fheight-1, gutter_size(), fheight); } diff --git a/lib/x11.c b/lib/x11.c index 6bc1d57..381ca02 100644 --- a/lib/x11.c +++ b/lib/x11.c @@ -63,7 +63,8 @@ static struct XSel { { .name = "CLIPBOARD" }, }; -static void xftcolor(XftColor* xc, uint32_t c) { +static void xftcolor(XftColor* xc, int id) { + uint32_t c = config_get_int(id + Color00); xc->color.alpha = 0xFF | ((c & 0xFF000000) >> 16); xc->color.red = 0xFF | ((c & 0x00FF0000) >> 8); xc->color.green = 0xFF | ((c & 0x0000FF00)); @@ -97,6 +98,7 @@ void x11_init(XConfig* cfg) { SelTarget = XInternAtom(X.display, "UTF8_STRING", 0); if (SelTarget == None) SelTarget = XInternAtom(X.display, "STRING", 0); + config_init(X.display); } int x11_keybtnstate(void) { @@ -119,7 +121,7 @@ void x11_window(char* name, int width, int height) { X.width, X.height, 0, X.depth, - ColorPalette[0]); + config_get_int(Color00)); /* register interest in the delete window message */ Atom wmDeleteMessage = XInternAtom(X.display, "WM_DELETE_WINDOW", False); @@ -415,7 +417,7 @@ size_t x11_font_descent(XFont fnt) { void x11_draw_rect(int color, int x, int y, int width, int height) { XftColor clr; - xftcolor(&clr, ColorPalette[color]); + xftcolor(&clr, color); XftDrawRect(X.xft, &clr, x, y, width, height); XftColorFree(X.display, X.visual, X.colormap, &clr); } @@ -492,13 +494,13 @@ void x11_draw_glyphs(int fg, int bg, XGlyphSpec* specs, size_t nspecs) { XGlyphInfo extent; XftTextExtentsUtf8(X.display, font, (const FcChar8*)"0", 1, &extent); int w = extent.xOff; - int h = (font->height - font->descent) + LineSpacing; - xftcolor(&bgc, ColorPalette[bg]); + int h = (font->height - font->descent) + config_get_int(LineSpacing); + xftcolor(&bgc, bg); size_t width = specs[nspecs-1].x - specs[0].x + w; - x11_draw_rect(bg, specs[0].x, specs[0].y - h, width, font->height + LineSpacing); + x11_draw_rect(bg, specs[0].x, specs[0].y - h, width, font->height + config_get_int(LineSpacing)); XftColorFree(X.display, X.visual, X.colormap, &bgc); } - xftcolor(&fgc, ColorPalette[fg]); + xftcolor(&fgc, fg); XftDrawGlyphFontSpec(X.xft, &fgc, (XftGlyphFontSpec*)specs, nspecs); XftColorFree(X.display, X.visual, X.colormap, &fgc); } diff --git a/tests/tide.c b/tests/tide.c index 80982c4..e6c97ac 100644 --- a/tests/tide.c +++ b/tests/tide.c @@ -782,7 +782,7 @@ TEST_SUITE(UnitTests) { setup_view(EDIT, "", CRLF, 0); win_buf(EDIT)->modified = true; ExitCode = 42; - usleep((DblClickTime+1) * 1000); + usleep((config_get_int(DblClickTime)+1) * 1000); EXPECT_EXIT { exec("Quit"); CHECK(ExitCode == 42); diff --git a/tide.c b/tide.c index 16e86db..74bca95 100644 --- a/tide.c +++ b/tide.c @@ -112,7 +112,7 @@ static void ondiagmsg(char* msg) { static void trim_whitespace(void) { Buf* buf = win_buf(EDIT); - if (TrimOnSave && buf_end(buf) > 0) { + if (config_get_bool(TrimOnSave) && buf_end(buf) > 0) { View* view = win_view(EDIT); unsigned off = 0, prev = 1; /* loop through the buffer till we hit the end or we stop advancing */ @@ -136,7 +136,7 @@ static void trim_whitespace(void) { static void quit(void) { static uint64_t before = 0; uint64_t now = getmillis(); - if (!win_buf(EDIT)->modified || (now-before) <= DblClickTime) { + if (!win_buf(EDIT)->modified || (now-before) <= config_get_int(DblClickTime)) { #ifndef TEST x11_deinit(); #else @@ -177,7 +177,7 @@ void onmouseleft(WinRegion id, bool pressed, size_t row, size_t col) { static uint64_t before = 0; if (!pressed) return; uint64_t now = getmillis(); - count = ((now-before) <= DblClickTime ? count+1 : 1); + count = ((now-before) <= config_get_int(DblClickTime) ? count+1 : 1); before = now; if (count == 1) { @@ -592,9 +592,9 @@ int main(int argc, char** argv) { /* Create the window and enter the event loop */ win_window("tide", ondiagmsg); char* tags = getenv("EDITTAGS"); - win_settext(TAGS, (tags ? tags : DefaultTags)); - win_setruler(RulePosition); - win_setlinenums((bool)LineNumbers); + win_settext(TAGS, (tags ? tags : config_get_str(TagString))); + win_setruler(config_get_int(RulerColumn)); + win_setlinenums(config_get_bool(LineNumbers)); /* open the first file in this instance */ if (argc > 1) edit_relative(argv[1]); -- 2.49.0