1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
|
/*
* Copyright (C) 2012-2015 Robin Haberkorn
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <unistd.h>
#include <locale.h>
#include <errno.h>
#include <glib.h>
#include <glib/gprintf.h>
#include <glib/gstdio.h>
#include <curses.h>
#ifdef HAVE_TIGETSTR
#include <term.h>
/*
* Some macros in term.h interfere with our code.
*/
#undef lines
#endif
#include <Scintilla.h>
#include <ScintillaTerm.h>
#ifdef EMSCRIPTEN
#include <emscripten.h>
#endif
#include "sciteco.h"
#include "string-utils.h"
#include "cmdline.h"
#include "qregisters.h"
#include "ring.h"
#include "interface.h"
#include "interface-curses.h"
#ifdef HAVE_WINDOWS_H
/* here it shouldn't cause conflicts with other headers */
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#endif
/**
* Whether we have PDCurses-only routines:
* Could be 0, even on PDCurses
*/
#ifndef PDCURSES
#define PDCURSES 0
#endif
/**
* Whether we're on PDCurses/win32
*/
#if defined(__PDCURSES__) && defined(G_OS_WIN32) && \
!defined(PDCURSES_WIN32A)
#define PDCURSES_WIN32
#endif
#ifdef NCURSES_VERSION
#ifdef G_OS_UNIX
/**
* Whether we're on ncurses/UNIX
*/
#define NCURSES_UNIX
#elif defined(G_OS_WIN32)
/**
* Whether we're on ncurses/win32 console
*/
#define NCURSES_WIN32
#endif
#endif
namespace SciTECO {
extern "C" {
/*
* PDCurses/win32a by default assigns functions to certain
* keys like CTRL+V, CTRL++, CTRL+- and CTRL+=.
* This conflicts with SciTECO that must remain in control
* of keyboard processing.
* Unfortunately, the default mapping can only be disabled
* or changed via the internal PDC_set_function_key() in
* pdcwin.h. Therefore we declare it manually here.
*/
#ifdef PDCURSES_WIN32A
int PDC_set_function_key(const unsigned function, const int new_key);
#endif
static void scintilla_notify(Scintilla *sci, int idFrom,
void *notify, void *user_data);
#if defined(PDCURSES_WIN32) || defined(NCURSES_WIN32)
/**
* This handler is the Windows-analogue of a signal
* handler. MinGW provides signal(), but it's not
* reliable.
* This may also be used to handle CTRL_CLOSE_EVENTs.
* NOTE: Unlike signal handlers, this is executed in a
* separate thread.
*/
static BOOL WINAPI
console_ctrl_handler(DWORD type)
{
switch (type) {
case CTRL_C_EVENT:
sigint_occurred = TRUE;
return TRUE;
}
return FALSE;
}
#endif
} /* extern "C" */
#define UNNAMED_FILE "(Unnamed)"
/**
* Curses attribute for the color combination
* `f` (foreground) and `b` (background)
* according to the color pairs initialized by
* Scinterm.
* NOTE: This depends on the global variable
* `COLORS` and is thus not a constant expression.
*/
#define SCI_COLOR_ATTR(f, b) \
((attr_t)COLOR_PAIR(SCI_COLOR_PAIR(f, b)))
void
ViewCurses::initialize_impl(void)
{
sci = scintilla_new(scintilla_notify);
setup();
}
void
InterfaceCurses::main_impl(int &argc, char **&argv)
{
/*
* We must register this handler to handle
* asynchronous interruptions via CTRL+C
* reliably. The signal handler we already
* have won't do.
*/
#if defined(PDCURSES_WIN32) || defined(NCURSES_WIN32)
SetConsoleCtrlHandler(console_ctrl_handler, TRUE);
#endif
/*
* Make sure we have a string for the info line
* even if info_update() is never called.
*/
info_current = g_strdup(PACKAGE_NAME);
}
#ifdef NCURSES_UNIX
void
InterfaceCurses::init_screen(void)
{
screen_tty = g_fopen("/dev/tty", "r+");
/* should never fail */
g_assert(screen_tty != NULL);
screen = newterm(NULL, screen_tty, screen_tty);
if (!screen) {
g_fprintf(stderr, "Error initializing interactive mode. "
"$TERM may be incorrect.\n");
exit(EXIT_FAILURE);
}
/*
* If stdout or stderr would go to the terminal,
* redirect it. Otherwise, they are already redirected
* (e.g. to a file) and writing to them does not
* interrupt terminal interaction.
*/
if (isatty(1)) {
FILE *stdout_new;
stdout_orig = dup(1);
g_assert(stdout_orig >= 0);
stdout_new = g_freopen("/dev/null", "a+", stdout);
g_assert(stdout_new != NULL);
}
if (isatty(2)) {
FILE *stderr_new;
stderr_orig = dup(2);
g_assert(stderr_orig >= 0);
stderr_new = g_freopen("/dev/null", "a+", stderr);
g_assert(stderr_new != NULL);
}
}
#elif defined(XCURSES)
void
InterfaceCurses::init_screen(void)
{
const char *argv[] = {PACKAGE_NAME, NULL};
/*
* This sets the program name to "SciTECO"
* which may then also be used as the X11 class name
* for overwriting X11 resources in .Xdefaults
* FIXME: We could support passing in resource
* overrides via the SciTECO command line.
* But unfortunately, Xinitscr() is called too
* late to modify argc/argv for command-line parsing.
* Therefore this could only be supported by
* adding a special option like --resource.
*/
Xinitscr(1, (char **)argv);
}
#else
void
InterfaceCurses::init_screen(void)
{
initscr();
}
#endif
void
InterfaceCurses::init_interactive(void)
{
/*
* Curses accesses many environment variables
* internally. In order to be able to modify them in
* the SciTECO profile, we must update the process
* environment before initscr()/newterm().
* This is safe to do here since there are no threads.
*/
QRegisters::globals.update_environ();
/*
* $TERM must be unset or "#win32con" for the win32
* driver to load.
* So we always ignore any $TERM changes by the user.
*/
#ifdef NCURSES_WIN32
g_setenv("TERM", "#win32con", TRUE);
#endif
#ifdef PDCURSES_WIN32A
/*
* Necessary to enable window resizing in Win32a port
*/
PDC_set_resize_limits(25, 0xFFFF, 80, 0xFFFF);
/*
* Disable all magic function keys.
* NOTE: This could also be used to assign
* a "shutdown" key when program termination is requested.
*/
for (int i = 0; i < 5; i++)
PDC_set_function_key(i, 0);
#endif
/* for displaying UTF-8 characters properly */
setlocale(LC_CTYPE, "");
init_screen();
cbreak();
noecho();
/* Scintilla draws its own cursor */
curs_set(0);
info_window = newwin(1, 0, 0, 0);
msg_window = newwin(1, 0, LINES - 2, 0);
cmdline_window = newwin(0, 0, LINES - 1, 0);
keypad(cmdline_window, TRUE);
#ifdef EMSCRIPTEN
nodelay(cmdline_window, TRUE);
#endif
/*
* Will also initialize Scinterm, Curses color pairs
* and resizes the current view.
*/
if (current_view)
show_view(current_view);
}
void
InterfaceCurses::restore_batch(void)
{
/*
* Set window title to a reasonable default,
* in case it is not reset immediately by the
* shell.
* FIXME: See set_window_title() why this
* is necessary.
*/
#if defined(NCURSES_UNIX) && defined(HAVE_TIGETSTR)
set_window_title(g_getenv("TERM") ? : "");
#endif
/*
* Restore ordinary terminal behaviour
* (i.e. return to batch mode)
*/
endwin();
/*
* Restore stdout and stderr, so output goes to
* the terminal again in case we "muted" them.
*/
#ifdef NCURSES_UNIX
if (stdout_orig >= 0) {
int fd = dup2(stdout_orig, 1);
g_assert(fd == 1);
}
if (stderr_orig >= 0) {
int fd = dup2(stderr_orig, 2);
g_assert(fd == 2);
}
#endif
/*
* See vmsg_impl(): It looks at msg_win to determine
* whether we're in batch mode.
*/
if (msg_window) {
delwin(msg_window);
msg_window = NULL;
}
}
void
InterfaceCurses::resize_all_windows(void)
{
int lines, cols; /* screen dimensions */
getmaxyx(stdscr, lines, cols);
wresize(info_window, 1, cols);
wresize(current_view->get_window(),
lines - 3, cols);
wresize(msg_window, 1, cols);
mvwin(msg_window, lines - 2, 0);
wresize(cmdline_window, 1, cols);
mvwin(cmdline_window, lines - 1, 0);
draw_info();
msg_clear(); /* FIXME: use saved message */
popup_clear();
draw_cmdline();
}
void
InterfaceCurses::vmsg_impl(MessageType type, const gchar *fmt, va_list ap)
{
attr_t attr;
if (!msg_window) { /* batch mode */
stdio_vmsg(type, fmt, ap);
return;
}
/*
* On most platforms we can write to stdout/stderr
* even in interactive mode.
*/
#if defined(XCURSES) || defined(PDCURSES_WIN32A) || \
defined(NCURSES_UNIX) || defined(NCURSES_WIN32)
va_list aq;
va_copy(aq, ap);
stdio_vmsg(type, fmt, aq);
va_end(aq);
#endif
switch (type) {
default:
case MSG_USER:
attr = SCI_COLOR_ATTR(COLOR_BLACK, COLOR_WHITE);
break;
case MSG_INFO:
attr = SCI_COLOR_ATTR(COLOR_BLACK, COLOR_GREEN);
break;
case MSG_WARNING:
attr = SCI_COLOR_ATTR(COLOR_BLACK, COLOR_YELLOW);
break;
case MSG_ERROR:
attr = SCI_COLOR_ATTR(COLOR_BLACK, COLOR_RED);
beep();
break;
}
wmove(msg_window, 0, 0);
wbkgdset(msg_window, ' ' | attr);
vw_printw(msg_window, fmt, ap);
wclrtoeol(msg_window);
}
void
InterfaceCurses::msg_clear(void)
{
if (!msg_window) /* batch mode */
return;
wmove(msg_window, 0, 0);
wbkgdset(msg_window, ' ' | SCI_COLOR_ATTR(COLOR_BLACK, COLOR_WHITE));
wclrtoeol(msg_window);
}
void
InterfaceCurses::show_view_impl(ViewCurses *view)
{
int lines, cols; /* screen dimensions */
WINDOW *current_view_win;
current_view = view;
if (!cmdline_window) /* batch mode */
return;
current_view_win = current_view->get_window();
/*
* screen size might have changed since
* this view's WINDOW was last active
*/
getmaxyx(stdscr, lines, cols);
wresize(current_view_win, lines - 3, cols);
/* Set up window position: never changes */
mvwin(current_view_win, 1, 0);
}
#if PDCURSES
void
InterfaceCurses::set_window_title(const gchar *title)
{
static gchar *last_title = NULL;
/*
* PDC_set_title() can result in flickering
* even when executed only once per pressed key,
* so we check whether it is really necessary to change
* the title.
* This is an issue at least with PDCurses/win32.
*/
if (!g_strcmp0(title, last_title))
return;
PDC_set_title(title);
g_free(last_title);
last_title = g_strdup(title);
}
#elif defined(NCURSES_UNIX) && defined(HAVE_TIGETSTR)
void
InterfaceCurses::set_window_title(const gchar *title)
{
if (!has_status_line || !to_status_line || !from_status_line)
return;
/*
* Modern terminal emulators map the window title to
* the historic status line.
* This feature is not standardized in ncurses,
* so we query the terminfo database.
* This feature may make problems with terminal emulators
* that do support a status line but do not map them
* to the window title. Some emulators (like xterm)
* support setting the window title via custom escape
* sequences and via the status line but their
* terminfo entry does not say so. (xterm can also
* save and restore window titles but there is not
* even a terminfo capability defined for this.)
* Taken the different emulator incompatibilites
* it may be best to make this configurable.
* Once we support configurable status lines,
* there could be a special status line that's sent
* to the terminal that may be set up in the profile
* depending on $TERM.
*
* NOTE: The terminfo manpage advises us to use putp()
* but on ncurses/UNIX (where terminfo is available),
* we do not let curses write to stdout.
* NOTE: This leaves the title set after we quit.
*/
fputs(to_status_line, screen_tty);
fputs(title, screen_tty);
fputs(from_status_line, screen_tty);
fflush(screen_tty);
}
#else
void
InterfaceCurses::set_window_title(const gchar *title)
{
/* no way to set window title */
}
#endif
void
InterfaceCurses::draw_info(void)
{
if (!info_window) /* batch mode */
return;
wmove(info_window, 0, 0);
wbkgdset(info_window, ' ' | SCI_COLOR_ATTR(COLOR_BLACK, COLOR_WHITE));
waddstr(info_window, info_current);
wclrtoeol(info_window);
set_window_title(info_current);
}
void
InterfaceCurses::info_update_impl(const QRegister *reg)
{
/*
* We cannot rely on Curses' control character drawing
* and we need the info_current string for other purposes
* (like PDC_set_title()), so we "canonicalize" the
* register name here:
*/
gchar *name = String::canonicalize_ctl(reg->name);
g_free(info_current);
info_current = g_strconcat(PACKAGE_NAME " - <QRegister> ",
name, NIL);
g_free(name);
/* NOTE: drawn in event_loop_iter() */
}
void
InterfaceCurses::info_update_impl(const Buffer *buffer)
{
g_free(info_current);
info_current = g_strconcat(PACKAGE_NAME " - <Buffer> ",
buffer->filename ? : UNNAMED_FILE,
buffer->dirty ? "*" : "", NIL);
/* NOTE: drawn in event_loop_iter() */
}
void
InterfaceCurses::format_chr(chtype *&target, gchar chr, attr_t attr)
{
/*
* NOTE: This mapping is similar to
* View::set_representations()
*/
switch (chr) {
case CTL_KEY_ESC:
*target++ = '$' | attr | A_REVERSE;
break;
case '\r':
*target++ = 'C' | attr | A_REVERSE;
*target++ = 'R' | attr | A_REVERSE;
break;
case '\n':
*target++ = 'L' | attr | A_REVERSE;
*target++ = 'F' | attr | A_REVERSE;
break;
case '\t':
*target++ = 'T' | attr | A_REVERSE;
*target++ = 'A' | attr | A_REVERSE;
*target++ = 'B' | attr | A_REVERSE;
break;
default:
if (IS_CTL(chr)) {
*target++ = '^' | attr | A_REVERSE;
*target++ = CTL_ECHO(chr) | attr | A_REVERSE;
} else {
*target++ = chr | attr;
}
}
}
void
InterfaceCurses::cmdline_update_impl(const Cmdline *cmdline)
{
gsize alloc_len = 1;
chtype *p;
/*
* AFAIK bold black should be rendered grey by any
* common terminal.
* If not, this problem will be gone once we support
* a Scintilla view command line.
* Also A_UNDERLINE is not supported by PDCurses/win32
* and causes weird colors, so we better leave it away.
*/
const attr_t rubout_attr =
#ifndef PDCURSES_WIN32
A_UNDERLINE |
#endif
A_BOLD | SCI_COLOR_ATTR(COLOR_BLACK, COLOR_BLACK);
/*
* Replace entire pre-formatted command-line.
* We don't know if it is similar to the last one,
* so realloc makes no sense.
* We approximate the size of the new formatted command-line,
* wasting a few bytes for control characters.
*/
delete[] cmdline_current;
for (guint i = 0; i < cmdline->len+cmdline->rubout_len; i++)
alloc_len += IS_CTL((*cmdline)[i]) ? 3 : 1;
p = cmdline_current = new chtype[alloc_len];
/* format effective command line */
for (guint i = 0; i < cmdline->len; i++)
format_chr(p, (*cmdline)[i]);
cmdline_len = p - cmdline_current;
/* Format rubbed-out command line. */
for (guint i = cmdline->len; i < cmdline->len+cmdline->rubout_len; i++)
format_chr(p, (*cmdline)[i], rubout_attr);
cmdline_rubout_len = p - cmdline_current - cmdline_len;
/* highlight cursor after effective command line */
if (cmdline_rubout_len) {
cmdline_current[cmdline_len] &= A_CHARTEXT | A_UNDERLINE;
cmdline_current[cmdline_len] |= A_REVERSE;
} else {
cmdline_current[cmdline_len++] = ' ' | A_REVERSE;
}
draw_cmdline();
}
void
InterfaceCurses::draw_cmdline(void)
{
/* total width available for command line */
guint total_width = getmaxx(stdscr) - 1;
/* beginning of command line to show */
guint disp_offset;
/* length of command line to show */
guint disp_len;
disp_offset = cmdline_len -
MIN(cmdline_len, total_width/2 + cmdline_len % (total_width/2));
disp_len = MIN(total_width, cmdline_len+cmdline_rubout_len - disp_offset);
werase(cmdline_window);
mvwaddch(cmdline_window, 0, 0, '*' | A_BOLD);
waddchnstr(cmdline_window, cmdline_current+disp_offset, disp_len);
}
void
InterfaceCurses::popup_add_impl(PopupEntryType type,
const gchar *name, bool highlight)
{
gchar *entry;
if (!cmdline_window) /* batch mode */
return;
entry = g_strconcat(highlight ? "*" : " ", name, NIL);
popup.longest = MAX(popup.longest, (gint)strlen(name));
popup.length++;
popup.list = g_slist_prepend(popup.list, entry);
}
void
InterfaceCurses::popup_show_impl(void)
{
int lines, cols; /* screen dimensions */
int popup_lines;
gint popup_cols;
gint popup_colwidth;
gint cur_col;
if (!cmdline_window || !popup.length)
/* batch mode or nothing to display */
return;
getmaxyx(stdscr, lines, cols);
if (popup.window)
delwin(popup.window);
else
/* reverse list only once */
popup.list = g_slist_reverse(popup.list);
if (!popup.cur_list) {
/* start from beginning of list */
popup.cur_list = popup.list;
popup.cur_entry = 0;
}
/* reserve 2 spaces between columns */
popup_colwidth = popup.longest + 2;
/* popup_cols = floor(cols / popup_colwidth) */
popup_cols = MAX(cols / popup_colwidth, 1);
/* popup_lines = ceil((popup.length-popup.cur_entry) / popup_cols) */
popup_lines = (popup.length-popup.cur_entry+popup_cols-1) / popup_cols;
/*
* Popup window can cover all but one screen row.
* If it does not fit, the list of tokens is truncated
* and "..." is displayed.
*/
popup_lines = MIN(popup_lines, lines - 1);
/* window covers message, scintilla and info windows */
popup.window = newwin(popup_lines, 0, lines - 1 - popup_lines, 0);
wbkgd(popup.window, ' ' | SCI_COLOR_ATTR(COLOR_BLACK, COLOR_BLUE));
/*
* cur_col is the row currently written.
* It does not wrap but grows indefinitely.
* Therefore the real current row is (cur_col % popup_cols)
*/
cur_col = 0;
while (popup.cur_list) {
gchar *entry = (gchar *)popup.cur_list->data;
gint cur_line = cur_col/popup_cols + 1;
wmove(popup.window,
cur_line-1, (cur_col % popup_cols)*popup_colwidth);
cur_col++;
if (cur_line == popup_lines && !(cur_col % popup_cols) &&
g_slist_next(popup.cur_list) != NULL) {
/* truncate entries in the popup's very last column */
(void)wattrset(popup.window, A_BOLD);
waddstr(popup.window, "...");
break;
}
(void)wattrset(popup.window, *entry == '*' ? A_BOLD : A_NORMAL);
waddstr(popup.window, entry + 1);
popup.cur_list = g_slist_next(popup.cur_list);
popup.cur_entry++;
}
redrawwin(info_window);
/* scintilla window is redrawn by ViewCurses::refresh() */
redrawwin(msg_window);
}
void
InterfaceCurses::popup_clear_impl(void)
{
g_slist_free_full(popup.list, g_free);
popup.list = NULL;
popup.length = 0;
/* reserve at least 3 characters for "..." */
popup.longest = 3;
popup.cur_list = NULL;
popup.cur_entry = 0;
if (!popup.window)
return;
redrawwin(info_window);
/* scintilla window is redrawn by ViewCurses::refresh() */
redrawwin(msg_window);
delwin(popup.window);
popup.window = NULL;
}
/**
* One iteration of the event loop.
*
* This is a global function, so it may
* be used as an Emscripten callback.
*
* @bug
* Can probably be defined as a static method,
* so we can avoid declaring it a fried function of
* InterfaceCurses.
*/
void
event_loop_iter()
{
int key;
/*
* On PDCurses/win32, raw() and cbreak() does
* not disable and enable CTRL+C handling properly.
* Since I don't want to patch PDCurses/win32,
* we do this manually here.
* NOTE: This exploits the fact that PDCurses uses
* STD_INPUT_HANDLE internally!
*/
#ifdef PDCURSES_WIN32
HANDLE console_hnd = GetStdHandle(STD_INPUT_HANDLE);
DWORD console_mode;
GetConsoleMode(console_hnd, &console_mode);
#endif
/*
* Setting function key processing is important
* on Unix Curses, as ESCAPE is handled as the beginning
* of a escape sequence when terminal emulators are
* involved.
* On some Curses variants (XCurses) however, keypad
* must always be TRUE so we receive KEY_RESIZE.
*/
#ifdef NCURSES_UNIX
keypad(interface.cmdline_window, Flags::ed & Flags::ED_FNKEYS);
#endif
/* no special <CTRL/C> handling */
raw();
#ifdef PDCURSES_WIN32
SetConsoleMode(console_hnd, console_mode & ~ENABLE_PROCESSED_INPUT);
#endif
key = wgetch(interface.cmdline_window);
/* allow asynchronous interruptions on <CTRL/C> */
sigint_occurred = FALSE;
noraw(); /* FIXME: necessary because of NCURSES_WIN32 bug */
cbreak();
#ifdef PDCURSES_WIN32
SetConsoleMode(console_hnd, console_mode | ENABLE_PROCESSED_INPUT);
#endif
if (key == ERR)
return;
switch (key) {
#ifdef KEY_RESIZE
case KEY_RESIZE:
#if PDCURSES
resize_term(0, 0);
#endif
interface.resize_all_windows();
break;
#endif
case CTL_KEY('H'):
case 0x7F: /* ^? */
case KEY_BACKSPACE:
/*
* For historic reasons terminals can send
* ASCII 8 (^H) or 127 (^?) for backspace.
* Curses also defines KEY_BACKSPACE, probably
* for terminals that send an escape sequence for
* backspace.
* In SciTECO backspace is normalized to ^H.
*/
cmdline.keypress(CTL_KEY('H'));
break;
case KEY_ENTER:
case '\r':
case '\n':
cmdline.keypress('\n');
break;
/*
* Function key macros
*/
#define FN(KEY) case KEY_##KEY: cmdline.fnmacro(#KEY); break
#define FNS(KEY) FN(KEY); FN(S##KEY)
FN(DOWN); FN(UP); FNS(LEFT); FNS(RIGHT);
FNS(HOME);
case KEY_F(0)...KEY_F(63): {
gchar macro_name[3+1];
g_snprintf(macro_name, sizeof(macro_name),
"F%d", key - KEY_F0);
cmdline.fnmacro(macro_name);
break;
}
FNS(DC);
FNS(IC);
FN(NPAGE); FN(PPAGE);
FNS(PRINT);
FN(A1); FN(A3); FN(B2); FN(C1); FN(C3);
FNS(END);
FNS(HELP);
FN(CLOSE);
#undef FNS
#undef FN
/*
* Control keys and keys with printable representation
*/
default:
if (key <= 0xFF)
cmdline.keypress((gchar)key);
}
/*
* Info window is updated very often which is very
* costly, especially when using PDC_set_title(),
* so we redraw it here, where the overhead does
* not matter much.
*/
interface.draw_info();
wnoutrefresh(interface.info_window);
/* FIXME: this does wrefresh() internally */
interface.current_view->refresh();
wnoutrefresh(interface.msg_window);
wnoutrefresh(interface.cmdline_window);
if (interface.popup.window)
wnoutrefresh(interface.popup.window);
doupdate();
}
void
InterfaceCurses::event_loop_impl(void)
{
static const Cmdline empty_cmdline;
/*
* Initialize Curses for interactive mode
*/
init_interactive();
/* initial refresh */
/* FIXME: this does wrefresh() internally */
current_view->refresh();
draw_info();
wnoutrefresh(info_window);
msg_clear();
wnoutrefresh(msg_window);
cmdline_update(&empty_cmdline);
wnoutrefresh(cmdline_window);
doupdate();
#ifdef EMSCRIPTEN
PDC_emscripten_set_handler(event_loop_iter, TRUE);
/*
* We must not block emscripten's main loop,
* instead event_loop_iter() is called asynchronously.
* We also must not exit the event_loop() method, since
* SciTECO would assume ordinary program termination.
* We also must not call exit() since that would run
* the global destructors.
* The following exits the main() function immediately
* while keeping the "runtime" alive.
*/
emscripten_exit_with_live_runtime();
#else
try {
for (;;)
event_loop_iter();
} catch (Quit) {
/* SciTECO termination (e.g. EX$$) */
}
restore_batch();
#endif
}
InterfaceCurses::Popup::~Popup()
{
if (window)
delwin(window);
if (list)
g_slist_free_full(list, g_free);
}
InterfaceCurses::~InterfaceCurses()
{
if (info_window)
delwin(info_window);
g_free(info_current);
if (cmdline_window)
delwin(cmdline_window);
delete[] cmdline_current;
if (msg_window)
delwin(msg_window);
/*
* PDCurses (win32) crashes if initscr() wasn't called.
* Others (XCurses) crash if we try to use isendwin() here.
* Perhaps Curses cleanup should be in restore_batch()
* instead.
*/
#ifndef XCURSES
if (info_window && !isendwin())
endwin();
#endif
if (screen)
delscreen(screen);
if (screen_tty)
fclose(screen_tty);
if (stderr_orig >= 0)
close(stderr_orig);
if (stdout_orig >= 0)
close(stdout_orig);
}
/*
* Callbacks
*/
static void
scintilla_notify(Scintilla *sci, int idFrom, void *notify, void *user_data)
{
interface.process_notify((SCNotification *)notify);
}
} /* namespace SciTECO */
|