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