gpt4 book ai didi

javascript - 如何将 "external"文本文件打包到 elisp 模块中?

转载 作者:搜寻专家 更新时间:2023-11-01 04:38:33 25 4
gpt4 key购买 nike

我正在编写一个需要外部文本文件的 elisp 模块。

出于好奇,该模块集成了 the interactive JS REPL for Cscript.exe在 emacs 中使用 shell 模式。它允许我在 Windows 上的 emacs 中运行交互式 javascript shell。

这是由 js-comint.el 激发的, 但它是一个独立的实现,依赖于 Windows 和 cscript.exe。

它目前正在运行,但有两个不同的文件:.el 文件和 .js 文件。我宁愿只有一个文件。

我的问题是:如何将外部 .js 文件打包到 .el 文件中,以便我可以进行单文件安装?

我想我可以用(可能缩小的)js 文件定义一个字符串变量并将其插入到 .el 模块中。我想会有一些字符串转义问题,但这会起作用。那是最好的方法吗?还有其他建议吗?

最佳答案

好吧,我没有找到更好的方法来做到这一点。但我确实决定将 Javascript 代码作为字符串嵌入到 elisp 模块中……效果很好。

为了实现它,我编写了一个简短的 elisp 函数,它创建了一个新的 elisp 模块。它根据旧名称选择一个新名称 - xxxx-bundle.el 而不是 xxxx.el 。然后它将原始 .el 文件中的内容写入具有新名称的文件中。然后它将一个简单的 setq 语句写入同一个文件;整个 Javascript 程序就是该语句中的文字字符串值。使 setq 变得简单的是 elisp 中的 pp-to-string 函数,它将所有 Javascript 代码转义为适合在 emacs 中使用的文字字符串。

生成包的 fn 看起来像这样:

(defun jsshell-produce-bundle (&optional jsshell-el bundle-el jsshell-js)
"Produce a new .el file, which contains all the jsshell.el
function and also embeds the jsshell.js source as a string. The
resulting .el file will then be suitable for a one-file
distribution of JSShell.

JSShell depends on two pieces: jsshell.el and jsshell.js. Rather
than distributing and installing two distinct files, the bundle
embeds the .js file into the .el file, for a one-file
distribution option. This function produces that one file.

Most people will never need to use this function. It's useful only
after modifying either the original jsshell.el or the jsshell.js file,
when you want to produce a new distributable bundle. In other words, it's
useful for the developer of jsshell.el.

"
(let ((jsshell-el (or jsshell-el
(concat (file-name-directory jsshell--load-path) "jsshell.el")

"jsshell.el")))
(let ((bundle-el (or bundle-el
(concat (file-name-directory jsshell-el) "jsshell-bundle.el")))
(jsshell-js (or jsshell-js
(and jsshell-js-tmpf
(file-readable-p jsshell-js-tmpf)
jsshell-js-tmpf)
jsshell-location-of-jsshell-js ;; orig dev wkstation
(concat (file-name-directory jsshell-el) "jsshell.js")))
jssrc)
(with-temp-file bundle-el
(insert (concat
";;; "
(file-name-nondirectory bundle-el)
" -- JSShell generated bundle\n"))
(insert (concat ";;\n;; generated " (current-time-string) "\n;;\n\n"))
(insert-file-contents jsshell-el)
(goto-char (point-max))
(insert "\n;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;\n")
(insert ";; this is the embedded Javascript code for the JS REPL\n\n")
(setq jssrc (jsshell--minimized-js-contents jsshell-js))
(goto-char (point-max))
(insert (concat "(setq jsshell-js-src "
(pp-to-string jssrc)
")\n"
"\n(provide '"
(file-name-sans-extension (file-name-nondirectory bundle-el))
")\n"
"\n;;; "
(file-name-nondirectory bundle-el)
" ends\n"))))))

用于最小化内容的助手 fn 只是折叠空格并消除连续的换行符,诸如此类。

生成的 .el 文件随后可以引用变量 jsshell-js-src,其中包含最小化的 Javascript 源代码。它看起来像这样:

enter image description here

我将其发布在这里是因为我认为该方法也可能在其他模块中有用 - 任何需要捆绑单独数据文件的模块。

关于javascript - 如何将 "external"文本文件打包到 elisp 模块中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9882475/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com