Use Org duration library
[org-mode/org-tableheadings.git] / lisp / ob-fortran.el
blobd059245b30cc63ab3a143cb7dca63b2a963bedf2
1 ;;; ob-fortran.el --- Babel Functions for Fortran -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2011-2017 Free Software Foundation, Inc.
5 ;; Authors: Sergey Litvinov
6 ;; Eric Schulte
7 ;; Keywords: literate programming, reproducible research, fortran
8 ;; Homepage: http://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 <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;; Org-Babel support for evaluating fortran code.
29 ;;; Code:
30 (require 'ob)
31 (require 'cc-mode)
32 (require 'cl-lib)
34 (declare-function org-entry-get "org"
35 (pom property &optional inherit literal-nil))
36 (declare-function org-remove-indentation "org" (code &optional n))
37 (declare-function org-trim "org" (s &optional keep-lead))
39 (defvar org-babel-tangle-lang-exts)
40 (add-to-list 'org-babel-tangle-lang-exts '("fortran" . "F90"))
42 (defvar org-babel-default-header-args:fortran '())
44 (defvar org-babel-fortran-compiler "gfortran"
45 "fortran command used to compile a fortran source code file into an
46 executable.")
48 (defun org-babel-execute:fortran (body params)
49 "This function should only be called by `org-babel-execute:fortran'"
50 (let* ((tmp-src-file (org-babel-temp-file "fortran-src-" ".F90"))
51 (tmp-bin-file (org-babel-temp-file "fortran-bin-" org-babel-exeext))
52 (cmdline (cdr (assq :cmdline params)))
53 (flags (cdr (assq :flags params)))
54 (full-body (org-babel-expand-body:fortran body params)))
55 (with-temp-file tmp-src-file (insert full-body))
56 (org-babel-eval
57 (format "%s -o %s %s %s"
58 org-babel-fortran-compiler
59 (org-babel-process-file-name tmp-bin-file)
60 (mapconcat 'identity
61 (if (listp flags) flags (list flags)) " ")
62 (org-babel-process-file-name tmp-src-file)) "")
63 (let ((results
64 (org-trim
65 (org-remove-indentation
66 (org-babel-eval
67 (concat tmp-bin-file (if cmdline (concat " " cmdline) "")) "")))))
68 (org-babel-reassemble-table
69 (org-babel-result-cond (cdr (assq :result-params params))
70 (org-babel-read results)
71 (let ((tmp-file (org-babel-temp-file "f-")))
72 (with-temp-file tmp-file (insert results))
73 (org-babel-import-elisp-from-file tmp-file)))
74 (org-babel-pick-name
75 (cdr (assq :colname-names params)) (cdr (assq :colnames params)))
76 (org-babel-pick-name
77 (cdr (assq :rowname-names params)) (cdr (assq :rownames params)))))))
79 (defun org-babel-expand-body:fortran (body params)
80 "Expand a block of fortran or fortran code with org-babel according to
81 its header arguments."
82 (let ((vars (org-babel--get-vars params))
83 (main-p (not (string= (cdr (assq :main params)) "no")))
84 (includes (or (cdr (assq :includes params))
85 (org-babel-read (org-entry-get nil "includes" t))))
86 (defines (org-babel-read
87 (or (cdr (assq :defines params))
88 (org-babel-read (org-entry-get nil "defines" t))))))
89 (mapconcat 'identity
90 (list
91 ;; includes
92 (mapconcat
93 (lambda (inc) (format "#include %s" inc))
94 (if (listp includes) includes (list includes)) "\n")
95 ;; defines
96 (mapconcat
97 (lambda (inc) (format "#define %s" inc))
98 (if (listp defines) defines (list defines)) "\n")
99 ;; body
100 (if main-p
101 (org-babel-fortran-ensure-main-wrap
102 (concat
103 ;; variables
104 (mapconcat 'org-babel-fortran-var-to-fortran vars "\n")
105 body) params)
106 body) "\n") "\n")))
108 (defun org-babel-fortran-ensure-main-wrap (body params)
109 "Wrap body in a \"program ... end program\" block if none exists."
110 (if (string-match "^[ \t]*program[ \t]*.*" (capitalize body))
111 (let ((vars (org-babel--get-vars params)))
112 (if vars (error "Cannot use :vars if `program' statement is present"))
113 body)
114 (format "program main\n%s\nend program main\n" body)))
116 (defun org-babel-prep-session:fortran (_session _params)
117 "This function does nothing as fortran is a compiled language with no
118 support for sessions"
119 (error "Fortran is a compiled languages -- no support for sessions"))
121 (defun org-babel-load-session:fortran (_session _body _params)
122 "This function does nothing as fortran is a compiled language with no
123 support for sessions"
124 (error "Fortran is a compiled languages -- no support for sessions"))
126 ;; helper functions
128 (defun org-babel-fortran-var-to-fortran (pair)
129 "Convert an elisp val into a string of fortran code specifying a var
130 of the same value."
131 ;; TODO list support
132 (let ((var (car pair))
133 (val (cdr pair)))
134 (when (symbolp val)
135 (setq val (symbol-name val))
136 (when (= (length val) 1)
137 (setq val (string-to-char val))))
138 (cond
139 ((integerp val)
140 (format "integer, parameter :: %S = %S\n" var val))
141 ((floatp val)
142 (format "real, parameter :: %S = %S\n" var val))
143 ((or (integerp val))
144 (format "character, parameter :: %S = '%S'\n" var val))
145 ((stringp val)
146 (format "character(len=%d), parameter :: %S = '%s'\n"
147 (length val) var val))
148 ;; val is a matrix
149 ((and (listp val) (cl-every #'listp val))
150 (format "real, parameter :: %S(%d,%d) = transpose( reshape( %s , (/ %d, %d /) ) )\n"
151 var (length val) (length (car val))
152 (org-babel-fortran-transform-list val)
153 (length (car val)) (length val)))
154 ((listp val)
155 (format "real, parameter :: %S(%d) = %s\n"
156 var (length val) (org-babel-fortran-transform-list val)))
158 (error "the type of parameter %s is not supported by ob-fortran" var)))))
160 (defun org-babel-fortran-transform-list (val)
161 "Return a fortran representation of enclose syntactic lists."
162 (if (listp val)
163 (concat "(/" (mapconcat #'org-babel-fortran-transform-list val ", ") "/)")
164 (format "%S" val)))
166 (provide 'ob-fortran)
168 ;;; ob-fortran.el ends here