ox-koma-letter: Tiny refactoring and cosmetics
[org-mode/org-tableheadings.git] / lisp / ob-fortran.el
blob5d175829cd0d8680cdc4f8714af8ccbe86135dee
1 ;;; ob-fortran.el --- Babel Functions for Fortran -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2011-2018 Free Software Foundation, Inc.
5 ;; Authors: Sergey Litvinov
6 ;; Eric Schulte
7 ;; Keywords: literate programming, reproducible research, fortran
8 ;; Homepage: https://orgmode.org
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;; Org-Babel support for evaluating fortran code.
29 ;;; Code:
30 (require 'ob)
31 (require 'org-macs)
32 (require 'cc-mode)
33 (require 'cl-lib)
35 (declare-function org-entry-get "org"
36 (pom property &optional inherit literal-nil))
38 (defvar org-babel-tangle-lang-exts)
39 (add-to-list 'org-babel-tangle-lang-exts '("fortran" . "F90"))
41 (defvar org-babel-default-header-args:fortran '())
43 (defvar org-babel-fortran-compiler "gfortran"
44 "fortran command used to compile a fortran source code file into an
45 executable.")
47 (defun org-babel-execute:fortran (body params)
48 "This function should only be called by `org-babel-execute:fortran'"
49 (let* ((tmp-src-file (org-babel-temp-file "fortran-src-" ".F90"))
50 (tmp-bin-file (org-babel-temp-file "fortran-bin-" org-babel-exeext))
51 (cmdline (cdr (assq :cmdline params)))
52 (flags (cdr (assq :flags params)))
53 (full-body (org-babel-expand-body:fortran body params)))
54 (with-temp-file tmp-src-file (insert full-body))
55 (org-babel-eval
56 (format "%s -o %s %s %s"
57 org-babel-fortran-compiler
58 (org-babel-process-file-name tmp-bin-file)
59 (mapconcat 'identity
60 (if (listp flags) flags (list flags)) " ")
61 (org-babel-process-file-name tmp-src-file)) "")
62 (let ((results
63 (org-trim
64 (org-remove-indentation
65 (org-babel-eval
66 (concat tmp-bin-file (if cmdline (concat " " cmdline) "")) "")))))
67 (org-babel-reassemble-table
68 (org-babel-result-cond (cdr (assq :result-params params))
69 (org-babel-read results)
70 (let ((tmp-file (org-babel-temp-file "f-")))
71 (with-temp-file tmp-file (insert results))
72 (org-babel-import-elisp-from-file tmp-file)))
73 (org-babel-pick-name
74 (cdr (assq :colname-names params)) (cdr (assq :colnames params)))
75 (org-babel-pick-name
76 (cdr (assq :rowname-names params)) (cdr (assq :rownames params)))))))
78 (defun org-babel-expand-body:fortran (body params)
79 "Expand a block of fortran or fortran code with org-babel according to
80 its header arguments."
81 (let ((vars (org-babel--get-vars params))
82 (main-p (not (string= (cdr (assq :main params)) "no")))
83 (includes (or (cdr (assq :includes params))
84 (org-babel-read (org-entry-get nil "includes" t))))
85 (defines (org-babel-read
86 (or (cdr (assq :defines params))
87 (org-babel-read (org-entry-get nil "defines" t))))))
88 (mapconcat 'identity
89 (list
90 ;; includes
91 (mapconcat
92 (lambda (inc) (format "#include %s" inc))
93 (if (listp includes) includes (list includes)) "\n")
94 ;; defines
95 (mapconcat
96 (lambda (inc) (format "#define %s" inc))
97 (if (listp defines) defines (list defines)) "\n")
98 ;; body
99 (if main-p
100 (org-babel-fortran-ensure-main-wrap
101 (concat
102 ;; variables
103 (mapconcat 'org-babel-fortran-var-to-fortran vars "\n")
104 body) params)
105 body) "\n") "\n")))
107 (defun org-babel-fortran-ensure-main-wrap (body params)
108 "Wrap body in a \"program ... end program\" block if none exists."
109 (if (string-match "^[ \t]*program[ \t]*.*" (capitalize body))
110 (let ((vars (org-babel--get-vars params)))
111 (when vars (error "Cannot use :vars if `program' statement is present"))
112 body)
113 (format "program main\n%s\nend program main\n" body)))
115 (defun org-babel-prep-session:fortran (_session _params)
116 "This function does nothing as fortran is a compiled language with no
117 support for sessions"
118 (error "Fortran is a compiled languages -- no support for sessions"))
120 (defun org-babel-load-session:fortran (_session _body _params)
121 "This function does nothing as fortran is a compiled language with no
122 support for sessions"
123 (error "Fortran is a compiled languages -- no support for sessions"))
125 ;; helper functions
127 (defun org-babel-fortran-var-to-fortran (pair)
128 "Convert an elisp val into a string of fortran code specifying a var
129 of the same value."
130 ;; TODO list support
131 (let ((var (car pair))
132 (val (cdr pair)))
133 (when (symbolp val)
134 (setq val (symbol-name val))
135 (when (= (length val) 1)
136 (setq val (string-to-char val))))
137 (cond
138 ((integerp val)
139 (format "integer, parameter :: %S = %S\n" var val))
140 ((floatp val)
141 (format "real, parameter :: %S = %S\n" var val))
142 ((or (integerp val))
143 (format "character, parameter :: %S = '%S'\n" var val))
144 ((stringp val)
145 (format "character(len=%d), parameter :: %S = '%s'\n"
146 (length val) var val))
147 ;; val is a matrix
148 ((and (listp val) (cl-every #'listp val))
149 (format "real, parameter :: %S(%d,%d) = transpose( reshape( %s , (/ %d, %d /) ) )\n"
150 var (length val) (length (car val))
151 (org-babel-fortran-transform-list val)
152 (length (car val)) (length val)))
153 ((listp val)
154 (format "real, parameter :: %S(%d) = %s\n"
155 var (length val) (org-babel-fortran-transform-list val)))
157 (error "the type of parameter %s is not supported by ob-fortran" var)))))
159 (defun org-babel-fortran-transform-list (val)
160 "Return a fortran representation of enclose syntactic lists."
161 (if (listp val)
162 (concat "(/" (mapconcat #'org-babel-fortran-transform-list val ", ") "/)")
163 (format "%S" val)))
165 (provide 'ob-fortran)
167 ;;; ob-fortran.el ends here