2012-03-27 14 views
5

Sto scrivendo un modulo elisp che richiede un file di testo esterno.Come posso impacchettare un file di testo "esterno" in un modulo elisp?

Per i curiosi, il modulo integra the interactive JS REPL for Cscript.exe con la modalità shell in emacs. Mi permette di eseguire una shell javascript interattiva in emacs, su Windows.

Questo è stato motivato da js-comint.el, ma è un'implementazione separata dipendente da Windows e cscript.exe.

Attualmente funziona, ma ci sono due file distinti: il file .el e il file .js. Preferirei avere solo un file.

La domanda che ho è questa: come posso impacchettare il file .js esterno che è un prerequisito per questa cosa, nel file .el, in modo che possa avere un'installazione di un file?

Immagino che potrei semplicemente definire una variabile di stringa con il file js (forse minificato) e inserirla nel modulo .el. Suppongo che ci saranno alcuni problemi di scappamento delle stringhe, ma funzionerà. È il modo migliore? Qualche altro suggerimento?

risposta

1

ok, non ho trovato un modo migliore per farlo. Ma ho deciso che inserire il codice Javascript nel modulo elisp come stringa ... ha funzionato piuttosto bene.

Per riuscirci, ho scritto una breve funzione elisp, che crea un nuovo modulo elisp. Seleziona un nuovo nome basato sul vecchio nome - xxxx-bundle.el invece di xxxx.el. Quindi scrive il contenuto del file .el originale in un file con il nuovo nome. Quindi scrive una semplice dichiarazione setq nello stesso file; l'intero programma Javascript è il valore stringa letterale in quella dichiarazione. La cosa che rende semplice setq è la funzione pp-to-string in elisp, che sfugge a tutti i codici Javascript in una stringa letterale adatta per l'uso in emacs.

fn per generare il fascio appare come questa

(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 aiutante di ridurre al minimo il contenuto semplicemente crolla spazi ed elimina nuove righe consecutive, questo genere di cose.

Il file .el risultante può quindi fare riferimento alla variabile, jsshell-js-src, che contiene l'origine Javascript ridotta. Ecco come si presenta:

enter image description here

sto postando questo qui perché penso che l'approccio sarebbe probabilmente utile in altri moduli, come pure - tutto ciò che ha bisogno di impacchettare un file di dati separato.

Problemi correlati