Skip to content

Instantly share code, notes, and snippets.

@jcowgill
Created October 23, 2017 10:00
Show Gist options
  • Save jcowgill/850e445081ace7c04c2fa9fa3b234130 to your computer and use it in GitHub Desktop.
Save jcowgill/850e445081ace7c04c2fa9fa3b234130 to your computer and use it in GitHub Desktop.
Testcase for LuaJIT/LuaJIT#362
local w = function(s)
if s:match('^ %.') then
print(s .. ',')
else
print(s)
end
end
local cstr = function(s)
return '"' .. s:gsub('["\\]', '\\%0'):gsub('\t', '\\t') .. '"'
end
local macros=function(s)
return function()
return s
end
end
local options = {
{
full_name='backupcopy', abbreviation='bkc',
type='string', list='onecomma', scope={'global', 'buffer'},
deny_duplicates=true,
vim=true,
varname='p_bkc',
defaults={
condition='UNIX',
if_true={vi="yes", vim="auto"},
if_false={vi="auto", vim="auto"}
},
},
{
full_name='fileignorecase', abbreviation='fic',
type='bool', scope={'global'},
vi_def=true,
varname='p_fic',
defaults={
condition='CASE_INSENSITIVE_FILENAME',
if_true={vi=true},
if_false={vi=false},
}
},
{
full_name='foldenable', abbreviation='fen',
type='bool', scope={'window'},
vi_def=true,
redraw={'current_window'},
defaults={if_true={vi=true}}
},
{
full_name='foldexpr', abbreviation='fde',
type='string', scope={'window'},
vi_def=true,
vim=true,
alloced=true,
redraw={'current_window'},
defaults={if_true={vi="0"}}
},
{
full_name='foldignore', abbreviation='fdi',
type='string', scope={'window'},
vi_def=true,
vim=true,
alloced=true,
redraw={'current_window'},
defaults={if_true={vi="#"}}
},
{
full_name='foldlevel', abbreviation='fdl',
type='number', scope={'window'},
vi_def=true,
redraw={'current_window'},
defaults={if_true={vi=0}}
},
{
full_name='foldlevelstart', abbreviation='fdls',
type='number', scope={'global'},
vi_def=true,
redraw={'curswant'},
varname='p_fdls',
defaults={if_true={vi=-1}}
},
{
full_name='foldmarker', abbreviation='fmr',
type='string', list='onecomma', scope={'window'},
deny_duplicates=true,
vi_def=true,
vim=true,
alloced=true,
redraw={'current_window'},
defaults={if_true={vi="{{{,}}}"}}
},
{
full_name='foldmethod', abbreviation='fdm',
type='string', scope={'window'},
vi_def=true,
vim=true,
alloced=true,
redraw={'current_window'},
defaults={if_true={vi="manual"}}
},
{
full_name='foldminlines', abbreviation='fml',
type='number', scope={'window'},
vi_def=true,
redraw={'current_window'},
defaults={if_true={vi=1}}
},
{
full_name='foldnestmax', abbreviation='fdn',
type='number', scope={'window'},
vi_def=true,
redraw={'current_window'},
defaults={if_true={vi=20}}
},
{
full_name='foldopen', abbreviation='fdo',
type='string', list='onecomma', scope={'global'},
deny_duplicates=true,
vi_def=true,
redraw={'curswant'},
varname='p_fdo',
defaults={if_true={vi="block,hor,mark,percent,quickfix,search,tag,undo"}}
},
{
full_name='foldtext', abbreviation='fdt',
type='string', scope={'window'},
vi_def=true,
vim=true,
alloced=true,
redraw={'current_window'},
defaults={if_true={vi="foldtext()"}}
},
{
full_name='formatexpr', abbreviation='fex',
type='string', scope={'buffer'},
vi_def=true,
vim=true,
alloced=true,
varname='p_fex',
defaults={if_true={vi=""}}
},
{
full_name='formatoptions', abbreviation='fo',
type='string', list='flags', scope={'buffer'},
vim=true,
alloced=true,
varname='p_fo',
defaults={if_true={vi=macros('DFLT_FO_VI'), vim=macros('DFLT_FO_VIM')}}
},
{
full_name='grepprg', abbreviation='gp',
type='string', scope={'global', 'buffer'},
secure=true,
vi_def=true,
expand=true,
varname='p_gp',
defaults={
condition='UNIX',
-- Add an extra file name so that grep will always
-- insert a file name in the match line. */
if_true={vi="grep -n $* /dev/null"},
if_false={vi="grep -n "},
}
},
}
local type_flags={
bool='P_BOOL',
number='P_NUM',
string='P_STRING',
}
local redraw_flags={
statuslines='P_RSTAT',
current_window='P_RWIN',
current_window_only='P_RWINONLY',
current_buffer='P_RBUF',
all_windows='P_RALL',
everything='P_RCLR',
curswant='P_CURSWANT',
}
local list_flags={
comma='P_COMMA',
onecomma='P_ONECOMMA',
flags='P_FLAGLIST',
flagscomma='P_COMMA|P_FLAGLIST',
}
local get_flags = function(o)
local ret = {type_flags[o.type]}
local add_flag = function(f)
ret[1] = ret[1] .. '|' .. f
end
if o.list then
add_flag(list_flags[o.list])
end
if o.redraw then
for _, r_flag in ipairs(o.redraw) do
add_flag(redraw_flags[r_flag])
end
end
if o.expand then
add_flag('P_EXPAND')
if o.expand == 'nodefault' then
add_flag('P_NO_DEF_EXP')
end
end
for _, flag_desc in ipairs({
{'alloced'},
{'nodefault'},
{'no_mkrc'},
{'vi_def'},
{'vim'},
{'secure'},
{'gettext'},
{'noglob'},
{'normal_fname_chars', 'P_NFNAME'},
{'pri_mkrc'},
{'deny_in_modelines', 'P_NO_ML'},
{'deny_duplicates', 'P_NODUP'},
}) do
local key_name = flag_desc[1]
local def_name = flag_desc[2] or ('P_' .. key_name:upper())
if o[key_name] then
add_flag(def_name)
end
end
return ret[1]
end
local get_cond
get_cond = function(c, base_string)
local cond_string = base_string or '#if '
if type(c) == 'table' then
cond_string = cond_string .. get_cond(c[1], '')
for i, subc in ipairs(c) do
if i > 1 then
cond_string = cond_string .. ' && ' .. get_cond(subc, '')
end
end
elseif c:sub(1, 1) == '!' then
cond_string = cond_string .. '!defined(' .. c:sub(2) .. ')'
else
cond_string = cond_string .. 'defined(' .. c .. ')'
end
return cond_string
end
value_dumpers = {
['function']=function(v) return v() end,
string=cstr,
boolean=function(v) return v and 'true' or 'false' end,
number=function(v) return ('%iL'):format(v) end,
['nil']=function(v) return '0L' end,
}
local get_value = function(v)
return '(char_u *) ' .. value_dumpers[type(v)](v)
end
local get_defaults = function(d)
return '{' .. get_value(d.vi) .. ', ' .. get_value(d.vim) .. '}'
end
local dump_option = function(i, o)
w(' [' .. ('%u'):format(i - 1) .. ']={')
w(' .fullname=' .. cstr(o.full_name))
if o.abbreviation then
w(' .shortname=' .. cstr(o.abbreviation))
end
w(' .flags=' .. get_flags(o))
if o.enable_if then
w(get_cond(o.enable_if))
end
if o.varname then
w(' .var=(char_u *)&' .. o.varname)
elseif #o.scope == 1 and o.scope[1] == 'window' then
w(' .var=VAR_WIN')
end
if o.enable_if then
w('#endif')
end
if #o.scope == 1 and o.scope[1] == 'global' then
w(' .indir=PV_NONE')
else
local min_scope = o.scope[#o.scope]
local varname = o.pv_name or o.varname or (
'p_' .. (o.abbreviation or o.full_name))
local pv_name = (
'OPT_' .. min_scope:sub(1, 3):upper() .. '(' .. (
min_scope:sub(1, 1):upper() .. 'V_' .. varname:sub(3):upper()
) .. ')'
)
if #o.scope == 2 then
pv_name = 'OPT_BOTH(' .. pv_name .. ')'
end
w(' .indir=' .. pv_name)
end
if o.defaults then
if o.defaults.condition then
w(get_cond(o.defaults.condition))
end
w(' .def_val=' .. get_defaults(o.defaults.if_true))
if o.defaults.condition then
if o.defaults.if_false then
w('#else')
w(' .def_val=' .. get_defaults(o.defaults.if_false))
end
w('#endif')
end
end
w(' },')
end
for i, o in ipairs(options) do
dump_option(i, o)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment