diff --git a/README.md b/README.md index b01417e4..9000d294 100644 --- a/README.md +++ b/README.md @@ -71,6 +71,7 @@ Plugin | Description [`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 [`projectmanager`](plugins/projectmanager.lua?raw=1) | Save projects and load/reload them quickly +[`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))* diff --git a/plugins/runinterminal.lua b/plugins/runinterminal.lua new file mode 100644 index 00000000..0cedad6d --- /dev/null +++ b/plugins/runinterminal.lua @@ -0,0 +1,101 @@ +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() + 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 .. "\"" + ) + + os.execute(config.run_cmd:format(cmd)) + 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", + ["%.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 = "xterm -hold -e \"%s\"" +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 + 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() + local func = run.choose() + if not func then + core.error "No matching run configuration was found" + return + end + func() +end + +command.add("core.docview", { + ["run:run-doc"] = run.run_doc, +}) + +keymap.add { + ["f5"] = "run:run-doc", +} + +return run