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