version bump
[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) 2018 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 = 1;
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             !CHECK_BIT(line->bits, IS_GFM_CODE)) {
661             // set static offset for code
662             offset = CODE_INDENT;
663         }
664
665         // reverse color for code blocks
666         if(colors)
667             wattron(window, COLOR_PAIR(CP_BLACK));
668
669         // print whole lines
670         waddwstr(window, &line->text->value[offset]);
671     }
672
673     if(!CHECK_BIT(line->bits, IS_UNORDERED_LIST_1) &&
674        !CHECK_BIT(line->bits, IS_UNORDERED_LIST_2) &&
675        !CHECK_BIT(line->bits, IS_UNORDERED_LIST_3) &&
676        !CHECK_BIT(line->bits, IS_CODE)) {
677
678         // IS_QUOTE
679         if(CHECK_BIT(line->bits, IS_QUOTE)) {
680             while(line->text->value[offset] == '>') {
681                 // print a reverse color block
682                 if(colors) {
683                     wattron(window, COLOR_PAIR(CP_BLACK));
684                     wprintw(window, "%s", " ");
685                     wattron(window, COLOR_PAIR(CP_WHITE));
686                     wprintw(window, "%s", " ");
687                 } else {
688                     wprintw(window, "%s", ">");
689                 }
690
691                 // find next quote or break
692                 offset++;
693                 if(line->text->value[offset] == ' ')
694                     offset = next_word(line->text, offset);
695             }
696
697             inline_display(window, &line->text->value[offset], colors);
698         } else {
699
700             // IS_CENTER
701             if(CHECK_BIT(line->bits, IS_CENTER)) {
702                 if(line->length < max_cols) {
703                     wmove(window, y, x + ((max_cols - line->length) / 2));
704                 }
705             }
706
707             // IS_H1 || IS_H2
708             if(CHECK_BIT(line->bits, IS_H1) || CHECK_BIT(line->bits, IS_H2)) {
709
710                 // set headline color
711                 if(colors)
712                     wattron(window, COLOR_PAIR(CP_BLUE));
713
714                 // enable underline for H1
715                 if(CHECK_BIT(line->bits, IS_H1))
716                     wattron(window, A_UNDERLINE);
717
718                 // skip hashes
719                 while(line->text->value[offset] == '#')
720                     offset = next_word(line->text, offset);
721
722                 // print whole lines
723                 waddwstr(window, &line->text->value[offset]);
724
725                 wattroff(window, A_UNDERLINE);
726
727             // no line-wide markdown
728             } else {
729
730                 inline_display(window, &line->text->value[offset], colors);
731             }
732         }
733     }
734
735     // fill rest off line with spaces
736     // we only need this if the color is inverted (e.g. code-blocks),
737     // to ensure the background fades too
738     if(CHECK_BIT(line->bits, IS_CODE))
739         for(i = getcurx(window) - x; i < max_cols; i++)
740             wprintw(window, "%s", " ");
741
742     // reset to default color
743     if(colors)
744         wattron(window, COLOR_PAIR(CP_WHITE));
745     wattroff(window, A_UNDERLINE);
746 }
747
748 void inline_display(WINDOW *window, const wchar_t *c, const int colors) {
749     const static wchar_t *special = L"\\*_`!["; // list of interpreted chars
750     const wchar_t *i = c; // iterator
751     const wchar_t *start_link_name, *start_url;
752     int length_link_name, url_num;
753     cstack_t *stack = cstack_init();
754
755
756     // for each char in line
757     for(; *i; i++) {
758
759         // if char is in special char list
760         if(wcschr(special, *i)) {
761
762             // closing special char (or second backslash)
763             // only if not followed by :alnum:
764             if((stack->top)(stack, *i) &&
765                (!iswalnum(i[1]) || *(i + 1) == L'\0' || *i == L'\\')) {
766
767                 switch(*i) {
768                     // print escaped backslash
769                     case L'\\':
770                         waddnwstr(window, i, 1);
771                         break;
772                     // disable highlight
773                     case L'*':
774                         if(colors)
775                             wattron(window, COLOR_PAIR(CP_WHITE));
776                         break;
777                     // disable underline
778                     case L'_':
779                         wattroff(window, A_UNDERLINE);
780                         break;
781                     // disable inline code
782                     case L'`':
783                         if(colors)
784                             wattron(window, COLOR_PAIR(CP_WHITE));
785                         break;
786                 }
787
788                 // remove top special char from stack
789                 (stack->pop)(stack);
790
791             // treat special as regular char
792             } else if((stack->top)(stack, L'\\')) {
793                 waddnwstr(window, i, 1);
794
795                 // remove backslash from stack
796                 (stack->pop)(stack);
797
798             // opening special char
799             } else {
800
801                 // emphasis or code span can start after new-line or space only
802                 // and of cause after another emphasis markup
803                 //TODO this condition looks ugly
804                 if(i == c ||
805                    iswspace(*(i - 1)) ||
806                    ((iswspace(*(i - 1)) || *(i - 1) == L'*' || *(i - 1) == L'_') &&
807                     ((i - 1) == c || iswspace(*(i - 2)))) ||
808                    *i == L'\\') {
809
810                     // url in pandoc style
811                     if ((*i == L'[' && wcschr(i, L']')) ||
812                         (*i == L'!' && *(i + 1) == L'[' && wcschr(i, L']'))) {
813
814                         if (*i == L'!') i++;
815
816                         if (wcschr(i, L']')[1] == L'(' && wcschr(i, L')')) {
817                             i++;
818
819                             // turn higlighting and underlining on
820                             if (colors)
821                                 wattron(window, COLOR_PAIR(CP_BLUE));
822                             wattron(window, A_UNDERLINE);
823
824                             start_link_name = i;
825
826                             // print the content of the label
827                             // the label is printed as is
828                             do {
829                                 waddnwstr(window, i, 1);
830                                 i++;
831                             } while (*i != L']');
832
833                             length_link_name = i - 1 - start_link_name;
834
835                             i++;
836                             i++;
837
838                             start_url = i;
839
840                             while (*i != L')') i++;
841
842                             url_num = url_add(start_link_name, length_link_name, start_url, i - start_url, 0, 0);
843
844                             wprintw(window, " [%d]", url_num);
845
846                             // turn highlighting and underlining off
847                             wattroff(window, A_UNDERLINE);
848                             wattron(window, COLOR_PAIR(CP_WHITE));
849
850                         } else {
851                             wprintw(window, "[");
852                         }
853
854                     } else switch(*i) {
855                         // enable highlight
856                         case L'*':
857                             if(colors)
858                                 wattron(window, COLOR_PAIR(CP_RED));
859                             break;
860                         // enable underline
861                         case L'_':
862                             wattron(window, A_UNDERLINE);
863                             break;
864                         // enable inline code
865                         case L'`':
866                             if(colors)
867                                 wattron(window, COLOR_PAIR(CP_BLACK));
868                             break;
869                         // do nothing for backslashes
870                     }
871
872                     // push special char to stack
873                     (stack->push)(stack, *i);
874
875                 } else {
876                     waddnwstr(window, i, 1);
877                 }
878             }
879
880         } else {
881             // remove backslash from stack
882             if((stack->top)(stack, L'\\'))
883                 (stack->pop)(stack);
884
885             // print regular char
886             waddnwstr(window, i, 1);
887         }
888     }
889
890     // pop stack until empty to prevent formated trailing spaces
891     while(!(stack->empty)(stack)) {
892         switch((stack->pop)(stack)) {
893             // disable highlight
894             case L'*':
895                 if(colors)
896                     wattron(window, COLOR_PAIR(CP_WHITE));
897                 break;
898             // disable underline
899             case L'_':
900                 wattroff(window, A_UNDERLINE);
901                 break;
902             // disable inline code
903             case L'`':
904                 if(colors)
905                     wattron(window, COLOR_PAIR(CP_WHITE));
906                 break;
907             // do nothing for backslashes
908         }
909     }
910
911     (stack->delete)(stack);
912 }
913
914 void fade_out(WINDOW *window, int trans, int colors, int invert) {
915     int i; // increment
916     if(colors && COLORS == 256) {
917         for(i = 22; i >= 0; i--) {
918
919             // dim color pairs
920             if(invert) {
921                 init_pair(CP_WHITE, white_ramp_invert[i], trans);
922                 init_pair(CP_BLUE, blue_ramp_invert[i], trans);
923                 init_pair(CP_RED, red_ramp_invert[i], trans);
924                 init_pair(CP_BLACK, 15, white_ramp_invert[i]);
925             } else {
926                 init_pair(CP_WHITE, white_ramp[i], trans);
927                 init_pair(CP_BLUE, blue_ramp[i], trans);
928                 init_pair(CP_RED, red_ramp[i], trans);
929                 init_pair(CP_BLACK, 16, white_ramp[i]);
930             }
931
932             // refresh virtual screen with new color
933             wnoutrefresh(window);
934
935             // compare virtual screen to physical screen and does the actual updates
936             doupdate();
937
938             // delay for our eyes to recognize the change
939             usleep(FADE_DELAY);
940         }
941     }
942 }
943
944 void fade_in(WINDOW *window, int trans, int colors, int invert) {
945     int i; // increment
946     if(colors && COLORS == 256) {
947         for(i = 0; i <= 23; i++) {
948
949             // brighten color pairs
950             if(invert) {
951                 init_pair(CP_WHITE, white_ramp_invert[i], trans);
952                 init_pair(CP_BLUE, blue_ramp_invert[i], trans);
953                 init_pair(CP_RED, red_ramp_invert[i], trans);
954                 init_pair(CP_BLACK, 15, white_ramp_invert[i]);
955             } else {
956                 init_pair(CP_WHITE, white_ramp[i], trans);
957                 init_pair(CP_BLUE, blue_ramp[i], trans);
958                 init_pair(CP_RED, red_ramp[i], trans);
959                 init_pair(CP_BLACK, 16, white_ramp[i]);
960             }
961
962             // refresh virtual screen with new color
963             wnoutrefresh(window);
964
965             // compare virtual screen to physical screen and does the actual updates
966             doupdate();
967
968             // delay for our eyes to recognize the change
969             usleep(FADE_DELAY);
970         }
971     }
972 }
973
974 int int_length (int val) {
975     int l = 1;
976     while(val > 9) {
977         l++;
978         val /= 10;
979     }
980     return l;
981 }
982
983 int get_slide_number(char init) {
984     int retval = init - '0';
985     int c;
986     // block for tenths of a second when using getch, ERR if no input
987     halfdelay(GOTO_SLIDE_DELAY);
988     while((c = getch()) != ERR) {
989         if (c < '0' || c > '9') {
990             retval = -1;
991             break;
992         }
993         retval = (retval * 10) + (c - '0');
994     }
995     nocbreak();     // cancel half delay mode
996     cbreak();       // go back to cbreak
997     return retval;
998 }