Skip to content

Commit

Permalink
Remember when options where reset because of the paste/binary option
Browse files Browse the repository at this point in the history
Mention if depending options for 'binary' or 'paste' have been reset
indirectly. Add a test to verify it works.

Also noticed as small bug, that the global option value for expandtab
was not reset when paste option is set, so fix that while at it.

Regarding the test: for some reason, setting paste does somehow affect
how the internal terminal vim renders the output so we cannot directly
use term_getline(), it will always return an empty vim line. Perhaps Vim
does not correctly redraw when paste option is in affect (probably to
make processing faster). Therefore, we will redir the output and
post-process it a bit (removing path components, removing empty lines)
so that it is easier to scrape the actual buffer line later.

fixes: vim#12837
  • Loading branch information
chrisbra committed Aug 21, 2023
1 parent a08c586 commit 13c5d58
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/option.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,21 @@ static void check_winopt(winopt_T *wop);
static int wc_use_keyname(char_u *varp, long *wcp);
static void compatible_set(void);

#if defined(FEAT_EVAL) || defined(PROTO)
static char *(p_bin_dep_opts[]) = {"textwidth", "wrapmargin", "modeline", "expandtab", NULL};
static char *(p_paste_dep_opts[]) = {"autoindent", "expandtab", "ruler", "showmatch", "smarttab",
"softtabstop", "textwidth", "wrapmargin",
#ifdef FEAT_RIGTHLEFT
"hkmap", "revins",
#endif
#ifdef FEAT_VARTABS
"varsofttabstop",
#endif
NULL};
static void didset_options_sctx(int opt_flags, char **buf);
#endif


/*
* Initialize the 'shell' option to a default value.
*/
Expand Down Expand Up @@ -2763,6 +2778,10 @@ set_options_bin(
p_et = p_et_nobin;
}
}
#ifdef FEAT_EVAL
// Remember where the dependent option were reset
didset_options_sctx(opt_flags, p_bin_dep_opts);
#endif
}

