robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
fd4655ea3a
master
1-- USAGE: 2-- 3-- # Optional: Delete cache to get latest terminfo from internet. 4-- rm -rf /tmp/nvim_terminfo/ 5-- 6-- # Optional: Ensure the latest ncurses+tic is in your PATH. 7-- export PATH="/opt/homebrew/Cellar/ncurses/6.5/bin/":"$PATH" 8-- 9-- nvim -l src/gen/gen_terminfo.lua 10-- 11-- This script does: 12-- 13-- 1. Download Dickey's terminfo.src 14-- 2. Compile temporary terminfo database from terminfo.src 15-- 3. Use database to generate src/nvim/tui/terminfo_defs.h 16 17local url ='https://invisible-island.net/datafiles/current/terminfo.src.gz' 18local target_gen ='src/nvim/tui/terminfo_builtin.h' 19local target_enum ='src/nvim/tui/terminfo_enum_defs.h' 20 21local terminfo =require ( 'src.gen.terminfo' ) 22local entries =terminfo . builtin_terminals 23local wanted_strings =terminfo . fields . strings 24local wanted_strings_ext =terminfo . fields . strings_ext 25local wanted_bools =terminfo . fields . bools 26local wanted_ints =terminfo . fields . ints 27local wanted_termkeys =terminfo . fields . termkeys 28 29local db ='/tmp/nvim_terminfo' 30if vim . uv . fs_stat ( db ) == nil then 31local function sys ( cmd ) 32( cmd ) 33os . execute ( cmd ) 34end 35sys ( 'curl -O ' .. url ) 36sys ( 'gunzip -f terminfo.src.gz' ) 37sys (( 'cat terminfo.src scripts/windows.ti | tic -x -o "%s" -' ): format ( db )) 38sys ( 'rm -f terminfo.src' ) 39else 40( 'using cached terminfo in ' .. db ) 41end 42 43local function enumify ( str ) 44return 'kTerm_' .. str 45end 46local function quote ( str ) 47if str == nil then 48return 'NULL' 49end 50-- remungle the strings to look like C strings 51str =string . gsub ( str , '\\E' , '\\033' ) 52str =string . gsub ( str , '%^G' , '\\a' ) 53str =string . gsub ( str , '%^H' , '\\b' ) 54str =string . gsub ( str , '%^O' , '\\017' ) -- o dod 55-- str = string.gsub(str, "\\", "\\\\") 56str =string . gsub ( str , '"' , '\\"' ) 57return '"' .. str .. '"' 58end 59 60--- @type fun(...: any) 61local dbg =function () end 62-- dbg = print 63 64local f_enum =assert ( io . open ( target_enum , 'wb' )) 65f_enum : write ( '// generated by src/gen/gen_terminfo.lua\n\n' ) 66f_enum : write ( '#pragma once\n\n' ) 67f_enum : write ( 'typedef enum {\n' ) 68for _ , name in ipairs ( wanted_strings ) do 69f_enum : write ( ' ' .. enumify ( name ) .. ',\n' ) 70end 71f_enum : write ( '#define kTermExtOffset ' .. enumify ( wanted_strings_ext [ 1 ][ 1 ]) .. '\n' ) 72for _ , item in ipairs ( wanted_strings_ext ) do 73f_enum : write ( ' ' .. enumify ( item [ 1 ]) .. ',\n' ) 74end 75f_enum : write ( ' kTermCount, // sentinel\n' ) 76f_enum : write ( '} TerminfoDef;\n\n' ) 77 78f_enum : write ( [[ 79// TODO(bfredl): physical F-keys beyond F12 are uncommon. But terminfo 80// likes to represent chords with shift and/or ctrl and F keys as high 81// F-key numbers. The same chords can also be recognized by driver-csi.c 82// but will then be encoded as chords. We might actually prefer that but it is 83// potentially breaking change. 84]] ) 85local func_key_max =terminfo . fields . func_key_max 86f_enum : write ( '#define kTerminfoFuncKeyMax ' .. func_key_max .. '\n' ) 87f_enum : write ( 'typedef enum {\n' ) 88for _ , item in ipairs ( wanted_termkeys ) do 89f_enum : write ( ' kTermKey_' .. item [ 1 ] .. ',\n' ) 90end 91f_enum : write ( ' kTermKeyCount,\n' ) 92f_enum : write ( '} TerminfoKey;\n' ) 93f_enum : close () 94 95local f_defs =assert ( io . open ( target_gen , 'wb' )) 96 97f_defs : write ( '// uncrustify:off\n\n' ) 98 99local version =io . popen ( 'infocmp -V' ): read '*a' 100f_defs : write ( '// Generated by src/gen/gen_terminfo.lua and ' .. version .. '\n' ) 101 102f_defs : write ( '#pragma once\n\n' ) 103f_defs : write ( '#include "nvim/tui/terminfo_defs.h"\n' ) 104 105for _ , entry in ipairs ( entries ) do 106local term , target =unpack ( entry ) 107local fil =io . popen ( 'infocmp -L -x -1 -A ' .. db .. ' ' .. term ): read '*a' 108local lines =vim . split ( fil , '\n' ) 109local prepat ='^%s*([%w_]+)' 110local boolpat =prepat .. ',' 111local numpat =prepat .. '#([^,]+),' 112local strpat =prepat .. '=([^,]+),' 113local bools ={} --- @type table<string, true> 114local nums ={} --- @type table<string, string> 115local strs ={} --- @type table<string, string> 116for i , line in ipairs ( lines ) do 117local boolmatch =string . match ( line , boolpat ) 118local nummatch , numval =string . match ( line , numpat ) 119local strmatch , strval =string . match ( line , strpat ) 120if boolmatch then 121dbg ( 'boolean: ' .. boolmatch ) 122bools [ boolmatch ] =true 123elseif nummatch then 124dbg ( 'number: ' .. nummatch .. ' is ' .. numval ) 125nums [ nummatch ] =numval 126elseif strmatch then 127dbg ( 'string: ' .. strmatch .. ' is ' .. strval ) 128strs [ strmatch ] =strval 129else 130dbg ( 'UNKNOWN:' , i , line ) 131end 132end 133 134f_defs : write ( '\nstatic const TerminfoEntry ' .. target .. ' = {\n' ) 135for _ , b in ipairs ( wanted_bools ) do 136f_defs : write ( ' .' .. b .. ' = ' .. tostring ( bools [ b ] or false ) .. ',\n' ) 137end 138 139for _ , name in ipairs ( wanted_ints ) do 140f_defs : write ( ' .' .. name .. ' = ' .. ( nums [ name ] or '-1' ) .. ',\n' ) 141end 142f_defs : write ( ' .defs = {\n' ) 143for _ , name in ipairs ( wanted_strings ) do 144f_defs : write ( ' [' .. enumify ( name ) .. '] = ' .. quote ( strs [ name ]) .. ',\n' ) 145end 146for _ , item in ipairs ( wanted_strings_ext ) do 147f_defs : write ( ' [' .. enumify ( item [ 1 ]) .. '] = ' .. quote ( strs [ item [ 2 ]]) .. ',\n' ) 148end 149f_defs : write ( ' },\n' ) 150f_defs : write ( ' .keys = {\n' ) 151for _ , item in ipairs ( wanted_termkeys ) do 152local name =item [ 1 ] 153f_defs : write ( 154' [kTermKey_' 155.. name 156.. '] = {' 157.. quote ( strs [ 'key_' .. name ]) 158.. ', ' 159.. quote ( strs [ 'key_s' .. name ]) 160.. '},\n' 161) 162end 163f_defs : write ( ' },\n' ) 164f_defs : write ( ' .f_keys = {\n' ) 165if strs [ 'key_f1' ] == nil then 166f_defs : write ( ' NULL,\n' ) -- compiler get sad if list is empty 167else 168f_defs : write ( ' // note: offset by one, f_keys[0] is F1 and so on\n' ) 169end 170for i =1 , func_key_max do 171if strs [ 'key_f' .. i ] ~= nil then 172f_defs : write ( ' [' .. i - 1 .. '] = ' .. quote ( strs [ 'key_f' .. i ]) .. ',\n' ) 173end 174end 175f_defs : write ( ' },\n' ) 176 177f_defs : write ( '};\n' ) 178end 179 180f_defs : write ( '\n#define XLIST_TERMINFO_BOOLS \\\n' ) 181for _ , item in ipairs ( wanted_bools ) do 182f_defs : write ( ' X(' .. item .. ') \\\n' ) 183end 184f_defs : write ( '// end of list\n\n' ) 185f_defs : write ( '#define XLIST_TERMINFO_INTS \\\n' ) 186for _ , item in ipairs ( wanted_ints ) do 187f_defs : write ( ' X(' .. item .. ') \\\n' ) 188end 189f_defs : write ( '// end of list\n\n' ) 190f_defs : write ( '#define XLIST_TERMINFO_BUILTIN \\\n' ) 191for _ , name in ipairs ( wanted_strings ) do 192f_defs : write ( ' X(' .. name .. ') \\\n' ) 193end 194f_defs : write ( '// end of list\n\n' ) 195f_defs : write ( '#define XLIST_TERMINFO_EXT \\\n' ) 196for _ , item in ipairs ( wanted_strings_ext ) do 197f_defs : write ( ' X(' .. item [ 1 ] .. ', ' .. item [ 2 ] .. ') \\\n' ) 198end 199f_defs : write ( '// end of list\n\n' ) 200f_defs : write ( '#define XYLIST_TERMINFO_KEYS \\\n' ) 201for _ , item in ipairs ( wanted_termkeys ) do 202f_defs : write ( ' ' .. ( item [ 2 ] and 'Y' or 'X' ) .. '(' .. item [ 1 ] .. ') \\\n' ) 203end 204f_defs : write ( '// end of list\n\n' ) 205f_defs : write ( '#define XLIST_TERMINFO_FKEYS \\\n' ) 206for i =1 , func_key_max do 207f_defs : write ( ' X(f' .. i .. ', ' .. i - 1 .. ') \\\n' ) 208end 209f_defs : write ( '// end of list\n' ) 210f_defs : close ()