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