Skip to content

Commit

Permalink
Add F12 save and make
Browse files Browse the repository at this point in the history
  • Loading branch information
Tero Karvinen committed Nov 2, 2022
1 parent bc8e1f0 commit 5704d31
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 4 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
# micro-run

*Press F5 to save and run the current file.*
*Press F5 to save and run the current file, F12 to make*

F12 compiles your project even if Makefile is in a higher directory. It goes up (cd ..) until it finds make file or is at root directory.

![micro-run screenshot - press F5 to run current file](micro-run.png)

Micro run is a plugin for Micro editor.

Supports Go, Go test, Python 3, Lua and executable files.
F5 save & run supports Go, Go test, Python 3, Lua and executable files.

As an extra feature, micro-run can run any script that's marked as executable - in any language. In Linux, executable files are those that have 'chmod u+x foo' set and usually start with a shebang "#!/usr/bin/foo".

Expand Down
78 changes: 77 additions & 1 deletion main.lua
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
-- micro-run - Press F5 to run the current file, a plugin for Micro editor
-- micro-run - Press F5 to run the current file, F12 to run make
-- Copyright 2020-2022 Tero Karvinen http://TeroKarvinen.com/micro
-- https://github.com/terokarvinen/micro-run

local config = import("micro/config")
local shell = import("micro/shell")
local micro = import("micro")
local os = import("os")

function init()
config.MakeCommand("runit", runitCommand, config.NoComplete)
config.TryBindKey("F5", "command:runit", true)

config.MakeCommand("makeup", makeupCommand, config.NoComplete)
config.TryBindKey("F12", "command:makeup", true)
end

function runitCommand(bp) -- bp BufPane
Expand All @@ -33,3 +38,74 @@ function runitCommand(bp) -- bp BufPane
shell.RunInteractiveShell(cmd, true, false)
end

function makeup()
-- Go up directories until a Makefile is found and run 'make'.

-- Caller is responsible for returning to original working directory,
-- and micro will save the current file into the directory where
-- we are in. In this plugin, makeupCommand() implements returning
-- to original directory

local err, pwd, prevdir
for i = 1,20 do -- arbitrary number to make sure we exit one day
-- pwd
pwd, err = os.Getwd()
if err ~= nil then
micro.InfoBar():Message("Error: os.Getwd() failed!")
return
end
micro.InfoBar():Message("Working directory is ", pwd)

-- are we at root
if pwd == prevdir then
micro.InfoBar():Message("Makefile not found, looked at ", i, " directories.")
return
end
prevdir = pwd

-- check for file
local dummy, err = os.Stat("Makefile")
if err ~= nil then
micro.InfoBar():Message("(not found in ", pwd, ")")
else
micro.InfoBar():Message("Running make, Found Makefile in ", pwd)
local out = shell.RunInteractiveShell("make", true, true)
return
end

-- cd ..
local err = os.Chdir("..")
if err ~= nil then
micro.InfoBar():Message("Error: os.Chdir() failed!")
return
end

end
micro.InfoBar():Message("Warning: ran full 20 rounds but did not recognize root directory")
return

end

function makeupCommand(bp)
bp:Save()
micro.InfoBar():Message("makeup called")

-- pwd
local pwd, err = os.Getwd()
if err ~= nil then
micro.InfoBar():Message("Error: os.Getwd() failed!")
return
end
micro.InfoBar():Message("Working directory is ", pwd)
local startDir = pwd

makeup()

-- finally, back to the directory where we were
local err = os.Chdir(startDir)
if err ~= nil then
micro.InfoBar():Message("Error: os.Chdir() failed!")
return
end

end
9 changes: 8 additions & 1 deletion repo.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
[{
"Name": "runit",
"Description": "Press F5 to save and run the current file. Python, Go, Lua, executable file.",
"Description": "Press F5 to save and run, F12 to make.",
"Website": "https://github.com/terokarvinen/micro-run",
"Tags": ["run", "execute", "Go", "Python", "Bash", "Lua", "F5"],
"Versions": [
{
"Version": "0.0.5",
"Url": "https://github.com/terokarvinen/micro-run/archive/v0.0.5.zip",
"Require": {
"micro": ">=2.0.0"
}
},
{
"Version": "0.0.4",
"Url": "https://github.com/terokarvinen/micro-run/archive/v0.0.4.zip",
Expand Down

0 comments on commit 5704d31

Please sign in to comment.