2012-03-27 15 views
5

मैं एक elisp मॉड्यूल लिख रहा हूं जिसके लिए बाहरी टेक्स्ट फ़ाइल की आवश्यकता है।मैं एक "बाहरी" पाठ फ़ाइल को एक elisp मॉड्यूल में कैसे पैकेज कर सकता हूं?

उत्सुकता के लिए, मॉड्यूल the interactive JS REPL for Cscript.exe को emacs में खोल मोड के साथ एकीकृत करता है। यह मुझे विंडोज़ पर emacs में एक इंटरैक्टिव जावास्क्रिप्ट खोल चलाने की अनुमति देता है।

यह js-comint.el द्वारा प्रेरित था, लेकिन विंडोज और cscript.exe पर निर्भर एक अलग कार्यान्वयन है।

यह वर्तमान में काम कर रहा है, लेकिन दो अलग-अलग फाइलें हैं: .el फ़ाइल और .js फ़ाइल। मैं सिर्फ एक फाइल रखना पसंद करूंगा।

मेरे पास यह प्रश्न है: मैं बाहरी .js फ़ाइल को कैसे पैकेज कर सकता हूं जो इस बात के लिए पूर्व-आवश्यकता है, .el फ़ाइल में, ताकि मेरे पास एक-फ़ाइल इंस्टॉल हो सके?

मुझे लगता है कि मैं सिर्फ एक स्ट्रिंग वैरिएबल को परिभाषित कर सकता हूं (शायद minified) जेएस फ़ाइल और उसे .el मॉड्यूल में डालें। मुझे लगता है कि कुछ स्ट्रिंग-एस्केपमेंट मुद्दे होंगे लेकिन यह काम करेगा। क्या यह सबसे अच्छा तरीका है? कोई अन्य सुझाव?

उत्तर

1

ठीक है, मुझे ऐसा करने का बेहतर तरीका नहीं मिला। लेकिन मैंने फैसला किया कि एक स्ट्रिंग के रूप में elisp मॉड्यूल में जावास्क्रिप्ट कोड एम्बेड करना ... बहुत अच्छी तरह से काम किया।

ऐसा करने के लिए, मैंने एक छोटा elisp फ़ंक्शन लिखा, जो एक नया elisp मॉड्यूल बनाता है। यह xxxx.el के बजाय पुराने नाम - xxxx-bundle.el पर आधारित एक नया नाम चुनता है। फिर यह मूल .el फ़ाइल से सामग्री को नए नाम के साथ एक फ़ाइल में लिखता है। फिर यह एक ही फाइल में एक साधारण setq कथन लिखता है; संपूर्ण जावास्क्रिप्ट प्रोग्राम उस कथन में शाब्दिक स्ट्रिंग मान है। setq आसान है जो pp-to-string एलीस्प में फ़ंक्शन है, जो सभी जावास्क्रिप्ट कोड को 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 का संदर्भ दे सकती है, जिसमें कम से कम जावास्क्रिप्ट स्रोत होता है। यह इस तरह दिखता है:

enter image description here

मैं क्योंकि मुझे लगता है कि दृष्टिकोण शायद के रूप में अच्छी अन्य मॉड्यूल में उपयोगी होगा इस यहाँ पोस्टिंग कर रहा हूँ - कुछ भी एक अलग डेटा फ़ाइल बंडल करने की जरूरत है कि।

संबंधित मुद्दे

 संबंधित मुद्दे