5

外部テキスト ファイルを必要とする elisp モジュールを作成しています。

興味深いことに、このモジュールは、Cscript.exe の対話型 JS REPL をemacs のシェル モードに統合します。これにより、Windows の emacs でインタラクティブな JavaScript シェルを実行できます。

これはjs-comint.elによって動機付けられましたが、Windows と cscript.exe に依存する個別の実装です。

現在動作していますが、.el ファイルと .js ファイルの 2 つの異なるファイルがあります。ファイルを 1 つだけにした方がよいでしょう。

私が持っている質問は次のとおりです。このことの前提条件である外部 .js ファイルを .el ファイルにパッケージ化して、1 つのファイルでインストールできるようにするにはどうすればよいですか?

(おそらく縮小された) js ファイルを使用して文字列変数を定義し、それを .el モジュールに挿入するだけでよいと思います。文字列のエスケープメントの問題がいくつかあると思いますが、これでうまくいきます。それが最善の方法ですか?他の提案はありますか?

4

1 に答える 1

1

わかりました、これを行うより良い方法が見つかりませんでした。しかし、Javascript コードを elisp モジュールに文字列として埋め込むことは...かなりうまくいくと判断しました。

それを実現するために、新しい elisp モジュールを作成する短い elisp 関数を書きました。xxxx.el ではなく、古い名前 xxxx-bundle.el に基づいて新しい名前を選択します。次に、元の .el ファイルの内容を新しい名前のファイルに書き込みます。setq次に、単純なステートメントを同じファイルに書き込みます。Javascript プログラム全体が、そのステートメントのリテラル文字列値です。setq簡単にするのは、pp-to-stringすべての Javascript コードを emacs での使用に適したリテラル文字列にエスケープする elisp の関数です。

バンドルを生成する 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 ソースを含む変数 を参照できます。次のようになります。

ここに画像の説明を入力

このアプローチはおそらく他のモジュール、つまり別のデータ ファイルをバンドルする必要があるものでも役立つと思うので、ここに投稿します。

于 2012-03-28T05:39:58.967 に答える