2012-03-27 11 views
5

私は、外部テキストファイルを必要とするelispモジュールを作成しています。elispモジュールに「外部」テキストファイルをパッケージ化する方法はありますか?

奇妙なことに、モジュールはthe interactive JS REPL for Cscript.exeをemacsのシェルモードと統合しています。 Windows上でemacsでインタラクティブなjavascriptシェルを実行できます。

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

現在は動作していますが、.elファイルと.jsファイルという2つの異なるファイルがあります。私は1つのファイルを持つことを好むでしょう。

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

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

答えて

1

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

私は、新しいelispモジュールを作成する短いelisp関数を作成しました。 古い名前(xxxx.elではなくxxxx-bundle.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ファイルは、最小化されたJavascriptソースを含む変数jsshell-js-srcを参照できます。 - 個別のデータファイルをバンドルする必要は何も私はアプローチがおそらく同様に他のモジュールに有用であろうと思うので

enter image description here

私はここにこれを掲示しています:それはこのようになります。

関連する問題