Skip to content

jmriff/format-fortran

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Fortran-lang community, Public domain, via Wikimedia Commons

fpretty: A Modern Fortran Pretty-Printer

A lightweight Fortran pretty-printer (indenter) for free-form source code.

About

fpretty is a simple command-line tool that automatically indents modern, free-form Fortran code. It reads from standard input or a file and writes the formatted code to standard output.

Prerequisites

fpretty is built using the Fortran Package Manager (fpm). You'll need to have fpm installed to build the project.

Installation

  1. Clone the repository:

    git clone https://github.com/jmriff/format-fortran.git
    cd fpretty
  2. Build and install with fpm:

    fpm install

Usage

fpretty can be used in several ways:

  • From standard input:

    cat my_fortran_file.f90 | fpretty
  • With a single file:

    fpretty my_fortran_file.f90

    The formatted output will be printed to the console. To save it to a new file:

    fpretty my_fortran_file.f90 > formatted_file.f90
  • To overwrite a file in-place (use with caution):

    fpretty my_fortran_file.f90 > temp.f90 && mv temp.f90 my_fortran_file.f90

Editor Integration

Vim / Neovim

You can configure fpretty to run automatically whenever you save a Fortran file in Vim or Neovim. Add the following to your .vimrc or init.vim:

" Run fpretty on Fortran files automatically on save
function! FprettyFormatAndWrite()
  let l:curpos = getpos('.')
  let l:winview = winsaveview()
  silent %!fpretty
  call winrestview(l:winview)
  call setpos('.', l:curpos)
  write!
  edit!
endfunction

augroup fpretty_autocmd
  autocmd!
  autocmd BufWriteCmd *.f,*.f90,*.for,*.F,*.F90,*.FOR call FprettyFormatAndWrite()
augroup END

Limitations

  • Not a full parser: fpretty is based on heuristics and may not correctly handle all complex one-line constructs, preprocessor directives, or fixed-form source code.
  • It is designed for free-form Fortran source only.

Contributing

Feedback, recommendations, issues, and code contributions are welcome! Please feel free to open an issue or submit a pull request.

About

Pretty print and format modern Fortran code. Written in Fortran.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors