如何在emacs中使用 :data 显示图片

求教大家一个问题:manual中说可以用 :data 来标记图片的数据,其类型是包含图片数据的string。但是emacs的string强制要求utf-8,是不是说明png jpeg这种二进制格式无法用string来表示,只能用bool vector?

起因是我想搞一个emacs的org mode latex mode等预览用图片数据管理器,于是用rust把tokio mathjax resvg等东西和emacs的FFI糊在了一起,但没想到卡在了最后一步,没办法把外部的二进制数据传到emacs里面,用string过不了utf-8 validation,user_ptr好像也没用,bool vector似乎也没有FFI。请问大家有什么好办法吗?

牺牲一些性能, base64 转换,可不可以?

Emacs FFI 没有这种要求

Emacs 没有 bool vector 这种数据类型

你应当用的是 make_unibyte_string

This function is like make_string, but has no restrictions on the values of the bytes in the C string, and can be used to pass binary data to Emacs in the form of a unibyte string.

https://www.gnu.org/software/emacs/manual/html_node/elisp/Module-Values.html#index-make_005funibyte_005fstring

2 个赞

感谢!应该是我直接print这个假字符串会出错,被我误解了,我再去试试

使用make_string会有如图所示的问题(svgear-test1是测试函数,用 FFI 中的make_string 返回一张png(rust binding)), 会显示不是utf-8-string, insert-file-contents 则可以

bool vector在手册上有写:bool vector,源码里也能找到,你的意思是说它就是vector吗?

谢谢,应该是可以的,之前忽略了这种手段

我的意思是 FFI 不提供这种数据类型。当然本质上 bool-vector 就是额外带一个长度的 unibyte string

insert-file-contents-literally

是不是就可以不用转换了,直接给 create-image?

你怎么直接在 temp-buffer 中 insert 二进制文件,然后调用 string-to-unibyte??为什么不将 coding-system-for-read 设置为 no-conversion,同时在 temp-buffer 中关闭 multibyte 编码?

是让你直接通过 FFI 传 unibyte string,没让你多此一举在 emacs 里用 string-to-unibyte,调用这个函数根本不重要,重要的是保证构造出的是每个字符都在 char 范围内的 byte string

明白了,感谢大佬!