2012-03-27 13 views
5

Tôi đang viết một mô-đun elisp yêu cầu tệp văn bản bên ngoài.Tôi có thể đóng gói một tệp văn bản "bên ngoài" vào một mô-đun elisp như thế nào?

Đối với tò mò, mô-đun tích hợp the interactive JS REPL for Cscript.exe với chế độ trình bao trong các emacs. Nó cho phép tôi chạy một trình bao javascript tương tác trong các emacs, trên Windows.

Điều này được thúc đẩy bởi js-comint.el, nhưng là triển khai riêng biệt phụ thuộc vào Windows và cscript.exe.

Nó hiện đang hoạt động, nhưng có hai tệp riêng biệt: tệp .el và tệp .js. Tôi muốn chỉ có một tập tin.

Câu hỏi tôi có là: làm thế nào tôi có thể gói tệp .js bên ngoài là điều kiện tiên quyết cho điều này, vào tệp .el, để tôi có thể cài đặt một tệp?

Tôi hình dung rằng tôi chỉ có thể định nghĩa một biến chuỗi với tệp js (có thể rút gọn) và chèn nó vào mô-đun .el. Tôi cho rằng sẽ có một vài vấn đề về chuỗi escapement nhưng điều này sẽ làm việc. Đó có phải là cách tốt nhất không? Bất cứ một đề nghị nào khác?

Trả lời

1

ok, tôi không tìm thấy cách nào tốt hơn để thực hiện việc này. Nhưng tôi đã quyết định rằng nhúng mã Javascript vào mô-đun elisp như một chuỗi ... làm việc khá tốt.

Để làm cho nó xảy ra, tôi đã viết một hàm elisp ngắn, tạo ra một mô-đun elisp mới. Nó chọn một tên mới dựa trên tên cũ - xxxx-bundle.el thay vì xxxx.el. Sau đó, nó ghi nội dung từ tệp .el gốc vào một tệp có tên mới. Sau đó, nó viết một tuyên bố đơn giản setq vào cùng một tệp; toàn bộ chương trình Javascript là giá trị chuỗi chữ trong câu lệnh đó. Điều làm cho hàm setq dễ dàng là chức năng pp-to-string trong elisp, thoát khỏi tất cả mã Javascript thành một chuỗi ký tự phù hợp để sử dụng trong các emacs.

Các fn để tạo ra bó Trông như thế này:

(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")))))) 

Các helper fn để giảm thiểu các nội dung chỉ sụp đổ khoảng trắng và loại bỏ dòng mới liên tục, đó đại loại như vậy.

Tệp .el kết quả sau đó có thể tham chiếu biến, jsshell-js-src, chứa nguồn Javascript được thu nhỏ. Nó trông giống như thế này:

enter image description here

Tôi gửi bài này ở đây vì tôi nghĩ rằng phương pháp này có lẽ sẽ có ích trong các module khác cũng - bất cứ điều gì mà cần phải bó một tệp dữ liệu riêng biệt.