[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[nongnu] elpa/lua-mode 6729533 234/468: Rewrite lua-send-buffer function
From: |
Philip Kaludercic |
Subject: |
[nongnu] elpa/lua-mode 6729533 234/468: Rewrite lua-send-buffer function (should fix issue #60) |
Date: |
Thu, 5 Aug 2021 04:58:43 -0400 (EDT) |
branch: elpa/lua-mode
commit 672953381a5ffcac59b78dae8f1c2ad109270c8a
Author: immerrr <immerrr+lua@gmail.com>
Commit: immerrr <immerrr+lua@gmail.com>
Rewrite lua-send-buffer function (should fix issue #60)
---
lua-mode.el | 118 +++++++++++++++++++++---------------------------------------
1 file changed, 40 insertions(+), 78 deletions(-)
diff --git a/lua-mode.el b/lua-mode.el
index 514af67..a67d56b 100644
--- a/lua-mode.el
+++ b/lua-mode.el
@@ -219,6 +219,7 @@ Should be a list of strings."
:type 'string
:group 'lua)
+
(defvar lua-process nil
"The active Lua subprocess")
@@ -1543,6 +1544,30 @@ When called interactively, switch to the process buffer."
(if (lua--called-interactively-p 'any)
(switch-to-buffer lua-process-buffer)))
+(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"))
+
+(defun lua-get-create-process ()
+ (or (and (comint-check-proc lua-process-buffer)
+ lua-process)
+ (prog1 (lua-start-process)
+ (when (fboundp 'process-kill-without-query)
+ (process-kill-without-query lua-process))))
+ (comint-simple-send lua-process lua-process-init-code)
+ lua-process)
+
(defun lua-kill-process ()
"Kill lua subprocess and its buffer."
(interactive)
@@ -1588,74 +1613,19 @@ If `lua-process' is nil or dead, start a new process
first."
(error "Not on a function definition")))))
(defun lua-send-region (start end)
- "Send region to lua subprocess."
(interactive "r")
- ;; make temporary lua file
- (let ((tempfile (lua-make-temp-file "lua-"))
- (last-prompt nil)
- (prompt-found nil)
- (lua-stdin-line-offset (count-lines (point-min) start))
- (lua-stdin-buffer (current-buffer))
- current-prompt )
- (write-region start end tempfile)
- (or (and lua-process
- (comint-check-proc lua-process-buffer))
- (lua-start-process lua-default-application))
- ;; kill lua process without query
- (if (fboundp 'process-kill-without-query)
- (process-kill-without-query lua-process))
- ;; send dofile(tempfile)
- (with-current-buffer lua-process-buffer
- (goto-char (point-max))
- (setq last-prompt (point-max))
- (comint-simple-send (get-buffer-process (current-buffer))
- (format "dofile(\"%s\")"
- (replace-regexp-in-string "\\\\" "\\\\\\\\"
tempfile)))
- ;; wait for prompt
- (while (not prompt-found)
- (accept-process-output (get-buffer-process (current-buffer)))
- (goto-char (point-max))
- (setq prompt-found (and (lua-prompt-line) (< last-prompt
(point-max)))))
- ;; remove temp. lua file
- (delete-file tempfile)
- (lua-postprocess-output-buffer lua-process-buffer last-prompt
lua-stdin-line-offset)
- (if lua-always-show
- (display-buffer lua-process-buffer)))))
-
-(defun lua-postprocess-output-buffer (buf start &optional
lua-stdin-line-offset)
- "Highlight tracebacks found in buf. If an traceback occurred return
-t, otherwise return nil. BUF must exist."
- (let ((lua-stdin-line-offset (or lua-stdin-line-offset 0))
- line file bol err-p)
- (with-current-buffer buf
- (goto-char start)
- (beginning-of-line)
- (if (re-search-forward lua-traceback-line-re nil t)
- (setq file (match-string 1)
- line (string-to-number (match-string 2)))))
- (when (and lua-jump-on-traceback line)
- (beep)
- ;; FIXME: highlight
- (lua-jump-to-traceback file line lua-stdin-line-offset)
- (setq err-p t))
- err-p))
-
-(defun lua-jump-to-traceback (file line lua-stdin-line-offset)
- "Jump to the Lua code in FILE at LINE."
- ;; sanity check: temporary-file-directory
- (if (string= (substring file 0 3) "...")
- (message "Lua traceback output truncated: customize
'temporary-file-directory' or increase 'LUA_IDSIZE' in 'luaconf.h'.")
- (let ((buffer (cond ((or (string-equal file tempfile) (string-equal file
"stdin"))
- (setq line (+ line lua-stdin-line-offset))
- lua-stdin-buffer)
- (t (find-file-noselect file)))))
- (pop-to-buffer buffer)
- ;; Force Lua mode
- (if (not (eq major-mode 'lua-mode))
- (lua-mode))
- ;; FIXME: fix offset when executing region
- (goto-char (point-min)) (forward-line (1- line))
- (message "Jumping to error in file %s on line %d" file line))))
+ (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)
+
+ (comint-simple-send (lua-get-create-process) command)
+ (when lua-always-show (lua-show-process-buffer))))
(defun lua-prompt-line ()
(save-excursion
@@ -1667,16 +1637,9 @@ t, otherwise return nil. BUF must exist."
(defun lua-send-lua-region ()
"Send preset lua region to lua subprocess."
(interactive)
- (or (and lua-region-start lua-region-end)
- (error "lua-region not set"))
- (or (and lua-process
- (comint-check-proc lua-process-buffer))
- (lua-start-process lua-default-application))
- (comint-simple-send lua-process
- (buffer-substring lua-region-start lua-region-end)
- )
- (if lua-always-show
- (display-buffer lua-process-buffer)))
+ (unless (and lua-region-start lua-region-end)
+ (error "lua-region not set"))
+ (lua-send-region lua-region-start lua-region-end))
(defalias 'lua-send-proc 'lua-send-defun)
@@ -1690,7 +1653,6 @@ t, otherwise return nil. BUF must exist."
"Restart lua subprocess and send whole file as input."
(interactive)
(lua-kill-process)
- (lua-start-process lua-default-application)
(lua-send-buffer))
(defun lua-show-process-buffer ()
- [nongnu] elpa/lua-mode 52dcf7b 167/468: Merge pull request #35 from vhallac/fix-34, (continued)
- [nongnu] elpa/lua-mode 52dcf7b 167/468: Merge pull request #35 from vhallac/fix-34, Philip Kaludercic, 2021/08/05
- [nongnu] elpa/lua-mode a3016a2 176/468: Avoid polluting "global namespace" when striving for backward compatibility, Philip Kaludercic, 2021/08/05
- [nongnu] elpa/lua-mode b124dd0 194/468: Add field containing commit SHA expanded during archive creation., Philip Kaludercic, 2021/08/05
- [nongnu] elpa/lua-mode 64c1e94 197/468: lua--builtins: add "self" as keyword, Philip Kaludercic, 2021/08/05
- [nongnu] elpa/lua-mode d6c8419 202/468: Fix incorrect error highlighting in 'for x = ...' statements, Philip Kaludercic, 2021/08/05
- [nongnu] elpa/lua-mode 2d94dee 204/468: Don't unindent comment close-bracket if it doesn't match open-bracket, Philip Kaludercic, 2021/08/05
- [nongnu] elpa/lua-mode c1fcfb6 211/468: Add NEWS file, Philip Kaludercic, 2021/08/05
- [nongnu] elpa/lua-mode d0fa50d 217/468: lua-make-delimited-matcher: wrap into eval-and-compile, Philip Kaludercic, 2021/08/05
- [nongnu] elpa/lua-mode b684790 219/468: Revert "Rename lua--function-name-rx, use it in font-lock keywords init", Philip Kaludercic, 2021/08/05
- [nongnu] elpa/lua-mode 8e19d23 231/468: Add basic indentation tests, Philip Kaludercic, 2021/08/05
- [nongnu] elpa/lua-mode 6729533 234/468: Rewrite lua-send-buffer function (should fix issue #60),
Philip Kaludercic <=
- [nongnu] elpa/lua-mode afc1ea0 235/468: Send init-code in lua-start-process, enable compilation-shell-minor-mode, Philip Kaludercic, 2021/08/05
- [nongnu] elpa/lua-mode 1ede907 242/468: Fix several cases for `indent-new-comment-line' (M-j), Philip Kaludercic, 2021/08/05
- [nongnu] elpa/lua-mode 6c55abb 245/468: Add lua-rx/-to-string wrappers to simplify writing Lua-specific regexps, Philip Kaludercic, 2021/08/05
- [nongnu] elpa/lua-mode 1452f8e 244/468: should-lua-indent: accept strings rather than lists to simplify writing tests, Philip Kaludercic, 2021/08/05
- [nongnu] elpa/lua-mode a0f3a6f 250/468: Simplify font-lock & imenu regexps with lua-rx, Philip Kaludercic, 2021/08/05
- [nongnu] elpa/lua-mode e430d01 252/468: Remove deprecated test code from test/, put current code there instead, Philip Kaludercic, 2021/08/05
- [nongnu] elpa/lua-mode 1d21a10 259/468: Reorganize test structure, Philip Kaludercic, 2021/08/05
- [nongnu] elpa/lua-mode dc55356 261/468: lua-forward-regexp: make sure direction is `forward', Philip Kaludercic, 2021/08/05
- [nongnu] elpa/lua-mode 0a986f7 266/468: Add hideshow support for "repeat... until" blocks, Philip Kaludercic, 2021/08/05
- [nongnu] elpa/lua-mode 8d27d2c 268/468: fixed up tests && improved lua-kill-process, Philip Kaludercic, 2021/08/05