aboutsummaryrefslogtreecommitdiff
path: root/src/term.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/term.c')
-rw-r--r--src/term.c11
1 files changed, 10 insertions, 1 deletions
diff --git a/src/term.c b/src/term.c
index 4deb5f1..650b838 100644
--- a/src/term.c
+++ b/src/term.c
@@ -36,6 +36,7 @@
36// Windows does not have a wcwidth function, so we use compatibilty code from 36// Windows does not have a wcwidth function, so we use compatibilty code from
37// http://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c by Markus Kuhn 37// http://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c by Markus Kuhn
38#include "wcwidth.h" 38#include "wcwidth.h"
39#include "wcwidtha.h" // ambiguous width checks for East Asian characters
39 40
40 41
41#ifdef _WIN32 42#ifdef _WIN32
@@ -950,14 +951,16 @@ int utf8_to_wchar(const char *utf8, size_t len, mk_wchar_t *codepoint) {
950Get the width of a utf8 character for terminal display. 951Get the width of a utf8 character for terminal display.
951@function utf8cwidth 952@function utf8cwidth
952@tparam string utf8_char the utf8 character to check, only the width of the first character will be returned 953@tparam string utf8_char the utf8 character to check, only the width of the first character will be returned
954@tparam bool ambiguous if `true` a second return value will be returned; boolean indicating if the character is ambiguous
953@treturn[1] int the display width in columns of the first character in the string (0 for an empty string) 955@treturn[1] int the display width in columns of the first character in the string (0 for an empty string)
954@treturn[2] nil 956@treturn[2] nil|bool if `ambiguous` is `true`, a boolean indicating if the character is ambiguous
955@treturn[2] string error message 957@treturn[2] string error message
956*/ 958*/
957int lst_utf8cwidth(lua_State *L) { 959int lst_utf8cwidth(lua_State *L) {
958 const char *utf8_char; 960 const char *utf8_char;
959 size_t utf8_len; 961 size_t utf8_len;
960 utf8_char = luaL_checklstring(L, 1, &utf8_len); 962 utf8_char = luaL_checklstring(L, 1, &utf8_len);
963 int ambiguous = lua_toboolean(L, 2);
961 int width = 0; 964 int width = 0;
962 965
963 mk_wchar_t wc; 966 mk_wchar_t wc;
@@ -984,6 +987,12 @@ int lst_utf8cwidth(lua_State *L) {
984 } 987 }
985 988
986 lua_pushinteger(L, width); 989 lua_pushinteger(L, width);
990
991 if (ambiguous) {
992 // also check if the width is ambiguous
993 lua_pushboolean(L, mk_wcwidth_a(wc));
994 return 2;
995 }
987 return 1; 996 return 1;
988} 997}
989 998