-
Notifications
You must be signed in to change notification settings - Fork 1
/
ui.c
1421 lines (1161 loc) · 29.2 KB
/
ui.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
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
/*
* Copyright (c) 2021, 2024 Omar Polo <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/*
* Ncurses UI for telescope.
*
* Text scrolling
* ==============
*
* ncurses allows you to scroll a window, but when a line goes out of
* the visible area it's forgotten. We keep a list of formatted lines
* (``visual lines'') that we know fits in the window, and draw them.
*
* This means that on every resize we have to clear our list of lines
* and re-render everything. A clever approach would be to do this
* ``on-demand'', but it's still missing.
*
*/
#include "compat.h"
#include <sys/time.h>
#include <sys/wait.h>
#include <curses.h>
#include <errno.h>
#include <locale.h>
#include <signal.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <grapheme.h>
#include "cmd.h"
#include "defaults.h"
#include "ev.h"
#include "exec.h"
#include "hist.h"
#include "keymap.h"
#include "mailcap.h"
#include "minibuffer.h"
#include "session.h"
#include "telescope.h"
#include "ui.h"
#include "utf8.h"
static void set_scroll_position(struct tab *, size_t, size_t);
static void restore_curs_x(struct buffer *);
static int readkey(void);
static void dispatch_stdio(int, int, void*);
static void handle_signal(int, int, void*);
static void handle_resize_nodelay(int, int, void*);
static void handle_download_refresh(int, int, void *);
static void rearrange_windows(void);
static void line_prefix_and_text(int, struct vline *, char *, size_t, const char **, const char **, int *);
static void print_vline(int, int, WINDOW*, struct vline*);
static void redraw_tabline(void);
static void redraw_window(WINDOW *, int, int, int, int, struct buffer *);
static void redraw_download(void);
static void redraw_help(void);
static void redraw_body(struct tab*);
static void redraw_modeline(struct tab*);
static void redraw_minibuffer(void);
static void do_redraw_echoarea(void);
static void do_redraw_minibuffer(void);
static void do_redraw_minibuffer_compl(void);
static void place_cursor(int);
static void redraw_tab(struct tab*);
static void update_loading_anim(int, int, void*);
static void stop_loading_anim(struct tab*);
static int should_rearrange_windows;
static int show_tab_bar;
static int too_small;
static int x_offset;
struct thiskey thiskey;
struct tab *current_tab;
static unsigned int resize_timer;
static struct timeval resize_tv = { 0, 250000 };
static unsigned int download_timer;
static struct timeval download_refresh_timer = { 0, 250000 };
static WINDOW *tabline, *body, *modeline, *echoarea, *minibuffer;
int body_lines, body_cols;
static WINDOW *help;
/* not static so we can see them from help.c */
struct buffer helpwin;
int help_lines, help_cols;
static WINDOW *download;
/* not static so we can see them from download.c */
struct buffer downloadwin;
int download_lines;
int download_cols;
static int side_window;
static int in_side_window;
static struct timeval loading_tv = { 0, 250000 };
static char keybuf[64];
/* XXX: don't forget to init these in main() */
struct kmap global_map,
minibuffer_map,
*current_map,
*base_map;
static inline void
update_x_offset(void)
{
if (olivetti_mode && fill_column < body_cols)
x_offset = (body_cols - fill_column)/2;
else
x_offset = 0;
}
static void
set_scroll_position(struct tab *tab, size_t top, size_t cur)
{
struct line *last;
struct vline *vl;
size_t i = 0;
int topfound = 0;
last = TAILQ_FIRST(&tab->buffer.head);
TAILQ_FOREACH(vl, &tab->buffer.vhead, vlines) {
if (last != vl->parent) {
last = vl->parent;
i++;
}
if (!topfound && i == top) {
topfound = 1;
tab->buffer.top_line = vl;
}
if (i == cur) {
tab->buffer.current_line = vl;
return;
}
}
if (!topfound)
tab->buffer.top_line = TAILQ_FIRST(&tab->buffer.vhead);
tab->buffer.current_line = tab->buffer.top_line;
}
void
get_scroll_position(struct tab *tab, size_t *top, size_t *cur)
{
struct line *l;
int topfound = 0;
*top = 0;
*cur = 0;
if (tab->buffer.top_line == NULL ||
tab->buffer.current_line == NULL)
return;
TAILQ_FOREACH(l, &tab->buffer.head, lines) {
if (tab->buffer.top_line->parent == l)
topfound = 1;
if (tab->buffer.current_line->parent == l)
return;
if (!topfound)
(*top)++;
(*cur)++;
}
}
void
save_excursion(struct excursion *place, struct buffer *buffer)
{
place->curs_x = buffer->curs_x;
place->curs_y = buffer->curs_y;
place->line_off = buffer->line_off;
place->top_line = buffer->top_line;
place->current_line = buffer->current_line;
place->point_offset = buffer->point_offset;
}
void
restore_excursion(struct excursion *place, struct buffer *buffer)
{
buffer->curs_x = place->curs_x;
buffer->curs_y = place->curs_y;
buffer->line_off = place->line_off;
buffer->top_line = place->top_line;
buffer->current_line = place->current_line;
buffer->point_offset = place->point_offset;
}
static void
restore_curs_x(struct buffer *buffer)
{
struct vline *vl;
struct lineprefix *lp = line_prefixes;
const char *prfx, *text;
if (dont_apply_styling)
lp = raw_prefixes;
buffer->curs_x = 0;
/* small hack: don't olivetti-mode the download pane */
if (buffer != &downloadwin)
buffer->curs_x += x_offset;
vl = buffer->current_line;
if (vl == NULL || vl->len == 0 || vl->parent == NULL)
buffer->curs_x += buffer->point_offset = 0;
else if (vl->parent->data != NULL) {
text = vl->parent->data;
buffer->curs_x += utf8_snwidth(text, buffer->point_offset,
buffer->curs_x);
} else {
text = vl->parent->line + vl->from;
buffer->curs_x += utf8_snwidth(text, buffer->point_offset,
buffer->curs_x);
}
if (vl == NULL)
return;
if (vl->parent->data != NULL)
buffer->curs_x += utf8_swidth_between(vl->parent->line,
vl->parent->data, buffer->curs_x);
else {
prfx = lp[vl->parent->type].prfx1;
buffer->curs_x += utf8_swidth(prfx, buffer->curs_x);
}
}
void
global_key_unbound(void)
{
message("%s is undefined", keybuf);
}
struct buffer *
current_buffer(void)
{
if (in_minibuffer)
return &ministate.buffer;
if (in_side_window & SIDE_WINDOW_LEFT)
return &helpwin;
if (in_side_window & SIDE_WINDOW_BOTTOM)
return &downloadwin;
return ¤t_tab->buffer;
}
static int
readkey(void)
{
uint32_t state = 0;
if ((thiskey.key = wgetch(body)) == ERR)
return 0;
thiskey.meta = thiskey.key == '\e';
if (thiskey.meta) {
thiskey.key = wgetch(body);
if (thiskey.key == ERR || thiskey.key == '\e') {
thiskey.meta = 0;
thiskey.key = '\e';
}
}
thiskey.cp = 0;
if ((unsigned int)thiskey.key >= UINT8_MAX)
return 1;
while (1) {
if (!utf8_decode(&state, &thiskey.cp, (uint8_t)thiskey.key))
break;
if ((thiskey.key = wgetch(body)) == ERR) {
message("Error decoding user input");
return 0;
}
}
return 1;
}
static void
dispatch_stdio(int fd, int ev, void *d)
{
int lk;
const char *keyname;
char tmp[5] = {0};
/* TODO: schedule a redraw? */
if (too_small)
return;
if (!readkey())
return;
if (keybuf[0] != '\0')
strlcat(keybuf, " ", sizeof(keybuf));
if (thiskey.meta)
strlcat(keybuf, "M-", sizeof(keybuf));
if (thiskey.cp != 0) {
grapheme_encode_utf8(thiskey.cp, tmp, sizeof(tmp));
strlcat(keybuf, tmp, sizeof(keybuf));
} else if ((keyname = unkbd(thiskey.key)) != NULL) {
strlcat(keybuf, keyname, sizeof(keybuf));
} else {
tmp[0] = thiskey.key;
strlcat(keybuf, tmp, sizeof(keybuf));
}
lk = lookup_key(¤t_map, &thiskey, current_buffer());
if (lk == LK_UNBOUND) {
if (current_map->unhandled_input != NULL)
current_map->unhandled_input();
else
global_key_unbound();
}
if (lk != LK_ADVANCED_MAP) {
current_map = base_map;
strlcpy(keybuf, "", sizeof(keybuf));
}
if (side_window & SIDE_WINDOW_LEFT)
recompute_help();
if (should_rearrange_windows)
rearrange_windows();
redraw_tab(current_tab);
}
static void
handle_signal(int sig, int ev, void *d)
{
int ret;
switch (sig) {
case SIGWINCH:
ev_timer_cancel(resize_timer);
resize_timer = ev_timer(&resize_tv, handle_resize_nodelay,
NULL);
break;
case SIGCHLD:
do {
ret = waitpid(-1, NULL, WNOHANG);
} while (ret == -1 && errno == EINTR);
break;
}
}
static void
handle_resize_nodelay(int s, int ev, void *d)
{
endwin();
refresh();
clear();
rearrange_windows();
}
static void
handle_download_refresh(int s, int v, void *d)
{
if (side_window & SIDE_WINDOW_BOTTOM) {
recompute_downloads();
redraw_tab(current_tab);
}
}
static inline int
should_show_tab_bar(void)
{
if (tab_bar_show == -1)
return 0;
if (tab_bar_show == 0)
return 1;
return TAILQ_NEXT(TAILQ_FIRST(&tabshead), tabs) != NULL;
}
static void
rearrange_windows(void)
{
int lines;
int minibuffer_lines;
should_rearrange_windows = 0;
show_tab_bar = should_show_tab_bar();
lines = LINES;
/* 3 lines for the ui and 12 for the body and minibuffer */
if ((too_small = lines < 15)) {
erase();
printw("Window too small.");
refresh();
return;
}
/* move and resize the windows, in reverse order! */
if (in_minibuffer == MB_COMPREAD) {
minibuffer_lines = MIN(10, lines/2);
mvwin(minibuffer, lines - minibuffer_lines, 0);
wresize(minibuffer, minibuffer_lines, COLS);
lines -= minibuffer_lines;
wrap_page(&ministate.compl.buffer, COLS, 0);
}
mvwin(echoarea, --lines, 0);
wresize(echoarea, 1, COLS);
mvwin(modeline, --lines, 0);
wresize(modeline, 1, COLS);
if (side_window & SIDE_WINDOW_BOTTOM) {
download_lines = MIN(5, lines/2);
download_cols = COLS;
mvwin(download, lines - download_lines, 0);
wresize(download, download_lines, download_cols);
lines -= download_lines;
wrap_page(&downloadwin, download_cols, 0);
}
body_lines = show_tab_bar ? --lines : lines;
body_cols = COLS;
/*
* Here we make the assumption that show_tab_bar is either 0
* or 1, and reuse that as argument to mvwin.
*/
if (side_window & SIDE_WINDOW_LEFT) {
help_cols = 0.3 * COLS;
help_lines = lines;
mvwin(help, show_tab_bar, 0);
wresize(help, help_lines, help_cols);
wrap_page(&helpwin, help_cols, 0);
body_cols = COLS - help_cols - 1;
mvwin(body, show_tab_bar, help_cols);
} else
mvwin(body, show_tab_bar, 0);
update_x_offset();
wresize(body, body_lines, body_cols);
if (show_tab_bar)
wresize(tabline, 1, COLS);
wrap_page(¤t_tab->buffer, body_cols, x_offset);
redraw_tab(current_tab);
}
static void
line_prefix_and_text(int col, struct vline *vl, char *buf, size_t len,
const char **prfx_ret, const char **text_ret, int *text_len)
{
struct lineprefix *lp = line_prefixes;
int type, cont;
size_t i, width;
char *space, *t;
if (dont_apply_styling)
lp = raw_prefixes;
if (vl->len == 0) {
*text_ret = "";
*text_len = 0;
}
cont = vl->flags & L_CONTINUATION;
type = vl->parent->type;
if (!cont)
*prfx_ret = lp[type].prfx1;
else
*prfx_ret = lp[type].prfx2;
space = vl->parent->data;
*text_ret = vl->parent->line + vl->from;
*text_len = MIN(INT_MAX, vl->len);
if (!emojify_link || type != LINE_LINK || space == NULL) {
return;
}
if (cont) {
memset(buf, 0, len);
width = utf8_swidth_between(vl->parent->line, space, col);
for (i = 0; i < width + 1 && i < len - 1; ++i)
buf[i] = ' ';
} else {
strlcpy(buf, vl->parent->line, len);
if ((t = strchr(buf, ' ')) != NULL)
*t = '\0';
strlcat(buf, " ", len);
}
*prfx_ret = buf;
}
static inline void
print_vline_descr(int width, WINDOW *window, struct vline *vl)
{
int x, y, goal;
switch (vl->parent->type) {
case LINE_COMPL:
case LINE_COMPL_CURRENT:
goal = width/2;
break;
case LINE_DOWNLOAD:
case LINE_DOWNLOAD_DONE:
goal = width/4;
break;
case LINE_HELP:
goal = 8;
break;
default:
return;
}
if (vl->parent->alt == NULL)
return;
(void)y;
getyx(window, y, x);
if (goal <= x)
wprintw(window, " ");
for (; goal > x; ++x)
wprintw(window, " ");
wprintw(window, "%s", vl->parent->alt);
}
/*
* Core part of the rendering. It prints a vline starting from the
* current cursor position. Printing a vline consists of skipping
* `off' columns (for olivetti-mode), print the correct prefix (which
* may be the emoji in case of emojified links-lines), printing the
* text itself, filling until width - off and filling off columns
* again.
*/
static void
print_vline(int off, int width, WINDOW *window, struct vline *vl)
{
/*
* Believe me or not, I've seen emoji ten code points long!
* That means, to stay large, 4*10 bytes + NUL.
*/
char emojibuf[41] = {0};
const char *text, *prfx;
struct line_face *f;
int i, left, x, y, textlen;
f = &line_faces[vl->parent->type];
/* unused, set by getyx */
(void)y;
if (vl->parent->type == LINE_FRINGE && fringe_ignore_offset)
off = 0;
line_prefix_and_text(off, vl, emojibuf, sizeof(emojibuf), &prfx,
&text, &textlen);
wattr_on(window, body_face.left, NULL);
for (i = 0; i < off; i++)
waddch(window, ' ');
wattr_off(window, body_face.left, NULL);
wattr_on(window, f->prefix, NULL);
wprintw(window, "%s", prfx);
wattr_off(window, f->prefix, NULL);
wattr_on(window, f->text, NULL);
if (text)
wprintw(window, "%.*s", textlen, text);
print_vline_descr(width, window, vl);
wattr_off(window, f->text, NULL);
getyx(window, y, x);
left = width - x;
wattr_on(window, f->trail, NULL);
for (i = 0; i < left - off; ++i)
waddch(window, ' ');
wattr_off(window, f->trail, NULL);
wattr_on(window, body_face.right, NULL);
for (i = 0; i < off; i++)
waddch(window, ' ');
wattr_off(window, body_face.right, NULL);
}
static void
redraw_tabline(void)
{
struct tab *tab;
size_t toskip, ots, tabwidth, space, x;
int current, y, truncated, pair;
const char *title;
char buf[25];
x = 0;
/* unused, but set by a getyx */
(void)y;
tabwidth = sizeof(buf)+1;
space = COLS-2;
toskip = 0;
TAILQ_FOREACH(tab, &tabshead, tabs) {
toskip++;
if (tab == current_tab)
break;
}
if (toskip * tabwidth <= space)
toskip = 0;
else {
ots = toskip;
toskip--;
while (toskip != 0 &&
(ots - toskip+1) * tabwidth < space)
toskip--;
}
werase(tabline);
wattr_on(tabline, tab_face.background, NULL);
wprintw(tabline, toskip == 0 ? " " : "<");
wattr_off(tabline, tab_face.background, NULL);
truncated = 0;
TAILQ_FOREACH(tab, &tabshead, tabs) {
if (truncated)
break;
if (toskip != 0) {
toskip--;
continue;
}
getyx(tabline, y, x);
if (x + sizeof(buf)+2 >= (size_t)COLS)
truncated = 1;
current = tab == current_tab;
if (*(title = tab->buffer.title) == '\0')
title = hist_cur(tab->hist);
if (tab->flags & TAB_URGENT)
strlcpy(buf, "!", sizeof(buf));
else
strlcpy(buf, " ", sizeof(buf));
if (strlcat(buf, title, sizeof(buf)) >= sizeof(buf)) {
/* truncation happens */
strlcpy(&buf[sizeof(buf)-4], "...", 4);
} else {
/* pad with spaces */
while (strlcat(buf, " ", sizeof(buf)) < sizeof(buf))
/* nop */ ;
}
pair = current ? tab_face.current : tab_face.tab;
wattr_on(tabline, pair, NULL);
wprintw(tabline, "%s", buf);
wattr_off(tabline, pair, NULL);
wattr_on(tabline, tab_face.background, NULL);
if (TAILQ_NEXT(tab, tabs) != NULL)
wprintw(tabline, "┃");
wattr_off(tabline, tab_face.background, NULL);
}
wattr_on(tabline, tab_face.background, NULL);
for (; x < (size_t)COLS; ++x)
waddch(tabline, ' ');
if (truncated)
mvwprintw(tabline, 0, COLS-1, ">");
wattr_off(tabline, tab_face.background, NULL);
}
/*
* Compute the first visible line around vl. Try to search forward
* until the end of the buffer; if a visible line is not found, search
* backward. Return NULL if no viable line was found.
*/
struct vline *
adjust_line(struct vline *vl, struct buffer *buffer)
{
struct vline *t;
if (vl == NULL)
return NULL;
if (!(vl->parent->flags & L_HIDDEN))
return vl;
/* search forward */
for (t = vl;
t != NULL && t->parent->flags & L_HIDDEN;
t = TAILQ_NEXT(t, vlines))
; /* nop */
if (t != NULL)
return t;
/* search backward */
for (t = vl;
t != NULL && t->parent->flags & L_HIDDEN;
t = TAILQ_PREV(t, vhead, vlines))
; /* nop */
return t;
}
static void
redraw_window(WINDOW *win, int off, int height, int width,
int show_fringe, struct buffer *buffer)
{
struct vline *vl;
int onscreen = 0, l = 0;
restore_curs_x(buffer);
/*
* TODO: ignoring buffer->force_update and always
* re-rendering. In theory we can recompute the y position
* without a re-render, and optimize here. It's not the only
* optimisation possible here, wscrl wolud also be an
* interesting one.
*/
again:
werase(win);
buffer->curs_y = 0;
if (TAILQ_EMPTY(&buffer->head))
goto end;
if (buffer->top_line == NULL)
buffer->top_line = TAILQ_FIRST(&buffer->vhead);
buffer->top_line = adjust_line(buffer->top_line, buffer);
if (buffer->top_line == NULL)
goto end;
buffer->current_line = adjust_line(buffer->current_line, buffer);
for (vl = buffer->top_line; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
if (vl->parent->flags & L_HIDDEN)
continue;
wmove(win, l, 0);
print_vline(off, width, win, vl);
if (vl == buffer->current_line)
onscreen = 1;
if (!onscreen)
buffer->curs_y++;
l++;
if (l == height)
break;
}
if (!onscreen) {
for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) {
if (vl == buffer->current_line)
break;
if (vl->parent->flags & L_HIDDEN)
continue;
buffer->line_off++;
buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
}
if (vl != NULL)
goto again;
}
buffer->last_line_off = buffer->line_off;
buffer->force_redraw = 0;
end:
for (; show_fringe && l < height; l++)
print_vline(off, width, win, &fringe);
wmove(win, buffer->curs_y, buffer->curs_x);
}
static void
redraw_download(void)
{
redraw_window(download, 0, download_lines, COLS, 0, &downloadwin);
}
static void
redraw_help(void)
{
redraw_window(help, 0, help_lines, help_cols, 1, &helpwin);
}
static void
redraw_body(struct tab *tab)
{
static struct tab *last_tab;
if (last_tab != tab)
tab->buffer.force_redraw =1;
last_tab = tab;
redraw_window(body, x_offset, body_lines, body_cols, 1, &tab->buffer);
}
static inline char
trust_status_char(enum trust_state ts)
{
switch (ts) {
case TS_UNKNOWN: return '-';
case TS_UNTRUSTED: return '!';
case TS_TEMP_TRUSTED: return '!';
case TS_TRUSTED: return 'v';
case TS_VERIFIED: return 'V';
default: return 'X';
}
}
static void
redraw_modeline(struct tab *tab)
{
struct buffer *buffer;
double pct;
int x, y, max_x, max_y;
const char *mode;
const char *spin = "-\\|/";
buffer = current_buffer();
mode = buffer->mode;
werase(modeline);
wattr_on(modeline, modeline_face.background, NULL);
wmove(modeline, 0, 0);
wprintw(modeline, "-%c%c%c%c %s ",
spin[tab->loading_anim_step],
trust_status_char(tab->trust),
tab->client_cert ? 'C' : '-',
tab->faulty_gemserver ? 'W' : '-',
mode == NULL ? "(none)" : mode);
pct = (buffer->line_off + buffer->curs_y) * 100.0
/ buffer->line_max;
if (buffer->line_max <= (size_t)body_lines)
wprintw(modeline, "All ");
else if (buffer->line_off == 0)
wprintw(modeline, "Top ");
else if (buffer->line_off + body_lines >= buffer->line_max)
wprintw(modeline, "Bottom ");
else
wprintw(modeline, "%.0f%% ", pct);
wprintw(modeline, "%zu/%zu %s ",
buffer->line_off + buffer->curs_y,
buffer->line_max,
hist_cur(tab->hist));
getyx(modeline, y, x);
getmaxyx(modeline, max_y, max_x);
(void)y;
(void)max_y;
for (; x < max_x; ++x)
waddstr(modeline, "-");
wattr_off(modeline, modeline_face.background, NULL);
}
static void
redraw_minibuffer(void)
{
wattr_on(echoarea, minibuffer_face.background, NULL);
werase(echoarea);
if (in_minibuffer)
do_redraw_minibuffer();
else
do_redraw_echoarea();
if (in_minibuffer == MB_COMPREAD)
do_redraw_minibuffer_compl();
wattr_off(echoarea, minibuffer_face.background, NULL);
}
static void
do_redraw_echoarea(void)
{
struct vline *vl;
if (ministate.curmesg != NULL)
wprintw(echoarea, "%s", ministate.curmesg);
else if (*keybuf != '\0')
waddstr(echoarea, keybuf);
else {
/* If nothing else, show the URL at point */
vl = current_tab->buffer.current_line;
if (vl != NULL && vl->parent->type == LINE_LINK)
waddstr(echoarea, vl->parent->alt);
}
}
static void
do_redraw_minibuffer(void)
{
struct buffer *cmplbuf, *buffer;
size_t off_y, off_x = 0;
const char *start, *c;
char *line;
cmplbuf = &ministate.compl.buffer;
buffer = &ministate.buffer;
(void)off_y; /* unused, set by getyx */
wmove(echoarea, 0, 0);
if (in_minibuffer == MB_COMPREAD)
wprintw(echoarea, "(%2zu) ",
cmplbuf->line_max);
wprintw(echoarea, "%s", ministate.prompt);
if (!ministate.editing)
wprintw(echoarea, "(%zu/%zu) ",
hist_off(ministate.hist) + 1,
hist_size(ministate.hist));
getyx(echoarea, off_y, off_x);
start = ministate.buf;
if (!ministate.editing)
start = hist_cur(ministate.hist);
line = buffer->current_line->parent->line + buffer->current_line->from;
c = line + buffer->point_offset;
while (start < c && utf8_swidth_between(start, c, off_x) > (size_t)COLS/2) {
start += grapheme_next_character_break_utf8(start, SIZE_MAX);
}
waddstr(echoarea, start);
if (ministate.curmesg != NULL)
wprintw(echoarea, " [%s]", ministate.curmesg);
wmove(echoarea, 0, off_x + utf8_swidth_between(start, c, off_x));
}
static void
do_redraw_minibuffer_compl(void)
{
redraw_window(minibuffer, 0, 10, COLS, 1,
&ministate.compl.buffer);
}
/*
* Place the cursor in the right ncurses window. If soft is 1, use
* wnoutrefresh (which shouldn't cause any I/O); otherwise use