config.h file
authorDaniel Liu <mr.picklepinosaur@gmail.com>
Sat, 22 May 2021 16:28:53 +0000 (12:28 -0400)
committerDaniel Liu <mr.picklepinosaur@gmail.com>
Sat, 22 May 2021 16:28:53 +0000 (12:28 -0400)
README.md
include/config.h [new file with mode: 0644]
include/viewer.h
mdp.1
src/viewer.c

index 7d390d7..c4b0072 100644 (file)
--- a/README.md
+++ b/README.md
@@ -61,7 +61,7 @@ Review sample.md for more details.
 
 ---
 
-***Controls:***
+***Default controls:***
 
 - h, j, k, l, Arrow keys,
     Space, Enter, Backspace,
@@ -73,6 +73,13 @@ Review sample.md for more details.
 - q - exit
 
 
+---
+
+***Configuration***:
+
+A `config.h` configuration file is available in `include/`, change the settings you want and recompile.
+Colors, keybindings and list types are configurable as of now. Note that configuring colors only works in 8 color mode.
+
 ---
 
 ***How to debug it:***
diff --git a/include/config.h b/include/config.h
new file mode 100644 (file)
index 0000000..28a4351
--- /dev/null
@@ -0,0 +1,101 @@
+#if !defined( CONFIG_H )
+#define CONFIG_H
+
+/*
+ * User configuration file
+ * Copyright (C) 2018 Michael Goehler
+ *
+ * This file is part of mdp.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+// unordered list characters
+//
+// you can also override via env vars:
+// export MDP_LIST_OPEN1="    " MDP_LIST_OPEN2="    " MDP_LIST_OPEN3="    "
+// export MDP_LIST_HEAD1=" ■  " MDP_LIST_HEAD2=" ●  " MDP_LIST_HEAD3=" ▫  "
+// or export MDP_LIST_OPEN to override all MDP_LIST_OPENx variables
+// and similarly for MDP_LIST_HEAD
+static const char *list_open1 = " |  ";
+static const char *list_open2 = " |  ";
+static const char *list_open3 = " |  ";
+static const char *list_head1 = " +- ";
+static const char *list_head2 = " +- ";
+static const char *list_head3 = " +- ";
+
+#define FADE_DELAY 15000    // micro seconds
+#define GOTO_SLIDE_DELAY 5  // tenths of seconds
+
+// colors - you can only set in 8-bit color mode
+//
+/* Use the ncurses defined colors, here's a list of them:
+ *      COLOR_BLACK
+ *      COLOR_RED
+ *      COLOR_GREEN
+ *      COLOR_YELLOW
+ *      COLOR_BLUE
+ *      COLOR_MAGENTA
+ *      COLOR_CYAN
+ *      COLOR_WHITE
+ */
+#define FG_COLOR        COLOR_WHITE
+#define BG_COLOR        COLOR_BLACK
+#define TITLE_COLOR     COLOR_YELLOW
+#define HEADER_COLOR    COLOR_BLUE
+#define BOLD_COLOR      COLOR_RED
+
+// keybindings
+static const int prev_slide_binding[] = {
+    KEY_UP,
+    KEY_LEFT,
+    KEY_PPAGE,
+    8,   // BACKSPACE (ascii)
+    127, // BACKSPACE (xterm)
+    263, // BACKSPACE (getty)
+    'h',
+    'k',
+    0
+};
+static const int next_slide_binding[] = {
+    KEY_DOWN,
+    KEY_RIGHT,
+    KEY_NPAGE,
+    '\n', // ENTER
+    ' ',  // SPACE
+    'j',
+    'l',
+    0
+};
+static const int first_slide_binding[] = {
+    'g',
+    KEY_HOME,
+    0
+};
+static const int last_slide_binding[] = {
+    'G',
+    KEY_END,
+    0
+};
+static const int reload_binding[] = {
+    'r',
+    0
+};
+static const int quit_binding[] = {
+    'q',
+    0
+};
+
+#endif // !defined( CONFIG_H )
index 2dbc266..a5f23c4 100644 (file)
@@ -52,9 +52,6 @@
 #define CP_YELLOW 4 // 208
 #define CP_BLACK  5 // CP_WHITE with foreground and background swapped
 
-#define FADE_DELAY 15000 // micro seconds
-#define GOTO_SLIDE_DELAY 5    // tenths of seconds
-
 int ncurses_display(deck_t *deck, int notrans, int nofade, int invert, int reload, int noreload, int slidenum, int nocodebg);
 void add_line(WINDOW *window, int y, int x, line_t *line, int max_cols, int colors, int nocodebg);
 void inline_display(WINDOW *window, const wchar_t *c, const int colors, int nocodebg);
