I set up M-x compile to automatically run appropriate commands depend on on the file types. For example, for C++

(add-hook 'c++-mode-hook
          (lambda ()
            (set (make-local-variable 'compile-command)
                 (concat "g++ " buffer-file-name " && ./a.out"  ) )))

So everytime I have a .cpp file, it would run g++ filename && ./a.out.

For Fortran, I have the same thing:

(add-hook 'fortran-mode-hook
          (lambda ()
            (set (make-local-variable 'compile-command)
                 (concat "gfortran " buffer-file-name " && ./a.out"  ) )))

But this only works if the file extension is .f, .for. It does not work for file extensions .f95, .f90.....

Please let me know how to fix this.


fortran-mode-hook only runs for buffers that end up in fortran-mode. To affect which extensions are recognized as fortran, you can modify auto-mode-alist, eg

(add-to-list 'auto-mode-alist '("\\.f9[50]" . fortran-mode))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.