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