~strahinja/poe

ref: v1.4.5 poe/draw.c -rw-r--r-- 23.7 KiB
1f716ca7Страхиња Радић po.c: Check msgstr != NULL 9 months ago
                                                                                
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
/* See the file LICENSE for copyright and license details. */
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "termbox.h"
#include "util.h"
#include "po.h"
#include "draw.h"
#include "config.h"
#include "version.h"

/* clang-format off */
static const char* help_title = "Main view help";
static const char* help[]     = {
	"Esc         - Close dialog, clear error",
	"Enter       - Edit entry", 
	"H   or F1   - Show this screen",
	"/   or F7   - Incremental search (end = Enter)",
	"n   or F3   - Find next",
	"N   or F4   - Find previous",
	"u   or F8   - Go to next untranslated entry",
	"f   or F9   - Go to next fuzzy entry",
	"w   or C-S  - Save file (with backup)",
	"g   or Home - Go to first entry",
	"C-B or PgUp - Go to previous page",
	"k   or \u2191    - Go to previous entry",
	"j   or \u2193    - Go to next entry",
	"C-F or PgDn - Go to next page",
	"G   or End  - Go to last entry",
	"z   or C-Z  - Toggle fuzzy flag",
	"q   or C-Q  - Quit",
	"See the manual (man poe) for full help."};
static const char* editbox_title = "Edit translation";
const char* errors[]	       = {
	[ERR_UNKNOWN_KEY]      = "Unknown key (press H for help)",
	[ERR_EXIT_KEY]	       = "Press q or C-Q to quit",
	[ERR_DLG_OPEN]	       = "Press Esc to close dialog or q/C-Q to quit",
	[ERR_DLG_OPEN_QUIT]    = "Press Esc to close dialog",
	[ERR_EMPTY_FILE]       = "No msgids to edit",
	[ERR_CANT_ALLOC]       = "Memory allocation failed",
	[ERR_CANT_SAVE]	       = "File is not writeable",
	[ERR_CANT_MOVE]	       = "Cannot move temporary file",
	[ERR_ILLEGAL_ON_FIRST] = "Not allowed on first entry",
	[ERR_NO_PLURAL_FORMS]  = "No Plural-Forms (add manually)"};
const char* flag_strings[]	= {
	[FL_C_FORMAT]		= "c-format",
	[FL_NO_C_FORMAT]	= "no-c-format",
	[FL_FUZZY]		= "fuzzy"};
const char* first_entry_msgid	= "*** SPECIAL: file info ***";
const char* program_name	= "poe";
const char* prompt_dirty	= "File changed, save [y/n/ESC]?";
const char* prompt_cancel_dirty = "Entry changed, save [y/n/ESC]?";
const char* prompt_overwrite	= "Not empty, overwrite? [y/n/ESC]?";
/* clang-format on */

void
init_bufferline(struct BufferLine* bl)
{
	if (!bl)
		return;
	bl->text   = NULL;
	bl->length = 0;
	bl->fg	   = 0;
	bl->bg	   = 0;
}

void
free_bufferline(struct BufferLine* bl)
{
	if (!bl)
		return;
	free(bl->text);
	bl->text   = NULL;
	bl->length = 0;
	bl->fg	   = 0;
	bl->bg	   = 0;
}

