emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[nongnu] elpa/lua-mode 397b883 236/468: Send code to inferior process vi


From: Philip Kaludercic
Subject: [nongnu] elpa/lua-mode 397b883 236/468: Send code to inferior process via loadstring to avoid tempfile issues
Date: Thu, 5 Aug 2021 04:58:44 -0400 (EDT)

branch: elpa/lua-mode
commit 397b883209648dc7e34903bc82b8c8ee0e4c8f3a
Author: immerrr <immerrr+lua@gmail.com>
Commit: immerrr <immerrr+lua@gmail.com>

    Send code to inferior process via loadstring to avoid tempfile issues
---
 lua-mode.el | 54 +++++++++++++++++++++++++++++++++---------------------
 1 file changed, 33 insertions(+), 21 deletions(-)

diff --git a/lua-mode.el b/lua-mode.el
index 336e93a..6c038f3 100644
--- a/lua-mode.el
+++ b/lua-mode.el
@@ -1525,19 +1525,32 @@ This function just searches for a `end' at the 
beginning of a line."
     ret))
 
 (defvar lua-process-init-code
-  (concat "function luamode_dofile(fname, displayname)"
-          "  local f = assert(io.open(fname))"
-          "  local d = f:read('*all')"
-          "  f:close()"
-          "  x, e = loadstring(d, '@'..displayname)"
-          "  if fname ~= displayname then"
-          "    os.remove(fname)"
-          "  end"
-          "  if e then"
-          "    error(e)"
-          "  end"
-          "  return x()"
-          "end"))
+  (mapconcat
+   'identity
+   '("function luamode_loadstring(str, displayname, lineoffset)"
+     "  if lineoffset > 1 then"
+     "    str = string.rep('\\n', lineoffset - 1) .. str"
+     "  end"
+     ""
+     "  x, e = loadstring(str, '@'..displayname)"
+     "  if e then"
+     "    error(e)"
+     "  end"
+     "  return x()"
+     "end")
+   " "))
+
+(defun lua-make-lua-string (str)
+  "Convert string to Lua literal."
+  (save-match-data
+    (with-temp-buffer
+      (insert str)
+      (goto-char (point-min))
+      (while (re-search-forward "[\"'\\\n]" nil t)
+        (if (string= (match-string 0) "\n")
+            (replace-match "\\\\n")
+          (replace-match "\\\\\\&" t)))
+      (concat "'" (buffer-string) "'"))))
 
 (defun lua-start-process (&optional name program startfile &rest switches)
   "Start a lua process named NAME, running PROGRAM.
@@ -1620,14 +1633,13 @@ If `lua-process' is nil or dead, start a new process 
first."
   (interactive "r")
   (let* ((lineno (line-number-at-pos start))
          (lua-tempfile (lua-make-temp-file "lua-"))
-         (lua-file (or (buffer-file-name) lua-tempfile))
-         (command (format "luamode_dofile('%s', '%s')"
-                          (replace-regexp-in-string "\\\\" "\\\\\\\\" 
lua-tempfile)
-                          (replace-regexp-in-string "\\\\" "\\\\\\\\" 
lua-file))))
-
-    (write-region (concat (make-string (1- lineno) ?\n)
-                          (buffer-substring-no-properties start end)) nil 
lua-tempfile)
-
+         (lua-file (or (buffer-file-name) (buffer-name)))
+         (region-str (buffer-substring-no-properties start end))
+         (command
+          (format "luamode_loadstring(%s, %s, %s)"
+                  (lua-make-lua-string region-str)
+                  (lua-make-lua-string lua-file)
+                  lineno)))
     (comint-simple-send (lua-get-create-process) command)
     (when lua-always-show (lua-show-process-buffer))))
 



reply via email to

[Prev in Thread] Current Thread [Next in Thread]