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