From 8026e94f6cf35c75a0a4f928c580b077f546a452 Mon Sep 17 00:00:00 2001 From: Vivien Kraus Date: Tue, 3 Aug 2021 23:53:41 +0200 Subject: [PATCH 3/4] Export the wcwidth function The wcwidth function computes the number of columns that a character spans. It is in gnulib, but not documented. It is part of POSIX, and its own module in Gnulib [1]. [1]: https://www.gnu.org/software/gnulib/manual/html_node/wcwidth.html --- doc/ref/api-data.texi | 7 +++++++ libguile/chars.c | 12 ++++++++++++ libguile/chars.h | 1 + 3 files changed, 20 insertions(+) diff --git a/doc/ref/api-data.texi b/doc/ref/api-data.texi index 2ad13f5a5..a6647c3bf 100644 --- a/doc/ref/api-data.texi +++ b/doc/ref/api-data.texi @@ -2275,6 +2275,13 @@ with their meanings. @end multitable @end deffn +@deffn {Scheme Procedure} wcwidth chr +@deffnx {C Function} scm_wcwidth (chr) +Return the number of columns that @var{chr} spans. This function depends +on the @code{LC_CTYPE} locale category. Return @code{-1} if @var{chr} is +not recognized. +@end deffn + @rnindex char->integer @deffn {Scheme Procedure} char->integer chr @deffnx {C Function} scm_char_to_integer (chr) diff --git a/libguile/chars.c b/libguile/chars.c index fe55f9e2e..7b31e0601 100644 --- a/libguile/chars.c +++ b/libguile/chars.c @@ -504,6 +504,18 @@ SCM_DEFINE (scm_char_general_category, "char-general-category", 1, 0, 0, } #undef FUNC_NAME +SCM_DEFINE (scm_wcwidth, "wcwidth", 1, 0, 0, + (SCM chr), + "Return the number of columns that @var{chr} spans.") +#define FUNC_NAME s_scm_wcwidth +{ + int width; + SCM_VALIDATE_CHAR (1, chr); + width = wcwidth (SCM_CHAR(chr)); + return scm_from_int (width); +} +#undef FUNC_NAME + diff --git a/libguile/chars.h b/libguile/chars.h index f6d4c6354..1bf7c2aa2 100644 --- a/libguile/chars.h +++ b/libguile/chars.h @@ -86,6 +86,7 @@ SCM_API SCM scm_char_upcase (SCM chr); SCM_API SCM scm_char_downcase (SCM chr); SCM_API SCM scm_char_titlecase (SCM chr); SCM_API SCM scm_char_general_category (SCM chr); +SCM_API SCM scm_wcwidth (SCM chr); SCM_INLINE SCM scm_c_make_char (scm_t_wchar c); SCM_API scm_t_wchar scm_c_upcase (scm_t_wchar c); -- 2.32.0