From bf8038702d4260b70bcd49dfd553cdcf432846f0 Mon Sep 17 00:00:00 2001 From: Wayne Rowcliffe Date: Sat, 12 Sep 2015 23:16:17 -0500 Subject: Clean up buffer.c build_stl_str_hl variable declarations --- src/nvim/buffer.c | 192 +++++++++++++++++++++++++----------------------------- 1 file changed, 89 insertions(+), 103 deletions(-) (limited to 'src') diff --git a/src/nvim/buffer.c b/src/nvim/buffer.c index 10106bcb1d..5306602949 100644 --- a/src/nvim/buffer.c +++ b/src/nvim/buffer.c @@ -2831,27 +2831,7 @@ build_stl_str_hl ( struct stl_hlrec *tabtab /* return: tab page nrs (can be NULL) */ ) { - char_u *p; - char_u *s; - char_u *t; - int byteval; - win_T *o_curwin; - buf_T *o_curbuf; - int empty_line; - colnr_T virtcol; - long l; - long n; - int prevchar_isflag; - int prevchar_isitem; - int itemisflag; - int fillable; - char_u *str; - long num; - int width; - int itemcnt; - int curitem; int groupitem[STL_MAX_ITEM]; - int groupdepth; struct stl_item { char_u *start; int minwid; @@ -2866,15 +2846,9 @@ build_stl_str_hl ( Trunc } type; } item[STL_MAX_ITEM]; - int minwid; - int maxwid; - int zeropad; - char_u base; - char_u opt; #define TMPLEN 70 char_u tmp[TMPLEN]; char_u *usefmt = fmt; - struct stl_hlrec *sp; /* * When the format starts with "%!" then evaluate it as an expression and @@ -2892,23 +2866,24 @@ build_stl_str_hl ( else if (mb_char2len(fillchar) > 1) fillchar = '-'; - /* Get line & check if empty (cursorpos will show "0-1"). Note that - * p will become invalid when getting another buffer line. */ - p = ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE); - empty_line = (*p == NUL); + /* Get line & check if empty (cursorpos will show "0-1"). */ + char_u *line_ptr = ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, false); + bool empty_line = (*line_ptr == NUL); /* Get the byte value now, in case we need it below. This is more * efficient than making a copy of the line. */ - if (wp->w_cursor.col > (colnr_T)STRLEN(p)) + int byteval; + if (wp->w_cursor.col > (colnr_T)STRLEN(line_ptr)) byteval = 0; else - byteval = (*mb_ptr2char)(p + wp->w_cursor.col); - - groupdepth = 0; - p = out; - curitem = 0; - prevchar_isflag = TRUE; - prevchar_isitem = FALSE; + byteval = (*mb_ptr2char)(line_ptr + wp->w_cursor.col); + + int groupdepth = 0; + char_u *p = out; + int curitem = 0; + bool prevchar_isflag = true; + bool prevchar_isitem = false; + char_u *s; for (s = usefmt; *s; ) { if (curitem == STL_MAX_ITEM) { /* There are too many items. Add the error code to the statusline @@ -2921,7 +2896,7 @@ build_stl_str_hl ( } if (*s != NUL && *s != '%') - prevchar_isflag = prevchar_isitem = FALSE; + prevchar_isflag = prevchar_isitem = false; /* * Handle up to the next '%' or the end. @@ -2941,7 +2916,7 @@ build_stl_str_hl ( if (p + 1 >= out + outlen) break; *p++ = *s++; - prevchar_isflag = prevchar_isitem = FALSE; + prevchar_isflag = prevchar_isitem = false; continue; } if (*s == STL_MIDDLEMARK) { @@ -2964,12 +2939,13 @@ build_stl_str_hl ( continue; groupdepth--; - t = item[groupitem[groupdepth]].start; + char_u *t = item[groupitem[groupdepth]].start; *p = NUL; - l = vim_strsize(t); + long l = vim_strsize(t); if (curitem > groupitem[groupdepth] + 1 && item[groupitem[groupdepth]].minwid == 0) { /* remove group if all items are empty */ + long n; for (n = groupitem[groupdepth] + 1; n < curitem; n++) if (item[n].type == Normal) break; @@ -2980,6 +2956,7 @@ build_stl_str_hl ( } if (l > item[groupitem[groupdepth]].maxwid) { /* truncate, remove n bytes of text at the start */ + long n; if (has_mbyte) { /* Find the first character that should be included. */ n = 0; @@ -3005,7 +2982,7 @@ build_stl_str_hl ( } } else if (abs(item[groupitem[groupdepth]].minwid) > l) { /* fill */ - n = item[groupitem[groupdepth]].minwid; + long n = item[groupitem[groupdepth]].minwid; if (n < 0) { /* fill by appending characters */ n = 0 - n; @@ -3026,13 +3003,13 @@ build_stl_str_hl ( } continue; } - minwid = 0; - maxwid = 9999; - zeropad = FALSE; - l = 1; + int minwid = 0; + int maxwid = 9999; + bool zeropad = false; + long l = 1; if (*s == '0') { s++; - zeropad = TRUE; + zeropad = true; } if (*s == '-') { s++; @@ -3056,7 +3033,7 @@ build_stl_str_hl ( if (minwid == 0) { /* %X ends the close label, go back to the previously * define tab label nr. */ - for (n = curitem - 1; n >= 0; --n) + for (long n = curitem - 1; n >= 0; --n) if (item[n].type == TabPage && item[n].minwid >= 0) { minwid = item[n].minwid; break; @@ -3095,25 +3072,26 @@ build_stl_str_hl ( s++; continue; } - opt = *s++; + char_u opt = *s++; /* OK - now for the real work */ - base = 'D'; - itemisflag = FALSE; - fillable = TRUE; - num = -1; - str = NULL; + char_u base = 'D'; + bool itemisflag = false; + bool fillable = true; + long num = -1; + char_u *str = NULL; switch (opt) { case STL_FILEPATH: case STL_FULLPATH: case STL_FILENAME: - fillable = FALSE; /* don't change ' ' to fillchar */ - if (buf_spname(wp->w_buffer) != NULL) + { + fillable = false; /* don't change ' ' to fillchar */ + if (buf_spname(wp->w_buffer) != NULL) { STRLCPY(NameBuff, buf_spname(wp->w_buffer), MAXPATHL); - else { - t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname - : wp->w_buffer->b_fname; - home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE); + } else { + char_u *t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname + : wp->w_buffer->b_fname; + home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, true); } trans_characters(NameBuff, MAXPATHL); if (opt != STL_FILENAME) @@ -3121,10 +3099,11 @@ build_stl_str_hl ( else str = path_tail(NameBuff); break; - + } case STL_VIM_EXPR: /* '{' */ - itemisflag = TRUE; - t = p; + { + itemisflag = true; + char_u *t = p; while (*s != '}' && *s != NUL && p + 1 < out + outlen) *p++ = *s++; if (*s != '}') /* missing '}' or out of space */ @@ -3136,8 +3115,8 @@ build_stl_str_hl ( vim_snprintf((char *)tmp, sizeof(tmp), "%d", curbuf->b_fnum); set_internal_string_var((char_u *)"actual_curbuf", tmp); - o_curbuf = curbuf; - o_curwin = curwin; + buf_T *o_curbuf = curbuf; + win_T *o_curwin = curwin; curwin = wp; curbuf = wp->w_buffer; @@ -3152,11 +3131,11 @@ build_stl_str_hl ( num = atoi((char *)str); xfree(str); str = NULL; - itemisflag = FALSE; + itemisflag = false; } } break; - + } case STL_LINE: num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) ? 0L : (long)(wp->w_cursor.lnum); @@ -3173,8 +3152,9 @@ build_stl_str_hl ( case STL_VIRTCOL: case STL_VIRTCOL_ALT: + { /* In list mode virtcol needs to be recomputed */ - virtcol = wp->w_virtcol; + colnr_T virtcol = wp->w_virtcol; if (wp->w_p_list && lcs_tab1 == NUL) { wp->w_p_list = FALSE; getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL); @@ -3188,6 +3168,7 @@ build_stl_str_hl ( break; num = (long)virtcol; break; + } case STL_PERCENTAGE: num = (int)(((long)wp->w_cursor.lnum * 100L) / @@ -3200,14 +3181,14 @@ build_stl_str_hl ( break; case STL_ARGLISTSTAT: - fillable = FALSE; + fillable = false; tmp[0] = 0; if (append_arg_number(wp, tmp, (int)sizeof(tmp), FALSE)) str = tmp; break; case STL_KEYMAP: - fillable = FALSE; + fillable = false; if (get_keymap_str(wp, tmp, TMPLEN)) str = tmp; break; @@ -3240,14 +3221,14 @@ build_stl_str_hl ( case STL_ROFLAG: case STL_ROFLAG_ALT: - itemisflag = TRUE; + itemisflag = true; if (wp->w_buffer->b_p_ro) str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : _("[RO]")); break; case STL_HELPFLAG: case STL_HELPFLAG_ALT: - itemisflag = TRUE; + itemisflag = true; if (wp->w_buffer->b_help) str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP" : _("[Help]")); @@ -3263,20 +3244,21 @@ build_stl_str_hl ( break; case STL_FILETYPE_ALT: - itemisflag = TRUE; + { + itemisflag = true; if (*wp->w_buffer->b_p_ft != NUL && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2) { vim_snprintf((char *)tmp, sizeof(tmp), ",%s", wp->w_buffer->b_p_ft); - for (t = tmp; *t != 0; t++) + for (char_u *t = tmp; *t != 0; t++) *t = TOUPPER_LOC(*t); str = tmp; } break; - + } case STL_PREVIEWFLAG: case STL_PREVIEWFLAG_ALT: - itemisflag = TRUE; + itemisflag = true; if (wp->w_p_pvw) str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV" : _("[Preview]")); @@ -3291,7 +3273,7 @@ build_stl_str_hl ( case STL_MODIFIED: case STL_MODIFIED_ALT: - itemisflag = TRUE; + itemisflag = true; switch ((opt == STL_MODIFIED_ALT) + bufIsChanged(wp->w_buffer) * 2 + (!MODIFIABLE(wp->w_buffer)) * 4) { @@ -3305,7 +3287,8 @@ build_stl_str_hl ( break; case STL_HIGHLIGHT: - t = s; + { + char_u *t = s; while (*s != '#' && *s != NUL) ++s; if (*s == '#') { @@ -3318,21 +3301,22 @@ build_stl_str_hl ( ++s; continue; } + } item[curitem].start = p; item[curitem].type = Normal; if (str != NULL && *str) { - t = str; + char_u *t = str; if (itemisflag) { if ((t[0] && t[1]) && ((!prevchar_isitem && *t == ',') || (prevchar_isflag && *t == ' '))) t++; - prevchar_isflag = TRUE; + prevchar_isflag = true; } l = vim_strsize(t); if (l > 0) - prevchar_isitem = TRUE; + prevchar_isitem = true; if (l > maxwid) { while (l >= maxwid) if (has_mbyte) { @@ -3371,8 +3355,8 @@ build_stl_str_hl ( if (p + 20 >= out + outlen) break; /* not sufficient space */ - prevchar_isitem = TRUE; - t = nstr; + prevchar_isitem = true; + char_u *t = nstr; if (opt == STL_VIRTCOL_ALT) { *t++ = '-'; minwid--; @@ -3384,6 +3368,7 @@ build_stl_str_hl ( *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd'); *t = 0; + long n; for (n = num, l = 1; n >= nbase; n /= nbase) l++; if (opt == STL_VIRTCOL_ALT) @@ -3410,19 +3395,19 @@ build_stl_str_hl ( xfree(str); if (num >= 0 || (!itemisflag && str && *str)) - prevchar_isflag = FALSE; /* Item not NULL, but not a flag */ + prevchar_isflag = false; /* Item not NULL, but not a flag */ curitem++; } *p = NUL; - itemcnt = curitem; + int itemcnt = curitem; if (usefmt != fmt) xfree(usefmt); - width = vim_strsize(out); + int width = vim_strsize(out); if (maxwidth > 0 && width > maxwidth) { /* Result is too long, must truncate somewhere. */ - l = 0; + long l = 0; if (itemcnt == 0) s = out; else { @@ -3462,6 +3447,7 @@ build_stl_str_hl ( *s++ = '>'; *s = 0; } else { + long n; if (has_mbyte) { n = 0; while (width >= maxwidth) { @@ -3493,24 +3479,24 @@ build_stl_str_hl ( } else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen) { /* Apply STL_MIDDLE if any */ - for (l = 0; l < itemcnt; l++) - if (item[l].type == Middle) + for (long l = 0; l < itemcnt; l++) + if (item[l].type == Middle) { + p = item[l].start + maxwidth - width; + STRMOVE(p, item[l].start); + for (s = item[l].start; s < p; s++) + *s = fillchar; + for (l++; l < itemcnt; l++) + item[l].start += maxwidth - width; + width = maxwidth; + break; - if (l < itemcnt) { - p = item[l].start + maxwidth - width; - STRMOVE(p, item[l].start); - for (s = item[l].start; s < p; s++) - *s = fillchar; - for (l++; l < itemcnt; l++) - item[l].start += maxwidth - width; - width = maxwidth; - } + } } /* Store the info about highlighting. */ if (hltab != NULL) { - sp = hltab; - for (l = 0; l < itemcnt; l++) { + struct stl_hlrec *sp = hltab; + for (long l = 0; l < itemcnt; l++) { if (item[l].type == Highlight) { sp->start = item[l].start; sp->userhl = item[l].minwid; @@ -3523,8 +3509,8 @@ build_stl_str_hl ( /* Store the info about tab pages labels. */ if (tabtab != NULL) { - sp = tabtab; - for (l = 0; l < itemcnt; l++) { + struct stl_hlrec *sp = tabtab; + for (long l = 0; l < itemcnt; l++) { if (item[l].type == TabPage) { sp->start = item[l].start; sp->userhl = item[l].minwid; -- cgit From cde2259a6babcbee2d2ee5ce7bbba733113ea512 Mon Sep 17 00:00:00 2001 From: Wayne Rowcliffe Date: Tue, 15 Sep 2015 02:06:02 -0500 Subject: Add comments to build_stl_str_hl --- src/nvim/buffer.c | 517 +++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 391 insertions(+), 126 deletions(-) (limited to 'src') diff --git a/src/nvim/buffer.c b/src/nvim/buffer.c index 5306602949..95a1f783eb 100644 --- a/src/nvim/buffer.c +++ b/src/nvim/buffer.c @@ -2818,7 +2818,7 @@ void free_titles(void) * If maxwidth is not zero, the string will be filled at any middle marker * or truncated if too long, fillchar is used for all whitespace. */ -int +int build_stl_str_hl ( win_T *wp, char_u *out, /* buffer to write into != NameBuff */ @@ -2833,8 +2833,11 @@ build_stl_str_hl ( { int groupitem[STL_MAX_ITEM]; struct stl_item { + // Where the item starts in the status line output buffer char_u *start; + // The minimum width of the item (at most 50 characters) int minwid; + // The maximum width of the item int maxwid; enum { Normal, @@ -2846,6 +2849,13 @@ build_stl_str_hl ( Trunc } type; } item[STL_MAX_ITEM]; + + enum number_base { + DECIMAL = 10, + OCTAL = 8, + HEXIDECIMAL = 16 + }; + #define TMPLEN 70 char_u tmp[TMPLEN]; char_u *usefmt = fmt; @@ -2879,12 +2889,18 @@ build_stl_str_hl ( byteval = (*mb_ptr2char)(line_ptr + wp->w_cursor.col); int groupdepth = 0; - char_u *p = out; + int curitem = 0; bool prevchar_isflag = true; bool prevchar_isitem = false; - char_u *s; - for (s = usefmt; *s; ) { + + // p is the current position in the output buffer + char_u *p = out; + + + // Proceed character by character through the statusline format string + // s is the current positon in the input buffer + for (char_u *s = usefmt; *s; ) { if (curitem == STL_MAX_ITEM) { /* There are too many items. Add the error code to the statusline * to give the user a hint about what went wrong. */ @@ -2898,64 +2914,99 @@ build_stl_str_hl ( if (*s != NUL && *s != '%') prevchar_isflag = prevchar_isitem = false; - /* - * Handle up to the next '%' or the end. - */ + // Copy the formatting verbatim until we reach the end of the string + // or find a formatting item (denoted by `%`) + // or run out of room in our output buffer. while (*s != NUL && *s != '%' && p + 1 < out + outlen) *p++ = *s++; + + // If we have processed the entire format string or run out of + // room in our output buffer, exit the loop. if (*s == NUL || p + 1 >= out + outlen) break; - /* - * Handle one '%' item. - */ + // The rest of this loop wil handle a single `%` item. + // Note: We increment here to skip over the `%` character we are currently + // on so we can process the item's contents. s++; - if (*s == NUL) /* ignore trailing % */ + + // Ignore `%` at the end of the format string + if (*s == NUL) { break; + } + + // Two `%` in a row is the escape sequence to print a + // single `%` in the output buffer. if (*s == '%') { + // Ignore the character if we're out of room in the output buffer. if (p + 1 >= out + outlen) break; *p++ = *s++; prevchar_isflag = prevchar_isitem = false; continue; } + + // STL_MIDDLEMARK denotes the separation place between left and right aligned items. if (*s == STL_MIDDLEMARK) { s++; - if (groupdepth > 0) + // Ignored when we are inside of a grouping + if (groupdepth > 0) { continue; + } item[curitem].type = Middle; item[curitem++].start = p; continue; } + + // STL_TRUNCMARK denotes where to begin truncating if the statusline is too long. if (*s == STL_TRUNCMARK) { s++; item[curitem].type = Trunc; item[curitem++].start = p; continue; } + + // The end of a grouping if (*s == ')') { s++; - if (groupdepth < 1) + // Ignore if we are not actually inside a group currently + if (groupdepth < 1) { continue; + } groupdepth--; + //{ Determine how long the group is. + // Note: We set the current output position to null so `vim_strsize` will work. char_u *t = item[groupitem[groupdepth]].start; *p = NUL; long l = vim_strsize(t); + //} + + // If the group contained internal items and the group did not have a minimum width, + // and if there were no normal items in the group, + // move the output pointer back to where the group started (erase the group). + // Note: This also erases any plaintext characters that were in the group. + // Otherwise there would be no reason to do this step. if (curitem > groupitem[groupdepth] + 1 && item[groupitem[groupdepth]].minwid == 0) { - /* remove group if all items are empty */ - long n; - for (n = groupitem[groupdepth] + 1; n < curitem; n++) - if (item[n].type == Normal) + bool has_normal_items = false; + for (long n = groupitem[groupdepth] + 1; n < curitem; n++) { + if (item[n].type == Normal) { + has_normal_items = true; break; - if (n == curitem) { + } + } + + if (!has_normal_items) { p = t; l = 0; } } + + // If the group is longer than it is allowed to be + // truncate by removing bytes from the start of the group text. if (l > item[groupitem[groupdepth]].maxwid) { - /* truncate, remove n bytes of text at the start */ + //{ Determine the number of bytes to remove long n; if (has_mbyte) { /* Find the first character that should be included. */ @@ -2966,60 +3017,88 @@ build_stl_str_hl ( } } else n = (long)(p - t) - item[groupitem[groupdepth]].maxwid + 1; + //} + // Prepend the `<` to indicate that the output was truncated. *t = '<'; + + //{ Move the truncated output memmove(t + 1, t + n, (size_t)(p - (t + n))); p = p - n + 1; /* Fill up space left over by half a double-wide char. */ while (++l < item[groupitem[groupdepth]].minwid) *p++ = fillchar; + //} /* correct the start of the items for the truncation */ for (l = groupitem[groupdepth] + 1; l < curitem; l++) { + // Shift everything back by the number of removed bytes item[l].start -= n; + + // If the item was partially or completely truncated, set its + // start to the start of the group if (item[l].start < t) item[l].start = t; } + // If the group is shorter than the minimum width, add padding characters. } else if (abs(item[groupitem[groupdepth]].minwid) > l) { - /* fill */ - long n = item[groupitem[groupdepth]].minwid; - if (n < 0) { - /* fill by appending characters */ - n = 0 - n; - while (l++ < n && p + 1 < out + outlen) + long min_group_width = item[groupitem[groupdepth]].minwid; + // If the group is left-aligned, add characters to the right. + if (min_group_width < 0) { + min_group_width = 0 - min_group_width; + while (l++ < min_group_width && p + 1 < out + outlen) *p++ = fillchar; + // If the group is right-aligned, shift everything to the right and + // prepend with filler characters. } else { - /* fill by inserting characters */ - memmove(t + n - l, t, (size_t)(p - t)); - l = n - l; - if (p + l >= out + outlen) + //{ Move the group to the right + memmove(t + min_group_width - l, t, (size_t)(p - t)); + l = min_group_width - l; + if (p + l >= out + outlen) { l = (long)((out + outlen) - p - 1); + } p += l; - for (n = groupitem[groupdepth] + 1; n < curitem; n++) + //} + + // Adjust item start positions + for (int n = groupitem[groupdepth] + 1; n < curitem; n++) { item[n].start += l; - for (; l > 0; l--) + } + + // Prepend the fill characters + for (; l > 0; l--) { *t++ = fillchar; + } } } continue; } int minwid = 0; int maxwid = 9999; - bool zeropad = false; - long l = 1; - if (*s == '0') { + bool left_align = false; + + // Denotes that numbers should be left-padded with zeros + bool zeropad = (*s == '0'); + if (zeropad) { s++; - zeropad = true; } + + // Denotes that the item should be left-aligned. + // This is tracked by using a negative length. if (*s == '-') { s++; - l = -1; + left_align = true; } + + // The first digit group is the item's min width if (ascii_isdigit(*s)) { minwid = getdigits_int(&s); if (minwid < 0) /* overflow */ minwid = 0; } + + // User highlight groups override the min width field + // to denote the styling to use. if (*s == STL_USER_HL) { item[curitem].type = Highlight; item[curitem].start = p; @@ -3049,6 +3128,9 @@ build_stl_str_hl ( curitem++; continue; } + + // Denotes the end of the minwid + // the maxwid may follow immediately after if (*s == '.') { s++; if (ascii_isdigit(*s)) { @@ -3057,7 +3139,12 @@ build_stl_str_hl ( maxwid = 50; } } - minwid = (minwid > 50 ? 50 : minwid) * l; + + // Bound the minimum width at 50. + // Make the number negative to denote left alignment of the item + minwid = (minwid > 50 ? 50 : minwid) * (left_align ? -1 : 1); + + // Denotes the start of a new group if (*s == '(') { groupitem[groupdepth++] = curitem; item[curitem].type = Group; @@ -3068,14 +3155,19 @@ build_stl_str_hl ( curitem++; continue; } + + // An invalid item was specified. + // Continue processing on the next character of the format string. if (vim_strchr(STL_ALL, *s) == NULL) { s++; continue; } + + // The status line item type char_u opt = *s++; /* OK - now for the real work */ - char_u base = 'D'; + enum number_base base = DECIMAL; bool itemisflag = false; bool fillable = true; long num = -1; @@ -3103,6 +3195,9 @@ build_stl_str_hl ( case STL_VIM_EXPR: /* '{' */ { itemisflag = true; + + // Attempt to copy the expression to evaluate into + // the output buffer as a null-terminated string. char_u *t = p; while (*s != '}' && *s != NUL && p + 1 < out + outlen) *p++ = *s++; @@ -3110,22 +3205,37 @@ build_stl_str_hl ( break; s++; *p = 0; + + // Move our position in the output buffer to the beginning of the expression p = t; + //{ Evaluate the expression + + // Store the current buffer number as a string variable vim_snprintf((char *)tmp, sizeof(tmp), "%d", curbuf->b_fnum); set_internal_string_var((char_u *)"actual_curbuf", tmp); + // Switch the curbuf and curwin to the buffer and window we are evaluating the + // statusline for. buf_T *o_curbuf = curbuf; win_T *o_curwin = curwin; curwin = wp; curbuf = wp->w_buffer; + // Note: The result stored in `t` is unused. str = eval_to_string_safe(p, &t, use_sandbox); + // Switch back to the actual current buffer and window. curwin = o_curwin; curbuf = o_curbuf; + + // Remove the variable we just stored do_unlet((char_u *)"g:actual_curbuf", TRUE); + //} + + // Check if the evaluated result is a number. + // If so, convert the number to an int and free the string. if (str != NULL && *str != 0) { if (*skipdigits(str) == NUL) { num = atoi((char *)str); @@ -3176,15 +3286,27 @@ build_stl_str_hl ( break; case STL_ALTPERCENT: + // Store the position percentage in our temporary buffer. + // Note: We cannot store the value in `num` because + // `get_rel_pos` can return a named position. Ex: "Top" + get_rel_pos(wp, tmp, TMPLEN); str = tmp; - get_rel_pos(wp, str, TMPLEN); break; case STL_ARGLISTSTAT: fillable = false; + + // Note: This is important because `append_arg_number` starts appending + // at the end of the null-terminated string. + // Setting the first byte to null means it will place the argument + // number string at the beginning of the buffer. tmp[0] = 0; - if (append_arg_number(wp, tmp, (int)sizeof(tmp), FALSE)) + + // Note: The call will only return true if it actually + // appended data to the `tmp` buffer. + if (append_arg_number(wp, tmp, (int)sizeof(tmp), FALSE)) { str = tmp; + } break; case STL_KEYMAP: @@ -3201,16 +3323,17 @@ build_stl_str_hl ( break; case STL_OFFSET_X: - base = 'X'; + base = HEXIDECIMAL; case STL_OFFSET: - l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL); + { + long l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL); num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ? 0L : l + 1 + (!(State & INSERT) && empty_line ? 0 : (int)wp->w_cursor.col); break; - + } case STL_BYTEVAL_X: - base = 'X'; + base = HEXIDECIMAL; case STL_BYTEVAL: num = byteval; if (num == NL) @@ -3235,6 +3358,8 @@ build_stl_str_hl ( break; case STL_FILETYPE: + // Copy the filetype if it is not null and the formatted string will fit + // in the temporary buffer (including the brackets and null terminating character) if (*wp->w_buffer->b_p_ft != NUL && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3) { vim_snprintf((char *)tmp, sizeof(tmp), "[%s]", @@ -3246,12 +3371,16 @@ build_stl_str_hl ( case STL_FILETYPE_ALT: { itemisflag = true; + // Copy the filetype if it is not null and the formatted string will fit + // in the temporary buffer (including the comma and null terminating character) if (*wp->w_buffer->b_p_ft != NUL && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2) { vim_snprintf((char *)tmp, sizeof(tmp), ",%s", wp->w_buffer->b_p_ft); - for (char_u *t = tmp; *t != 0; t++) + // Uppercase the file extension + for (char_u *t = tmp; *t != 0; t++) { *t = TOUPPER_LOC(*t); + } str = tmp; } break; @@ -3288,24 +3417,35 @@ build_stl_str_hl ( case STL_HIGHLIGHT: { + //{ The name of the highlight is surrounded by `#` char_u *t = s; - while (*s != '#' && *s != NUL) + while (*s != '#' && *s != NUL) { ++s; + } + //} + + // Create a highlight item based on the name if (*s == '#') { item[curitem].type = Highlight; item[curitem].start = p; item[curitem].minwid = -syn_namen2id(t, (int)(s - t)); curitem++; + s++; } - if (*s != NUL) - ++s; continue; } } + // If we made it this far, the item is normal and starts at + // our current position in the output buffer. + // Non-normal items would have `continued`. item[curitem].start = p; item[curitem].type = Normal; + + // Copy the item string into the output buffer if (str != NULL && *str) { + //{ Skip the leading `,` or ` ` if the item is a flag + // and the proper conditions are met char_u *t = str; if (itemisflag) { if ((t[0] && t[1]) @@ -3314,20 +3454,35 @@ build_stl_str_hl ( t++; prevchar_isflag = true; } - l = vim_strsize(t); - if (l > 0) + //} + + long l = vim_strsize(t); + + // If this item is non-empty, record that the last thing + // we put in the output buffer was an item + if (l > 0) { prevchar_isitem = true; + } + + // If the item is too wide, truncate it from the beginning if (l > maxwid) { while (l >= maxwid) if (has_mbyte) { l -= ptr2cells(t); t += (*mb_ptr2len)(t); - } else + } else { l -= byte2cells(*t++); + } + + // Early out if there isn't enough room for the truncation marker if (p + 1 >= out + outlen) break; + + // Add the truncation marker *p++ = '<'; } + + // If the item is right aligned and not wide enough, pad with fill characters. if (minwid > 0) { for (; l < minwid && p + 1 < out + outlen; l++) { /* Don't put a "-" in front of a digit. */ @@ -3337,8 +3492,13 @@ build_stl_str_hl ( *p++ = fillchar; } minwid = 0; - } else + } else { + // Note: The negative value denotes a left aligned item. + // Here we switch the minimum width back to a positive value. minwid *= -1; + } + + //{ Copy the string text into the output buffer while (*t && p + 1 < out + outlen) { *p++ = *t++; /* Change a space by fillchar, unless fillchar is '-' and a @@ -3347,150 +3507,255 @@ build_stl_str_hl ( && (!ascii_isdigit(*t) || fillchar != '-')) p[-1] = fillchar; } - for (; l < minwid && p + 1 < out + outlen; l++) + //} + + // For left-aligned items, fill any remaining space with the fillchar + for (; l < minwid && p + 1 < out + outlen; l++) { *p++ = fillchar; - } else if (num >= 0) { - int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16)); - char_u nstr[20]; + } + // Otherwise if the item is a number, copy that to the output buffer. + } else if (num >= 0) { if (p + 20 >= out + outlen) break; /* not sufficient space */ prevchar_isitem = true; + + //{ Build the formatting string + char_u nstr[20]; char_u *t = nstr; if (opt == STL_VIRTCOL_ALT) { *t++ = '-'; minwid--; } *t++ = '%'; - if (zeropad) + if (zeropad) { *t++ = '0'; + } + + // Note: The `*` means we take the width as one of the arguments *t++ = '*'; - *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd'); + *t++ = (char_u) (base == HEXIDECIMAL ? 'X' : (base == OCTAL ? 'o' : 'd')); *t = 0; + //} - long n; - for (n = num, l = 1; n >= nbase; n /= nbase) + //{ Determine how many characters the number will take up when printed + long l; + for (long n = num, l = 1; n >= base; n /= base) { l++; - if (opt == STL_VIRTCOL_ALT) + } + + // VIRTCOL_ALT takes up an extra character because of the `-` we added above. + if (opt == STL_VIRTCOL_ALT) { l++; + } + //} + + size_t remaining_buf_len = (outlen - (p - out)); + + // If the number is going to take up too much room + // Figure out the approximate number in "scientific" type notation. + // Ex: 14532 with maxwid of 4 -> '14>3' if (l > maxwid) { + // Add two to the width because the power piece will take two extra characters l += 2; - n = l - maxwid; - while (l-- > maxwid) - num /= nbase; + + // How many extra characters there are + long n = l - maxwid; + + //{ Reduce the number by base^n + while (l-- > maxwid) { + num /= base; + } + //} + + //{ Add the format string for the exponent bit *t++ = '>'; *t++ = '%'; + // Use the same base as the first number *t = t[-3]; *++t = 0; - vim_snprintf((char *)p, outlen - (p - out), (char *)nstr, + //} + + vim_snprintf((char *)p, remaining_buf_len, (char *)nstr, 0, num, n); - } else - vim_snprintf((char *)p, outlen - (p - out), (char *)nstr, + } else { + vim_snprintf((char *)p, remaining_buf_len, (char *)nstr, minwid, num); + } + + // Advance the output buffer position to the end of the number we just printed p += STRLEN(p); - } else + + // Otherwise, there was nothing to print so mark the item as empty + } else { item[curitem].type = Empty; + } - if (opt == STL_VIM_EXPR) + // Free the string buffer if we allocated it. + // Note: This is not needed if `str` is pointing at `tmp` + if (opt == STL_VIM_EXPR) { xfree(str); + } if (num >= 0 || (!itemisflag && str && *str)) prevchar_isflag = false; /* Item not NULL, but not a flag */ + + // Item processed, move to the next curitem++; } + + // Null terminate the output buffer *p = NUL; int itemcnt = curitem; - if (usefmt != fmt) + // Free the format buffer if we allocated it internally + if (usefmt != fmt) { xfree(usefmt); + } int width = vim_strsize(out); if (maxwidth > 0 && width > maxwidth) { /* Result is too long, must truncate somewhere. */ - long l = 0; - if (itemcnt == 0) - s = out; - else { - for (; l < itemcnt; l++) - if (item[l].type == Trunc) { - /* Truncate at %< item. */ - s = item[l].start; + int item_idx = 0; + char_u *trunc_p; + + // If there are no items, truncate from beginning + if (itemcnt == 0) { + trunc_p = out; + + // Otherwise, look for the truncation item + } else { + // Default to truncating at the first item + trunc_p = item[0].start; + item_idx = 0; + + for (int i; i < itemcnt; i++) + if (item[i].type == Trunc) { + // Truncate at %< item. + trunc_p = item[i].start; + item_idx = i; break; } - if (l == itemcnt) { - /* No %< item, truncate first item. */ - s = item[0].start; - l = 0; - } } - if (width - vim_strsize(s) >= maxwidth) { - /* Truncation mark is beyond max length */ + // If the truncation point we found is beyond the maximum + // length of the string, truncate the end of the string. + if (width - vim_strsize(trunc_p) >= maxwidth) { + // If we are using a multi-byte encoding, walk from the beginning of the + // string to find the last character that will fit. if (has_mbyte) { - s = out; + trunc_p = out; width = 0; for (;; ) { - width += ptr2cells(s); + width += ptr2cells(trunc_p); if (width >= maxwidth) break; - s += (*mb_ptr2len)(s); + + // Note: Only advance the pointer if the next + // character will fit in the available output space + trunc_p += (*mb_ptr2len)(trunc_p); } /* Fill up for half a double-wide character. */ - while (++width < maxwidth) - *s++ = fillchar; - } else - s = out + maxwidth - 1; - for (l = 0; l < itemcnt; l++) - if (item[l].start > s) + // XXX : This seems impossible given the exit condition above? + while (++width < maxwidth) { + *trunc_p++ = fillchar; + } + + // Otherwise put the truncation point at the end, leaving enough room + // for a single-character truncation marker + } else { + trunc_p = out + maxwidth - 1; + } + + // Ignore any items in the statusline that occur after + // the truncation point + for (int i = 0; i < itemcnt; i++) { + if (item[i].start > trunc_p) { + itemcnt = i; break; - itemcnt = l; - *s++ = '>'; - *s = 0; + } + } + + // Truncate the output + *trunc_p++ = '>'; + *trunc_p = 0; + + // Truncate at the truncation point we found } else { - long n; + //{ Determine how many bytes to remove + long trunc_len; if (has_mbyte) { - n = 0; + trunc_len = 0; while (width >= maxwidth) { - width -= ptr2cells(s + n); - n += (*mb_ptr2len)(s + n); + width -= ptr2cells(trunc_p + trunc_len); + trunc_len += (*mb_ptr2len)(trunc_p + trunc_len); } - } else - n = width - maxwidth + 1; - p = s + n; - STRMOVE(s + 1, p); - *s = '<'; + } else { + // Truncate an extra character so we can insert our `<`. + trunc_len = (width - maxwidth) + 1; + } + //} + + //{ Truncate the string + char_u *trunc_end_p = trunc_p + trunc_len; + STRMOVE(trunc_p + 1, trunc_end_p); + + // Put a `<` to mark where we truncated at + *trunc_p = '<'; + + // Advance the pointer to the end of the string + trunc_p = trunc_p + STRLEN(trunc_p); /* Fill up for half a double-wide character. */ while (++width < maxwidth) { - s = s + STRLEN(s); - *s++ = fillchar; - *s = NUL; + *trunc_p++ = fillchar; + *trunc_p = NUL; } + //} - --n; /* count the '<' */ - for (; l < itemcnt; l++) { - if (item[l].start - n >= s) - item[l].start -= n; - else - item[l].start = s; + //{ Change the start point for items based on + // their position relative to our truncation point + + // Note: The offset is one less than the truncation length because + // the truncation marker `<` is not counted. + long item_offset = trunc_len - 1; + + for (int i = item_idx; i < itemcnt; i++) { + // Items starting at or after the end of the truncated section need + // to be moved backwards. + if (item[i].start >= trunc_end_p) { + item[i].start -= item_offset; + // Anything inside the truncated area is set to start at the `<` truncation character. + } else { + item[i].start = trunc_p; + } } + //} } width = maxwidth; - } else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < - outlen) { - /* Apply STL_MIDDLE if any */ - for (long l = 0; l < itemcnt; l++) - if (item[l].type == Middle) { - p = item[l].start + maxwidth - width; - STRMOVE(p, item[l].start); - for (s = item[l].start; s < p; s++) + + // If there is room left in our statusline, and room left in our buffer, + // add characters at the middle marker (if there is one) to fill up the available space. + } else if (width < maxwidth + && STRLEN(out) + maxwidth - width + 1 < outlen) { + for (int item_idx = 0; item_idx < itemcnt; item_idx++) { + if (item[item_idx].type == Middle) { + // Move the statusline to make room for the middle characters + char_u *middle_end = item[item_idx].start + (maxwidth - width); + STRMOVE(middle_end, item[item_idx].start); + + // Fill the middle section with our fill character + for (char_u *s = item[item_idx].start; s < middle_end; s++) *s = fillchar; - for (l++; l < itemcnt; l++) - item[l].start += maxwidth - width; - width = maxwidth; + // Adjust the offset of any items after the middle + for (item_idx++; item_idx < itemcnt; item_idx++) + item[item_idx].start += maxwidth - width; + + width = maxwidth; break; } + } } /* Store the info about highlighting. */ -- cgit From 9e5a7d37511e4293cffe250590fe07a57a2a99dd Mon Sep 17 00:00:00 2001 From: Wayne Rowcliffe Date: Tue, 15 Sep 2015 23:31:14 -0500 Subject: Rename build_stl_str_hl input and output buffer pointers --- src/nvim/buffer.c | 185 +++++++++++++++++++++++++++--------------------------- 1 file changed, 93 insertions(+), 92 deletions(-) (limited to 'src') diff --git a/src/nvim/buffer.c b/src/nvim/buffer.c index 95a1f783eb..0d48fbeb79 100644 --- a/src/nvim/buffer.c +++ b/src/nvim/buffer.c @@ -2894,81 +2894,82 @@ build_stl_str_hl ( bool prevchar_isflag = true; bool prevchar_isitem = false; - // p is the current position in the output buffer - char_u *p = out; + // out_p is the current position in the output buffer + char_u *out_p = out; // Proceed character by character through the statusline format string - // s is the current positon in the input buffer - for (char_u *s = usefmt; *s; ) { + // fmt_p is the current positon in the input buffer + for (char_u *fmt_p = usefmt; *fmt_p; ) { if (curitem == STL_MAX_ITEM) { /* There are too many items. Add the error code to the statusline * to give the user a hint about what went wrong. */ - if (p + 6 < out + outlen) { - memmove(p, " E541", (size_t)5); - p += 5; + if (out_p + 6 < out + outlen) { + memmove(out_p, " E541", (size_t)5); + out_p += 5; } break; } - if (*s != NUL && *s != '%') + if (*fmt_p != NUL && *fmt_p != '%') { prevchar_isflag = prevchar_isitem = false; + } // Copy the formatting verbatim until we reach the end of the string // or find a formatting item (denoted by `%`) // or run out of room in our output buffer. - while (*s != NUL && *s != '%' && p + 1 < out + outlen) - *p++ = *s++; + while (*fmt_p != NUL && *fmt_p != '%' && out_p + 1 < out + outlen) + *out_p++ = *fmt_p++; // If we have processed the entire format string or run out of // room in our output buffer, exit the loop. - if (*s == NUL || p + 1 >= out + outlen) + if (*fmt_p == NUL || out_p + 1 >= out + outlen) break; // The rest of this loop wil handle a single `%` item. // Note: We increment here to skip over the `%` character we are currently // on so we can process the item's contents. - s++; + fmt_p++; // Ignore `%` at the end of the format string - if (*s == NUL) { + if (*fmt_p == NUL) { break; } // Two `%` in a row is the escape sequence to print a // single `%` in the output buffer. - if (*s == '%') { + if (*fmt_p == '%') { // Ignore the character if we're out of room in the output buffer. - if (p + 1 >= out + outlen) + if (out_p + 1 >= out + outlen) break; - *p++ = *s++; + *out_p++ = *fmt_p++; prevchar_isflag = prevchar_isitem = false; continue; } // STL_MIDDLEMARK denotes the separation place between left and right aligned items. - if (*s == STL_MIDDLEMARK) { - s++; + if (*fmt_p == STL_MIDDLEMARK) { + fmt_p++; // Ignored when we are inside of a grouping if (groupdepth > 0) { continue; } item[curitem].type = Middle; - item[curitem++].start = p; + item[curitem++].start = out_p; continue; } // STL_TRUNCMARK denotes where to begin truncating if the statusline is too long. - if (*s == STL_TRUNCMARK) { - s++; + if (*fmt_p == STL_TRUNCMARK) { + fmt_p++; item[curitem].type = Trunc; - item[curitem++].start = p; + item[curitem++].start = out_p; continue; } // The end of a grouping - if (*s == ')') { - s++; + if (*fmt_p == ')') { + fmt_p++; // Ignore if we are not actually inside a group currently if (groupdepth < 1) { continue; @@ -2978,7 +2979,7 @@ build_stl_str_hl ( //{ Determine how long the group is. // Note: We set the current output position to null so `vim_strsize` will work. char_u *t = item[groupitem[groupdepth]].start; - *p = NUL; + *out_p = NUL; long l = vim_strsize(t); //} @@ -2998,7 +2999,7 @@ build_stl_str_hl ( } if (!has_normal_items) { - p = t; + out_p = t; l = 0; } } @@ -3016,18 +3017,18 @@ build_stl_str_hl ( n += (*mb_ptr2len)(t + n); } } else - n = (long)(p - t) - item[groupitem[groupdepth]].maxwid + 1; + n = (long)(out_p - t) - item[groupitem[groupdepth]].maxwid + 1; //} // Prepend the `<` to indicate that the output was truncated. *t = '<'; //{ Move the truncated output - memmove(t + 1, t + n, (size_t)(p - (t + n))); - p = p - n + 1; + memmove(t + 1, t + n, (size_t)(out_p - (t + n))); + out_p = out_p - n + 1; /* Fill up space left over by half a double-wide char. */ while (++l < item[groupitem[groupdepth]].minwid) - *p++ = fillchar; + *out_p++ = fillchar; //} /* correct the start of the items for the truncation */ @@ -3046,18 +3047,18 @@ build_stl_str_hl ( // If the group is left-aligned, add characters to the right. if (min_group_width < 0) { min_group_width = 0 - min_group_width; - while (l++ < min_group_width && p + 1 < out + outlen) - *p++ = fillchar; + while (l++ < min_group_width && out_p + 1 < out + outlen) + *out_p++ = fillchar; // If the group is right-aligned, shift everything to the right and // prepend with filler characters. } else { //{ Move the group to the right - memmove(t + min_group_width - l, t, (size_t)(p - t)); + memmove(t + min_group_width - l, t, (size_t)(out_p - t)); l = min_group_width - l; - if (p + l >= out + outlen) { - l = (long)((out + outlen) - p - 1); + if (out_p + l >= out + outlen) { + l = (long)((out + outlen) - out_p - 1); } - p += l; + out_p += l; //} // Adjust item start positions @@ -3078,37 +3079,37 @@ build_stl_str_hl ( bool left_align = false; // Denotes that numbers should be left-padded with zeros - bool zeropad = (*s == '0'); + bool zeropad = (*fmt_p == '0'); if (zeropad) { - s++; + fmt_p++; } // Denotes that the item should be left-aligned. // This is tracked by using a negative length. - if (*s == '-') { - s++; + if (*fmt_p == '-') { + fmt_p++; left_align = true; } // The first digit group is the item's min width - if (ascii_isdigit(*s)) { - minwid = getdigits_int(&s); + if (ascii_isdigit(*fmt_p)) { + minwid = getdigits_int(&fmt_p); if (minwid < 0) /* overflow */ minwid = 0; } // User highlight groups override the min width field // to denote the styling to use. - if (*s == STL_USER_HL) { + if (*fmt_p == STL_USER_HL) { item[curitem].type = Highlight; - item[curitem].start = p; + item[curitem].start = out_p; item[curitem].minwid = minwid > 9 ? 1 : minwid; - s++; + fmt_p++; curitem++; continue; } - if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR) { - if (*s == STL_TABCLOSENR) { + if (*fmt_p == STL_TABPAGENR || *fmt_p == STL_TABCLOSENR) { + if (*fmt_p == STL_TABCLOSENR) { if (minwid == 0) { /* %X ends the close label, go back to the previously * define tab label nr. */ @@ -3122,19 +3123,19 @@ build_stl_str_hl ( minwid = -minwid; } item[curitem].type = TabPage; - item[curitem].start = p; + item[curitem].start = out_p; item[curitem].minwid = minwid; - s++; + fmt_p++; curitem++; continue; } // Denotes the end of the minwid // the maxwid may follow immediately after - if (*s == '.') { - s++; - if (ascii_isdigit(*s)) { - maxwid = getdigits_int(&s); + if (*fmt_p == '.') { + fmt_p++; + if (ascii_isdigit(*fmt_p)) { + maxwid = getdigits_int(&fmt_p); if (maxwid <= 0) /* overflow */ maxwid = 50; } @@ -3145,26 +3146,26 @@ build_stl_str_hl ( minwid = (minwid > 50 ? 50 : minwid) * (left_align ? -1 : 1); // Denotes the start of a new group - if (*s == '(') { + if (*fmt_p == '(') { groupitem[groupdepth++] = curitem; item[curitem].type = Group; - item[curitem].start = p; + item[curitem].start = out_p; item[curitem].minwid = minwid; item[curitem].maxwid = maxwid; - s++; + fmt_p++; curitem++; continue; } // An invalid item was specified. // Continue processing on the next character of the format string. - if (vim_strchr(STL_ALL, *s) == NULL) { - s++; + if (vim_strchr(STL_ALL, *fmt_p) == NULL) { + fmt_p++; continue; } // The status line item type - char_u opt = *s++; + char_u opt = *fmt_p++; /* OK - now for the real work */ enum number_base base = DECIMAL; @@ -3198,16 +3199,16 @@ build_stl_str_hl ( // Attempt to copy the expression to evaluate into // the output buffer as a null-terminated string. - char_u *t = p; - while (*s != '}' && *s != NUL && p + 1 < out + outlen) - *p++ = *s++; - if (*s != '}') /* missing '}' or out of space */ + char_u *t = out_p; + while (*fmt_p != '}' && *fmt_p != NUL && out_p + 1 < out + outlen) + *out_p++ = *fmt_p++; + if (*fmt_p != '}') /* missing '}' or out of space */ break; - s++; - *p = 0; + fmt_p++; + *out_p = 0; // Move our position in the output buffer to the beginning of the expression - p = t; + out_p = t; //{ Evaluate the expression @@ -3223,7 +3224,7 @@ build_stl_str_hl ( curbuf = wp->w_buffer; // Note: The result stored in `t` is unused. - str = eval_to_string_safe(p, &t, use_sandbox); + str = eval_to_string_safe(out_p, &t, use_sandbox); // Switch back to the actual current buffer and window. curwin = o_curwin; @@ -3418,19 +3419,19 @@ build_stl_str_hl ( case STL_HIGHLIGHT: { //{ The name of the highlight is surrounded by `#` - char_u *t = s; - while (*s != '#' && *s != NUL) { - ++s; + char_u *t = fmt_p; + while (*fmt_p != '#' && *fmt_p != NUL) { + ++fmt_p; } //} // Create a highlight item based on the name - if (*s == '#') { + if (*fmt_p == '#') { item[curitem].type = Highlight; - item[curitem].start = p; - item[curitem].minwid = -syn_namen2id(t, (int)(s - t)); + item[curitem].start = out_p; + item[curitem].minwid = -syn_namen2id(t, (int)(fmt_p - t)); curitem++; - s++; + fmt_p++; } continue; } @@ -3439,7 +3440,7 @@ build_stl_str_hl ( // If we made it this far, the item is normal and starts at // our current position in the output buffer. // Non-normal items would have `continued`. - item[curitem].start = p; + item[curitem].start = out_p; item[curitem].type = Normal; // Copy the item string into the output buffer @@ -3475,21 +3476,21 @@ build_stl_str_hl ( } // Early out if there isn't enough room for the truncation marker - if (p + 1 >= out + outlen) + if (out_p + 1 >= out + outlen) break; // Add the truncation marker - *p++ = '<'; + *out_p++ = '<'; } // If the item is right aligned and not wide enough, pad with fill characters. if (minwid > 0) { - for (; l < minwid && p + 1 < out + outlen; l++) { + for (; l < minwid && out_p + 1 < out + outlen; l++) { /* Don't put a "-" in front of a digit. */ if (l + 1 == minwid && fillchar == '-' && ascii_isdigit(*t)) - *p++ = ' '; + *out_p++ = ' '; else - *p++ = fillchar; + *out_p++ = fillchar; } minwid = 0; } else { @@ -3499,24 +3500,24 @@ build_stl_str_hl ( } //{ Copy the string text into the output buffer - while (*t && p + 1 < out + outlen) { - *p++ = *t++; + while (*t && out_p + 1 < out + outlen) { + *out_p++ = *t++; /* Change a space by fillchar, unless fillchar is '-' and a * digit follows. */ - if (fillable && p[-1] == ' ' + if (fillable && out_p[-1] == ' ' && (!ascii_isdigit(*t) || fillchar != '-')) - p[-1] = fillchar; + out_p[-1] = fillchar; } //} // For left-aligned items, fill any remaining space with the fillchar - for (; l < minwid && p + 1 < out + outlen; l++) { - *p++ = fillchar; + for (; l < minwid && out_p + 1 < out + outlen; l++) { + *out_p++ = fillchar; } // Otherwise if the item is a number, copy that to the output buffer. } else if (num >= 0) { - if (p + 20 >= out + outlen) + if (out_p + 20 >= out + outlen) break; /* not sufficient space */ prevchar_isitem = true; @@ -3550,7 +3551,7 @@ build_stl_str_hl ( } //} - size_t remaining_buf_len = (outlen - (p - out)); + size_t remaining_buf_len = (outlen - (out_p - out)); // If the number is going to take up too much room // Figure out the approximate number in "scientific" type notation. @@ -3576,15 +3577,15 @@ build_stl_str_hl ( *++t = 0; //} - vim_snprintf((char *)p, remaining_buf_len, (char *)nstr, + vim_snprintf((char *)out_p, remaining_buf_len, (char *)nstr, 0, num, n); } else { - vim_snprintf((char *)p, remaining_buf_len, (char *)nstr, + vim_snprintf((char *)out_p, remaining_buf_len, (char *)nstr, minwid, num); } // Advance the output buffer position to the end of the number we just printed - p += STRLEN(p); + out_p += STRLEN(out_p); // Otherwise, there was nothing to print so mark the item as empty } else { @@ -3605,7 +3606,7 @@ build_stl_str_hl ( } // Null terminate the output buffer - *p = NUL; + *out_p = NUL; int itemcnt = curitem; // Free the format buffer if we allocated it internally -- cgit From 12e53ca95c31aee9a1191e54e23b6e84f85d2c2e Mon Sep 17 00:00:00 2001 From: Wayne Rowcliffe Date: Tue, 15 Sep 2015 23:53:12 -0500 Subject: Add out_end_p to mark last valid character in statusline buffer --- src/nvim/buffer.c | 53 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 30 insertions(+), 23 deletions(-) (limited to 'src') diff --git a/src/nvim/buffer.c b/src/nvim/buffer.c index 0d48fbeb79..46f9e3fdab 100644 --- a/src/nvim/buffer.c +++ b/src/nvim/buffer.c @@ -2897,6 +2897,11 @@ build_stl_str_hl ( // out_p is the current position in the output buffer char_u *out_p = out; + // out_end_p is the last valid character in the output buffer + // Note: The null termination character must occur here or earlier, + // so any user-visible characters must occur before here. + char_u *out_end_p = (out + outlen) - 1; + // Proceed character by character through the statusline format string // fmt_p is the current positon in the input buffer @@ -2904,7 +2909,7 @@ build_stl_str_hl ( if (curitem == STL_MAX_ITEM) { /* There are too many items. Add the error code to the statusline * to give the user a hint about what went wrong. */ - if (out_p + 6 < out + outlen) { + if (out_p + 5 < out_end_p) { memmove(out_p, " E541", (size_t)5); out_p += 5; } @@ -2918,12 +2923,12 @@ build_stl_str_hl ( // Copy the formatting verbatim until we reach the end of the string // or find a formatting item (denoted by `%`) // or run out of room in our output buffer. - while (*fmt_p != NUL && *fmt_p != '%' && out_p + 1 < out + outlen) + while (*fmt_p != NUL && *fmt_p != '%' && out_p < out_end_p) *out_p++ = *fmt_p++; // If we have processed the entire format string or run out of // room in our output buffer, exit the loop. - if (*fmt_p == NUL || out_p + 1 >= out + outlen) + if (*fmt_p == NUL || out_p >= out_end_p) break; // The rest of this loop wil handle a single `%` item. @@ -2940,7 +2945,7 @@ build_stl_str_hl ( // single `%` in the output buffer. if (*fmt_p == '%') { // Ignore the character if we're out of room in the output buffer. - if (out_p + 1 >= out + outlen) + if (out_p >= out_end_p) break; *out_p++ = *fmt_p++; prevchar_isflag = prevchar_isitem = false; @@ -3047,7 +3052,7 @@ build_stl_str_hl ( // If the group is left-aligned, add characters to the right. if (min_group_width < 0) { min_group_width = 0 - min_group_width; - while (l++ < min_group_width && out_p + 1 < out + outlen) + while (l++ < min_group_width && out_p < out_end_p) *out_p++ = fillchar; // If the group is right-aligned, shift everything to the right and // prepend with filler characters. @@ -3055,8 +3060,8 @@ build_stl_str_hl ( //{ Move the group to the right memmove(t + min_group_width - l, t, (size_t)(out_p - t)); l = min_group_width - l; - if (out_p + l >= out + outlen) { - l = (long)((out + outlen) - out_p - 1); + if (out_p + l >= (out_end_p + 1)) { + l = (long)(out_end_p - out_p); } out_p += l; //} @@ -3200,7 +3205,7 @@ build_stl_str_hl ( // Attempt to copy the expression to evaluate into // the output buffer as a null-terminated string. char_u *t = out_p; - while (*fmt_p != '}' && *fmt_p != NUL && out_p + 1 < out + outlen) + while (*fmt_p != '}' && *fmt_p != NUL && out_p < out_end_p) *out_p++ = *fmt_p++; if (*fmt_p != '}') /* missing '}' or out of space */ break; @@ -3476,7 +3481,7 @@ build_stl_str_hl ( } // Early out if there isn't enough room for the truncation marker - if (out_p + 1 >= out + outlen) + if (out_p >= out_end_p) break; // Add the truncation marker @@ -3485,7 +3490,7 @@ build_stl_str_hl ( // If the item is right aligned and not wide enough, pad with fill characters. if (minwid > 0) { - for (; l < minwid && out_p + 1 < out + outlen; l++) { + for (; l < minwid && out_p < out_end_p; l++) { /* Don't put a "-" in front of a digit. */ if (l + 1 == minwid && fillchar == '-' && ascii_isdigit(*t)) *out_p++ = ' '; @@ -3500,7 +3505,7 @@ build_stl_str_hl ( } //{ Copy the string text into the output buffer - while (*t && out_p + 1 < out + outlen) { + while (*t && out_p < out_end_p) { *out_p++ = *t++; /* Change a space by fillchar, unless fillchar is '-' and a * digit follows. */ @@ -3511,13 +3516,13 @@ build_stl_str_hl ( //} // For left-aligned items, fill any remaining space with the fillchar - for (; l < minwid && out_p + 1 < out + outlen; l++) { + for (; l < minwid && out_p < out_end_p; l++) { *out_p++ = fillchar; } // Otherwise if the item is a number, copy that to the output buffer. } else if (num >= 0) { - if (out_p + 20 >= out + outlen) + if (out_p + 20 > out_end_p) break; /* not sufficient space */ prevchar_isitem = true; @@ -3540,31 +3545,33 @@ build_stl_str_hl ( //} //{ Determine how many characters the number will take up when printed - long l; - for (long n = num, l = 1; n >= base; n /= base) { - l++; + // Note: We have to cast the base because the compiler uses + // unsigned ints for the enum values. + long num_chars = 0; + for (long n = num, num_chars = 1; n >= (int) base; n /= (int) base) { + num_chars++; } // VIRTCOL_ALT takes up an extra character because of the `-` we added above. if (opt == STL_VIRTCOL_ALT) { - l++; + num_chars++; } //} - size_t remaining_buf_len = (outlen - (out_p - out)); + size_t remaining_buf_len = (out_end_p - out_p) + 1; // If the number is going to take up too much room // Figure out the approximate number in "scientific" type notation. // Ex: 14532 with maxwid of 4 -> '14>3' - if (l > maxwid) { + if (num_chars > maxwid) { // Add two to the width because the power piece will take two extra characters - l += 2; + num_chars += 2; // How many extra characters there are - long n = l - maxwid; + long n = num_chars - maxwid; //{ Reduce the number by base^n - while (l-- > maxwid) { + while (num_chars-- > maxwid) { num /= base; } //} @@ -3630,7 +3637,7 @@ build_stl_str_hl ( trunc_p = item[0].start; item_idx = 0; - for (int i; i < itemcnt; i++) + for (int i = 0; i < itemcnt; i++) if (item[i].type == Trunc) { // Truncate at %< item. trunc_p = item[i].start; -- cgit From 7c32d86f7de972e859d15dd9bfbed877e773c929 Mon Sep 17 00:00:00 2001 From: Wayne Rowcliffe Date: Wed, 16 Sep 2015 00:51:41 -0500 Subject: Fix lint --- src/nvim/buffer.c | 122 ++++++++++++++++++++++++++++++------------------------ 1 file changed, 68 insertions(+), 54 deletions(-) (limited to 'src') diff --git a/src/nvim/buffer.c b/src/nvim/buffer.c index 46f9e3fdab..d5fbcdeab8 100644 --- a/src/nvim/buffer.c +++ b/src/nvim/buffer.c @@ -2819,7 +2819,7 @@ void free_titles(void) * or truncated if too long, fillchar is used for all whitespace. */ int -build_stl_str_hl ( +build_stl_str_hl( win_T *wp, char_u *out, /* buffer to write into != NameBuff */ size_t outlen, /* length of out[] */ @@ -2850,11 +2850,11 @@ build_stl_str_hl ( } type; } item[STL_MAX_ITEM]; - enum number_base { + typedef enum { DECIMAL = 10, OCTAL = 8, HEXIDECIMAL = 16 - }; + } number_base; #define TMPLEN 70 char_u tmp[TMPLEN]; @@ -2952,7 +2952,8 @@ build_stl_str_hl ( continue; } - // STL_MIDDLEMARK denotes the separation place between left and right aligned items. + // STL_MIDDLEMARK denotes the separation place between + // left and right aligned items. if (*fmt_p == STL_MIDDLEMARK) { fmt_p++; // Ignored when we are inside of a grouping @@ -2964,7 +2965,8 @@ build_stl_str_hl ( continue; } - // STL_TRUNCMARK denotes where to begin truncating if the statusline is too long. + // STL_TRUNCMARK denotes where to begin truncating if the + // statusline is too long. if (*fmt_p == STL_TRUNCMARK) { fmt_p++; item[curitem].type = Trunc; @@ -2981,17 +2983,19 @@ build_stl_str_hl ( } groupdepth--; - //{ Determine how long the group is. - // Note: We set the current output position to null so `vim_strsize` will work. + // { Determine how long the group is. + // Note: We set the current output position to null + // so `vim_strsize` will work. char_u *t = item[groupitem[groupdepth]].start; *out_p = NUL; long l = vim_strsize(t); - //} + // } - // If the group contained internal items and the group did not have a minimum width, + // If the group contained internal items + // and the group did not have a minimum width, // and if there were no normal items in the group, - // move the output pointer back to where the group started (erase the group). - // Note: This also erases any plaintext characters that were in the group. + // move the output pointer back to where the group started. + // Note: This erases any non-item characters that were in the group. // Otherwise there would be no reason to do this step. if (curitem > groupitem[groupdepth] + 1 && item[groupitem[groupdepth]].minwid == 0) { @@ -3012,7 +3016,7 @@ build_stl_str_hl ( // If the group is longer than it is allowed to be // truncate by removing bytes from the start of the group text. if (l > item[groupitem[groupdepth]].maxwid) { - //{ Determine the number of bytes to remove + // { Determine the number of bytes to remove long n; if (has_mbyte) { /* Find the first character that should be included. */ @@ -3021,20 +3025,21 @@ build_stl_str_hl ( l -= ptr2cells(t + n); n += (*mb_ptr2len)(t + n); } - } else + } else { n = (long)(out_p - t) - item[groupitem[groupdepth]].maxwid + 1; - //} + } + // } // Prepend the `<` to indicate that the output was truncated. *t = '<'; - //{ Move the truncated output + // { Move the truncated output memmove(t + 1, t + n, (size_t)(out_p - (t + n))); out_p = out_p - n + 1; /* Fill up space left over by half a double-wide char. */ while (++l < item[groupitem[groupdepth]].minwid) *out_p++ = fillchar; - //} + // } /* correct the start of the items for the truncation */ for (l = groupitem[groupdepth] + 1; l < curitem; l++) { @@ -3057,14 +3062,14 @@ build_stl_str_hl ( // If the group is right-aligned, shift everything to the right and // prepend with filler characters. } else { - //{ Move the group to the right + // { Move the group to the right memmove(t + min_group_width - l, t, (size_t)(out_p - t)); l = min_group_width - l; if (out_p + l >= (out_end_p + 1)) { l = (long)(out_end_p - out_p); } out_p += l; - //} + // } // Adjust item start positions for (int n = groupitem[groupdepth] + 1; n < curitem; n++) { @@ -3173,7 +3178,7 @@ build_stl_str_hl ( char_u opt = *fmt_p++; /* OK - now for the real work */ - enum number_base base = DECIMAL; + number_base base = DECIMAL; bool itemisflag = false; bool fillable = true; long num = -1; @@ -3212,17 +3217,18 @@ build_stl_str_hl ( fmt_p++; *out_p = 0; - // Move our position in the output buffer to the beginning of the expression + // Move our position in the output buffer + // to the beginning of the expression out_p = t; - //{ Evaluate the expression + // { Evaluate the expression // Store the current buffer number as a string variable vim_snprintf((char *)tmp, sizeof(tmp), "%d", curbuf->b_fnum); set_internal_string_var((char_u *)"actual_curbuf", tmp); - // Switch the curbuf and curwin to the buffer and window we are evaluating the - // statusline for. + // Switch the curbuf and curwin to the buffer and window we are + // evaluating the statusline for. buf_T *o_curbuf = curbuf; win_T *o_curwin = curwin; curwin = wp; @@ -3236,9 +3242,9 @@ build_stl_str_hl ( curbuf = o_curbuf; // Remove the variable we just stored - do_unlet((char_u *)"g:actual_curbuf", TRUE); + do_unlet((char_u *)"g:actual_curbuf", true); - //} + // } // Check if the evaluated result is a number. // If so, convert the number to an int and free the string. @@ -3310,7 +3316,7 @@ build_stl_str_hl ( // Note: The call will only return true if it actually // appended data to the `tmp` buffer. - if (append_arg_number(wp, tmp, (int)sizeof(tmp), FALSE)) { + if (append_arg_number(wp, tmp, (int)sizeof(tmp), false)) { str = tmp; } break; @@ -3365,7 +3371,8 @@ build_stl_str_hl ( case STL_FILETYPE: // Copy the filetype if it is not null and the formatted string will fit - // in the temporary buffer (including the brackets and null terminating character) + // in the temporary buffer + // (including the brackets and null terminating character) if (*wp->w_buffer->b_p_ft != NUL && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3) { vim_snprintf((char *)tmp, sizeof(tmp), "[%s]", @@ -3378,7 +3385,8 @@ build_stl_str_hl ( { itemisflag = true; // Copy the filetype if it is not null and the formatted string will fit - // in the temporary buffer (including the comma and null terminating character) + // in the temporary buffer + // (including the comma and null terminating character) if (*wp->w_buffer->b_p_ft != NUL && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2) { vim_snprintf((char *)tmp, sizeof(tmp), ",%s", @@ -3423,12 +3431,12 @@ build_stl_str_hl ( case STL_HIGHLIGHT: { - //{ The name of the highlight is surrounded by `#` + // { The name of the highlight is surrounded by `#` char_u *t = fmt_p; while (*fmt_p != '#' && *fmt_p != NUL) { ++fmt_p; } - //} + // } // Create a highlight item based on the name if (*fmt_p == '#') { @@ -3450,7 +3458,7 @@ build_stl_str_hl ( // Copy the item string into the output buffer if (str != NULL && *str) { - //{ Skip the leading `,` or ` ` if the item is a flag + // { Skip the leading `,` or ` ` if the item is a flag // and the proper conditions are met char_u *t = str; if (itemisflag) { @@ -3460,7 +3468,7 @@ build_stl_str_hl ( t++; prevchar_isflag = true; } - //} + // } long l = vim_strsize(t); @@ -3488,7 +3496,8 @@ build_stl_str_hl ( *out_p++ = '<'; } - // If the item is right aligned and not wide enough, pad with fill characters. + // If the item is right aligned and not wide enough, + // pad with fill characters. if (minwid > 0) { for (; l < minwid && out_p < out_end_p; l++) { /* Don't put a "-" in front of a digit. */ @@ -3504,7 +3513,7 @@ build_stl_str_hl ( minwid *= -1; } - //{ Copy the string text into the output buffer + // { Copy the string text into the output buffer while (*t && out_p < out_end_p) { *out_p++ = *t++; /* Change a space by fillchar, unless fillchar is '-' and a @@ -3513,7 +3522,7 @@ build_stl_str_hl ( && (!ascii_isdigit(*t) || fillchar != '-')) out_p[-1] = fillchar; } - //} + // } // For left-aligned items, fill any remaining space with the fillchar for (; l < minwid && out_p < out_end_p; l++) { @@ -3526,7 +3535,7 @@ build_stl_str_hl ( break; /* not sufficient space */ prevchar_isitem = true; - //{ Build the formatting string + // { Build the formatting string char_u nstr[20]; char_u *t = nstr; if (opt == STL_VIRTCOL_ALT) { @@ -3542,9 +3551,9 @@ build_stl_str_hl ( *t++ = '*'; *t++ = (char_u) (base == HEXIDECIMAL ? 'X' : (base == OCTAL ? 'o' : 'd')); *t = 0; - //} + // } - //{ Determine how many characters the number will take up when printed + // { Determine how many characters the number will take up when printed // Note: We have to cast the base because the compiler uses // unsigned ints for the enum values. long num_chars = 0; @@ -3552,11 +3561,12 @@ build_stl_str_hl ( num_chars++; } - // VIRTCOL_ALT takes up an extra character because of the `-` we added above. + // VIRTCOL_ALT takes up an extra character because + // of the `-` we added above. if (opt == STL_VIRTCOL_ALT) { num_chars++; } - //} + // } size_t remaining_buf_len = (out_end_p - out_p) + 1; @@ -3564,25 +3574,26 @@ build_stl_str_hl ( // Figure out the approximate number in "scientific" type notation. // Ex: 14532 with maxwid of 4 -> '14>3' if (num_chars > maxwid) { - // Add two to the width because the power piece will take two extra characters + // Add two to the width because the power piece will take + // two extra characters num_chars += 2; // How many extra characters there are long n = num_chars - maxwid; - //{ Reduce the number by base^n + // { Reduce the number by base^n while (num_chars-- > maxwid) { num /= base; } - //} + // } - //{ Add the format string for the exponent bit + // { Add the format string for the exponent bit *t++ = '>'; *t++ = '%'; // Use the same base as the first number *t = t[-3]; *++t = 0; - //} + // } vim_snprintf((char *)out_p, remaining_buf_len, (char *)nstr, 0, num, n); @@ -3591,7 +3602,8 @@ build_stl_str_hl ( minwid, num); } - // Advance the output buffer position to the end of the number we just printed + // Advance the output buffer position to the end of the + // number we just printed out_p += STRLEN(out_p); // Otherwise, there was nothing to print so mark the item as empty @@ -3690,7 +3702,7 @@ build_stl_str_hl ( // Truncate at the truncation point we found } else { - //{ Determine how many bytes to remove + // { Determine how many bytes to remove long trunc_len; if (has_mbyte) { trunc_len = 0; @@ -3702,9 +3714,9 @@ build_stl_str_hl ( // Truncate an extra character so we can insert our `<`. trunc_len = (width - maxwidth) + 1; } - //} + // } - //{ Truncate the string + // { Truncate the string char_u *trunc_end_p = trunc_p + trunc_len; STRMOVE(trunc_p + 1, trunc_end_p); @@ -3719,9 +3731,9 @@ build_stl_str_hl ( *trunc_p++ = fillchar; *trunc_p = NUL; } - //} + // } - //{ Change the start point for items based on + // { Change the start point for items based on // their position relative to our truncation point // Note: The offset is one less than the truncation length because @@ -3733,17 +3745,19 @@ build_stl_str_hl ( // to be moved backwards. if (item[i].start >= trunc_end_p) { item[i].start -= item_offset; - // Anything inside the truncated area is set to start at the `<` truncation character. + // Anything inside the truncated area is set to start + // at the `<` truncation character. } else { item[i].start = trunc_p; } } - //} + // } } width = maxwidth; // If there is room left in our statusline, and room left in our buffer, - // add characters at the middle marker (if there is one) to fill up the available space. + // add characters at the middle marker (if there is one) to + // fill up the available space. } else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen) { for (int item_idx = 0; item_idx < itemcnt; item_idx++) { -- cgit From d12f328b60f1454abfaacffdb8bae03ac7d40586 Mon Sep 17 00:00:00 2001 From: Wayne Rowcliffe Date: Thu, 17 Sep 2015 19:30:48 -0500 Subject: Add documentation about what TABPAGENR and TABCLOSENR do --- src/nvim/buffer.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'src') diff --git a/src/nvim/buffer.c b/src/nvim/buffer.c index d5fbcdeab8..3ccef9aaf0 100644 --- a/src/nvim/buffer.c +++ b/src/nvim/buffer.c @@ -3118,6 +3118,30 @@ build_stl_str_hl( curitem++; continue; } + + // TABPAGE pairs are used to denote a region that when clicked will + // either switch to or close a tab. + // + // Ex: tabline=%0Ttab\ zero%X + // This tabline has a TABPAGENR item with minwid `0`, + // which is then closed with a TABCLOSENR item. + // Clicking on this region with mouse enabled will switch to tab 0. + // Setting the minwid to a different value will switch + // to that tab, if it exists + // + // Ex: tabline=%1Xtab\ one%X + // This tabline has a TABCLOSENR item with minwid `1`, + // which is then closed with a TABCLOSENR item. + // Clicking on this region with mouse enabled will close tab 0. + // This is determined by the following formula: + // tab to close = (1 - minwid) + // This is because for TABPAGENR we use `minwid` = `tab number`. + // For TABCLOSENR we store the tab number as a negative value. + // Because 0 is a valid TABPAGENR value, we have to + // start our numbering at `-1`. + // So, `-1` corresponds to us wanting to close tab `0` + // + // Note: These options are only valid when creating a tabline. if (*fmt_p == STL_TABPAGENR || *fmt_p == STL_TABCLOSENR) { if (*fmt_p == STL_TABCLOSENR) { if (minwid == 0) { -- cgit From a9dfcb7e02a0b1268c46a1bacd3f29031265a777 Mon Sep 17 00:00:00 2001 From: Wayne Rowcliffe Date: Fri, 18 Sep 2015 22:41:49 -0500 Subject: Fix shadowed variable in build_stl_str_hl --- src/nvim/buffer.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/nvim/buffer.c b/src/nvim/buffer.c index 3ccef9aaf0..d6ab0ef671 100644 --- a/src/nvim/buffer.c +++ b/src/nvim/buffer.c @@ -3580,8 +3580,8 @@ build_stl_str_hl( // { Determine how many characters the number will take up when printed // Note: We have to cast the base because the compiler uses // unsigned ints for the enum values. - long num_chars = 0; - for (long n = num, num_chars = 1; n >= (int) base; n /= (int) base) { + long num_chars = 1; + for (long n = num; n >= (int) base; n /= (int) base) { num_chars++; } -- cgit From 6cfb81eaa7ad2ea033d34c71f64d2baa499e0466 Mon Sep 17 00:00:00 2001 From: Wayne Rowcliffe Date: Wed, 7 Oct 2015 21:33:41 -0500 Subject: Updates from review --- src/nvim/buffer.c | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/nvim/buffer.c b/src/nvim/buffer.c index d6ab0ef671..56469c8507 100644 --- a/src/nvim/buffer.c +++ b/src/nvim/buffer.c @@ -2803,6 +2803,16 @@ void free_titles(void) # endif +/** + * Enumeration specifying the valid numeric bases that can + * be used when printing numbers in the status line. + **/ +typedef enum { + kNumBaseDecimal = 10, + kNumBaseOctal = 8, + kNumBaseHexadecimal = 16 +} NumberBase; + /* * Build a string from the status line items in "fmt". @@ -2818,8 +2828,7 @@ void free_titles(void) * If maxwidth is not zero, the string will be filled at any middle marker * or truncated if too long, fillchar is used for all whitespace. */ -int -build_stl_str_hl( +int build_stl_str_hl( win_T *wp, char_u *out, /* buffer to write into != NameBuff */ size_t outlen, /* length of out[] */ @@ -2850,12 +2859,6 @@ build_stl_str_hl( } type; } item[STL_MAX_ITEM]; - typedef enum { - DECIMAL = 10, - OCTAL = 8, - HEXIDECIMAL = 16 - } number_base; - #define TMPLEN 70 char_u tmp[TMPLEN]; char_u *usefmt = fmt; @@ -3202,7 +3205,7 @@ build_stl_str_hl( char_u opt = *fmt_p++; /* OK - now for the real work */ - number_base base = DECIMAL; + NumberBase base = kNumBaseDecimal; bool itemisflag = false; bool fillable = true; long num = -1; @@ -3359,7 +3362,7 @@ build_stl_str_hl( break; case STL_OFFSET_X: - base = HEXIDECIMAL; + base = kNumBaseHexadecimal; case STL_OFFSET: { long l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL); @@ -3369,7 +3372,7 @@ build_stl_str_hl( break; } case STL_BYTEVAL_X: - base = HEXIDECIMAL; + base = kNumBaseHexadecimal; case STL_BYTEVAL: num = byteval; if (num == NL) @@ -3573,7 +3576,9 @@ build_stl_str_hl( // Note: The `*` means we take the width as one of the arguments *t++ = '*'; - *t++ = (char_u) (base == HEXIDECIMAL ? 'X' : (base == OCTAL ? 'o' : 'd')); + *t++ = (char_u) (base == kNumBaseHexadecimal ? 'X' + : (base == kNumBaseOctal ? 'o' + : 'd')); *t = 0; // } -- cgit From 81f32bd153c3e72531a796e63d7f9bd7761d6718 Mon Sep 17 00:00:00 2001 From: Wayne Rowcliffe Date: Thu, 8 Oct 2015 23:08:25 -0500 Subject: Switch comments to match style guide --- src/nvim/buffer.c | 99 ++++++++++++++++++++++++++++++------------------------- 1 file changed, 54 insertions(+), 45 deletions(-) (limited to 'src') diff --git a/src/nvim/buffer.c b/src/nvim/buffer.c index 56469c8507..f2c74d6c21 100644 --- a/src/nvim/buffer.c +++ b/src/nvim/buffer.c @@ -2803,10 +2803,8 @@ void free_titles(void) # endif -/** - * Enumeration specifying the valid numeric bases that can - * be used when printing numbers in the status line. - **/ +/// Enumeration specifying the valid numeric bases that can +/// be used when printing numbers in the status line. typedef enum { kNumBaseDecimal = 10, kNumBaseOctal = 8, @@ -2814,30 +2812,41 @@ typedef enum { } NumberBase; -/* - * Build a string from the status line items in "fmt". - * Return length of string in screen cells. - * - * Normally works for window "wp", except when working for 'tabline' then it - * is "curwin". - * - * Items are drawn interspersed with the text that surrounds it - * Specials: %-(xxx%) => group, %= => middle marker, %< => truncation - * Item: %-. All but are optional - * - * If maxwidth is not zero, the string will be filled at any middle marker - * or truncated if too long, fillchar is used for all whitespace. - */ +/// Build a string from the status line items in "fmt". +/// Return length of string in screen cells. +/// +/// Normally works for window "wp", except when working for 'tabline' then it +/// is "curwin". +/// +/// Items are drawn interspersed with the text that surrounds it +/// Specials: %-(xxx%) => group, %= => middle marker, %< => truncation +/// Item: %-. All but are optional +/// +/// If maxwidth is not zero, the string will be filled at any middle marker +/// or truncated if too long, fillchar is used for all whitespace. +/// +/// @param wp The window to build a statusline for +/// @param out The output buffer to write the statusline to +/// Note: This should not be NameBuff +/// @param outlen The length of the output buffer +/// @param fmt The statusline format string +/// @param use_sandbox Use a sandboxed environment when evaluating fmt +/// @param fillchar Character to use when filling empty space in the statusline +/// @param maxwidth The maximum width to make the statusline +/// @param hltab HL attributes (can be NULL) +/// @param tabtab tab page nrs (can be NULL) +/// +/// @return The final width of the statusline int build_stl_str_hl( win_T *wp, - char_u *out, /* buffer to write into != NameBuff */ - size_t outlen, /* length of out[] */ + char_u *out, + size_t outlen, char_u *fmt, - int use_sandbox, /* "fmt" was set insecurely, use sandbox */ + int use_sandbox, int fillchar, int maxwidth, - struct stl_hlrec *hltab, /* return: HL attributes (can be NULL) */ - struct stl_hlrec *tabtab /* return: tab page nrs (can be NULL) */ + struct stl_hlrec *hltab, + struct stl_hlrec *tabtab ) { int groupitem[STL_MAX_ITEM]; @@ -2863,10 +2872,8 @@ int build_stl_str_hl( char_u tmp[TMPLEN]; char_u *usefmt = fmt; - /* - * When the format starts with "%!" then evaluate it as an expression and - * use the result as the actual format string. - */ + // When the format starts with "%!" then evaluate it as an expression and + // use the result as the actual format string. if (fmt[0] == '%' && fmt[1] == '!') { usefmt = eval_to_string_safe(fmt + 2, NULL, use_sandbox); if (usefmt == NULL) @@ -2875,16 +2882,16 @@ int build_stl_str_hl( if (fillchar == 0) fillchar = ' '; - /* Can't handle a multi-byte fill character yet. */ + // Can't handle a multi-byte fill character yet. else if (mb_char2len(fillchar) > 1) fillchar = '-'; - /* Get line & check if empty (cursorpos will show "0-1"). */ + // Get line & check if empty (cursorpos will show "0-1"). char_u *line_ptr = ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, false); bool empty_line = (*line_ptr == NUL); - /* Get the byte value now, in case we need it below. This is more - * efficient than making a copy of the line. */ + // Get the byte value now, in case we need it below. This is more + // efficient than making a copy of the line. int byteval; if (wp->w_cursor.col > (colnr_T)STRLEN(line_ptr)) byteval = 0; @@ -2910,8 +2917,8 @@ int build_stl_str_hl( // fmt_p is the current positon in the input buffer for (char_u *fmt_p = usefmt; *fmt_p; ) { if (curitem == STL_MAX_ITEM) { - /* There are too many items. Add the error code to the statusline - * to give the user a hint about what went wrong. */ + // There are too many items. Add the error code to the statusline + // to give the user a hint about what went wrong. if (out_p + 5 < out_end_p) { memmove(out_p, " E541", (size_t)5); out_p += 5; @@ -3204,7 +3211,7 @@ int build_stl_str_hl( // The status line item type char_u opt = *fmt_p++; - /* OK - now for the real work */ + // OK - now for the real work NumberBase base = kNumBaseDecimal; bool itemisflag = false; bool fillable = true; @@ -3215,7 +3222,9 @@ int build_stl_str_hl( case STL_FULLPATH: case STL_FILENAME: { - fillable = false; /* don't change ' ' to fillchar */ + // Set fillable to false to that ' ' in the filename will not + // get replaced with the fillchar + fillable = false; if (buf_spname(wp->w_buffer) != NULL) { STRLCPY(NameBuff, buf_spname(wp->w_buffer), MAXPATHL); } else { @@ -3302,7 +3311,7 @@ int build_stl_str_hl( case STL_VIRTCOL: case STL_VIRTCOL_ALT: { - /* In list mode virtcol needs to be recomputed */ + // In list mode virtcol needs to be recomputed colnr_T virtcol = wp->w_virtcol; if (wp->w_p_list && lcs_tab1 == NUL) { wp->w_p_list = FALSE; @@ -3310,7 +3319,7 @@ int build_stl_str_hl( wp->w_p_list = TRUE; } ++virtcol; - /* Don't display %V if it's the same as %c. */ + // Don't display %V if it's the same as %c. if (opt == STL_VIRTCOL_ALT && (virtcol == (colnr_T)(!(State & INSERT) && empty_line ? 0 : (int)wp->w_cursor.col + 1))) @@ -3527,7 +3536,7 @@ int build_stl_str_hl( // pad with fill characters. if (minwid > 0) { for (; l < minwid && out_p < out_end_p; l++) { - /* Don't put a "-" in front of a digit. */ + // Don't put a "-" in front of a digit. if (l + 1 == minwid && fillchar == '-' && ascii_isdigit(*t)) *out_p++ = ' '; else @@ -3543,8 +3552,8 @@ int build_stl_str_hl( // { Copy the string text into the output buffer while (*t && out_p < out_end_p) { *out_p++ = *t++; - /* Change a space by fillchar, unless fillchar is '-' and a - * digit follows. */ + // Change a space by fillchar, unless fillchar is '-' and a + // digit follows. if (fillable && out_p[-1] == ' ' && (!ascii_isdigit(*t) || fillchar != '-')) out_p[-1] = fillchar; @@ -3664,7 +3673,7 @@ int build_stl_str_hl( int width = vim_strsize(out); if (maxwidth > 0 && width > maxwidth) { - /* Result is too long, must truncate somewhere. */ + // Result is too long, must truncate somewhere. int item_idx = 0; char_u *trunc_p; @@ -3704,7 +3713,7 @@ int build_stl_str_hl( // character will fit in the available output space trunc_p += (*mb_ptr2len)(trunc_p); } - /* Fill up for half a double-wide character. */ + // Fill up for half a double-wide character. // XXX : This seems impossible given the exit condition above? while (++width < maxwidth) { *trunc_p++ = fillchar; @@ -3755,7 +3764,7 @@ int build_stl_str_hl( // Advance the pointer to the end of the string trunc_p = trunc_p + STRLEN(trunc_p); - /* Fill up for half a double-wide character. */ + // Fill up for half a double-wide character. while (++width < maxwidth) { *trunc_p++ = fillchar; *trunc_p = NUL; @@ -3809,7 +3818,7 @@ int build_stl_str_hl( } } - /* Store the info about highlighting. */ + // Store the info about highlighting. if (hltab != NULL) { struct stl_hlrec *sp = hltab; for (long l = 0; l < itemcnt; l++) { @@ -3823,7 +3832,7 @@ int build_stl_str_hl( sp->userhl = 0; } - /* Store the info about tab pages labels. */ + // Store the info about tab pages labels. if (tabtab != NULL) { struct stl_hlrec *sp = tabtab; for (long l = 0; l < itemcnt; l++) { -- cgit From eed4df33f93a49af0cdfd67a9bdb2495be9e6998 Mon Sep 17 00:00:00 2001 From: Wayne Rowcliffe Date: Tue, 10 Nov 2015 20:33:02 -0600 Subject: Cleanup from review --- src/nvim/buffer.c | 71 +++++++++++++++++++++++++------------------------------ 1 file changed, 32 insertions(+), 39 deletions(-) (limited to 'src') diff --git a/src/nvim/buffer.c b/src/nvim/buffer.c index f2c74d6c21..6fc08643af 100644 --- a/src/nvim/buffer.c +++ b/src/nvim/buffer.c @@ -2853,7 +2853,7 @@ int build_stl_str_hl( struct stl_item { // Where the item starts in the status line output buffer char_u *start; - // The minimum width of the item (at most 50 characters) + // The minimum width of the item int minwid; // The maximum width of the item int maxwid; @@ -2941,7 +2941,7 @@ int build_stl_str_hl( if (*fmt_p == NUL || out_p >= out_end_p) break; - // The rest of this loop wil handle a single `%` item. + // The rest of this loop will handle a single `%` item. // Note: We increment here to skip over the `%` character we are currently // on so we can process the item's contents. fmt_p++; @@ -2962,8 +2962,7 @@ int build_stl_str_hl( continue; } - // STL_MIDDLEMARK denotes the separation place between - // left and right aligned items. + // STL_MIDDLEMARK: Separation place between left and right aligned items. if (*fmt_p == STL_MIDDLEMARK) { fmt_p++; // Ignored when we are inside of a grouping @@ -2975,8 +2974,7 @@ int build_stl_str_hl( continue; } - // STL_TRUNCMARK denotes where to begin truncating if the - // statusline is too long. + // STL_TRUNCMARK: Where to begin truncating if the statusline is too long. if (*fmt_p == STL_TRUNCMARK) { fmt_p++; item[curitem].type = Trunc; @@ -2993,13 +2991,12 @@ int build_stl_str_hl( } groupdepth--; - // { Determine how long the group is. - // Note: We set the current output position to null - // so `vim_strsize` will work. + // Determine how long the group is. + // Note: We set the current output position to null + // so `vim_strsize` will work. char_u *t = item[groupitem[groupdepth]].start; *out_p = NUL; - long l = vim_strsize(t); - // } + long group_len = vim_strsize(t); // If the group contained internal items // and the group did not have a minimum width, @@ -3019,20 +3016,20 @@ int build_stl_str_hl( if (!has_normal_items) { out_p = t; - l = 0; + group_len = 0; } } // If the group is longer than it is allowed to be // truncate by removing bytes from the start of the group text. - if (l > item[groupitem[groupdepth]].maxwid) { + if (group_len > item[groupitem[groupdepth]].maxwid) { // { Determine the number of bytes to remove long n; if (has_mbyte) { /* Find the first character that should be included. */ n = 0; - while (l >= item[groupitem[groupdepth]].maxwid) { - l -= ptr2cells(t + n); + while (group_len >= item[groupitem[groupdepth]].maxwid) { + group_len -= ptr2cells(t + n); n += (*mb_ptr2len)(t + n); } } else { @@ -3047,47 +3044,48 @@ int build_stl_str_hl( memmove(t + 1, t + n, (size_t)(out_p - (t + n))); out_p = out_p - n + 1; /* Fill up space left over by half a double-wide char. */ - while (++l < item[groupitem[groupdepth]].minwid) + while (++group_len < item[groupitem[groupdepth]].minwid) *out_p++ = fillchar; // } /* correct the start of the items for the truncation */ - for (l = groupitem[groupdepth] + 1; l < curitem; l++) { + for (int idx = groupitem[groupdepth] + 1; idx < curitem; idx++) { // Shift everything back by the number of removed bytes - item[l].start -= n; + item[idx].start -= n; // If the item was partially or completely truncated, set its // start to the start of the group - if (item[l].start < t) - item[l].start = t; + if (item[idx].start < t) { + item[idx].start = t; + } } // If the group is shorter than the minimum width, add padding characters. - } else if (abs(item[groupitem[groupdepth]].minwid) > l) { + } else if (abs(item[groupitem[groupdepth]].minwid) > group_len) { long min_group_width = item[groupitem[groupdepth]].minwid; // If the group is left-aligned, add characters to the right. if (min_group_width < 0) { min_group_width = 0 - min_group_width; - while (l++ < min_group_width && out_p < out_end_p) + while (group_len++ < min_group_width && out_p < out_end_p) *out_p++ = fillchar; // If the group is right-aligned, shift everything to the right and // prepend with filler characters. } else { // { Move the group to the right - memmove(t + min_group_width - l, t, (size_t)(out_p - t)); - l = min_group_width - l; - if (out_p + l >= (out_end_p + 1)) { - l = (long)(out_end_p - out_p); + memmove(t + min_group_width - group_len, t, (size_t)(out_p - t)); + group_len = min_group_width - group_len; + if (out_p + group_len >= (out_end_p + 1)) { + group_len = (long)(out_end_p - out_p); } - out_p += l; + out_p += group_len; // } // Adjust item start positions for (int n = groupitem[groupdepth] + 1; n < curitem; n++) { - item[n].start += l; + item[n].start += group_len; } // Prepend the fill characters - for (; l > 0; l--) { + for (; group_len > 0; group_len--) { *t++ = fillchar; } } @@ -3263,8 +3261,6 @@ int build_stl_str_hl( vim_snprintf((char *)tmp, sizeof(tmp), "%d", curbuf->b_fnum); set_internal_string_var((char_u *)"actual_curbuf", tmp); - // Switch the curbuf and curwin to the buffer and window we are - // evaluating the statusline for. buf_T *o_curbuf = curbuf; win_T *o_curwin = curwin; curwin = wp; @@ -3273,7 +3269,6 @@ int build_stl_str_hl( // Note: The result stored in `t` is unused. str = eval_to_string_safe(out_p, &t, use_sandbox); - // Switch back to the actual current buffer and window. curwin = o_curwin; curbuf = o_curbuf; @@ -3649,7 +3644,7 @@ int build_stl_str_hl( item[curitem].type = Empty; } - // Free the string buffer if we allocated it. + // Only free the string buffer if we allocated it. // Note: This is not needed if `str` is pointing at `tmp` if (opt == STL_VIM_EXPR) { xfree(str); @@ -3662,7 +3657,6 @@ int build_stl_str_hl( curitem++; } - // Null terminate the output buffer *out_p = NUL; int itemcnt = curitem; @@ -3671,6 +3665,10 @@ int build_stl_str_hl( xfree(usefmt); } + // We have now processed the entire statusline format string. + // What follows is post-processing to handle alignment and + // highlighting factors. + int width = vim_strsize(out); if (maxwidth > 0 && width > maxwidth) { // Result is too long, must truncate somewhere. @@ -3713,11 +3711,6 @@ int build_stl_str_hl( // character will fit in the available output space trunc_p += (*mb_ptr2len)(trunc_p); } - // Fill up for half a double-wide character. - // XXX : This seems impossible given the exit condition above? - while (++width < maxwidth) { - *trunc_p++ = fillchar; - } // Otherwise put the truncation point at the end, leaving enough room // for a single-character truncation marker -- cgit