void
init_drawstate(struct DrawState* state, char* error, char* filename,
	char* prompt)
{
	state->error			 = error;
	state->filename			 = filename;
	state->search			 = NULL;
	state->input_buffer		 = NULL;
	state->input_changed_fuzzy	 = UNCHANGED;
	state->input_column		 = 0;
	state->input_display_column	 = 0;
	state->input_first_shown_column	 = 0;
	state->input_first_shown_row	 = 0;
	state->input_row		 = 0;
	state->input_rows_count		 = 0;
	state->saved_column		 = -1;
	state->info_buffer		 = NULL;
	state->info_first_shown_column	 = 0;
	state->info_first_shown_row	 = 0;
	state->info_rows_count		 = 0;
	state->help_width		 = calculate_help_width(state);
	state->edit_width		 = calculate_edit_width(state);
	state->search_width		 = calculate_search_width(state);
	state->paste_buffer		 = NULL;
	state->paste_rows_count		 = 0;
	state->search_column		 = 0;
	state->search_first_shown_column = 0;
	state->search_display_column	 = 0;
	state->nplurals			 = 2;
	state->prompt			 = prompt;
	state->prompt_callback		 = NULL;
	state->running			 = 1;
	state->dirty			 = 0;
	state->dirty_before_edit	 = 0;
	state->fuzzy_before_edit	 = 0;
	state->in_prompt		 = 0;
	state->show_help		 = 0;
	state->show_edit		 = 0;
	state->edit_info_focused	 = 0;
	state->show_search		 = 0;
	state->msgid_number		 = state->msgid_count > 0 ? 1 : 0;
	state->fuzzy_count		 = 0;
	state->untranslated_count	 = 0;
	state->obsolete_count		 = 0;
	state->msgstr_index		 = 0;
	state->first_shown_msgid	 = state->msgid_number;
}

void
free_drawstate(struct DrawState* state)
{
	struct BufferLine* current    = NULL;
	struct PoEntry* current_entry = NULL;
	size_t i		      = 0;

	free(state->search);

	for (i = 0; i < state->input_rows_count; i++)
	{
		current = state->input_buffer + i;
		free_bufferline(current);
	}
	free(state->input_buffer);
	for (i = 0; i < state->paste_rows_count; i++)
	{
		current = state->paste_buffer + i;
		free_bufferline(current);
	}
	free(state->paste_buffer);
	for (i = 0; i < state->info_rows_count; i++)
	{
		current = state->info_buffer + i;
		free_bufferline(current);
	}
	free(state->info_buffer);
	for (i = 0; i < state->real_msgid_count; i++)
	{
		current_entry = state->entries + i;
		free_po_entry(current_entry);
	}
	free(state->entries);
}

struct DrawState*
set_nplurals(struct DrawState* state, struct PoEntry* entry)
{
	if (!entry || !entry->plural_forms)
		return NULL;

	int read_number = 0;
	uint32_t* ppf	= entry->plural_forms;
	char token[MAXBUFLINE];
	char* ptoken = token;

	*ptoken = 0;
	while (*ppf)
	{
		if (u32_starts_with(ppf, (uint32_t*)L"nplurals="))
		{
			ppf += strlen("nplurals=");
			read_number = 1;
		}
		else if (read_number && *ppf >= '0' && *ppf <= '9')
		{
			*ptoken++ = *ppf++;
		}
		else if (read_number && *ppf == ';')
		{
			*ptoken = 0;
			ptoken	= token;
			break;
		}
		else
			ppf++;
	}
	if (*ptoken)
	{
		state->nplurals = strtol(ptoken, NULL, 0);
		if (errno)
			return NULL;
	}

	return state;
}

/* Calculates display length of an input buffer line.
 * max_index = the input index up to which the length is calculated
 */
size_t
display_length(const struct DrawState* state, uint32_t* buffer,
	const size_t max_index)
{
	size_t len     = 0;
	uint32_t* pbuf = buffer;

	for (size_t i = 0; *pbuf && i < max_index; i++)
	{
		pbuf = buffer + i;
		if (*pbuf == '\t')
			len += TAB_SIZE - (len % TAB_SIZE);
		else
			len++;
	}

	return len;
}

/* Calculates input length from a display length.
 * max_index = the display index up to which the length is calculated
 */
size_t
input_length(const struct DrawState* state, uint32_t* buffer,
	const size_t max_index)
{
	size_t len     = 0;
	uint32_t* pbuf = buffer;
	size_t i       = 0;

	for (i = 0; *pbuf && len < max_index; i++)
	{
		pbuf = buffer + i;
		if (*pbuf == '\t')
			len += MIN(TAB_SIZE - (len % TAB_SIZE), max_index - len);
		else
			len++;
	}

	return i;
}

