Added configurable list string from env.
[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
528 export MDP_LIST_OPEN3="    "
529 export MDP_LIST_OPEN2="    "
530 export MDP_LIST_OPEN3="    "
531 export MDP_LIST_HEAD1=" ■  "
532 export MDP_LIST_HEAD2=" ▫  "
533 export MDP_LIST_HEAD3=" ●  "
534
535 export MDP_LIST_OPEN2=" │  "
536 export MDP_LIST_OPEN2=" │  "
537 export MDP_LIST_OPEN3=" │  "
538 export MDP_LIST_HEAD1=" ▇─ "
539 export MDP_LIST_HEAD2=" ▓─ "
540 export MDP_LIST_HEAD3=" ▒─ "
541 *
542 */
543 void setup_list_strings(void)
544 {
545     const char *str;
546
547     if ((str = getenv("MDP_LIST_OPEN")) != NULL) {
548         list_open1 = list_open2 = list_open3 = str;
549     } else {
550         if ((str = getenv("MDP_LIST_OPEN1")) != NULL)
551             list_open1 = str;
552         if ((str = getenv("MDP_LIST_OPEN2")) != NULL)
553             list_open2 = str;
554         if ((str = getenv("MDP_LIST_OPEN3")) != NULL)
555             list_open3 = str;
556     }
557     if ((str = getenv("MDP_LIST_HEAD")) != NULL) {
558         list_head1 = list_head2 = list_head3 = str;
559     } else {
560         if ((str = getenv("MDP_LIST_HEAD1")) != NULL)
561             list_head1 = str;
562         if ((str = getenv("MDP_LIST_HEAD2")) != NULL)
563             list_head2 = str;
564         if ((str = getenv("MDP_LIST_HEAD3")) != NULL)
565             list_head3 = str;
566     }
567 }
568
569 void add_line(WINDOW *window, int y, int x, line_t *line, int max_cols, int colors) {
570
571     int i; // increment
572     int offset = 0; // text offset
573
574     // move the cursor in position
575     wmove(window, y, x);
576
577     if(!line->text->value) {
578
579         // fill rest off line with spaces if we are in a code block
580         if(CHECK_BIT(line->bits, IS_CODE) && colors) {
581             if(colors)
582                 wattron(window, COLOR_PAIR(CP_BLACK));
583             for(i = getcurx(window) - x; i < max_cols; i++)
584                 wprintw(window, "%s", " ");
585         }
586
587         // do nothing
588         return;
589     }
590
591     // IS_UNORDERED_LIST_3
592     if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_3)) {
593         offset = next_nonblank(line->text, 0);
594         char prompt[13 * 6];
595         int pos = 0, len, cnt;
596         len = sizeof(prompt) - pos;
597         cnt = snprintf(&prompt[pos], len, "%s", CHECK_BIT(line->bits, IS_UNORDERED_LIST_1)? list_open1 : "    ");
598         pos += (cnt > len - 1 ? len - 1 : cnt);
599         len = sizeof(prompt) - pos;
600         cnt = snprintf(&prompt[pos], len, "%s", CHECK_BIT(line->bits, IS_UNORDERED_LIST_2)? list_open2 : "    ");
601         pos += (cnt > len - 1 ? len - 1 : cnt);
602         len = sizeof(prompt) - pos;
603
604         if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_EXT)) {
605             snprintf(&prompt[pos], len, "%s", line->next && CHECK_BIT(line->next->bits, IS_UNORDERED_LIST_3)? list_open3 : "    ");
606         } else {
607             snprintf(&prompt[pos], len, "%s", list_head3);
608             offset += 2;
609         }
610
611         wprintw(window,
612                 "%s", prompt);
613
614         if(!CHECK_BIT(line->bits, IS_CODE))
615             inline_display(window, &line->text->value[offset], colors);
616
617     // IS_UNORDERED_LIST_2
618     } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_2)) {
619         offset = next_nonblank(line->text, 0);
620         char prompt[9 * 6];
621         int pos = 0, len, cnt;
622         len = sizeof(prompt) - pos;
623         cnt = snprintf(&prompt[pos], len, "%s", CHECK_BIT(line->bits, IS_UNORDERED_LIST_1)? list_open1 : "    ");
624         pos += (cnt > len - 1 ? len - 1 : cnt);
625         len = sizeof(prompt) - pos;
626
627         if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_EXT)) {
628             snprintf(&prompt[pos], len, "%s", line->next && CHECK_BIT(line->next->bits, IS_UNORDERED_LIST_2)? list_open2 : "    ");
629         } else {
630             snprintf(&prompt[pos], len, "%s", list_head2);
631             offset += 2;
632         }
633
634         wprintw(window,
635                 "%s", prompt);
636
637         if(!CHECK_BIT(line->bits, IS_CODE))
638             inline_display(window, &line->text->value[offset], colors);
639
640     // IS_UNORDERED_LIST_1
641     } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_1)) {
642         offset = next_nonblank(line->text, 0);
643         char prompt[5 * 6];
644
645         if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_EXT)) {
646             strcpy(&prompt[0], line->next && CHECK_BIT(line->next->bits, IS_UNORDERED_LIST_1)? list_open1 : "    ");
647         } else {
648             strcpy(&prompt[0], list_head1);
649             offset += 2;
650         }
651
652         wprintw(window,
653                 "%s", prompt);
654
655         if(!CHECK_BIT(line->bits, IS_CODE))
656             inline_display(window, &line->text->value[offset], colors);
657     }
658
659     // IS_CODE
660     if(CHECK_BIT(line->bits, IS_CODE)) {
661
662         if (!CHECK_BIT(line->bits, IS_TILDE_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     char 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 }