Reformat and Reindent C/C++ or Java Source Code

Find Tips and tricks on how to better use the Zeus IDE. Feel free to post your own tips but please do not post bug reports, feature requests or questions here.
Post Reply
jussij
Site Admin
Posts: 2650
Joined: Fri Aug 13, 2004 5:10 pm

Reformat and Reindent C/C++ or Java Source Code

Post by jussij »

The Artistic Style is a reindenter and reformatter of C/C++ and Java source code.

The macro below allows you to seamless reformat you current document from within Zeus:

Code: Select all

--
--        Name: Astyle Code Reformating Macro
--
--    Language: Lua Macro
--
-- Description: This macro will excute the astyle.exe utility to reformat
--              the current file. For more details on how to use astyle.exe
--              visit the following web page:
--
--               http://guinness.cs.stevens-tech.edu/packages/astyle/astyle.html
--
--              NOTE: For this macro to work the astyle.exe must be in the
--                    system path.
--

function key_macro()
  -- 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 astyle.exe on current document
    message("This macro only works for named document files!")
    beep()
  else
    -- the directory of the current document
    local dir = macro_tag("$fdd")

    -- the name of the current document
    local file_name = macro_tag("$f")

    -- build uup the command line for the required styling
--    local cmd = "astyle.exe --style=java" .. " " .. file_name
    local cmd = "astyle.exe --style=ansi" .. " " .. file_name

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

    -- run command using the 'save' and 'wait for complete' options
    local flags = 1+32

    -- run the astyle.exe command
    if system(cmd, dir, flags) == 0 then
      -- reload the newly styled docuemnt
      FileReloadCurrent()
    else
      -- had problems running the command
      local error_msg = cmd .. "\n\n" .. "Error executing astyle.exe command!"
      message_box(1, error_msg)
    end
  end
end

key_macro() -- run the macro
To use this macro you need to download the astyle.exe making sure to put the file in the system PATH.

To run the macro, save the above source to zScript/astyle.lua file, open a C/C++ or Java test file, use the Macros, Execute Script menu and type in [astyle.lua] and you should see the code reformatted.

Jussi
jussij
Site Admin
Posts: 2650
Joined: Fri Aug 13, 2004 5:10 pm

Post by jussij »

The Jindent is another tool that can do Java source code reformatting. I also comes with a command line version, so it should be easy enough to interface into the Zeus tools menu :)

Jussi
Post Reply