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