Merge pull request #30918 from zeertzjq/vim-9.1.0802

vim-patch:9.1.{0802,0803,0804,0806,0812}: :setglobal fixes
This commit is contained in:
zeertzjq 2024-10-24 07:22:04 +08:00 committed by GitHub
commit 611c416359
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 204 additions and 148 deletions

View File

@ -255,7 +255,7 @@ int open_buffer(bool read_stdin, exarg_T *eap, int flags_arg)
emsg(_("E83: Cannot allocate buffer, using other one...")); emsg(_("E83: Cannot allocate buffer, using other one..."));
enter_buffer(curbuf); enter_buffer(curbuf);
if (old_tw != curbuf->b_p_tw) { if (old_tw != curbuf->b_p_tw) {
check_colorcolumn(curwin); check_colorcolumn(NULL, curwin);
} }
return FAIL; return FAIL;
} }
@ -1029,7 +1029,7 @@ void handle_swap_exists(bufref_T *old_curbuf)
enter_buffer(buf); enter_buffer(buf);
if (old_tw != curbuf->b_p_tw) { if (old_tw != curbuf->b_p_tw) {
check_colorcolumn(curwin); check_colorcolumn(NULL, curwin);
} }
} }
// If "old_curbuf" is NULL we are in big trouble here... // If "old_curbuf" is NULL we are in big trouble here...
@ -1669,7 +1669,7 @@ void set_curbuf(buf_T *buf, int action, bool update_jumplist)
// enter some buffer. Using the last one is hopefully OK. // enter some buffer. Using the last one is hopefully OK.
enter_buffer(valid ? buf : lastbuf); enter_buffer(valid ? buf : lastbuf);
if (old_tw != curbuf->b_p_tw) { if (old_tw != curbuf->b_p_tw) {
check_colorcolumn(curwin); check_colorcolumn(NULL, curwin);
} }
} }

View File