int
calculate_help_width(const struct DrawState* state)
{
	return state->maxx > HELP_WIDTH + 2 ? HELP_WIDTH : state->maxx - 2;
}

int
calculate_edit_width(const struct DrawState* state)
{
	return state->maxx > EDIT_WIDTH + 3 ? EDIT_WIDTH : state->maxx - 3;
}

int
calculate_search_width(const struct DrawState* state)
{
	return state->maxx > SEARCH_WIDTH + 2 ? SEARCH_WIDTH : state->maxx - 2;
}

void
draw_rect(const int startx, const int endx, const int starty, const int endy,
	const uint16_t fg, const uint16_t bg)
{
	for (int y = starty; y < endy; y++)
		for (int x = startx; x < endx; x++)
			tb_change_cell(x, y, ' ', fg, bg);
}

void
draw_box(const int startx, const int endx, const int starty, const int endy,
	const uint16_t fg, const uint16_t bg)
{
	int x, y;

	tb_change_cell(startx, starty, BORDER_NW, fg, bg);
	for (x = startx + 1; x < endx - 1; x++)
		tb_change_cell(x, starty, BORDER_HOR, fg, bg);
	tb_change_cell(endx - 1, starty, BORDER_NE, fg, bg);

	for (y = starty + 1; y < endy - 2; y++)
	{
		tb_change_cell(startx, y, BORDER_VER, fg, bg);
		for (x = startx + 1; x < endx - 1; x++)
			tb_change_cell(x, y, ' ', fg, bg);
		tb_change_cell(endx - 1, y, BORDER_VER, fg, bg);
		tb_change_cell(endx, y, ' ', SHADOW_FG, SHADOW_BG);
	}

	tb_change_cell(startx, endy - 2, BORDER_SW, fg, bg);
	for (x = startx + 1; x < endx - 1; x++)
		tb_change_cell(x, endy - 2, BORDER_HOR, fg, bg);
	tb_change_cell(endx - 1, endy - 2, BORDER_SE, fg, bg);
	tb_change_cell(endx, endy - 2, ' ', SHADOW_FG, SHADOW_BG);

	for (x = startx + 1; x < endx + 1; x++)
		tb_change_cell(x, endy - 1, ' ', SHADOW_FG, SHADOW_BG);
}

/*  mc  - (max cols) pack text and padding into this many columns
 *  fc  - (fill cols) this many columns from the left/right/center will be
 *  	  filled
 *  p_? - (padding) this many cells will be skipped from ? side
 *  ds  - (display start) string starts being displayed from this display column
 * 	  counted from the start of the string; an "entry point" joining string
 * 	  index with screen
 * fill - boolean; 1 = fill fc, 0 = don't fill fc
 * srch - search string; if empty or NULL no highlighting happens
 * sfg  - fg color of search highlght
 * sbg  - bg color of search highlight
 * */
const char*
draw_string(const int x, const int y, const uint16_t fg, const uint16_t bg,
	const char* s, const int mc, const int fc, const int p_l, const int p_r,
	const Alignment align, const int ds, const int fill,
	const uint32_t* srch, const uint16_t sfg, const uint16_t sbg)
{
	uint32_t* us = calloc(strlen(s) + 1, sizeof(uint32_t));
	if (!us)
		return NULL;
	u8_string_to_unicode(us, s, mc);
	u32_draw_string(x, y, fg, bg, us, mc, fc, p_l, p_r, align, ds, fill,
		srch, sfg, sbg);
	free(us);
	return s;
}

/*  mc  - (max cols) pack text and padding into this many columns
 *  fc  - (fill cols) this many columns from the left/right/center will be
 *  	  filled
 *  p_? - (padding) this many cells will be skipped from ? side
 *  ds  - (display start) string starts being displayed from this display column
 * 	  counted from the start of the string; an "entry point" joining string
 * 	  index with screen
 * fill - boolean; 1 = fill fc, 0 = don't fill fc
 * srch - search string; if empty or NULL no highlighting happens
 * sfg  - fg color of search highlght
 * sbg  - bg color of search highlight
 * */
