robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1--- @brief 2--- Asks the user to download missing spellfiles. The spellfile is written to 3--- `stdpath('data') .. 'site/spell'` or the first writable directory in the 4--- 'runtimepath'. 5--- 6--- The plugin can be disabled by setting `g:loaded_spellfile_plugin = 1`. 7 8local M ={} 9 10--- @class nvim.spellfile.Info 11--- @inlinedoc 12--- @field files string[] 13--- @field key string 14--- @field lang string 15--- @field encoding string 16--- @field dir string 17 18--- A table with the following fields: 19--- @class nvim.spellfile.Config 20--- 21--- The base URL from where the spellfiles are downloaded. Uses `g:spellfile_URL` 22--- if it's set, otherwise https://ftp.nluug.nl/pub/vim/runtime/spell. 23--- @field url string 24--- 25--- Number of milliseconds after which the [vim.net.request()] times out. 26--- (default: 15000) 27--- @field timeout_ms integer 28--- 29--- Whether to ask user to confirm download. 30--- (default: `true`) 31--- @field confirm boolean 32 33--- @type nvim.spellfile.Config 34local config ={ 35url =vim . g . spellfile_URL or 'https://ftp.nluug.nl/pub/vim/runtime/spell' , 36timeout_ms =15000 , 37confirm =true , 38} 39 40--- Configure spellfile download options. Omit {opts} to retrieve the current 41--- configuration. Otherwise, update only the supplied fields. 42--- 43--- For example: 44--- ```lua 45--- require('nvim.spellfile').config({ url = '...' }) 46--- ``` 47--- @param opts Partial<nvim.spellfile.Config>? 48--- @return nvim.spellfile.Config? : Current config if {opts} is omitted. 49function M . config ( opts ) 50vim . validate ( 'opts' , opts , 'table' , true ) 51if not opts then 52return vim . deepcopy ( config , true ) 53end 54for k , v in pairs ( opts ) do 55config [ k ] =v 56end 57end 58 59--- TODO(justinmk): add on_done/on_err callbacks to download(), instead of exposing this? 60---@type table<string, boolean> 61M . _done ={} 62 63---@return string[] 64local function rtp_list () 65return vim . opt . rtp : get () --[[@as string[] ]] 66end 67 68---@param msg string 69---@param level vim.log.levels? 70local function notify ( msg , level ) 71vim . notify ( msg , level or vim . log . levels . INFO ) 72end 73 74---@param lang string 75---@return string 76local function normalize_lang ( lang ) 77local l =( lang or '' ): lower (): gsub ( '-' , '_' ) 78return ( l : match ( '^[^,%s]+' ) or l ) 79end 80 81---@param path string 82---@return boolean 83local function file_ok ( path ) 84local s =vim . uv . fs_stat ( path ) 85return s ~= nil and s . type == 'file' and ( s . size or 0 ) > 0 86end 87 88---@param dir string 89---@return boolean 90local function can_use_dir ( dir ) 91return not not ( vim . fn . isdirectory ( dir ) == 1 and vim . uv . fs_access ( dir , 'W' )) 92end 93 94---@return string[] 95local function writable_spell_dirs_from_rtp () 96local dirs ={} 97for _ , dir in ipairs ( rtp_list ()) do 98local spell =vim . fs . joinpath ( vim . fs . abspath ( dir ), 'spell' ) 99if can_use_dir ( spell ) then 100table . insert ( dirs , spell ) 101end 102end 103return dirs 104end 105 106---@return string 107local function ensure_target_dir () 108local dir =vim . fs . abspath ( vim . fs . joinpath ( vim . fn . stdpath ( 'data' ), 'site/spell' )) 109if vim . fn . isdirectory ( dir ) == 0 and pcall ( vim . fn . mkdir , dir , 'p' ) then 110notify ( 'Created ' .. dir ) 111end 112if can_use_dir ( dir ) then 113return dir 114end 115 116-- Else, look for a spell/ dir in 'runtimepath'. 117local dirs =writable_spell_dirs_from_rtp () 118if # dirs > 0 then 119return dirs [ 1 ] 120end 121 122-- vim.fs.relpath does not prepend '~/' while fnamemodify does 123dir =vim . fn . fnamemodify ( dir , ':~' ) 124error (( 'cannot find a writable spell/ dir in runtimepath, and %s is not usable' ): format ( dir )) 125end 126 127local function reload_spell_silent () 128vim . cmd ( 'silent! setlocal spell!' ) 129if vim . bo . spelllang and vim . bo . spelllang ~= '' then 130vim . cmd ( 'silent! setlocal spelllang=' .. vim . bo . spelllang ) 131end 132vim . cmd ( 'echo ""' ) 133end 134 135--- Fetch file via blocking HTTP GET and write to `outpath`. 136--- 137--- Treats status==0 as success if file exists. 138--- 139--- @param url string 140--- @param outpath string 141--- @return boolean ok, integer|nil status, string|nil err 142local function fetch_file_sync ( url , outpath ) 143local done , err , res =false , nil , nil 144vim . net . request ( url , { outpath =outpath } , function ( e , r ) 145err , res , done =e , r , true 146end ) 147vim . wait ( config . timeout_ms , function () 148return done 149end , 50 , false ) 150 151local status =res and res . status or 0 152local ok =( not err ) and (( status >= 200 and status < 300 ) or ( status == 0 and file_ok ( outpath ))) 153return not not ok , ( status ~= 0 and status or nil ), err 154end 155 156---@param lang string 157---@return nvim.spellfile.Info 158local function parse ( lang ) 159local code =normalize_lang ( lang ) 160local enc ='utf-8' 161local dir =ensure_target_dir () 162 163local missing ={} 164local candidates ={ 165string . format ( '%s.%s.spl' , code , enc ), 166string . format ( '%s.%s.sug' , code , enc ), 167} 168for _ , fn in ipairs ( candidates ) do 169if not file_ok ( vim . fs . joinpath ( dir , fn )) then 170table . insert ( missing , fn ) 171end 172end 173 174return { 175files =missing , 176key =code .. '.' .. enc , 177lang =code , 178encoding =enc , 179dir =dir , 180} 181end 182 183---@param info nvim.spellfile.Info 184local function download ( info ) 185local dir =info . dir or ensure_target_dir () 186if not dir then 187notify ( 'No (writable) spell directory found and could not create one.' , vim . log . levels . ERROR ) 188return 189end 190 191local lang =info . lang 192local enc =info . encoding 193 194local spl_utf8 =string . format ( '%s.%s.spl' , lang , enc ) 195local spl_ascii =string . format ( '%s.ascii.spl' , lang ) 196local sug_name =string . format ( '%s.%s.sug' , lang , enc ) 197 198local url_utf8 =config . url .. '/' .. spl_utf8 199local out_utf8 =vim . fs . joinpath ( dir , spl_utf8 ) 200notify ( 'Downloading ' .. spl_utf8 .. ' …' ) 201local ok , st , err =fetch_file_sync ( url_utf8 , out_utf8 ) 202if not ok then 203notify ( 204( 'Could not get %s (status %s): trying %s …' ): format ( 205spl_utf8 , 206tostring ( st or 'nil' ), 207spl_ascii 208) 209) 210local url_ascii =config . url .. '/' .. spl_ascii 211local out_ascii =vim . fs . joinpath ( dir , spl_ascii ) 212local ok2 , st2 , err2 =fetch_file_sync ( url_ascii , out_ascii ) 213if not ok2 then 214notify ( 215( 'No spell file available for %s (utf8:%s ascii:%s) — %s' ): format ( 216lang , 217tostring ( st or err or 'fail' ), 218tostring ( st2 or err2 or 'fail' ), 219url_utf8 220), 221vim . log . levels . WARN 222) 223vim . schedule ( function () 224vim . cmd ( 'echo ""' ) 225end ) 226M . _done [ info . key ] =true 227return 228end 229notify ( 'Saved ' .. spl_ascii .. ' to ' .. out_ascii ) 230else 231notify ( 'Saved ' .. spl_utf8 .. ' to ' .. out_utf8 ) 232end 233 234reload_spell_silent () 235 236if not file_ok ( vim . fs . joinpath ( dir , sug_name )) then 237local url_sug =config . url .. '/' .. sug_name 238local out_sug =vim . fs . joinpath ( dir , sug_name ) 239notify ( 'Downloading ' .. sug_name .. ' …' ) 240local ok3 , st3 , err3 =fetch_file_sync ( url_sug , out_sug ) 241if ok3 then 242notify ( 'Saved ' .. sug_name .. ' to ' .. out_sug ) 243else 244local is404 =( st3 == 404 ) or ( tostring ( err3 or '' ): match ( '%f[%d]404%f[%D]' ) ~= nil ) 245if is404 then 246notify ( 'Suggestion file not available: ' .. sug_name , vim . log . levels . DEBUG ) 247else 248notify ( 249( 'Failed to download %s (status %s): %s' ): format ( 250sug_name , 251tostring ( st3 or 'nil' ), 252tostring ( err3 or '' ) 253), 254vim . log . levels . INFO 255) 256end 257vim . schedule ( function () 258vim . cmd ( 'echo ""' ) 259end ) 260end 261end 262 263M . _done [ info . key ] =true 264end 265 266--- Download spellfiles for language {lang} if available. 267--- @param lang string Language code. 268--- @return nvim.spellfile.Info? 269function M . get ( lang ) 270local info =parse ( lang ) 271if # info . files == 0 then 272return 273end 274if M . _done [ info . key ] then 275notify ( 'Already attempted spell load for ' .. lang , vim . log . levels . DEBUG ) 276return 277end 278 279if config . confirm then 280local prompt =( 'No spell file found for %s (%s). Download? [y/N] ' ): format ( 281info . lang , 282info . encoding 283) 284vim . ui . input ( { prompt =prompt , scope ='editor' } , function ( input ) 285-- properly clear the message window 286vim . api . nvim_echo ( { { ' ' } } , false , { kind ='empty' } ) 287if not input or input : lower () ~= 'y' then 288return 289end 290download ( info ) 291end ) 292else 293download ( info ) 294end 295 296return info 297end 298 299return M