1 ;;; ob-fortran.el --- org-babel functions for fortran
3 ;; Copyright (C) 2011-2014 Free Software Foundation, Inc.
5 ;; Authors: Sergey Litvinov
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/>.
27 ;; Org-Babel support for evaluating fortran code.
33 (declare-function org-entry-get
"org"
34 (pom property
&optional inherit literal-nil
))
35 (declare-function org-every
"org" (pred seq
))
36 (declare-function org-remove-indentation
"org" (code &optional n
))
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
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 (assoc :cmdline params
)))
52 (flags (cdr (assoc :flags params
)))
53 (full-body (org-babel-expand-body:fortran body params
))
56 (with-temp-file tmp-src-file
(insert full-body
))
58 (format "%s -o %s %s %s"
59 org-babel-fortran-compiler
60 (org-babel-process-file-name tmp-bin-file
)
62 (if (listp flags
) flags
(list flags
)) " ")
63 (org-babel-process-file-name tmp-src-file
)) ""))))
66 (org-remove-indentation
68 (concat tmp-bin-file
(if cmdline
(concat " " cmdline
) "")) "")))))
69 (org-babel-reassemble-table
70 (org-babel-result-cond (cdr (assoc :result-params params
))
71 (org-babel-read results
)
72 (let ((tmp-file (org-babel-temp-file "f-")))
73 (with-temp-file tmp-file
(insert results
))
74 (org-babel-import-elisp-from-file tmp-file
)))
76 (cdr (assoc :colname-names params
)) (cdr (assoc :colnames params
)))
78 (cdr (assoc :rowname-names params
)) (cdr (assoc :rownames params
)))))))
80 (defun org-babel-expand-body:fortran
(body params
)
81 "Expand a block of fortran or fortran code with org-babel according to
82 it's header arguments."
83 (let ((vars (mapcar #'cdr
(org-babel-get-header params
:var
)))
84 (main-p (not (string= (cdr (assoc :main params
)) "no")))
85 (includes (or (cdr (assoc :includes params
))
86 (org-babel-read (org-entry-get nil
"includes" t
))))
87 (defines (org-babel-read
88 (or (cdr (assoc :defines params
))
89 (org-babel-read (org-entry-get nil
"defines" t
))))))
94 (lambda (inc) (format "#include %s" inc
))
95 (if (listp includes
) includes
(list includes
)) "\n")
98 (lambda (inc) (format "#define %s" inc
))
99 (if (listp defines
) defines
(list defines
)) "\n")
102 (org-babel-fortran-ensure-main-wrap
105 (mapconcat 'org-babel-fortran-var-to-fortran vars
"\n")
109 (defun org-babel-fortran-ensure-main-wrap (body params
)
110 "Wrap body in a \"program ... end program\" block if none exists."
111 (if (string-match "^[ \t]*program[ \t]*.*" (capitalize body
))
112 (let ((vars (mapcar #'cdr
(org-babel-get-header params
:var
))))
113 (if vars
(error "Cannot use :vars if 'program' statement is present"))
115 (format "program main\n%s\nend program main\n" body
)))
117 (defun org-babel-prep-session:fortran
(session params
)
118 "This function does nothing as fortran is a compiled language with no
119 support for sessions"
120 (error "Fortran is a compiled languages -- no support for sessions"))
122 (defun org-babel-load-session:fortran
(session body params
)
123 "This function does nothing as fortran is a compiled language with no
124 support for sessions"
125 (error "Fortran is a compiled languages -- no support for sessions"))
129 (defun org-babel-fortran-var-to-fortran (pair)
130 "Convert an elisp val into a string of fortran code specifying a var
133 (let ((var (car pair
))
136 (setq val
(symbol-name val
))
137 (when (= (length val
) 1)
138 (setq val
(string-to-char val
))))
141 (format "integer, parameter :: %S = %S\n" var val
))
143 (format "real, parameter :: %S = %S\n" var val
))
145 (format "character, parameter :: %S = '%S'\n" var val
))
147 (format "character(len=%d), parameter :: %S = '%s'\n"
148 (length val
) var val
))
150 ((and (listp val
) (org-every #'listp val
))
151 (format "real, parameter :: %S(%d,%d) = transpose( reshape( %s , (/ %d, %d /) ) )\n"
152 var
(length val
) (length (car val
))
153 (org-babel-fortran-transform-list val
)
154 (length (car val
)) (length val
)))
156 (format "real, parameter :: %S(%d) = %s\n"
157 var
(length val
) (org-babel-fortran-transform-list val
)))
159 (error (format "the type of parameter %s is not supported by ob-fortran"
162 (defun org-babel-fortran-transform-list (val)
163 "Return a fortran representation of enclose syntactic lists."
165 (concat "(/" (mapconcat #'org-babel-fortran-transform-list val
", ") "/)")
168 (provide 'ob-fortran
)
170 ;;; ob-fortran.el ends here