Spelling fixes.
[bpt/emacs.git] / lisp / org / ob-fortran.el
CommitLineData
e66ba1df
BG
1;;; ob-fortran.el --- org-babel functions for fortran
2
c7e9ed79 3;; Copyright (C) 2011-2012 Free Software Foundation, Inc.
e66ba1df 4
c7e9ed79
GM
5;; Authors: Sergey Litvinov
6;; Eric Schulte
e66ba1df
BG
7;; Keywords: literate programming, reproducible research, fortran
8;; Homepage: http://orgmode.org
9;; Version: 7.8.02
10
c7e9ed79
GM
11;; This file is part of GNU Emacs.
12
13;; GNU Emacs is free software: you can redistribute it and/or modify
e66ba1df 14;; it under the terms of the GNU General Public License as published by
c7e9ed79
GM
15;; the Free Software Foundation, either version 3 of the License, or
16;; (at your option) any later version.
17
18;; GNU Emacs is distributed in the hope that it will be useful,
e66ba1df
BG
19;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21;; GNU General Public License for more details.
c7e9ed79 22
e66ba1df 23;; You should have received a copy of the GNU General Public License
c7e9ed79 24;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
e66ba1df
BG
25
26;;; Commentary:
27
28;; Org-Babel support for evaluating fortran code.
c7e9ed79 29;; Based on ob-C.el by Eric Schulte.
e66ba1df
BG
30
31;;; Code:
32(require 'ob)
33(require 'ob-eval)
34(require 'cc-mode)
35
36(declare-function org-entry-get "org"
37 (pom property &optional inherit literal-nil))
38
39(defvar org-babel-tangle-lang-exts)
40(add-to-list 'org-babel-tangle-lang-exts '("fortran" . "F90"))
41
42(defvar org-babel-default-header-args:fortran '())
43
44(defvar org-babel-fortran-compiler "gfortran"
45 "fortran command used to compile a fortran source code file into an
46 executable.")
47
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-"))
52 (cmdline (cdr (assoc :cmdline params)))
53 (flags (cdr (assoc :flags params)))
54 (full-body (org-babel-expand-body:fortran body params))
55 (compile
56 (progn
57 (with-temp-file tmp-src-file (insert full-body))
58 (org-babel-eval
59 (format "%s -o %s %s %s"
60 org-babel-fortran-compiler
61 (org-babel-process-file-name tmp-bin-file)
62 (mapconcat 'identity
63 (if (listp flags) flags (list flags)) " ")
64 (org-babel-process-file-name tmp-src-file)) ""))))
65 ((lambda (results)
66 (org-babel-reassemble-table
67 (if (member "vector" (cdr (assoc :result-params params)))
68 (let ((tmp-file (org-babel-temp-file "f-")))
69 (with-temp-file tmp-file (insert results))
70 (org-babel-import-elisp-from-file tmp-file))
71 (org-babel-read results))
72 (org-babel-pick-name
73 (cdr (assoc :colname-names params)) (cdr (assoc :colnames params)))
74 (org-babel-pick-name
75 (cdr (assoc :rowname-names params)) (cdr (assoc :rownames params)))))
76 (org-babel-trim
77 (org-babel-eval
78 (concat tmp-bin-file (if cmdline (concat " " cmdline) "")) "")))))
79
80(defun org-babel-expand-body:fortran (body params)
81 "Expand a block of fortran or fortran code with org-babel according to
82it'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))))))
90 (mapconcat 'identity
91 (list
92 ;; includes
93 (mapconcat
94 (lambda (inc) (format "#include %s" inc))
95 (if (listp includes) includes (list includes)) "\n")
96 ;; defines
97 (mapconcat
98 (lambda (inc) (format "#define %s" inc))
99 (if (listp defines) defines (list defines)) "\n")
100 ;; body
101 (if main-p
102 (org-babel-fortran-ensure-main-wrap
103 (concat
104 ;; variables
105 (mapconcat 'org-babel-fortran-var-to-fortran vars "\n")
106 body) params)
107 body) "\n") "\n")))
108
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))))
27e428e7 113 (if vars (error "cannot use :vars if 'program' statement is present"))
e66ba1df
BG
114 body)
115 (format "program main\n%s\nend program main\n" body)))
116
117(defun org-babel-prep-session:fortran (session params)
118 "This function does nothing as fortran is a compiled language with no
119support for sessions"
120 (error "fortran is a compiled languages -- no support for sessions"))
121
122(defun org-babel-load-session:fortran (session body params)
123 "This function does nothing as fortran is a compiled language with no
124support for sessions"
125 (error "fortran is a compiled languages -- no support for sessions"))
126
127;; helper functions
128
129(defun org-babel-fortran-var-to-fortran (pair)
27e428e7 130 "Convert an elisp val into a string of fortran code specifying a var
e66ba1df
BG
131of the same value."
132 ;; TODO list support
133 (let ((var (car pair))
134 (val (cdr pair)))
135 (when (symbolp val)
136 (setq val (symbol-name val))
137 (when (= (length val) 1)
138 (setq val (string-to-char val))))
139 (cond
140 ((integerp val)
141 (format "integer, parameter :: %S = %S\n" var val))
142 ((floatp val)
143 (format "real, parameter :: %S = %S\n" var val))
144 ((or (integerp val))
145 (format "character, parameter :: %S = '%S'\n" var val))
146 ((stringp val)
147 (format "character(len=%d), parameter :: %S = '%s'\n"
148 (length val) var val))
149 ((listp val)
150 (format "real, parameter :: %S(%d) = %s\n"
151 var (length val) (ob-fortran-transform-list val)))
152 (t
153 (error (format "the type of parameter %s is not supported by ob-fortran"
154 var))))))
155
156(defun ob-fortran-transform-list (val)
157 "Return a fortran representation of enclose syntactic lists."
158 (if (listp val)
159 (concat "(/" (mapconcat #'ob-fortran-transform-list val ", ") "/)")
160 (format "%S" val)))
161
162(provide 'ob-fortran)
163
164;;; ob-fortran.el ends here