Skip to content

Commit

Permalink
Add F9 to 'make' in background
Browse files Browse the repository at this point in the history
  • Loading branch information
Tero Karvinen committed Nov 2, 2022
1 parent 5704d31 commit 3392fed
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 10 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# micro-run

*Press F5 to save and run the current file, F12 to make*
*Press F5 to save and run the current file, F12 to make, F9 to make in background*

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.

F9 runs 'make' in background, allowing you to keep editing while your project compiles.

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

Micro run is a plugin for Micro editor.
Expand Down
42 changes: 34 additions & 8 deletions main.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
-- micro-run - Press F5 to run the current file, F12 to run make
-- micro-run - Press F5 to run the current file, F12 to run make, F9 to make in background
-- Copyright 2020-2022 Tero Karvinen http://TeroKarvinen.com/micro
-- https://github.com/terokarvinen/micro-run

Expand All @@ -13,9 +13,14 @@ function init()

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

config.MakeCommand("makeupbg", makeupbgCommand, config.NoComplete)
config.TryBindKey("F9", "command:makeupbg", true)
end

function runitCommand(bp) -- bp BufPane
-- save & run the file we're editing
-- choose run command according to filetype detected by micro
bp:Save()

local filename = bp.Buf.GetName(bp.Buf)
Expand All @@ -38,14 +43,16 @@ function runitCommand(bp) -- bp BufPane
shell.RunInteractiveShell(cmd, true, false)
end

function makeup()
function makeup(bg)
-- 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

micro.Log("bg: ", bg)

local err, pwd, prevdir
for i = 1,20 do -- arbitrary number to make sure we exit one day
-- pwd
Expand All @@ -68,8 +75,14 @@ function makeup()
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)
micro.InfoBar():Message("Running make, found Makefile in ", pwd)
if bg then
local outfunc, err = shell.RunBackgroundShell("make")
-- local out = string.sub(outfunc(), -60) -- this will block, can't edit when blocking
-- micro.InfoBar():Message("'make' done: ", out)
else
local out, err = shell.RunInteractiveShell("make", true, true)
end
return
end

Expand All @@ -86,8 +99,10 @@ function makeup()

end

function makeupCommand(bp)
bp:Save()
function makeupWrapper(bg)
-- makeupWrapper returns us to original working directory after running make
-- this must be used, because ctrl-S saving saves the current file in working directory
-- if bg is true, run 'make' in the background
micro.InfoBar():Message("makeup called")

-- pwd
Expand All @@ -99,13 +114,24 @@ function makeupCommand(bp)
micro.InfoBar():Message("Working directory is ", pwd)
local startDir = pwd

makeup()
makeup(bg)

-- 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

function makeupCommand(bp)
-- run make in this or any higher directory, show output
bp:Save()
makeupWrapper(false)
end

function makeupbgCommand(bp)
-- run make in this or higher directory, in the background, hide most output
bp:Save()
makeupWrapper(true)
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, F12 to make.",
"Description": "Press F5 to save and run, F12 to make, F9 to make in background.",
"Website": "https://github.com/terokarvinen/micro-run",
"Tags": ["run", "execute", "Go", "Python", "Bash", "Lua", "F5"],
"Versions": [
{
"Version": "0.0.6",
"Url": "https://github.com/terokarvinen/micro-run/archive/v0.0.6.zip",
"Require": {
"micro": ">=2.0.0"
}
},
{
"Version": "0.0.5",
"Url": "https://github.com/terokarvinen/micro-run/archive/v0.0.5.zip",
Expand Down

0 comments on commit 3392fed

Please sign in to comment.