/*
Expand Down Expand Up @@ -3846,6 +3865,7 @@ did_set_paste(optset_T *args UNUSED)
p_wm = 0;
p_sts = 0;
p_ai = 0;
p_et = 0;
#ifdef FEAT_VARTABS
if (p_vsts)
free_string_option(p_vsts);
Expand Down Expand Up @@ -3902,6 +3922,11 @@ did_set_paste(optset_T *args UNUSED)

old_p_paste = p_paste;

#ifdef FEAT_EVAL
// Remember where the dependent options were reset
didset_options_sctx((OPT_LOCAL | OPT_GLOBAL), p_paste_dep_opts);
#endif

return NULL;
}

Expand Down Expand Up @@ -8170,3 +8195,22 @@ option_set_callback_func(char_u *optval UNUSED, callback_T *optcb UNUSED)
return FAIL;
#endif
}

#if defined(FEAT_EVAL) || defined(PROTO)
static void
didset_options_sctx(int opt_flags, char **buf)
{
int i;
int idx;

for (i = 0; ; ++i)
{
if (buf[i] == NULL)
break;

idx = findoption((char_u *)buf[i]);
if (idx >= 0)
set_option_sctx_idx(idx, opt_flags, current_sctx);
}
}
#endif
85 changes: 85 additions & 0 deletions src/testdir/test_options.vim
Original file line number Diff line number Diff line change
Expand Up @@ -1774,4 +1774,89 @@ func Test_set_option_window_global_local_all()
bw!
endfunc

func Test_paste_depending_options()
CheckRunVimInTerminal
" setting the paste option, resets all dependent options
" and will be reported correctly using :verbose set <option>?
let lines =<< trim [CODE]
" set paste test
set autoindent
set expandtab
" disabled, because depends on compiled feature set
" set hkmap
" set revins
" set varsofttabstop=8,32,8
set ruler
set showmatch
set smarttab
set softtabstop=4
set textwidth=80
set wrapmargin=10

source Xvimrc2
[CODE]

call writefile(lines, 'Xvimrc1', 'D')
call writefile(['set paste'], 'Xvimrc2', 'D')
let buf = RunVimInTerminal('-N -u Xvimrc1', {'rows': 10})
call TermWait(buf)
call term_sendkeys(buf, ":redir @a\<CR>")
call term_sendkeys(buf, ":verbose set expandtab?\<CR>")
call term_sendkeys(buf, ":verbose setl expandtab?\<CR>")
call term_sendkeys(buf, ":verbose setg expandtab?\<CR>")
call term_sendkeys(buf, ":redir end\<cr>")
call term_sendkeys(buf, ":put a\<cr>")
" remove path components
call term_sendkeys(buf, ":%s#\\([\\\\/]\\).*\\1##\<cr>")
" remove empty lines
call term_sendkeys(buf, ":v/./d\<cr>")
call term_sendkeys(buf, ":1\<cr>")
call term_sendkeys(buf, ":redraw!\<cr>")
call TermWait(buf)
call WaitForAssert({-> assert_match('noexpandtab', term_getline(buf, 1))}, 1000)
call WaitForAssert({-> assert_match('Last set from .*Xvimrc2 line 1', term_getline(buf, 2))}, 1000)
call WaitForAssert({-> assert_match('noexpandtab', term_getline(buf, 3))}, 1000)
call WaitForAssert({-> assert_match('Last set from .*Xvimrc2 line 1', term_getline(buf, 4))}, 1000)
call WaitForAssert({-> assert_match('noexpandtab', term_getline(buf, 5))}, 1000)
call WaitForAssert({-> assert_match('Last set from .*Xvimrc2 line 1', term_getline(buf, 6))}, 1000)
call StopVimInTerminal(buf)
endfunc

func Test_binary_depending_options()
CheckRunVimInTerminal
" setting the paste option, resets all dependent options
" and will be reported correctly using :verbose set <option>?
let lines =<< trim [CODE]
" set binary test
set autoindent

source Xvimrc_bin2
[CODE]

call writefile(lines, 'Xvimrc_bin', 'D')
call writefile(['set binary'], 'Xvimrc_bin2', 'D')
let buf = RunVimInTerminal('-N -u Xvimrc_bin', {'rows': 10})
call TermWait(buf)
call term_sendkeys(buf, ":redir @a\<CR>")
call term_sendkeys(buf, ":verbose set expandtab?\<CR>")
call term_sendkeys(buf, ":verbose setl expandtab?\<CR>")
call term_sendkeys(buf, ":verbose setg expandtab?\<CR>")
call term_sendkeys(buf, ":redir end\<cr>")
call term_sendkeys(buf, ":put a\<cr>")
" remove path components
call term_sendkeys(buf, ":%s#\\([\\\\/]\\).*\\1##\<cr>")
" remove empty lines
call term_sendkeys(buf, ":v/./d\<cr>")
call term_sendkeys(buf, ":1\<cr>")
call term_sendkeys(buf, ":redraw!\<cr>")
call TermWait(buf)
call WaitForAssert({-> assert_match('noexpandtab', term_getline(buf, 1))}, 1000)
call WaitForAssert({-> assert_match('Last set from .*Xvimrc_bin2 line 1', term_getline(buf, 2))}, 1000)
call WaitForAssert({-> assert_match('noexpandtab', term_getline(buf, 3))}, 1000)
call WaitForAssert({-> assert_match('Last set from .*Xvimrc_bin2 line 1', term_getline(buf, 4))}, 1000)
call WaitForAssert({-> assert_match('noexpandtab', term_getline(buf, 5))}, 1000)
call WaitForAssert({-> assert_match('Last set from .*Xvimrc_bin2 line 1', term_getline(buf, 6))}, 1000)
call StopVimInTerminal(buf)
endfunc

" vim: shiftwidth=2 sts=2 expandtab

0 comments on commit 13c5d58

Please sign in to comment.