Subject: 25.0.91; emacs-module.*

From: Atsuo Ohki <[email-masked]>
Date: Fri, 26 Feb 2016 16:28:09 +0900

This is not a bug report, but feature enhancemnt for emacs-module.*

 API of emacs-module has two Lisp string related functions,
 `copy_string_contents' and `make_string'.
 Those functions assume UTF-8 encoded strings,
 but it becomes flexible if a user can specify a specific encoding.

 I made a following patch for accepting encoding.

--- src/emacs-module.c-ORIG	2016-02-05 03:15:31.000000000 +0900
+++ src/emacs-module.c	2016-02-26 10:20:31.831515000 +0900
@@ -502,17 +502,22 @@
 
 static bool
 module_copy_string_contents (emacs_env *env, emacs_value value, char *buffer,
-			     ptrdiff_t *length)
+			     ptrdiff_t *length, emacs_value coding)
 {
   MODULE_FUNCTION_BEGIN (false);
   Lisp_Object lisp_str = value_to_lisp (value);
+  Lisp_Object lisp_coding;
   if (! STRINGP (lisp_str))
     {
       module_wrong_type (env, Qstringp, lisp_str);
       return false;
     }
 
-  Lisp_Object lisp_str_utf8 = ENCODE_UTF_8 (lisp_str);
+  if (coding == module_nil) lisp_coding = Qutf_8;
+  else if ((lisp_coding = value_to_lisp(coding)) == Qnil) lisp_coding = Qutf_8;
+
+  Lisp_Object lisp_str_utf8 =
+      code_convert_string_norecord (lisp_str, lisp_coding, false);
   ptrdiff_t raw_size = SBYTES (lisp_str_utf8);
   if (raw_size == PTRDIFF_MAX)
     {
@@ -545,16 +550,23 @@
 }
 
 static emacs_value
-module_make_string (emacs_env *env, const char *str, ptrdiff_t length)
+module_make_string (emacs_env *env, const char *str,	
+		    ptrdiff_t length, emacs_value coding)
 {
   MODULE_FUNCTION_BEGIN (module_nil);
+  Lisp_Object lisp_coding;
   if (length > STRING_BYTES_BOUND)
     {
       module_non_local_exit_signal_1 (env, Qoverflow_error, Qnil);
       return module_nil;
     }
   Lisp_Object lstr = make_unibyte_string (str, length);
-  return lisp_to_value (code_convert_string_norecord (lstr, Qutf_8, false));
+
+  if (coding == module_nil) lisp_coding = Qutf_8;
+  else if ((lisp_coding = value_to_lisp(coding)) == Qnil) lisp_coding = Qutf_8;
+
+  return lisp_to_value (
+	code_convert_string_norecord (lstr, lisp_coding, false));
 }
 
 static emacs_value
--- src/emacs-module.h-ORIG	2016-02-05 03:15:31.000000000 +0900
+++ src/emacs-module.h	2016-02-25 14:25:04.785587000 +0900
@@ -151,7 +151,7 @@
   emacs_value (*make_float) (emacs_env *env, double value);
 
   /* Copy the content of the Lisp string VALUE to BUFFER as an utf8
-     null-terminated string.
+     (or specified coding) null-terminated string.
 
      SIZE must point to the total size of the buffer.  If BUFFER is
      NULL or if SIZE is not big enough, write the required buffer size
@@ -165,11 +165,13 @@
   bool (*copy_string_contents) (emacs_env *env,
                                 emacs_value value,
                                 char *buffer,
-                                ptrdiff_t *size_inout);
+                                ptrdiff_t *size_inout,
+                                emacs_value coding);
 
-  /* Create a Lisp string from a utf8 encoded string.  */
+  /* Create a Lisp string from a utf8 (or specified) encoded string.  */
   emacs_value (*make_string) (emacs_env *env,
-			      const char *contents, ptrdiff_t length);
+			      const char *contents,
+			      ptrdiff_t length, emacs_value coding);
 
   /* Embedded pointer type.  */
   emacs_value (*make_user_ptr) (emacs_env *env,

#===========================================================================
In GNU Emacs 25.0.91.1 (x86_64-ibm-freebsd10.2, X toolkit, Xaw3d scroll bars)
 of 2016-02-26 built on smr00
Configured using:
 'configure x86_64-ibm-freebsd10.2
 --srcdir=/usr/src/local/GNU/emacs/emacs-25.0.91 --with-x
 --x-includes=/usr/local/include --x-libraries=/usr/local/lib
 --with-x-toolkit=lucid --without-xim --with-modules --without-pop
 --without-xpm --without-jpeg --without-tiff --without-gif --without-png
 --without-rsvg --without-imagemagick --without-gpm --without-dbus
 --without-gconf --without-gsettings --without-selinux --without-gnutls
 LDFLAGS=-L/usr/local/lib'

Configured features:
XAW3D SOUND NOTIFY ACL LIBXML2 FREETYPE XFT ZLIB TOOLKIT_SCROLL_BARS
LUCID X11 MODULES

Important settings:
  locale-coding-system: nil

Major mode: Fundamental

Minor modes in effect:
  tooltip-mode: t
  global-eldoc-mode: t
  electric-indent-mode: t
  mouse-wheel-mode: t
  tool-bar-mode: t
  menu-bar-mode: t
  file-name-shadow-mode: t
  global-font-lock-mode: t
  font-lock-mode: t
  auto-composition-mode: t
  auto-encryption-mode: t
  auto-compression-mode: t
  line-number-mode: t
  transient-mark-mode: t

Recent messages:
Loading /usr/local/share/emacs/site-lisp/site-start-25.x.el (source)...
Loading /usr/local/share/emacs/25.0.91/site-lisp/canna-leim.el (source)...
byte-code: Canna is not built into this Emacs
No docstring slot for setup-japanese-environment-internal

Load-path shadows:
None found.

Features:
(shadow sort mail-extr emacsbug message idna dired format-spec rfc822
mml mml-sec epg epg-config gnus-util mm-decode mm-bodies mm-encode
mail-parse rfc2231 mailabbrev gmm-utils mailheader sendmail rfc2047
rfc2045 ietf-drums mm-util help-fns help-mode easymenu cl-loaddefs pcase
cl-lib mail-prsvr mail-utils time-date japan-util disp-table mule-util
tooltip eldoc electric uniquify ediff-hook vc-hooks lisp-float-type
mwheel x-win term/common-win x-dnd tool-bar dnd fontset image regexp-opt
fringe tabulated-list newcomment elisp-mode lisp-mode prog-mode register
page menu-bar rfn-eshadow timer select scroll-bar mouse jit-lock
font-lock syntax facemenu font-core frame cl-generic cham georgian
utf-8-lang misc-lang vietnamese tibetan thai tai-viet lao korean
japanese eucjp-ms cp51932 hebrew greek romanian slovak czech european
ethiopic indian cyrillic chinese charscript case-table epa-hook
jka-cmpr-hook help simple abbrev minibuffer cl-preloaded nadvice
loaddefs button faces cus-face macroexp files text-properties overlay
sha1 md5 base64 format env code-pages mule custom widget
hashtable-print-readable backquote kqueue dynamic-setting
font-render-setting x-toolkit x multi-tty make-network-process emacs)

Memory information:
((conses 16 87847 5715)
 (symbols 48 19548 0)
 (miscs 40 44 85)
 (strings 32 14122 4881)
 (string-bytes 1 406710)
 (vectors 16 9853)
 (vector-slots 8 453785 38367)
 (floats 8 162 258)
 (intervals 56 200 18)
 (buffers 976 11))