@@ -63,5 +60,6 @@ void fade_in(WINDOW *window, int trans, int colors, int invert);
 int int_length (int val);
 int get_slide_number(char init);
 void setup_list_strings(void);
+bool evaluate_binding(const int bindings[], char c);
 
 #endif // !defined( VIEWER_H )
diff --git a/mdp.1 b/mdp.1
index f40e43e..b43fcff 100644 (file)
--- a/mdp.1
+++ b/mdp.1
@@ -168,6 +168,9 @@ This key is disabled if input was read from standard input.
 Exit
 .BR mdp "."
 .
+.SH CUSTOMIZATION
+.B mdp
+can be configured by modifying config.h and recompiling.
 .SH AUTHOR
 Written by Michael Goehler and others, see
 .IR https://github.com/visit1985/mdp/blob/master/AUTHORS "."
index 1a3ac00..c538e7c 100644 (file)
@@ -28,6 +28,7 @@
 #include <unistd.h> // usleep
 #include <stdlib.h> // getenv
 #include "viewer.h"
+#include "config.h"
 
 // color ramp for fading from black to color
 static short white_ramp[24] = { 16, 232, 233, 234, 235, 236,
@@ -61,18 +62,6 @@ static short red_ramp_invert[24]   = { 15, 231, 231, 224, 224, 225,
                                       206, 207, 201, 200, 199, 199,
                                       198, 198, 197, 197, 196, 196};
 
-// unordered list characters
-//
-// override via env vars:
-// export MDP_LIST_OPEN1="    " MDP_LIST_OPEN2="    " MDP_LIST_OPEN3="    "
-// export MDP_LIST_HEAD1=" ■  " MDP_LIST_HEAD2=" ●  " MDP_LIST_HEAD3=" ▫  "
-static const char *list_open1 = " |  ";
-static const char *list_open2 = " |  ";
-static const char *list_open3 = " |  ";
-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 nocodebg) {
 
     int c = 0;                // char
@@ -227,22 +216,22 @@ int ncurses_display(deck_t *deck, int notrans, int nofade, int invert, int reloa
 
             if(notrans) {
                 if(invert) {
-                    trans = 7; // white in 8 color mode
+                    trans = FG_COLOR; // white in 8 color mode
                 } else {
-                    trans = 0; // black in 8 color mode
+                    trans = BG_COLOR; // black in 8 color mode
                 }
             }
 
             if(invert) {
-                init_pair(CP_WHITE, 0, trans);
-                init_pair(CP_BLACK, 7, 0);
+                init_pair(CP_WHITE, BG_COLOR, trans);
+                init_pair(CP_BLACK, FG_COLOR, BG_COLOR);
             } else {
-                init_pair(CP_WHITE, 7, trans);
-                init_pair(CP_BLACK, 0, 7);
+                init_pair(CP_WHITE, FG_COLOR, trans);
+                init_pair(CP_BLACK, BG_COLOR, FG_COLOR);
             }
-            init_pair(CP_BLUE, 4, trans);
-            init_pair(CP_RED, 1, trans);
-            init_pair(CP_YELLOW, 3, trans);
+            init_pair(CP_BLUE, HEADER_COLOR, trans);
+            init_pair(CP_RED, BOLD_COLOR, trans);
+            init_pair(CP_YELLOW, TITLE_COLOR, trans);
         }
 
         colors = 1;
@@ -385,138 +374,96 @@ int ncurses_display(deck_t *deck, int notrans, int nofade, int invert, int reloa
 
         // evaluate user input
         i = 0;
-        switch(c) {
 
+        if (evaluate_binding(prev_slide_binding, c)) {
             // show previous slide or stop bit
-            case KEY_UP:
-            case KEY_LEFT:
-            case KEY_PPAGE:
-            case 8:   // BACKSPACE (ascii)
-            case 127: // BACKSPACE (xterm)
-            case 263: // BACKSPACE (getty)
-            case 'h':
-            case 'k':
-                if(stop > 1 || (stop == 1 && !line)) {
-                    // show current slide again
-                    // but stop one stop bit earlier
-                    slide->stop--;
-                    fade = false;
+            if(stop > 1 || (stop == 1 && !line)) {
+                // show current slide again
+                // but stop one stop bit earlier
+                slide->stop--;
+                fade = false;
+            } else {
+                if(slide->prev) {
+                    // show previous slide
+                    slide = slide->prev;
+                    sc--;
+                    //stop on first bullet point always
+                    if(slide->stop > 0)
+                        slide->stop = 0;
                 } else {
-                    if(slide->prev) {
-                        // show previous slide
-                        slide = slide->prev;
-                        sc--;
-                        //stop on first bullet point always
-                        if(slide->stop > 0)
-                            slide->stop = 0;
-                    } else {
-                        // do nothing
-                        fade = false;
-                    }
+                    // do nothing
+                    fade = false;
                 }
-                break;
-
+            }
+        } else if (evaluate_binding(next_slide_binding, c)) {
             // show next slide or stop bit
-            case KEY_DOWN:
-            case KEY_RIGHT:
-            case KEY_NPAGE:
-            case '\n': // ENTER
-            case ' ':  // SPACE
-            case 'j':
-            case 'l':
-                if(stop && line) {
-                    // show current slide again
-                    // but stop one stop bit later (or at end of slide)
-                    slide->stop++;
-                    fade = false;
+            if(stop && line) {
+                // show current slide again
+                // but stop one stop bit later (or at end of slide)
+                slide->stop++;
+                fade = false;
+            } else {
+                if(slide->next) {
+                    // show next slide
+                    slide = slide->next;
+                    sc++;
                 } else {
-                    if(slide->next) {
-                        // show next slide
-                        slide = slide->next;
-                        sc++;
-                    } else {
-                        // do nothing
-                        fade = false;
-                    }
+                    // do nothing
+                    fade = false;
                 }
-                break;
-
+            }
+        } else if (isdigit(c) && c != '0') {
             // show slide n
-            case '9':
-            case '8':
-            case '7':
-            case '6':
-            case '5':
-            case '4':
-            case '3':
-            case '2':
-            case '1':
-                i = get_slide_number(c);
-                if(i > 0 && i <= deck->slides) {
-                    while(sc != i) {
-                        // search forward
-                        if(sc < i) {
-                            if(slide->next) {
-                                slide = slide->next;
-                                sc++;
-                            }
-                        // search backward
-                        } else {
-                            if(slide->prev) {
-                                slide = slide->prev;
-                                sc--;
-                            }
+            i = get_slide_number(c);
+            if(i > 0 && i <= deck->slides) {
+                while(sc != i) {
+                    // search forward
+                    if(sc < i) {
+                        if(slide->next) {
+                            slide = slide->next;
+                            sc++;
+                        }
+                    // search backward
+                    } else {
+                        if(slide->prev) {
+                            slide = slide->prev;
+                            sc--;
                         }
                     }
-                } else {
-                    // disable fading if slide n doesn't exist
-                    fade = false;
                 }
-                break;
-
+            }        
+        } else if (evaluate_binding(first_slide_binding, c)) {
             // show first slide
-            case 'g':
-            case KEY_HOME:
-                slide = deck->slide;
-                sc = 1;
-                break;
-
+            slide = deck->slide;
+            sc = 1;
+        } else if (evaluate_binding(last_slide_binding, c)) {
             // show last slide
-            case 'G':
-            case KEY_END:
-                for(i = sc; i <= deck->slides; i++) {
-                    if(slide->next) {
-                            slide = slide->next;
-                            sc++;
-                    }
+            for(i = sc; i <= deck->slides; i++) {
+                if(slide->next) {
+                        slide = slide->next;
+                        sc++;
                 }
-                break;
-
+            }
+        } else if (evaluate_binding(reload_binding, c)) {
             // reload
-            case 'r':
-                if(noreload == 0) {
-                    // reload slide N
-                    reload = sc;
-                    slide = NULL;
-                } else {
-                    // disable fading if reload is not possible
-                    fade = false;
-                }
-                break;
-
-            // quit
-            case 'q':
-                // do not fade out on exit
-                fade = false;
-                // do not reload
-                reload = 0;
+            if(noreload == 0) {
+                // reload slide N
+                reload = sc;
                 slide = NULL;
-                break;
-
-            default:
-                // disable fading on undefined key press
+            } else {
+                // disable fading if reload is not possible
                 fade = false;
-                break;
+            }
+        } else if (evaluate_binding(quit_binding, c)) {
+            // quit
+            // do not fade out on exit
+            fade = false;
+            // do not reload
+            reload = 0;
+            slide = NULL;
+        } else {
+            // disable fading on undefined key press
+            fade = false;
         }
 
         // fade out
@@ -998,3 +945,14 @@ int get_slide_number(char init) {
     cbreak();       // go back to cbreak
     return retval;
 }
+
+bool evaluate_binding(const int bindings[], char c) {
+    int binding;
+    int ind = 0; 
+    while((binding = bindings[ind]) != 0) {
+        if (c == binding) return true;
+        ind++;
+    }
+    return false;
+}
+