void
u32_draw_string(const int x, const int y, const uint16_t fg, const uint16_t bg,
	const uint32_t* s, const int mc, const int fc, const int p_l,
	const int p_r, const Alignment align, const int ds, const int fill,
	const uint32_t* srch, const uint16_t sfg, const uint16_t sbg)
{
	/*
	 *                   <-------- visible part ---------->
	 *        +---------+-------+----------------+---------+
	 *        |         |       |                |         |
	 *        |<-- d -->|<-p_l->|<------ c ----->|<- p_r ->|
	 *        |         |<-------------- mc -------------->|
	 *        |<------------------ fc -------------------->|
	 *        x                 |                |
	 *                          ds               ds+c
	 *                           <.....(sc).....>
	 *                      (align == RIGHT in this case)
	 * */

	int d = 0; /* distance, in screen columns, from x to
		    * visible part of the string, followed by
		    * p_l */

	int c = 0; /* distance in screen columns from
		    * d + p_l to d + mc - p_r (part of the
		    * displayed string containing text, between
		    * padding; string fits within this many columns
		    * */

	int sc = 0; /* current screen column; goes from 0..c-1
		     * only increased when actual characters are
		     * written
		     */

	int ssc = 0; /* current string screen column; goes from start
		      * of the string until ds+c */

	const int ts = TAB_SIZE;

	const uint32_t* ps = s; /* pointer to current character in string;
				 * string index: ps-s */

	uint16_t cfg = fg; /* current fg */
	uint16_t cbg = bg; /* current bg */

	int hctl = 0; /* highlight "characters to live", when this
		       * reaches zero the highlighting is turned
		       * off */

	c = mc - p_l - p_r;

	if (align == LEFT)
		d = 0;
	else if (align == RIGHT)
		d = fc >= mc ? fc - mc : 0;
	else if (align == CENTER)
		d = fc >= mc ? (fc - mc) / 2 : 0;

	/* fill */
	if (fill)
		for (int j = 0; j < fc; j++)
			tb_change_cell(x + j, y, ' ', fg, bg);

	/* left padding */
	for (int j = 0; j < p_l; j++)
		tb_change_cell(x + d + j, y, ' ', fg, bg);

	sc = 0;

	/*
	 * main idea: we go from start of the string to the end and count
	 * displayed characters until we get to ds, then we also output
	 * characters; tabs count as spaces up to multiple of ts, counted from
	 * the start of the string
	 *
	 * exit condition is also sc == c, when we have reached the maximum
	 * number of characters to output
	 * */
	while (ps && *ps && sc < c)
	{
		if (srch && *srch && hctl == 0)
		{
			if (u32_starts_with(ps, srch))
			{
				hctl = u32_strlen(srch);
				cfg  = sfg;
				cbg  = sbg;
			}
		}

		if (*ps == '\t')
		{
			if (ssc < ds)
			{
				/*                     ssc-(ssc%ts)+ts
				 *              \t     |  = ssc+(ts-(ssc%ts))
				 *  ----------|-----[ :|::: ]
				 *            | |     |
				 *            | |     ds
				 * ssc-(ssc%ts) |
				 *              ssc
				 * */
				if (ssc - (ssc % ts) + ts >= ds)
				{
					for (int j = 0;
						j < ssc - (ssc % ts) + ts - ds;
						j++)
						tb_change_cell(x + d + p_l + sc
								+ j,
							y, ' ', cfg, cbg);
					sc += ssc + (ts - (ssc % ts)) - ds;
					hctl -= ssc + (ts - (ssc % ts)) - ds;
					if (hctl < 0)
						hctl = 0;
				}
				/* else, just increase counter
				 *
				 * \t
				 * ----------|----[ ::::: ]
				 * |         |      |
				 * |         |      ds
				 * |         ssc-(ssc%ts)+ts
				 * ssc         = ssc + (ts-(ssc%ts))
				 * */
				ssc += (ts - (ssc % ts));
			}
			else /* ssc >= ds */
			{
				/*
				 *   ds          ssc+(ts-(ssc%ts))
				 *   |   \t      |
				 * [ ::::::: ]---|-----
				 *       |  |
				 *       |  ds+c
				 *       ssc
				 * */
				if (ssc + (ts - (ssc % ts)) >= ds + c)
				{
					for (int j = 0; j < ds + c - ssc; j++)
						tb_change_cell(x + d + p_l + sc
								+ j,
							y, ' ', cfg, cbg);
					sc   = c;
					ssc  = ds + c;
					hctl = 0;
					cfg  = fg;
					cbg  = bg;
				}
				else /* ssc+(ts-(ssc%ts)) < ds+c */
				{
					/*
					 *      \t
					 * [ :::::::::::|::: ]-----
					 *   |  |       |   |
					 *   |  |       |   ds+c
					 *   |  |       ssc+(ts-(ssc%ts))
					 *   |  ssc
					 *   ds
					 * */
					for (int j = 0; j < ts - (ssc % ts); j++)
						tb_change_cell(x + d + p_l + sc
								+ j,
							y, ' ', cfg, cbg);
					sc += ts - (ssc % ts);
					hctl -= ts - (ssc % ts);
					if (hctl < 0)
						hctl = 0;
					ssc += ts - (ssc % ts);
				}
			}
		}
		else
		{
			if (ssc >= ds)
			{
				if (sc < c)
					tb_change_cell(x + d + p_l + sc, y, *ps,
						cfg, cbg);
				sc++;
				hctl--;
				if (hctl < 0)
					hctl = 0;
			}
			ssc++;
		}
		if (hctl == 0)
		{
			cfg = fg;
			cbg = bg;
		}
		ps++;
	}

	/* right padding */
	for (int j = 0; j < p_r; j++)
		tb_change_cell(x + d + p_l + c + j, y, ' ', fg, bg);
}

