From a6145c06e8c1c77cc6e72535d4fe079d63f1887f Mon Sep 17 00:00:00 2001 From: qermon Date: Fri, 24 Jul 2020 00:03:43 +0200 Subject: [PATCH 1/9] Add run.lua --- plugins/run.lua | 71 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 plugins/run.lua diff --git a/plugins/run.lua b/plugins/run.lua new file mode 100644 index 00000000..7609a720 --- /dev/null +++ b/plugins/run.lua @@ -0,0 +1,71 @@ +local core = require "core" +local common = require "core.common" +local command = require "core.command" +local config = require "core.config" +local keymap = require "core.keymap" + +local run = {} + +-- return a function which will run the current doc +-- as a script for the given language +function run.lang(lang) + return function() + core.log "Running a file..." + local cmd = assert(config.run[lang]):format( + "\"" .. core.active_view.doc.filename .. "\"" + ) + + os.execute(config.run_cmd:format(cmd)) + end +end + +-- file extensions and functions +config.run_files = { + ["%.py$"] = run.lang "python", + ["%.pyw$"] = run.lang "python", + ["%.lua$"] = run.lang "lua", + ["%.c$"] = run.lang "c", +} + +-- system commands for running files +-- the filename is already quoted +config.run = { + python = "python %s", + lua = "lua %s", + c = "gcc %s && " .. (PLATFORM == "Windows" and "a.exe" or "./a.out"), +} + +-- for systems other than Windows +if PLATFORM == "Windows" then + config.run_cmd = "start cmd /c \"call %s & pause\"" +else + config.run_cmd = "gnome-terminal -x sh -c \"%s; bash\"" +end + +-- run the current doc based on its extension +function run.run_file(this) + local doc = core.active_view.doc + if doc.filename then + doc:save() + else + core.error("Cannot run an unsaved file") + return + end + + for pattern, func in pairs(config.run_files) do + if common.match_pattern(doc.filename, pattern) then + func() + break + end + end +end + +command.add("core.docview", { + ["run:run-doc"] = run.run_file, +}) + +keymap.add { + ["f5"] = "run:run-doc", +} + +return run From 504ca6f40ba102d900198d4909660bc73a092748 Mon Sep 17 00:00:00 2001 From: qermon Date: Fri, 24 Jul 2020 00:05:28 +0200 Subject: [PATCH 2/9] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 55e92a8e..23f28944 100644 --- a/README.md +++ b/README.md @@ -68,6 +68,7 @@ Plugin | Description [`motiontrail`](plugins/motiontrail.lua?raw=1) | Adds a motion-trail to the caret *([screenshot](https://user-images.githubusercontent.com/3920290/83256814-085ccb00-a1ab-11ea-9e35-e6633cbed1a9.gif))* [`openfilelocation`](plugins/openfilelocation.lua?raw=1) | Opens the parent directory of the current file in the file manager [`openselected`](plugins/openselected.lua?raw=1) | Opens the selected filename or url +[`run`](plugins/run.lua?raw=1) | Runs the currently open file in a terminal window (`f5` or `run:run-doc`) [`scale`](plugins/scale.lua?raw=1) | Provides support for dynamically adjusting the scale of the code font / UI (`ctrl+-`, `ctrl+=`) [`scalestatus`](plugins/scalestatus.lua?raw=1) | Displays current scale (zoom) in status view (depends on scale plugin) [`selectionhighlight`](plugins/selectionhighlight.lua?raw=1) | Highlights regions of code that match the current selection *([screenshot](https://user-images.githubusercontent.com/3920290/80710883-5f597c80-8ae7-11ea-97f0-76dfacc08439.png))* From 85ceabb20f1f3a2a1090d8d2b8beebb9f8820046 Mon Sep 17 00:00:00 2001 From: qermon Date: Fri, 24 Jul 2020 00:37:06 +0200 Subject: [PATCH 3/9] Update run.lua --- plugins/run.lua | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/plugins/run.lua b/plugins/run.lua index 7609a720..f932b2e2 100644 --- a/plugins/run.lua +++ b/plugins/run.lua @@ -10,6 +10,14 @@ local run = {} -- as a script for the given language function run.lang(lang) return function() + local doc = core.active_view.doc + if doc.filename then + doc:save() + else + core.error("Cannot run an unsaved file") + return + end + core.log "Running a file..." local cmd = assert(config.run[lang]):format( "\"" .. core.active_view.doc.filename .. "\"" @@ -42,26 +50,23 @@ else config.run_cmd = "gnome-terminal -x sh -c \"%s; bash\"" end --- run the current doc based on its extension -function run.run_file(this) +-- choose the proper function based on filename +function run.choose() local doc = core.active_view.doc - if doc.filename then - doc:save() - else - core.error("Cannot run an unsaved file") - return - end - for pattern, func in pairs(config.run_files) do if common.match_pattern(doc.filename, pattern) then - func() - break + return func end end end +-- run the currently open doc +function run.run_doc() + run.choose()() +end + command.add("core.docview", { - ["run:run-doc"] = run.run_file, + ["run:run-doc"] = run.run_doc, }) keymap.add { From 7f3dc07a2254482372077caca772cf3014735f75 Mon Sep 17 00:00:00 2001 From: qermon Date: Fri, 24 Jul 2020 01:35:12 +0200 Subject: [PATCH 4/9] Improve matching in run.choose It will now always choose the longest match --- plugins/run.lua | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/plugins/run.lua b/plugins/run.lua index f932b2e2..1b87305c 100644 --- a/plugins/run.lua +++ b/plugins/run.lua @@ -50,19 +50,33 @@ else config.run_cmd = "gnome-terminal -x sh -c \"%s; bash\"" end +local function compare_length(a, b) + return a.length > b.length +end + -- choose the proper function based on filename function run.choose() local doc = core.active_view.doc + local res = {} for pattern, func in pairs(config.run_files) do - if common.match_pattern(doc.filename, pattern) then - return func + local s, e = common.match_pattern(doc.filename, pattern) + if s then + table.insert(res, { func=func, length=e-s }) end end + if #res == 0 then return false end + table.sort(res, compare_length) + return res[1].func end -- run the currently open doc function run.run_doc() - run.choose()() + local func = run.choose() + if not func then + core.error "No matching run configuration was found" + return + end + func() end command.add("core.docview", { From 5fd0f2acbc60a78a3c6ad2977b0c61a3242f0756 Mon Sep 17 00:00:00 2001 From: qermon Date: Sat, 1 Aug 2020 01:15:54 +0200 Subject: [PATCH 5/9] Rename run.lua to runinterminal.lua --- plugins/{run.lua => runinterminal.lua} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename plugins/{run.lua => runinterminal.lua} (100%) diff --git a/plugins/run.lua b/plugins/runinterminal.lua similarity index 100% rename from plugins/run.lua rename to plugins/runinterminal.lua From fecd56b0ffd1b817a7c847bfb496b308233e1ad3 Mon Sep 17 00:00:00 2001 From: qermon Date: Sat, 1 Aug 2020 01:16:29 +0200 Subject: [PATCH 6/9] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 23f28944..4e01df08 100644 --- a/README.md +++ b/README.md @@ -68,7 +68,7 @@ Plugin | Description [`motiontrail`](plugins/motiontrail.lua?raw=1) | Adds a motion-trail to the caret *([screenshot](https://user-images.githubusercontent.com/3920290/83256814-085ccb00-a1ab-11ea-9e35-e6633cbed1a9.gif))* [`openfilelocation`](plugins/openfilelocation.lua?raw=1) | Opens the parent directory of the current file in the file manager [`openselected`](plugins/openselected.lua?raw=1) | Opens the selected filename or url -[`run`](plugins/run.lua?raw=1) | Runs the currently open file in a terminal window (`f5` or `run:run-doc`) +[`runinterminal`](plugins/runinterminal.lua?raw=1) | Runs the currently open file in a terminal window (`f5` or `run:run-doc`) [`scale`](plugins/scale.lua?raw=1) | Provides support for dynamically adjusting the scale of the code font / UI (`ctrl+-`, `ctrl+=`) [`scalestatus`](plugins/scalestatus.lua?raw=1) | Displays current scale (zoom) in status view (depends on scale plugin) [`selectionhighlight`](plugins/selectionhighlight.lua?raw=1) | Highlights regions of code that match the current selection *([screenshot](https://user-images.githubusercontent.com/3920290/80710883-5f597c80-8ae7-11ea-97f0-76dfacc08439.png))* From 34aa4b034cf7d80b8965a345dc9b525a9d261dda Mon Sep 17 00:00:00 2001 From: qermon Date: Wed, 12 Aug 2020 00:25:52 +0200 Subject: [PATCH 7/9] Default to $TERMINAL or xterm --- plugins/runinterminal.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/runinterminal.lua b/plugins/runinterminal.lua index 1b87305c..7875b459 100644 --- a/plugins/runinterminal.lua +++ b/plugins/runinterminal.lua @@ -47,7 +47,7 @@ config.run = { if PLATFORM == "Windows" then config.run_cmd = "start cmd /c \"call %s & pause\"" else - config.run_cmd = "gnome-terminal -x sh -c \"%s; bash\"" + config.run_cmd = (os.getenv("TERMINAL") or "xterm") .. " -x sh -c \"%s; bash\"" end local function compare_length(a, b) From 691cae8f805c157355f782407a5bcb8ace2c745f Mon Sep 17 00:00:00 2001 From: qermon Date: Wed, 12 Aug 2020 00:26:38 +0200 Subject: [PATCH 8/9] Add run.build method --- plugins/runinterminal.lua | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/plugins/runinterminal.lua b/plugins/runinterminal.lua index 7875b459..8c59693d 100644 --- a/plugins/runinterminal.lua +++ b/plugins/runinterminal.lua @@ -27,6 +27,17 @@ function run.lang(lang) end end +-- same as run.lang except it doesn't use the active doc's name +-- (thus doesn't require a docview to be open) +function run.build(build) + return function() + core.log "Running a build..." + local cmd = assert(config.run[build]) + + os.execute(config.run_cmd:format(cmd)) + end +end + -- file extensions and functions config.run_files = { ["%.py$"] = run.lang "python", From 6ea7d0e94b93783831562174015dba3933a8d4c4 Mon Sep 17 00:00:00 2001 From: qermon Date: Sun, 20 Sep 2020 19:10:53 +0200 Subject: [PATCH 9/9] Change default xterm arguments --- plugins/runinterminal.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/runinterminal.lua b/plugins/runinterminal.lua index 8c59693d..0cedad6d 100644 --- a/plugins/runinterminal.lua +++ b/plugins/runinterminal.lua @@ -58,7 +58,7 @@ config.run = { if PLATFORM == "Windows" then config.run_cmd = "start cmd /c \"call %s & pause\"" else - config.run_cmd = (os.getenv("TERMINAL") or "xterm") .. " -x sh -c \"%s; bash\"" + config.run_cmd = "xterm -hold -e \"%s\"" end local function compare_length(a, b)