Fully-featured Fortran setup for Linux/Mac in Emacs (Intel Fortran support included!)

Hi everyone, we here from the Doom Emacs project recently completed its brand-new Fortran Module. Out of the box, it automatically supports:

  1. Integration with fpm.
  2. LSP support via fortls.
  3. Auto-formatting via fprettier.
  4. (Optional) Intel Fortran! (Otherwise gfortran is the default).

This should provide a fully-featured, modern Fortran setup for anyone on Linux or MacOS. The Intel support is especially important, given how much of a pain its installation / editor integration has been outside of the classic Windows + Visual Studio setup.

If you try out this module and have any issues, please let me know! I’m also regularly available on Doom’s Discourse or its Discord.

Happy computing!

24 Likes

Welcome to the community @fosskers and also nice seeing fortls being part of the mix!

2 Likes

Small question. Does this work on vanilla Emacs as well or just Doom Emacs ?

1 Like

Thanks for sharing. This is great. Does this also work fully in Microsoft WSL? I just installed it and it seems like it works, but I am not sure if it has all the bells and whistles it gets in the native Unix environment.

There are Fortran modes for F90 and legacy Fortran built in to Emacs natively, and this module uses those and expands on them. The “module” however, is Doom-specific, so requires Doom to be installed and configured. Note that Doom is just Emacs with a lot of packages/settings pre-configured for convenience.

1 Like

I don’t see why not? So long as you get Doom installed, it should work.

1 Like

Welcome @fosskers!
I was about to ask the same question as @Aurelius_Nero just did. Well, I guess it’s time to give Doom Emacs a try - although I must admit I avoided it so far because lots of packages usually means larger memory footprint - and I’m still using some legacy computers with vanilla Emacs somewhat stripped.

1 Like

Doom requires at least Emacs 27, but since Emacs 28 is out now, you might as well use that. It features the new Elisp “native compilation” feature which really speeds up Emacs itself. As for memory footprint… my emacs process is currently sitting at 2.7% RAM. For comparison, Firefox is over 10%.

Thanks for this module! I’m a complete emacs (doom or otherwise) noob and I can’t seem to get the module to automatically recognize .F90 files extensions. By contrast I don’t have an issue with other :lang modules (e.g. julia). Aside from updating my .doom.d/init.el file to use (fortran +lsp) (and making sure that lsp is also enabled in :tools), is there something else that needs to be enabled?

Hi @glemieux, welcome to the Fortraners Discourse.

By default, Emacs uses Fortran mode for files with several specific extensions (have a look there.) The list of recognized extensions doesn’t include .F90, but you can easily set this (or any other extension you like) in your init.el. For example, Add this:

(add-to-list 'auto-mode-alist '("\\.foo\\'" . f90-mode))

to make Emacs activate Fortran 90 mode for *.foo files. I am using “vanilla” Emacs (with some packages from Melpa) but I will be surprised if this doesn’t work in Doom Emacs as well.

2 Likes

Just to avoid some confusion, emacs has two different fortran modes, fortran-mode for fixed-form and f90-mode for free-form source. They are basically treated like different languages, with different indent options, and so on.

You are right, Ron (I’ve set up my Emacs years ago, and I forgot about that.) I corrected my post accordingly.

Hi @glemieux , the use of Doom’s fortran means you shouldn’t have to manually set anything yourself as mentioned by @Pap above. If you’ve added any such lines to your config, please remove them.

Could you describe the exact issue you’re facing? While a long shot, does anything change if you downcase the characters of the file extension to .f90?

1 Like

@fosskers Sure enough, downcasing the extensions worked! Is this something that is relatively straightfoward to account for in the module config.el file? I’m new to emacs lisp as well.

The instructions for that are given above a few posts up. I put these commands in my .emacs file in my home directory. The above instructions specify a init.el file. I don’t know the difference or which is best. Maybe it is OS dependent?

The de facto standard is that upper case .F90 means that compiler should run the preprocessor, lower case .f90 means that it shouldn’t. So as far as emacs is concerned, both extensions should trigger the f90-mode. One can also manually specify the mode with the M-x f90-mode command, but you don’t want to have to do that if you are working on dozens or hundreds of files.

It is not OS-dependent, although it makes perfect sense to guess it is. In relatively recent versions of Emacs, the standard configuration file is considered to be ~/.emacs.d/init.el. However the traditional ~/.emacs file still works. You can leave it as it is, or you can move it to ~/.emacs.d/ and rename it to init.el.

1 Like

I will fix this upstream.

Done: fix(fortran): account for the `.F90` extension by fosskers · Pull Request #7110 · doomemacs/doomemacs · GitHub

The fix is already merged, so if you update your Doom (assuming you’re using Doom), then you shouldn’t have to worry about the extension case anymore.

2 Likes

Thanks @fosskers! That did the trick.

1 Like

I am currently trying to use doom emacs in org mode to write fortran and have ifort installed.

I have the package installed by enabling (fortran +lsp +intel).

But when trying to compile the source blocks i get an error telling me that gfortran is missing.
my doom emacs version is 042fe0c438
and ifort version is 2021.8.0

Thanks for reporting; sorry I didn’t notice right away.

Can you tell me the output of doom doctor?