NOTE: For users new to Zeus this script may have already been copied over by the latest Zeus installer, so before trying to create this new script, first attempt to see if the script already exists. If the script does already exists, just do the Trigger configuration change as per the instructions below.
Installation Steps
To use this macro first save the macro code to the go_fmt_on_save_postfix_event.lua in the zScript folder.
Then use the Options, Editor Options menu, switch to the Triggers panel and bind that macro postfix file save trigger entry field.
Code: Select all
--
-- Name: Go Reformating Macro - Trigger
--
-- Language: Lua Macro
--
-- Description: This macro is a posfix file save event trigger that runs
-- the gofmt.exe each time the file is saved. To use the macro
-- use the Options, Editor Options menu and add the macro to
-- the postfix file save entry field of the Triggers panel.
--
-- IMPORTANT: For the macro to work it is assumed the go.exe can be found
-- in the system path.
--
function key_macro()
-- get the document type for the current file
document_type = macro_tag("$DTD")
-- look for the Go document type
if (document_type == "Go Document Type") then
-- macro only works for documents
local document = is_document()
-- macro only works for read/write documents.
local locked = is_read_only()
-- macro only works for named documents.
local named = is_named()
if (locked == 1) or (document == 0) or (named == 0) then
-- can't run command on current document
message("This macro can only be run for a named, writable document file.")
beep()
return
end
-- the directory of the current document
local dir = macro_tag("$fdd")
-- the name of the current document
local file_name = macro_tag("$f")
message("Go is formatting the code....")
-------------------------------------------------------------------------
-- usage: gofmt [flags] [path ...]
-------------------------------------------------------------------------
-- -comments=true : print comments
-- -cpuprofile="" : write cpu profile to this file
-- -d=false : display diffs instead of rewriting files
-- -e=false : print all (including spurious) errors
-- -l=false : list files whose formatting differs from gofmt's
-- -r="" : rewrite rule (e.g., 'a[b:len(a)] -> a[b:]')
-- -s=false : simplify code
-- -tabs=true : indent with tabs
-- -tabwidth=8 : tab width
-- -w=false : write result to (source) file instead of stdout
-------------------------------------------------------------------------
local cmd_options = "-comments=true -w=true "
-- get the tab size for the current document
local tab_width = "-tabwidth=" .. macro_tag("$TabSize")
-- see if real tabs are to be used
local use_tabs = macro_tag("$UseTabs")
if (use_tabs == "true") then
-- add the tabs option
cmd_options = cmd_options .. " " .. "-tabs=true" .. " " .. tab_width
else
-- add the spaces option
cmd_options = cmd_options .. " " .. "-tabs=false" .. " " .. tab_width
end
-- build up the final styling command line
local cmd = "gofmt.exe" .. " " .. cmd_options .. " \"" .. file_name .. "\""
-- run command using the 'save' and 'wait for complete' options
local flags = 1+32
-- run the command
if (system(cmd, dir, flags) == 0) then
-- reload the newly formatted docuemnt
FileReloadCurrent()
-- some feedback
message("The formatting of the document is complete.")
else
-- display an error in the debug window
debug_enable()
debug_output(cmd .. "\n\n" .. "Error executing the 'Go Format' command!")
debug_output("\nMake sure the Go.exe is in the current PATH environment variable.\n")
-- set capture flags
flags = 1+32+2+4
-- run the format again but this time capture the error output
system(cmd, dir, flags)
end
end
end
key_macro() -- run the macro