From 71538f6707268ceabb76571b8561b768437d9fbe Mon Sep 17 00:00:00 2001 From: Alin Mr Date: Wed, 20 Mar 2024 22:56:38 +0200 Subject: [PATCH 1/4] lift up execute() check into s:Execute() --- plugin/sensible.vim | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/plugin/sensible.vim b/plugin/sensible.vim index 0fc26e0..c17906d 100644 --- a/plugin/sensible.vim +++ b/plugin/sensible.vim @@ -15,17 +15,22 @@ if &compatible set nocompatible endif +if exists('*execute') + let s:Execute = function('execute') +else + function! s:Execute(cmd) abort + redir => out + silent verbose execute a:cmd + redir END + return out + endfunction +endif + " Check if an option was set from a file in $HOME. This lets us avoid " overriding options in the user's vimrc, but still override options in the " system vimrc. function! s:MaySet(option) abort - if exists('*execute') - let out = execute('verbose setglobal all ' . a:option . '?') - else - redir => out - silent verbose execute 'setglobal all' a:option . '?' - redir END - endif + let out = s:Execute('verbose setglobal all ' . a:option . '?') return out !~# " \\(\\~[\\/][^\n]*\\|Lua\\)$" endfunction From d6cb88ac44921e13e416cd43fbc2c78437df3a83 Mon Sep 17 00:00:00 2001 From: Alin Mr Date: Wed, 20 Mar 2024 22:57:22 +0200 Subject: [PATCH 2/4] accumulate :set's, perform at end --- plugin/sensible.vim | 117 +++++++++++++++++++++++++++----------------- 1 file changed, 73 insertions(+), 44 deletions(-) diff --git a/plugin/sensible.vim b/plugin/sensible.vim index c17906d..0ac266f 100644 --- a/plugin/sensible.vim +++ b/plugin/sensible.vim @@ -26,24 +26,32 @@ else endfunction endif -" Check if an option was set from a file in $HOME. This lets us avoid -" overriding options in the user's vimrc, but still override options in the -" system vimrc. -function! s:MaySet(option) abort - let out = s:Execute('verbose setglobal all ' . a:option . '?') - return out !~# " \\(\\~[\\/][^\n]*\\|Lua\\)$" +let s:defaultval_yes = ['set ', ''] +let s:defaultval_no = ['set no', ''] +let s:defaultval_checked = ['" ', ''] " translates to comment -> no :set +let s:default_opts = [] +let s:default_vals = {} +let s:option_query_str = ' ' +function! s:AddOptionQuery(option) + let s:option_query_str .= a:option . '? ' +endfunction +function! s:DefaultSet(option, setcmd) abort + if exists('&' . a:option) + call s:AddOptionQuery(a:option) + call add(s:default_opts, a:option) + let s:default_vals[a:option] = a:setcmd[0] . a:option . a:setcmd[1] + endif +endfunction +function! s:Default(option, value) abort + call s:DefaultSet(a:option, ['set ', '=' . a:value]) endfunction -if s:MaySet('backspace') - set backspace=indent,eol,start -endif +call s:Default('backspace', 'indent,eol,start') " Disable completing keywords in included files (e.g., #include in C). When " configured properly, this can result in the slow, recursive scanning of " hundreds of files of dubious relevance. set complete-=i -if s:MaySet('smarttab') - set smarttab -endif +call s:DefaultSet('smarttab', s:defaultval_yes) set nrformats-=octal @@ -54,8 +62,8 @@ if !has('nvim') && &ttimeoutlen == -1 set ttimeoutlen=100 endif -if has('reltime') && s:MaySet('incsearch') - set incsearch +if has('reltime') + call s:DefaultSet('incsearch', s:defaultval_yes) endif " Use CTRL-L to clear the highlighting of 'hlsearch' (off by default) and call " :diffupdate. @@ -63,31 +71,28 @@ if maparg('', 'n') ==# '' nnoremap :nohlsearch=has('diff')?'diffupdate':'' endif -if s:MaySet('laststatus') - set laststatus=2 -endif -if s:MaySet('ruler') - set ruler -endif -if s:MaySet('wildmenu') - set wildmenu -endif +call s:Default('laststatus', '2') +call s:DefaultSet('ruler', s:defaultval_yes) +call s:DefaultSet('wildmenu', s:defaultval_yes) + +call s:Default('scrolloff', '1') + +call s:AddOptionQuery('sidescroll') +call s:AddOptionQuery('sidescrolloff') +" call at the end +function! s:SetSidescroll() abort + if s:MaySet('sidescroll') && s:MaySet('sidescrolloff') + set sidescroll=1 + set sidescrolloff=2 + endif +endfunction -if s:MaySet('scrolloff') - set scrolloff=1 -endif -if s:MaySet('sidescroll') && s:MaySet('sidescrolloff') - set sidescroll=1 - set sidescrolloff=2 -endif set display+=lastline if has('patch-7.4.2109') set display+=truncate endif -if s:MaySet('listchars') - set listchars=tab:>\ ,trail:-,extends:>,precedes:<,nbsp:+ -endif +call s:Default('listchars', 'tab:>\ ,trail:-,extends:>,precedes:<,nbsp:+') " Delete comment character when joining commented lines. if v:version > 703 || v:version == 703 && has("patch541") @@ -100,16 +105,10 @@ if has('path_extra') && (',' . &g:tags . ',') =~# ',\./tags,' setglobal tags-=./tags tags-=./tags; tags^=./tags; endif -if s:MaySet('autoread') - set autoread -endif +call s:DefaultSet('autoread', s:defaultval_yes) -if s:MaySet('history') - set history=1000 -endif -if s:MaySet('tabpagemax') - set tabpagemax=50 -endif +call s:Default('history', '1000') +call s:Default('tabpagemax', '50') " Persist g:UPPERCASE variables, used by some plugins, in .viminfo. if !empty(&viminfo) @@ -131,10 +130,36 @@ if &shell =~# 'fish$' && (v:version < 704 || v:version == 704 && !has('patch276' endif " Disable a legacy behavior that can break plugin maps. -if has('langmap') && exists('+langremap') && &langremap && s:MaySet('langremap') - set nolangremap +if has('langmap') && exists('+langremap') && &langremap + call s:DefaultSet('langremap', s:defaultval_no) endif +function! s:CheckDefaults() abort + let s:global_settings = s:Execute('verbose setglobal all lines? ' . s:option_query_str) + " 'lines=' delimits the start of the answer + let s:global_settings = substitute(s:global_settings, '.*lines=[^\n]*', '', '') . "\n" + " ^ begins and ends in newline +endfunction + +" Check if an option was set from a file in $HOME. This lets us avoid +" overriding options in the user's vimrc, but still override options in the +" system vimrc. +let s:homerx = " \\(\\~[\\/][^\n]*\\|Lua\\)\n" +function! s:MaySet(option) abort + "return s:Execute('verbose setglobal all ' . a:option . '?') . "\n" !~# s:homerx " previous version + return s:global_settings !~# ('\n\(\s\+\|no\)\?' . a:option . '\(=[^\n]*\n\|\n\)' . '[^\n]*' . s:homerx) +endfunction + +function! s:ComputeSettings() abort + call filter(s:default_opts, 's:MaySet(v:val)') + return join(map(s:default_opts, 's:default_vals[v:val]'), "\n") +endfunction + +call s:CheckDefaults() +let s:computed_settings = s:ComputeSettings() +call s:Execute(s:computed_settings) +call s:SetSidescroll() + if !(exists('g:did_load_filetypes') && exists('g:did_load_ftplugin') && exists('g:did_indent_on')) filetype plugin indent on endif @@ -169,3 +194,7 @@ endif if exists(':Man') != 2 && !exists('g:loaded_man') && &filetype !=? 'man' && !has('nvim') runtime ftplugin/man.vim endif + +function! s:GetComputedSettings() abort " DEBUG: call with from :scriptnames + return s:computed_settings +endfun From 66c1a95d63877009cbd5ae1d8845cfc8a360fb3d Mon Sep 17 00:00:00 2001 From: Alin Mr Date: Sun, 24 Mar 2024 14:34:52 +0200 Subject: [PATCH 3/4] support unqueried options --- plugin/sensible.vim | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/plugin/sensible.vim b/plugin/sensible.vim index 0ac266f..c868eb3 100644 --- a/plugin/sensible.vim +++ b/plugin/sensible.vim @@ -32,18 +32,25 @@ let s:defaultval_checked = ['" ', ''] " translates to comment -> no :set let s:default_opts = [] let s:default_vals = {} let s:option_query_str = ' ' +let s:option_not_queried = { } function! s:AddOptionQuery(option) + let s:option_not_queried[a:option] = 0 let s:option_query_str .= a:option . '? ' endfunction -function! s:DefaultSet(option, setcmd) abort - if exists('&' . a:option) - call s:AddOptionQuery(a:option) +function! s:DefaultSet(option, setcmd, query_it = 1) abort + if ! a:query_it || exists('&' . a:option) + if a:query_it + call s:AddOptionQuery(a:option) + endif call add(s:default_opts, a:option) let s:default_vals[a:option] = a:setcmd[0] . a:option . a:setcmd[1] endif endfunction -function! s:Default(option, value) abort - call s:DefaultSet(a:option, ['set ', '=' . a:value]) +function! s:DefaultMod(option, mod, value, query_it = 1) abort + call s:DefaultSet(a:option, ['set ', a:mod . a:value], a:query_it) +endfunction +function! s:Default(option, value, query_it = 1) abort + call s:DefaultSet(a:option, ['set ', '=' . a:value], a:query_it) endfunction call s:Default('backspace', 'indent,eol,start') @@ -147,7 +154,7 @@ endfunction let s:homerx = " \\(\\~[\\/][^\n]*\\|Lua\\)\n" function! s:MaySet(option) abort "return s:Execute('verbose setglobal all ' . a:option . '?') . "\n" !~# s:homerx " previous version - return s:global_settings !~# ('\n\(\s\+\|no\)\?' . a:option . '\(=[^\n]*\n\|\n\)' . '[^\n]*' . s:homerx) + return get(s:option_not_queried, a:option, 1) || s:global_settings !~# ('\n\(\s\+\|no\)\?' . a:option . '\(=[^\n]*\n\|\n\)' . '[^\n]*' . s:homerx) endfunction function! s:ComputeSettings() abort From 13bd44664b451881106be2a831313605f7db7003 Mon Sep 17 00:00:00 2001 From: Alin Mr Date: Sun, 24 Mar 2024 15:00:39 +0200 Subject: [PATCH 4/4] accumulate non-MaySet() options too --- plugin/sensible.vim | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/plugin/sensible.vim b/plugin/sensible.vim index c868eb3..399bd79 100644 --- a/plugin/sensible.vim +++ b/plugin/sensible.vim @@ -57,16 +57,16 @@ call s:Default('backspace', 'indent,eol,start') " Disable completing keywords in included files (e.g., #include in C). When " configured properly, this can result in the slow, recursive scanning of " hundreds of files of dubious relevance. -set complete-=i +call s:DefaultMod('complete', '-=', 'i', 0) call s:DefaultSet('smarttab', s:defaultval_yes) -set nrformats-=octal +call s:DefaultMod('nrformats', '-=', 'octal', 0) " Make the escape key more responsive by decreasing the wait time for an " escape sequence (e.g., arrow keys). if !has('nvim') && &ttimeoutlen == -1 - set ttimeout - set ttimeoutlen=100 + call s:DefaultSet('ttimeout', s:defaultval_yes, 0) + call s:Default('ttimeoutlen', '100', 0) endif if has('reltime') @@ -94,16 +94,17 @@ function! s:SetSidescroll() abort endif endfunction -set display+=lastline +let s:display_mods='lastline' if has('patch-7.4.2109') - set display+=truncate + let s:display_mods .= ',truncate' endif +call s:DefaultMod('display', '+=', s:display_mods, 0) call s:Default('listchars', 'tab:>\ ,trail:-,extends:>,precedes:<,nbsp:+') " Delete comment character when joining commented lines. if v:version > 703 || v:version == 703 && has("patch541") - set formatoptions+=j + call s:DefaultMod('formatoptions', '+=', 'j', 0) endif " Replace the check for a tags file in the parent directory of the current @@ -123,17 +124,17 @@ if !empty(&viminfo) endif " Saving options in session and view files causes more problems than it " solves, so disable it. -set sessionoptions-=options -set viewoptions-=options +call s:DefaultMod('sessionoptions', '-=', 'options', 0) +call s:DefaultMod('viewoptions', '-=', 'options', 0) " Allow color schemes to do bright colors without forcing bold. if &t_Co == 8 && $TERM !~# '^Eterm' - set t_Co=16 + call s:Default('t_Co', '16', 0) endif " If the running Vim lacks support for the Fish shell, use Bash instead. if &shell =~# 'fish$' && (v:version < 704 || v:version == 704 && !has('patch276')) - set shell=/usr/bin/env\ bash + call s:Default('shell', '/usr/bin/env\ bash', 0) endif " Disable a legacy behavior that can break plugin maps.