poe

Уређивач .po фајлова
Дневник | Датотеке | Референце | ПРОЧИТАЈМЕ | ЛИЦЕНЦА

чување bf4e5a286dcc78d1a1d3891593e07feb05d8b6c6
родитељ 70bd2d786f3c7cd50e191ef0c6d9ba04c9f1d551
Аутор: Страхиња Радић <contact@strahinja.org>
Датум:   Fri, 13 Aug 2021 16:34:05 +0200

Formatted code using clang-format; replaced while with goto in places to decrease indentation

Signed-off-by: Страхиња Радић <contact@strahinja.org>

Diffstat:
Mdraw.c | 644+++++++++++++++++++++++++++++++++++++++----------------------------------------
Mdraw.h | 86++++++++++++++++++++++++++++++++++++-------------------------------------------
Mpo.c | 930++++++++++++++++++++++++++++++++++++++-----------------------------------------
Mpo.h | 115+++++++++++++++++++++++++++++++++++++++----------------------------------------
Mpoe.c | 1132+++++++++++++++++++++++++++++++++++++++----------------------------------------
Mutil.c | 74+++++++++++++++++++++++++++++++++++++-------------------------------------
Mutil.h | 14+++++++-------
измењених датотека: 7, додавања: 1458(+), брисања: 1537(-)

