To install the script save the code to a file to this file/folder location:
Code: Select all
C:\Users\CurrentUser\AppData\Roaming\Xidicone\Zeus\xScript\d_dfmt.lua
Here is the code:
Code: Select all
--
-- Name: D Reformating Macro
--
-- Language: Lua Macro
--
-- Description: This macro will execute the dfmt.exe utility to reformat
-- the current file.
--
-- Project: https://github.com/Hackerpilot/dfmt
--
local utils = require "utils"
function key_macro()
-- details about the active document
local document, named, read_only, document_type = utils.document_details()
if (document_type ~= "D Document Type") or (document == 0) or (named == 0) then
-- only complain if this is not a trigger
if is_trigger() == 0 then
message("This macro only works with named, writable D documents.")
beep()
end
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("Running dfmt to format code....")
-------------------------------------------------------------------------
-- usage: dfmt [flags] [filename]
-------------------------------------------------------------------------
-- Options: --inplace : a file name is required and the file
-- will be edited in-place.
-- --braces=otbs : Use "The One True Brace Style", placing
-- open braces on the same line as the
-- previous token.
-- --braces=allman : Use "Allman Style", placing opening braces
-- on their own line. This is the default
-- --tabs : Use tabs for indentation instead of spaces
------------------------------------------------------------------------
local cmd_options = "--inplace --braces=allman "
-- get the tab size for the current document
-- see if real tabs are to be used
local use_tabs = macro_tag("$UseTabs")
if (use_tabs == "true") then
-- dfmt has no way to pass in the tab size via the command line
--local tab_size = macro_tag("$TabSize")
--cmd_options = cmd_options .. "--tabs " .. tab_size .. " "
-- add the tabs option
cmd_options = cmd_options .. "--tabs "
end
-- build up the final styling command line
local cmd = "dfmt.exe " .. cmd_options .. utils.checkLFN(file_name)
-- for debugging only
-- message_box(1, cmd, "Command Line")
-------------------------------------------------------------------------------
-- System Control Values
-------------------------------------------------------------------------------
-- 1 - save the document before running the program
-- 2 - capture any standard output generated by the program
-- 4 - capture any standard error generated by the program
-- 8 - ask for additional arguments
-- 16 - the program will use the MS-DOS command interpreter (ie. dir *.* etc)
-- 32 - wait for the program to complete (the ESC key will cancel the wait)
-- 64 - run the program in a visible DOS session (otherwise runs hidden)
-- 128 - capture output to a compler window (not the default tool window)
-------------------------------------------------------------------------------
-- Output Control Flags:
-------------------------------------------------------------------------------
-- 0 - ALWAYS
-- 1 - NEVER
-- 2 - ERRORS
-- 3 - WARNINGS
-- 4 - ANYOUTPUT
-------------------------------------------------------------------------------
-- display the tool window if any output is generated
local ANYOUTPUT = 4
-- run with 'save', 'capture output' and 'wait' and 'compiler' options
local flags = 1+2+4+32+128
local caption = "dfmt: " .. file_name
-- remember the current line count
local current_count = get_line_count()
if (system(cmd, dir, flags, caption, ANYOUTPUT) == 0) then
-- reload the newly formatted docuemnt
FileReloadCurrent()
-- get the new line count
local line_change = get_line_count() - current_count
-- maintain the current line location
if line_change ~= 0 then
local line_current = get_line_pos() + line_change
if line_current < 0 then line_current = 0 end
set_line_pos(line_current)
end
-- no errors detected
set_return_code(1)
-- some feedback
message("The formatting of the document is complete.")
else
-- formatting errors detected
set_return_code(0)
end
end
key_macro() -- run the macro