const struct DrawState*
draw_help(const struct DrawState* state)
{
	if (!state)
		return NULL;

	int startx, endx, starty, endy;
	int w	 = state->help_width;
	int h	 = HELP_HEIGHT;
	int row	 = 0;
	int maxx = state->maxx;
	int maxy = state->maxy;

	startx = w > maxx ? 0 : (maxx - w) / 2;
	endx   = w > maxx ? maxx - 1 : maxx - startx;

	starty = h > maxy - 1 ? 0 : (maxy - h) / 2;
	endy   = h > maxy - 1 ? maxy - 2 : maxy - starty;

	draw_box(startx, endx, starty, endy, DLG_FG, DLG_BG);
	if (!draw_string(startx, starty, TITLE_FG, TITLE_BG, help_title,
		    strlen(help_title) + 2, w - 2, 1, 1, CENTER, 0, 0, NULL, 0,
		    0))
		return NULL;
	while (row < LEN(help))
	{
		if (!draw_string(startx + 1, starty + 1 + row, DLG_FG, DLG_BG,
			    help[row], w - 2, w - 2, 1, 1, LEFT, 0, 1, NULL, 0,
			    0))
			return NULL;
		row++;
	}
	return state;
}

const struct DrawState*
draw_editbox(const struct DrawState* state)
{
	if (!state)
		return NULL;

	int startx, endx, starty, endy;
	int w	 = state->edit_width;
	int h	 = EDIT_HEIGHT;
	int maxx = state->maxx;
	int maxy = state->maxy;
	char flags_buf[MAXFLAGSBUF + 4];
	struct PoEntry* entry = &state->entries[state->msgid_number - 1];
	size_t dlen	      = 0;

	strcpy(flags_buf, "[ ");
	format_flags(flags_buf + 2, MAXFLAGSBUF, entry);
	strcpy(flags_buf + 2 + MAXFLAGSBUF - 1, " ]");

	startx = w > maxx ? 0 : (maxx - w) / 2;
	endx   = w > maxx ? maxx - 1 : maxx - startx;

	starty = h > maxy - 1 ? 0 : (maxy - h) / 2;
	endy   = h > maxy - 1 ? maxy - 2 : maxy - starty;

	draw_box(startx, endx, starty, endy, DLG_FG, DLG_BG);
	if (!draw_string(startx + 1, starty, TITLE_FG, TITLE_BG, editbox_title,
		    strlen(editbox_title) + 2, w - 2, 1, 1, CENTER, 0, 0, NULL,
		    0, 0))
		return NULL;

	uint16_t fg = state->edit_info_focused ? EDIT_MSGID_FOCUS_FG
					       : EDIT_MSGID_FG;
	uint16_t bg = state->edit_info_focused ? EDIT_MSGID_FOCUS_BG
					       : EDIT_MSGID_BG;

	draw_rect(startx + 1, endx - 1, starty + 1, endy - h / 2 - 1, fg, bg);
	for (size_t i = 0; i < (h - 3) / 2 + 1
		&& state->info_first_shown_row + i < state->info_rows_count;
		i++)
	{
		struct BufferLine* curline
			= &state->info_buffer[state->info_first_shown_row + i];
		if (!curline || !curline->text)
			return NULL;
		dlen = display_length(state, curline->text, curline->length);
		if (dlen > state->info_first_shown_column)
			u32_draw_string(startx + 1, starty + 1 + i,
				curline->fg == 0 ? fg : curline->fg,
				curline->bg == 0 ? bg : curline->bg,
				curline->text, w - 2, w - 2, 1, 1, LEFT,
				state->info_first_shown_column, 1, NULL, 0, 0);
	}

	if (state->info_first_shown_row > 0)
		u32_draw_string(endx - 2, starty + 1, fg, bg,
			(uint32_t*)L"\u2191", 1, 1, 0, 0, LEFT, 0, 0, NULL, 0,
			0);
	if (state->info_first_shown_row + (h - 3) / 2 < state->info_rows_count)
		u32_draw_string(endx - 2, starty + (h - 3) / 2, fg, bg,
			(uint32_t*)L"\u2193", 1, 1, 0, 0, LEFT, 0, 0, NULL, 0,
			0);

	fg = state->edit_info_focused ? EDIT_MSGSTR_FG : EDIT_MSGSTR_FOCUS_FG;
	bg = state->edit_info_focused ? EDIT_MSGSTR_BG : EDIT_MSGSTR_FOCUS_BG;

	draw_rect(startx + 1, endx - 1, endy - h / 2 - 1, endy - 2, fg, bg);
	for (size_t i = 0; i < (h - 3) / 2 + 1
		&& state->input_first_shown_row + i < state->input_rows_count;
		i++)
	{
		struct BufferLine* curline
			= &state->input_buffer[state->input_first_shown_row + i];
		if (!curline || !curline->text)
			return NULL;
		dlen = display_length(state, curline->text, curline->length);
		if (dlen > state->input_first_shown_column)
			u32_draw_string(startx + 1, endy - h / 2 - 1 + i, fg,
				bg, curline->text, w - 2, w - 2, 1, 1, LEFT,
				state->input_first_shown_column, 1, NULL, 0, 0);
	}

	if (state->edit_info_focused)
		tb_set_cursor(TB_HIDE_CURSOR, TB_HIDE_CURSOR);
	else
		tb_set_cursor(startx + 2 + state->input_display_column
				- state->input_first_shown_column,
			endy - h / 2 - 1 + state->input_row
				- state->input_first_shown_row);

	char sbuf[MAXBUFLINE];
	if (entry->msgstr_count > 1)
		sprintf(sbuf, "%d[%d]:%d[%d]/%d (plural: %d/%d)",
			state->input_display_column,
			state->input_first_shown_column, state->input_row,
			state->input_first_shown_row, state->input_rows_count,
			state->msgstr_index + 1, entry->msgstr_count);
	else
		sprintf(sbuf, "%d[%d]:%d[%d]/%d", state->input_display_column,
			state->input_first_shown_column, state->input_row,
			state->input_first_shown_row, state->input_rows_count);
	draw_string(startx + 1, endy - 2, DLG_FG, DLG_BG, sbuf,
		strlen(sbuf) + 2, w - 2, 1, 1, LEFT, 0, 0, NULL, 0, 0);
	if (entry->flags != FL_NONE)
		if (!draw_string(endx - MAXFLAGSBUF - 4 - 5, endy - 2, DLG_FG,
			    DLG_BG, flags_buf, MAXFLAGSBUF + 4 - 1,
			    MAXFLAGSBUF + 4 - 1, 0, 0, LEFT, 0, 0, NULL, 0, 0))
			return NULL;
	return state;
}