diff --git a/draw.c b/draw.c @@ -1,7 +1,7 @@ /* See the file LICENSE for copyright and license details. */ #include <errno.h> -#include <stdlib.h> #include <stdio.h> +#include <stdlib.h> #include <string.h> #include "termbox.h" @@ -12,7 +12,7 @@ #include "version.h" static const char* help_title = "Main view help"; -static const char* help[] = { +static const char* help[] = { "", "Esc - Close dialog, clear error", "Enter - Edit entry", @@ -23,7 +23,7 @@ static const char* help[] = { "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", + "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", @@ -34,35 +34,35 @@ static const char* help[] = { "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" +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" }; -const char* flag_strings[] = { - [FL_C_FORMAT] = "c-format", - [FL_NO_C_FORMAT] = "no-c-format", - [FL_FUZZY] = "fuzzy" +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]?"; -void +void init_bufferline(struct BufferLine* bl) { if (!bl) return; bl->text = NULL; bl->length = 0; - bl->fg = 0; - bl->bg = 0; + bl->fg = 0; + bl->bg = 0; } void @@ -76,60 +76,60 @@ free_bufferline(struct BufferLine* bl) bl->text = NULL; } bl->length = 0; - bl->fg = 0; - bl->bg = 0; + bl->fg = 0; + bl->bg = 0; } -void -init_drawstate(struct DrawState* state, char* error, char* filename, - char* prompt) +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_column = 0; - state->input_first_shown_column = 0; - state->input_first_shown_row = 0; - state->input_rows_count = 0; - state->input_display_column = 0; - state->input_changed_fuzzy = UNCHANGED; - 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->paste_buffer = NULL; - state->paste_rows_count = 0; - state->search_column = 0; + state->error = error; + state->filename = filename; + state->search = NULL; + state->input_buffer = NULL; + state->input_column = 0; + state->input_first_shown_column = 0; + state->input_first_shown_row = 0; + state->input_rows_count = 0; + state->input_display_column = 0; + state->input_changed_fuzzy = UNCHANGED; + 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->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; + 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 +void free_drawstate(struct DrawState* state) { struct BufferLine* current = NULL; struct PoEntry* current_entry = NULL; - size_t i = 0; + size_t i = 0; if (state->search) free(state->search); @@ -138,7 +138,7 @@ free_drawstate(struct DrawState* state) { for (i = 0; i < state->input_rows_count; i++) { - current = state->input_buffer+i; + current = state->input_buffer + i; free_bufferline(current); } free(state->input_buffer); @@ -147,7 +147,7 @@ free_drawstate(struct DrawState* state) { for (i = 0; i < state->paste_rows_count; i++) { - current = state->paste_buffer+i; + current = state->paste_buffer + i; free_bufferline(current); } free(state->paste_buffer); @@ -156,7 +156,7 @@ free_drawstate(struct DrawState* state) { for (i = 0; i < state->info_rows_count; i++) { - current = state->info_buffer+i; + current = state->info_buffer + i; free_bufferline(current); } free(state->info_buffer); @@ -165,21 +165,21 @@ free_drawstate(struct DrawState* state) { for (i = 0; i < state->real_msgid_count; i++) { - current_entry = state->entries+i; + current_entry = state->entries + i; free_po_entry(current_entry); } free(state->entries); } } -struct DrawState* +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; + uint32_t* ppf = entry->plural_forms; char token[MAXBUFLINE]; char* ptoken = token; @@ -198,7 +198,7 @@ set_nplurals(struct DrawState* state, struct PoEntry* entry) else if (read_number && *ppf == ';') { *ptoken = 0; - ptoken = token; + ptoken = token; break; } else @@ -220,10 +220,10 @@ split(const uint32_t* s) if (!s) return NULL; - size_t num_lines = u32_lines_in_string(s, NULL); - struct BufferLine* newbuf = NULL; + size_t num_lines = u32_lines_in_string(s, NULL); + struct BufferLine* newbuf = NULL; struct BufferLine* pnewbuf = NULL; - const uint32_t* ps = s; + const uint32_t* ps = s; newbuf = calloc(num_lines, sizeof(struct BufferLine)); if (!newbuf) @@ -236,10 +236,10 @@ split(const uint32_t* s) uint32_t* newtext = calloc(MAXBUFLINE, sizeof(uint32_t)); if (!newtext) return NULL; - pnewbuf->text = newtext; - size_t len = u32_line_length(ps); - pnewbuf->length = u32_strncpy(pnewbuf->text, ps, - MIN(len+1, MAXBUFLINE)); + pnewbuf->text = newtext; + size_t len = u32_line_length(ps); + pnewbuf->length = u32_strncpy(pnewbuf->text, ps, + MIN(len + 1, MAXBUFLINE)); ps += len + (ps[len] ? 2 : 0); pnewbuf++; } @@ -251,10 +251,10 @@ split(const uint32_t* s) * 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) +display_length(const struct DrawState* state, uint32_t* buffer, + const size_t max_index) { - size_t len = 0; + size_t len = 0; uint32_t* pbuf = buffer; for (size_t i = 0; *pbuf && i < max_index; i++) @@ -274,18 +274,17 @@ display_length(const struct DrawState* state, uint32_t* buffer, */ size_t input_length(const struct DrawState* state, uint32_t* buffer, - const size_t max_index) + const size_t max_index) { - size_t len = 0; + size_t len = 0; uint32_t* pbuf = buffer; - size_t i = 0; + 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); + len += MIN(TAB_SIZE - (len % TAB_SIZE), max_index - len); else len++; } @@ -295,7 +294,7 @@ input_length(const struct DrawState* state, uint32_t* buffer, void draw_rect(const int startx, const int endx, const int starty, const int endy, - const uint16_t fg, const uint16_t bg) + const uint16_t fg, const uint16_t bg) { for (int y = starty; y < endy; y++) for (int x = startx; x < endx; x++) @@ -304,32 +303,32 @@ draw_rect(const int startx, const int endx, const int starty, const int endy, void draw_box(const int startx, const int endx, const int starty, const int endy, - const uint16_t fg, const uint16_t bg) + 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++) + 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); + tb_change_cell(endx - 1, starty, BORDER_NE, fg, bg); - for (y = starty+1; y < endy-2; y++) + 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++) + 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 - 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); + 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); + 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 @@ -346,17 +345,16 @@ draw_box(const int startx, const int endx, const int starty, const int endy, * */ 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) + 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)); + 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); + srch, sfg, sbg); free(us); return s; } @@ -375,12 +373,10 @@ draw_string(const int x, const int y, const uint16_t fg, const uint16_t bg, * */ 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) + 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 ----------> * +---------+-------+----------------+---------+ @@ -394,54 +390,53 @@ u32_draw_string(const int x, const int y, const uint16_t fg, const uint16_t bg, * (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 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 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 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 */ + 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; + 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 */ + 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 */ + 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; + d = fc >= mc ? fc - mc : 0; else if (align == CENTER) - d = fc >= mc ? (fc - mc)/2 : 0; + 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); + 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); + tb_change_cell(x + d + j, y, ' ', fg, bg); sc = 0; @@ -461,8 +456,8 @@ u32_draw_string(const int x, const int y, const uint16_t fg, const uint16_t bg, if (u32_starts_with(ps, srch)) { hctl = u32_strlen(srch); - cfg = sfg; - cbg = sbg; + cfg = sfg; + cbg = sbg; } } @@ -480,12 +475,14 @@ u32_draw_string(const int x, const int y, const uint16_t fg, const uint16_t bg, * */ 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; + 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; } @@ -500,7 +497,7 @@ u32_draw_string(const int x, const int y, const uint16_t fg, const uint16_t bg, * */ ssc += (ts - (ssc % ts)); } - else /* ssc >= ds */ + else /* ssc >= ds */ { /* * ds ssc+(ts-(ssc%ts)) @@ -510,36 +507,38 @@ u32_draw_string(const int x, const int y, const uint16_t fg, const uint16_t bg, * | ds+c * ssc * */ - if (ssc+(ts-(ssc%ts)) >= ds+c) + 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, + 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; + sc = c; + ssc = ds + c; hctl = 0; - cfg = fg; - cbg = bg; + cfg = fg; + cbg = bg; } - else /* ssc+(ts-(ssc%ts)) < ds+c */ + 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, + /* + * \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); + sc += ts - (ssc % ts); + hctl -= ts - (ssc % ts); if (hctl < 0) hctl = 0; - ssc += ts-(ssc%ts); + ssc += ts - (ssc % ts); } } } @@ -548,8 +547,8 @@ u32_draw_string(const int x, const int y, const uint16_t fg, const uint16_t bg, if (ssc >= ds) { if (sc < c) - tb_change_cell(x+d+p_l+sc, y, *ps, - cfg, cbg); + tb_change_cell(x + d + p_l + sc, y, *ps, + cfg, cbg); sc++; hctl--; if (hctl < 0) @@ -567,8 +566,7 @@ u32_draw_string(const int x, const int y, const uint16_t fg, const uint16_t bg, /* right padding */ for (int j = 0; j < p_r; j++) - tb_change_cell(x+d+p_l+c+j, y, ' ', fg, bg); - + tb_change_cell(x + d + p_l + c + j, y, ' ', fg, bg); } const struct DrawState* @@ -578,28 +576,28 @@ draw_help(const struct DrawState* state) return NULL; int startx, endx, starty, endy; - int w = HELP_WIDTH; - int h = HELP_HEIGHT; - int row = 0; + int w = 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; + 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)) + 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)) + 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++; } @@ -613,117 +611,106 @@ draw_editbox(const struct DrawState* state) return NULL; int startx, endx, starty, endy; - int w = 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; + int w = 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, " ]"); + 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; + 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)) + 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; + 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++) + 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]; + 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); + 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); + 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++) + 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]; + 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); + 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, + 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); + 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); + 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); + 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)) + 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; } @@ -732,31 +719,30 @@ const struct DrawState* draw_search(const struct DrawState* state) { int startx, endx, starty, endy; - int w = SEARCH_WIDTH; - int h = SEARCH_HEIGHT; + int w = 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; + 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); + 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); + 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; } @@ -767,45 +753,43 @@ draw_status(const struct DrawState* state) return NULL; char buf[MAXBUFLINE]; - int seg_size = state->maxx/3; - int current_start = 0; + 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)) + 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)) + 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; + int last = pseg - status_segments + == LEN(status_segments) - 1; buf[0] = 0; if (pseg->callback) - pseg->callback(buf, MAXBUFLINE, pseg->format, - state); + pseg->callback(buf, MAXBUFLINE, pseg->format, + state); else strncpy(buf, pseg->format, MAXBUFLINE); - 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)) + 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; @@ -813,7 +797,7 @@ draw_status(const struct DrawState* state) return state; } -const struct DrawState* +const struct DrawState* draw_list_visible(const struct DrawState* state) { if (!state) @@ -823,53 +807,53 @@ draw_list_visible(const struct DrawState* state) return state; const struct PoEntry* current = NULL; - int max_screen_entries = state->maxy-1; - int cy = 0; - size_t i = state->first_shown_msgid; + 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; + 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); + 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)) + 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); + 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 && - current->msgstr + state->msgstr_index) + if (current->msgstr && current->msgstr_count > 0 + && current->msgstr + state->msgstr_index) u32_draw_string(state->maxx/2, cy, sel ? LIST_MSGSTR_SEL_FG : LIST_MSGSTR_FG, diff --git a/draw.h b/draw.h @@ -3,33 +3,25 @@ #include "termbox.h" -#define BORDER_NW ((uint32_t)L'\u250c') -#define BORDER_NE ((uint32_t)L'\u2510') -#define BORDER_SW ((uint32_t)L'\u2514') -#define BORDER_SE ((uint32_t)L'\u2518') -#define BORDER_VER ((uint32_t)L'\u2502') -#define BORDER_HOR ((uint32_t)L'\u2500') +#define BORDER_NW ((uint32_t)L'\u250c') +#define BORDER_NE ((uint32_t)L'\u2510') +#define BORDER_SW ((uint32_t)L'\u2514') +#define BORDER_SE ((uint32_t)L'\u2518') +#define BORDER_VER ((uint32_t)L'\u2502') +#define BORDER_HOR ((uint32_t)L'\u2500') -#define EDIT_WIDTH 70 -#define EDIT_HEIGHT 20 -#define HELP_WIDTH 50 -#define HELP_HEIGHT 21 -#define SEARCH_WIDTH 20 -#define SEARCH_HEIGHT 4 -#define TAB_SIZE 8 -#define MAXSEARCH (512+1) +#define EDIT_WIDTH 70 +#define EDIT_HEIGHT 20 +#define HELP_WIDTH 50 +#define HELP_HEIGHT 21 +#define SEARCH_WIDTH 20 +#define SEARCH_HEIGHT 4 +#define TAB_SIZE 8 +#define MAXSEARCH (512 + 1) -typedef enum { - LEFT, - CENTER, - RIGHT -} Alignment; +typedef enum { LEFT, CENTER, RIGHT } Alignment; -typedef enum { - UNCHANGED, - SET, - CLEARED -} FlagChange; +typedef enum { UNCHANGED, SET, CLEARED } FlagChange; struct BufferLine { uint32_t* text; @@ -43,7 +35,7 @@ struct DrawState { char* prompt; char* filename; uint32_t* search; - struct BufferLine* input_buffer; /* edit box */ + struct BufferLine* input_buffer; /* edit box */ int input_column; int input_row; int input_first_shown_column; @@ -52,8 +44,8 @@ struct DrawState { int input_display_column; FlagChange input_changed_fuzzy; int saved_column; - struct BufferLine* info_buffer; /* information pane above - * edit box */ + struct BufferLine* info_buffer; /* information pane above + * edit box */ int info_first_shown_column; int info_first_shown_row; int info_rows_count; @@ -83,8 +75,8 @@ struct DrawState { size_t fuzzy_count; size_t untranslated_count; size_t obsolete_count; - size_t real_msgid_count; /* msgid_count + obsolete_count */ - int msgstr_index; /* with msgstr[] */ + size_t real_msgid_count; /* msgid_count + obsolete_count */ + int msgstr_index; /* with msgstr[] */ size_t first_shown_msgid; }; @@ -100,29 +92,29 @@ extern struct PoEntry entries[]; void init_bufferline(struct BufferLine* bl); void free_bufferline(struct BufferLine* bl); -void init_drawstate(struct DrawState* state, char* error, char* filename, - char* prompt); +void init_drawstate(struct DrawState* state, char* error, char* filename, + char* prompt); void free_drawstate(struct DrawState* state); struct DrawState* set_nplurals(struct DrawState* state, struct PoEntry* entry); struct BufferLine* split(const uint32_t* s); size_t display_length(const struct DrawState* state, uint32_t* buffer, - const size_t max_index); + const size_t max_index); size_t input_length(const struct DrawState* state, uint32_t* buffer, - const size_t max_index); -void draw_rect(const int startx, const int endx, const int starty, - const int endy, const uint16_t fg, const uint16_t bg); -void draw_box(const int startx, const int endx, const int starty, - const int endy, const uint16_t fg, const uint16_t bg); -const char* draw_string(const int x, const int y, const uint16_t fg, const - uint16_t bg, const char* s, const int max_cols, const int - fill_cols, const int padding_left, const int padding_right, - const Alignment align, const int ds, const int fill, - const uint32_t* srch, const uint16_t sfg, const uint16_t sbg); -void u32_draw_string(const int x, const int y, const uint16_t fg, const uint16_t bg, - const uint32_t* s, const int max_cols, const int fill_cols, - const int padding_left, const int padding_right, - const Alignment align, const int ds, const int fill, - const uint32_t* srch, const uint16_t sfg, const uint16_t sbg); + const size_t max_index); +void draw_rect(const int startx, const int endx, const int starty, + const int endy, const uint16_t fg, const uint16_t bg); +void draw_box(const int startx, const int endx, const int starty, + const int endy, const uint16_t fg, const uint16_t bg); +const char* draw_string(const int x, const int y, const uint16_t fg, + const uint16_t bg, const char* s, const int max_cols, + const int fill_cols, const int padding_left, const int padding_right, + const Alignment align, const int ds, const int fill, + const uint32_t* srch, const uint16_t sfg, const uint16_t sbg); +void u32_draw_string(const int x, const int y, const uint16_t fg, + const uint16_t bg, const uint32_t* s, const int max_cols, + const int fill_cols, const int padding_left, const int padding_right, + const Alignment align, const int ds, const int fill, + const uint32_t* srch, const uint16_t sfg, const uint16_t sbg); const struct DrawState* draw_help(const struct DrawState* state); const struct DrawState* draw_editbox(const struct DrawState* state); const struct DrawState* draw_search(const struct DrawState* state); diff --git a/po.c b/po.c @@ -15,32 +15,32 @@ char* format_flags(char* buffer, const size_t max, const struct PoEntry* entry) { snprintf(buffer, max, "%c%c%c%c", - entry->flags & FL_C_FORMAT ? 'C' : ' ', - entry->flags & FL_FUZZY ? 'F' : ' ', - entry->msgstr_count > 1 ? 'P' : ' ', - entry->flags & FL_NO_C_FORMAT ? 'N' : ' '); + entry->flags & FL_C_FORMAT ? 'C' : ' ', + entry->flags & FL_FUZZY ? 'F' : ' ', + entry->msgstr_count > 1 ? 'P' : ' ', + entry->flags & FL_NO_C_FORMAT ? 'N' : ' '); return buffer; } -void +void init_po_entry(struct PoEntry* entry) { - entry->flags = FL_NONE; - entry->obsolete = 0; - entry->msgid = NULL; - entry->msgid_plural = NULL; - entry->plural_forms = NULL; - entry->msgstr = NULL; - entry->msgstr_count = 0; - entry->comments = NULL; - entry->comment_lines = 0; - entry->trans_comments = NULL; + entry->flags = FL_NONE; + entry->obsolete = 0; + entry->msgid = NULL; + entry->msgid_plural = NULL; + entry->plural_forms = NULL; + entry->msgstr = NULL; + entry->msgstr_count = 0; + entry->comments = NULL; + entry->comment_lines = 0; + entry->trans_comments = NULL; entry->trans_comment_lines = 0; - entry->ref_comments = NULL; + entry->ref_comments = NULL; entry->ref_comment_lines = 0; } -void +void free_po_entry(struct PoEntry* entry) { if (entry->msgid) @@ -68,8 +68,8 @@ free_po_entry(struct PoEntry* entry) if (entry->trans_comments) { uint32_t** trans_comment = entry->trans_comments; - while (trans_comment < entry->trans_comments + - entry->trans_comment_lines) + while (trans_comment + < entry->trans_comments + entry->trans_comment_lines) free(*trans_comment++); free(entry->trans_comments); } @@ -92,7 +92,7 @@ set_flags(struct PoEntry* entry, const Flags flags) return entry; } -struct PoEntry* +struct PoEntry* set_msgid(struct PoEntry* entry, const char* msgid, int append) { uint32_t umsgid[MAXMSG]; @@ -102,7 +102,7 @@ set_msgid(struct PoEntry* entry, const char* msgid, int append) return u32_set_msgid(entry, umsgid, append); } -struct PoEntry* +struct PoEntry* set_msgid_plural(struct PoEntry* entry, const char* msgid_plural, int append) { uint32_t umsgid_plural[MAXMSG]; @@ -112,7 +112,7 @@ set_msgid_plural(struct PoEntry* entry, const char* msgid_plural, int append) return u32_set_msgid_plural(entry, umsgid_plural, append); } -struct PoEntry* +struct PoEntry* set_plural_forms(struct PoEntry* entry, const char* plural_forms, int append) { uint32_t uplural_forms[MAXMSG]; @@ -122,9 +122,9 @@ set_plural_forms(struct PoEntry* entry, const char* plural_forms, int append) return u32_set_plural_forms(entry, uplural_forms, append); } -struct PoEntry* +struct PoEntry* set_msgstr(struct PoEntry* entry, const char* msgstr, int msgstr_index, - int append, struct DrawState* state) + int append, struct DrawState* state) { uint32_t umsgstr[MAXMSG]; if (!entry) @@ -133,7 +133,7 @@ set_msgstr(struct PoEntry* entry, const char* msgstr, int msgstr_index, return u32_set_msgstr(entry, umsgstr, msgstr_index, append, state); } -struct PoEntry* +struct PoEntry* u32_set_msgid(struct PoEntry* entry, const uint32_t* msgid, int append) { if (!entry) @@ -151,9 +151,9 @@ u32_set_msgid(struct PoEntry* entry, const uint32_t* msgid, int append) return entry; } -struct PoEntry* -u32_set_msgid_plural(struct PoEntry* entry, const uint32_t* msgid_plural, - int append) +struct PoEntry* +u32_set_msgid_plural(struct PoEntry* entry, const uint32_t* msgid_plural, + int append) { if (!entry) return NULL; @@ -170,9 +170,9 @@ u32_set_msgid_plural(struct PoEntry* entry, const uint32_t* msgid_plural, return entry; } -struct PoEntry* -u32_set_plural_forms(struct PoEntry* entry, const uint32_t* plural_forms, - int append) +struct PoEntry* +u32_set_plural_forms(struct PoEntry* entry, const uint32_t* plural_forms, + int append) { if (!entry) return NULL; @@ -189,37 +189,37 @@ u32_set_plural_forms(struct PoEntry* entry, const uint32_t* plural_forms, return entry; } -struct PoEntry* +struct PoEntry* u32_set_msgstr(struct PoEntry* entry, const uint32_t* msgstr, int msgstr_index, - int append, struct DrawState* state) + int append, struct DrawState* state) { if (!entry) return NULL; if (entry->msgstr_count == 0 || msgstr_index + 1 > entry->msgstr_count) { uint32_t** newmsgstr = NULL; - entry->msgstr_count = msgstr_index + 1; + entry->msgstr_count = msgstr_index + 1; if (!entry->msgstr) { newmsgstr = calloc(entry->msgstr_count, - sizeof(uint32_t*)); + sizeof(uint32_t*)); /* when the .po file is not a standard text file (no LF * at end) */ if (state->untranslated_count > 0) state->untranslated_count--; } else - newmsgstr = realloc(entry->msgstr, - entry->msgstr_count * - sizeof(uint32_t*)); + newmsgstr = realloc(entry->msgstr, + entry->msgstr_count * sizeof(uint32_t*)); if (!newmsgstr) return NULL; *(newmsgstr + msgstr_index) = NULL; - entry->msgstr = newmsgstr; + entry->msgstr = newmsgstr; } if (!*(entry->msgstr + msgstr_index)) { - *(entry->msgstr + msgstr_index) = calloc(MAXMSG, sizeof(uint32_t)); + *(entry->msgstr + msgstr_index) + = calloc(MAXMSG, sizeof(uint32_t)); if (!*(entry->msgstr + msgstr_index)) return NULL; } @@ -244,7 +244,7 @@ u32_add_comment(struct PoEntry* entry, const uint32_t* comment) comments = calloc(entry->comment_lines, sizeof(uint32_t*)); else comments = realloc(entry->comments, - entry->comment_lines * sizeof(uint32_t*)); + entry->comment_lines * sizeof(uint32_t*)); if (!comments) return NULL; entry->comments = comments; @@ -252,7 +252,7 @@ u32_add_comment(struct PoEntry* entry, const uint32_t* comment) new_line = calloc(MAXCOMMENTLINE, sizeof(uint32_t)); if (!new_line) return NULL; - entry->comments[entry->comment_lines-1] = new_line; + entry->comments[entry->comment_lines - 1] = new_line; u32_strncpy(new_line, comment, MAXCOMMENTLINE); return entry; } @@ -261,18 +261,18 @@ struct PoEntry* u32_add_trans_comment(struct PoEntry* entry, const uint32_t* trans_comment) { uint32_t** trans_comments = NULL; - uint32_t* new_line = NULL; + uint32_t* new_line = NULL; if (!entry) return NULL; entry->trans_comment_lines++; if (!entry->trans_comments) - trans_comments = calloc(entry->trans_comment_lines, - sizeof(uint32_t*)); + trans_comments + = calloc(entry->trans_comment_lines, sizeof(uint32_t*)); else trans_comments = realloc(entry->trans_comments, - entry->trans_comment_lines * sizeof(uint32_t*)); + entry->trans_comment_lines * sizeof(uint32_t*)); if (!trans_comments) return NULL; entry->trans_comments = trans_comments; @@ -280,7 +280,7 @@ u32_add_trans_comment(struct PoEntry* entry, const uint32_t* trans_comment) new_line = calloc(MAXCOMMENTLINE, sizeof(uint32_t)); if (!new_line) return NULL; - entry->trans_comments[entry->trans_comment_lines-1] = new_line; + entry->trans_comments[entry->trans_comment_lines - 1] = new_line; u32_strncpy(new_line, trans_comment, MAXCOMMENTLINE); return entry; } @@ -288,8 +288,8 @@ u32_add_trans_comment(struct PoEntry* entry, const uint32_t* trans_comment) struct PoEntry* add_ref_comment(struct PoEntry* entry, const char* comment) { - char** ref_comments = NULL; - char* new_line = NULL; + char** ref_comments = NULL; + char* new_line = NULL; if (!entry) return NULL; @@ -300,34 +300,34 @@ add_ref_comment(struct PoEntry* entry, const char* comment) ref_comments = calloc(entry->ref_comment_lines, sizeof(char*)); else ref_comments = realloc(entry->ref_comments, - entry->ref_comment_lines * sizeof(char*)); + entry->ref_comment_lines * sizeof(char*)); if (!ref_comments) return NULL; entry->ref_comments = ref_comments; - new_line = calloc(MAXCOMMENTLINE, 1); + new_line = calloc(MAXCOMMENTLINE + 1, 1); if (!new_line) return NULL; - entry->ref_comments[entry->ref_comment_lines-1] = new_line; + entry->ref_comments[entry->ref_comment_lines - 1] = new_line; strncpy(new_line, comment, MAXCOMMENTLINE); return entry; } -int +int load_file(struct DrawState* dstate, long* lineno, long* col) { struct PoEntry* newchunk = calloc(ALLOC_DELTA, sizeof(struct PoEntry)); - struct PoEntry* entry = NULL; - ParseState pstate = PS_NONE; + struct PoEntry* entry = NULL; + ParseState pstate = PS_NONE; dstate->msgid_size = 0; if (!newchunk) return LOAD_ERR_CANT_ALLOC; dstate->msgid_count = 0; - dstate->msgid_size = ALLOC_DELTA; - dstate->entries = newchunk; - entry = dstate->entries; + dstate->msgid_size = ALLOC_DELTA; + dstate->entries = newchunk; + entry = dstate->entries; dstate->untranslated_count++; FILE* input = NULL; @@ -335,8 +335,8 @@ load_file(struct DrawState* dstate, long* lineno, long* col) return LOAD_ERR_CANT_OPEN_FILE; char input_line[MAXBUFLINE]; /* lines to tolerate the lack of msgid */ - int msgid_counter = PO_DETECTION_LINES; - int msgstr_index = -1; + int msgid_counter = PO_DETECTION_LINES; + int msgstr_index = -1; while (!feof(input)) { if (!fgets(input_line, MAXBUFLINE, input)) @@ -347,8 +347,8 @@ load_file(struct DrawState* dstate, long* lineno, long* col) *eol = 0; int result; - result = parse_po_line(input_line, dstate, &entry, &pstate, - col, &msgstr_index); + result = parse_po_line(input_line, dstate, &entry, &pstate, col, + &msgstr_index); if (result == PARSE_ERR_NONE) { if (pstate & PS_MSGID) @@ -361,7 +361,7 @@ load_file(struct DrawState* dstate, long* lineno, long* col) } else if (result == PARSE_ERR_SYNTAX) sprintf(dstate->error, "%s:%ld:%ld: Syntax error", - dstate->filename, *lineno, *col); + dstate->filename, *lineno, *col); else { if (dstate->entries) @@ -397,262 +397,293 @@ load_file(struct DrawState* dstate, long* lineno, long* col) return LOAD_ERR_NONE; } -int -save_file(const struct PoEntry* entries, const size_t real_msgid_count, - const char* filename, const int nplurals) +int +save_file(const struct PoEntry* entries, const size_t real_msgid_count, + const char* filename, const int nplurals) { const struct PoEntry* current = entries; - size_t len = strlen(filename); - FILE* output = NULL; - char* backup_filename = calloc(len+2, 1); + size_t len = strlen(filename); + FILE* output = NULL; + char* backup_filename = calloc(len + 2, 1); char tmp_filename[MAXPATH]; strcpy(tmp_filename, filename); dirname(tmp_filename); - strncat(tmp_filename, "/poe.tmp", MAXPATH-1); + strncat(tmp_filename, "/poe.tmp", MAXPATH - 1); if (!(output = fopen(tmp_filename, "wt"))) return SAVE_ERR_CANT_OPEN_FILE; - strncpy(backup_filename, filename, len+1); - backup_filename[len] = '~'; - backup_filename[len+1] = 0; - - - while (current < entries + real_msgid_count) + strncpy(backup_filename, filename, len + 1); + backup_filename[len] = '~'; + backup_filename[len + 1] = 0; + +save_loop_entries_start: + if (current >= entries + real_msgid_count) + goto save_loop_entries_done; + if (current->comment_lines > 0) { - if (current->comment_lines > 0) + char u8_comment[MAXCOMMENTLINE * 6]; + for (size_t c = 0; c < current->comment_lines; c++) { - char u8_comment[MAXCOMMENTLINE*6]; - for (size_t c = 0; c < current->comment_lines; c++) - { - unicode_string_to_u8(u8_comment, - *(current->comments + c), - MAXCOMMENTLINE * 6); - fprintf(output, "#%s%s\n", - current->obsolete ? "~" : "", - u8_comment); - } + unicode_string_to_u8(u8_comment, + *(current->comments + c), MAXCOMMENTLINE * 6); + fprintf(output, "#%s%s\n", current->obsolete ? "~" : "", + u8_comment); } - if (current->trans_comment_lines > 0) + } + if (current->trans_comment_lines > 0) + { + char u8_trans_comment[MAXCOMMENTLINE * 6]; + for (size_t tc = 0; tc < current->trans_comment_lines; tc++) { - char u8_trans_comment[MAXCOMMENTLINE*6]; - for (size_t tc = 0; tc < current->trans_comment_lines; - tc++) - { - unicode_string_to_u8(u8_trans_comment, - *(current->trans_comments + tc), - MAXCOMMENTLINE * 6); - fprintf(output, "#.%s%s\n", - u8_trans_comment[0] == 0 ? "" : - " ", - u8_trans_comment); - } + unicode_string_to_u8(u8_trans_comment, + *(current->trans_comments + tc), + MAXCOMMENTLINE * 6); + fprintf(output, "#.%s%s\n", + u8_trans_comment[0] == 0 ? "" : " ", + u8_trans_comment); } - if (current->ref_comment_lines > 0) + } + if (current->ref_comment_lines > 0) + { + for (size_t rc = 0; rc < current->ref_comment_lines; rc++) + fprintf(output, "#: %s\n", + *(current->ref_comments + rc)); + } + Flags flags = current->flags; + if (current->flags != FL_NONE) + fprintf(output, "#, "); + while (flags != FL_NONE) + { + if (flags & FL_C_FORMAT) { - for (size_t rc = 0; rc < current->ref_comment_lines; - rc++) - fprintf(output, "#: %s\n", - *(current->ref_comments + rc)); + flags &= ~FL_C_FORMAT; + fprintf(output, "%s", flag_strings[FL_C_FORMAT]); } - Flags flags = current->flags; - if (current->flags != FL_NONE) - fprintf(output, "#, "); - while (flags != FL_NONE) + else if (flags & FL_NO_C_FORMAT) { - if (flags & FL_C_FORMAT) - { - flags &= ~FL_C_FORMAT; - fprintf(output, "%s", - flag_strings[FL_C_FORMAT]); - } - else if (flags & FL_NO_C_FORMAT) + flags &= ~FL_NO_C_FORMAT; + fprintf(output, "%s", flag_strings[FL_NO_C_FORMAT]); + } + else if (flags & FL_FUZZY) + { + flags &= ~FL_FUZZY; + fprintf(output, "%s", flag_strings[FL_FUZZY]); + } + else + flags = FL_NONE; /* see po.h */ + + if (flags != FL_NONE) + fprintf(output, ", "); + } + if (current->flags != FL_NONE) + fprintf(output, "\n"); + + if (current->obsolete) + { + fprintf(output, "\n"); + current++; + goto save_loop_entries_start; + } + + if (current->msgid) + { + char msgid[MAXMSG]; + unicode_string_to_u8(msgid, current->msgid, MAXMSG); + size_t actual_lines = 0; + size_t num_lines + = u32_lines_in_string(current->msgid, &actual_lines); + if (actual_lines > 1) + { + struct BufferLine* msgid_split = split(current->msgid); + struct BufferLine* pmsgid_split = NULL; + + if (!msgid_split) + return SAVE_ERR_CANT_ALLOC; + + fprintf(output, "msgid \"\"\n"); + for (size_t i = 0; i < num_lines; i++) { - flags &= ~FL_NO_C_FORMAT; - fprintf(output, "%s", - flag_strings[FL_NO_C_FORMAT]); + pmsgid_split = msgid_split + i; + int last = i == num_lines - 1; + char u8_msgid[MAXMSG * 6]; + size_t len = u32_strlen(pmsgid_split->text); + unicode_string_to_u8(u8_msgid, + pmsgid_split->text, MAXMSG * 6); + if (!(last && len == 0)) + fprintf(output, "\"%s%s\"\n", u8_msgid, + last ? "" : "\\n"); } - else if (flags & FL_FUZZY) + for (size_t i = 0; i < num_lines; i++) { - flags &= ~FL_FUZZY; - fprintf(output, "%s", - flag_strings[FL_FUZZY]); + pmsgid_split = msgid_split + i; + free_bufferline(pmsgid_split); } - else - flags = FL_NONE; /* see po.h */ - - if (flags != FL_NONE) - fprintf(output, ", "); + free(msgid_split); } - if (current->flags != FL_NONE) - fprintf(output, "\n"); - - if (current->obsolete) + else { - fprintf(output, "\n"); - current++; - continue; + char u8_msgid[MAXMSG * 6]; + unicode_string_to_u8(u8_msgid, current->msgid, + MAXMSG * 6); + fprintf(output, "msgid \"%s\"\n", u8_msgid); } + } + else + fprintf(output, "msgid \"\"\n"); - if (current->msgid) + if (current->msgid_plural) + { + char msgid_plural[MAXMSG]; + unicode_string_to_u8(msgid_plural, current->msgid_plural, + MAXMSG); + size_t actual_lines = 0; + size_t num_lines = u32_lines_in_string(current->msgid_plural, + &actual_lines); + if (actual_lines > 1) { - char msgid[MAXMSG]; - unicode_string_to_u8(msgid, current->msgid, MAXMSG); - size_t actual_lines = 0; - size_t num_lines = u32_lines_in_string(current->msgid, - &actual_lines); - if (actual_lines > 1) - { - struct BufferLine* msgid_split = split( - current->msgid); - struct BufferLine* pmsgid_split = NULL; + struct BufferLine* msgid_split + = split(current->msgid_plural); + struct BufferLine* pmsgid_split = NULL; - if (!msgid_split) - return SAVE_ERR_CANT_ALLOC; + if (!msgid_split) + return SAVE_ERR_CANT_ALLOC; - fprintf(output, "msgid \"\"\n"); - for (size_t i = 0; i < num_lines; i++) - { - pmsgid_split = msgid_split + i; - int last = i == num_lines - 1; - char u8_msgid[MAXMSG*6]; - size_t len = u32_strlen( - pmsgid_split->text); - unicode_string_to_u8(u8_msgid, - pmsgid_split->text, MAXMSG * 6); - if (!(last && len == 0)) - fprintf(output, "\"%s%s\"\n", - u8_msgid, last ? "" : - "\\n"); - } - for (size_t i = 0; i < num_lines; i++) - { - pmsgid_split = msgid_split + i; - free_bufferline(pmsgid_split); - } - free(msgid_split); + fprintf(output, "msgid_plural \"\"\n"); + for (size_t i = 0; i < num_lines; i++) + { + pmsgid_split = msgid_split + i; + int last = i == num_lines - 1; + char u8_msgid[MAXMSG * 6]; + size_t len = u32_strlen(pmsgid_split->text); + unicode_string_to_u8(u8_msgid, + pmsgid_split->text, MAXMSG * 6); + if (!(last && len == 0)) + fprintf(output, "\"%s%s\"\n", u8_msgid, + last ? "" : "\\n"); } - else + for (size_t i = 0; i < num_lines; i++) { - char u8_msgid[MAXMSG*6]; - unicode_string_to_u8(u8_msgid, current->msgid, - MAXMSG * 6); - fprintf(output, "msgid \"%s\"\n", u8_msgid); + pmsgid_split = msgid_split + i; + free_bufferline(pmsgid_split); } + free(msgid_split); } else - fprintf(output, "msgid \"\"\n"); + { + char u8_msgid[MAXMSG * 6]; + unicode_string_to_u8(u8_msgid, current->msgid_plural, + MAXMSG * 6); + fprintf(output, "msgid_plural \"%s\"\n", u8_msgid); + } + } - if (current->msgid_plural) + if (current->msgstr_count == 1) + { + size_t actual_lines = 0; + size_t num_lines + = u32_lines_in_string(*current->msgstr, &actual_lines); + if (actual_lines > 1) { - char msgid_plural[MAXMSG]; - unicode_string_to_u8(msgid_plural, - current->msgid_plural, MAXMSG); - size_t actual_lines = 0; - size_t num_lines = u32_lines_in_string( - current->msgid_plural, &actual_lines); - if (actual_lines > 1) + int first_entry = current == entries && !current->msgid; + int seen_generator = 0; + char now[MAXDATEBUF]; + time_t t = time(NULL); + struct tm* now_tm = localtime(&t); + strftime(now, MAXDATEBUF, "%F %H:%M%z", now_tm); + + struct BufferLine* msgstr_split + = split(*current->msgstr); + struct BufferLine* pmsgstr_split = NULL; + + if (!msgstr_split) + return SAVE_ERR_CANT_ALLOC; + + fprintf(output, "msgstr \"\"\n"); + for (size_t i = 0; i < num_lines; i++) { - struct BufferLine* msgid_split = split( - current->msgid_plural); - struct BufferLine* pmsgid_split = NULL; - - if (!msgid_split) - return SAVE_ERR_CANT_ALLOC; - - fprintf(output, "msgid_plural \"\"\n"); - for (size_t i = 0; i < num_lines; i++) + pmsgstr_split = msgstr_split + i; + int last = i == num_lines - 1; + char u8_msgstr[MAXMSG * 6]; + size_t len = u32_strlen(pmsgstr_split->text); + unicode_string_to_u8(u8_msgstr, + pmsgstr_split->text, MAXMSG * 6); + + if (first_entry + && starts_with(u8_msgstr, + "PO-Revision-Date: ")) + fprintf(output, + "\"PO-Revision-Date:" + " %s\\n\"\n", + now); + else if (first_entry + && starts_with(u8_msgstr, + "X-Generator: ")) { - pmsgid_split = msgid_split + i; - int last = i == num_lines - 1; - char u8_msgid[MAXMSG*6]; - size_t len = u32_strlen( - pmsgid_split->text); - unicode_string_to_u8(u8_msgid, - pmsgid_split->text, MAXMSG * 6); - if (!(last && len == 0)) - fprintf(output, "\"%s%s\"\n", - u8_msgid, last ? "" : - "\\n"); - } - for (size_t i = 0; i < num_lines; i++) - { - pmsgid_split = msgid_split + i; - free_bufferline(pmsgid_split); + seen_generator = 1; + fprintf(output, + "\"X-Generator:" + " poe %s" + "\\n\"\n", + VERSION); } - free(msgid_split); + else if (!(last && len == 0)) + fprintf(output, "\"%s%s\"\n", u8_msgstr, + last ? "" : "\\n"); } - else + if (first_entry && !seen_generator) + fprintf(output, "\"X-Generator: poe %s\"\n", + VERSION); + for (size_t i = 0; i < num_lines; i++) { - char u8_msgid[MAXMSG*6]; - unicode_string_to_u8(u8_msgid, - current->msgid_plural, - MAXMSG * 6); - fprintf(output, "msgid_plural \"%s\"\n", - u8_msgid); + pmsgstr_split = msgstr_split + i; + free_bufferline(pmsgstr_split); } + free(msgstr_split); } - - if (current->msgstr_count == 1) + else if (current->msgid_plural + && (!*current->msgstr || !**current->msgstr)) + for (int i = 0; i < nplurals; i++) + fprintf(output, "msgstr[%d] \"\"\n", i); + else + { + char u8_msgstr[MAXMSG * 6]; + unicode_string_to_u8(u8_msgstr, *current->msgstr, + MAXMSG * 6); + fprintf(output, "msgstr \"%s\"\n", u8_msgstr); + } + } + else if (current->msgstr_count > 0) + { + for (size_t m = 0; m < current->msgstr_count; m++) { size_t actual_lines = 0; - size_t num_lines = u32_lines_in_string( - *current->msgstr, &actual_lines); + size_t num_lines = u32_lines_in_string(*current->msgstr, + &actual_lines); if (actual_lines > 1) { - int first_entry = current == entries && - !current->msgid; - int seen_generator = 0; - char now[MAXDATEBUF]; - time_t t = time(NULL); - struct tm* now_tm = localtime(&t); - strftime(now, MAXDATEBUF, "%F %H:%M%z", - now_tm); - - struct BufferLine* msgstr_split = - split(*current->msgstr); + struct BufferLine* msgstr_split + = split(*(current->msgstr + m)); struct BufferLine* pmsgstr_split = NULL; if (!msgstr_split) return SAVE_ERR_CANT_ALLOC; - fprintf(output, "msgstr \"\"\n"); + fprintf(output, "msgstr[%ld] \"\"\n", m); for (size_t i = 0; i < num_lines; i++) { pmsgstr_split = msgstr_split + i; - int last = i == num_lines - 1; - char u8_msgstr[MAXMSG*6]; + int last = i == num_lines - 1; + char u8_msgstr[MAXMSG * 6]; size_t len = u32_strlen( pmsgstr_split->text); - unicode_string_to_u8(u8_msgstr, - pmsgstr_split->text, + unicode_string_to_u8(u8_msgstr, + pmsgstr_split->text, MAXMSG * 6); - - if (first_entry && starts_with( - u8_msgstr, - "PO-Revision-Date: ")) - fprintf(output, - "\"PO-Revision-Date:" - " %s\\n\"\n", now); - else if (first_entry && starts_with( - u8_msgstr, - "X-Generator: ")) - { - seen_generator = 1; - fprintf(output, - "\"X-Generator:" - " poe %s" - "\\n\"\n", - VERSION); - } - else if (!(last && len == 0)) - fprintf(output, "\"%s%s\"\n", + if (!(last && len == 0)) + fprintf(output, "\"%s%s\"\n", u8_msgstr, last ? "" : "\\n"); } - if (first_entry && !seen_generator) - fprintf(output, - "\"X-Generator: poe %s\"\n", - VERSION); for (size_t i = 0; i < num_lines; i++) { pmsgstr_split = msgstr_split + i; @@ -660,87 +691,29 @@ save_file(const struct PoEntry* entries, const size_t real_msgid_count, } free(msgstr_split); } - else if (current->msgid_plural && - (!*current->msgstr || - !**current->msgstr)) - for (int i = 0; i < nplurals; i++) - fprintf(output, "msgstr[%d] \"\"\n", i); else { - char u8_msgstr[MAXMSG*6]; - unicode_string_to_u8(u8_msgstr, - *current->msgstr, MAXMSG * 6); - fprintf(output, "msgstr \"%s\"\n", u8_msgstr); - } - } - else if (current->msgstr_count > 0) - { - for (size_t m = 0; m < current->msgstr_count; m++) - { - size_t actual_lines = 0; - size_t num_lines = u32_lines_in_string( - *current->msgstr, - &actual_lines); - if (actual_lines > 1) - { - struct BufferLine* msgstr_split = - split(*(current->msgstr + m)); - struct BufferLine* pmsgstr_split = NULL; - - if (!msgstr_split) - return SAVE_ERR_CANT_ALLOC; - - fprintf(output, "msgstr[%ld] \"\"\n", - m); - for (size_t i = 0; i < num_lines; i++) - { - pmsgstr_split = - msgstr_split + i; - int last = i == num_lines - 1; - char u8_msgstr[MAXMSG*6]; - size_t len = u32_strlen( - pmsgstr_split->text); - unicode_string_to_u8(u8_msgstr, - pmsgstr_split->text, - MAXMSG * 6); - if (!(last && len == 0)) - fprintf(output, - "\"%s%s\"\n", - u8_msgstr, - last ? "" : - "\\n"); - } - for (size_t i = 0; i < num_lines; i++) - { - pmsgstr_split = - msgstr_split + i; - free_bufferline(pmsgstr_split); - } - free(msgstr_split); - } - else - { - char u8_msgstr[MAXMSG*6]; - unicode_string_to_u8(u8_msgstr, - *(current->msgstr + m), - MAXMSG * 6); - fprintf(output, "msgstr[%ld] \"%s\"\n", - m, u8_msgstr); - } + char u8_msgstr[MAXMSG * 6]; + unicode_string_to_u8(u8_msgstr, + *(current->msgstr + m), MAXMSG * 6); + fprintf(output, "msgstr[%ld] \"%s\"\n", m, + u8_msgstr); } } + } + else + { + if (current->msgid_plural) + for (int i = 0; i < nplurals; i++) + fprintf(output, "msgstr[%d] \"\"\n", i); else - { - if (current->msgid_plural) - for (int i = 0; i < nplurals; i++) - fprintf(output, "msgstr[%d] \"\"\n", i); - else - fprintf(output, "msgstr \"\"\n"); - } - - fprintf(output, "\n"); - current++; + fprintf(output, "msgstr \"\"\n"); } + + fprintf(output, "\n"); + current++; + goto save_loop_entries_start; +save_loop_entries_done: fclose(output); if (rename(filename, backup_filename) == -1) @@ -753,15 +726,15 @@ save_file(const struct PoEntry* entries, const size_t real_msgid_count, } int -parse_po_line(const char* line, struct DrawState* dstate, - struct PoEntry** current_entry, ParseState* pstate, long* col, - int* msgstr_index) +parse_po_line(const char* line, struct DrawState* dstate, + struct PoEntry** current_entry, ParseState* pstate, long* col, + int* msgstr_index) { - size_t i = 0; - *col = 1; + size_t i = 0; + *col = 1; const char* pline = line; char token[MAXBUFLINE]; - *token = 0; + *token = 0; char* ptoken = token; if (!line) @@ -806,7 +779,7 @@ parse_po_line(const char* line, struct DrawState* dstate, pline++; (*col)++; } - + if (starts_with(pline, "] ")) { pline += 2; @@ -829,115 +802,115 @@ parse_po_line(const char* line, struct DrawState* dstate, { switch (*pline) { - case '#': - if (*col == 1) - { - *pstate |= PS_COMMENT_TRAN; - pline++; - (*col)++; - } - else - { - *ptoken++ = *pline++; - (*col)++; - } - break; - case '.': - if (*col == 2 && *pstate & PS_COMMENT_TRAN) - { - *pstate &= ~PS_COMMENT_TRAN; - *pstate |= PS_COMMENT_EXTR; - pline++; - (*col)++; - if (*pline == ' ') - { - pline++; - (*col)++; - } - else if (*pline) - return PARSE_ERR_SYNTAX; - } - else - { - *ptoken++ = *pline++; - (*col)++; - } - break; - case ',': - if (*col == 2 && *pstate & PS_COMMENT_TRAN) - { - *pstate &= ~PS_COMMENT_TRAN; - *pstate |= PS_COMMENT_FLAG; - pline++; - (*col)++; - if (*pline == ' ') - { - pline++; - (*col)++; - } - else if (*pline) - return PARSE_ERR_SYNTAX; - } - else - { - *ptoken++ = *pline++; - (*col)++; - } - break; - case ':': - if (*col == 2 && *pstate & PS_COMMENT_TRAN) + case '#': + if (*col == 1) + { + *pstate |= PS_COMMENT_TRAN; + pline++; + (*col)++; + } + else + { + *ptoken++ = *pline++; + (*col)++; + } + break; + case '.': + if (*col == 2 && *pstate & PS_COMMENT_TRAN) + { + *pstate &= ~PS_COMMENT_TRAN; + *pstate |= PS_COMMENT_EXTR; + pline++; + (*col)++; + if (*pline == ' ') { - *pstate &= ~PS_COMMENT_TRAN; - *pstate |= PS_COMMENT_REFN; pline++; (*col)++; - if (*pline == ' ') - { - pline++; - (*col)++; - } - else if (*pline) - return PARSE_ERR_SYNTAX; - } - else - { - *ptoken++ = *pline++; - (*col)++; } - break; - case '~': - if (*col == 2 && *pstate & PS_COMMENT_TRAN) + else if (*pline) + return PARSE_ERR_SYNTAX; + } + else + { + *ptoken++ = *pline++; + (*col)++; + } + break; + case ',': + if (*col == 2 && *pstate & PS_COMMENT_TRAN) + { + *pstate &= ~PS_COMMENT_TRAN; + *pstate |= PS_COMMENT_FLAG; + pline++; + (*col)++; + if (*pline == ' ') { - *pstate &= ~PS_COMMENT_TRAN; - *pstate |= PS_COMMENT_OBSO; pline++; (*col)++; } - else - { - *ptoken++ = *pline++; - (*col)++; - } - break; - case '"': - /* skip beginning and end quote */ - if ((ptoken == token || *(pline+1) == 0) - && !(*pstate & (PS_COMMENT_TRAN - | PS_COMMENT_EXTR - | PS_COMMENT_OBSO))) + else if (*pline) + return PARSE_ERR_SYNTAX; + } + else + { + *ptoken++ = *pline++; + (*col)++; + } + break; + case ':': + if (*col == 2 && *pstate & PS_COMMENT_TRAN) + { + *pstate &= ~PS_COMMENT_TRAN; + *pstate |= PS_COMMENT_REFN; + pline++; + (*col)++; + if (*pline == ' ') { pline++; (*col)++; } - else - { - *ptoken++ = *pline++; - (*col)++; - } - break; - default: + else if (*pline) + return PARSE_ERR_SYNTAX; + } + else + { + *ptoken++ = *pline++; + (*col)++; + } + break; + case '~': + if (*col == 2 && *pstate & PS_COMMENT_TRAN) + { + *pstate &= ~PS_COMMENT_TRAN; + *pstate |= PS_COMMENT_OBSO; + pline++; + (*col)++; + } + else + { + *ptoken++ = *pline++; + (*col)++; + } + break; + case '"': + /* skip beginning and end quote */ + if ((ptoken == token || *(pline + 1) == 0) + && !(*pstate + & (PS_COMMENT_TRAN | PS_COMMENT_EXTR + | PS_COMMENT_OBSO))) + { + pline++; + (*col)++; + } + else + { *ptoken++ = *pline++; (*col)++; + } + break; + default: + *ptoken++ = *pline++; + (*col)++; } } if (*pstate == PS_NONE) @@ -947,17 +920,17 @@ parse_po_line(const char* line, struct DrawState* dstate, if (!(*current_entry)->obsolete) dstate->msgid_count++; - (*current_entry)++; - if (*current_entry == dstate->entries + dstate->msgid_size) - { - dstate->msgid_size += ALLOC_DELTA; - newchunk = realloc(dstate->entries, dstate->msgid_size * - sizeof(struct PoEntry)); - if (!newchunk) - return PARSE_ERR_ALLOC; - dstate->entries = newchunk; - *current_entry = dstate->entries + dstate->msgid_size - - ALLOC_DELTA; + (*current_entry)++; + if (*current_entry == dstate->entries + dstate->msgid_size) + { + dstate->msgid_size += ALLOC_DELTA; + newchunk = realloc(dstate->entries, + dstate->msgid_size * sizeof(struct PoEntry)); + if (!newchunk) + return PARSE_ERR_ALLOC; + dstate->entries = newchunk; + *current_entry = dstate->entries + dstate->msgid_size + - ALLOC_DELTA; } init_po_entry(*current_entry); *msgstr_index = -1; @@ -977,16 +950,17 @@ parse_po_line(const char* line, struct DrawState* dstate, { if (starts_with(token, "Plural-Forms: ")) { - set_plural_forms(*current_entry, token + - strlen("Plural-Forms: "), 0); + set_plural_forms(*current_entry, + token + strlen("Plural-Forms: "), + 0); set_nplurals(dstate, *current_entry); } } set_msgstr(*current_entry, token, 0, 1, dstate); } else if (*pstate & PS_MSGSTR_ARRAY) - set_msgstr(*current_entry, token, *msgstr_index, 1, - dstate); + set_msgstr(*current_entry, token, *msgstr_index, 1, + dstate); else if (*pstate & PS_COMMENT_TRAN) { uint32_t u32_token[MAXCOMMENTLINE]; @@ -1005,28 +979,28 @@ parse_po_line(const char* line, struct DrawState* dstate, while (*ptoken) { int first = *current_entry == dstate->entries; - if (starts_with(ptoken, flag_strings[ - FL_C_FORMAT])) + if (starts_with(ptoken, + flag_strings[FL_C_FORMAT])) { (*current_entry)->flags |= FL_C_FORMAT; - ptoken += strlen(flag_strings[ - FL_C_FORMAT]); + ptoken += strlen( + flag_strings[FL_C_FORMAT]); } - else if (starts_with(ptoken, flag_strings[ - FL_NO_C_FORMAT])) + else if (starts_with(ptoken, + flag_strings[FL_NO_C_FORMAT])) { - (*current_entry)->flags |= - FL_NO_C_FORMAT; - ptoken += strlen(flag_strings[ - FL_NO_C_FORMAT]); + (*current_entry)->flags + |= FL_NO_C_FORMAT; + ptoken += strlen( + flag_strings[FL_NO_C_FORMAT]); } - else if (starts_with(ptoken, flag_strings[ - FL_FUZZY]) && !first) + else if (starts_with(ptoken, + flag_strings[FL_FUZZY]) + && !first) { (*current_entry)->flags |= FL_FUZZY; dstate->fuzzy_count++; - ptoken += strlen(flag_strings[ - FL_FUZZY]); + ptoken += strlen(flag_strings[FL_FUZZY]); } else if (starts_with(ptoken, ", ")) ptoken += 2; @@ -1037,12 +1011,11 @@ parse_po_line(const char* line, struct DrawState* dstate, /*return PARSE_ERR_SYNTAX;*/ while (*ptoken && *ptoken != ' ' - && *ptoken != ',' - && *ptoken != '\t') + && *ptoken != ',' + && *ptoken != '\t') ptoken++; } } - } else if (*pstate & PS_COMMENT_REFN) add_ref_comment(*current_entry, token); @@ -1061,21 +1034,21 @@ parse_po_line(const char* line, struct DrawState* dstate, u32_add_trans_comment(*current_entry, (uint32_t*)L""); /* one-line states */ - *pstate &= ~(PS_COMMENT_TRAN | PS_COMMENT_EXTR | PS_COMMENT_FLAG | - PS_COMMENT_REFN | PS_COMMENT_OBSO); + *pstate &= ~(PS_COMMENT_TRAN | PS_COMMENT_EXTR | PS_COMMENT_FLAG + | PS_COMMENT_REFN | PS_COMMENT_OBSO); return PARSE_ERR_NONE; } -void +void update_statistics(const struct PoEntry* entries, struct DrawState* state, - struct PoEntry* entry) + struct PoEntry* entry) { const struct PoEntry* current = NULL; - int advance = 1; + int advance = 1; - state->fuzzy_count = 0; - state->obsolete_count = 0; + state->fuzzy_count = 0; + state->obsolete_count = 0; state->untranslated_count = 0; current = entries; @@ -1085,8 +1058,9 @@ update_statistics(const struct PoEntry* entries, struct DrawState* state, state->obsolete_count++; else { - if (!current->msgstr || !*current->msgstr || - !**current->msgstr) + if (current->msgid + && (!current->msgstr || !*current->msgstr + || !**current->msgstr)) state->untranslated_count++; if (current->flags & FL_FUZZY) state->fuzzy_count++; diff --git a/po.h b/po.h @@ -3,51 +3,51 @@ #include <stdint.h> #include <stdlib.h> -#define MSGSTR_MAX 4096 -#define MAXCOMMENTLINE 512 -#define ALLOC_DELTA 10 -#define PO_DETECTION_LINES 24 +#define MSGSTR_MAX 4096 +#define MAXCOMMENTLINE 512 +#define ALLOC_DELTA 10 +#define PO_DETECTION_LINES 24 #define LOAD_PARSE_ERR(e) (LOAD_ERR_PARSE_PREFIX + (e)) struct DrawState; enum { - PARSE_ERR_NONE = 0, - PARSE_ERR_INVAL = 1, - PARSE_ERR_ALLOC = 2, - PARSE_ERR_SYNTAX = 3 + PARSE_ERR_NONE = 0, + PARSE_ERR_INVAL = 1, + PARSE_ERR_ALLOC = 2, + PARSE_ERR_SYNTAX = 3 }; enum { - LOAD_ERR_NONE = 0, - LOAD_ERR_CANT_ALLOC = 1, + LOAD_ERR_NONE = 0, + LOAD_ERR_CANT_ALLOC = 1, LOAD_ERR_CANT_OPEN_FILE = 2, LOAD_ERR_NOT_PO_FILE = 3, - LOAD_ERR_PARSE_PREFIX = 100, - LOAD_ERR_P_INVAL = LOAD_PARSE_ERR(PARSE_ERR_INVAL), - LOAD_ERR_P_ALLOC = LOAD_PARSE_ERR(PARSE_ERR_ALLOC), - LOAD_ERR_P_SYNTAX = LOAD_PARSE_ERR(PARSE_ERR_SYNTAX) + LOAD_ERR_PARSE_PREFIX = 100, + LOAD_ERR_P_INVAL = LOAD_PARSE_ERR(PARSE_ERR_INVAL), + LOAD_ERR_P_ALLOC = LOAD_PARSE_ERR(PARSE_ERR_ALLOC), + LOAD_ERR_P_SYNTAX = LOAD_PARSE_ERR(PARSE_ERR_SYNTAX) }; enum { - SAVE_ERR_NONE = 0, - SAVE_ERR_CANT_ALLOC = 1, + SAVE_ERR_NONE = 0, + SAVE_ERR_CANT_ALLOC = 1, SAVE_ERR_CANT_OPEN_FILE = 2, SAVE_ERR_CANT_MOVE_FILE = 3 }; typedef enum { - PS_NONE = 0, - PS_COMMENT_TRAN = 1, /* "translator-comments" : # */ - PS_COMMENT_EXTR = 1 << 1, /* "extracted-comments" : #. */ - PS_COMMENT_FLAG = 1 << 2, /* "flag" : #, */ - PS_COMMENT_REFN = 1 << 3, /* "reference" : #: */ - PS_COMMENT_OBSO = 1 << 4, /* obsolete (undocumented) : #~ */ - PS_MSGID = 1 << 5, - PS_MSGID_PLURAL = 1 << 6, - PS_MSGSTR = 1 << 7, - PS_MSGSTR_ARRAY = 1 << 8 + PS_NONE = 0, + PS_COMMENT_TRAN = 1, /* "translator-comments" : # */ + PS_COMMENT_EXTR = 1 << 1, /* "extracted-comments" : #. */ + PS_COMMENT_FLAG = 1 << 2, /* "flag" : #, */ + PS_COMMENT_REFN = 1 << 3, /* "reference" : #: */ + PS_COMMENT_OBSO = 1 << 4, /* obsolete (undocumented) : #~ */ + PS_MSGID = 1 << 5, + PS_MSGID_PLURAL = 1 << 6, + PS_MSGSTR = 1 << 7, + PS_MSGSTR_ARRAY = 1 << 8 } ParseState; /* Complete list at: @@ -56,12 +56,12 @@ typedef enum { * * - JS, awk and sh using gettext, seriously? * "Let's just shove every programming and scripting language there is into - * flags!" /s - * Where are the flags for Pascal and Ada, by the way, hmm? [SR] + * flags!" /s + * Where are the flags for Pascal and Ada, by the way, hmm? [SR] * * - Including just the sane values, will strip the rest. */ typedef enum { - FL_NONE = 0, + FL_NONE = 0, FL_FUZZY = 1, FL_C_FORMAT = 1 << 1, FL_NO_C_FORMAT = 1 << 2 @@ -70,49 +70,48 @@ typedef enum { struct PoEntry { Flags flags; int obsolete; - uint32_t* msgid; - uint32_t* msgid_plural; - uint32_t* plural_forms; + uint32_t* msgid; + uint32_t* msgid_plural; + uint32_t* plural_forms; uint32_t** msgstr; - int msgstr_count; + int msgstr_count; uint32_t** comments; - size_t comment_lines; + size_t comment_lines; uint32_t** trans_comments; - size_t trans_comment_lines; - char** ref_comments; - size_t ref_comment_lines; + size_t trans_comment_lines; + char** ref_comments; + size_t ref_comment_lines; }; char* format_flags(char* buffer, const size_t max, const struct PoEntry* entry); void init_po_entry(struct PoEntry* entry); void free_po_entry(struct PoEntry* entry); struct PoEntry* set_flags(struct PoEntry* entry, const Flags flags); -struct PoEntry* set_msgid(struct PoEntry* entry, const char* msgid, - int append); -struct PoEntry* set_msgid_plural(struct PoEntry* entry, - const char* msgid_plural, int append); -struct PoEntry* set_plural_forms(struct PoEntry* entry, - const char* plural_forms, int append); +struct PoEntry* set_msgid(struct PoEntry* entry, const char* msgid, int append); +struct PoEntry* set_msgid_plural(struct PoEntry* entry, + const char* msgid_plural, int append); +struct PoEntry* set_plural_forms(struct PoEntry* entry, + const char* plural_forms, int append); struct PoEntry* set_msgstr(struct PoEntry* entry, const char* msgstr, - int msgstr_index, int append, struct DrawState* state); -struct PoEntry* u32_set_msgid(struct PoEntry* entry, const uint32_t* msgid, - int append); -struct PoEntry* u32_set_msgid_plural(struct PoEntry* entry, - const uint32_t* msgid_plural, int append); -struct PoEntry* u32_set_plural_forms(struct PoEntry* entry, - const uint32_t* plural_forms, int append); + int msgstr_index, int append, struct DrawState* state); +struct PoEntry* u32_set_msgid(struct PoEntry* entry, const uint32_t* msgid, + int append); +struct PoEntry* u32_set_msgid_plural(struct PoEntry* entry, + const uint32_t* msgid_plural, int append); +struct PoEntry* u32_set_plural_forms(struct PoEntry* entry, + const uint32_t* plural_forms, int append); struct PoEntry* u32_set_msgstr(struct PoEntry* entry, const uint32_t* msgstr, - int msgstr_index, int append, struct DrawState* state); + int msgstr_index, int append, struct DrawState* state); struct PoEntry* u32_add_comment(struct PoEntry* entry, const uint32_t* comment); -struct PoEntry* u32_add_trans_comment(struct PoEntry* entry, - const uint32_t* trans_comment); +struct PoEntry* u32_add_trans_comment(struct PoEntry* entry, + const uint32_t* trans_comment); struct PoEntry* add_ref_comment(struct PoEntry* entry, const char* comment); int load_file(struct DrawState* state, long* lineno, long* col); int save_file(const struct PoEntry* entries, const size_t real_msgid_count, - const char* filename, const int nplurals); -int parse_po_line(const char* line, struct DrawState* dstate, - struct PoEntry** current_entry, ParseState* pstate, long* col, - int* msgstr_index); + const char* filename, const int nplurals); +int parse_po_line(const char* line, struct DrawState* dstate, + struct PoEntry** current_entry, ParseState* pstate, long* col, + int* msgstr_index); void update_statistics(const struct PoEntry* entries, struct DrawState* state, - struct PoEntry* entry); + struct PoEntry* entry); diff --git a/poe.c b/poe.c @@ -24,10 +24,10 @@ extern const char* program_name; int print_error(const int code, const char* msg, ...); int version(); int usage(); -void format_filename(char* result, size_t result_size, const char* format, - const struct DrawState* state); +void format_filename(char* result, size_t result_size, const char* format, + const struct DrawState* state); void format_msgs(char* result, size_t result_size, const char* format, - const struct DrawState* state); + const struct DrawState* state); void quit_callback(struct DrawState* state, struct tb_event* ev); struct BufferLine* load_info(struct DrawState* state); struct BufferLine* load_msgstr(struct DrawState* state); @@ -83,8 +83,8 @@ void erase_forward(struct DrawState* state); void erase_to_start(struct DrawState* state); void erase_to_end(struct DrawState* state); struct DrawState* insert_line(struct DrawState* state); -struct DrawState* join_lines(struct DrawState* state, size_t first_line, - int forward); +struct DrawState* join_lines(struct DrawState* state, size_t first_line, + int forward); void handle_editbox_key_event(struct tb_event* ev, struct DrawState* state); void handle_key_event(struct tb_event* ev, struct DrawState* state); void handle_search_key_event(struct tb_event* ev, struct DrawState* state); @@ -112,33 +112,32 @@ int usage() { printf("Usage: %s [-h|--help] [-v|--version] [filename.po]\n", - program_name); + program_name); return 0; } void -format_filename(char* result, size_t result_size, const char* format, - const struct DrawState* state) +format_filename(char* result, size_t result_size, const char* format, + const struct DrawState* state) { if (!result) return; - snprintf(result, result_size, format, u8_basename(state->filename), - state->dirty ? "*" : ""); + snprintf(result, result_size, format, u8_basename(state->filename), + state->dirty ? "*" : ""); } void format_msgs(char* result, size_t result_size, const char* format, - const struct DrawState* state) + const struct DrawState* state) { if (!result) return; - snprintf(result, result_size, format, - state ? state->msgid_number : 0, - state ? state->msgid_count: 0, - state ? state->untranslated_count : 0, - state ? state->fuzzy_count : 0, - state ? state->obsolete_count : 0); + snprintf(result, result_size, format, state ? state->msgid_number : 0, + state ? state->msgid_count : 0, + state ? state->untranslated_count : 0, + state ? state->fuzzy_count : 0, + state ? state->obsolete_count : 0); } void @@ -148,39 +147,37 @@ quit_callback(struct DrawState* state, struct tb_event* ev) { if (ev->ch == 'y') { - int result = save_file(state->entries, - state->real_msgid_count, - state->filename, state->nplurals); + int result = save_file(state->entries, + state->real_msgid_count, state->filename, + state->nplurals); switch (result) { - case SAVE_ERR_CANT_ALLOC: - strcpy(state->error, - errors[ERR_CANT_ALLOC]); - break; - case SAVE_ERR_CANT_OPEN_FILE: - strcpy(state->error, - errors[ERR_CANT_SAVE]); - break; - default: - state->error[0] = 0; + case SAVE_ERR_CANT_ALLOC: + strcpy(state->error, errors[ERR_CANT_ALLOC]); + break; + case SAVE_ERR_CANT_OPEN_FILE: + strcpy(state->error, errors[ERR_CANT_SAVE]); + break; + default: + state->error[0] = 0; } } - state->dirty = 0; + state->dirty = 0; state->prompt_callback = NULL; - state->running = 0; + state->running = 0; } } struct BufferLine* load_info(struct DrawState* state) { - struct PoEntry* first = &state->entries[0]; - struct PoEntry* current = &state->entries[state->msgid_number-1]; - size_t msgid_num_lines = u32_lines_in_string(current->msgid, NULL); - size_t msgid_plural_num_lines = u32_lines_in_string( - current->msgid_plural, NULL); - size_t num_lines = 0; - struct BufferLine* newbuf = NULL; + struct PoEntry* first = &state->entries[0]; + struct PoEntry* current = &state->entries[state->msgid_number - 1]; + size_t msgid_num_lines = u32_lines_in_string(current->msgid, NULL); + size_t msgid_plural_num_lines + = u32_lines_in_string(current->msgid_plural, NULL); + size_t num_lines = 0; + struct BufferLine* newbuf = NULL; struct BufferLine* pnewbuf = NULL; state->info_rows_maxlen = 0; @@ -216,14 +213,13 @@ load_info(struct DrawState* state) state->info_rows_count = num_lines; - pnewbuf = newbuf; - size_t newlen = 0; + pnewbuf = newbuf; + size_t newlen = 0; uint32_t* pumsgid = NULL; if (current->msgid) { pumsgid = current->msgid; - newlen = u32_u8_strncpy(pnewbuf->text, "msgid:", - MAXBUFLINE); + newlen = u32_u8_strncpy(pnewbuf->text, "msgid:", MAXBUFLINE); pnewbuf->length = newlen; if (newlen > state->info_rows_maxlen) state->info_rows_maxlen = newlen; @@ -233,21 +229,21 @@ load_info(struct DrawState* state) for (size_t i = 0; i < msgid_num_lines; i++) { size_t len = u32_line_length(pumsgid); - newlen = 0; - u32_strncpy(pnewbuf->text, pumsgid, MIN(len+1, - MAXBUFLINE)); + newlen = 0; + u32_strncpy(pnewbuf->text, pumsgid, + MIN(len + 1, MAXBUFLINE)); newlen = u32_decode_tabs(pnewbuf->text, MAXBUFLINE); pnewbuf->length = newlen; if (newlen > state->info_rows_maxlen) state->info_rows_maxlen = newlen; - pumsgid += len+(pumsgid[len] ? 2 : 0); + pumsgid += len + (pumsgid[len] ? 2 : 0); pnewbuf++; } } if (current->msgid_plural) { - newlen = u32_u8_strncpy(pnewbuf->text, "msgid_plural:", - MAXBUFLINE); + newlen = u32_u8_strncpy(pnewbuf->text, + "msgid_plural:", MAXBUFLINE); pnewbuf->length = newlen; if (newlen > state->info_rows_maxlen) state->info_rows_maxlen = newlen; @@ -258,28 +254,28 @@ load_info(struct DrawState* state) for (size_t i = 0; i < msgid_plural_num_lines; i++) { size_t len = u32_line_length(pumsgid); - newlen = 0; - u32_strncpy(pnewbuf->text, pumsgid, MIN(len+1, - MAXBUFLINE)); + newlen = 0; + u32_strncpy(pnewbuf->text, pumsgid, + MIN(len + 1, MAXBUFLINE)); newlen = u32_decode_tabs(pnewbuf->text, MAXBUFLINE); pnewbuf->length = newlen; if (newlen > state->info_rows_maxlen) state->info_rows_maxlen = newlen; - pumsgid += len+(pumsgid[len] ? 2 : 0); + pumsgid += len + (pumsgid[len] ? 2 : 0); pnewbuf++; } if (first->plural_forms) { - newlen = u32_u8_strncpy(pnewbuf->text, "Plural forms:", - MAXBUFLINE); + newlen = u32_u8_strncpy(pnewbuf->text, + "Plural forms:", MAXBUFLINE); pnewbuf->length = newlen; if (newlen > state->info_rows_maxlen) state->info_rows_maxlen = newlen; pnewbuf->fg = EDIT_MSGID_HEADING_FG; pnewbuf->bg = EDIT_MSGID_HEADING_BG; pnewbuf++; - newlen = u32_strncpy(pnewbuf->text, first->plural_forms, - MAXBUFLINE); + newlen = u32_strncpy(pnewbuf->text, first->plural_forms, + MAXBUFLINE); pnewbuf->length = newlen; if (newlen > state->info_rows_maxlen) state->info_rows_maxlen = newlen; @@ -288,8 +284,7 @@ load_info(struct DrawState* state) } if (current->comments) { - newlen = u32_u8_strncpy(pnewbuf->text, "Comments:", - MAXBUFLINE); + newlen = u32_u8_strncpy(pnewbuf->text, "Comments:", MAXBUFLINE); pnewbuf->length = newlen; if (newlen > state->info_rows_maxlen) state->info_rows_maxlen = newlen; @@ -299,10 +294,10 @@ load_info(struct DrawState* state) for (size_t i = 0; i < current->comment_lines; i++) { uint32_t* comment = current->comments[i]; - size_t len = u32_strlen(comment); - newlen = u32_strncpy(pnewbuf->text, comment, - MIN(len+1, MAXBUFLINE)); - pnewbuf->length = newlen; + size_t len = u32_strlen(comment); + newlen = u32_strncpy(pnewbuf->text, comment, + MIN(len + 1, MAXBUFLINE)); + pnewbuf->length = newlen; if (newlen > state->info_rows_maxlen) state->info_rows_maxlen = newlen; pnewbuf++; @@ -310,8 +305,8 @@ load_info(struct DrawState* state) } if (current->trans_comments) { - newlen = u32_u8_strncpy(pnewbuf->text, "Translation comments:", - MAXBUFLINE); + newlen = u32_u8_strncpy(pnewbuf->text, + "Translation comments:", MAXBUFLINE); pnewbuf->length = newlen; if (newlen > state->info_rows_maxlen) state->info_rows_maxlen = newlen; @@ -321,9 +316,9 @@ load_info(struct DrawState* state) for (size_t i = 0; i < current->trans_comment_lines; i++) { uint32_t* trans_comment = current->trans_comments[i]; - size_t len = u32_strlen(trans_comment); - newlen = u32_strncpy(pnewbuf->text, trans_comment, - MIN(len+1, MAXBUFLINE)); + size_t len = u32_strlen(trans_comment); + newlen = u32_strncpy(pnewbuf->text, trans_comment, + MIN(len + 1, MAXBUFLINE)); pnewbuf->length = newlen; if (newlen > state->info_rows_maxlen) state->info_rows_maxlen = newlen; @@ -332,8 +327,8 @@ load_info(struct DrawState* state) } if (current->ref_comments) { - newlen = u32_u8_strncpy(pnewbuf->text, "References:", - MAXBUFLINE); + newlen = u32_u8_strncpy(pnewbuf->text, + "References:", MAXBUFLINE); pnewbuf->length = newlen; if (newlen > state->info_rows_maxlen) state->info_rows_maxlen = newlen; @@ -343,9 +338,9 @@ load_info(struct DrawState* state) for (size_t i = 0; i < current->ref_comment_lines; i++) { char* ref_comment = current->ref_comments[i]; - size_t len = strlen(ref_comment); - newlen = u32_u8_strncpy(pnewbuf->text, ref_comment, - MIN(len+1, MAXBUFLINE)); + size_t len = strlen(ref_comment); + newlen = u32_u8_strncpy(pnewbuf->text, ref_comment, + MIN(len + 1, MAXBUFLINE)); pnewbuf->length = newlen; if (newlen > state->info_rows_maxlen) state->info_rows_maxlen = newlen; @@ -361,12 +356,12 @@ load_info(struct DrawState* state) struct BufferLine* load_msgstr(struct DrawState* state) { - int msgstr_index = state->msgstr_index; - struct PoEntry* current = &state->entries[state->msgid_number-1]; - size_t num_lines = current->msgstr_count == 0 ? 1 - : u32_lines_in_string(*(current->msgstr+msgstr_index), - NULL); - struct BufferLine* newbuf = NULL; + int msgstr_index = state->msgstr_index; + struct PoEntry* current = &state->entries[state->msgid_number - 1]; + size_t num_lines = current->msgstr_count == 0 + ? 1 + : u32_lines_in_string(*(current->msgstr + msgstr_index), NULL); + struct BufferLine* newbuf = NULL; struct BufferLine* pnewbuf = NULL; newbuf = calloc(num_lines, sizeof(struct BufferLine)); @@ -394,13 +389,14 @@ load_msgstr(struct DrawState* state) pnewbuf = newbuf; for (size_t i = 0; i < num_lines; i++) { - size_t len = u32_line_length(pumsgstr); + size_t len = u32_line_length(pumsgstr); size_t newlen = 0; - u32_strncpy(pnewbuf->text, pumsgstr, MIN(len+1, - MAXBUFLINE)); - newlen = u32_decode_tabs(pnewbuf->text, MAXBUFLINE); + u32_strncpy(pnewbuf->text, pumsgstr, + MIN(len + 1, MAXBUFLINE)); + newlen = u32_decode_tabs(pnewbuf->text, + MAXBUFLINE); pnewbuf->length = newlen; - pumsgstr += len+(pumsgstr[len] ? 2 : 0); + pumsgstr += len + (pumsgstr[len] ? 2 : 0); pnewbuf++; } } @@ -419,7 +415,7 @@ save_msgstr(struct DrawState* state) { uint32_t to_save[MAXMSG]; - state->show_edit = 0; + state->show_edit = 0; state->edit_info_focused = 0; if (state->input_buffer) @@ -427,15 +423,15 @@ save_msgstr(struct DrawState* state) struct BufferLine* pinput_buffer = NULL; for (size_t i = 0; i < state->input_rows_count; i++) { - pinput_buffer = state->input_buffer+i; + pinput_buffer = state->input_buffer + i; if (i == 0) u32_strncpy(to_save, pinput_buffer->text, - MAXMSG); + MAXMSG); else { u32_strncat(to_save, (uint32_t*)L"\\n", MAXMSG); u32_strncat(to_save, pinput_buffer->text, - MAXMSG); + MAXMSG); } free_bufferline(pinput_buffer); } @@ -444,12 +440,12 @@ save_msgstr(struct DrawState* state) state->input_buffer = NULL; if (state->info_buffer) for (size_t i = 0; i < state->info_rows_count; i++) - free_bufferline(state->info_buffer+i); + free_bufferline(state->info_buffer + i); free(state->info_buffer); state->info_buffer = NULL; u32_encode_tabs(to_save, MAXBUFLINE); - u32_set_msgstr(&state->entries[state->msgid_number-1], to_save, - state->msgstr_index, 0, state); + u32_set_msgstr(&state->entries[state->msgid_number - 1], to_save, + state->msgstr_index, 0, state); state->input_changed_fuzzy = UNCHANGED; tb_set_cursor(TB_HIDE_CURSOR, TB_HIDE_CURSOR); @@ -458,49 +454,48 @@ save_msgstr(struct DrawState* state) void cancel_msgstr(struct DrawState* state) { - state->show_edit = 0; + state->show_edit = 0; state->edit_info_focused = 0; if (state->input_buffer) for (size_t i = 0; i < state->input_rows_count; i++) - free_bufferline(state->input_buffer+i); + free_bufferline(state->input_buffer + i); free(state->input_buffer); state->input_buffer = NULL; if (state->info_buffer) for (size_t i = 0; i < state->info_rows_count; i++) - free_bufferline(state->info_buffer+i); + free_bufferline(state->info_buffer + i); free(state->info_buffer); state->info_buffer = NULL; - state->dirty = state->dirty_before_edit; + state->dirty = state->dirty_before_edit; state->fuzzy_count = state->fuzzy_before_edit; switch (state->input_changed_fuzzy) { - case SET: - state->entries[state->msgid_number-1].flags &= - ~FL_FUZZY; - break; - case CLEARED: - state->entries[state->msgid_number-1].flags |= FL_FUZZY; - break; - default:; + case SET: + state->entries[state->msgid_number - 1].flags &= ~FL_FUZZY; + break; + case CLEARED: + state->entries[state->msgid_number - 1].flags |= FL_FUZZY; + break; + default:; } state->input_changed_fuzzy = UNCHANGED; tb_set_cursor(TB_HIDE_CURSOR, TB_HIDE_CURSOR); } -void +void show_search(struct DrawState* state) { - state->show_search = 1; - state->search_column = 0; - state->search_display_column = 0; + state->show_search = 1; + state->search_column = 0; + state->search_display_column = 0; state->search_first_shown_column = 0; if (!state->search) state->search = calloc(MAXSEARCH, sizeof(uint32_t)); state->search[0] = 0; - tb_set_cursor((state->maxx-SEARCH_WIDTH+2)/2, - state->maxy-1-SEARCH_HEIGHT+1); + tb_set_cursor((state->maxx - SEARCH_WIDTH + 2) / 2, + state->maxy - 1 - SEARCH_HEIGHT + 1); } void @@ -514,38 +509,36 @@ void cancel_search(struct DrawState* state) { state->show_search = 0; - state->search[0] = 0; + state->search[0] = 0; tb_set_cursor(TB_HIDE_CURSOR, TB_HIDE_CURSOR); } -void +void move_search_start(struct DrawState* state) { - state->search_column = 0; - state->search_display_column = 0; + state->search_column = 0; + state->search_display_column = 0; state->search_first_shown_column = 0; } -void +void move_search_left(struct DrawState* state) { if (state->search_column == 0) return; - if (state->search[state->search_column-1] == '\t') - state->search_display_column = display_length(state, - state->search, state->search_column-1); + if (state->search[state->search_column - 1] == '\t') + state->search_display_column = display_length(state, + state->search, state->search_column - 1); else state->search_display_column--; state->search_column--; - if (state->search_display_column < - state->search_first_shown_column) - state->search_first_shown_column = - state->search_display_column; + if (state->search_display_column < state->search_first_shown_column) + state->search_first_shown_column = state->search_display_column; } -void +void move_search_right(struct DrawState* state) { size_t len = u32_strlen(state->search); @@ -553,58 +546,59 @@ move_search_right(struct DrawState* state) return; if (state->search[state->search_column] == '\t') - state->search_display_column = display_length(state, - state->search, state->search_column+1); + state->search_display_column = display_length(state, + state->search, state->search_column + 1); else state->search_display_column++; state->search_column++; - if (state->search_display_column - - state->search_first_shown_column > SEARCH_WIDTH-4) - state->search_first_shown_column = - state->search_display_column - SEARCH_WIDTH+4; + if (state->search_display_column - state->search_first_shown_column + > SEARCH_WIDTH - 4) + state->search_first_shown_column + = state->search_display_column - SEARCH_WIDTH + 4; } -void +void move_search_end(struct DrawState* state) { - size_t len = u32_strlen(state->search); + size_t len = u32_strlen(state->search); state->search_column = len; - size_t dlen = display_length(state, state->search, state->search_column); + size_t dlen + = display_length(state, state->search, state->search_column); state->search_display_column = dlen; - if (dlen > SEARCH_WIDTH-4) - state->search_first_shown_column = dlen - SEARCH_WIDTH+4; + if (dlen > SEARCH_WIDTH - 4) + state->search_first_shown_column = dlen - SEARCH_WIDTH + 4; else state->search_first_shown_column = 0; } -void +void insert_search_char(struct DrawState* state, uint32_t ch) { size_t len = u32_strlen(state->search); - if (state->search_column < MAXSEARCH-1) + if (state->search_column < MAXSEARCH - 1) { - size_t i = len+1; + size_t i = len + 1; int add_null = state->search[state->search_column] == 0; - while (i < MAXSEARCH-2 && i > state->search_column) + while (i < MAXSEARCH - 2 && i > state->search_column) { - state->search[i] = state->search[i-1]; + state->search[i] = state->search[i - 1]; i--; } state->search[state->search_column++] = ch; if (state->search_column == MAXSEARCH) - state->search_column = MAXSEARCH-1; - state->search_display_column = display_length(state, - state->search, - state->search_column); - if (state->search_display_column - - state->search_first_shown_column > - SEARCH_WIDTH-4) + state->search_column = MAXSEARCH - 1; + state->search_display_column = display_length(state, + state->search, state->search_column); + if (state->search_display_column + - state->search_first_shown_column + > SEARCH_WIDTH - 4) { - state->search_first_shown_column = - state->search_display_column - SEARCH_WIDTH+4; - state->search_display_column = display_length(state, - state->search, state->search_column); + state->search_first_shown_column + = state->search_display_column - SEARCH_WIDTH + + 4; + state->search_display_column = display_length(state, + state->search, state->search_column); } if (add_null) state->search[state->search_column] = 0; @@ -613,7 +607,7 @@ insert_search_char(struct DrawState* state, uint32_t ch) } } -void +void erase_search_backwards(struct DrawState* state) { if (state->search_column > 0) @@ -621,25 +615,22 @@ erase_search_backwards(struct DrawState* state) size_t i = state->search_column; do { - state->search[i-1] = state->search[i]; + state->search[i - 1] = state->search[i]; i++; - } - while (state->search[i]); - state->search[i-1] = 0; + } while (state->search[i]); + state->search[i - 1] = 0; state->search_column--; - if (state->search_column < - state->search_first_shown_column) + if (state->search_column < state->search_first_shown_column) { - if (state->search_column > SEARCH_WIDTH-4) - state->search_first_shown_column = - state->search_column - - SEARCH_WIDTH+4; + if (state->search_column > SEARCH_WIDTH - 4) + state->search_first_shown_column + = state->search_column - SEARCH_WIDTH + + 4; else state->search_first_shown_column = 0; } - state->search_display_column = display_length(state, - state->search, - state->search_column); + state->search_display_column = display_length(state, + state->search, state->search_column); next_match(state); } @@ -651,9 +642,9 @@ erase_search_forward(struct DrawState* state) if (state->search[state->search_column]) { size_t i = state->search_column; - while (i < MAXSEARCH-2 && state->search[i+1]) + while (i < MAXSEARCH - 2 && state->search[i + 1]) { - state->search[i] = state->search[i+1]; + state->search[i] = state->search[i + 1]; i++; } state->search[i] = 0; @@ -662,23 +653,22 @@ erase_search_forward(struct DrawState* state) } } -void +void erase_search_to_start(struct DrawState* state) { - size_t i = state->search_column; + size_t i = state->search_column; size_t len = u32_strlen(state->search); do { - state->search[i-state->search_column] = state->search[i]; + state->search[i - state->search_column] = state->search[i]; i++; - } - while (state->search[i]); - state->search[len-state->search_column] = 0; + } while (state->search[i]); + state->search[len - state->search_column] = 0; move_search_start(state); next_match(state); } -void +void erase_search_to_end(struct DrawState* state) { state->search[state->search_column] = 0; @@ -686,10 +676,10 @@ erase_search_to_end(struct DrawState* state) next_match(state); } -void +void move_search_prev_word(struct DrawState* state) { - int i = state->search_column; + int i = state->search_column; uint32_t* pline = state->search; while (i > 0 && (pline[i] == ' ' || pline[i] == '\t')) @@ -698,54 +688,50 @@ move_search_prev_word(struct DrawState* state) i--; state->search_column = i; - state->search_display_column = display_length(state, state->search, - state->search_column); + state->search_display_column + = display_length(state, state->search, state->search_column); - if (state->search_column < - state->search_first_shown_column) + if (state->search_column < state->search_first_shown_column) { - if (state->search_column > SEARCH_WIDTH-4) - state->search_first_shown_column = - state->search_column - - SEARCH_WIDTH+4; + if (state->search_column > SEARCH_WIDTH - 4) + state->search_first_shown_column + = state->search_column - SEARCH_WIDTH + 4; else state->search_first_shown_column = 0; } - state->search_display_column = display_length(state, - state->search, - state->search_column); + state->search_display_column + = display_length(state, state->search, state->search_column); } -void +void move_search_next_word(struct DrawState* state) { - int i = state->search_column; + int i = state->search_column; uint32_t* pline = state->search; - size_t len = u32_strlen(state->search); + size_t len = u32_strlen(state->search); while (i < len && (pline[i] != ' ' && pline[i] != '\t')) i++; while (i < len && (pline[i] == ' ' || pline[i] == '\t')) i++; state->search_column = i; - state->search_display_column = display_length(state, state->search, - state->search_column); + state->search_display_column + = display_length(state, state->search, state->search_column); - if (state->search_display_column - - state->search_first_shown_column > - SEARCH_WIDTH-4) + if (state->search_display_column - state->search_first_shown_column + > SEARCH_WIDTH - 4) { - state->search_first_shown_column = - state->search_display_column - SEARCH_WIDTH+4; - state->search_display_column = display_length(state, - state->search, state->search_column); + state->search_first_shown_column + = state->search_display_column - SEARCH_WIDTH + 4; + state->search_display_column = display_length(state, + state->search, state->search_column); } } void erase_search_prev_word(struct DrawState* state) { - int i = state->search_column; + int i = state->search_column; size_t len = u32_strlen(state->search); uint32_t* pline = state->search; @@ -757,33 +743,30 @@ erase_search_prev_word(struct DrawState* state) int d = state->search_column - i; - for (int j = i; j < len-d; j++) - pline[j] = pline[j+d]; + for (int j = i; j < len - d; j++) + pline[j] = pline[j + d]; - len -= state->search_column-i; - pline[len] = 0; + len -= state->search_column - i; + pline[len] = 0; state->search_column = i; - state->search_display_column = display_length(state, state->search, - state->search_column); + state->search_display_column + = display_length(state, state->search, state->search_column); - if (state->search_column < - state->search_first_shown_column) + if (state->search_column < state->search_first_shown_column) { - if (state->search_column > SEARCH_WIDTH-4) - state->search_first_shown_column = - state->search_column - - SEARCH_WIDTH+4; + if (state->search_column > SEARCH_WIDTH - 4) + state->search_first_shown_column + = state->search_column - SEARCH_WIDTH + 4; else state->search_first_shown_column = 0; } - state->search_display_column = display_length(state, - state->search, - state->search_column); + state->search_display_column + = display_length(state, state->search, state->search_column); next_match(state); } -struct DrawState* +struct DrawState* copy_msgid_to_input(struct DrawState* state) { if (state->edit_info_focused) @@ -795,18 +778,20 @@ copy_msgid_to_input(struct DrawState* state) return NULL; } - struct PoEntry* current = &state->entries[state->msgid_number-1]; - size_t msgid_num_lines = u32_lines_in_string(current->msgid, NULL); - size_t lines_to_init = state->input_rows_count < msgid_num_lines - ? msgid_num_lines - state->input_rows_count : 0; - size_t lines_to_free = state->input_rows_count > msgid_num_lines - ? state->input_rows_count - msgid_num_lines : 0; + struct PoEntry* current = &state->entries[state->msgid_number - 1]; + size_t msgid_num_lines = u32_lines_in_string(current->msgid, NULL); + size_t lines_to_init = state->input_rows_count < msgid_num_lines + ? msgid_num_lines - state->input_rows_count + : 0; + size_t lines_to_free = state->input_rows_count > msgid_num_lines + ? state->input_rows_count - msgid_num_lines + : 0; for (size_t i = 0; i < lines_to_free; i++) - free_bufferline(state->input_buffer+ - state->input_rows_count-i-1); - struct BufferLine* newbuf = realloc(state->input_buffer, - msgid_num_lines * sizeof(struct BufferLine)); - uint32_t* pmsgid = current->msgid; + free_bufferline( + state->input_buffer + state->input_rows_count - i - 1); + struct BufferLine* newbuf = realloc(state->input_buffer, + msgid_num_lines * sizeof(struct BufferLine)); + uint32_t* pmsgid = current->msgid; if (!newbuf) return NULL; @@ -814,7 +799,7 @@ copy_msgid_to_input(struct DrawState* state) struct BufferLine* pbuffer = NULL; for (size_t i = 0; i < lines_to_init; i++) { - pbuffer = newbuf+msgid_num_lines-1-i; + pbuffer = newbuf + msgid_num_lines - 1 - i; init_bufferline(pbuffer); uint32_t* newtext = calloc(MAXBUFLINE, sizeof(uint32_t)); if (!newtext) @@ -825,25 +810,24 @@ copy_msgid_to_input(struct DrawState* state) if (pmsgid) for (size_t i = 0; i < msgid_num_lines; i++) { - pbuffer = newbuf+i; + pbuffer = newbuf + i; size_t len = u32_line_length(pmsgid); size_t newlen = 0; - u32_strncpy(pbuffer->text, pmsgid, MIN(len+1, - MAXBUFLINE)); - newlen = u32_decode_tabs(pbuffer->text, - MAXBUFLINE); - pbuffer->length = newlen; - pmsgid += len+(pmsgid[len] ? 2 : 0); + u32_strncpy(pbuffer->text, pmsgid, + MIN(len + 1, MAXBUFLINE)); + newlen = u32_decode_tabs(pbuffer->text, MAXBUFLINE); + pbuffer->length = newlen; + pmsgid += len + (pmsgid[len] ? 2 : 0); } - + state->input_rows_count = msgid_num_lines; - state->input_buffer = newbuf; - state->dirty = 1; + state->input_buffer = newbuf; + state->dirty = 1; return state; } -struct DrawState* +struct DrawState* yank_msgstr_to_paste_buffer(struct DrawState* state) { if (state->edit_info_focused) @@ -855,19 +839,22 @@ yank_msgstr_to_paste_buffer(struct DrawState* state) return NULL; } - struct PoEntry* current = &state->entries[state->msgid_number-1]; - size_t msgstr_num_lines = u32_lines_in_string(*(current->msgstr + - state->msgstr_index), NULL); - size_t lines_to_init = state->paste_rows_count < msgstr_num_lines - ? msgstr_num_lines - state->paste_rows_count : 0; - size_t lines_to_free = state->paste_rows_count > msgstr_num_lines - ? state->paste_rows_count - msgstr_num_lines : 0; + struct PoEntry* current = &state->entries[state->msgid_number - 1]; + size_t msgstr_num_lines + = u32_lines_in_string(*(current->msgstr + state->msgstr_index), + NULL); + size_t lines_to_init = state->paste_rows_count < msgstr_num_lines + ? msgstr_num_lines - state->paste_rows_count + : 0; + size_t lines_to_free = state->paste_rows_count > msgstr_num_lines + ? state->paste_rows_count - msgstr_num_lines + : 0; for (size_t i = 0; i < lines_to_free; i++) - free_bufferline(state->paste_buffer+ - state->paste_rows_count-i-1); - struct BufferLine* newbuf = realloc(state->paste_buffer, - msgstr_num_lines * sizeof(struct BufferLine)); - uint32_t* pmsgstr = *(current->msgstr+state->msgstr_index); + free_bufferline( + state->paste_buffer + state->paste_rows_count - i - 1); + struct BufferLine* newbuf = realloc(state->paste_buffer, + msgstr_num_lines * sizeof(struct BufferLine)); + uint32_t* pmsgstr = *(current->msgstr + state->msgstr_index); if (!newbuf) return NULL; @@ -875,7 +862,7 @@ yank_msgstr_to_paste_buffer(struct DrawState* state) struct BufferLine* pbuffer = NULL; for (size_t i = 0; i < lines_to_init; i++) { - pbuffer = newbuf+msgstr_num_lines-1-i; + pbuffer = newbuf + msgstr_num_lines - 1 - i; init_bufferline(pbuffer); uint32_t* newtext = calloc(MAXBUFLINE, sizeof(uint32_t)); if (!newtext) @@ -886,19 +873,18 @@ yank_msgstr_to_paste_buffer(struct DrawState* state) if (pmsgstr) for (size_t i = 0; i < msgstr_num_lines; i++) { - pbuffer = newbuf+i; + pbuffer = newbuf + i; size_t len = u32_line_length(pmsgstr); size_t newlen = 0; - u32_strncpy(pbuffer->text, pmsgstr, MIN(len+1, - MAXBUFLINE)); - newlen = u32_decode_tabs(pbuffer->text, - MAXBUFLINE); - pbuffer->length = newlen; - pmsgstr += len+(pmsgstr[len] ? 2 : 0); + u32_strncpy(pbuffer->text, pmsgstr, + MIN(len + 1, MAXBUFLINE)); + newlen = u32_decode_tabs(pbuffer->text, MAXBUFLINE); + pbuffer->length = newlen; + pmsgstr += len + (pmsgstr[len] ? 2 : 0); } - + state->paste_rows_count = msgstr_num_lines; - state->paste_buffer = newbuf; + state->paste_buffer = newbuf; return state; } @@ -915,17 +901,17 @@ paste_paste_buffer_to_input(struct DrawState* state) return NULL; } - size_t lines_to_init = state->input_rows_count < - state->paste_rows_count ? state->paste_rows_count - - state->input_rows_count : 0; - size_t lines_to_free = state->input_rows_count > - state->paste_rows_count ? state->input_rows_count - - state->paste_rows_count: 0; + size_t lines_to_init = state->input_rows_count < state->paste_rows_count + ? state->paste_rows_count - state->input_rows_count + : 0; + size_t lines_to_free = state->input_rows_count > state->paste_rows_count + ? state->input_rows_count - state->paste_rows_count + : 0; for (size_t i = 0; i < lines_to_free; i++) - free_bufferline(state->input_buffer+ - state->input_rows_count-i-1); - struct BufferLine* newbuf = realloc(state->input_buffer, - state->paste_rows_count * sizeof(struct BufferLine)); + free_bufferline( + state->input_buffer + state->input_rows_count - i - 1); + struct BufferLine* newbuf = realloc(state->input_buffer, + state->paste_rows_count * sizeof(struct BufferLine)); struct BufferLine* ppaste = state->paste_buffer; if (!newbuf) @@ -934,7 +920,7 @@ paste_paste_buffer_to_input(struct DrawState* state) struct BufferLine* pinput = NULL; for (size_t i = 0; i < lines_to_init; i++) { - pinput = newbuf+state->paste_rows_count-1-i; + pinput = newbuf + state->paste_rows_count - 1 - i; init_bufferline(pinput); uint32_t* newtext = calloc(MAXBUFLINE, sizeof(uint32_t)); if (!newtext) @@ -945,78 +931,74 @@ paste_paste_buffer_to_input(struct DrawState* state) if (ppaste) for (size_t i = 0; i < state->paste_rows_count; i++) { - pinput = newbuf+i; - u32_strncpy(pinput->text, ppaste->text, - MIN(ppaste->length+1, MAXBUFLINE)); - pinput->length = ppaste->length; + pinput = newbuf + i; + u32_strncpy(pinput->text, ppaste->text, + MIN(ppaste->length + 1, MAXBUFLINE)); + pinput->length = ppaste->length; ppaste++; } - + state->input_rows_count = state->paste_rows_count; - state->input_buffer = newbuf; - state->dirty = 1; + state->input_buffer = newbuf; + state->dirty = 1; return state; } -void +void goto_msgid(struct DrawState* state, const size_t msgid_number) { state->msgid_number = msgid_number; - if (state->msgid_number < (state->maxy-1)/2) - state->first_shown_msgid = - state->msgid_count > 0 ? 1 : 0; - else if (state->msgid_count - state->msgid_number < - (state->maxy-1)/2) - state->first_shown_msgid = MAX(1, - state->msgid_count - (state->maxy-2)); + if (state->msgid_number < (state->maxy - 1) / 2) + state->first_shown_msgid = state->msgid_count > 0 ? 1 : 0; + else if (state->msgid_count - state->msgid_number + < (state->maxy - 1) / 2) + state->first_shown_msgid + = MAX(1, state->msgid_count - (state->maxy - 2)); else - state->first_shown_msgid = MAX(1, - state->msgid_number - - (state->maxy-1)/2); + state->first_shown_msgid + = MAX(1, state->msgid_number - (state->maxy - 1) / 2); } -void +void next_match(struct DrawState* state) { if (state->msgid_count == 0 || !state->search || !*state->search) return; - state->error[0] = 0; + state->error[0] = 0; struct PoEntry* current = NULL; - for (size_t e = state->msgid_number; - e < state->msgid_count; e++) + for (size_t e = state->msgid_number; e < state->msgid_count; e++) { current = state->entries + e; - if ((current->msgid && *current->msgid && - u32_strstr(current->msgid, state->search)) - || (current->msgstr && *current->msgstr && - **current->msgstr && u32_strstr(*current->msgstr, - state->search))) + if ((current->msgid && *current->msgid + && u32_strstr(current->msgid, state->search)) + || (current->msgstr && *current->msgstr + && **current->msgstr + && u32_strstr(*current->msgstr, state->search))) { - goto_msgid(state, e+1); + goto_msgid(state, e + 1); return; } } } -void +void prev_match(struct DrawState* state) { if (state->msgid_count == 0 || !state->search || !*state->search) return; - state->error[0] = 0; + state->error[0] = 0; struct PoEntry* current = NULL; - for (size_t e = state->msgid_number-1; - e > 0; e--) - { - current = state->entries + e-1; - if ((current->msgid && *current->msgid && - u32_strstr(current->msgid, state->search)) - || (current->msgstr && *current->msgstr && - **current->msgstr && u32_strstr(*current->msgstr, - state->search))) + for (size_t e = state->msgid_number - 1; e > 0; e--) + { + current = state->entries + e - 1; + if ((current->msgid && *current->msgid + && u32_strstr(current->msgid, state->search)) + || (current->msgstr && *current->msgstr + && **current->msgstr + && u32_strstr(*current->msgstr, state->search))) { goto_msgid(state, e); return; @@ -1024,7 +1006,7 @@ prev_match(struct DrawState* state) } } -void +void next_untranslated(struct DrawState* state) { if (state->msgid_count == 0) @@ -1036,8 +1018,8 @@ next_untranslated(struct DrawState* state) for (size_t e = state->msgid_number; e < state->msgid_count; e++) { current = state->entries + e; - if (!current->msgstr || !*current->msgstr || - u32_strlen(*current->msgstr) == 0) + if (!current->msgstr || !*current->msgstr + || u32_strlen(*current->msgstr) == 0) { int save_show_edit = state->show_edit; if (save_show_edit) @@ -1048,7 +1030,7 @@ next_untranslated(struct DrawState* state) state->dirty = state->dirty_before_edit; } - goto_msgid(state, e+1); + goto_msgid(state, e + 1); if (save_show_edit) { @@ -1061,7 +1043,7 @@ next_untranslated(struct DrawState* state) } } -void +void next_fuzzy(struct DrawState* state) { if (state->msgid_count == 0) @@ -1084,7 +1066,7 @@ next_fuzzy(struct DrawState* state) state->dirty = state->dirty_before_edit; } - goto_msgid(state, e+1); + goto_msgid(state, e + 1); if (save_show_edit) { @@ -1102,15 +1084,15 @@ show_edit(struct DrawState* state) { if (load_msgstr(state) && load_info(state)) { - state->dirty_before_edit = state->dirty; - state->fuzzy_before_edit = state->fuzzy_count; - state->input_changed_fuzzy = UNCHANGED; - state->show_edit = 1; - state->input_row = 0; - state->input_first_shown_row = 0; + state->dirty_before_edit = state->dirty; + state->fuzzy_before_edit = state->fuzzy_count; + state->input_changed_fuzzy = UNCHANGED; + state->show_edit = 1; + state->input_row = 0; + state->input_first_shown_row = 0; state->input_first_shown_column = 0; - state->info_first_shown_row = 0; - state->info_first_shown_column = 0; + state->info_first_shown_row = 0; + state->info_first_shown_column = 0; move_start(state); state->error[0] = 0; } @@ -1137,25 +1119,25 @@ move_up(struct DrawState* state) state->input_first_shown_row = state->input_row; if (state->saved_column == -1) state->saved_column = state->input_display_column; - size_t dlen = display_length(state, state->input_buffer[ - state->input_row].text, - state->input_buffer[state->input_row].length); + size_t dlen = display_length(state, + state->input_buffer[state->input_row].text, + state->input_buffer[state->input_row].length); if (dlen < state->saved_column) state->input_display_column = dlen; else state->input_display_column = state->saved_column; - state->input_column = input_length(state, state->input_buffer[ - state->input_row].text, - state->input_display_column); + state->input_column = input_length(state, + state->input_buffer[state->input_row].text, + state->input_display_column); if (state->input_first_shown_column > state->input_display_column) state->input_first_shown_column = state->input_display_column; - if (state->input_first_shown_column + EDIT_WIDTH-4 < - state->input_display_column) - state->input_first_shown_column = state->input_display_column - - EDIT_WIDTH+4; - state->input_display_column = display_length(state, - state->input_buffer[state->input_row].text, - state->input_column); + if (state->input_first_shown_column + EDIT_WIDTH - 4 + < state->input_display_column) + state->input_first_shown_column + = state->input_display_column - EDIT_WIDTH + 4; + state->input_display_column = display_length(state, + state->input_buffer[state->input_row].text, + state->input_column); } void @@ -1163,48 +1145,48 @@ move_down(struct DrawState* state) { if (state->edit_info_focused) { - if (state->info_first_shown_row + (EDIT_HEIGHT-3)/2 < - state->info_rows_count) + if (state->info_first_shown_row + (EDIT_HEIGHT - 3) / 2 + < state->info_rows_count) state->info_first_shown_row++; return; } - if (state->input_row < state->input_rows_count-1) + if (state->input_row < state->input_rows_count - 1) state->input_row++; - if (state->input_first_shown_row + (EDIT_HEIGHT-3)/2 < - state->input_row) - state->input_first_shown_row = state->input_row - - (EDIT_HEIGHT-3)/2; + if (state->input_first_shown_row + (EDIT_HEIGHT - 3) / 2 + < state->input_row) + state->input_first_shown_row + = state->input_row - (EDIT_HEIGHT - 3) / 2; if (state->saved_column == -1) state->saved_column = state->input_display_column; - size_t dlen = display_length(state, state->input_buffer[ - state->input_row].text, - state->input_buffer[state->input_row].length); + size_t dlen = display_length(state, + state->input_buffer[state->input_row].text, + state->input_buffer[state->input_row].length); if (dlen < state->saved_column) state->input_display_column = dlen; else state->input_display_column = state->saved_column; - state->input_column = input_length(state, state->input_buffer[ - state->input_row].text, - state->input_display_column); + state->input_column = input_length(state, + state->input_buffer[state->input_row].text, + state->input_display_column); if (state->input_first_shown_column > state->input_display_column) state->input_first_shown_column = state->input_display_column; - if (state->input_first_shown_column + EDIT_WIDTH-4 < - state->input_display_column) - state->input_first_shown_column = state->input_display_column - - EDIT_WIDTH+4; - state->input_display_column = display_length(state, - state->input_buffer[state->input_row].text, - state->input_column); + if (state->input_first_shown_column + EDIT_WIDTH - 4 + < state->input_display_column) + state->input_first_shown_column + = state->input_display_column - EDIT_WIDTH + 4; + state->input_display_column = display_length(state, + state->input_buffer[state->input_row].text, + state->input_column); } -void +void edit_page_up(struct DrawState* state) { if (state->edit_info_focused) { - if (state->info_first_shown_row > (EDIT_HEIGHT-3)/2) - state->info_first_shown_row -= (EDIT_HEIGHT-3)/2; + if (state->info_first_shown_row > (EDIT_HEIGHT - 3) / 2) + state->info_first_shown_row -= (EDIT_HEIGHT - 3) / 2; else move_start(state); return; @@ -1219,14 +1201,14 @@ edit_page_up(struct DrawState* state) show_edit(state); } -void +void edit_page_down(struct DrawState* state) { if (state->edit_info_focused) { - if (state->info_first_shown_row + (EDIT_HEIGHT-3)/2 < - state->info_rows_count-1) - state->info_first_shown_row += (EDIT_HEIGHT-3)/2; + if (state->info_first_shown_row + (EDIT_HEIGHT - 3) / 2 + < state->info_rows_count - 1) + state->info_first_shown_row += (EDIT_HEIGHT - 3) / 2; return; } @@ -1244,13 +1226,13 @@ move_start(struct DrawState* state) { if (state->edit_info_focused) { - state->info_first_shown_row = 0; + state->info_first_shown_row = 0; state->info_first_shown_column = 0; return; } - state->input_column = 0; - state->input_display_column = 0; + state->input_column = 0; + state->input_display_column = 0; state->input_first_shown_column = 0; } @@ -1267,11 +1249,11 @@ move_left(struct DrawState* state) if (state->input_column == 0) return; - if (state->input_buffer[state->input_row].text[ - state->input_column-1] == '\t') - state->input_display_column = display_length(state, - state->input_buffer[state->input_row].text, - state->input_column-1); + if (state->input_buffer[state->input_row].text[state->input_column - 1] + == '\t') + state->input_display_column = display_length(state, + state->input_buffer[state->input_row].text, + state->input_column - 1); else state->input_display_column--; state->input_column--; @@ -1285,8 +1267,8 @@ move_right(struct DrawState* state) { if (state->edit_info_focused) { - if (state->info_first_shown_column + EDIT_WIDTH-3 < - state->info_rows_maxlen + 1) + if (state->info_first_shown_column + EDIT_WIDTH - 3 + < state->info_rows_maxlen + 1) state->info_first_shown_column++; return; } @@ -1295,22 +1277,22 @@ move_right(struct DrawState* state) if (state->input_column == len) return; - if (state->input_buffer[state->input_row].text[ - state->input_column] == '\t') - state->input_display_column = display_length(state, - state->input_buffer[state->input_row].text, - state->input_column+1); + if (state->input_buffer[state->input_row].text[state->input_column] + == '\t') + state->input_display_column = display_length(state, + state->input_buffer[state->input_row].text, + state->input_column + 1); else state->input_display_column++; state->input_column++; if (state->input_display_column - state->input_first_shown_column - > EDIT_WIDTH-4) - state->input_first_shown_column = state->input_display_column - - EDIT_WIDTH+4; + > EDIT_WIDTH - 4) + state->input_first_shown_column + = state->input_display_column - EDIT_WIDTH + 4; } -void +void move_prev_word(struct DrawState* state) { if (state->edit_info_focused) @@ -1319,7 +1301,7 @@ move_prev_word(struct DrawState* state) return; } - int i = state->input_column; + int i = state->input_column; uint32_t* pline = state->input_buffer[state->input_row].text; while (i > 0 && (pline[i] == ' ' || pline[i] == '\t')) @@ -1327,14 +1309,15 @@ move_prev_word(struct DrawState* state) while (i > 0 && (pline[i] != ' ' && pline[i] != '\t')) i--; - state->input_column = i; - state->input_display_column = display_length(state, state->input_buffer[ - state->input_row].text, state->input_column); + state->input_column = i; + state->input_display_column = display_length(state, + state->input_buffer[state->input_row].text, + state->input_column); if (state->input_display_column < state->input_first_shown_column) state->input_first_shown_column = state->input_display_column; } -void +void move_next_word(struct DrawState* state) { if (state->edit_info_focused) @@ -1343,21 +1326,22 @@ move_next_word(struct DrawState* state) return; } - int i = state->input_column; + int i = state->input_column; uint32_t* pline = state->input_buffer[state->input_row].text; - size_t len = state->input_buffer[state->input_row].length; + size_t len = state->input_buffer[state->input_row].length; while (i < len && (pline[i] != ' ' && pline[i] != '\t')) i++; while (i < len && (pline[i] == ' ' || pline[i] == '\t')) i++; - state->input_column = i; - state->input_display_column = display_length(state, state->input_buffer[ - state->input_row].text, state->input_column); + state->input_column = i; + state->input_display_column = display_length(state, + state->input_buffer[state->input_row].text, + state->input_column); if (state->input_display_column - state->input_first_shown_column - > EDIT_WIDTH-4) - state->input_first_shown_column = state->input_display_column - - EDIT_WIDTH+4; + > EDIT_WIDTH - 4) + state->input_first_shown_column + = state->input_display_column - EDIT_WIDTH + 4; } void @@ -1369,7 +1353,7 @@ erase_prev_word(struct DrawState* state) return; } - int i = state->input_column; + int i = state->input_column; size_t len = state->input_buffer[state->input_row].length; uint32_t* pline = state->input_buffer[state->input_row].text; @@ -1381,16 +1365,17 @@ erase_prev_word(struct DrawState* state) int d = state->input_column - i; - for (int j = i; j < len-d; j++) - pline[j] = pline[j+d]; + for (int j = i; j < len - d; j++) + pline[j] = pline[j + d]; len -= d; state->input_buffer[state->input_row].length = len; - pline[len] = 0; - state->input_column = i; - state->input_display_column = display_length(state, state->input_buffer[ - state->input_row].text, state->input_column); - state->dirty = 1; + pline[len] = 0; + state->input_column = i; + state->input_display_column = display_length(state, + state->input_buffer[state->input_row].text, + state->input_column); + state->dirty = 1; } void @@ -1398,25 +1383,26 @@ move_end(struct DrawState* state) { if (state->edit_info_focused) { - if (state->info_rows_count > (EDIT_HEIGHT-3)/2) - state->info_first_shown_row = state->info_rows_count - - (EDIT_HEIGHT-3)/2; + if (state->info_rows_count > (EDIT_HEIGHT - 3) / 2) + state->info_first_shown_row = state->info_rows_count + - (EDIT_HEIGHT - 3) / 2; state->info_first_shown_column = 0; return; } - size_t len = state->input_buffer[state->input_row].length; + size_t len = state->input_buffer[state->input_row].length; state->input_column = len; - size_t dlen = display_length(state, state->input_buffer[ - state->input_row].text, state->input_column); + size_t dlen = display_length(state, + state->input_buffer[state->input_row].text, + state->input_column); state->input_display_column = dlen; - if (dlen > EDIT_WIDTH-4) - state->input_first_shown_column = dlen - EDIT_WIDTH+4; + if (dlen > EDIT_WIDTH - 4) + state->input_first_shown_column = dlen - EDIT_WIDTH + 4; else state->input_first_shown_column = 0; } -void +void move_list_up(struct DrawState* state) { if (state->msgid_number > 1) @@ -1428,37 +1414,38 @@ move_list_up(struct DrawState* state) } } -void +void move_list_down(struct DrawState* state) { if (state->msgid_number < state->msgid_count) { state->error[0] = 0; state->msgid_number++; - if (state->msgid_number - state->first_shown_msgid > - state->maxy - 2) + if (state->msgid_number - state->first_shown_msgid + > state->maxy - 2) state->first_shown_msgid++; } } -void +void move_list_start(struct DrawState* state) { - state->error[0] = 0; - state->msgid_number = state->msgid_count > 0 ? 1 : 0; + state->error[0] = 0; + state->msgid_number = state->msgid_count > 0 ? 1 : 0; state->first_shown_msgid = state->msgid_number; } -void +void move_list_end(struct DrawState* state) { - state->error[0] = 0; - state->msgid_number = state->msgid_count; - state->first_shown_msgid = state->msgid_count > 0 ? - MAX(1, state->msgid_number - (state->maxy - 2)) : 0; + state->error[0] = 0; + state->msgid_number = state->msgid_count; + state->first_shown_msgid = state->msgid_count > 0 + ? MAX(1, state->msgid_number - (state->maxy - 2)) + : 0; } -void +void move_list_page_up(struct DrawState* state) { state->error[0] = 0; @@ -1472,35 +1459,35 @@ move_list_page_up(struct DrawState* state) move_list_start(state); } -void +void move_list_page_down(struct DrawState* state) { state->error[0] = 0; if (state->msgid_number < state->msgid_count - state->maxy - 2) { state->msgid_number += state->maxy - 2; - if (state->msgid_number - state->first_shown_msgid > - state->maxy - 2) - state->first_shown_msgid = state->msgid_number - - (state->maxy - 2); + if (state->msgid_number - state->first_shown_msgid + > state->maxy - 2) + state->first_shown_msgid + = state->msgid_number - (state->maxy - 2); } else move_list_end(state); } -void +void next_plural(struct DrawState* state) { if (state->edit_info_focused) return; - struct PoEntry* current = &state->entries[state->msgid_number-1]; + struct PoEntry* current = &state->entries[state->msgid_number - 1]; save_msgstr(state); if (state->msgstr_index + 1 < current->msgstr_count) state->msgstr_index++; show_edit(state); } -void +void prev_plural(struct DrawState* state) { if (state->edit_info_focused) @@ -1521,8 +1508,8 @@ toggle_fuzzy(struct DrawState* state) } if (state->msgid_count > 0) { - state->entries[state->msgid_number-1].flags ^= FL_FUZZY; - if (state->entries[state->msgid_number-1].flags & FL_FUZZY) + state->entries[state->msgid_number - 1].flags ^= FL_FUZZY; + if (state->entries[state->msgid_number - 1].flags & FL_FUZZY) { state->fuzzy_count++; if (state->input_changed_fuzzy == CLEARED) @@ -1548,31 +1535,31 @@ insert_char(struct DrawState* state, uint32_t ch) if (state->edit_info_focused) return; struct BufferLine* buffer_line = &state->input_buffer[state->input_row]; - size_t len = buffer_line->length; - if (state->input_column < MAXBUFLINE-1) + size_t len = buffer_line->length; + if (state->input_column < MAXBUFLINE - 1) { - size_t i = len+1; + size_t i = len + 1; int add_null = buffer_line->text[state->input_column] == 0; - while (i < MAXBUFLINE-2 && i > state->input_column) + while (i < MAXBUFLINE - 2 && i > state->input_column) { - buffer_line->text[i] = buffer_line->text[i-1]; + buffer_line->text[i] = buffer_line->text[i - 1]; i--; } buffer_line->text[state->input_column++] = ch; buffer_line->length++; if (state->input_column == MAXBUFLINE) - state->input_column = MAXBUFLINE-1; - state->input_display_column = display_length(state, - state->input_buffer[state->input_row].text, - state->input_column); + state->input_column = MAXBUFLINE - 1; + state->input_display_column = display_length(state, + state->input_buffer[state->input_row].text, + state->input_column); if (state->input_display_column - state->input_first_shown_column - > EDIT_WIDTH-4) + > EDIT_WIDTH - 4) { - state->input_first_shown_column = state->input_display_column - - EDIT_WIDTH+4; - state->input_display_column = display_length(state, - state->input_buffer[state->input_row].text, - state->input_column); + state->input_first_shown_column + = state->input_display_column - EDIT_WIDTH + 4; + state->input_display_column = display_length(state, + state->input_buffer[state->input_row].text, + state->input_column); } if (add_null) buffer_line->text[state->input_column] = 0; @@ -1590,25 +1577,23 @@ erase_backwards(struct DrawState* state) size_t i = state->input_column; do { - buffer_line->text[i-1] = buffer_line->text[i]; + buffer_line->text[i - 1] = buffer_line->text[i]; i++; - } - while (buffer_line->text[i]); - buffer_line->text[i-1] = 0; + } while (buffer_line->text[i]); + buffer_line->text[i - 1] = 0; buffer_line->length--; state->input_column--; - state->input_display_column = display_length(state, - state->input_buffer[ - state->input_row].text, - state->input_column); - if (state->input_display_column < - state->input_first_shown_column) - state->input_first_shown_column = - state->input_display_column; + state->input_display_column = display_length(state, + state->input_buffer[state->input_row].text, + state->input_column); + if (state->input_display_column + < state->input_first_shown_column) + state->input_first_shown_column + = state->input_display_column; } else if (state->input_row > 0) { - if (!join_lines(state, state->input_row-1, 0)) + if (!join_lines(state, state->input_row - 1, 0)) { strcpy(state->error, errors[ERR_CANT_ALLOC]); state->running = 0; @@ -1626,16 +1611,16 @@ erase_forward(struct DrawState* state) if (buffer_line->text[state->input_column]) { size_t i = state->input_column; - while (i < MAXBUFLINE-2 && buffer_line->text[i+1]) + while (i < MAXBUFLINE - 2 && buffer_line->text[i + 1]) { - buffer_line->text[i] = buffer_line->text[i+1]; + buffer_line->text[i] = buffer_line->text[i + 1]; i++; } buffer_line->text[i] = 0; buffer_line->length--; state->dirty = 1; } - else if (state->input_row < state->input_rows_count-1) + else if (state->input_row < state->input_rows_count - 1) { if (!join_lines(state, state->input_row, 1)) { @@ -1654,18 +1639,17 @@ erase_to_start(struct DrawState* state) return; } - size_t i = state->input_column; + size_t i = state->input_column; struct BufferLine* buffer_line = &state->input_buffer[state->input_row]; - size_t len = buffer_line->length; + size_t len = buffer_line->length; do { - buffer_line->text[i-state->input_column] = - buffer_line->text[i]; + buffer_line->text[i - state->input_column] + = buffer_line->text[i]; i++; - } - while (buffer_line->text[i]); - buffer_line->text[len-state->input_column] = 0; - buffer_line->length = len-state->input_column; + } while (buffer_line->text[i]); + buffer_line->text[len - state->input_column] = 0; + buffer_line->length = len - state->input_column; move_start(state); state->dirty = 1; } @@ -1677,7 +1661,7 @@ erase_to_end(struct DrawState* state) return; struct BufferLine* buffer_line = &state->input_buffer[state->input_row]; buffer_line->text[state->input_column] = 0; - buffer_line->length = state->input_column; + buffer_line->length = state->input_column; move_end(state); state->dirty = 1; } @@ -1688,39 +1672,39 @@ insert_line(struct DrawState* state) if (state->edit_info_focused) return state; state->input_rows_count++; - struct BufferLine* newbuf = realloc( - state->input_buffer, state->input_rows_count * - sizeof(struct BufferLine)); + struct BufferLine* newbuf = realloc(state->input_buffer, + state->input_rows_count * sizeof(struct BufferLine)); if (!newbuf) return NULL; state->input_buffer = newbuf; - init_bufferline(&state->input_buffer[state->input_rows_count-1]); + init_bufferline(&state->input_buffer[state->input_rows_count - 1]); uint32_t* newtext = calloc(MAXBUFLINE, sizeof(uint32_t)); if (!newtext) return NULL; - state->input_buffer[state->input_rows_count-1].text = newtext; - for (size_t i = state->input_rows_count-1; i > - state->input_row+1; i--) + state->input_buffer[state->input_rows_count - 1].text = newtext; + for (size_t i = state->input_rows_count - 1; i > state->input_row + 1; + i--) { u32_strncpy(state->input_buffer[i].text, - state->input_buffer[i-1].text, MAXBUFLINE); - state->input_buffer[i].length = state->input_buffer[i-1].length; - } - u32_strncpy(state->input_buffer[state->input_row+1].text, - state->input_buffer[state->input_row].text + - state->input_column, MAXBUFLINE); - state->input_buffer[state->input_row+1].length = - u32_strlen(state->input_buffer[state->input_row+1].text); + state->input_buffer[i - 1].text, MAXBUFLINE); + state->input_buffer[i].length + = state->input_buffer[i - 1].length; + } + u32_strncpy(state->input_buffer[state->input_row + 1].text, + state->input_buffer[state->input_row].text + state->input_column, + MAXBUFLINE); + state->input_buffer[state->input_row + 1].length + = u32_strlen(state->input_buffer[state->input_row + 1].text); state->input_buffer[state->input_row].text[state->input_column] = 0; - state->input_buffer[state->input_row].length = - u32_strlen(state->input_buffer[state->input_row].text); + state->input_buffer[state->input_row].length + = u32_strlen(state->input_buffer[state->input_row].text); state->input_row++; - if (state->input_row > state->input_first_shown_row + - (EDIT_HEIGHT-3)/2) - state->input_first_shown_row = state->input_row - - (EDIT_HEIGHT-3)/2; + if (state->input_row + > state->input_first_shown_row + (EDIT_HEIGHT - 3) / 2) + state->input_first_shown_row + = state->input_row - (EDIT_HEIGHT - 3) / 2; move_start(state); state->dirty = 1; @@ -1733,36 +1717,35 @@ join_lines(struct DrawState* state, size_t first_line, int forward) if (!state) return NULL; - state->input_column = state->input_buffer[first_line].length; - state->input_display_column = display_length(state, - state->input_buffer[state->input_row].text, - state->input_column); - u32_strncat(state->input_buffer[first_line].text, state->input_buffer[ - first_line+1].text, MAXBUFLINE); - state->input_buffer[first_line].length = u32_strlen( - state->input_buffer[first_line].text); + state->input_column = state->input_buffer[first_line].length; + state->input_display_column = display_length(state, + state->input_buffer[state->input_row].text, + state->input_column); + u32_strncat(state->input_buffer[first_line].text, + state->input_buffer[first_line + 1].text, MAXBUFLINE); + state->input_buffer[first_line].length + = u32_strlen(state->input_buffer[first_line].text); if (!forward) { state->input_row--; if (state->input_row < 0) state->input_row = 0; } - for (size_t i = first_line+1; i < - state->input_rows_count-1; i++) + for (size_t i = first_line + 1; i < state->input_rows_count - 1; i++) { u32_strncpy(state->input_buffer[i].text, - state->input_buffer[i+1].text, MAXBUFLINE); - state->input_buffer[i].length = state->input_buffer[i+1].length; + state->input_buffer[i + 1].text, MAXBUFLINE); + state->input_buffer[i].length + = state->input_buffer[i + 1].length; } - free_bufferline(state->input_buffer+state->input_rows_count-1); + free_bufferline(state->input_buffer + state->input_rows_count - 1); state->input_rows_count--; - struct BufferLine* newbuf = realloc( - state->input_buffer, state->input_rows_count * - sizeof(struct BufferLine)); + struct BufferLine* newbuf = realloc(state->input_buffer, + state->input_rows_count * sizeof(struct BufferLine)); if (!newbuf) return NULL; state->input_buffer = newbuf; - state->dirty = 1; + state->dirty = 1; return state; } @@ -1776,22 +1759,20 @@ handle_editbox_key_event(struct tb_event* ev, struct DrawState* state) if (state->error[0]) state->error[0] = 0; - if (ev->key != TB_KEY_ARROW_DOWN && - ev->key != TB_KEY_ARROW_UP) + if (ev->key != TB_KEY_ARROW_DOWN && ev->key != TB_KEY_ARROW_UP) state->saved_column = -1; if (ev->key == TB_KEY_ARROW_DOWN) move_down(state); - else if (ev->key == TB_KEY_ARROW_LEFT ) + else if (ev->key == TB_KEY_ARROW_LEFT) move_left(state); else if (ev->key == TB_KEY_ARROW_RIGHT) move_right(state); else if (ev->key == TB_KEY_ARROW_UP) move_up(state); - else if ((ev->key == TB_KEY_BACKSPACE - || ev->key == TB_KEY_BACKSPACE2) - && !state->edit_info_focused) - erase_backwards(state); + else if ((ev->key == TB_KEY_BACKSPACE || ev->key == TB_KEY_BACKSPACE2) + && !state->edit_info_focused) + erase_backwards(state); else if (ev->key == TB_KEY_CTRL_C) yank_msgstr_to_paste_buffer(state); else if (ev->key == TB_KEY_CTRL_K) @@ -1818,8 +1799,7 @@ handle_editbox_key_event(struct tb_event* ev, struct DrawState* state) toggle_fuzzy(state); else if (ev->key == TB_KEY_DELETE && !state->edit_info_focused) erase_forward(state); - else if (ev->key == TB_KEY_END - || ev->key == TB_KEY_CTRL_E) + else if (ev->key == TB_KEY_END || ev->key == TB_KEY_CTRL_E) move_end(state); else if (ev->key == TB_KEY_ENTER) { @@ -1839,14 +1819,11 @@ handle_editbox_key_event(struct tb_event* ev, struct DrawState* state) next_untranslated(state); else if (ev->key == TB_KEY_F9) next_fuzzy(state); - else if (ev->key == TB_KEY_HOME - || ev->key == TB_KEY_CTRL_A) + else if (ev->key == TB_KEY_HOME || ev->key == TB_KEY_CTRL_A) move_start(state); - else if (ev->key == TB_KEY_PGDN - || ev->key == TB_KEY_CTRL_F) + else if (ev->key == TB_KEY_PGDN || ev->key == TB_KEY_CTRL_F) edit_page_down(state); - else if (ev->key == TB_KEY_PGUP - || ev->key == TB_KEY_CTRL_B) + else if (ev->key == TB_KEY_PGUP || ev->key == TB_KEY_CTRL_B) edit_page_up(state); else if (ev->key == TB_KEY_SPACE) insert_char(state, ' '); @@ -1916,25 +1893,25 @@ handle_key_event(struct tb_event* ev, struct DrawState* state) { if (!state->dirty) return; - int result = save_file(state->entries, state->real_msgid_count, - state->filename, state->nplurals); + int result = save_file(state->entries, state->real_msgid_count, + state->filename, state->nplurals); switch (result) { - case SAVE_ERR_CANT_ALLOC: - strcpy(state->error, errors[ERR_CANT_ALLOC]); - state->running = 0; - break; - case SAVE_ERR_CANT_OPEN_FILE: - strcpy(state->error, errors[ERR_CANT_SAVE]); - state->running = 0; - break; - case SAVE_ERR_CANT_MOVE_FILE: - strcpy(state->error, strerror(errno)); - state->running = 0; - break; - case SAVE_ERR_NONE: - default: - state->error[0] = 0; + case SAVE_ERR_CANT_ALLOC: + strcpy(state->error, errors[ERR_CANT_ALLOC]); + state->running = 0; + break; + case SAVE_ERR_CANT_OPEN_FILE: + strcpy(state->error, errors[ERR_CANT_SAVE]); + state->running = 0; + break; + case SAVE_ERR_CANT_MOVE_FILE: + strcpy(state->error, strerror(errno)); + state->running = 0; + break; + case SAVE_ERR_NONE: + default: + state->error[0] = 0; } state->dirty = 0; } @@ -1955,7 +1932,7 @@ handle_key_event(struct tb_event* ev, struct DrawState* state) else if (ev->key == TB_KEY_F1 || ev->ch == 'H') { state->show_help = 1; - state->error[0] = 0; + state->error[0] = 0; } else if (ev->key == TB_KEY_F3 || ev->ch == 'n') next_match(state); @@ -1973,18 +1950,18 @@ handle_key_event(struct tb_event* ev, struct DrawState* state) strcpy(state->error, errors[ERR_UNKNOWN_KEY]); } -void +void handle_search_key_event(struct tb_event* ev, struct DrawState* state) { if (!state) return; - if (ev->key == TB_KEY_ARROW_LEFT ) + if (ev->key == TB_KEY_ARROW_LEFT) move_search_left(state); else if (ev->key == TB_KEY_ARROW_RIGHT) move_search_right(state); else if ((ev->key == TB_KEY_BACKSPACE || ev->key == TB_KEY_BACKSPACE2)) - erase_search_backwards(state); + erase_search_backwards(state); else if (ev->key == TB_KEY_CTRL_K) erase_search_to_end(state); else if (ev->key == TB_KEY_CTRL_R) @@ -2035,16 +2012,14 @@ main(int argc, char** argv) if (argc == 1) return print_error(1, "No filename given"); - if (!strcmp(argv[1], "-h") - || !strcmp(argv[1], "--help")) + if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) return usage(); - else if (!strcmp(argv[1], "-v") - || !strcmp(argv[1], "--version")) + else if (!strcmp(argv[1], "-v") || !strcmp(argv[1], "--version")) return version(); else { - strncpy(filename, argv[1], MAXPATH-1); - filename[MAXPATH-1] = 0; + strncpy(filename, argv[1], MAXPATH - 1); + filename[MAXPATH - 1] = 0; if (!filename[0]) print_error(EINVAL, "Invalid argument"); } @@ -2059,33 +2034,30 @@ main(int argc, char** argv) init_drawstate(&state, error, filename, prompt); long lineno = 1; - long col = 1; - int result = load_file(&state, &lineno, &col); + long col = 1; + int result = load_file(&state, &lineno, &col); switch (result) { - case LOAD_ERR_CANT_ALLOC: - case LOAD_ERR_P_ALLOC: - return print_error(result, - "Allocation failed (out of memory?)"); - case LOAD_ERR_CANT_OPEN_FILE: - return print_error(result, "Cannot open file `%s'", - filename); - case LOAD_ERR_NOT_PO_FILE: - return print_error(result, "Not a .po file `%s'", - filename); - case LOAD_ERR_P_INVAL: - return print_error(result, "Invalid value"); - case LOAD_ERR_P_SYNTAX: - return print_error(result, "%s:%ld:%ld: Syntax error", - filename, lineno, col); - case LOAD_ERR_NONE: - if (error[0] != 0) - return print_error(1, error); - break; - default: - return print_error(1, "Unknown error (%d)", result); - } - state.msgid_number = state.msgid_count > 0 ? 1 : 0; + case LOAD_ERR_CANT_ALLOC: + case LOAD_ERR_P_ALLOC: + return print_error(result, "Allocation failed (out of memory?)"); + case LOAD_ERR_CANT_OPEN_FILE: + return print_error(result, "Cannot open file `%s'", filename); + case LOAD_ERR_NOT_PO_FILE: + return print_error(result, "Not a .po file `%s'", filename); + case LOAD_ERR_P_INVAL: + return print_error(result, "Invalid value"); + case LOAD_ERR_P_SYNTAX: + return print_error(result, "%s:%ld:%ld: Syntax error", filename, + lineno, col); + case LOAD_ERR_NONE: + if (error[0] != 0) + return print_error(1, error); + break; + default: + return print_error(1, "Unknown error (%d)", result); + } + state.msgid_number = state.msgid_count > 0 ? 1 : 0; state.first_shown_msgid = state.msgid_number; tb_init(); @@ -2105,7 +2077,7 @@ main(int argc, char** argv) if (state.running) { sleep_spec.tv_nsec = 500; - sleep_spec.tv_sec = 0; + sleep_spec.tv_sec = 0; nanosleep(&sleep_spec, NULL); } } diff --git a/util.c b/util.c @@ -15,8 +15,8 @@ u8_string_to_unicode(uint32_t* us, const char* s, const size_t max) { uint32_t uch; uint32_t* pus = us; - int u8_len = 0; - size_t added = 0; + int u8_len = 0; + size_t added = 0; while (s && *s && u8_len != TB_EOF) { @@ -39,7 +39,7 @@ unicode_string_to_u8(char* s, const uint32_t* us, const size_t max) { char ch[8]; const uint32_t* pus = us; - size_t added = 0; + size_t added = 0; *s = 0; while (*pus) @@ -54,15 +54,15 @@ unicode_string_to_u8(char* s, const uint32_t* us, const size_t max) return added; } -size_t +size_t u32_encode_tabs(uint32_t* s, size_t max) { - uint32_t* buf = calloc(max+1, sizeof(uint32_t)); + uint32_t* buf = calloc(max + 1, sizeof(uint32_t)); if (!buf) return 0; uint32_t* pbuf = buf; - uint32_t* ps = s; - size_t newlen = 0; + uint32_t* ps = s; + size_t newlen = 0; while (*ps) { @@ -85,19 +85,19 @@ u32_encode_tabs(uint32_t* s, size_t max) return newlen; } -size_t +size_t u32_decode_tabs(uint32_t* s, size_t max) { - uint32_t* buf = calloc(max+1, sizeof(uint32_t)); + uint32_t* buf = calloc(max + 1, sizeof(uint32_t)); if (!buf) return 0; uint32_t* pbuf = buf; - uint32_t* ps = s; - size_t newlen = 0; + uint32_t* ps = s; + size_t newlen = 0; while (*ps) { - if (*ps == '\\' && *(ps+1) == 't') + if (*ps == '\\' && *(ps + 1) == 't') { *pbuf++ = '\t'; ps += 2; @@ -119,7 +119,7 @@ size_t u32_strlen(const uint32_t* s) { const uint32_t* ps = s; - size_t len = 0; + size_t len = 0; if (!s) return 0; while (*ps++) @@ -127,7 +127,7 @@ u32_strlen(const uint32_t* s) return len; } -uint32_t* +uint32_t* u32_strstr(uint32_t* haystack, const uint32_t* needle) { uint32_t* ph = haystack; @@ -144,8 +144,8 @@ size_t u32_strncpy(uint32_t* to, const uint32_t* from, size_t max) { const uint32_t* pfrom = from; - size_t copied = 0; - while (*pfrom && copied < max-1) + size_t copied = 0; + while (*pfrom && copied < max - 1) { *to++ = *pfrom++; copied++; @@ -162,13 +162,13 @@ size_t u32_strncat(uint32_t* to, const uint32_t* from, size_t max) { const uint32_t* pfrom = from; - size_t copied = 0; + size_t copied = 0; while (*to) { to++; copied++; } - while (*pfrom && copied < max-1) + while (*pfrom && copied < max - 1) { *to++ = *pfrom++; copied++; @@ -180,7 +180,7 @@ u32_strncat(uint32_t* to, const uint32_t* from, size_t max) size_t u32_u8_strncpy(uint32_t* to, const char* from, size_t max) { - uint32_t* ufrom = calloc(strlen(from)+1, sizeof(uint32_t)); + uint32_t* ufrom = calloc(strlen(from) + 1, sizeof(uint32_t)); if (!ufrom) return 0; u8_string_to_unicode(ufrom, from, max); @@ -192,12 +192,12 @@ u32_u8_strncpy(uint32_t* to, const char* from, size_t max) size_t u8_u32_strncpy(char* to, const uint32_t* from, size_t max) { - char* cfrom = calloc(u32_strlen(from)*6+1, 1); + char* cfrom = calloc(u32_strlen(from) * 6 + 1, 1); if (!cfrom) return 0; unicode_string_to_u8(cfrom, from, max); strncpy(to, cfrom, max); - to[max-1] = 0; + to[max - 1] = 0; free(cfrom); return strlen(to); } @@ -211,7 +211,7 @@ u8_line_length(const char* s) const char* ps = s; while (*ps) { - if (*ps == '\\' && *(ps+1) == 'n') + if (*ps == '\\' && *(ps + 1) == 'n') return len; ps++; len++; @@ -228,7 +228,7 @@ u32_line_length(const uint32_t* s) const uint32_t* ps = s; while (*ps) { - if (*ps == '\\' && *(ps+1) == 'n') + if (*ps == '\\' && *(ps + 1) == 'n') return len; ps++; len++; @@ -238,7 +238,7 @@ u32_line_length(const uint32_t* s) /* actual_lines - doesn't take into account empty lines * */ -size_t +size_t u8_lines_in_string(const char* s, size_t* actual_lines) { size_t result = 1; @@ -249,16 +249,16 @@ u8_lines_in_string(const char* s, size_t* actual_lines) return result; while (*ps) { - if (*ps == '\\' && *(ps+1) == 'n') + if (*ps == '\\' && *(ps + 1) == 'n') { result++; if (actual_lines) { (*actual_lines)++; - if (*ps && *(ps+1) && - (!*(ps+2) || - (*(ps+2) == '\\' && - *(ps+3) == 'n'))) + if (*ps && *(ps + 1) + && (!*(ps + 2) + || (*(ps + 2) == '\\' + && *(ps + 3) == 'n'))) (*actual_lines)--; } ps++; @@ -270,7 +270,7 @@ u8_lines_in_string(const char* s, size_t* actual_lines) /* actual_lines - doesn't take into account empty lines * */ -size_t +size_t u32_lines_in_string(const uint32_t* s, size_t* actual_lines) { size_t result = 1; @@ -281,16 +281,16 @@ u32_lines_in_string(const uint32_t* s, size_t* actual_lines) return result; while (*ps) { - if (*ps == '\\' && *(ps+1) == 'n') + if (*ps == '\\' && *(ps + 1) == 'n') { result++; if (actual_lines) { (*actual_lines)++; - if (*ps && *(ps+1) && - (!*(ps+2) || - (*(ps+2) == '\\' && - *(ps+3) == 'n'))) + if (*ps && *(ps + 1) + && (!*(ps + 2) + || (*(ps + 2) == '\\' + && *(ps + 3) == 'n'))) (*actual_lines)--; } ps++; @@ -300,7 +300,7 @@ u32_lines_in_string(const uint32_t* s, size_t* actual_lines) return result; } -int +int starts_with(const char* s, const char* with) { while (*s && *s == *with) @@ -313,7 +313,7 @@ starts_with(const char* s, const char* with) return *with == 0; } -int +int u32_starts_with(const uint32_t* s, const uint32_t* with) { while (*s && *s == *with) diff --git a/util.h b/util.h @@ -1,9 +1,9 @@ #include <bits/stdint-uintn.h> #include <sys/types.h> -#define ABS(a) ((a < 0) ? (-a) : (a)) -#define LEN(x) (sizeof(x) / sizeof(x[0])) -#define MAX(a,b) ((a > b) ? a : b) -#define MIN(a,b) ((a < b) ? a : b) +#define ABS(a) ((a < 0) ? (-a) : (a)) +#define LEN(x) (sizeof(x) / sizeof(x[0])) +#define MAX(a, b) ((a > b) ? a : b) +#define MIN(a, b) ((a < b) ? a : b) enum { ERR_UNKNOWN_KEY, @@ -19,9 +19,9 @@ enum { #define MAXBUFLINE 4096 #define MAXDATEBUF 80 -#define MAXFLAGSBUF (4+1) -#define MAXMSG 1024 -#define MAXPATH 1024 +#define MAXFLAGSBUF (4 + 1) +#define MAXMSG 1024 +#define MAXPATH 1024 size_t u8_string_to_unicode(uint32_t* us, const char* s, const size_t max); size_t unicode_string_to_u8(char* s, const uint32_t* us, const size_t max);