reformat and reindent fortran files

Post any comments, suggestions, annoyances or ideas for future releases here. Please do not post bug reports or questions here.
Post Reply
jpsam

reformat and reindent fortran files

Post by jpsam »

Hi,
I think I like the editor so far but I wonder whether it has the capability to perform reformating and indenting of fortran files. I presently use emacs to reformat and indent the files and use Zeus for code folding options.
for example:

subroutine sub()
integer:: k, n = 2
real( 8 ):: x, y, e=2
DO k = 1, n
x= x+k
y = y - e
ENDDO
end subroutine sub

would be reformatted and indented to:

subroutine sub()
  • integer:: k, n = 2
    real( 8 ):: x, y, e=2
  • DO k = 1, n
    • x= x+k
      y = y - e
    ENDDO
end subroutine sub

I will be glad to do everything within one editor like Zeus.

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

Post by jussij »

When it comes to code reformatting, it is easy to seamlessly integrate external formatters into Zeus. Here are at least two examples of how this is done: So provided you can find a Fortran code formatter utility it should easy enough to integrate into Zeus :)

Zeus itself has a very limited code reformatting capability. Basically all it can do is indent/un-indent any marked area using the Tab and Shift+Tab keys :(

But one other option is to do the reformatting as the code is entered. In this case there are a few more options :)

Option #1 It is possible to use the code templates feature to define your basic code constructs.

Option #2 Using a macro you can also make Zeus indent on the enter key. For example consider this Lua macro which when bound to the Enter key will indent on the subroutine, if and do keywords.

NOTE: This macro is nothing more than a quick two minute effort, and will most certainly still need some improvement. It is presented here only as an example of what is possible.

Code: Select all

--
--        Name: Fortran Enter Key Macro
--
--    Language: Lua Macro
--
-- Description: Does a special Fortran processing on the enter key.
--
function key_macro()
  -- check for read/write documents
  local locked = is_read_only()

  -- check for document windows
  local document = is_document()

  if (locked == 1) or (document == 0) then
    -- leave this case to the default enter function
    Enter()
  else
    -- get the current line
    local current_line = get_line_text()

    -- add the enter key
    Enter()

    local need_indent = false;

    -- see if line starts with 'subroutine'
    if (string.find(current_line, "subroutine") == 1) then
        need_indent = true;
    end

    -- see if line starts with 'do' allowinf white space
    if (string.find(current_line, "[%s]*do") == 1) then
        need_indent = true;
    end

    -- see if line starts with 'do' allowinf white space
    if (string.find(current_line, "[%s]*if") == 1) then
        need_indent = true;
    end

    -- add the indent if required
    if (need_indent == true) then
        TabChar()
    end
  end
end

key_macro() -- run the macro


Cheers Jussi
Post Reply