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