]> git.mdlowis.com Git - projs/tide.git/commitdiff
Added logic to allowing toggling line numbers in win.c. Default is off but config... line-numbers
authorMichael D. Lowis <mike.lowis@gentex.com>
Tue, 6 Jun 2017 16:50:13 +0000 (12:50 -0400)
committerMichael D. Lowis <mike.lowis@gentex.com>
Tue, 6 Jun 2017 16:50:13 +0000 (12:50 -0400)
config.h
inc/win.h
lib/view.c
lib/win.c
tide.c

index 6589ee2bb3d5a21f669e66221d4c8b3e7762406b..c7b17d652ea692864ff4de3409d62da1ef3cd1d8 100644 (file)
--- a/config.h
+++ b/config.h
@@ -5,7 +5,6 @@ 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;
-extern int LineNumbers;
 
 /* OS-Specific Config
  ******************************************************************************/
@@ -35,6 +34,7 @@ enum {
     DblClickTime   = 250,     /* 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 */
 };
 
 #ifdef INCLUDE_DEFS
@@ -93,7 +93,5 @@ int CLR_VerBorder    = 2; // Vertical border color
 int CLR_Ruler        = 1; // Ruler color
 int CLR_Cursor       = 7; // Cursor color
 
-int LineNumbers = 1;
-
 #undef INCLUDE_DEFS
 #endif
index 911746d206d88cfb9ba0fe9e17fec7dd21af2028..b12dd7c7bc7bf655ae90b7ecec68f61391cb9193 100644 (file)
--- a/inc/win.h
+++ b/inc/win.h
@@ -44,6 +44,8 @@ void win_window(char* name, void (*errfn)(char*));
 void win_dialog(char* name, void (*errfn)(char*));
 void win_loop(void);
 void win_settext(WinRegion id, char* text);
+void win_showlinenums(bool enable);
+bool win_getlinenums(void);
 void win_setruler(size_t ruler);
 Rune win_getkey(void);
 void win_setkeys(KeyBinding* bindings);
index 28574cb9b6d69c80d526200a96331af24c89abe9..0a36dd5a5ba7e0a863586d9449e898c056aa9e91 100644 (file)
@@ -550,8 +550,11 @@ static size_t setcell(View* view, size_t row, size_t col, uint32_t attr, Rune r)
 
 static size_t fill_row(View* view, unsigned row, size_t pos, size_t* line) {
     view_getrow(view, row)->off  = pos;
-    if (line)
+    if (line) {
+        if (pos > buf_end(&(view->buffer)))
+            *line = 0;
         view_getrow(view, row)->line = *line;
+    }
     clearrow(view, row);
     for (size_t x = 0; x < view->ncols;) {
         uint32_t attr = (in_selection(view->selection, pos) ? CLR_SelectedText : CLR_NormalText);
index 1387d0099cd7a32b78c412642391951d3ead7f2d..609f6b6256913a0e70d655f43da9f1c29623b0d2 100644 (file)
--- a/lib/win.c
+++ b/lib/win.c
@@ -32,6 +32,7 @@ static WinRegion Focused = EDIT;
 static Region Regions[NREGIONS] = {0};
 static Rune LastKey;
 static KeyBinding* Keys = NULL;
+static bool ShowLineNumbers = false;
 
 static void win_init(void (*errfn)(char*)) {
     for (int i = 0; i < SCROLL; i++)
@@ -90,6 +91,14 @@ void win_settext(WinRegion id, char* text) {
     buf_logclear(&(view->buffer));
 }
 
+void win_setlinenums(bool enable) {
+    ShowLineNumbers = enable;
+}
+
+bool win_getlinenums(void) {
+    return ShowLineNumbers;
+}
+
 void win_setruler(size_t ruler) {
     Ruler = ruler;
 }
@@ -143,15 +152,15 @@ void win_setscroll(double offset, double visible) {
 }
 
 static size_t gutter_cols(void) {
-    size_t len   = (LineNumbers ? 1 : 0),
+    size_t len   = (ShowLineNumbers ? 1 : 0),
            lines = win_buf(EDIT)->nlines;
-    while (LineNumbers && lines > 9)
+    while (ShowLineNumbers && lines > 9)
         lines /= 10, len++;
     return len;
 }
 
 static size_t gutter_size(void) {
-    return (gutter_cols() * x11_font_width(Font)) + (LineNumbers ? 5 : 0);
+    return (gutter_cols() * x11_font_width(Font)) + (ShowLineNumbers ? 5 : 0);
 }
 
 static void layout(int width, int height) {
@@ -220,7 +229,7 @@ static void onredraw(int width, int height) {
             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);
-            if (LineNumbers)
+            if (ShowLineNumbers)
                 x11_draw_rect(CLR_Ruler, Regions[SCROLL].width, Regions[SCROLL].y-2, gsz, Regions[SCROLL].height+7);
         }
 
@@ -359,7 +368,7 @@ static void onwheeldn(WinRegion id, bool pressed, size_t row, size_t col) {
 
 static void draw_line_num(size_t x, size_t y, size_t gcols, size_t num) {
     UGlyph glyphs[gcols];
-    if (LineNumbers) {
+    if (ShowLineNumbers) {
         for (int i = gcols-1; i >= 0; i--) {
             glyphs[i].attr = CLR_GutterText;
             if (num > 0) {
diff --git a/tide.c b/tide.c
index 71c6553ae7b05031621112bf44fd097ba4e20898..16e86dbf5a74077bb8cf0f1ac34e77b18722acf5 100644 (file)
--- a/tide.c
+++ b/tide.c
@@ -238,7 +238,7 @@ static void tag_redo(void) {
 }
 
 static void tag_lnnum(void) {
-    LineNumbers = !LineNumbers;
+    win_setlinenums(!win_getlinenums());
 }
 
 static void search(void) {
@@ -594,6 +594,7 @@ int main(int argc, char** argv) {
     char* tags = getenv("EDITTAGS");
     win_settext(TAGS, (tags ? tags : DefaultTags));
     win_setruler(RulePosition);
+    win_setlinenums((bool)LineNumbers);
     /* open the first file in this instance */
     if (argc > 1)
         edit_relative(argv[1]);