fixed right alignment
[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         // set static offset for code
581         offset = CODE_INDENT;
582
583         // reverse color for code blocks
584         if(colors)
585             wattron(window, COLOR_PAIR(CP_BLACK));
586
587         // print whole lines
588         waddwstr(window, &line->text->value[offset]);
589     }
590
591     if(!CHECK_BIT(line->bits, IS_UNORDERED_LIST_1) &&
592        !CHECK_BIT(line->bits, IS_UNORDERED_LIST_2) &&
593        !CHECK_BIT(line->bits, IS_UNORDERED_LIST_3) &&
594        !CHECK_BIT(line->bits, IS_CODE)) {
595
596         // IS_QUOTE
597         if(CHECK_BIT(line->bits, IS_QUOTE)) {
598             while(line->text->value[offset] == '>') {
599                 // print a reverse color block
600                 if(colors) {
601                     wattron(window, COLOR_PAIR(CP_BLACK));
602                     wprintw(window, "%s", " ");
603                     wattron(window, COLOR_PAIR(CP_WHITE));
604                     wprintw(window, "%s", " ");
605                 } else {
606                     wprintw(window, "%s", ">");
607                 }
608
609                 // find next quote or break
610                 offset++;
611                 if(line->text->value[offset] == ' ')
612                     offset = next_word(line->text, offset);
613             }
614
615             inline_display(window, &line->text->value[offset], colors);
616         } else {
617
618             // IS_CENTER
619             if(CHECK_BIT(line->bits, IS_CENTER)) {
620                 if(line->length < max_cols) {
621                     wmove(window, y, x + ((max_cols - line->length) / 2));
622                 }
623             }
624
625             // IS_H1 || IS_H2
626             if(CHECK_BIT(line->bits, IS_H1) || CHECK_BIT(line->bits, IS_H2)) {
627
628                 // set headline color
629                 if(colors)
630                     wattron(window, COLOR_PAIR(CP_BLUE));
631
632                 // enable underline for H1
633                 if(CHECK_BIT(line->bits, IS_H1))
634                     wattron(window, A_UNDERLINE);
635
636                 // skip hashes
637                 while(line->text->value[offset] == '#')
638                     offset = next_word(line->text, offset);
639
640                 // print whole lines
641                 waddwstr(window, &line->text->value[offset]);
642
643                 wattroff(window, A_UNDERLINE);
644
645             // no line-wide markdown
646             } else {
647
648                 inline_display(window, &line->text->value[offset], colors);
649             }
650         }
651     }
652
653     // fill rest off line with spaces
654     // we only need this if the color is inverted (e.g. code-blocks),
655     // to ensure the background fades too
656     if(CHECK_BIT(line->bits, IS_CODE))
657         for(i = getcurx(window) - x; i < max_cols; i++)
658             wprintw(window, "%s", " ");
659
660     // reset to default color
661     if(colors)
662         wattron(window, COLOR_PAIR(CP_WHITE));
663     wattroff(window, A_UNDERLINE);
664 }
665
666 void inline_display(WINDOW *window, const wchar_t *c, const int colors) {
667     const static wchar_t *special = L"\\*_`!["; // list of interpreted chars
668     const wchar_t *i = c; // iterator
669     const wchar_t *start_link_name, *start_url;
670     int length_link_name, url_num;
671     cstack_t *stack = cstack_init();
672
673
674     // for each char in line
675     for(; *i; i++) {
676
677         // if char is in special char list
678         if(wcschr(special, *i)) {
679
680             // closing special char (or second backslash)
681             // only if not followed by :alnum:
682             if((stack->top)(stack, *i) &&
683                (!iswalnum(i[1]) || *(i + 1) == L'\0' || *i == L'\\')) {
684
685                 switch(*i) {
686                     // print escaped backslash
687                     case L'\\':
688                         waddnwstr(window, i, 1);
689                         break;
690                     // disable highlight
691                     case L'*':
692                         if(colors)
693                             wattron(window, COLOR_PAIR(CP_WHITE));
694                         break;
695                     // disable underline
696                     case L'_':
697                         wattroff(window, A_UNDERLINE);
698                         break;
699                     // disable inline code
700                     case L'`':
701                         if(colors)
702                             wattron(window, COLOR_PAIR(CP_WHITE));
703                         break;
704                 }
705
706                 // remove top special char from stack
707                 (stack->pop)(stack);
708
709             // treat special as regular char
710             } else if((stack->top)(stack, L'\\')) {
711                 waddnwstr(window, i, 1);
712
713                 // remove backslash from stack
714                 (stack->pop)(stack);
715
716             // opening special char
717             } else {
718
719                 // emphasis or code span can start after new-line or space only
720                 // and of cause after another emphasis markup
721                 //TODO this condition looks ugly
722                 if(i == c ||
723                    iswspace(*(i - 1)) ||
724                    ((iswspace(*(i - 1)) || *(i - 1) == L'*' || *(i - 1) == L'_') &&
725                     ((i - 1) == c || iswspace(*(i - 2)))) ||
726                    *i == L'\\') {
727
728                     // url in pandoc style
729                     if ((*i == L'[' && wcschr(i, L']')) ||
730                         (*i == L'!' && *(i + 1) == L'[' && wcschr(i, L']'))) {
731
732                         if (*i == L'!') i++;
733
734                         if (wcschr(i, L']')[1] == L'(' && wcschr(i, L')')) {
735                             i++;
736
737                             // turn higlighting and underlining on
738                             if (colors)
739                                 wattron(window, COLOR_PAIR(CP_BLUE));
740                             wattron(window, A_UNDERLINE);
741
742                             start_link_name = i;
743
744                             // print the content of the label
745                             // the label is printed as is
746                             do {
747                                 waddnwstr(window, i, 1);
748                                 i++;
749                             } while (*i != L']');
750
751                             length_link_name = i - 1 - start_link_name;
752
753                             i++;
754                             i++;
755
756                             start_url = i;
757
758                             while (*i != L')') i++;
759
760                             url_num = url_add(start_link_name, length_link_name, start_url, i - start_url, 0, 0);
761
762                             wprintw(window, " [%d]", url_num);
763
764                             // turn highlighting and underlining off
765                             wattroff(window, A_UNDERLINE);
766                             wattron(window, COLOR_PAIR(CP_WHITE));
767
768                         } else {
769                             wprintw(window, "[");
770                         }
771
772                     } else switch(*i) {
773                         // enable highlight
774                         case L'*':
775                             if(colors)
776                                 wattron(window, COLOR_PAIR(CP_RED));
777                             break;
778                         // enable underline
779                         case L'_':
780                             wattron(window, A_UNDERLINE);
781                             break;
782                         // enable inline code
783                         case L'`':
784                             if(colors)
785                                 wattron(window, COLOR_PAIR(CP_BLACK));
786                             break;
787                         // do nothing for backslashes
788                     }
789
790                     // push special char to stack
791                     (stack->push)(stack, *i);
792
793                 } else {
794                     waddnwstr(window, i, 1);
795                 }
796             }
797
798         } else {
799             // remove backslash from stack
800             if((stack->top)(stack, L'\\'))
801                 (stack->pop)(stack);
802
803             // print regular char
804             waddnwstr(window, i, 1);
805         }
806     }
807
808     // pop stack until empty to prevent formated trailing spaces
809     while(!(stack->empty)(stack)) {
810         switch((stack->pop)(stack)) {
811             // disable highlight
812             case L'*':
813                 if(colors)
814                     wattron(window, COLOR_PAIR(CP_WHITE));
815                 break;
816             // disable underline
817             case L'_':
818                 wattroff(window, A_UNDERLINE);
819                 break;
820             // disable inline code
821             case L'`':
822                 if(colors)
823                     wattron(window, COLOR_PAIR(CP_WHITE));
824                 break;
825             // do nothing for backslashes
826         }
827     }
828
829     (stack->delete)(stack);
830 }
831
832 void fade_out(WINDOW *window, int trans, int colors, int invert) {
833     int i; // increment
834     if(colors && COLORS == 256) {
835         for(i = 22; i >= 0; i--) {
836
837             // dim color pairs
838             if(invert) {
839                 init_pair(CP_WHITE, white_ramp_invert[i], trans);
840                 init_pair(CP_BLUE, blue_ramp_invert[i], trans);
841                 init_pair(CP_RED, red_ramp_invert[i], trans);
842                 init_pair(CP_BLACK, 15, white_ramp_invert[i]);
843             } else {
844                 init_pair(CP_WHITE, white_ramp[i], trans);
845                 init_pair(CP_BLUE, blue_ramp[i], trans);
846                 init_pair(CP_RED, red_ramp[i], trans);
847                 init_pair(CP_BLACK, 16, white_ramp[i]);
848             }
849
850             // refresh window with new color
851             wrefresh(window);
852
853             // delay for our eyes to recognize the change
854             usleep(FADE_DELAY);
855         }
856     }
857 }
858
859 void fade_in(WINDOW *window, int trans, int colors, int invert) {
860     int i; // increment
861     if(colors && COLORS == 256) {
862         for(i = 0; i <= 23; i++) {
863
864             // brighten color pairs
865             if(invert) {
866                 init_pair(CP_WHITE, white_ramp_invert[i], trans);
867                 init_pair(CP_BLUE, blue_ramp_invert[i], trans);
868                 init_pair(CP_RED, red_ramp_invert[i], trans);
869                 init_pair(CP_BLACK, 15, white_ramp_invert[i]);
870             } else {
871                 init_pair(CP_WHITE, white_ramp[i], trans);
872                 init_pair(CP_BLUE, blue_ramp[i], trans);
873                 init_pair(CP_RED, red_ramp[i], trans);
874                 init_pair(CP_BLACK, 16, white_ramp[i]);
875             }
876
877             // refresh window with new color
878             wrefresh(window);
879
880             // delay for our eyes to recognize the change
881             usleep(FADE_DELAY);
882         }
883     }
884 }
885
886 int int_length (int val) {
887     int l = 1;
888     while(val > 9) {
889         l++;
890         val /= 10;
891     }
892     return l;
893 }
894
895 int get_slide_number(char init) {
896     int retval = init - '0';
897     char c;
898     // block for tenths of a second when using getch, ERR if no input
899     halfdelay(GOTO_SLIDE_DELAY);
900     while((c = getch()) != ERR) {
901         if (c < '0' || c > '9') {
902             retval = -1;
903             break;
904         }
905         retval = (retval * 10) + (c - '0');
906     }
907     nocbreak();     // cancel half delay mode
908     cbreak();       // go back to cbreak
909     return retval;
910 }