diff options
author | Lewis Russell <lewis6991@gmail.com> | 2022-08-24 14:41:31 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-08-24 14:41:31 +0100 |
commit | b1eaa2b9a3bae46f6dcbab8dc3a84e0044b11317 (patch) | |
tree | 486a0a3f0118952157e38edad14f71ca16922a77 /src/nvim/lua/stdlib.c | |
parent | 9be4bfc5f4951976131e5e99ecf05882f6767fb4 (diff) | |
download | rneovim-b1eaa2b9a3bae46f6dcbab8dc3a84e0044b11317.tar.gz rneovim-b1eaa2b9a3bae46f6dcbab8dc3a84e0044b11317.tar.bz2 rneovim-b1eaa2b9a3bae46f6dcbab8dc3a84e0044b11317.zip |
feat(lua): add vim.iconv (#18286)
Co-authored-by: Justin M. Keyes <justinkz@gmail.com>
Diffstat (limited to 'src/nvim/lua/stdlib.c')
-rw-r--r-- | src/nvim/lua/stdlib.c | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/src/nvim/lua/stdlib.c b/src/nvim/lua/stdlib.c index 5a82ae30b5..64e9abe0c9 100644 --- a/src/nvim/lua/stdlib.c +++ b/src/nvim/lua/stdlib.c @@ -474,6 +474,52 @@ static int nlua_stricmp(lua_State *const lstate) FUNC_ATTR_NONNULL_ALL return 1; } +#if defined(HAVE_ICONV) + +/// Convert string from one encoding to another +static int nlua_iconv(lua_State *lstate) +{ + int narg = lua_gettop(lstate); + + if (narg < 3) { + return luaL_error(lstate, "Expected at least 3 arguments"); + } + + for (int i = 1; i <= 3; i++) { + if (lua_type(lstate, i) != LUA_TSTRING) { + return luaL_argerror(lstate, i, "expected string"); + } + } + + size_t str_len = 0; + const char *str = lua_tolstring(lstate, 1, &str_len); + + char_u *from = enc_canonize(enc_skip((char_u *)lua_tolstring(lstate, 2, NULL))); + char_u *to = enc_canonize(enc_skip((char_u *)lua_tolstring(lstate, 3, NULL))); + + vimconv_T vimconv; + vimconv.vc_type = CONV_NONE; + convert_setup_ext(&vimconv, from, false, to, false); + + char_u *ret = string_convert(&vimconv, (char_u *)str, &str_len); + + convert_setup(&vimconv, NULL, NULL); + + xfree(from); + xfree(to); + + if (ret == NULL) { + lua_pushnil(lstate); + } else { + lua_pushlstring(lstate, (char *)ret, str_len); + xfree(ret); + } + + return 1; +} + +#endif + void nlua_state_add_stdlib(lua_State *const lstate, bool is_thread) { if (!is_thread) { @@ -519,6 +565,13 @@ void nlua_state_add_stdlib(lua_State *const lstate, bool is_thread) // vim.spell luaopen_spell(lstate); lua_setfield(lstate, -2, "spell"); + +#if defined(HAVE_ICONV) + // vim.iconv + // depends on p_ambw, p_emoji + lua_pushcfunction(lstate, &nlua_iconv); + lua_setfield(lstate, -2, "iconv"); +#endif } // vim.mpack |