diff options
author | zeertzjq <zeertzjq@outlook.com> | 2022-08-19 19:20:39 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-08-19 19:20:39 +0800 |
commit | 2af9be3db59b2e26268dc62cb65e673e2f7d4783 (patch) | |
tree | 37d12b5e0ff7b9847c2504c544e691733015a577 /src/nvim/window.c | |
parent | 5dc43265b1f966a9653cc552fa5301ee487a8116 (diff) | |
download | rneovim-2af9be3db59b2e26268dc62cb65e673e2f7d4783.tar.gz rneovim-2af9be3db59b2e26268dc62cb65e673e2f7d4783.tar.bz2 rneovim-2af9be3db59b2e26268dc62cb65e673e2f7d4783.zip |
vim-patch:8.1.1966: some code in options.c fits better elsewhere (#19840)
Problem: Some code in options.c fits better elsewhere.
Solution: Move functions from options.c to other files. (Yegappan
Lakshmanan, closes vim/vim#4889)
https://github.com/vim/vim/commit/e677df8d93772a705f40a94f3c871aee78fe4d99
Diffstat (limited to 'src/nvim/window.c')
-rw-r--r-- | src/nvim/window.c | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/src/nvim/window.c b/src/nvim/window.c index 496bc13a37..564b5c4c51 100644 --- a/src/nvim/window.c +++ b/src/nvim/window.c @@ -7269,6 +7269,88 @@ static bool frame_check_width(const frame_T *topfrp, int width) return true; } +/// Simple int comparison function for use with qsort() +static int int_cmp(const void *a, const void *b) +{ + return *(const int *)a - *(const int *)b; +} + +/// Handle setting 'colorcolumn' or 'textwidth' in window "wp". +/// +/// @return error message, NULL if it's OK. +char *check_colorcolumn(win_T *wp) +{ + char *s; + int col; + unsigned int count = 0; + int color_cols[256]; + int j = 0; + + if (wp->w_buffer == NULL) { + return NULL; // buffer was closed + } + + for (s = (char *)wp->w_p_cc; *s != NUL && count < 255;) { + if (*s == '-' || *s == '+') { + // -N and +N: add to 'textwidth' + col = (*s == '-') ? -1 : 1; + s++; + if (!ascii_isdigit(*s)) { + return e_invarg; + } + col = col * getdigits_int(&s, true, 0); + if (wp->w_buffer->b_p_tw == 0) { + goto skip; // 'textwidth' not set, skip this item + } + assert((col >= 0 + && wp->w_buffer->b_p_tw <= INT_MAX - col + && wp->w_buffer->b_p_tw + col >= INT_MIN) + || (col < 0 + && wp->w_buffer->b_p_tw >= INT_MIN - col + && wp->w_buffer->b_p_tw + col <= INT_MAX)); + col += (int)wp->w_buffer->b_p_tw; + if (col < 0) { + goto skip; + } + } else if (ascii_isdigit(*s)) { + col = getdigits_int(&s, true, 0); + } else { + return e_invarg; + } + color_cols[count++] = col - 1; // 1-based to 0-based +skip: + if (*s == NUL) { + break; + } + if (*s != ',') { + return e_invarg; + } + if (*++s == NUL) { + return e_invarg; // illegal trailing comma as in "set cc=80," + } + } + + xfree(wp->w_p_cc_cols); + if (count == 0) { + wp->w_p_cc_cols = NULL; + } else { + wp->w_p_cc_cols = xmalloc(sizeof(int) * (count + 1)); + // sort the columns for faster usage on screen redraw inside + // win_line() + qsort(color_cols, count, sizeof(int), int_cmp); + + for (unsigned int i = 0; i < count; i++) { + // skip duplicates + if (j == 0 || wp->w_p_cc_cols[j - 1] != color_cols[i]) { + wp->w_p_cc_cols[j++] = color_cols[i]; + } + } + wp->w_p_cc_cols[j] = -1; // end marker + } + + return NULL; // no error +} + int win_getid(typval_T *argvars) { if (argvars[0].v_type == VAR_UNKNOWN) { |