-
Notifications
You must be signed in to change notification settings - Fork 626
/
Copy pathdisplay.c
939 lines (773 loc) · 20.9 KB
/
display.c
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
/* Copyright (c) 2006-2015 Jonas Fonseca <[email protected]>
*
* 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 2 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.
*/
#include "tig/tig.h"
#include "tig/argv.h"
#include "tig/io.h"
#include "tig/repo.h"
#include "tig/options.h"
#include "tig/view.h"
#include "tig/draw.h"
#include "tig/display.h"
#include "tig/watch.h"
#define MAX_KEYS 2000
static void set_terminal_modes(void);
struct view *display[2];
unsigned int current_view;
static WINDOW *display_win[2];
static WINDOW *display_title[2];
static WINDOW *display_sep;
struct display_tty {
FILE *file;
int fd;
struct termios *attr;
pid_t opgrp;
};
static struct display_tty opt_tty = { NULL, -1, NULL, -1 };
static struct io script_io = { -1 };
bool
is_script_executing(void)
{
return script_io.pipe != -1;
}
enum status_code
open_script(const char *path)
{
if (is_script_executing())
return error("Scripts cannot be run from scripts");
char buf[SIZEOF_STR];
if (!path_expand(buf, sizeof(buf), path))
return error("Failed to expand path: %s", path);
return io_open(&script_io, "%s", buf)
? SUCCESS : error("Failed to open %s", buf);
}
bool
open_external_viewer(const char *argv[], const char *dir, bool silent, bool confirm, bool echo, bool quick, bool do_refresh, const char *notice)
{
bool ok;
if (echo) {
char buf[SIZEOF_STR] = "";
io_run_buf(argv, buf, sizeof(buf), dir, false);
if (*buf) {
report("%s", buf);
return true;
} else {
report("No output");
return false;
}
} else if (silent || is_script_executing()) {
ok = io_run_bg(argv, dir);
} else {
clear();
refresh();
endwin(); /* restore original tty modes */
tcsetattr(opt_tty.fd, TCSAFLUSH, opt_tty.attr);
ok = io_run_fg(argv, dir);
if (confirm || !ok) {
if (!ok && *notice)
fprintf(stderr, "%s", notice);
if (!ok || !quick) {
fprintf(stderr, "Press Enter to continue");
getc(opt_tty.file);
}
}
fseek(opt_tty.file, 0, SEEK_END);
tcsetattr(opt_tty.fd, TCSAFLUSH, opt_tty.attr);
set_terminal_modes();
}
if (watch_update(WATCH_EVENT_AFTER_COMMAND) && do_refresh) {
struct view *view;
int i;
foreach_displayed_view (view, i) {
if (watch_dirty(&view->watch))
refresh_view(view);
}
}
redraw_display(true);
return ok;
}
#define EDITOR_LINENO_MSG \
"*** Your editor reported an error while opening the file.\n" \
"*** This is probably because it doesn't support the line\n" \
"*** number argument added automatically. The line number\n" \
"*** has been disabled for now. You can permanently disable\n" \
"*** it by adding the following line to ~/.tigrc\n" \
"*** set editor-line-number = no\n"
void
open_editor(const char *file, unsigned int lineno)
{
const char *editor_argv[SIZEOF_ARG + 3] = { "vi", file, NULL };
char editor_cmd[SIZEOF_STR];
char lineno_cmd[SIZEOF_STR];
const char *editor;
int argc = 0;
editor = getenv("TIG_EDITOR");
if (!editor)
editor = getenv("GIT_EDITOR");
if (!editor && *opt_editor)
editor = opt_editor;
if (!editor)
editor = getenv("VISUAL");
if (!editor)
editor = getenv("EDITOR");
if (!editor)
editor = "vi";
string_ncopy(editor_cmd, editor, strlen(editor));
if (!argv_from_string_no_quotes(editor_argv, &argc, editor_cmd)) {
report("Failed to read editor command");
return;
}
if (lineno && opt_editor_line_number && string_format(lineno_cmd, "+%u", lineno))
editor_argv[argc++] = lineno_cmd;
editor_argv[argc] = file;
if (!open_external_viewer(editor_argv, repo.cdup, false, false, false, false, true, EDITOR_LINENO_MSG))
opt_editor_line_number = false;
}
static void
apply_horizontal_split(struct view *base, struct view *view)
{
view->width = base->width;
view->height = apply_step(opt_split_view_height, base->height);
view->height = MAX(view->height, MIN_VIEW_HEIGHT);
view->height = MIN(view->height, base->height - MIN_VIEW_HEIGHT);
base->height -= view->height;
}
int
apply_vertical_split(int base_width)
{
int width = apply_step(opt_split_view_width, base_width);
width = MAX(width, MIN_VIEW_WIDTH);
width = MIN(width, base_width - MIN_VIEW_WIDTH);
return width;
}
bool
vertical_split_is_enabled(enum vertical_split vsplit, int height, int width)
{
if (vsplit == VERTICAL_SPLIT_AUTO)
return width > 160 || width * VSPLIT_SCALE > (height - 1) * 2;
return vsplit == VERTICAL_SPLIT_VERTICAL;
}
static void
redraw_display_separator(bool clear)
{
if (display_sep) {
int lineno = 0;
if (clear)
wclear(display_sep);
wbkgdset(display_sep, get_line_attr(NULL, LINE_TITLE_BLUR));
switch (opt_line_graphics) {
case GRAPHIC_ASCII:
while (mvwaddch(display_sep, lineno++, 0, '|') == OK);
break;
case GRAPHIC_DEFAULT:
while (mvwaddch(display_sep, lineno++, 0, ACS_VLINE) == OK);
break;
case GRAPHIC_UTF_8:
while (mvwaddstr(display_sep, lineno++, 0, "│") == OK);
break;
}
wnoutrefresh(display_sep);
}
}
static void create_or_move_display_separator(int height, int x)
{
if (!display_sep) {
display_sep = newwin(height, 1, 0, x);
if (!display_sep)
die("Failed to create separator window");
} else {
wresize(display_sep, height, 1);
mvwin(display_sep, 0, x);
}
}
static void remove_display_separator(void)
{
if (display_sep) {
delwin(display_sep);
display_sep = NULL;
}
}
void
resize_display(void)
{
int x, y, i;
int height, width;
struct view *base = display[0];
struct view *view = display[1] ? display[1] : display[0];
bool vsplit;
/* Setup window dimensions */
getmaxyx(stdscr, height, width);
/* Make room for the status window. */
base->height = height - 1;
base->width = width;
vsplit = vertical_split_is_enabled(opt_vertical_split, height, width);
if (view != base) {
if (vsplit) {
view->height = base->height;
view->width = apply_vertical_split(base->width);
base->width -= view->width;
/* Make room for the separator bar. */
view->width -= 1;
create_or_move_display_separator(base->height, base->width);
redraw_display_separator(false);
} else {
remove_display_separator();
apply_horizontal_split(base, view);
}
/* Make room for the title bar. */
view->height -= 1;
} else {
remove_display_separator();
}
/* Make room for the title bar. */
base->height -= 1;
x = y = 0;
foreach_displayed_view (view, i) {
if (!display_win[i]) {
display_win[i] = newwin(view->height, view->width, y, x);
if (!display_win[i])
die("Failed to create %s view", view->name);
scrollok(display_win[i], false);
display_title[i] = newwin(1, view->width, y + view->height, x);
if (!display_title[i])
die("Failed to create title window");
} else {
wresize(display_win[i], view->height, view->width);
mvwin(display_win[i], y, x);
wresize(display_title[i], 1, view->width);
mvwin(display_title[i], y + view->height, x);
}
view->win = display_win[i];
view->title = display_title[i];
if (vsplit)
x += view->width + 1;
else
y += view->height + 1;
}
redraw_display_separator(false);
}
void
redraw_display(bool clear)
{
struct view *view;
int i;
foreach_displayed_view (view, i) {
if (clear)
wclear(view->win);
redraw_view(view);
update_view_title(view);
}
redraw_display_separator(clear);
}
static bool
save_window_line(FILE *file, WINDOW *win, int y, char *buf, size_t bufsize)
{
int read = mvwinnstr(win, y, 0, buf, bufsize);
const char *out = read == ERR ? "" : string_trim_end(buf);
return read == ERR ? false : fprintf(file, "%s\n", out) == strlen(out) + 1;
}
static bool
save_window_vline(FILE *file, WINDOW *left, WINDOW *right, int y, char *buf, size_t bufsize)
{
int read1 = mvwinnstr(left, y, 0, buf, bufsize);
int read2 = read1 == ERR ? ERR : mvwinnstr(right, y, 0, buf + read1 + 1, bufsize - read1 - 1);
if (read2 == ERR)
return false;
buf[read1] = '|';
buf = string_trim_end(buf);
return fprintf(file, "%s\n", string_trim_end(buf)) == strlen(buf) + 1;
}
bool
save_display(const char *path)
{
int i, width;
size_t linelen;
char *line;
FILE *file = fopen(path, "w");
bool ok = true;
struct view *view = display[0];
if (!file)
return false;
getmaxyx(stdscr, i, width);
linelen = width * 4;
line = malloc(linelen + 1);
if (!line) {
fclose(file);
return false;
}
if (view->width < width && display[1]) {
struct view *left = display[0],
*right = display[1];
for (i = 0; ok && i < left->height; i++)
ok = save_window_vline(file, left->win, right->win, i, line, linelen);
if (ok)
ok = save_window_vline(file, left->title, right->title, 0, line, linelen);
} else {
int j;
foreach_displayed_view (view, j) {
for (i = 0; ok && i < view->height; i++)
ok = save_window_line(file, view->win, i, line, linelen);
if (ok)
ok = save_window_line(file, view->title, 0, line, linelen);
}
}
free(line);
fclose(file);
return ok;
}
/*
* Dump view data to file.
*
* FIXME: Add support for more line state and column data.
*/
bool
save_view(struct view *view, const char *path)
{
struct view_column_data column_data = {0};
FILE *file = fopen(path, "w");
size_t i;
if (!file)
return false;
fprintf(file, "View: %s\n", view->name);
if (view->prev && view->prev != view)
fprintf(file, "Prev: %s\n", view->prev->name);
if (view->parent)
fprintf(file, "Parent: %s\n", view->parent->name);
fprintf(file, "Ref: %s\n", view->ref);
fprintf(file, "Dimensions: height=%d width=%d\n", view->height, view->width);
fprintf(file, "Position: offset=%lu column=%lu lineno=%lu\n",
view->pos.offset,
view->pos.col,
view->pos.lineno);
for (i = 0; i < view->lines; i++) {
struct line *line = &view->line[i];
fprintf(file, "line[%3zu] type=%s selected=%u\n",
i,
enum_name(get_line_type_name(line->type)),
line->selected);
if (view->columns &&
view->ops->get_column_data(view, line, &column_data) &&
column_data.box) {
const struct box *box = column_data.box;
size_t j;
size_t offset;
fprintf(file, "line[%3zu] cells=%zu text=",
i, box->cells);
for (j = 0, offset = 0; j < box->cells; j++) {
const struct box_cell *cell = &box->cell[j];
fprintf(file, "[%.*s]", (int) cell->length, box->text + offset);
offset += cell->length;
}
fprintf(file, "\n");
}
}
fclose(file);
return true;
}
/*
* Status management
*/
/* Whether or not the curses interface has been initialized. */
static bool cursed = false;
/* Terminal hacks and workarounds. */
static bool use_scroll_redrawwin;
static bool use_scroll_status_wclear;
/* The status window is used for polling keystrokes. */
WINDOW *status_win;
/* Reading from the prompt? */
static bool input_mode = false;
static bool status_empty = false;
/* Update status and title window. */
static bool
update_status_window(struct view *view, const char *context, const char *msg, va_list args)
{
if (input_mode)
return false;
if (!status_empty || *msg) {
wmove(status_win, 0, 0);
if (view && view->has_scrolled && use_scroll_status_wclear)
wclear(status_win);
if (*msg) {
vw_printw(status_win, msg, args);
status_empty = false;
} else {
status_empty = true;
}
wclrtoeol(status_win);
if (context && *context) {
size_t contextlen = strlen(context);
int x, y, width, ___;
getyx(status_win, y, x);
getmaxyx(status_win, ___, width);
(void) ___;
if (contextlen < width - x) {
mvwprintw(status_win, 0, width - contextlen, "%s", context);
wmove(status_win, y, x);
}
}
return true;
}
return false;
}
void
update_status(const char *msg, ...)
{
va_list args;
va_start(args, msg);
update_status_window(display[current_view], "", msg, args);
va_end(args);
}
void
update_status_with_context(const char *context, const char *msg, ...)
{
va_list args;
va_start(args, msg);
update_status_window(display[current_view], context, msg, args);
va_end(args);
}
void
report(const char *msg, ...)
{
struct view *view = display[current_view];
va_list args;
if (!view) {
char buf[SIZEOF_STR];
int retval;
FORMAT_BUFFER(buf, sizeof(buf), msg, retval, true);
die("%s", buf);
}
va_start(args, msg);
if (update_status_window(view, "", msg, args))
wnoutrefresh(status_win);
va_end(args);
update_view_title(view);
}
void
report_clear(void)
{
struct view *view = display[current_view];
if (!view)
return;
if (!input_mode && !status_empty) {
werase(status_win);
doupdate();
}
status_empty = true;
update_view_title(view);
}
static void
done_display(void)
{
if (cursed) {
if (status_win) {
werase(status_win);
doupdate();
}
curs_set(1);
endwin();
}
cursed = false;
if (opt_tty.attr) {
tcsetattr(opt_tty.fd, TCSAFLUSH, opt_tty.attr);
free(opt_tty.attr);
opt_tty.attr = NULL;
}
if (opt_tty.opgrp != -1) {
signal(SIGTTOU, SIG_IGN);
tcsetpgrp(opt_tty.fd, opt_tty.opgrp);
signal(SIGTTOU, SIG_DFL);
}
}
static void
set_terminal_modes(void)
{
nonl(); /* Disable conversion and detect newlines from input. */
raw(); /* Take input chars one at a time, no wait for \n */
noecho(); /* Don't echo input */
curs_set(0);
leaveok(stdscr, false);
}
void
init_tty(void)
{
/* open */
opt_tty.file = fopen("/dev/tty", "r+");
if (!opt_tty.file)
die("Failed to open tty for input");
opt_tty.fd = fileno(opt_tty.file);
/* attributes */
opt_tty.attr = calloc(1, sizeof(struct termios));
if (!opt_tty.attr)
die("Failed allocation for tty attributes");
tcgetattr(opt_tty.fd, opt_tty.attr);
if (opt_pgrp) {
/* process-group leader */
setpgid(getpid(), getpid());
opt_tty.opgrp = tcgetpgrp(opt_tty.fd);
signal(SIGTTOU, SIG_IGN);
tcsetpgrp(opt_tty.fd, getpid());
signal(SIGTTOU, SIG_DFL);
}
die_callback = done_display;
}
void
init_display(void)
{
bool no_display = !!getenv("TIG_NO_DISPLAY");
const char *term;
int x, y;
int code;
if (!opt_tty.file)
die("Can't initialize display without tty");
if (atexit(done_display))
die("Failed to register done_display");
/* Initialize the curses library */
if (!no_display && isatty(STDIN_FILENO)) {
/* Needed for ncurses 5.4 compatibility. */
cursed = !!initscr();
} else {
/* Leave stdin and stdout alone when acting as a pager. */
FILE *out_tty;
out_tty = no_display ? fopen("/dev/null", "w+") : opt_tty.file;
if (!out_tty)
die("Failed to open tty for output");
cursed = !!newterm(NULL, out_tty, opt_tty.file);
}
if (!cursed)
die("Failed to initialize curses");
set_terminal_modes();
init_colors();
getmaxyx(stdscr, y, x);
status_win = newwin(1, x, y - 1, 0);
if (!status_win)
die("Failed to create status window");
/* Enable keyboard mapping */
keypad(status_win, true);
wbkgdset(status_win, get_line_attr(NULL, LINE_STATUS));
enable_mouse(opt_mouse);
#ifdef NCURSES_VERSION
/* Disable extended keys so that esc-codes will be received
* instead of extended key values (> KEY_MAX).
* Then these keys can be mapped in .tigrc etc. */
for (code = KEY_MAX; code < MAX_KEYS; code++) {
keyok(code, false);
}
#endif
#if defined(NCURSES_VERSION_PATCH) && (NCURSES_VERSION_PATCH >= 20080119)
set_tabsize(opt_tab_size);
#else
TABSIZE = opt_tab_size;
#endif
term = getenv("XTERM_VERSION")
? NULL
: (getenv("TERM_PROGRAM") ? getenv("TERM_PROGRAM") : getenv("COLORTERM"));
if (term && !strcmp(term, "gnome-terminal")) {
/* In the gnome-terminal-emulator, the warning message
* shown when scrolling up one line while the cursor is
* on the first line followed by scrolling down one line
* corrupts the status line. This is fixed by calling
* wclear. */
use_scroll_status_wclear = true;
use_scroll_redrawwin = false;
} else if (term &&
(!strcmp(term, "xrvt-xpm") || !strcmp(term, "Apple_Terminal") ||
!strcmp(term, "iTerm.app"))) {
/* No problems with full optimizations in
* xrvt-(unicode)
* aterm
* Terminal.app
* iTerm2 */
use_scroll_status_wclear = use_scroll_redrawwin = false;
} else {
/* When scrolling in (u)xterm the last line in the
* scrolling direction will update slowly. This is
* the conservative default. */
use_scroll_redrawwin = true;
use_scroll_status_wclear = false;
}
}
static bool
read_script(struct key *key)
{
static struct buffer input_buffer;
static const char *line = "";
enum status_code code;
while (!line || !*line) {
if (input_buffer.data && *input_buffer.data == ':') {
line = "<Enter>";
memset(&input_buffer, 0, sizeof(input_buffer));
} else if (!io_get(&script_io, &input_buffer, '\n', true)) {
io_done(&script_io);
return false;
} else if (input_buffer.data[strspn(input_buffer.data, " \t")] == '#') {
continue;
} else {
line = input_buffer.data;
}
}
code = get_key_value(&line, key);
if (code != SUCCESS)
die("Error reading script: %s", get_status_message(code));
return true;
}
int
get_input_char(void)
{
if (is_script_executing()) {
static struct key key;
static int bytes_pos;
if (!key.modifiers.multibytes || bytes_pos >= strlen(key.data.bytes)) {
if (!read_script(&key))
return 0;
bytes_pos = 0;
}
if (!key.modifiers.multibytes) {
if (key.data.value < 128)
return key.data.value;
die("Only ASCII control characters can be used in prompts: %d", key.data.value);
}
return key.data.bytes[bytes_pos++];
}
return getc(opt_tty.file);
}
static bool
update_views(void)
{
struct view *view;
int i;
bool is_loading = false;
foreach_view (view, i) {
update_view(view);
if (view_is_displayed(view) && view->has_scrolled &&
use_scroll_redrawwin)
redrawwin(view->win);
view->has_scrolled = false;
if (view->pipe)
is_loading = true;
}
return is_loading;
}
int
get_input(int prompt_position, struct key *key)
{
struct view *view;
int i, key_value, cursor_y, cursor_x;
if (prompt_position > 0)
input_mode = true;
memset(key, 0, sizeof(*key));
while (true) {
int delay = -1;
if (opt_refresh_mode != REFRESH_MODE_MANUAL) {
if (opt_refresh_mode == REFRESH_MODE_PERIODIC)
delay = watch_periodic(opt_refresh_interval);
foreach_displayed_view (view, i) {
if (view_can_refresh(view) &&
!view->pipe &&
watch_dirty(&view->watch)) {
refresh_view(view);
}
}
}
if (update_views())
delay = 0;
else
/* Check there is no pending update after update_views() */
foreach_displayed_view (view, i)
if (view->watch.changed) {
delay = 0;
break;
}
/* Update the cursor position. */
if (prompt_position) {
getbegyx(status_win, cursor_y, cursor_x);
cursor_x = prompt_position;
} else {
view = display[current_view];
getbegyx(view->win, cursor_y, cursor_x);
cursor_x += view->width - 1;
cursor_y += view->pos.lineno - view->pos.offset;
}
set_cursor_pos(cursor_y, cursor_x);
if (is_script_executing()) {
/* Wait for the current command to complete. */
if (delay == 0 || !read_script(key))
continue;
return key->modifiers.multibytes ? OK : key->data.value;
} else {
/* Refresh, accept single keystroke of input */
doupdate();
wtimeout(status_win, delay);
key_value = wgetch(status_win);
}
/* wgetch() with nodelay() enabled returns ERR when
* there's no input. */
if (key_value == ERR) {
} else if (key_value == KEY_RESIZE) {
int height, width;
getmaxyx(stdscr, height, width);
wresize(status_win, 1, width);
mvwin(status_win, height - 1, 0);
wnoutrefresh(status_win);
resize_display();
redraw_display(true);
} else if (key_value == KEY_CTL('z')) {
raise(SIGTSTP);
} else {
int pos, key_length;
input_mode = false;
if (key_value == erasechar())
key_value = KEY_BACKSPACE;
/*
* Ctrl-<key> values are represented using a 0x1F
* bitmask on the key value. To 'unmap' we assume that:
*
* - Ctrl-Z is handled separately for job control.
* - Ctrl-m is the same as Return/Enter.
* - Ctrl-i is the same as Tab.
*
* For all other key values in the range the Ctrl flag
* is set and the key value is updated to the proper
* ASCII value.
*/
if (KEY_CTL('@') <= key_value && key_value <= KEY_CTL('_') &&
key_value != KEY_RETURN && key_value != KEY_TAB && key_value != KEY_ESC) {
key->modifiers.control = 1;
key_value = key_value | 0x40;
}
if ((key_value >= KEY_MIN && key_value < KEY_MAX) || key_value <= 0x1F) {
key->data.value = key_value;
return key->data.value;
}
key->modifiers.multibytes = 1;
key->data.bytes[0] = key_value;
key_length = utf8_char_length(key->data.bytes);
for (pos = 1; pos < key_length && pos < sizeof(key->data.bytes) - 1; pos++) {
key->data.bytes[pos] = wgetch(status_win);
}
return OK;
}
}
}
void
enable_mouse(bool enable)
{
#ifdef NCURSES_MOUSE_VERSION
static bool enabled = false;
if (enable != enabled) {
mmask_t mask = enable ? ALL_MOUSE_EVENTS : 0;
if (mousemask(mask, NULL))
mouseinterval(0);
enabled = enable;
}
#endif
}
/* vim: set ts=8 sw=8 noexpandtab: */