const struct DrawState*
draw_search(const struct DrawState* state)
{
	int startx, endx, starty, endy;
	int w	    = state->search_width;
	int h	    = SEARCH_HEIGHT;
	int maxx    = state->maxx;
	int maxy    = state->maxy;
	size_t len  = u32_strlen(state->search);
	size_t dlen = 0;

	startx = w > maxx ? 0 : (maxx - w) / 2;
	endx   = w > maxx ? maxx - 1 : maxx - startx;

	starty = maxy - 1 - h;
	endy   = maxy - 1;

	draw_box(startx, endx, starty, endy, SEARCH_FG, SEARCH_BG);
	draw_rect(startx + 1, endx - 1, starty + 1, endy - 2, SEARCH_EDIT_FG,
		SEARCH_EDIT_BG);
	dlen = display_length(state, state->search, len);
	if (dlen > state->search_first_shown_column)
		u32_draw_string(startx + 1, starty + 1, SEARCH_EDIT_FG,
			SEARCH_EDIT_BG, state->search, w - 2, w - 2, 1, 1, LEFT,
			state->search_first_shown_column, 1, NULL, 0, 0);
	tb_set_cursor(startx + 2 + state->search_display_column
			- state->search_first_shown_column,
		starty + 1);
	return state;
}

const struct DrawState*
draw_status(const struct DrawState* state)
{
	if (!state)
		return NULL;

	char buf[MAXBUFLINE];
	int seg_size			 = state->maxx / 3;
	int current_start		 = 0;
	const struct StatusSegment* pseg = status_segments;

	if (state->in_prompt && state->prompt[0])
	{
		if (!draw_string(0, state->maxy - 1, PROMPT_FG, PROMPT_BG,
			    state->prompt, strlen(state->prompt) + 2,
			    state->maxx, 1, 1, CENTER, 0, 1, NULL, 0, 0))
			return NULL;
	}
	else if (state->error && state->error[0])
	{
		if (!draw_string(0, state->maxy - 1, ERROR_FG, ERROR_BG,
			    state->error, strlen(state->error) + 2, state->maxx,
			    1, 1, CENTER, 0, 1, NULL, 0, 0))
			return NULL;
	}
	else
		while (pseg < status_segments + LEN(status_segments))
		{
			int last = pseg - status_segments
				== LEN(status_segments) - 1;
			buf[0] = 0;
			if (pseg->callback)
				pseg->callback(buf, MAXBUFLINE, pseg->format,
					state);
			else
				strncpy(buf, pseg->format, MAXBUFLINE - 1);
			buf[MAXBUFLINE - 1] = 0;
			if (pseg == status_segments + LEN(status_segments) - 1)
				seg_size = state->maxx - 1 - current_start;
			if (!draw_string(current_start, state->maxy - 1,
				    pseg->fg, pseg->bg, buf,
				    MIN(seg_size, strlen(buf) + 2),
				    last ? seg_size + 1 : seg_size, 1, 1,
				    pseg->alignment, 0, 1, NULL, 0, 0))
				return NULL;
			pseg++;
			current_start += seg_size;
		}
	return state;
}

