From 9eddee17bc2ef1b9118449c1d750e1ef143d5786 Mon Sep 17 00:00:00 2001 From: arbv Date: Wed, 22 Jun 2016 21:49:10 +0300 Subject: [PATCH 1/3] Fix sending of long code strings to REPL using temporary files (issue: #89) --- lua-mode.el | 49 +++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 45 insertions(+), 4 deletions(-) diff --git a/lua-mode.el b/lua-mode.el index 1ca228b..afe4a78 100644 --- a/lua-mode.el +++ b/lua-mode.el @@ -1779,12 +1779,53 @@ When called interactively, switch to the process buffer." (set-marker lua-region-end (or arg (point)))) (defun lua-send-string (str) - "Send STR plus a newline to Lua subprocess. + "Load STR plus a newline into Lua subprocess. If `lua-process' is nil or dead, start a new process first." - (unless (string-equal (substring str -1) "\n") - (setq str (concat str "\n"))) - (process-send-string (lua-get-create-process) str)) + (let ((tmp-file (make-temp-file "luamode" nil ".tmp")) + (lua-process (lua-get-create-process))) + ;; write data into temporary file + (with-temp-buffer + (insert (if (string-equal (substring str -1) "\n") + str + (concat str "\n"))) + (write-file tmp-file)) + ;; evaluate data in the temporary file and then remove it + (process-send-string + lua-process + (format (concat + "\n" + "local tmp = '%s';" + "local res, e = pcall(function () " + " local do_loadstring = loadstring or load;" + "" + " local f, e = io.open(tmp, 'r');" ; open temporary file + " if e then " + " os.remove(tmp);" + " error(e);" + " return;" + " end " + "" + " local cont, e = f:read('*all');" ; read all data + " if e then " + " os.remove(tmp);" + " error(e);" + " return;" + " end " + "" + " f:close(); f = nil;" ; close and remove file + " os.remove(tmp);" + "" + " local f, e = do_loadstring(cont);" ; handle chunk + " if e then " + " error(e);" + " return;" + " end " + "" + " return f();" ; execute chunk + "end);" + "if e then _, _ = os.remove(tmp); error(e); end" ; handle error, if any + "\n") tmp-file)))) (defun lua-send-current-line () "Send current line to Lua subprocess, found in `lua-process'. From 1c8238e85acb30013320142780fbb4ce1b91900c Mon Sep 17 00:00:00 2001 From: arbv Date: Wed, 22 Jun 2016 23:00:03 +0300 Subject: [PATCH 2/3] Send short (<= 500 characters) code strings directly to Lua REPL. --- lua-mode.el | 95 ++++++++++++++++++++++++++++------------------------- 1 file changed, 50 insertions(+), 45 deletions(-) diff --git a/lua-mode.el b/lua-mode.el index afe4a78..514f1b4 100644 --- a/lua-mode.el +++ b/lua-mode.el @@ -1778,54 +1778,59 @@ When called interactively, switch to the process buffer." (interactive) (set-marker lua-region-end (or arg (point)))) -(defun lua-send-string (str) - "Load STR plus a newline into Lua subprocess. +(defun lua-send-string (chunk-str) + "Load CHUNK-STR plus a newline into Lua subprocess. If `lua-process' is nil or dead, start a new process first." - (let ((tmp-file (make-temp-file "luamode" nil ".tmp")) + (let ((str (if (string-equal (substring chunk-str -1) "\n") + chunk-str + (concat chunk-str "\n"))) (lua-process (lua-get-create-process))) - ;; write data into temporary file - (with-temp-buffer - (insert (if (string-equal (substring str -1) "\n") - str - (concat str "\n"))) - (write-file tmp-file)) - ;; evaluate data in the temporary file and then remove it - (process-send-string - lua-process - (format (concat - "\n" - "local tmp = '%s';" - "local res, e = pcall(function () " - " local do_loadstring = loadstring or load;" - "" - " local f, e = io.open(tmp, 'r');" ; open temporary file - " if e then " - " os.remove(tmp);" - " error(e);" - " return;" - " end " - "" - " local cont, e = f:read('*all');" ; read all data - " if e then " - " os.remove(tmp);" - " error(e);" - " return;" - " end " - "" - " f:close(); f = nil;" ; close and remove file - " os.remove(tmp);" - "" - " local f, e = do_loadstring(cont);" ; handle chunk - " if e then " - " error(e);" - " return;" - " end " - "" - " return f();" ; execute chunk - "end);" - "if e then _, _ = os.remove(tmp); error(e); end" ; handle error, if any - "\n") tmp-file)))) + (if (<= (length str) 500) ; handle "short" strings directly + (process-send-string lua-process str) + (let ((tmp-file (make-temp-file "luamode" nil ".tmp"))) ; handle long strings via temporary files + ;; write data into temporary file + (with-temp-buffer + (insert (if (string-equal (substring str -1) "\n") + str + (concat str "\n"))) + (write-file tmp-file)) + ;; evaluate data in the temporary file and then remove it + (process-send-string + lua-process + (format (concat + "\n" + "local tmp = '%s';" + "local res, e = pcall(function () " + " local do_loadstring = loadstring or load;" + "" + " local f, e = io.open(tmp, 'r');" ; open temporary file + " if e then " + " os.remove(tmp);" + " error(e);" + " return;" + " end " + "" + " local cont, e = f:read('*all');" ; read all data + " if e then " + " os.remove(tmp);" + " error(e);" + " return;" + " end " + "" + " f:close(); f = nil;" ; close and remove file + " os.remove(tmp);" + "" + " local f, e = do_loadstring(cont);" ; handle chunk + " if e then " + " error(e);" + " return;" + " end " + "" + " return f();" ; execute chunk + "end);" + "if e then _, _ = os.remove(tmp); error(e); end" ; handle error, if any + "\n") tmp-file)))))) (defun lua-send-current-line () "Send current line to Lua subprocess, found in `lua-process'. From 9368e417529afbdba0b92eb887f4349e33823616 Mon Sep 17 00:00:00 2001 From: Artem Boldarev Date: Thu, 23 Jun 2016 12:06:52 +0300 Subject: [PATCH 3/3] Remove useless check for trailing newline (it was made above). --- lua-mode.el | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lua-mode.el b/lua-mode.el index 514f1b4..cb7cff0 100644 --- a/lua-mode.el +++ b/lua-mode.el @@ -1791,9 +1791,7 @@ If `lua-process' is nil or dead, start a new process first." (let ((tmp-file (make-temp-file "luamode" nil ".tmp"))) ; handle long strings via temporary files ;; write data into temporary file (with-temp-buffer - (insert (if (string-equal (substring str -1) "\n") - str - (concat str "\n"))) + (insert str) (write-file tmp-file)) ;; evaluate data in the temporary file and then remove it (process-send-string