Add option to not invert code background color.
[smdp.git] / src / viewer.c
index 89365bf..1a3ac00 100644 (file)
@@ -2,7 +2,7 @@
  * Functions necessary to display a deck of slides in different color modes
  * using ncurses. Only white, red, and blue are supported, as they can be
  * faded in 256 color mode.
- * Copyright (C) 2016 Michael Goehler
+ * Copyright (C) 2018 Michael Goehler
  *
  * This file is part of mdp.
  *
@@ -73,20 +73,21 @@ static const char *list_head1 = " +- ";
 static const char *list_head2 = " +- ";
 static const char *list_head3 = " +- ";
 
-int ncurses_display(deck_t *deck, int notrans, int nofade, int invert, int reload, int noreload, int slidenum) {
-
-    int c = 0;          // char
-    int i = 0;          // iterate
-    int l = 0;          // line number
-    int lc = 0;         // line count
-    int sc = 1;         // slide count
-    int colors = 0;     // amount of colors supported
-    int fade = 0;       // disable color fading by default
-    int trans = -1;     // enable transparency if term supports it
-    int max_lines = 0;  // max lines per slide
-    int max_cols = 0;   // max columns per line
-    int offset;         // text offset
-    int stop = 0;       // passed stop bits per slide
+int ncurses_display(deck_t *deck, int notrans, int nofade, int invert, int reload, int noreload, int slidenum, int nocodebg) {
+
+    int c = 0;                // char
+    int i = 0;                // iterate
+    int l = 0;                // line number
+    int lc = 0;               // line count
+    int sc = 1;               // slide count
+    int colors = 0;           // amount of colors supported
+    int fade = 0;             // disable color fading by default
+    int trans = -1;           // enable transparency if term supports it
+    int max_lines = 0;        // max lines per slide
+    int max_lines_slide = -1; // the slide that has the most lines
+    int max_cols = 0;         // max columns per line
+    int offset;               // text offset
+    int stop = 0;             // passed stop bits per slide
 
     // header line 1 is displayed at the top
     int bar_top = (deck->headers > 0) ? 1 : 0;
@@ -153,8 +154,13 @@ int ncurses_display(deck_t *deck, int notrans, int nofade, int invert, int reloa
         }
 
         max_lines = MAX(lc, max_lines);
+        if (lc == max_lines) {
+            max_lines_slide = sc;
+        }
 
+        slide->lines_consumed = lc;
         slide = slide->next;
+        ++sc;
     }
 
     // not enough lines
@@ -164,7 +170,7 @@ int ncurses_display(deck_t *deck, int notrans, int nofade, int invert, int reloa
         endwin();
 
         // print error
-        fwprintf(stderr, L"Error: Terminal height (%i lines) too small. Need at least %i lines.\n", LINES, max_lines + bar_top + bar_bottom);
+        fwprintf(stderr, L"Error: Terminal height (%i lines) too small. Need at least %i lines for slide #%i.\n", LINES, max_lines + bar_top + bar_bottom, max_lines_slide);
         fwprintf(stderr, L"You may need to add additional horizontal rules (---) to split your file in shorter slides.\n");
 
         // no reload
@@ -256,6 +262,7 @@ int ncurses_display(deck_t *deck, int notrans, int nofade, int invert, int reloa
     slide = deck->slide;
 
     // find slide to reload
+    sc = 1;
     while(reload > 1 && reload <= deck->slides) {
         slide = slide->next;
         sc++;
@@ -331,7 +338,8 @@ int ncurses_display(deck_t *deck, int notrans, int nofade, int invert, int reloa
 
         // print lines
         while(line) {
-            add_line(content, l, (COLS - max_cols) / 2, line, max_cols, colors);
+            add_line(content, l + ((LINES - slide->lines_consumed - bar_top - bar_bottom) / 2),
+                     (COLS - max_cols) / 2, line, max_cols, colors, nocodebg);
 
             // raise stop counter if we pass a line having a stop bit
             if(CHECK_BIT(line->bits, IS_STOP))
@@ -346,8 +354,9 @@ int ncurses_display(deck_t *deck, int notrans, int nofade, int invert, int reloa
         }
 
         // print pandoc URL references
-        // only if we already printed all lines of the current slide
-        if(!line) {
+        // only if we already printed all lines of the current slide (or output is stopped)
+        if(!line ||
+           stop > slide->stop) {
             int i, ymax;
             getmaxyx( content, ymax, i );
             for (i = 0; i < url_get_amount(); i++) {
@@ -556,7 +565,7 @@ void setup_list_strings(void)
     }
 }
 
-void add_line(WINDOW *window, int y, int x, line_t *line, int max_cols, int colors) {
+void add_line(WINDOW *window, int y, int x, line_t *line, int max_cols, int colors, int nocodebg) {
 
     int i; // increment
     int offset = 0; // text offset
@@ -568,7 +577,7 @@ void add_line(WINDOW *window, int y, int x, line_t *line, int max_cols, int colo
 
         // fill rest off line with spaces if we are in a code block
         if(CHECK_BIT(line->bits, IS_CODE) && colors) {
-            if(colors)
+            if(colors && !nocodebg)
                 wattron(window, COLOR_PAIR(CP_BLACK));
             for(i = getcurx(window) - x; i < max_cols; i++)
                 wprintw(window, "%s", " ");
@@ -602,7 +611,7 @@ void add_line(WINDOW *window, int y, int x, line_t *line, int max_cols, int colo
                 "%s", prompt);
 
         if(!CHECK_BIT(line->bits, IS_CODE))
-            inline_display(window, &line->text->value[offset], colors);
+            inline_display(window, &line->text->value[offset], colors, nocodebg);
 
     // IS_UNORDERED_LIST_2
     } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_2)) {
@@ -625,7 +634,7 @@ void add_line(WINDOW *window, int y, int x, line_t *line, int max_cols, int colo
                 "%s", prompt);
 
         if(!CHECK_BIT(line->bits, IS_CODE))
-            inline_display(window, &line->text->value[offset], colors);
+            inline_display(window, &line->text->value[offset], colors, nocodebg);
 
     // IS_UNORDERED_LIST_1
     } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_1)) {
@@ -643,19 +652,20 @@ void add_line(WINDOW *window, int y, int x, line_t *line, int max_cols, int colo
                 "%s", prompt);
 
         if(!CHECK_BIT(line->bits, IS_CODE))
-            inline_display(window, &line->text->value[offset], colors);
+            inline_display(window, &line->text->value[offset], colors, nocodebg);
     }
 
     // IS_CODE
     if(CHECK_BIT(line->bits, IS_CODE)) {
 
-        if (!CHECK_BIT(line->bits, IS_TILDE_CODE)) {
+        if (!CHECK_BIT(line->bits, IS_TILDE_CODE) &&
+            !CHECK_BIT(line->bits, IS_GFM_CODE)) {
             // set static offset for code
             offset = CODE_INDENT;
         }
 
         // reverse color for code blocks
-        if(colors)
+        if(colors && !nocodebg)
             wattron(window, COLOR_PAIR(CP_BLACK));
 
         // print whole lines
@@ -686,7 +696,7 @@ void add_line(WINDOW *window, int y, int x, line_t *line, int max_cols, int colo
                     offset = next_word(line->text, offset);
             }
 
-            inline_display(window, &line->text->value[offset], colors);
+            inline_display(window, &line->text->value[offset], colors, nocodebg);
         } else {
 
             // IS_CENTER
@@ -719,7 +729,7 @@ void add_line(WINDOW *window, int y, int x, line_t *line, int max_cols, int colo
             // no line-wide markdown
             } else {
 
-                inline_display(window, &line->text->value[offset], colors);
+                inline_display(window, &line->text->value[offset], colors, nocodebg);
             }
         }
     }
@@ -737,7 +747,7 @@ void add_line(WINDOW *window, int y, int x, line_t *line, int max_cols, int colo
     wattroff(window, A_UNDERLINE);
 }
 
-void inline_display(WINDOW *window, const wchar_t *c, const int colors) {
+void inline_display(WINDOW *window, const wchar_t *c, const int colors, int nocodebg) {
     const static wchar_t *special = L"\\*_`!["; // list of interpreted chars
     const wchar_t *i = c; // iterator
     const wchar_t *start_link_name, *start_url;
@@ -855,7 +865,7 @@ void inline_display(WINDOW *window, const wchar_t *c, const int colors) {
                             break;
                         // enable inline code
                         case L'`':
-                            if(colors)
+                            if(colors && !nocodebg)
                                 wattron(window, COLOR_PAIR(CP_BLACK));
                             break;
                         // do nothing for backslashes
@@ -974,7 +984,7 @@ int int_length (int val) {
 
 int get_slide_number(char init) {
     int retval = init - '0';
-    char c;
+    int c;
     // block for tenths of a second when using getch, ERR if no input
     halfdelay(GOTO_SLIDE_DELAY);
     while((c = getch()) != ERR) {