]> git.mdlowis.com Git - projs/tide.git/commitdiff
added xresource config options
authorMichael D. Lowis <mike@mdlowis.com>
Mon, 19 Jun 2017 02:44:25 +0000 (22:44 -0400)
committerMichael D. Lowis <mike@mdlowis.com>
Mon, 19 Jun 2017 02:44:25 +0000 (22:44 -0400)
14 files changed:
Makefile
config.h
inc/edit.h
inc/x11.h
lib/buf.c
lib/colors.c
lib/config.c [new file with mode: 0644]
lib/utf8.c
lib/utils.c
lib/view.c
lib/win.c
lib/x11.c
tests/tide.c
tide.c

index 4d58821e0010e8af49c725de227125cfba96dd2f..4f2da711131a6dd5aaff537cca797d98c7824bf9 100644 (file)
--- 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  \
index e801e375a2889e13f8f3d6faf4fdd1a5cffc66bc..64531e9aeba633f357b3a5de9ad968e2fb903bc3 100644 (file)
--- 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
index 5e655f1654bb47dca668bc0cdc9ae7ac6fdedde5..1ff0a5e5b4b7ef29142978bf4f3a04ba41c267c8 100644 (file)
@@ -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);
index 6137c6a1445c498790bcbb41e6836b5cb7c0ce2f..bb3c7460d05094244c08018f97c7f9ef3c1c7169 100644 (file)
--- 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);
 
+
index 9050732b43e85f265c8f56a5dae515eeb3f486a7..d0d39f4d11bdcc157591b66f65b08c7416060158 100644 (file)
--- 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 {
index 2ee094c4ea8b1188b4e64480af02333421fc24db..1e4585d6072b45f66a556fb107559f796b06de4b 100644 (file)
@@ -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 (file)
index 0000000..5e9d981
--- /dev/null
@@ -0,0 +1,165 @@
+#include <X11/Xlib.h>
+#include <X11/Xresource.h>
+#include <stdc.h>
+#include <utf.h>
+#include <edit.h>
+
+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;
+}
index 77f6bee961e12eecc6e299a8d4e96ef62458ca75..23806b7210acb06e6113fccfac7e9ca442fb7618 100644 (file)
@@ -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;
index f94eb87dae8fea055360d49371d64195afa69a89..c9228ddc639556331b323eba0bb7cbe78f3bf4f1 100644 (file)
@@ -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;
+}
index ff7013f758dc2dba7760c27b5b8f1c94ecaa729f..101a24894622258ae8e82a2ee62d808a4983a9af 100644 (file)
@@ -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)) {
index 574460738924aab703ab520314435b4a2d5e1c50..e9731a60d907657b8e4e479ef80d4563cf7e4bd3 100644 (file)
--- 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);
         }
index 6bc1d572e97a481042d0dfc6614258bb62e5d32d..381ca02a8a5976f65bfe0ce24a1640fb9d368e22 100644 (file)
--- 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);
 }
index 80982c4fef473c20656fbe41c80a8145c4ea2626..e6c97ace1e958d7b23a0279cacc3f186c5391520 100644 (file)
@@ -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 16e86dbf5a74077bb8cf0f1ac34e77b18722acf5..74bca951d8fa6b416e067c528e5fe72b6fadf30b 100644 (file)
--- 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]);