* etc/publicsuffix.txt: Update from source.
[bpt/emacs.git] / lisp / org / ob-fortran.el
CommitLineData
e66ba1df
BG
1;;; ob-fortran.el --- org-babel functions for fortran
2
ba318903 3;; Copyright (C) 2011-2014 Free Software Foundation, Inc.
e66ba1df 4
dfd98937
BG
5;; Authors: Sergey Litvinov
6;; Eric Schulte
e66ba1df
BG
7;; Keywords: literate programming, reproducible research, fortran
8;; Homepage: http://orgmode.org
e66ba1df 9
74bfe42a 10;; This file is part of GNU Emacs.
8223b1d2 11;;
74bfe42a 12;; GNU Emacs is free software: you can redistribute it and/or modify
e66ba1df 13;; it under the terms of the GNU General Public License as published by
74bfe42a
GM
14;; the Free Software Foundation, either version 3 of the License, or
15;; (at your option) any later version.
16
17;; GNU Emacs is distributed in the hope that it will be useful,
e66ba1df
BG
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.
74bfe42a 21
e66ba1df 22;; You should have received a copy of the GNU General Public License
74bfe42a 23;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
e66ba1df
BG
24
25;;; Commentary:
26
27;; Org-Babel support for evaluating fortran code.
28
29;;; Code:
30(require 'ob)
e66ba1df
BG
31(require 'cc-mode)
32
33(declare-function org-entry-get "org"
34 (pom property &optional inherit literal-nil))
271672fa 35(declare-function org-every "org" (pred seq))
e66ba1df
BG
36
37(defvar org-babel-tangle-lang-exts)
38(add-to-list 'org-babel-tangle-lang-exts '("fortran" . "F90"))
39
40(defvar org-babel-default-header-args:fortran '())
41
42(defvar org-babel-fortran-compiler "gfortran"
43 "fortran command used to compile a fortran source code file into an
44 executable.")
45
46(defun org-babel-execute:fortran (body params)
47 "This function should only be called by `org-babel-execute:fortran'"
48 (let* ((tmp-src-file (org-babel-temp-file "fortran-src-" ".F90"))
8223b1d2 49 (tmp-bin-file (org-babel-temp-file "fortran-bin-" org-babel-exeext))
e66ba1df
BG
50 (cmdline (cdr (assoc :cmdline params)))
51 (flags (cdr (assoc :flags params)))
52 (full-body (org-babel-expand-body:fortran body params))
53 (compile
54 (progn
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)) ""))))
666ffc7e
SM
63 (let ((results
64 (org-babel-trim
65 (org-babel-eval
66 (concat tmp-bin-file (if cmdline (concat " " cmdline) "")) ""))))
67 (org-babel-reassemble-table
68 (org-babel-result-cond (cdr (assoc :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 (assoc :colname-names params)) (cdr (assoc :colnames params)))
75 (org-babel-pick-name
76 (cdr (assoc :rowname-names params)) (cdr (assoc :rownames params)))))))
e66ba1df
BG
77
78(defun org-babel-expand-body:fortran (body params)
79 "Expand a block of fortran or fortran code with org-babel according to
80it's header arguments."
81 (let ((vars (mapcar #'cdr (org-babel-get-header params :var)))
82 (main-p (not (string= (cdr (assoc :main params)) "no")))
83 (includes (or (cdr (assoc :includes params))
84 (org-babel-read (org-entry-get nil "includes" t))))
85 (defines (org-babel-read
86 (or (cdr (assoc :defines params))
87 (org-babel-read (org-entry-get nil "defines" t))))))
8223b1d2
BG
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")))
e66ba1df
BG
106
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))
8223b1d2
BG
110 (let ((vars (mapcar #'cdr (org-babel-get-header params :var))))
111 (if vars (error "Cannot use :vars if 'program' statement is present"))
112 body)
e66ba1df
BG
113 (format "program main\n%s\nend program main\n" body)))
114
115(defun org-babel-prep-session:fortran (session params)
116 "This function does nothing as fortran is a compiled language with no
117support for sessions"
8223b1d2 118 (error "Fortran is a compiled languages -- no support for sessions"))
e66ba1df
BG
119
120(defun org-babel-load-session:fortran (session body params)
121 "This function does nothing as fortran is a compiled language with no
122support for sessions"
8223b1d2 123 (error "Fortran is a compiled languages -- no support for sessions"))
e66ba1df
BG
124
125;; helper functions
126
127(defun org-babel-fortran-var-to-fortran (pair)
27e428e7 128 "Convert an elisp val into a string of fortran code specifying a var
e66ba1df
BG
129of 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))
271672fa
BG
147 ;; val is a matrix
148 ((and (listp val) (org-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)))
e66ba1df
BG
153 ((listp val)
154 (format "real, parameter :: %S(%d) = %s\n"
8223b1d2 155 var (length val) (org-babel-fortran-transform-list val)))
e66ba1df
BG
156 (t
157 (error (format "the type of parameter %s is not supported by ob-fortran"
158 var))))))
159
8223b1d2 160(defun org-babel-fortran-transform-list (val)
e66ba1df
BG
161 "Return a fortran representation of enclose syntactic lists."
162 (if (listp val)
8223b1d2 163 (concat "(/" (mapconcat #'org-babel-fortran-transform-list val ", ") "/)")
e66ba1df
BG
164 (format "%S" val)))
165
166(provide 'ob-fortran)
167
168;;; ob-fortran.el ends here