This commit is contained in:
Wayne Rowcliffe
2015-09-16 00:51:41 -05:00
parent 12e53ca95c
commit 7c32d86f7d

View File

@@ -2819,7 +2819,7 @@ void free_titles(void)
* or truncated if too long, fillchar is used for all whitespace. * or truncated if too long, fillchar is used for all whitespace.
*/ */
int int
build_stl_str_hl ( build_stl_str_hl(
win_T *wp, win_T *wp,
char_u *out, /* buffer to write into != NameBuff */ char_u *out, /* buffer to write into != NameBuff */
size_t outlen, /* length of out[] */ size_t outlen, /* length of out[] */
@@ -2850,11 +2850,11 @@ build_stl_str_hl (
} type; } type;
} item[STL_MAX_ITEM]; } item[STL_MAX_ITEM];
enum number_base { typedef enum {
DECIMAL = 10, DECIMAL = 10,
OCTAL = 8, OCTAL = 8,
HEXIDECIMAL = 16 HEXIDECIMAL = 16
}; } number_base;
#define TMPLEN 70 #define TMPLEN 70
char_u tmp[TMPLEN]; char_u tmp[TMPLEN];
@@ -2952,7 +2952,8 @@ build_stl_str_hl (
continue; 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) { if (*fmt_p == STL_MIDDLEMARK) {
fmt_p++; fmt_p++;
// Ignored when we are inside of a grouping // Ignored when we are inside of a grouping
@@ -2964,7 +2965,8 @@ build_stl_str_hl (
continue; 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) { if (*fmt_p == STL_TRUNCMARK) {
fmt_p++; fmt_p++;
item[curitem].type = Trunc; item[curitem].type = Trunc;
@@ -2981,17 +2983,19 @@ build_stl_str_hl (
} }
groupdepth--; groupdepth--;
//{ Determine how long the group is. // { Determine how long the group is.
// Note: We set the current output position to null so `vim_strsize` will work. // Note: We set the current output position to null
// so `vim_strsize` will work.
char_u *t = item[groupitem[groupdepth]].start; char_u *t = item[groupitem[groupdepth]].start;
*out_p = NUL; *out_p = NUL;
long l = vim_strsize(t); 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, // and if there were no normal items in the group,
// move the output pointer back to where the group started (erase the group). // move the output pointer back to where the group started.
// Note: This also erases any plaintext characters that were in the group. // Note: This erases any non-item characters that were in the group.
// Otherwise there would be no reason to do this step. // Otherwise there would be no reason to do this step.
if (curitem > groupitem[groupdepth] + 1 if (curitem > groupitem[groupdepth] + 1
&& item[groupitem[groupdepth]].minwid == 0) { && item[groupitem[groupdepth]].minwid == 0) {
@@ -3012,7 +3016,7 @@ build_stl_str_hl (
// If the group is longer than it is allowed to be // If the group is longer than it is allowed to be
// truncate by removing bytes from the start of the group text. // truncate by removing bytes from the start of the group text.
if (l > item[groupitem[groupdepth]].maxwid) { if (l > item[groupitem[groupdepth]].maxwid) {
//{ Determine the number of bytes to remove // { Determine the number of bytes to remove
long n; long n;
if (has_mbyte) { if (has_mbyte) {
/* Find the first character that should be included. */ /* Find the first character that should be included. */
@@ -3021,20 +3025,21 @@ build_stl_str_hl (
l -= ptr2cells(t + n); l -= ptr2cells(t + n);
n += (*mb_ptr2len)(t + n); n += (*mb_ptr2len)(t + n);
} }
} else } else {
n = (long)(out_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. // Prepend the `<` to indicate that the output was truncated.
*t = '<'; *t = '<';
//{ Move the truncated output // { Move the truncated output
memmove(t + 1, t + n, (size_t)(out_p - (t + n))); memmove(t + 1, t + n, (size_t)(out_p - (t + n)));
out_p = out_p - n + 1; out_p = out_p - n + 1;
/* Fill up space left over by half a double-wide char. */ /* Fill up space left over by half a double-wide char. */
while (++l < item[groupitem[groupdepth]].minwid) while (++l < item[groupitem[groupdepth]].minwid)
*out_p++ = fillchar; *out_p++ = fillchar;
//} // }
/* correct the start of the items for the truncation */ /* correct the start of the items for the truncation */
for (l = groupitem[groupdepth] + 1; l < curitem; l++) { 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 // If the group is right-aligned, shift everything to the right and
// prepend with filler characters. // prepend with filler characters.
} else { } 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)); memmove(t + min_group_width - l, t, (size_t)(out_p - t));
l = min_group_width - l; l = min_group_width - l;
if (out_p + l >= (out_end_p + 1)) { if (out_p + l >= (out_end_p + 1)) {
l = (long)(out_end_p - out_p); l = (long)(out_end_p - out_p);
} }
out_p += l; out_p += l;
//} // }
// Adjust item start positions // Adjust item start positions
for (int n = groupitem[groupdepth] + 1; n < curitem; n++) { for (int n = groupitem[groupdepth] + 1; n < curitem; n++) {
@@ -3173,7 +3178,7 @@ build_stl_str_hl (
char_u opt = *fmt_p++; char_u opt = *fmt_p++;
/* OK - now for the real work */ /* OK - now for the real work */
enum number_base base = DECIMAL; number_base base = DECIMAL;
bool itemisflag = false; bool itemisflag = false;
bool fillable = true; bool fillable = true;
long num = -1; long num = -1;
@@ -3212,17 +3217,18 @@ build_stl_str_hl (
fmt_p++; fmt_p++;
*out_p = 0; *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; out_p = t;
//{ Evaluate the expression // { Evaluate the expression
// Store the current buffer number as a string variable // Store the current buffer number as a string variable
vim_snprintf((char *)tmp, sizeof(tmp), "%d", curbuf->b_fnum); vim_snprintf((char *)tmp, sizeof(tmp), "%d", curbuf->b_fnum);
set_internal_string_var((char_u *)"actual_curbuf", tmp); set_internal_string_var((char_u *)"actual_curbuf", tmp);
// Switch the curbuf and curwin to the buffer and window we are evaluating the // Switch the curbuf and curwin to the buffer and window we are
// statusline for. // evaluating the statusline for.
buf_T *o_curbuf = curbuf; buf_T *o_curbuf = curbuf;
win_T *o_curwin = curwin; win_T *o_curwin = curwin;
curwin = wp; curwin = wp;
@@ -3236,9 +3242,9 @@ build_stl_str_hl (
curbuf = o_curbuf; curbuf = o_curbuf;
// Remove the variable we just stored // 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. // Check if the evaluated result is a number.
// If so, convert the number to an int and free the string. // 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 // Note: The call will only return true if it actually
// appended data to the `tmp` buffer. // 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; str = tmp;
} }
break; break;
@@ -3365,7 +3371,8 @@ build_stl_str_hl (
case STL_FILETYPE: case STL_FILETYPE:
// Copy the filetype if it is not null and the formatted string will fit // 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 if (*wp->w_buffer->b_p_ft != NUL
&& STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3) { && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3) {
vim_snprintf((char *)tmp, sizeof(tmp), "[%s]", vim_snprintf((char *)tmp, sizeof(tmp), "[%s]",
@@ -3378,7 +3385,8 @@ build_stl_str_hl (
{ {
itemisflag = true; itemisflag = true;
// Copy the filetype if it is not null and the formatted string will fit // 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 if (*wp->w_buffer->b_p_ft != NUL
&& STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2) { && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2) {
vim_snprintf((char *)tmp, sizeof(tmp), ",%s", vim_snprintf((char *)tmp, sizeof(tmp), ",%s",
@@ -3423,12 +3431,12 @@ build_stl_str_hl (
case STL_HIGHLIGHT: case STL_HIGHLIGHT:
{ {
//{ The name of the highlight is surrounded by `#` // { The name of the highlight is surrounded by `#`
char_u *t = fmt_p; char_u *t = fmt_p;
while (*fmt_p != '#' && *fmt_p != NUL) { while (*fmt_p != '#' && *fmt_p != NUL) {
++fmt_p; ++fmt_p;
} }
//} // }
// Create a highlight item based on the name // Create a highlight item based on the name
if (*fmt_p == '#') { if (*fmt_p == '#') {
@@ -3450,7 +3458,7 @@ build_stl_str_hl (
// Copy the item string into the output buffer // Copy the item string into the output buffer
if (str != NULL && *str) { 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 // and the proper conditions are met
char_u *t = str; char_u *t = str;
if (itemisflag) { if (itemisflag) {
@@ -3460,7 +3468,7 @@ build_stl_str_hl (
t++; t++;
prevchar_isflag = true; prevchar_isflag = true;
} }
//} // }
long l = vim_strsize(t); long l = vim_strsize(t);
@@ -3488,7 +3496,8 @@ build_stl_str_hl (
*out_p++ = '<'; *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) { if (minwid > 0) {
for (; l < minwid && out_p < out_end_p; l++) { 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. */
@@ -3504,7 +3513,7 @@ build_stl_str_hl (
minwid *= -1; 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) { while (*t && out_p < out_end_p) {
*out_p++ = *t++; *out_p++ = *t++;
/* Change a space by fillchar, unless fillchar is '-' and a /* Change a space by fillchar, unless fillchar is '-' and a
@@ -3513,7 +3522,7 @@ build_stl_str_hl (
&& (!ascii_isdigit(*t) || fillchar != '-')) && (!ascii_isdigit(*t) || fillchar != '-'))
out_p[-1] = fillchar; out_p[-1] = fillchar;
} }
//} // }
// For left-aligned items, fill any remaining space with the fillchar // For left-aligned items, fill any remaining space with the fillchar
for (; l < minwid && out_p < out_end_p; l++) { for (; l < minwid && out_p < out_end_p; l++) {
@@ -3526,7 +3535,7 @@ build_stl_str_hl (
break; /* not sufficient space */ break; /* not sufficient space */
prevchar_isitem = true; prevchar_isitem = true;
//{ Build the formatting string // { Build the formatting string
char_u nstr[20]; char_u nstr[20];
char_u *t = nstr; char_u *t = nstr;
if (opt == STL_VIRTCOL_ALT) { if (opt == STL_VIRTCOL_ALT) {
@@ -3542,9 +3551,9 @@ build_stl_str_hl (
*t++ = '*'; *t++ = '*';
*t++ = (char_u) (base == HEXIDECIMAL ? 'X' : (base == OCTAL ? 'o' : 'd')); *t++ = (char_u) (base == HEXIDECIMAL ? 'X' : (base == OCTAL ? 'o' : 'd'));
*t = 0; *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 // Note: We have to cast the base because the compiler uses
// unsigned ints for the enum values. // unsigned ints for the enum values.
long num_chars = 0; long num_chars = 0;
@@ -3552,11 +3561,12 @@ build_stl_str_hl (
num_chars++; 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) { if (opt == STL_VIRTCOL_ALT) {
num_chars++; num_chars++;
} }
//} // }
size_t remaining_buf_len = (out_end_p - out_p) + 1; 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. // Figure out the approximate number in "scientific" type notation.
// Ex: 14532 with maxwid of 4 -> '14>3' // Ex: 14532 with maxwid of 4 -> '14>3'
if (num_chars > maxwid) { 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; num_chars += 2;
// How many extra characters there are // How many extra characters there are
long n = num_chars - maxwid; long n = num_chars - maxwid;
//{ Reduce the number by base^n // { Reduce the number by base^n
while (num_chars-- > maxwid) { while (num_chars-- > maxwid) {
num /= base; num /= base;
} }
//} // }
//{ Add the format string for the exponent bit // { Add the format string for the exponent bit
*t++ = '>'; *t++ = '>';
*t++ = '%'; *t++ = '%';
// Use the same base as the first number // Use the same base as the first number
*t = t[-3]; *t = t[-3];
*++t = 0; *++t = 0;
//} // }
vim_snprintf((char *)out_p, remaining_buf_len, (char *)nstr, vim_snprintf((char *)out_p, remaining_buf_len, (char *)nstr,
0, num, n); 0, num, n);
@@ -3591,7 +3602,8 @@ build_stl_str_hl (
minwid, num); 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); out_p += STRLEN(out_p);
// Otherwise, there was nothing to print so mark the item as empty // 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 // Truncate at the truncation point we found
} else { } else {
//{ Determine how many bytes to remove // { Determine how many bytes to remove
long trunc_len; long trunc_len;
if (has_mbyte) { if (has_mbyte) {
trunc_len = 0; trunc_len = 0;
@@ -3702,9 +3714,9 @@ build_stl_str_hl (
// Truncate an extra character so we can insert our `<`. // Truncate an extra character so we can insert our `<`.
trunc_len = (width - maxwidth) + 1; trunc_len = (width - maxwidth) + 1;
} }
//} // }
//{ Truncate the string // { Truncate the string
char_u *trunc_end_p = trunc_p + trunc_len; char_u *trunc_end_p = trunc_p + trunc_len;
STRMOVE(trunc_p + 1, trunc_end_p); STRMOVE(trunc_p + 1, trunc_end_p);
@@ -3719,9 +3731,9 @@ build_stl_str_hl (
*trunc_p++ = fillchar; *trunc_p++ = fillchar;
*trunc_p = NUL; *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 // their position relative to our truncation point
// Note: The offset is one less than the truncation length because // Note: The offset is one less than the truncation length because
@@ -3733,17 +3745,19 @@ build_stl_str_hl (
// to be moved backwards. // to be moved backwards.
if (item[i].start >= trunc_end_p) { if (item[i].start >= trunc_end_p) {
item[i].start -= item_offset; 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 { } else {
item[i].start = trunc_p; item[i].start = trunc_p;
} }
} }
//} // }
} }
width = maxwidth; width = maxwidth;
// If there is room left in our statusline, and room left in our buffer, // 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 } else if (width < maxwidth
&& STRLEN(out) + maxwidth - width + 1 < outlen) { && STRLEN(out) + maxwidth - width + 1 < outlen) {
for (int item_idx = 0; item_idx < itemcnt; item_idx++) { for (int item_idx = 0; item_idx < itemcnt; item_idx++) {