Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run plugin #82

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
[`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))*
Expand Down
101 changes: 101 additions & 0 deletions plugins/runinterminal.lua
Original file line number Diff line number Diff line change
@@ -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")
qrmoon marked this conversation as resolved.
Show resolved Hide resolved
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 = (os.getenv("TERMINAL") or "xterm") .. " -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
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