Artistic Style Code Refomatting

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

Artistic Style Code Refomatting

Post by jussij »

The macro below provides a method of performing code reformatting from inside the editro, by interfacing Zeus with the Artistic Style utility.

Simply highlight the text to be reformatted and run the macro. The highlighted text will be saved to a temp file, reformatted by Artistic Style and the reformatted text re-inserted back into the document.

The style of the reformatting can be configured to taste by changing the Artistic Style command line options used in the macro.

NOTE: Be careful when using this macro since there is only one level of undo. To undo the changes made bu the formatting utility you will need to reload the original file format from the backup copy that was created by the macro before it ran the fromatting. Refer to the source code below for more details.

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://astyle.sourceforge.net/astyle.html
--

function key_macro()
  -- dialog box caption
  local caption = "Astyle Utility"

  -- 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 can only be run with 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")

  -- the name of the backup document
  local file_backup = file_name .. ".orig"

  local message1 = "Astyle is ready to re-style the current file, while the following backup\nfile will also be created:\n\n         '" .. file_backup .."'\n"
  local message2 = "NOTE: Astyle is specifically designed to work with C, C++ and Java\nfiles, so if the styling does not work as expected use backup file to\nundo the changes made.\n\nDo you want to continue with the styling?"

  local message_text = message1 .. "\n" .. message2

  -- run astyle.exe on current document
  local result = 6; --message_box(4, message_text, caption)

  -- check for the IDYES return value
  if (result == 6) then
      message("Astyle is formatting the code....")
  else
      message("Operation canceled by user.")
      return
  end

  -- ===================================================================
  -- Predefined Styling options:
  -- ===================================================================
  --     --style=ansi
  --     ANSI style formatting/indenting.
  --
  --     --style=kr
  --     Kernighan&Ritchie style formatting/indenting.
  --
  --     --style=gnu
  --     GNU style formatting/indenting.
  --
  --     --style=java
  --     Java mode, with standard java style formatting/indenting.
  --
  --     --style=linux
  --     Linux mode (i.e. 8 spaces per indent, break definition-block
  --     brackets but attach command-block brackets.
  --
  --     --break-blocks  OR  -f
  --     Insert empty lines around unrelated blocks, labels, classes, ...
  --
  --     --break-blocks=all  OR  -F
  --     Like --break-blocks, except also insert empty lines
  --     around closing headers (e.g. 'else', 'catch', ...).
  --
  --     --break-closing-brackets  OR  -y
  --     Break brackets before closing headers (e.g. 'else', 'catch', ...).
  --
  --     --pad-oper  OR  -p
  --     Insert space paddings around operators.
  --
  --     --pad-paren  OR  -P
  --     Insert space padding around parenthesis on both the outside
  --     and the inside.
  --
  --     --pad-paren-out  OR  -d
  --     Insert space padding around parenthesis on the outside only.
  --
  --     --pad-paren-in  OR  -D
  --     Insert space padding around parenthesis on the inside only.
  --
  --     --pad-header  OR  -H
  --     Insert space padding after paren headers (e.g. 'if', 'for'...).
  --
  --     --indent-namespaces  OR  -N
  --     Indent the contents of namespace blocks.
  --
  --     --indent-switches  OR  -S
  --     Indent 'switch' blocks, so that the inner 'case XXX:'
  --     headers are indented in relation to the switch block.
  --
  --     --indent-cases  OR  -K
  --     Indent case blocks from the 'case XXX:' headers.
  --     Case statements not enclosed in blocks are NOT indented.
  --
  --     --unpad-paren  OR  -U
  --     Remove unnecessary space padding around parenthesis.  This
  --     can be used in combination with the 'pad' options above.
  --     --keep-one-line-blocks  OR  -O
  --     Don't break blocks residing completely on one line.
  --
  --     --keep-one-line-statements  OR  -o
  --     Don't break lines containing multiple statements into
  --     multiple single-statement lines.
  --
  --     --align-pointer=type    OR  -k1
  --     --align-pointer=middle  OR  -k2
  --     --align-pointer=name    OR  -k3
  --     Attach a pointer or reference operator (* or &) to either
  --     the operator type (left), middle, or operator name (right).
  --
  --     --convert-tabs  OR  -c
  --     Convert tabs to the appropriate number of spaces.
  --
  -- ===================================================================

  -- padding command line options (see above)
  --local cmd_padding = "--pad-oper --pad-paren-out --unpad-paren "
  local cmd_padding = "--pad-oper " --pad-paren-out "

  -- indent command line options (see above)
  local cmd_indent  = "--indent-namespaces --indent-switches --indent-col1-comments --indent-preprocessor "

  -- break command line options (see above)
  local cmd_break = "--break-blocks --break-closing-brackets "

  -- other command line options (see above)
  local cmd_other  = "--align-pointer=name --keep-one-line-blocks --keep-one-line-statements --max-instatement-indent=79 "

  -- build up the full command line option (see above)
  local cmd_options = "--style=ansi " .. cmd_padding .. cmd_indent .. cmd_other .. cmd_break

  -- get the tab size for the current document
  local tab_size = 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 .. " " .. "--indent=tab=" .. tab_size
  else
    -- add the spaces option
    cmd_options = cmd_options .. " " .. "--indent=spaces=" .. tab_size .. "--convert-tabs"
  end

  -- build up the final styling command line
  local cmd = "astyle.exe" .. " " ..  cmd_options  .. " \"" .. file_name .. "\""

  -- for debugging only
  --message_box(1, cmd, "Command Line", caption)

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

    -- get the current window id
    local id = get_window_id()

    -- build the name of the backup file
    local backup_file = dir .. file_backup

    -- load the backup file as well
    if file_open(backup_file) == 1 then
      -- re-activate the reformated file
      window_activate(id)
    end

    -- some feedback
    message("The styling of the document is now complete.")
  else
    -- had problems running the command
    local error_msg = cmd .. "\n\n" .. "Error executing the 'astyle.exe' command!"
    message_box(1, error_msg, caption)
  end
end

key_macro() -- run the macro
Jussi
Post Reply