config.h file
[smdp.git] / src / viewer.c
1 /*
2  * Functions necessary to display a deck of slides in different color modes
3  * using ncurses. Only white, red, and blue are supported, as they can be
4  * faded in 256 color mode.
5  * Copyright (C) 2018 Michael Goehler
6  *
7  * This file is part of mdp.
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <http://www.gnu.org/licenses/>.
21  *
22  */
23
24 #include <ctype.h>  // isalnum
25 #include <wchar.h>  // wcschr
26 #include <wctype.h> // iswalnum
27 #include <string.h> // strcpy
28 #include <unistd.h> // usleep
29 #include <stdlib.h> // getenv
30 #include "viewer.h"
31 #include "config.h"
32
33 // color ramp for fading from black to color
34 static short white_ramp[24] = { 16, 232, 233, 234, 235, 236,
35                                237, 238, 239, 240, 241, 242,
36                                244, 245, 246, 247, 248, 249,
37                                250, 251, 252, 253, 254, 255 };
38
39 static short blue_ramp[24]  = { 16,  17,  17,  18,  18,  19,
40                                 19,  20,  20,  21,  27,  33,
41                                 32,  39,  38,  45,  44,  44,
42                                 81,  81,  51,  51, 123, 123 };
43
44 static short red_ramp[24]   = { 16,  52,  52,  53,  53,  89,
45                                 89,  90,  90, 126, 127, 127,
46                                163, 163, 164, 164, 200, 200,
47                                201, 201, 207, 207, 213, 213 };
48
49 // color ramp for fading from white to color
50 static short white_ramp_invert[24] = { 15, 255, 254, 254, 252, 251,
51                                       250, 249, 248, 247, 246, 245,
52                                       243, 242, 241, 240, 239, 238,
53                                       237, 236, 235, 234, 233, 232};
54
55 static short blue_ramp_invert[24]  = { 15, 231, 231, 195, 195, 159,
56                                       159, 123, 123,  87,  51,  44,
57                                        45,  38,  39,  32,  33,  33,
58                                        26,  26,  27,  27,  21,  21};
59
60 static short red_ramp_invert[24]   = { 15, 231, 231, 224, 224, 225,
61                                       225, 218, 218, 219, 212, 213,
62                                       206, 207, 201, 200, 199, 199,
63                                       198, 198, 197, 197, 196, 196};
64
65 int ncurses_display(deck_t *deck, int notrans, int nofade, int invert, int reload, int noreload, int slidenum, int nocodebg) {
66
67     int c = 0;                // char
68     int i = 0;                // iterate
69     int l = 0;                // line number
70     int lc = 0;               // line count
71     int sc = 1;               // slide count
72     int colors = 0;           // amount of colors supported
73     int fade = 0;             // disable color fading by default
74     int trans = -1;           // enable transparency if term supports it
75     int max_lines = 0;        // max lines per slide
76     int max_lines_slide = -1; // the slide that has the most lines
77     int max_cols = 0;         // max columns per line
78     int offset;               // text offset
79     int stop = 0;             // passed stop bits per slide
80
81     // header line 1 is displayed at the top
82     int bar_top = (deck->headers > 0) ? 1 : 0;
83     // header line 2 is displayed at the bottom
84     // anyway we display the slide number at the bottom
85     int bar_bottom = (slidenum || deck->headers > 1)? 1 : 0;
86
87     slide_t *slide = deck->slide;
88     line_t *line;
89
90     // init ncurses
91     initscr();
92
93     while(slide) {
94         lc = 0;
95         line = slide->line;
96
97         while(line && line->text) {
98
99             if (line->text->value) {
100                 lc += url_count_inline(line->text->value);
101                 line->length -= url_len_inline(line->text->value);
102             }
103
104             if(line->length > COLS) {
105                 i = line->length;
106                 offset = 0;
107                 while(i > COLS) {
108
109                     i = prev_blank(line->text, offset + COLS) - offset;
110
111                     // single word is > COLS
112                     if(!i) {
113                         // calculate min_width
114                         i = next_blank(line->text, offset + COLS) - offset;
115
116                         // disable ncurses
117                         endwin();
118
119                         // print error
120                         fwprintf(stderr, L"Error: Terminal width (%i columns) too small. Need at least %i columns.\n", COLS, i);
121                         fwprintf(stderr, L"You may need to shorten some lines by inserting line breaks.\n");
122
123                         // no reload
124                         return 0;
125                     }
126
127                     // set max_cols
128                     max_cols = MAX(i, max_cols);
129
130                     // iterate to next line
131                     offset = prev_blank(line->text, offset + COLS);
132                     i = line->length - offset;
133                     lc++;
134                 }
135                 // set max_cols one last time
136                 max_cols = MAX(i, max_cols);
137             } else {
138                 // set max_cols
139                 max_cols = MAX(line->length, max_cols);
140             }
141             lc++;
142             line = line->next;
143         }
144
145         max_lines = MAX(lc, max_lines);
146         if (lc == max_lines) {
147             max_lines_slide = sc;
148         }
149
150         slide->lines_consumed = lc;
151         slide = slide->next;
152         ++sc;
153     }
154
155     // not enough lines
156     if(max_lines + bar_top + bar_bottom > LINES) {
157
158         // disable ncurses
159         endwin();
160
161         // print error
162         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);
163         fwprintf(stderr, L"You may need to add additional horizontal rules (---) to split your file in shorter slides.\n");
164
165         // no reload
166         return 0;
167     }
168
169     // disable cursor
170     curs_set(0);
171
172     // disable output of keyboard typing
173     noecho();
174
175     // make getch() process one char at a time
176     cbreak();
177
178     // enable arrow keys
179     keypad(stdscr,TRUE);
180
181     // set colors
182     if(has_colors() == TRUE) {
183         start_color();
184         use_default_colors();
185
186         // 256 color mode
187         if(COLORS == 256) {
188
189             if(notrans) {
190                 if(invert) {
191                     trans = 15; // white in 256 color mode
192                 } else {
193                     trans = 16; // black in 256 color mode
194                 }
195             }
196
197             if(invert) {
198                 init_pair(CP_WHITE, 232, trans);
199                 init_pair(CP_BLUE, 21, trans);
200                 init_pair(CP_RED, 196, trans);
201                 init_pair(CP_BLACK, 15, 232);
202             } else {
203                 init_pair(CP_WHITE, 255, trans);
204                 init_pair(CP_BLUE, 123, trans);
205                 init_pair(CP_RED, 213, trans);
206                 init_pair(CP_BLACK, 16, 255);
207             }
208             init_pair(CP_YELLOW, 208, trans);
209
210             // enable color fading
211             if(!nofade)
212                 fade = true;
213
214         // 8 color mode
215         } else {
216
217             if(notrans) {
218                 if(invert) {
219                     trans = FG_COLOR; // white in 8 color mode
220                 } else {
221                     trans = BG_COLOR; // black in 8 color mode
222                 }
223             }
224
225             if(invert) {
226                 init_pair(CP_WHITE, BG_COLOR, trans);
227                 init_pair(CP_BLACK, FG_COLOR, BG_COLOR);
228             } else {
229                 init_pair(CP_WHITE, FG_COLOR, trans);
230                 init_pair(CP_BLACK, BG_COLOR, FG_COLOR);
231             }
232             init_pair(CP_BLUE, HEADER_COLOR, trans);
233             init_pair(CP_RED, BOLD_COLOR, trans);
234             init_pair(CP_YELLOW, TITLE_COLOR, trans);
235         }
236
237         colors = 1;
238     }
239
240     // set background color for main window
241     if(colors)
242         wbkgd(stdscr, COLOR_PAIR(CP_WHITE));
243
244     // setup content window
245     WINDOW *content = newwin(LINES - bar_top - bar_bottom, COLS, 0 + bar_top, 0);
246
247     // set background color of content window
248     if(colors)
249         wbkgd(content, COLOR_PAIR(CP_WHITE));
250
251     slide = deck->slide;
252
253     // find slide to reload
254     sc = 1;
255     while(reload > 1 && reload <= deck->slides) {
256         slide = slide->next;
257         sc++;
258         reload--;
259     }
260
261     // reset reload indicator
262     reload = 0;
263
264     while(slide) {
265
266         url_init();
267
268         // clear windows
269         werase(content);
270         werase(stdscr);
271
272         // always resize window in case terminal geometry has changed
273         wresize(content, LINES - bar_top - bar_bottom, COLS);
274
275         // set main window text color
276         if(colors)
277             wattron(stdscr, COLOR_PAIR(CP_YELLOW));
278
279         // setup header
280         if(bar_top) {
281             line = deck->header;
282             offset = next_blank(line->text, 0) + 1;
283             // add text to header
284             mvwaddwstr(stdscr,
285                        0, (COLS - line->length + offset) / 2,
286                        &line->text->value[offset]);
287         }
288
289         // setup footer
290         if(deck->headers > 1) {
291             line = deck->header->next;
292             offset = next_blank(line->text, 0) + 1;
293             switch(slidenum) {
294                 case 0: // add text to center footer
295                     mvwaddwstr(stdscr,
296                                LINES - 1, (COLS - line->length + offset) / 2,
297                                &line->text->value[offset]);
298                     break;
299                 case 1:
300                 case 2: // add text to left footer
301                     mvwaddwstr(stdscr,
302                                LINES - 1, 3,
303                                &line->text->value[offset]);
304                     break;
305             }
306         }
307
308         // add slide number to right footer
309         switch(slidenum) {
310             case 1: // show slide number only
311                 mvwprintw(stdscr,
312                           LINES - 1, COLS - int_length(sc) - 3,
313                           "%d", sc);
314                 break;
315             case 2: // show current slide & number of slides
316                 mvwprintw(stdscr,
317                           LINES - 1, COLS - int_length(deck->slides) - int_length(sc) - 6,
318                           "%d / %d", sc, deck->slides);
319                 break;
320         }
321
322         // copy changed lines in main window to virtual screen
323         wnoutrefresh(stdscr);
324
325         line = slide->line;
326         l = stop = 0;
327
328         // print lines
329         while(line) {
330             add_line(content, l + ((LINES - slide->lines_consumed - bar_top - bar_bottom) / 2),
331                      (COLS - max_cols) / 2, line, max_cols, colors, nocodebg);
332
333             // raise stop counter if we pass a line having a stop bit
334             if(CHECK_BIT(line->bits, IS_STOP))
335                 stop++;
336
337             l += (line->length / COLS) + 1;
338             line = line->next;
339
340             // only stop here if we didn't stop here recently
341             if(stop > slide->stop)
342                 break;
343         }
344
345         // print pandoc URL references
346         // only if we already printed all lines of the current slide (or output is stopped)
347         if(!line ||
348            stop > slide->stop) {
349             int i, ymax;
350             getmaxyx( content, ymax, i );
351             for (i = 0; i < url_get_amount(); i++) {
352                 mvwprintw(content, ymax - url_get_amount() - 1 + i, 3,
353                           "[%d] ", i);
354                 waddwstr(content, url_get_target(i));
355             }
356         }
357
358         // copy changed lines in content window to virtual screen
359         wnoutrefresh(content);
360
361         // compare virtual screen to physical screen and does the actual updates
362         doupdate();
363
364         // fade in
365         if(fade)
366             fade_in(content, trans, colors, invert);
367
368         // re-enable fading after any undefined key press
369         if(COLORS == 256 && !nofade)
370             fade = true;
371
372         // wait for user input
373         c = getch();
374
375         // evaluate user input
376         i = 0;
377
378         if (evaluate_binding(prev_slide_binding, c)) {
379             // show previous slide or stop bit
380             if(stop > 1 || (stop == 1 && !line)) {
381                 // show current slide again
382                 // but stop one stop bit earlier
383                 slide->stop--;
384                 fade = false;
385             } else {
386                 if(slide->prev) {
387                     // show previous slide
388                     slide = slide->prev;
389                     sc--;
390                     //stop on first bullet point always
391                     if(slide->stop > 0)
392                         slide->stop = 0;
393                 } else {
394                     // do nothing
395                     fade = false;
396                 }
397             }
398         } else if (evaluate_binding(next_slide_binding, c)) {
399             // show next slide or stop bit
400             if(stop && line) {
401                 // show current slide again
402                 // but stop one stop bit later (or at end of slide)
403                 slide->stop++;
404                 fade = false;
405             } else {
406                 if(slide->next) {
407                     // show next slide
408                     slide = slide->next;
409                     sc++;
410                 } else {
411                     // do nothing
412                     fade = false;
413                 }
414             }
415         } else if (isdigit(c) && c != '0') {
416             // show slide n
417             i = get_slide_number(c);
418             if(i > 0 && i <= deck->slides) {
419                 while(sc != i) {
420                     // search forward
421                     if(sc < i) {
422                         if(slide->next) {
423                             slide = slide->next;
424                             sc++;
425                         }
426                     // search backward
427                     } else {
428                         if(slide->prev) {
429                             slide = slide->prev;
430                             sc--;
431                         }
432                     }
433                 }
434             }        
435         } else if (evaluate_binding(first_slide_binding, c)) {
436             // show first slide
437             slide = deck->slide;
438             sc = 1;
439         } else if (evaluate_binding(last_slide_binding, c)) {
440             // show last slide
441             for(i = sc; i <= deck->slides; i++) {
442                 if(slide->next) {
443                         slide = slide->next;
444                         sc++;
445                 }
446             }
447         } else if (evaluate_binding(reload_binding, c)) {
448             // reload
449             if(noreload == 0) {
450                 // reload slide N
451                 reload = sc;
452                 slide = NULL;
453             } else {
454                 // disable fading if reload is not possible
455                 fade = false;
456             }
457         } else if (evaluate_binding(quit_binding, c)) {
458             // quit
459             // do not fade out on exit
460             fade = false;
461             // do not reload
462             reload = 0;
463             slide = NULL;
464         } else {
465             // disable fading on undefined key press
466             fade = false;
467         }
468
469         // fade out
470         if(fade)
471             fade_out(content, trans, colors, invert);
472
473         url_purge();
474     }
475
476     // disable ncurses
477     endwin();
478
479     // free ncurses memory
480     delwin(content);
481     if(reload == 0)
482         delwin(stdscr);
483
484     // return reload indicator (0 means no reload)
485     return reload;
486 }
487
488 void setup_list_strings(void)
489 {
490     const char *str;
491
492     /* utf8 can require 6 bytes */
493     if ((str = getenv("MDP_LIST_OPEN")) != NULL && strlen(str) <= 4*6) {
494         list_open1 = list_open2 = list_open3 = str;
495     } else {
496         if ((str = getenv("MDP_LIST_OPEN1")) != NULL && strlen(str) <= 4*6)
497             list_open1 = str;
498         if ((str = getenv("MDP_LIST_OPEN2")) != NULL && strlen(str) <= 4*6)
499             list_open2 = str;
500         if ((str = getenv("MDP_LIST_OPEN3")) != NULL && strlen(str) <= 4*6)
501             list_open3 = str;
502     }
503     if ((str = getenv("MDP_LIST_HEAD")) != NULL && strlen(str) <= 4*6) {
504         list_head1 = list_head2 = list_head3 = str;
505     } else {
506         if ((str = getenv("MDP_LIST_HEAD1")) != NULL && strlen(str) <= 4*6)
507             list_head1 = str;
508         if ((str = getenv("MDP_LIST_HEAD2")) != NULL && strlen(str) <= 4*6)
509             list_head2 = str;
510         if ((str = getenv("MDP_LIST_HEAD3")) != NULL && strlen(str) <= 4*6)
511             list_head3 = str;
512     }
513 }
514
515 void add_line(WINDOW *window, int y, int x, line_t *line, int max_cols, int colors, int nocodebg) {
516
517     int i; // increment
518     int offset = 0; // text offset
519
520     // move the cursor in position
521     wmove(window, y, x);
522
523     if(!line->text->value) {
524
525         // fill rest off line with spaces if we are in a code block
526         if(CHECK_BIT(line->bits, IS_CODE) && colors) {
527             if(colors && !nocodebg)
528                 wattron(window, COLOR_PAIR(CP_BLACK));
529             for(i = getcurx(window) - x; i < max_cols; i++)
530                 wprintw(window, "%s", " ");
531         }
532
533         // do nothing
534         return;
535     }
536
537     // IS_UNORDERED_LIST_3
538     if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_3)) {
539         offset = next_nonblank(line->text, 0);
540         char prompt[13 * 6];
541         int pos = 0, len, cnt;
542         len = sizeof(prompt) - pos;
543         cnt = snprintf(&prompt[pos], len, "%s", CHECK_BIT(line->bits, IS_UNORDERED_LIST_1)? list_open1 : "    ");
544         pos += (cnt > len - 1 ? len - 1 : cnt);
545         len = sizeof(prompt) - pos;
546         cnt = snprintf(&prompt[pos], len, "%s", CHECK_BIT(line->bits, IS_UNORDERED_LIST_2)? list_open2 : "    ");
547         pos += (cnt > len - 1 ? len - 1 : cnt);
548         len = sizeof(prompt) - pos;
549
550         if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_EXT)) {
551             snprintf(&prompt[pos], len, "%s", line->next && CHECK_BIT(line->next->bits, IS_UNORDERED_LIST_3)? list_open3 : "    ");
552         } else {
553             snprintf(&prompt[pos], len, "%s", list_head3);
554             offset += 2;
555         }
556
557         wprintw(window,
558                 "%s", prompt);
559
560         if(!CHECK_BIT(line->bits, IS_CODE))
561             inline_display(window, &line->text->value[offset], colors, nocodebg);
562
563     // IS_UNORDERED_LIST_2
564     } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_2)) {
565         offset = next_nonblank(line->text, 0);
566         char prompt[9 * 6];
567         int pos = 0, len, cnt;
568         len = sizeof(prompt) - pos;
569         cnt = snprintf(&prompt[pos], len, "%s", CHECK_BIT(line->bits, IS_UNORDERED_LIST_1)? list_open1 : "    ");
570         pos += (cnt > len - 1 ? len - 1 : cnt);
571         len = sizeof(prompt) - pos;
572
573         if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_EXT)) {
574             snprintf(&prompt[pos], len, "%s", line->next && CHECK_BIT(line->next->bits, IS_UNORDERED_LIST_2)? list_open2 : "    ");
575         } else {
576             snprintf(&prompt[pos], len, "%s", list_head2);
577             offset += 2;
578         }
579
580         wprintw(window,
581                 "%s", prompt);
582
583         if(!CHECK_BIT(line->bits, IS_CODE))
584             inline_display(window, &line->text->value[offset], colors, nocodebg);
585
586     // IS_UNORDERED_LIST_1
587     } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_1)) {
588         offset = next_nonblank(line->text, 0);
589         char prompt[5 * 6];
590
591         if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_EXT)) {
592             strcpy(&prompt[0], line->next && CHECK_BIT(line->next->bits, IS_UNORDERED_LIST_1)? list_open1 : "    ");
593         } else {
594             strcpy(&prompt[0], list_head1);
595             offset += 2;
596         }
597
598         wprintw(window,
599                 "%s", prompt);
600
601         if(!CHECK_BIT(line->bits, IS_CODE))
602             inline_display(window, &line->text->value[offset], colors, nocodebg);
603     }
604
605     // IS_CODE
606     if(CHECK_BIT(line->bits, IS_CODE)) {
607
608         if (!CHECK_BIT(line->bits, IS_TILDE_CODE) &&
609             !CHECK_BIT(line->bits, IS_GFM_CODE)) {
610             // set static offset for code
611             offset = CODE_INDENT;
612         }
613
614         // reverse color for code blocks
615         if(colors && !nocodebg)
616             wattron(window, COLOR_PAIR(CP_BLACK));
617
618         // print whole lines
619         waddwstr(window, &line->text->value[offset]);
620     }
621
622     if(!CHECK_BIT(line->bits, IS_UNORDERED_LIST_1) &&
623        !CHECK_BIT(line->bits, IS_UNORDERED_LIST_2) &&
624        !CHECK_BIT(line->bits, IS_UNORDERED_LIST_3) &&
625        !CHECK_BIT(line->bits, IS_CODE)) {
626
627         // IS_QUOTE
628         if(CHECK_BIT(line->bits, IS_QUOTE)) {
629             while(line->text->value[offset] == '>') {
630                 // print a reverse color block
631                 if(colors) {
632                     wattron(window, COLOR_PAIR(CP_BLACK));
633                     wprintw(window, "%s", " ");
634                     wattron(window, COLOR_PAIR(CP_WHITE));
635                     wprintw(window, "%s", " ");
636                 } else {
637                     wprintw(window, "%s", ">");
638                 }
639
640                 // find next quote or break
641                 offset++;
642                 if(line->text->value[offset] == ' ')
643                     offset = next_word(line->text, offset);
644             }
645
646             inline_display(window, &line->text->value[offset], colors, nocodebg);
647         } else {
648
649             // IS_CENTER
650             if(CHECK_BIT(line->bits, IS_CENTER)) {
651                 if(line->length < max_cols) {
652                     wmove(window, y, x + ((max_cols - line->length) / 2));
653                 }
654             }
655
656             // IS_H1 || IS_H2
657             if(CHECK_BIT(line->bits, IS_H1) || CHECK_BIT(line->bits, IS_H2)) {
658
659                 // set headline color
660                 if(colors)
661                     wattron(window, COLOR_PAIR(CP_BLUE));
662
663                 // enable underline for H1
664                 if(CHECK_BIT(line->bits, IS_H1))
665                     wattron(window, A_UNDERLINE);
666
667                 // skip hashes
668                 while(line->text->value[offset] == '#')
669                     offset = next_word(line->text, offset);
670
671                 // print whole lines
672                 waddwstr(window, &line->text->value[offset]);
673
674                 wattroff(window, A_UNDERLINE);
675
676             // no line-wide markdown
677             } else {
678
679                 inline_display(window, &line->text->value[offset], colors, nocodebg);
680             }
681         }
682     }
683
684     // fill rest off line with spaces
685     // we only need this if the color is inverted (e.g. code-blocks),
686     // to ensure the background fades too
687     if(CHECK_BIT(line->bits, IS_CODE))
688         for(i = getcurx(window) - x; i < max_cols; i++)
689             wprintw(window, "%s", " ");
690
691     // reset to default color
692     if(colors)
693         wattron(window, COLOR_PAIR(CP_WHITE));
694     wattroff(window, A_UNDERLINE);
695 }
696
697 void inline_display(WINDOW *window, const wchar_t *c, const int colors, int nocodebg) {
698     const static wchar_t *special = L"\\*_`!["; // list of interpreted chars
699     const wchar_t *i = c; // iterator
700     const wchar_t *start_link_name, *start_url;
701     int length_link_name, url_num;
702     cstack_t *stack = cstack_init();
703
704
705     // for each char in line
706     for(; *i; i++) {
707
708         // if char is in special char list
709         if(wcschr(special, *i)) {
710
711             // closing special char (or second backslash)
712             // only if not followed by :alnum:
713             if((stack->top)(stack, *i) &&
714                (!iswalnum(i[1]) || *(i + 1) == L'\0' || *i == L'\\')) {
715
716                 switch(*i) {
717                     // print escaped backslash
718                     case L'\\':
719                         waddnwstr(window, i, 1);
720                         break;
721                     // disable highlight
722                     case L'*':
723                         if(colors)
724                             wattron(window, COLOR_PAIR(CP_WHITE));
725                         break;
726                     // disable underline
727                     case L'_':
728                         wattroff(window, A_UNDERLINE);
729                         break;
730                     // disable inline code
731                     case L'`':
732                         if(colors)
733                             wattron(window, COLOR_PAIR(CP_WHITE));
734                         break;
735                 }
736
737                 // remove top special char from stack
738                 (stack->pop)(stack);
739
740             // treat special as regular char
741             } else if((stack->top)(stack, L'\\')) {
742                 waddnwstr(window, i, 1);
743
744                 // remove backslash from stack
745                 (stack->pop)(stack);
746
747             // opening special char
748             } else {
749
750                 // emphasis or code span can start after new-line or space only
751                 // and of cause after another emphasis markup
752                 //TODO this condition looks ugly
753                 if(i == c ||
754                    iswspace(*(i - 1)) ||
755                    ((iswspace(*(i - 1)) || *(i - 1) == L'*' || *(i - 1) == L'_') &&
756                     ((i - 1) == c || iswspace(*(i - 2)))) ||
757                    *i == L'\\') {
758
759                     // url in pandoc style
760                     if ((*i == L'[' && wcschr(i, L']')) ||
761                         (*i == L'!' && *(i + 1) == L'[' && wcschr(i, L']'))) {
762
763                         if (*i == L'!') i++;
764
765                         if (wcschr(i, L']')[1] == L'(' && wcschr(i, L')')) {
766                             i++;
767
768                             // turn higlighting and underlining on
769                             if (colors)
770                                 wattron(window, COLOR_PAIR(CP_BLUE));
771                             wattron(window, A_UNDERLINE);
772
773                             start_link_name = i;
774
775                             // print the content of the label
776                             // the label is printed as is
777                             do {
778                                 waddnwstr(window, i, 1);
779                                 i++;
780                             } while (*i != L']');
781
782                             length_link_name = i - 1 - start_link_name;
783
784                             i++;
785                             i++;
786
787                             start_url = i;
788
789                             while (*i != L')') i++;
790
791                             url_num = url_add(start_link_name, length_link_name, start_url, i - start_url, 0, 0);
792
793                             wprintw(window, " [%d]", url_num);
794
795                             // turn highlighting and underlining off
796                             wattroff(window, A_UNDERLINE);
797                             wattron(window, COLOR_PAIR(CP_WHITE));
798
799                         } else {
800                             wprintw(window, "[");
801                         }
802
803                     } else switch(*i) {
804                         // enable highlight
805                         case L'*':
806                             if(colors)
807                                 wattron(window, COLOR_PAIR(CP_RED));
808                             break;
809                         // enable underline
810                         case L'_':
811                             wattron(window, A_UNDERLINE);
812                             break;
813                         // enable inline code
814                         case L'`':
815                             if(colors && !nocodebg)
816                                 wattron(window, COLOR_PAIR(CP_BLACK));
817                             break;
818                         // do nothing for backslashes
819                     }
820
821                     // push special char to stack
822                     (stack->push)(stack, *i);
823
824                 } else {
825                     waddnwstr(window, i, 1);
826                 }
827             }
828
829         } else {
830             // remove backslash from stack
831             if((stack->top)(stack, L'\\'))
832                 (stack->pop)(stack);
833
834             // print regular char
835             waddnwstr(window, i, 1);
836         }
837     }
838
839     // pop stack until empty to prevent formated trailing spaces
840     while(!(stack->empty)(stack)) {
841         switch((stack->pop)(stack)) {
842             // disable highlight
843             case L'*':
844                 if(colors)
845                     wattron(window, COLOR_PAIR(CP_WHITE));
846                 break;
847             // disable underline
848             case L'_':
849                 wattroff(window, A_UNDERLINE);
850                 break;
851             // disable inline code
852             case L'`':
853                 if(colors)
854                     wattron(window, COLOR_PAIR(CP_WHITE));
855                 break;
856             // do nothing for backslashes
857         }
858     }
859
860     (stack->delete)(stack);
861 }
862
863 void fade_out(WINDOW *window, int trans, int colors, int invert) {
864     int i; // increment
865     if(colors && COLORS == 256) {
866         for(i = 22; i >= 0; i--) {
867
868             // dim color pairs
869             if(invert) {
870                 init_pair(CP_WHITE, white_ramp_invert[i], trans);
871                 init_pair(CP_BLUE, blue_ramp_invert[i], trans);
872                 init_pair(CP_RED, red_ramp_invert[i], trans);
873                 init_pair(CP_BLACK, 15, white_ramp_invert[i]);
874             } else {
875                 init_pair(CP_WHITE, white_ramp[i], trans);
876                 init_pair(CP_BLUE, blue_ramp[i], trans);
877                 init_pair(CP_RED, red_ramp[i], trans);
878                 init_pair(CP_BLACK, 16, white_ramp[i]);
879             }
880
881             // refresh virtual screen with new color
882             wnoutrefresh(window);
883
884             // compare virtual screen to physical screen and does the actual updates
885             doupdate();
886
887             // delay for our eyes to recognize the change
888             usleep(FADE_DELAY);
889         }
890     }
891 }
892
893 void fade_in(WINDOW *window, int trans, int colors, int invert) {
894     int i; // increment
895     if(colors && COLORS == 256) {
896         for(i = 0; i <= 23; i++) {
897
898             // brighten color pairs
899             if(invert) {
900                 init_pair(CP_WHITE, white_ramp_invert[i], trans);
901                 init_pair(CP_BLUE, blue_ramp_invert[i], trans);
902                 init_pair(CP_RED, red_ramp_invert[i], trans);
903                 init_pair(CP_BLACK, 15, white_ramp_invert[i]);
904             } else {
905                 init_pair(CP_WHITE, white_ramp[i], trans);
906                 init_pair(CP_BLUE, blue_ramp[i], trans);
907                 init_pair(CP_RED, red_ramp[i], trans);
908                 init_pair(CP_BLACK, 16, white_ramp[i]);
909             }
910
911             // refresh virtual screen with new color
912             wnoutrefresh(window);
913
914             // compare virtual screen to physical screen and does the actual updates
915             doupdate();
916
917             // delay for our eyes to recognize the change
918             usleep(FADE_DELAY);
919         }
920     }
921 }
922
923 int int_length (int val) {
924     int l = 1;
925     while(val > 9) {
926         l++;
927         val /= 10;
928     }
929     return l;
930 }
931
932 int get_slide_number(char init) {
933     int retval = init - '0';
934     int c;
935     // block for tenths of a second when using getch, ERR if no input
936     halfdelay(GOTO_SLIDE_DELAY);
937     while((c = getch()) != ERR) {
938         if (c < '0' || c > '9') {
939             retval = -1;
940             break;
941         }
942         retval = (retval * 10) + (c - '0');
943     }
944     nocbreak();     // cancel half delay mode
945     cbreak();       // go back to cbreak
946     return retval;
947 }
948
949 bool evaluate_binding(const int bindings[], char c) {
950     int binding;
951     int ind = 0; 
952     while((binding = bindings[ind]) != 0) {
953         if (c == binding) return true;
954         ind++;
955     }
956     return false;
957 }
958