robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
38986188ba
master
1#include <assert.h> 2#include <lauxlib.h> 3#include <limits.h> 4#include <lua.h> 5#include <stdbool.h> 6#include <stddef.h> 7 8#include "nvim/ascii_defs.h" 9#include "nvim/buffer_defs.h" 10#include "nvim/errors.h" 11#include "nvim/gettext_defs.h" 12#include "nvim/globals.h" 13#include "nvim/highlight_defs.h" 14#include "nvim/lua/spell.h" 15#include "nvim/message.h" 16#include "nvim/spell.h" 17 18#include "lua/spell.c.generated.h" // IWYU pragma: keep 19 20int nlua_spell_check (lua_State * lstate ) 21FUNC_ATTR_NONNULL_ALL 22{ 23if (lua_gettop (lstate )< 1 ) { 24return luaL_error (lstate ,"Expected 1 argument" ); 25 } 26 27if (lua_type (lstate ,1 )!= LUA_TSTRING ) { 28luaL_argerror (lstate ,1 ,"expected string" ); 29 } 30 31const char * str = lua_tolstring (lstate ,1 ,NULL ); 32 33// spell.c requires that 'spell' is enabled, so we need to temporarily enable 34// it before we can call spell functions. 35const int wo_spell_save = curwin -> w_p_spell ; 36 37if (!curwin -> w_p_spell ) { 38parse_spelllang (curwin ); 39curwin -> w_p_spell = true; 40 } 41 42// Check 'spelllang' 43if (* curwin -> w_s -> b_p_spl == NUL ) { 44emsg (_ (e_no_spell )); 45curwin -> w_p_spell = wo_spell_save ; 46return 0 ; 47 } 48 49hlf_T attr = HLF_COUNT ; 50size_t pos = 0 ; 51int capcol = -1 ; 52int no_res = 0 ; 53const char * result ; 54 55lua_createtable (lstate ,0 ,0 ); 56 57while (* str != NUL ) { 58attr = HLF_COUNT ; 59size_t len = spell_check (curwin , (char * )str ,& attr ,& capcol , false); 60assert (len <=INT_MAX ); 61 62if (attr != HLF_COUNT ) { 63lua_createtable (lstate ,3 ,0 ); 64 65lua_pushlstring (lstate ,str ,len ); 66lua_rawseti (lstate ,-2 ,1 ); 67 68result = attr == HLF_SPB 69 ?"bad" : (attr == HLF_SPR 70 ?"rare" : (attr == HLF_SPL 71 ?"local" : (attr == HLF_SPC 72 ?"caps" :NULL ))); 73 74assert (result != NULL ); 75 76lua_pushstring (lstate ,result ); 77lua_rawseti (lstate ,-2 ,2 ); 78 79// +1 for 1-indexing 80lua_pushinteger (lstate , (lua_Integer )pos + 1 ); 81lua_rawseti (lstate ,-2 ,3 ); 82 83lua_rawseti (lstate ,-2 ,++ no_res ); 84 } 85 86str += len ; 87pos += len ; 88capcol -= (int )len ; 89 } 90 91// Restore 'spell' 92curwin -> w_p_spell = wo_spell_save ; 93return 1 ; 94} 95 96static const luaL_Reg spell_functions []= { 97 {"check" ,nlua_spell_check }, 98 {NULL ,NULL } 99}; 100 101int luaopen_spell (lua_State * L ) 102FUNC_ATTR_NONNULL_ALL 103{ 104lua_newtable (L ); 105luaL_register (L ,NULL ,spell_functions ); 106return 1 ; 107}