首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将“外部”文本文件打包到elisp模块中?

如何将“外部”文本文件打包到elisp模块中?
EN

Stack Overflow用户
提问于 2012-03-27 10:12:15
回答 1查看 177关注 0票数 5

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

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

这是由js-comint.el驱动的,但它是依赖于Windows和cscript.exe的单独实现。

它目前正在工作,但有两个不同的文件:.el文件和.js文件。我更喜欢只有一个文件。

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

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-03-28 13:39:59

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

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

生成包的fn如下所示:

代码语言:javascript
复制
(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源代码。它看起来是这样的:

我之所以在这里发布这篇文章,是因为我认为这种方法可能在其他模块中也很有用-任何需要捆绑单独数据文件的模块。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9882475

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档