const struct DrawState*
draw_list_visible(const struct DrawState* state)
{
	if (!state)
		return NULL;

	if (state->msgid_count == 0)
		return state;

	const struct PoEntry* current = NULL;
	int max_screen_entries	      = state->maxy - 1;
	int cy			      = 0;
	size_t i		      = state->first_shown_msgid;

	while (i <= state->msgid_count && cy < max_screen_entries)
	{
		current = state->entries + i - 1;

		int sel = i == state->msgid_number;
		char flags_buf[MAXFLAGSBUF];

		if (sel)
			draw_rect(0, state->maxx - 1, cy, cy + 1, LIST_SEL_FG,
				LIST_SEL_BG);

		format_flags(flags_buf, MAXFLAGSBUF, current);

		if (!draw_string(0, cy, sel ? LIST_FLAGS_SEL_FG : LIST_FLAGS_FG,
			    sel ? LIST_FLAGS_SEL_BG : LIST_FLAGS_BG, flags_buf,
			    MAXFLAGSBUF - 1 + 2, MAXFLAGSBUF - 1 + 2, 2, 0,
			    LEFT, 0, 1, NULL, 0, 0))
			return NULL;
		if (current->msgid)
			u32_draw_string(MAXFLAGSBUF - 1 + 2, cy,
				sel ? LIST_MSGID_SEL_FG : LIST_MSGID_FG,
				sel ? LIST_MSGID_SEL_BG : LIST_MSGID_BG,
				current->msgid,
				state->maxx / 2 - MAXFLAGSBUF + 1 - 2,
				state->maxx / 2 - MAXFLAGSBUF + 1 - 2, 2, 0,
				LEFT, 0, 1, state->search, SEARCH_HL_FG,
				SEARCH_HL_BG);
		else if (current == state->entries) /* special */
			draw_string(MAXFLAGSBUF - 1 + 2, cy,
				sel ? LIST_MSGID_SPECIAL_SEL_FG
				    : LIST_MSGID_SPECIAL_FG,
				sel ? LIST_MSGID_SPECIAL_SEL_BG
				    : LIST_MSGID_SPECIAL_BG,
				first_entry_msgid,
				state->maxx / 2 - MAXFLAGSBUF + 1 - 2,
				state->maxx / 2 - MAXFLAGSBUF + 1 - 2, 2, 0,
				LEFT, 0, 1, NULL, 0, 0);

		/* don't use msgstr_index as then the entire list will shift
		 * when changing currently displayed plural form msgstr in an
		 * edit box */
		if (current->msgstr && current->msgstr_count > 0)
			u32_draw_string(state->maxx / 2, cy,
				current->warning
					? (sel ? LIST_MSGSTR_SEL_WARN_FG
					       : LIST_MSGSTR_WARN_FG)
					: (sel ? LIST_MSGSTR_SEL_FG
					       : LIST_MSGSTR_FG),
				current->warning
					? (sel ? LIST_MSGSTR_SEL_WARN_BG
					       : LIST_MSGSTR_WARN_BG)
					: (sel ? LIST_MSGSTR_SEL_BG
					       : LIST_MSGSTR_BG),
				*(current->msgstr), state->maxx / 2,
				state->maxx / 2, 2, 2, LEFT, 0, 1,
				state->search, SEARCH_HL_FG, SEARCH_HL_BG);
		i++;
		cy++;
	}
	return state;
}

const struct DrawState*
draw(const struct DrawState* state)
{
	if (!state)
		return NULL;

	if (!draw_list_visible(state))
		return NULL;

	if (state->show_help)
		if (!draw_help(state))
			return NULL;

	if (state->show_edit)
		if (!draw_editbox(state))
			return NULL;

	if (state->show_search)
		if (!draw_search(state))
			return NULL;

	if (!draw_status(state))
		return NULL;

	return state;
}