@ -88,13 +88,11 @@ int init_chartab(void)
/// an error, OK otherwise. /// an error, OK otherwise.
int buf_init_chartab(buf_T *buf, bool global) int buf_init_chartab(buf_T *buf, bool global)
{ {
int c;
if (global) { if (global) {
// Set the default size for printable characters: // Set the default size for printable characters:
// From <Space> to '~' is 1 (printable), others are 2 (not printable). // From <Space> to '~' is 1 (printable), others are 2 (not printable).
// This also inits all 'isident' and 'isfname' flags to false. // This also inits all 'isident' and 'isfname' flags to false.
c = 0; int c = 0;
while (c < ' ') { while (c < ' ') {
g_chartab[c++] = (dy_flags & DY_UHEX) ? 4 : 2; g_chartab[c++] = (dy_flags & DY_UHEX) ? 4 : 2;
@ -124,9 +122,7 @@ int buf_init_chartab(buf_T *buf, bool global)
SET_CHARTAB(buf, '-'); SET_CHARTAB(buf, '-');
} }
// Walk through the 'isident', 'iskeyword', 'isfname' and 'isprint' // Walk through the 'isident', 'iskeyword', 'isfname' and 'isprint' options.
// options Each option is a list of characters, character numbers or
// ranges, separated by commas, e.g.: "200-210,x,#-178,-"
for (int i = global ? 0 : 3; i <= 3; i++) { for (int i = global ? 0 : 3; i <= 3; i++) {
const char *p; const char *p;
if (i == 0) { if (i == 0) {
@ -142,16 +138,41 @@ int buf_init_chartab(buf_T *buf, bool global)
// fourth round: 'iskeyword' // fourth round: 'iskeyword'
p = buf->b_p_isk; p = buf->b_p_isk;
} }
if (parse_isopt(p, buf, false) == FAIL) {
return FAIL;
}
}
chartab_initialized = true;
return OK;
}
/// Checks the format for the option settings 'iskeyword', 'isident', 'isfname'
/// or 'isprint'.
/// Returns FAIL if has an error, OK otherwise.
int check_isopt(char *var)
{
return parse_isopt(var, NULL, true);
}
/// @param only_check if false: refill g_chartab[]
static int parse_isopt(const char *var, buf_T *buf, bool only_check)
{
const char *p = var;
// Parses the 'isident', 'iskeyword', 'isfname' and 'isprint' options.
// Each option is a list of characters, character numbers or ranges,
// separated by commas, e.g.: "200-210,x,#-178,-"
while (*p) { while (*p) {
bool tilde = false; bool tilde = false;
bool do_isalpha = false; bool do_isalpha = false;
if ((*p == '^') && (p[1] != NUL)) { if (*p == '^' && p[1] != NUL) {
tilde = true; tilde = true;
p++; p++;
} }
int c;
if (ascii_isdigit(*p)) { if (ascii_isdigit(*p)) {
c = getdigits_int((char **)&p, true, 0); c = getdigits_int((char **)&p, true, 0);
} else { } else {
@ -159,7 +180,7 @@ int buf_init_chartab(buf_T *buf, bool global)
} }
int c2 = -1; int c2 = -1;
if ((*p == '-') && (p[1] != NUL)) { if (*p == '-' && p[1] != NUL) {
p++; p++;
if (ascii_isdigit(*p)) { if (ascii_isdigit(*p)) {
@ -169,14 +190,22 @@ int buf_init_chartab(buf_T *buf, bool global)
} }
} }
if ((c <= 0) if (c <= 0 || c >= 256 || (c2 < c && c2 != -1) || c2 >= 256
|| (c >= 256) || !(*p == NUL || *p == ',')) {
|| ((c2 < c) && (c2 != -1))
|| (c2 >= 256)
|| !((*p == NUL) || (*p == ','))) {
return FAIL; return FAIL;
} }
bool trail_comma = *p == ',';
p = skip_to_option_part(p);
if (trail_comma && *p == NUL) {
// Trailing comma is not allowed.
return FAIL;
}
if (only_check) {
continue;
}
if (c2 == -1) { // not a range if (c2 == -1) { // not a range
// A single '@' (not "@-@"): // A single '@' (not "@-@"):
// Decide on letters being ID/printable/keyword chars with // Decide on letters being ID/printable/keyword chars with
@ -198,15 +227,13 @@ int buf_init_chartab(buf_T *buf, bool global)
if (!do_isalpha if (!do_isalpha
|| mb_islower(c) || mb_islower(c)
|| mb_isupper(c)) { || mb_isupper(c)) {
if (i == 0) { if (var == p_isi) { // (re)set ID flag
// (re)set ID flag
if (tilde) { if (tilde) {
g_chartab[c] &= (uint8_t) ~CT_ID_CHAR; g_chartab[c] &= (uint8_t) ~CT_ID_CHAR;
} else { } else {
g_chartab[c] |= CT_ID_CHAR; g_chartab[c] |= CT_ID_CHAR;
} }
} else if (i == 1) { } else if (var == p_isp) { // (re)set printable
// (re)set printable
if (c < ' ' || c > '~') { if (c < ' ' || c > '~') {
if (tilde) { if (tilde) {
g_chartab[c] = (uint8_t)((g_chartab[c] & ~CT_CELL_MASK) g_chartab[c] = (uint8_t)((g_chartab[c] & ~CT_CELL_MASK)
@ -217,15 +244,13 @@ int buf_init_chartab(buf_T *buf, bool global)
g_chartab[c] |= CT_PRINT_CHAR; g_chartab[c] |= CT_PRINT_CHAR;
} }
} }
} else if (i == 2) { } else if (var == p_isf) { // (re)set fname flag
// (re)set fname flag
if (tilde) { if (tilde) {
g_chartab[c] &= (uint8_t) ~CT_FNAME_CHAR; g_chartab[c] &= (uint8_t) ~CT_FNAME_CHAR;
} else { } else {
g_chartab[c] |= CT_FNAME_CHAR; g_chartab[c] |= CT_FNAME_CHAR;
} }
} else { // i == 3 } else { // (var == p_isk || var == buf->b_p_isk) (re)set keyword flag
// (re)set keyword flag
if (tilde) { if (tilde) {
RESET_CHARTAB(buf, c); RESET_CHARTAB(buf, c);
} else { } else {
@ -235,17 +260,8 @@ int buf_init_chartab(buf_T *buf, bool global)
} }
c++; c++;
} }
}
c = (uint8_t)(*p);
p = skip_to_option_part(p);
if ((c == ',') && (*p == NUL)) {
// Trailing comma is not allowed.
return FAIL;
}
}
}
chartab_initialized = true;
return OK; return OK;
} }

View File

@ -769,9 +769,15 @@ int get_number_indent(linenr_T lnum)
return (int)col; return (int)col;
} }
/// Check "briopt" as 'breakindentopt' and update the members of "wp".
/// This is called when 'breakindentopt' is changed and when a window is /// This is called when 'breakindentopt' is changed and when a window is
/// initialized /// initialized
bool briopt_check(win_T *wp) ///
/// @param briopt when NULL: use "wp->w_p_briopt"
/// @param wp when NULL: only check "briopt"
///
/// @return FAIL for failure, OK otherwise.
bool briopt_check(char *briopt, win_T *wp)
{ {
int bri_shift = 0; int bri_shift = 0;
int bri_min = 20; int bri_min = 20;
@ -779,7 +785,13 @@ bool briopt_check(win_T *wp)
int bri_list = 0; int bri_list = 0;
int bri_vcol = 0; int bri_vcol = 0;
char *p = wp->w_p_briopt; char *p = empty_string_option;
if (briopt != NULL) {
p = briopt;
} else if (wp != NULL) {
p = wp->w_p_briopt;
}
while (*p != NUL) { while (*p != NUL) {
// Note: Keep this in sync with p_briopt_values // Note: Keep this in sync with p_briopt_values
if (strncmp(p, "shift:", 6) == 0 if (strncmp(p, "shift:", 6) == 0
@ -807,6 +819,10 @@ bool briopt_check(win_T *wp)
} }
} }
if (wp == NULL) {
return OK;
}
wp->w_briopt_shift = bri_shift; wp->w_briopt_shift = bri_shift;
wp->w_briopt_min = bri_min; wp->w_briopt_min = bri_min;
wp->w_briopt_sbr = bri_sbr; wp->w_briopt_sbr = bri_sbr;

View File

@ -2592,7 +2592,7 @@ static const char *did_set_swapfile(optset_T *args)
static const char *did_set_textwidth(optset_T *args FUNC_ATTR_UNUSED) static const char *did_set_textwidth(optset_T *args FUNC_ATTR_UNUSED)
{ {
FOR_ALL_TAB_WINDOWS(tp, wp) { FOR_ALL_TAB_WINDOWS(tp, wp) {
check_colorcolumn(wp); check_colorcolumn(NULL, wp);
} }
return NULL; return NULL;
@ -5111,8 +5111,8 @@ void didset_window_options(win_T *wp, bool valid_cursor)
} else { } else {
wp->w_skipcol = 0; wp->w_skipcol = 0;
} }
check_colorcolumn(wp); check_colorcolumn(NULL, wp);
briopt_check(wp); briopt_check(NULL, wp);
fill_culopt_flags(NULL, wp); fill_culopt_flags(NULL, wp);
set_chars_option(wp, wp->w_p_fcs, kFillchars, true, NULL, 0); set_chars_option(wp, wp->w_p_fcs, kFillchars, true, NULL, 0);
set_chars_option(wp, wp->w_p_lcs, kListchars, true, NULL, 0); set_chars_option(wp, wp->w_p_lcs, kListchars, true, NULL, 0);

View File

@ -4459,7 +4459,7 @@ return {
{ {
abbreviation = 'isk', abbreviation = 'isk',
alloced = true, alloced = true,
cb = 'did_set_isopt', cb = 'did_set_iskeyword',
defaults = { if_true = '@,48-57,_,192-255' }, defaults = { if_true = '@,48-57,_,192-255' },
deny_duplicates = true, deny_duplicates = true,
desc = [=[ desc = [=[

View File

@ -717,11 +717,14 @@ const char *did_set_breakat(optset_T *args FUNC_ATTR_UNUSED)
const char *did_set_breakindentopt(optset_T *args) const char *did_set_breakindentopt(optset_T *args)
{ {
win_T *win = (win_T *)args->os_win; win_T *win = (win_T *)args->os_win;
if (briopt_check(win) == FAIL) { char **varp = (char **)args->os_varp;
if (briopt_check(*varp, varp == &win->w_p_briopt ? win : NULL) == FAIL) {
return e_invarg; return e_invarg;
} }
// list setting requires a redraw // list setting requires a redraw
if (win == curwin && win->w_briopt_list) { if (varp == &win->w_p_briopt && win->w_briopt_list) {
redraw_all_later(UPD_NOT_VALID); redraw_all_later(UPD_NOT_VALID);
} }
@ -898,7 +901,8 @@ int expand_set_clipboard(optexpand_T *args, int *numMatches, char ***matches)
const char *did_set_colorcolumn(optset_T *args) const char *did_set_colorcolumn(optset_T *args)
{ {
win_T *win = (win_T *)args->os_win; win_T *win = (win_T *)args->os_win;
return check_colorcolumn(win); char **varp = (char **)args->os_varp;
return check_colorcolumn(*varp, varp == &win->w_p_cc ? win : NULL);
} }
/// The 'comments' option is changed. /// The 'comments' option is changed.
@ -1448,8 +1452,7 @@ const char *did_set_foldmethod(optset_T *args)
{ {
win_T *win = (win_T *)args->os_win; win_T *win = (win_T *)args->os_win;
char **varp = (char **)args->os_varp; char **varp = (char **)args->os_varp;
if (check_opt_strings(*varp, p_fdm_values, false) != OK if (check_opt_strings(*varp, p_fdm_values, false) != OK || **varp == NUL) {
|| *win->w_p_fdm == NUL) {
return e_invarg; return e_invarg;
} }
foldUpdateAll(win); foldUpdateAll(win);
@ -1573,12 +1576,28 @@ int expand_set_inccommand(optexpand_T *args, int *numMatches, char ***matches)
matches); matches);
} }
/// The 'iskeyword' option is changed.
const char *did_set_iskeyword(optset_T *args)
{
char **varp = (char **)args->os_varp;
if (varp == &p_isk) { // only check for global-value
if (check_isopt(*varp) == FAIL) {
return e_invarg;
}
} else { // fallthrough for local-value
return did_set_isopt(args);
}
return NULL;
}
/// The 'isident' or the 'iskeyword' or the 'isprint' or the 'isfname' option is /// The 'isident' or the 'iskeyword' or the 'isprint' or the 'isfname' option is
/// changed. /// changed.
const char *did_set_isopt(optset_T *args) const char *did_set_isopt(optset_T *args)
{ {
buf_T *buf = (buf_T *)args->os_buf; buf_T *buf = (buf_T *)args->os_buf;
// 'isident', 'iskeyword', 'isprint or 'isfname' option: refill g_chartab[] // 'isident', 'iskeyword', 'isprint' or 'isfname' option: refill g_chartab[]
// If the new option is invalid, use old value. // If the new option is invalid, use old value.
// 'lisp' option: refill g_chartab[] for '-' char // 'lisp' option: refill g_chartab[] for '-' char
if (buf_init_chartab(buf, true) == FAIL) { if (buf_init_chartab(buf, true) == FAIL) {

View File

@ -7371,18 +7371,37 @@ static int int_cmp(const void *pa, const void *pb)
return a == b ? 0 : a < b ? -1 : 1; return a == b ? 0 : a < b ? -1 : 1;
} }
/// Handle setting 'colorcolumn' or 'textwidth' in window "wp". /// Check "cc" as 'colorcolumn' and update the members of "wp".
/// This is called when 'colorcolumn' or 'textwidth' is changed.
///
/// @param cc when NULL: use "wp->w_p_cc"
/// @param wp when NULL: only parse "cc"
/// ///
/// @return error message, NULL if it's OK. /// @return error message, NULL if it's OK.
const char *check_colorcolumn(win_T *wp) const char *check_colorcolumn(char *cc, win_T *wp)
{ {
if (wp->w_buffer == NULL) { if (wp != NULL && wp->w_buffer == NULL) {
return NULL; // buffer was closed return NULL; // buffer was closed
} }
char *s = empty_string_option;
if (cc != NULL) {
s = cc;
} else if (wp != NULL) {
s = wp->w_p_cc;
}
OptInt tw;
if (wp != NULL) {
tw = wp->w_buffer->b_p_tw;
} else {
// buffer-local value not set, assume zero
tw = 0;
}
unsigned count = 0; unsigned count = 0;
int color_cols[256]; int color_cols[256];
for (char *s = wp->w_p_cc; *s != NUL && count < 255;) { while (*s != NUL && count < 255) {
int col; int col;
if (*s == '-' || *s == '+') { if (*s == '-' || *s == '+') {
// -N and +N: add to 'textwidth' // -N and +N: add to 'textwidth'
@ -7392,16 +7411,12 @@ const char *check_colorcolumn(win_T *wp)
return e_invarg; return e_invarg;
} }
col = col * getdigits_int(&s, true, 0); col = col * getdigits_int(&s, true, 0);
if (wp->w_buffer->b_p_tw == 0) { if (tw == 0) {
goto skip; // 'textwidth' not set, skip this item goto skip; // 'textwidth' not set, skip this item
} }
assert((col >= 0 assert((col >= 0 && tw <= INT_MAX - col && tw + col >= INT_MIN)
&& wp->w_buffer->b_p_tw <= INT_MAX - col || (col < 0 && tw >= INT_MIN - col && tw + col <= INT_MAX));
&& wp->w_buffer->b_p_tw + col >= INT_MIN) col += (int)tw;
|| (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) { if (col < 0) {
goto skip; goto skip;
} }
@ -7423,6 +7438,10 @@ skip:
} }
} }
if (wp == NULL) {
return NULL; // only parse "cc"
}
xfree(wp->w_p_cc_cols); xfree(wp->w_p_cc_cols);
if (count == 0) { if (count == 0) {
wp->w_p_cc_cols = NULL; wp->w_p_cc_cols = NULL;

View File

@ -45,21 +45,7 @@ endwhile
let skip_setglobal_reasons = #{ let skip_setglobal_reasons = #{
\ iminsert: 'The global value is always overwritten by the local value', \ iminsert: 'The global value is always overwritten by the local value',
\ imsearch: 'The global value is always overwritten by the local value', \ imsearch: 'The global value is always overwritten by the local value',
\ breakindentopt: 'TODO: fix missing error handling for setglobal',
\ colorcolumn: 'TODO: fix missing error handling for setglobal',
\ conceallevel: 'TODO: fix missing error handling for setglobal',
\ foldcolumn: 'TODO: fix missing error handling for setglobal',
\ foldmethod: 'TODO: fix `setglobal fdm=` not given an error',
\ iskeyword: 'TODO: fix missing error handling for setglobal',
\ numberwidth: 'TODO: fix missing error handling for setglobal',
\ scrolloff: 'TODO: fix missing error handling for setglobal',
\ shiftwidth: 'TODO: fix missing error handling for setglobal',
\ sidescrolloff: 'TODO: fix missing error handling for setglobal',
\ signcolumn: 'TODO(nvim): fix missing error handling for setglobal', \ signcolumn: 'TODO(nvim): fix missing error handling for setglobal',
\ tabstop: 'TODO: fix missing error handling for setglobal',
\ termwinkey: 'TODO: fix missing error handling for setglobal',
\ termwinsize: 'TODO: fix missing error handling for setglobal',
\ textwidth: 'TODO: fix missing error handling for setglobal',
\ winhighlight: 'TODO(nvim): fix missing error handling for setglobal', \ winhighlight: 'TODO(nvim): fix missing error handling for setglobal',
\} \}