robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
2bbc90f4d5
master
1-- Updates version.c list of applied Vim patches. 2-- 3-- Usage: 4-- VIM_SOURCE_DIR=~/neovim/.vim-src/ nvim -l ./scripts/vimpatch.lua 5 6local nvim =vim . api 7 8local function systemlist ( ... ) 9local rv =nvim . nvim_call_function ( 'systemlist' , ... ) 10local err =nvim . nvim_get_vvar ( 'shell_error' ) 11local args_str =nvim . nvim_call_function ( 'string' , ... ) 12if 0 ~= err then 13error ( 'command failed: ' .. args_str ) 14end 15return rv 16end 17 18local function vimpatch_sh_list_tokens () 19return systemlist ( { { 'bash' , '-c' , 'scripts/vim-patch.sh -M' } } ) 20end 21 22-- Generate the data,lines to update src/nvim/version.c. 23-- - `vim_versions[]` 24-- - `Versions[]` 25-- - `num_patches[]` 26-- - `included_patchsets[]` 27local function gen_version_c_lines () 28-- List of version sets where each set contains: 29-- 1. major_minor_version (int) 30-- 2. major_minor_version (string) 31-- 3. set of merged patch numbers 32local merged_version_list ={} 33for _ , token in ipairs ( vimpatch_sh_list_tokens ()) do 34local major_version , minor_version , patch_num =string . match ( token , '^(%d+).(%d+).(%d+)$' ) 35local n =tonumber ( patch_num ) 36if n then 37local major_minor_version =major_version * 100 + minor_version 38local len =# merged_version_list 39if len == 0 or merged_version_list [ len ][ 1 ] ~= major_minor_version then 40local vstr ='"' .. major_version .. '.' .. minor_version .. '"' 41table . insert ( merged_version_list , { major_minor_version , vstr , { n } } ) 42else 43table . insert ( merged_version_list [ len ][ 3 ], n ) 44end 45end 46end 47 48local major_vim_versions ={} 49local major_vim_versions_str ={} 50local num_patches ={} 51local patch_lines ={} 52for _ , version_set in ipairs ( merged_version_list ) do 53local major_minor_version , major_minor_version_str , patch_set =unpack ( version_set ) 54table . insert ( major_vim_versions , major_minor_version ) 55table . insert ( major_vim_versions_str , major_minor_version_str ) 56table . insert ( num_patches , # patch_set ) 57table . insert ( patch_lines , ' (const int[]) { // ' .. major_minor_version ) 58 59local patchset_set ={} 60for i =# patch_set , 1 , - 1 do 61local patch =patch_set [ i ] 62local next_patch =patch_set [ i - 1 ] 63local patch_diff =patch - ( next_patch or 0 ) 64table . insert ( patchset_set , patch ) 65 66-- guard against last patch or `make formatc` 67if # patchset_set > 15 or i == 1 or patch_diff > 1 then 68table . insert ( patch_lines , ' ' .. table . concat ( patchset_set , ', ' ) .. ',' ) 69patchset_set ={} 70end 71if i == 1 and patch > 0 then 72local line =' // 0' 73if patch > 1 then 74line =line .. '-' .. ( patch - 1 ) 75end 76table . insert ( patch_lines , line ) 77elseif patch_diff > 1 then 78local line =' // ' .. ( next_patch + 1 ) 79if patch_diff > 2 then 80line =line .. '-' .. ( patch - 1 ) 81end 82table . insert ( patch_lines , line ) 83end 84end 85 86table . insert ( patch_lines , ' },' ) 87end 88 89return major_vim_versions , major_vim_versions_str , num_patches , patch_lines 90end 91 92local function patch_version_c () 93local major_vim_versions , major_vim_versions_str , num_patches , patch_lines =gen_version_c_lines () 94 95nvim . nvim_command ( 'silent noswapfile noautocmd edit src/nvim/version.c' ) 96nvim . nvim_command ( [[/^char \*Versions]] ) 97-- Replace the line. 98nvim . nvim_call_function ( 'setline' , { 99nvim . nvim_eval ( 'line(".")' ), 100'char *Versions[] = { ' .. table . concat ( major_vim_versions_str , ', ' ) .. ' };' , 101} ) 102nvim . nvim_command ( [[/^static const int vim_versions]] ) 103-- Replace the line. 104nvim . nvim_call_function ( 'setline' , { 105nvim . nvim_eval ( 'line(".")' ), 106'static const int vim_versions[] = { ' .. table . concat ( major_vim_versions , ', ' ) .. ' };' , 107} ) 108nvim . nvim_command ( [[/^static const int num_patches]] ) 109-- Replace the line. 110nvim . nvim_call_function ( 'setline' , { 111nvim . nvim_eval ( 'line(".")' ), 112'static const int num_patches[] = { ' .. table . concat ( num_patches , ', ' ) .. ' };' , 113} ) 114nvim . nvim_command ( [[/^static const int \*included_patchsets]] ) 115-- Delete the existing lines. 116nvim . nvim_command ( 'silent normal! j0d/};\rk' ) 117-- Insert the lines. 118nvim . nvim_call_function ( 'append' , { 119nvim . nvim_eval ( 'line(".")' ), 120patch_lines , 121} ) 122nvim . nvim_command ( 'silent write' ) 123end 124 125patch_version_c ()