el/dot-emacs.el: Work-in-progress support for Fortran!
authorMark Wooding <mdw@distorted.org.uk>
Fri, 10 Jul 2020 19:45:22 +0000 (20:45 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Sun, 9 Aug 2020 03:34:42 +0000 (04:34 +0100)
el/dot-emacs.el

index 3692353..788569c 100644 (file)
@@ -3256,6 +3256,217 @@ strip numbers instead."
   (add-hook 'icon-mode-hook 'mdw-fontify-icon t))
 
 ;;;--------------------------------------------------------------------------
+;;; Fortran mode.
+
+(defun mdw-fontify-fortran-common ()
+  (let ((fortran-keywords
+        (mdw-regexps "access"
+                     "assign"
+                     "associate"
+                     "backspace"
+                     "blank"
+                     "block\\s-*data"
+                     "call"
+                     "case"
+                     "character"
+                     "class"
+                     "close"
+                     "common"
+                     "complex"
+                     "continue"
+                     "critical"
+                     "data"
+                     "dimension"
+                     "do"
+                     "double\\s-*precision"
+                     "else" "elseif" "elsewhere"
+                     "end"
+                       "endblock" "endblockdata"
+                       "endcritical"
+                       "enddo"
+                       "endinterface"
+                       "endmodule"
+                       "endprocedure"
+                       "endprogram"
+                       "endselect"
+                       "endsubmodule"
+                       "endsubroutine"
+                       "endtype"
+                       "endwhere"
+                       "endenum"
+                       "end\\s-*file"
+                       "endforall"
+                       "endfunction"
+                       "endif"
+                     "entry"
+                     "enum"
+                     "equivalence"
+                     "err"
+                     "external"
+                     "file"
+                     "fmt"
+                     "forall"
+                     "form"
+                     "format"
+                     "function"
+                     "go\\s-*to"
+                     "if"
+                     "implicit"
+                     "in" "inout"
+                     "inquire"
+                     "include"
+                     "integer"
+                     "interface"
+                     "intrinsic"
+                     "iostat"
+                     "len"
+                     "logical"
+                     "module"
+                     "open"
+                     "out"
+                     "parameter"
+                     "pause"
+                     "procedure"
+                     "program"
+                     "precision"
+                     "program"
+                     "read"
+                     "real"
+                     "rec"
+                     "recl"
+                     "return"
+                     "rewind"
+                     "save"
+                     "select" "selectcase" "selecttype"
+                     "status"
+                     "stop"
+                     "submodule"
+                     "subroutine"
+                     "then"
+                     "to"
+                     "type"
+                     "unit"
+                     "where"
+                     "write"))
+       (fortran-operators (mdw-regexps "and"
+                                       "eq"
+                                       "eqv"
+                                       "false"
+                                       "ge"
+                                       "gt"
+                                       "le"
+                                       "lt"
+                                       "ne"
+                                       "neqv"
+                                       "not"
+                                       "or"
+                                       "true"))
+       (fortran-intrinsics (mdw-regexps "abs" "dabs" "iabs" "cabs"
+                                        "atan" "datan" "atan2" "datan2"
+                                        "cmplx"
+                                        "conjg"
+                                        "cos" "dcos" "ccos"
+                                        "dble"
+                                        "dim" "idim"
+                                        "exp" "dexp" "cexp"
+                                        "float"
+                                        "ifix"
+                                        "aimag"
+                                        "int" "aint" "idint"
+                                        "alog" "dlog" "clog"
+                                        "alog10" "dlog10"
+                                        "max"
+                                        "amax0" "amax1"
+                                        "max0" "max1"
+                                        "dmax1"
+                                        "min"
+                                        "amin0" "amin1"
+                                        "min0" "min1"
+                                        "dmin1"
+                                        "mod" "amod" "dmod"
+                                        "sin" "dsin" "csin"
+                                        "sign" "isign" "dsign"
+                                        "sngl"
+                                        "sqrt" "dsqrt" "csqrt"
+                                        "tanh"))
+       (preprocessor-keywords
+        (mdw-regexps "assert" "define" "elif" "else" "endif" "error"
+                     "ident" "if" "ifdef" "ifndef" "import" "include"
+                     "line" "pragma" "unassert" "undef" "warning")))
+    (setq font-lock-keywords-case-fold-search t
+           font-lock-keywords
+           (list
+
+            ;; Fontify include files as strings.
+            (list (concat "^[ \t]*\\#[ \t]*" "include"
+                          "[ \t]*\\(<[^>]+>?\\)")
+                  '(1 font-lock-string-face))
+
+            ;; Preprocessor directives are `references'?.
+            (list (concat "^\\([ \t]*#[ \t]*\\(\\("
+                          preprocessor-keywords
+                          "\\)\\>\\|[0-9]+\\|$\\)\\)")
+                  '(1 font-lock-keyword-face))
+
+            ;; Set up the keywords defined above.
+            (list (concat "\\<\\(" fortran-keywords "\\)\\>")
+                  '(0 font-lock-keyword-face))
+
+            ;; Set up the `.foo.' operators.
+            (list (concat "\\.\\(" fortran-operators "\\)\\.")
+                  '(0 font-lock-keyword-face))
+
+            ;; Set up the intrinsic functions.
+            (list (concat "\\<\\(" fortran-intrinsics "\\)\\>")
+                  '(0 font-lock-variable-name-face))
+
+            ;; Numbers.
+            (list (concat       "\\(" "\\<" "[0-9]+" "\\(\\.[0-9]*\\)?"
+                                "\\|" "\\.[0-9]+"
+                                "\\)"
+                                "\\(" "[de]" "[+-]?" "[0-9]+" "\\)?"
+                                "\\(" "_" "\\sw+" "\\)?"
+                          "\\|" "b'[01]*'" "\\|" "'[01]*'b"
+                          "\\|" "b\"[01]*\"" "\\|" "\"[01]*\"b"
+                          "\\|" "o'[0-7]*'" "\\|" "'[0-7]*'o"
+                          "\\|" "o\"[0-7]*\"" "\\|" "\"[0-7]*\"o"
+                          "\\|" "[xz]'[0-9a-f]*'" "\\|" "'[0-9a-f]*'[xz]"
+                          "\\|" "[xz]\"[0-9a-f]*\"" "\\|" "\"[0-9a-f]*\"[xz]")
+                  '(0 mdw-number-face))
+
+            ;; Any anything else is punctuation.
+            (list "\\(\\s.\\|\\s(\\|\\s)\\|\\s\\\\|\\s/\\)"
+                  '(0 mdw-punct-face))))
+
+    (modify-syntax-entry ?/ "." font-lock-syntax-table)
+    (modify-syntax-entry ?< ".")
+    (modify-syntax-entry ?> ".")))
+
+(defun mdw-fontify-fortran () (mdw-fontify-fortran-common))
+(defun mdw-fontify-f90 () (mdw-fontify-fortran-common))
+
+(setq fortran-do-indent 2
+      fortran-if-indent 2
+      fortran-structure-indent 2
+      fortran-comment-line-start "*"
+      fortran-comment-indent-style 'relative
+      fortran-continuation-string "&"
+      fortran-continuation-indent 4)
+
+(setq f90-do-indent 2
+      f90-if-indent 2
+      f90-program-indent 2
+      f90-continuation-indent 4
+      f90-smart-end-names nil
+      f90-smart-end 'no-blink)
+
+(progn
+  (add-hook 'fortran-mode-hook 'mdw-misc-mode-config t)
+  (add-hook 'fortran-mode-hook 'mdw-fontify-fortran t)
+  (add-hook 'f90-mode-hook 'mdw-misc-mode-config t)
+  (add-hook 'f90-mode-hook 'mdw-fontify-f90 t))
+
+;;;--------------------------------------------------------------------------
 ;;; Assembler mode.
 
 (defun mdw-fontify-asm ()